formEditor.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 ($, Drupal, _) {
  8. Drupal.quickedit.editors.form = Drupal.quickedit.EditorView.extend({
  9. $formContainer: null,
  10. formSaveAjax: null,
  11. stateChange: function stateChange(fieldModel, state) {
  12. var from = fieldModel.previous('state');
  13. var to = state;
  14. switch (to) {
  15. case 'inactive':
  16. break;
  17. case 'candidate':
  18. if (from !== 'inactive') {
  19. this.removeForm();
  20. }
  21. break;
  22. case 'highlighted':
  23. break;
  24. case 'activating':
  25. if (from !== 'invalid') {
  26. this.loadForm();
  27. }
  28. break;
  29. case 'active':
  30. break;
  31. case 'changed':
  32. break;
  33. case 'saving':
  34. this.save();
  35. break;
  36. case 'saved':
  37. break;
  38. case 'invalid':
  39. this.showValidationErrors();
  40. break;
  41. }
  42. },
  43. getQuickEditUISettings: function getQuickEditUISettings() {
  44. return {
  45. padding: true,
  46. unifiedToolbar: true,
  47. fullWidthToolbar: true,
  48. popup: true
  49. };
  50. },
  51. loadForm: function loadForm() {
  52. var fieldModel = this.fieldModel;
  53. var id = 'quickedit-form-for-' + fieldModel.id.replace(/[/[\]]/g, '_');
  54. var $formContainer = $(Drupal.theme('quickeditFormContainer', {
  55. id: id,
  56. loadingMsg: Drupal.t('Loading…')
  57. }));
  58. this.$formContainer = $formContainer;
  59. $formContainer.find('.quickedit-form').addClass('quickedit-editable quickedit-highlighted quickedit-editing').attr('role', 'dialog');
  60. if (this.$el.css('display') === 'inline') {
  61. $formContainer.prependTo(this.$el.offsetParent());
  62. var pos = this.$el.position();
  63. $formContainer.css('left', pos.left).css('top', pos.top);
  64. } else {
  65. $formContainer.insertBefore(this.$el);
  66. }
  67. var formOptions = {
  68. fieldID: fieldModel.get('fieldID'),
  69. $el: this.$el,
  70. nocssjs: false,
  71. reset: !fieldModel.get('entity').get('inTempStore')
  72. };
  73. Drupal.quickedit.util.form.load(formOptions, function (form, ajax) {
  74. Drupal.AjaxCommands.prototype.insert(ajax, {
  75. data: form,
  76. selector: '#' + id + ' .placeholder'
  77. });
  78. $formContainer.on('formUpdated.quickedit', ':input', function (event) {
  79. var state = fieldModel.get('state');
  80. if (state === 'invalid') {
  81. fieldModel.set('state', 'activating');
  82. } else {
  83. fieldModel.set('state', 'changed');
  84. }
  85. }).on('keypress.quickedit', 'input', function (event) {
  86. if (event.keyCode === 13) {
  87. return false;
  88. }
  89. });
  90. fieldModel.set('state', 'active');
  91. });
  92. },
  93. removeForm: function removeForm() {
  94. if (this.$formContainer === null) {
  95. return;
  96. }
  97. delete this.formSaveAjax;
  98. Drupal.detachBehaviors(this.$formContainer.get(0), null, 'unload');
  99. this.$formContainer.off('change.quickedit', ':input').off('keypress.quickedit', 'input').remove();
  100. this.$formContainer = null;
  101. },
  102. save: function save() {
  103. var $formContainer = this.$formContainer;
  104. var $submit = $formContainer.find('.quickedit-form-submit');
  105. var editorModel = this.model;
  106. var fieldModel = this.fieldModel;
  107. var formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving({
  108. nocssjs: false,
  109. other_view_modes: fieldModel.findOtherViewModes()
  110. }, $submit);
  111. function cleanUpAjax() {
  112. Drupal.quickedit.util.form.unajaxifySaving(formSaveAjax);
  113. formSaveAjax = null;
  114. }
  115. formSaveAjax.commands.quickeditFieldFormSaved = function (ajax, response, status) {
  116. cleanUpAjax();
  117. fieldModel.set('state', 'saved');
  118. fieldModel.set('htmlForOtherViewModes', response.other_view_modes);
  119. _.defer(function () {
  120. fieldModel.set('html', response.data);
  121. });
  122. };
  123. formSaveAjax.commands.quickeditFieldFormValidationErrors = function (ajax, response, status) {
  124. editorModel.set('validationErrors', response.data);
  125. fieldModel.set('state', 'invalid');
  126. };
  127. formSaveAjax.commands.quickeditFieldForm = function (ajax, response, status) {
  128. Drupal.AjaxCommands.prototype.insert(ajax, {
  129. data: response.data,
  130. selector: '#' + $formContainer.attr('id') + ' form'
  131. });
  132. };
  133. $submit.trigger('click.quickedit');
  134. },
  135. showValidationErrors: function showValidationErrors() {
  136. this.$formContainer.find('.quickedit-form').addClass('quickedit-validation-error').find('form').prepend(this.model.get('validationErrors'));
  137. }
  138. });
  139. })(jQuery, Drupal, _);