PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/nadejhdaa/service8.germes.online
JavaScript | 325 lines | 261 code | 58 blank | 6 comment | 52 complexity | 69e3dbfb668d3c7cd6ceb09f47548567 MD5 | raw file
Possible License(s): LGPL-2.1, CC-BY-SA-3.0, MIT, BSD-3-Clause
  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', { reason: 'networkerror' });
  159. 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') });
  160. Drupal.quickedit.util.networkErrorModal(Drupal.t('Network problem!'), message);
  161. }
  162. });
  163. }
  164. break;
  165. }
  166. case 'deactivating':
  167. _.defer(function () {
  168. entityModel.set('state', 'closing', {
  169. 'accept-field-states': Drupal.quickedit.app.readyFieldStates
  170. });
  171. });
  172. break;
  173. case 'closing':
  174. _.defer(function () {
  175. entityModel.set('state', 'closed', {
  176. 'accept-field-states': ['inactive']
  177. });
  178. });
  179. break;
  180. }
  181. },
  182. save: function save(options) {
  183. var entityModel = this;
  184. var entitySaverAjax = Drupal.ajax({
  185. url: Drupal.url('quickedit/entity/' + entityModel.get('entityID')),
  186. error: function error() {
  187. options.error.call(entityModel);
  188. }
  189. });
  190. entitySaverAjax.commands.quickeditEntitySaved = function (ajax, response, status) {
  191. entityModel.get('fields').each(function (fieldModel) {
  192. fieldModel.set('inTempStore', false);
  193. });
  194. entityModel.set('inTempStore', false);
  195. entityModel.set('fieldsInTempStore', []);
  196. if (options.success) {
  197. options.success.call(entityModel);
  198. }
  199. };
  200. entitySaverAjax.execute();
  201. },
  202. validate: function validate(attrs, options) {
  203. var acceptedFieldStates = options['accept-field-states'] || [];
  204. var currentState = this.get('state');
  205. var nextState = attrs.state;
  206. if (currentState !== nextState) {
  207. if (_.indexOf(this.constructor.states, nextState) === -1) {
  208. return '"' + nextState + '" is an invalid state';
  209. }
  210. if (!this._acceptStateChange(currentState, nextState, options)) {
  211. return 'state change not accepted';
  212. } else if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
  213. return 'state change not accepted because fields are not in acceptable state';
  214. }
  215. }
  216. var currentIsCommitting = this.get('isCommitting');
  217. var nextIsCommitting = attrs.isCommitting;
  218. if (currentIsCommitting === false && nextIsCommitting === true) {
  219. if (!this._fieldsHaveAcceptableStates(acceptedFieldStates)) {
  220. return 'isCommitting change not accepted because fields are not in acceptable state';
  221. }
  222. } else if (currentIsCommitting === true && nextIsCommitting === true) {
  223. return 'isCommitting is a mutex, hence only changes are allowed';
  224. }
  225. },
  226. _acceptStateChange: function _acceptStateChange(from, to, context) {
  227. var accept = true;
  228. if (!this.constructor.followsStateSequence(from, to)) {
  229. accept = false;
  230. if (from === 'closing' && to === 'closed') {
  231. accept = true;
  232. } else if (from === 'committing' && to === 'opened' && context.reason && (context.reason === 'invalid' || context.reason === 'networkerror')) {
  233. accept = true;
  234. } else if (from === 'deactivating' && to === 'opened' && context.confirming) {
  235. accept = true;
  236. } else if (from === 'opened' && to === 'deactivating' && context.confirmed) {
  237. accept = true;
  238. }
  239. }
  240. return accept;
  241. },
  242. _fieldsHaveAcceptableStates: function _fieldsHaveAcceptableStates(acceptedFieldStates) {
  243. var accept = true;
  244. if (acceptedFieldStates.length > 0) {
  245. var fieldStates = this.get('fields').pluck('state') || [];
  246. if (_.difference(fieldStates, acceptedFieldStates).length) {
  247. accept = false;
  248. }
  249. }
  250. return accept;
  251. },
  252. destroy: function destroy(options) {
  253. Drupal.quickedit.BaseModel.prototype.destroy.call(this, options);
  254. this.stopListening();
  255. this.get('fields').reset();
  256. },
  257. sync: function sync() {}
  258. }, {
  259. states: ['closed', 'launching', 'opening', 'opened', 'committing', 'deactivating', 'closing'],
  260. followsStateSequence: function followsStateSequence(from, to) {
  261. return _.indexOf(this.states, from) < _.indexOf(this.states, to);
  262. }
  263. });
  264. Drupal.quickedit.EntityCollection = Backbone.Collection.extend({
  265. model: Drupal.quickedit.EntityModel
  266. });
  267. })(_, jQuery, Backbone, Drupal);