PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/quickedit/js/models/EntityModel.js

http://github.com/drupal/drupal
JavaScript | 329 lines | 264 code | 59 blank | 6 comment | 51 complexity | 14f098c4e6e50b7a00c3e8b99b338550 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.EntityModel = Drupal.quickedit.BaseModel.extend({
  9. defaults: {
  10. el: null,
  11. entityID: null,
  12. entityInstanceID: null,
  13. id: null,
  14. label: null,
  15. fields: null,
  16. isActive: false,
  17. inTempStore: false,
  18. isDirty: false,
  19. isCommitting: false,
  20. state: 'closed',
  21. fieldsInTempStore: [],
  22. reload: false
  23. },
  24. initialize: function initialize() {
  25. this.set('fields', new Drupal.quickedit.FieldCollection());
  26. this.listenTo(this, 'change:state', this.stateChange);
  27. this.listenTo(this.get('fields'), 'change:state', this.fieldStateChange);
  28. Drupal.quickedit.BaseModel.prototype.initialize.call(this);
  29. },
  30. stateChange: function stateChange(entityModel, state, options) {
  31. var to = state;
  32. switch (to) {
  33. case 'closed':
  34. this.set({
  35. isActive: false,
  36. inTempStore: false,
  37. isDirty: false
  38. });
  39. break;
  40. case 'launching':
  41. break;
  42. case 'opening':
  43. entityModel.get('fields').each(function (fieldModel) {
  44. fieldModel.set('state', 'candidate', options);
  45. });
  46. break;
  47. case 'opened':
  48. this.set('isActive', true);
  49. break;
  50. case 'committing':
  51. {
  52. var fields = this.get('fields');
  53. fields.chain().filter(function (fieldModel) {
  54. return _.intersection([fieldModel.get('state')], ['active']).length;
  55. }).each(function (fieldModel) {
  56. fieldModel.set('state', 'candidate');
  57. });
  58. fields.chain().filter(function (fieldModel) {
  59. return _.intersection([fieldModel.get('state')], Drupal.quickedit.app.changedFieldStates).length;
  60. }).each(function (fieldModel) {
  61. fieldModel.set('state', 'saving');
  62. });
  63. break;
  64. }
  65. case 'deactivating':
  66. {
  67. var changedFields = this.get('fields').filter(function (fieldModel) {
  68. return _.intersection([fieldModel.get('state')], ['changed', 'invalid']).length;
  69. });
  70. if ((changedFields.length || this.get('fieldsInTempStore').length) && !options.saved && !options.confirmed) {
  71. this.set('state', 'opened', { confirming: true });
  72. _.defer(function () {
  73. Drupal.quickedit.app.confirmEntityDeactivation(entityModel);
  74. });
  75. } else {
  76. var invalidFields = this.get('fields').filter(function (fieldModel) {
  77. return _.intersection([fieldModel.get('state')], ['invalid']).length;
  78. });
  79. entityModel.set('reload', this.get('fieldsInTempStore').length || invalidFields.length);
  80. entityModel.get('fields').each(function (fieldModel) {
  81. if (_.intersection([fieldModel.get('state')], ['candidate', 'highlighted']).length) {
  82. fieldModel.trigger('change:state', fieldModel, fieldModel.get('state'), options);
  83. } else {
  84. fieldModel.set('state', 'candidate', options);
  85. }
  86. });
  87. }
  88. break;
  89. }
  90. case 'closing':
  91. options.reason = 'stop';
  92. this.get('fields').each(function (fieldModel) {
  93. fieldModel.set({
  94. inTempStore: false,
  95. state: 'inactive'
  96. }, options);
  97. });
  98. break;
  99. }
  100. },
  101. _updateInTempStoreAttributes: function _updateInTempStoreAttributes(entityModel, fieldModel) {
  102. var current = fieldModel.get('state');
  103. var previous = fieldModel.previous('state');
  104. var fieldsInTempStore = entityModel.get('fieldsInTempStore');
  105. if (current === 'saved') {
  106. entityModel.set('inTempStore', true);
  107. fieldModel.set('inTempStore', true);
  108. fieldsInTempStore.push(fieldModel.get('fieldID'));
  109. fieldsInTempStore = _.uniq(fieldsInTempStore);
  110. entityModel.set('fieldsInTempStore', fieldsInTempStore);
  111. } else if (current === 'candidate' && previous === 'inactive') {
  112. fieldModel.set('inTempStore', _.intersection([fieldModel.get('fieldID')], fieldsInTempStore).length > 0);
  113. }
  114. },
  115. fieldStateChange: function fieldStateChange(fieldModel, state) {
  116. var entityModel = this;
  117. var fieldState = state;
  118. switch (this.get('state')) {
  119. case 'closed':
  120. case 'launching':
  121. break;
  122. case 'opening':
  123. _.defer(function () {
  124. entityModel.set('state', 'opened', {
  125. 'accept-field-states': Drupal.quickedit.app.readyFieldStates
  126. });
  127. });
  128. break;
  129. case 'opened':
  130. if (fieldState === 'changed') {
  131. entityModel.set('isDirty', true);
  132. } else {
  133. this._updateInTempStoreAttributes(entityModel, fieldModel);
  134. }
  135. break;
  136. case 'committing':
  137. {
  138. if (fieldState === 'invalid') {
  139. _.defer(function () {
  140. entityModel.set('state', 'opened', { reason: 'invalid' });
  141. });
  142. } else {
  143. this._updateInTempStoreAttributes(entityModel, fieldModel);
  144. }
  145. var options = {
  146. 'accept-field-states': Drupal.quickedit.app.readyFieldStates
  147. };
  148. if (entityModel.set('isCommitting', true, options)) {
  149. entityModel.save({
  150. success: function success() {
  151. entityModel.set({
  152. state: 'deactivating',
  153. isCommitting: false
  154. }, { saved: true });
  155. },
  156. error: function error() {
  157. entityModel.set('isCommitting', false);
  158. entityModel.set('state', 'opened', {
  159. reason: 'networkerror'
  160. });
  161. 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') });
  162. Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message);
  163. }
  164. });
  165. }
  166. break;
  167. }
  168. case 'deactivating':
  169. _.defer(function () {
  170. entityModel.set('state', 'closing', {
  171. 'accept-field-states': Drupal.quickedit.app.readyFieldStates
  172. });
  173. });
  174. break;
  175. case 'closing':
  176. _.defer(function () {
  177. entityModel.set('state', 'closed', {
  178. 'accept-field-states': ['inactive']
  179. });
  180. });
  181. break;
  182. }
  183. },
  184. save: function save(options) {
  185. var entityModel = this;
  186. var entitySaverAjax = Drupal.ajax({
  187. url: Drupal.url('quickedit/entity/' + entityModel.get('entityID')),
  188. error: function error() {
  189. options.error.call(entityModel);
  190. }
  191. });
  192. entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) {
  193. entityModel.get('fields').each(function (fieldModel) {
  194. fieldModel.set('inTempStore', false);
  195. });
  196. entityModel.set('inTempStore', false);
  197. entityModel.set('fieldsInTempStore', []);
  198. if (options.success) {
  199. options.success.call(entityModel);
  200. }
  201. };
  202. entitySaverAjax.execute();
  203. },
  204. validate: function validate(attrs, options) {
  205. var acceptedFieldStates = options['accept-field-states'] || [];
  206. var currentState = this.get('state');
  207. var nextState = attrs.state;
  208. if (currentState !== nextState) {
  209. if (_.indexOf(this.constructor.states, nextState) === -1) {
  210. return '"' + nextState + '" is an invalid state';
  211. }
  212. if (!this._acceptStateChange(currentState, nextState, options)) {
  213. return 'state change not accepted';
  214. }
  215. if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
  216. return 'state change not accepted because fields are not in acceptable state';
  217. }
  218. }
  219. var currentIsCommitting = this.get('isCommitting');
  220. var nextIsCommitting = attrs.isCommitting;
  221. if (currentIsCommitting === false && nextIsCommitting === true) {
  222. if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
  223. return 'isCommitting change not accepted because fields are not in acceptable state';
  224. }
  225. } else if (currentIsCommitting === true && nextIsCommitting === true) {
  226. return 'isCommitting is a mutex, hence only changes are allowed';
  227. }
  228. },
  229. _acceptStateChange: function _acceptStateChange(from, to, context) {
  230. var accept = true;
  231. if (!this.constructor.followsStateSequence(from, to)) {
  232. accept = false;
  233. if (from === 'closing' && to === 'closed') {
  234. accept = true;
  235. } else if (from === 'committing' && to === 'opened' && context.reason && (context.reason === 'invalid' || context.reason === 'networkerror')) {
  236. accept = true;
  237. } else if (from === 'deactivating' && to === 'opened' && context.confirming) {
  238. accept = true;
  239. } else if (from === 'opened' && to === 'deactivating' && context.confirmed) {
  240. accept = true;
  241. }
  242. }
  243. return accept;
  244. },
  245. _fieldsHaveAcceptableStates: function _fieldsHaveAcceptableStates(acceptedFieldStates) {
  246. var accept = true;
  247. if (acceptedFieldStates.length > 0) {
  248. var fieldStates = this.get('fields').pluck('state') || [];
  249. if (_.difference(fieldStates, acceptedFieldStates).length) {
  250. accept = false;
  251. }
  252. }
  253. return accept;
  254. },
  255. destroy: function destroy(options) {
  256. Drupal.quickedit.BaseModel.prototype.destroy.call(this, options);
  257. this.stopListening();
  258. this.get('fields').reset();
  259. },
  260. sync: function sync() {}
  261. }, {
  262. states: ['closed', 'launching', 'opening', 'opened', 'committing', 'deactivating', 'closing'],
  263. followsStateSequence: function followsStateSequence(from, to) {
  264. return _.indexOf(this.states, from) < _.indexOf(this.states, to);
  265. }
  266. });
  267. Drupal.quickedit.EntityCollection = Backbone.Collection.extend({
  268. model: Drupal.quickedit.EntityModel
  269. });
  270. })(_, jQuery, Backbone, Drupal);