drupal/core/modules/quickedit/js/models/FieldModel.js

89 lines
2.4 KiB
JavaScript

/**
* DO NOT EDIT THIS FILE.
* See the following change record for more information,
* https://www.drupal.org/node/2815083
* @preserve
**/
(function (_, Backbone, Drupal) {
Drupal.quickedit.FieldModel = Drupal.quickedit.BaseModel.extend({
defaults: {
el: null,
fieldID: null,
id: null,
entity: null,
metadata: null,
acceptStateChange: null,
logicalFieldID: null,
state: 'inactive',
isChanged: false,
inTempStore: false,
html: null,
htmlForOtherViewModes: null
},
initialize(options) {
this.set('html', options.el.outerHTML);
this.get('entity').get('fields').add(this);
this.set('logicalFieldID', this.get('fieldID').split('/').slice(0, 4).join('/'));
Drupal.quickedit.BaseModel.prototype.initialize.call(this, options);
},
destroy(options) {
if (this.get('state') !== 'inactive') {
throw new Error('FieldModel cannot be destroyed if it is not inactive state.');
}
Drupal.quickedit.BaseModel.prototype.destroy.call(this, options);
},
sync() {},
validate(attrs, options) {
const current = this.get('state');
const next = attrs.state;
if (current !== next) {
if (_.indexOf(this.constructor.states, next) === -1) {
return `"${next}" is an invalid state`;
}
if (!this.get('acceptStateChange')(current, next, options, this)) {
return 'state change not accepted';
}
}
},
getEntityID() {
return this.get('fieldID').split('/').slice(0, 2).join('/');
},
getViewMode() {
return this.get('fieldID').split('/').pop();
},
findOtherViewModes() {
const currentField = this;
const otherViewModes = [];
Drupal.quickedit.collections.fields.where({
logicalFieldID: currentField.get('logicalFieldID')
}).forEach(field => {
if (field !== currentField && field.get('fieldID') !== currentField.get('fieldID')) {
otherViewModes.push(field.getViewMode());
}
});
return otherViewModes;
}
}, {
states: ['inactive', 'candidate', 'highlighted', 'activating', 'active', 'changed', 'saving', 'saved', 'invalid'],
followsStateSequence(from, to) {
return _.indexOf(this.states, from) < _.indexOf(this.states, to);
}
});
Drupal.quickedit.FieldCollection = Backbone.Collection.extend({
model: Drupal.quickedit.FieldModel
});
})(_, Backbone, Drupal);