ad5c1317771b6a3b99caeed3731a4ed2cc4acc6d
[yaffs-website] / web / core / modules / quickedit / js / views / EditorView.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Backbone, Drupal) {
9   Drupal.quickedit.EditorView = Backbone.View.extend({
10     initialize: function initialize(options) {
11       this.fieldModel = options.fieldModel;
12       this.listenTo(this.fieldModel, 'change:state', this.stateChange);
13     },
14     remove: function remove() {
15       this.setElement();
16       Backbone.View.prototype.remove.call(this);
17     },
18     getEditedElement: function getEditedElement() {
19       return this.$el;
20     },
21     getQuickEditUISettings: function getQuickEditUISettings() {
22       return { padding: false, unifiedToolbar: false, fullWidthToolbar: false, popup: false };
23     },
24     stateChange: function stateChange(fieldModel, state) {
25       var from = fieldModel.previous('state');
26       var to = state;
27       switch (to) {
28         case 'inactive':
29           break;
30
31         case 'candidate':
32           if (from === 'invalid') {
33             this.removeValidationErrors();
34           }
35           break;
36
37         case 'highlighted':
38           break;
39
40         case 'activating':
41           var loadDependencies = function loadDependencies(callback) {
42             callback();
43           };
44           loadDependencies(function () {
45             fieldModel.set('state', 'active');
46           });
47           break;
48
49         case 'active':
50           break;
51
52         case 'changed':
53           break;
54
55         case 'saving':
56           if (from === 'invalid') {
57             this.removeValidationErrors();
58           }
59           this.save();
60           break;
61
62         case 'saved':
63           break;
64
65         case 'invalid':
66           this.showValidationErrors();
67           break;
68       }
69     },
70     revert: function revert() {},
71     save: function save() {
72       var fieldModel = this.fieldModel;
73       var editorModel = this.model;
74       var backstageId = 'quickedit_backstage-' + this.fieldModel.id.replace(/[\/\[\]\_\s]/g, '-');
75
76       function fillAndSubmitForm(value) {
77         var $form = $('#' + backstageId).find('form');
78
79         $form.find(':input[type!="hidden"][type!="submit"]:not(select)').not('[name$="\\[summary\\]"]').val(value);
80
81         $form.find('.quickedit-form-submit').trigger('click.quickedit');
82       }
83
84       var formOptions = {
85         fieldID: this.fieldModel.get('fieldID'),
86         $el: this.$el,
87         nocssjs: true,
88         other_view_modes: fieldModel.findOtherViewModes(),
89
90         reset: !this.fieldModel.get('entity').get('inTempStore')
91       };
92
93       var self = this;
94       Drupal.quickedit.util.form.load(formOptions, function (form, ajax) {
95         var $backstage = $(Drupal.theme('quickeditBackstage', { id: backstageId })).appendTo('body');
96
97         var $form = $(form).appendTo($backstage);
98
99         $form.prop('novalidate', true);
100         var $submit = $form.find('.quickedit-form-submit');
101         self.formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving(formOptions, $submit);
102
103         function removeHiddenForm() {
104           Drupal.quickedit.util.form.unajaxifySaving(self.formSaveAjax);
105           delete self.formSaveAjax;
106           $backstage.remove();
107         }
108
109         self.formSaveAjax.commands.quickeditFieldFormSaved = function (ajax, response, status) {
110           removeHiddenForm();
111
112           fieldModel.set('state', 'saved');
113
114           fieldModel.set('htmlForOtherViewModes', response.other_view_modes);
115
116           fieldModel.set('html', response.data);
117         };
118
119         self.formSaveAjax.commands.quickeditFieldFormValidationErrors = function (ajax, response, status) {
120           removeHiddenForm();
121           editorModel.set('validationErrors', response.data);
122           fieldModel.set('state', 'invalid');
123         };
124
125         self.formSaveAjax.commands.quickeditFieldForm = function () {};
126
127         fillAndSubmitForm(editorModel.get('currentValue'));
128       });
129     },
130     showValidationErrors: function showValidationErrors() {
131       var $errors = $('<div class="quickedit-validation-errors"></div>').append(this.model.get('validationErrors'));
132       this.getEditedElement().addClass('quickedit-validation-error').after($errors);
133     },
134     removeValidationErrors: function removeValidationErrors() {
135       this.getEditedElement().removeClass('quickedit-validation-error').next('.quickedit-validation-errors').remove();
136     }
137   });
138 })(jQuery, Backbone, Drupal);