formEditor.js 4.8 KB

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