Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / core / modules / quickedit / js / editors / formEditor.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 ($, Drupal, _) {
9   Drupal.quickedit.editors.form = Drupal.quickedit.EditorView.extend({
10     $formContainer: null,
11
12     formSaveAjax: null,
13
14     stateChange: function stateChange(fieldModel, state) {
15       var from = fieldModel.previous('state');
16       var to = state;
17       switch (to) {
18         case 'inactive':
19           break;
20
21         case 'candidate':
22           if (from !== 'inactive') {
23             this.removeForm();
24           }
25           break;
26
27         case 'highlighted':
28           break;
29
30         case 'activating':
31           if (from !== 'invalid') {
32             this.loadForm();
33           }
34           break;
35
36         case 'active':
37           break;
38
39         case 'changed':
40           break;
41
42         case 'saving':
43           this.save();
44           break;
45
46         case 'saved':
47           break;
48
49         case 'invalid':
50           this.showValidationErrors();
51           break;
52       }
53     },
54     getQuickEditUISettings: function getQuickEditUISettings() {
55       return {
56         padding: true,
57         unifiedToolbar: true,
58         fullWidthToolbar: true,
59         popup: true
60       };
61     },
62     loadForm: function loadForm() {
63       var fieldModel = this.fieldModel;
64
65       var id = 'quickedit-form-for-' + fieldModel.id.replace(/[/[\]]/g, '_');
66
67       var $formContainer = $(Drupal.theme('quickeditFormContainer', {
68         id: id,
69         loadingMsg: Drupal.t('Loading…')
70       }));
71       this.$formContainer = $formContainer;
72       $formContainer.find('.quickedit-form').addClass('quickedit-editable quickedit-highlighted quickedit-editing').attr('role', 'dialog');
73
74       if (this.$el.css('display') === 'inline') {
75         $formContainer.prependTo(this.$el.offsetParent());
76
77         var pos = this.$el.position();
78         $formContainer.css('left', pos.left).css('top', pos.top);
79       } else {
80         $formContainer.insertBefore(this.$el);
81       }
82
83       var formOptions = {
84         fieldID: fieldModel.get('fieldID'),
85         $el: this.$el,
86         nocssjs: false,
87
88         reset: !fieldModel.get('entity').get('inTempStore')
89       };
90       Drupal.quickedit.util.form.load(formOptions, function (form, ajax) {
91         Drupal.AjaxCommands.prototype.insert(ajax, {
92           data: form,
93           selector: '#' + id + ' .placeholder'
94         });
95
96         $formContainer.on('formUpdated.quickedit', ':input', function (event) {
97           var state = fieldModel.get('state');
98
99           if (state === 'invalid') {
100             fieldModel.set('state', 'activating');
101           } else {
102               fieldModel.set('state', 'changed');
103             }
104         }).on('keypress.quickedit', 'input', function (event) {
105           if (event.keyCode === 13) {
106             return false;
107           }
108         });
109
110         fieldModel.set('state', 'active');
111       });
112     },
113     removeForm: function removeForm() {
114       if (this.$formContainer === null) {
115         return;
116       }
117
118       delete this.formSaveAjax;
119
120       Drupal.detachBehaviors(this.$formContainer.get(0), null, 'unload');
121       this.$formContainer.off('change.quickedit', ':input').off('keypress.quickedit', 'input').remove();
122       this.$formContainer = null;
123     },
124     save: function save() {
125       var $formContainer = this.$formContainer;
126       var $submit = $formContainer.find('.quickedit-form-submit');
127       var editorModel = this.model;
128       var fieldModel = this.fieldModel;
129
130       var formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving({
131         nocssjs: false,
132         other_view_modes: fieldModel.findOtherViewModes()
133       }, $submit);
134
135       function cleanUpAjax() {
136         Drupal.quickedit.util.form.unajaxifySaving(formSaveAjax);
137         formSaveAjax = null;
138       }
139
140       formSaveAjax.commands.quickeditFieldFormSaved = function (ajax, response, status) {
141         cleanUpAjax();
142
143         fieldModel.set('state', 'saved');
144
145         fieldModel.set('htmlForOtherViewModes', response.other_view_modes);
146
147         _.defer(function () {
148           fieldModel.set('html', response.data);
149         });
150       };
151
152       formSaveAjax.commands.quickeditFieldFormValidationErrors = function (ajax, response, status) {
153         editorModel.set('validationErrors', response.data);
154         fieldModel.set('state', 'invalid');
155       };
156
157       formSaveAjax.commands.quickeditFieldForm = function (ajax, response, status) {
158         Drupal.AjaxCommands.prototype.insert(ajax, {
159           data: response.data,
160           selector: '#' + $formContainer.attr('id') + ' form'
161         });
162       };
163
164       $submit.trigger('click.quickedit');
165     },
166     showValidationErrors: function showValidationErrors() {
167       this.$formContainer.find('.quickedit-form').addClass('quickedit-validation-error').find('form').prepend(this.model.get('validationErrors'));
168     }
169   });
170 })(jQuery, Drupal, _);