Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/quickedit/js/views/AppView.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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 var reload = false; | |
10 | |
11 Drupal.quickedit.AppView = Backbone.View.extend({ | |
12 initialize: function initialize(options) { | |
13 this.activeFieldStates = ['activating', 'active']; | |
14 this.singleFieldStates = ['highlighted', 'activating', 'active']; | |
15 this.changedFieldStates = ['changed', 'saving', 'saved', 'invalid']; | |
16 this.readyFieldStates = ['candidate', 'highlighted']; | |
17 | |
18 this.listenTo(options.entitiesCollection, 'change:state', this.appStateChange); | |
19 this.listenTo(options.entitiesCollection, 'change:isActive', this.enforceSingleActiveEntity); | |
20 | |
21 this.listenTo(options.fieldsCollection, 'change:state', this.editorStateChange); | |
22 | |
23 this.listenTo(options.fieldsCollection, 'change:html', this.renderUpdatedField); | |
24 this.listenTo(options.fieldsCollection, 'change:html', this.propagateUpdatedField); | |
25 | |
26 this.listenTo(options.fieldsCollection, 'add', this.rerenderedFieldToCandidate); | |
27 | |
28 this.listenTo(options.fieldsCollection, 'destroy', this.teardownEditor); | |
29 }, | |
30 appStateChange: function appStateChange(entityModel, state) { | |
31 var app = this; | |
32 var entityToolbarView = void 0; | |
33 switch (state) { | |
34 case 'launching': | |
35 reload = false; | |
36 | |
37 entityToolbarView = new Drupal.quickedit.EntityToolbarView({ | |
38 model: entityModel, | |
39 appModel: this.model | |
40 }); | |
41 entityModel.toolbarView = entityToolbarView; | |
42 | |
43 entityModel.get('fields').each(function (fieldModel) { | |
44 app.setupEditor(fieldModel); | |
45 }); | |
46 | |
47 _.defer(function () { | |
48 entityModel.set('state', 'opening'); | |
49 }); | |
50 break; | |
51 | |
52 case 'closed': | |
53 entityToolbarView = entityModel.toolbarView; | |
54 | |
55 entityModel.get('fields').each(function (fieldModel) { | |
56 app.teardownEditor(fieldModel); | |
57 }); | |
58 | |
59 if (entityToolbarView) { | |
60 entityToolbarView.remove(); | |
61 delete entityModel.toolbarView; | |
62 } | |
63 | |
64 if (reload) { | |
65 reload = false; | |
66 location.reload(); | |
67 } | |
68 break; | |
69 } | |
70 }, | |
71 acceptEditorStateChange: function acceptEditorStateChange(from, to, context, fieldModel) { | |
72 var accept = true; | |
73 | |
74 if (context && (context.reason === 'stop' || context.reason === 'rerender')) { | |
75 if (from === 'candidate' && to === 'inactive') { | |
76 accept = true; | |
77 } | |
78 } else { | |
79 if (!Drupal.quickedit.FieldModel.followsStateSequence(from, to)) { | |
80 accept = false; | |
81 | |
82 if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { | |
83 accept = true; | |
84 } else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { | |
85 accept = true; | |
86 } else if (from === 'highlighted' && to === 'candidate') { | |
87 accept = true; | |
88 } else if (from === 'saved' && to === 'candidate') { | |
89 accept = true; | |
90 } else if (from === 'invalid' && to === 'saving') { | |
91 accept = true; | |
92 } else if (from === 'invalid' && to === 'activating') { | |
93 accept = true; | |
94 } | |
95 } | |
96 | |
97 if (accept) { | |
98 var activeField = void 0; | |
99 var activeFieldState = void 0; | |
100 | |
101 if ((this.readyFieldStates.indexOf(from) !== -1 || from === 'invalid') && this.activeFieldStates.indexOf(to) !== -1) { | |
102 activeField = this.model.get('activeField'); | |
103 if (activeField && activeField !== fieldModel) { | |
104 activeFieldState = activeField.get('state'); | |
105 | |
106 if (this.activeFieldStates.indexOf(activeFieldState) !== -1) { | |
107 activeField.set('state', 'candidate'); | |
108 } else if (activeFieldState === 'changed' || activeFieldState === 'invalid') { | |
109 activeField.set('state', 'saving'); | |
110 } | |
111 | |
112 if (from === 'invalid') { | |
113 this.model.set('activeField', fieldModel); | |
114 accept = false; | |
115 } | |
116 } | |
117 } else if (_.indexOf(this.activeFieldStates, from) !== -1 && to === 'candidate') { | |
118 if (context && context.reason === 'mouseleave') { | |
119 accept = false; | |
120 } | |
121 } else if ((from === 'changed' || from === 'invalid') && to === 'candidate') { | |
122 if (context && context.reason === 'mouseleave') { | |
123 accept = false; | |
124 } else if (context && context.confirmed) { | |
125 accept = true; | |
126 } | |
127 } | |
128 } | |
129 } | |
130 | |
131 return accept; | |
132 }, | |
133 setupEditor: function setupEditor(fieldModel) { | |
134 var entityModel = fieldModel.get('entity'); | |
135 var entityToolbarView = entityModel.toolbarView; | |
136 | |
137 var fieldToolbarRoot = entityToolbarView.getToolbarRoot(); | |
138 | |
139 var editorName = fieldModel.get('metadata').editor; | |
140 var editorModel = new Drupal.quickedit.EditorModel(); | |
141 var editorView = new Drupal.quickedit.editors[editorName]({ | |
142 el: $(fieldModel.get('el')), | |
143 model: editorModel, | |
144 fieldModel: fieldModel | |
145 }); | |
146 | |
147 var toolbarView = new Drupal.quickedit.FieldToolbarView({ | |
148 el: fieldToolbarRoot, | |
149 model: fieldModel, | |
150 $editedElement: $(editorView.getEditedElement()), | |
151 editorView: editorView, | |
152 entityModel: entityModel | |
153 }); | |
154 | |
155 var decorationView = new Drupal.quickedit.FieldDecorationView({ | |
156 el: $(editorView.getEditedElement()), | |
157 model: fieldModel, | |
158 editorView: editorView | |
159 }); | |
160 | |
161 fieldModel.editorView = editorView; | |
162 fieldModel.toolbarView = toolbarView; | |
163 fieldModel.decorationView = decorationView; | |
164 }, | |
165 teardownEditor: function teardownEditor(fieldModel) { | |
166 if (typeof fieldModel.editorView === 'undefined') { | |
167 return; | |
168 } | |
169 | |
170 fieldModel.toolbarView.remove(); | |
171 delete fieldModel.toolbarView; | |
172 | |
173 fieldModel.decorationView.remove(); | |
174 delete fieldModel.decorationView; | |
175 | |
176 fieldModel.editorView.remove(); | |
177 delete fieldModel.editorView; | |
178 }, | |
179 confirmEntityDeactivation: function confirmEntityDeactivation(entityModel) { | |
180 var that = this; | |
181 var discardDialog = void 0; | |
182 | |
183 function closeDiscardDialog(action) { | |
184 discardDialog.close(action); | |
185 | |
186 that.model.set('activeModal', null); | |
187 | |
188 if (action === 'save') { | |
189 entityModel.set('state', 'committing', { confirmed: true }); | |
190 } else { | |
191 entityModel.set('state', 'deactivating', { confirmed: true }); | |
192 | |
193 if (entityModel.get('reload')) { | |
194 reload = true; | |
195 entityModel.set('reload', false); | |
196 } | |
197 } | |
198 } | |
199 | |
200 if (!this.model.get('activeModal')) { | |
201 var $unsavedChanges = $('<div>' + Drupal.t('You have unsaved changes') + '</div>'); | |
202 discardDialog = Drupal.dialog($unsavedChanges.get(0), { | |
203 title: Drupal.t('Discard changes?'), | |
204 dialogClass: 'quickedit-discard-modal', | |
205 resizable: false, | |
206 buttons: [{ | |
207 text: Drupal.t('Save'), | |
208 click: function click() { | |
209 closeDiscardDialog('save'); | |
210 }, | |
211 | |
212 primary: true | |
213 }, { | |
214 text: Drupal.t('Discard changes'), | |
215 click: function click() { | |
216 closeDiscardDialog('discard'); | |
217 } | |
218 }], | |
219 | |
220 closeOnEscape: false, | |
221 create: function create() { | |
222 $(this).parent().find('.ui-dialog-titlebar-close').remove(); | |
223 }, | |
224 | |
225 beforeClose: false, | |
226 close: function close(event) { | |
227 $(event.target).remove(); | |
228 } | |
229 }); | |
230 this.model.set('activeModal', discardDialog); | |
231 | |
232 discardDialog.showModal(); | |
233 } | |
234 }, | |
235 editorStateChange: function editorStateChange(fieldModel, state) { | |
236 var from = fieldModel.previous('state'); | |
237 var to = state; | |
238 | |
239 if (_.indexOf(this.singleFieldStates, to) !== -1 && this.model.get('highlightedField') !== fieldModel) { | |
240 this.model.set('highlightedField', fieldModel); | |
241 } else if (this.model.get('highlightedField') === fieldModel && to === 'candidate') { | |
242 this.model.set('highlightedField', null); | |
243 } | |
244 | |
245 if (_.indexOf(this.activeFieldStates, to) !== -1 && this.model.get('activeField') !== fieldModel) { | |
246 this.model.set('activeField', fieldModel); | |
247 } else if (this.model.get('activeField') === fieldModel && to === 'candidate') { | |
248 if (from === 'changed' || from === 'invalid') { | |
249 fieldModel.editorView.revert(); | |
250 } | |
251 this.model.set('activeField', null); | |
252 } | |
253 }, | |
254 renderUpdatedField: function renderUpdatedField(fieldModel, html, options) { | |
255 var $fieldWrapper = $(fieldModel.get('el')); | |
256 var $context = $fieldWrapper.parent(); | |
257 | |
258 var renderField = function renderField() { | |
259 fieldModel.destroy(); | |
260 | |
261 $fieldWrapper.replaceWith(html); | |
262 | |
263 Drupal.attachBehaviors($context.get(0)); | |
264 }; | |
265 | |
266 if (!options.propagation) { | |
267 _.defer(function () { | |
268 fieldModel.set('state', 'candidate'); | |
269 | |
270 _.defer(function () { | |
271 fieldModel.set('state', 'inactive', { reason: 'rerender' }); | |
272 | |
273 renderField(); | |
274 }); | |
275 }); | |
276 } else { | |
277 renderField(); | |
278 } | |
279 }, | |
280 propagateUpdatedField: function propagateUpdatedField(updatedField, html, options) { | |
281 if (options.propagation) { | |
282 return; | |
283 } | |
284 | |
285 var htmlForOtherViewModes = updatedField.get('htmlForOtherViewModes'); | |
286 Drupal.quickedit.collections.fields.where({ logicalFieldID: updatedField.get('logicalFieldID') }).forEach(function (field) { | |
287 if (field === updatedField) {} else if (field.getViewMode() === updatedField.getViewMode()) { | |
288 field.set('html', updatedField.get('html')); | |
289 } else if (field.getViewMode() in htmlForOtherViewModes) { | |
290 field.set('html', htmlForOtherViewModes[field.getViewMode()], { propagation: true }); | |
291 } | |
292 }); | |
293 }, | |
294 rerenderedFieldToCandidate: function rerenderedFieldToCandidate(fieldModel) { | |
295 var activeEntity = Drupal.quickedit.collections.entities.findWhere({ isActive: true }); | |
296 | |
297 if (!activeEntity) { | |
298 return; | |
299 } | |
300 | |
301 if (fieldModel.get('entity') === activeEntity) { | |
302 this.setupEditor(fieldModel); | |
303 fieldModel.set('state', 'candidate'); | |
304 } | |
305 }, | |
306 enforceSingleActiveEntity: function enforceSingleActiveEntity(changedEntityModel) { | |
307 if (changedEntityModel.get('isActive') === false) { | |
308 return; | |
309 } | |
310 | |
311 changedEntityModel.collection.chain().filter(function (entityModel) { | |
312 return entityModel.get('isActive') === true && entityModel !== changedEntityModel; | |
313 }).each(function (entityModel) { | |
314 entityModel.set('state', 'deactivating'); | |
315 }); | |
316 } | |
317 }); | |
318 })(jQuery, _, Backbone, Drupal); |