2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function (_, $, Backbone, Drupal) {
9 Drupal.quickedit.EntityModel = Drupal.quickedit.BaseModel.extend({
15 entityInstanceID: null,
33 fieldsInTempStore: [],
38 initialize: function initialize() {
39 this.set('fields', new Drupal.quickedit.FieldCollection());
41 this.listenTo(this, 'change:state', this.stateChange);
43 this.listenTo(this.get('fields'), 'change:state', this.fieldStateChange);
45 Drupal.quickedit.BaseModel.prototype.initialize.call(this);
47 stateChange: function stateChange(entityModel, state, options) {
62 entityModel.get('fields').each(function (fieldModel) {
63 fieldModel.set('state', 'candidate', options);
68 this.set('isActive', true);
72 var fields = this.get('fields');
74 fields.chain().filter(function (fieldModel) {
75 return _.intersection([fieldModel.get('state')], ['active']).length;
76 }).each(function (fieldModel) {
77 fieldModel.set('state', 'candidate');
80 fields.chain().filter(function (fieldModel) {
81 return _.intersection([fieldModel.get('state')], Drupal.quickedit.app.changedFieldStates).length;
82 }).each(function (fieldModel) {
83 fieldModel.set('state', 'saving');
88 var changedFields = this.get('fields').filter(function (fieldModel) {
89 return _.intersection([fieldModel.get('state')], ['changed', 'invalid']).length;
92 if ((changedFields.length || this.get('fieldsInTempStore').length) && !options.saved && !options.confirmed) {
93 this.set('state', 'opened', { confirming: true });
96 Drupal.quickedit.app.confirmEntityDeactivation(entityModel);
99 var invalidFields = this.get('fields').filter(function (fieldModel) {
100 return _.intersection([fieldModel.get('state')], ['invalid']).length;
103 entityModel.set('reload', this.get('fieldsInTempStore').length || invalidFields.length);
105 entityModel.get('fields').each(function (fieldModel) {
106 if (_.intersection([fieldModel.get('state')], ['candidate', 'highlighted']).length) {
107 fieldModel.trigger('change:state', fieldModel, fieldModel.get('state'), options);
109 fieldModel.set('state', 'candidate', options);
116 options.reason = 'stop';
117 this.get('fields').each(function (fieldModel) {
126 _updateInTempStoreAttributes: function _updateInTempStoreAttributes(entityModel, fieldModel) {
127 var current = fieldModel.get('state');
128 var previous = fieldModel.previous('state');
129 var fieldsInTempStore = entityModel.get('fieldsInTempStore');
131 if (current === 'saved') {
132 entityModel.set('inTempStore', true);
134 fieldModel.set('inTempStore', true);
136 fieldsInTempStore.push(fieldModel.get('fieldID'));
137 fieldsInTempStore = _.uniq(fieldsInTempStore);
138 entityModel.set('fieldsInTempStore', fieldsInTempStore);
139 } else if (current === 'candidate' && previous === 'inactive') {
140 fieldModel.set('inTempStore', _.intersection([fieldModel.get('fieldID')], fieldsInTempStore).length > 0);
143 fieldStateChange: function fieldStateChange(fieldModel, state) {
144 var entityModel = this;
145 var fieldState = state;
147 switch (this.get('state')) {
153 _.defer(function () {
154 entityModel.set('state', 'opened', {
155 'accept-field-states': Drupal.quickedit.app.readyFieldStates
161 if (fieldState === 'changed') {
162 entityModel.set('isDirty', true);
164 this._updateInTempStoreAttributes(entityModel, fieldModel);
169 if (fieldState === 'invalid') {
170 _.defer(function () {
171 entityModel.set('state', 'opened', { reason: 'invalid' });
174 this._updateInTempStoreAttributes(entityModel, fieldModel);
178 'accept-field-states': Drupal.quickedit.app.readyFieldStates
180 if (entityModel.set('isCommitting', true, options)) {
182 success: function success() {
184 state: 'deactivating',
188 error: function error() {
189 entityModel.set('isCommitting', false);
191 entityModel.set('state', 'opened', { reason: 'networkerror' });
193 var message = Drupal.t('Your changes to <q>@entity-title</q> could not be saved, either due to a website problem or a network connection problem.<br>Please try again.', { '@entity-title': entityModel.get('label') });
194 Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message);
201 _.defer(function () {
202 entityModel.set('state', 'closing', {
203 'accept-field-states': Drupal.quickedit.app.readyFieldStates
209 _.defer(function () {
210 entityModel.set('state', 'closed', {
211 'accept-field-states': ['inactive']
217 save: function save(options) {
218 var entityModel = this;
220 var entitySaverAjax = Drupal.ajax({
221 url: Drupal.url('quickedit/entity/' + entityModel.get('entityID')),
222 error: function error() {
223 options.error.call(entityModel);
227 entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) {
228 entityModel.get('fields').each(function (fieldModel) {
229 fieldModel.set('inTempStore', false);
231 entityModel.set('inTempStore', false);
232 entityModel.set('fieldsInTempStore', []);
234 if (options.success) {
235 options.success.call(entityModel);
239 entitySaverAjax.execute();
241 validate: function validate(attrs, options) {
242 var acceptedFieldStates = options['accept-field-states'] || [];
244 var currentState = this.get('state');
245 var nextState = attrs.state;
246 if (currentState !== nextState) {
247 if (_.indexOf(this.constructor.states, nextState) === -1) {
248 return '"' + nextState + '" is an invalid state';
251 if (!this._acceptStateChange(currentState, nextState, options)) {
252 return 'state change not accepted';
253 } else if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
254 return 'state change not accepted because fields are not in acceptable state';
258 var currentIsCommitting = this.get('isCommitting');
259 var nextIsCommitting = attrs.isCommitting;
260 if (currentIsCommitting === false && nextIsCommitting === true) {
261 if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
262 return 'isCommitting change not accepted because fields are not in acceptable state';
264 } else if (currentIsCommitting === true && nextIsCommitting === true) {
265 return 'isCommitting is a mutex, hence only changes are allowed';
268 _acceptStateChange: function _acceptStateChange(from, to, context) {
271 if (!this.constructor.followsStateSequence(from, to)) {
274 if (from === 'closing' && to === 'closed') {
276 } else if (from === 'committing' && to === 'opened' && context.reason && (context.reason === 'invalid' || context.reason === 'networkerror')) {
278 } else if (from === 'deactivating' && to === 'opened' && context.confirming) {
280 } else if (from === 'opened' && to === 'deactivating' && context.confirmed) {
287 _fieldsHaveAcceptableStates: function _fieldsHaveAcceptableStates(acceptedFieldStates) {
290 if (acceptedFieldStates.length > 0) {
291 var fieldStates = this.get('fields').pluck('state') || [];
293 if (_.difference(fieldStates, acceptedFieldStates).length) {
300 destroy: function destroy(options) {
301 Drupal.quickedit.BaseModel.prototype.destroy.call(this, options);
303 this.stopListening();
305 this.get('fields').reset();
307 sync: function sync() {}
309 states: ['closed', 'launching', 'opening', 'opened', 'committing', 'deactivating', 'closing'],
311 followsStateSequence: function followsStateSequence(from, to) {
312 return _.indexOf(this.states, from) < _.indexOf(this.states, to);
316 Drupal.quickedit.EntityCollection = Backbone.Collection.extend({
317 model: Drupal.quickedit.EntityModel
319 })(_, jQuery, Backbone, Drupal);