PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/drupal/drupal
JavaScript | 92 lines | 67 code | 19 blank | 6 comment | 7 complexity | 3f439d7aaaa032cf670f70665a8877f1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. (function (_, Backbone, Drupal) {
  8. Drupal.quickedit.FieldModel = Drupal.quickedit.BaseModel.extend({
  9. defaults: {
  10. el: null,
  11. fieldID: null,
  12. id: null,
  13. entity: null,
  14. metadata: null,
  15. acceptStateChange: null,
  16. logicalFieldID: null,
  17. state: 'inactive',
  18. isChanged: false,
  19. inTempStore: false,
  20. html: null,
  21. htmlForOtherViewModes: null
  22. },
  23. initialize: function initialize(options) {
  24. this.set('html', options.el.outerHTML);
  25. this.get('entity').get('fields').add(this);
  26. this.set('logicalFieldID', this.get('fieldID').split('/').slice(0, 4).join('/'));
  27. Drupal.quickedit.BaseModel.prototype.initialize.call(this, options);
  28. },
  29. destroy: function destroy(options) {
  30. if (this.get('state') !== 'inactive') {
  31. throw new Error('FieldModel cannot be destroyed if it is not inactive state.');
  32. }
  33. Drupal.quickedit.BaseModel.prototype.destroy.call(this, options);
  34. },
  35. sync: function sync() {},
  36. validate: function validate(attrs, options) {
  37. var current = this.get('state');
  38. var next = attrs.state;
  39. if (current !== next) {
  40. if (_.indexOf(this.constructor.states, next) === -1) {
  41. return '"' + next + '" is an invalid state';
  42. }
  43. if (!this.get('acceptStateChange')(current, next, options, this)) {
  44. return 'state change not accepted';
  45. }
  46. }
  47. },
  48. getEntityID: function getEntityID() {
  49. return this.get('fieldID').split('/').slice(0, 2).join('/');
  50. },
  51. getViewMode: function getViewMode() {
  52. return this.get('fieldID').split('/').pop();
  53. },
  54. findOtherViewModes: function findOtherViewModes() {
  55. var currentField = this;
  56. var otherViewModes = [];
  57. Drupal.quickedit.collections.fields.where({ logicalFieldID: currentField.get('logicalFieldID') }).forEach(function (field) {
  58. if (field !== currentField && field.get('fieldID') !== currentField.get('fieldID')) {
  59. otherViewModes.push(field.getViewMode());
  60. }
  61. });
  62. return otherViewModes;
  63. }
  64. }, {
  65. states: ['inactive', 'candidate', 'highlighted', 'activating', 'active', 'changed', 'saving', 'saved', 'invalid'],
  66. followsStateSequence: function followsStateSequence(from, to) {
  67. return _.indexOf(this.states, from) < _.indexOf(this.states, to);
  68. }
  69. });
  70. Drupal.quickedit.FieldCollection = Backbone.Collection.extend({
  71. model: Drupal.quickedit.FieldModel
  72. });
  73. })(_, Backbone, Drupal);