Updated to Drupal 8.5. Core Media not yet in use.
[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 { padding: true, unifiedToolbar: true, fullWidthToolbar: true, popup: true };
56     },
57     loadForm: function loadForm() {
58       var fieldModel = this.fieldModel;
59
60       var id = 'quickedit-form-for-' + fieldModel.id.replace(/[/[\]]/g, '_');
61
62       var $formContainer = $(Drupal.theme('quickeditFormContainer', {
63         id: id,
64         loadingMsg: Drupal.t('Loading…')
65       }));
66       this.$formContainer = $formContainer;
67       $formContainer.find('.quickedit-form').addClass('quickedit-editable quickedit-highlighted quickedit-editing').attr('role', 'dialog');
68
69       if (this.$el.css('display') === 'inline') {
70         $formContainer.prependTo(this.$el.offsetParent());
71
72         var pos = this.$el.position();
73         $formContainer.css('left', pos.left).css('top', pos.top);
74       } else {
75         $formContainer.insertBefore(this.$el);
76       }
77
78       var formOptions = {
79         fieldID: fieldModel.get('fieldID'),
80         $el: this.$el,
81         nocssjs: false,
82
83         reset: !fieldModel.get('entity').get('inTempStore')
84       };
85       Drupal.quickedit.util.form.load(formOptions, function (form, ajax) {
86         Drupal.AjaxCommands.prototype.insert(ajax, {
87           data: form,
88           selector: '#' + id + ' .placeholder'
89         });
90
91         $formContainer.on('formUpdated.quickedit', ':input', function (event) {
92           var state = fieldModel.get('state');
93
94           if (state === 'invalid') {
95             fieldModel.set('state', 'activating');
96           } else {
97               fieldModel.set('state', 'changed');
98             }
99         }).on('keypress.quickedit', 'input', function (event) {
100           if (event.keyCode === 13) {
101             return false;
102           }
103         });
104
105         fieldModel.set('state', 'active');
106       });
107     },
108     removeForm: function removeForm() {
109       if (this.$formContainer === null) {
110         return;
111       }
112
113       delete this.formSaveAjax;
114
115       Drupal.detachBehaviors(this.$formContainer.get(0), null, 'unload');
116       this.$formContainer.off('change.quickedit', ':input').off('keypress.quickedit', 'input').remove();
117       this.$formContainer = null;
118     },
119     save: function save() {
120       var $formContainer = this.$formContainer;
121       var $submit = $formContainer.find('.quickedit-form-submit');
122       var editorModel = this.model;
123       var fieldModel = this.fieldModel;
124
125       function cleanUpAjax() {
126         Drupal.quickedit.util.form.unajaxifySaving(formSaveAjax);
127         formSaveAjax = null;
128       }
129
130       var formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving({
131         nocssjs: false,
132         other_view_modes: fieldModel.findOtherViewModes()
133       }, $submit);
134
135       formSaveAjax.commands.quickeditFieldFormSaved = function (ajax, response, status) {
136         cleanUpAjax();
137
138         fieldModel.set('state', 'saved');
139
140         fieldModel.set('htmlForOtherViewModes', response.other_view_modes);
141
142         _.defer(function () {
143           fieldModel.set('html', response.data);
144         });
145       };
146
147       formSaveAjax.commands.quickeditFieldFormValidationErrors = function (ajax, response, status) {
148         editorModel.set('validationErrors', response.data);
149         fieldModel.set('state', 'invalid');
150       };
151
152       formSaveAjax.commands.quickeditFieldForm = function (ajax, response, status) {
153         Drupal.AjaxCommands.prototype.insert(ajax, {
154           data: response.data,
155           selector: '#' + $formContainer.attr('id') + ' form'
156         });
157       };
158
159       $submit.trigger('click.quickedit');
160     },
161     showValidationErrors: function showValidationErrors() {
162       this.$formContainer.find('.quickedit-form').addClass('quickedit-validation-error').find('form').prepend(this.model.get('validationErrors'));
163     }
164   });
165 })(jQuery, Drupal, _);