ajax.es6.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * @file
  3. * Handles AJAX submission and response in Views UI.
  4. */
  5. (function($, Drupal, drupalSettings) {
  6. /**
  7. * Ajax command for highlighting elements.
  8. *
  9. * @param {Drupal.Ajax} [ajax]
  10. * An Ajax object.
  11. * @param {object} response
  12. * The Ajax response.
  13. * @param {string} response.selector
  14. * The selector in question.
  15. * @param {number} [status]
  16. * The HTTP status code.
  17. */
  18. Drupal.AjaxCommands.prototype.viewsHighlight = function(
  19. ajax,
  20. response,
  21. status,
  22. ) {
  23. $('.hilited').removeClass('hilited');
  24. $(response.selector).addClass('hilited');
  25. };
  26. /**
  27. * Ajax command to set the form submit action in the views modal edit form.
  28. *
  29. * @param {Drupal.Ajax} [ajax]
  30. * An Ajax object.
  31. * @param {object} response
  32. * The Ajax response. Contains .url
  33. * @param {string} [status]
  34. * The XHR status code?
  35. */
  36. Drupal.AjaxCommands.prototype.viewsSetForm = function(
  37. ajax,
  38. response,
  39. status,
  40. ) {
  41. const $form = $('.js-views-ui-dialog form');
  42. // Identify the button that was clicked so that .ajaxSubmit() can use it.
  43. // We need to do this for both .click() and .mousedown() since JavaScript
  44. // code might trigger either behavior.
  45. const $submitButtons = $form
  46. .find('input[type=submit].js-form-submit, button.js-form-submit')
  47. .once('views-ajax-submit');
  48. $submitButtons.on('click mousedown', function() {
  49. this.form.clk = this;
  50. });
  51. $form.once('views-ajax-submit').each(function() {
  52. const $form = $(this);
  53. const elementSettings = {
  54. url: response.url,
  55. event: 'submit',
  56. base: $form.attr('id'),
  57. element: this,
  58. };
  59. const ajaxForm = Drupal.ajax(elementSettings);
  60. ajaxForm.$form = $form;
  61. });
  62. };
  63. /**
  64. * Ajax command to show certain buttons in the views edit form.
  65. *
  66. * @param {Drupal.Ajax} [ajax]
  67. * An Ajax object.
  68. * @param {object} response
  69. * The Ajax response.
  70. * @param {bool} response.changed
  71. * Whether the state changed for the buttons or not.
  72. * @param {number} [status]
  73. * The HTTP status code.
  74. */
  75. Drupal.AjaxCommands.prototype.viewsShowButtons = function(
  76. ajax,
  77. response,
  78. status,
  79. ) {
  80. $('div.views-edit-view div.form-actions').removeClass('js-hide');
  81. if (response.changed) {
  82. $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
  83. }
  84. };
  85. /**
  86. * Ajax command for triggering preview.
  87. *
  88. * @param {Drupal.Ajax} [ajax]
  89. * An Ajax object.
  90. * @param {object} [response]
  91. * The Ajax response.
  92. * @param {number} [status]
  93. * The HTTP status code.
  94. */
  95. Drupal.AjaxCommands.prototype.viewsTriggerPreview = function(
  96. ajax,
  97. response,
  98. status,
  99. ) {
  100. if ($('input#edit-displays-live-preview').is(':checked')) {
  101. $('#preview-submit').trigger('click');
  102. }
  103. };
  104. /**
  105. * Ajax command to replace the title of a page.
  106. *
  107. * @param {Drupal.Ajax} [ajax]
  108. * An Ajax object.
  109. * @param {object} response
  110. * The Ajax response.
  111. * @param {string} response.siteName
  112. * The site name.
  113. * @param {string} response.title
  114. * The new page title.
  115. * @param {number} [status]
  116. * The HTTP status code.
  117. */
  118. Drupal.AjaxCommands.prototype.viewsReplaceTitle = function(
  119. ajax,
  120. response,
  121. status,
  122. ) {
  123. const doc = document;
  124. // For the <title> element, make a best-effort attempt to replace the page
  125. // title and leave the site name alone. If the theme doesn't use the site
  126. // name in the <title> element, this will fail.
  127. const oldTitle = doc.title;
  128. // Escape the site name, in case it has special characters in it, so we can
  129. // use it in our regex.
  130. const escapedSiteName = response.siteName.replace(
  131. /[-[\]{}()*+?.,\\^$|#\s]/g,
  132. '\\$&',
  133. );
  134. const re = new RegExp(`.+ (.) ${escapedSiteName}`);
  135. doc.title = oldTitle.replace(
  136. re,
  137. `${response.title} $1 ${response.siteName}`,
  138. );
  139. $('h1.page-title').text(response.title);
  140. };
  141. /**
  142. * Get rid of irritating tabledrag messages.
  143. *
  144. * @return {Array}
  145. * An array of messages. Always empty array, to get rid of the messages.
  146. */
  147. Drupal.theme.tableDragChangedWarning = function() {
  148. return [];
  149. };
  150. /**
  151. * Trigger preview when the "live preview" checkbox is checked.
  152. *
  153. * @type {Drupal~behavior}
  154. *
  155. * @prop {Drupal~behaviorAttach} attach
  156. * Attaches behavior to trigger live preview if the live preview option is
  157. * checked.
  158. */
  159. Drupal.behaviors.livePreview = {
  160. attach(context) {
  161. $('input#edit-displays-live-preview', context)
  162. .once('views-ajax')
  163. .on('click', function() {
  164. if ($(this).is(':checked')) {
  165. $('#preview-submit').trigger('click');
  166. }
  167. });
  168. },
  169. };
  170. /**
  171. * Sync preview display.
  172. *
  173. * @type {Drupal~behavior}
  174. *
  175. * @prop {Drupal~behaviorAttach} attach
  176. * Attaches behavior to sync the preview display when needed.
  177. */
  178. Drupal.behaviors.syncPreviewDisplay = {
  179. attach(context) {
  180. $('#views-tabset a')
  181. .once('views-ajax')
  182. .on('click', function() {
  183. const href = $(this).attr('href');
  184. // Cut of #views-tabset.
  185. const displayId = href.substr(11);
  186. // Set the form element.
  187. $('#views-live-preview #preview-display-id').val(displayId);
  188. });
  189. },
  190. };
  191. /**
  192. * Ajax behaviors for the views_ui module.
  193. *
  194. * @type {Drupal~behavior}
  195. *
  196. * @prop {Drupal~behaviorAttach} attach
  197. * Attaches ajax behaviors to the elements with the classes in question.
  198. */
  199. Drupal.behaviors.viewsAjax = {
  200. collapseReplaced: false,
  201. attach(context, settings) {
  202. const baseElementSettings = {
  203. event: 'click',
  204. progress: { type: 'fullscreen' },
  205. };
  206. // Bind AJAX behaviors to all items showing the class.
  207. $('a.views-ajax-link', context)
  208. .once('views-ajax')
  209. .each(function() {
  210. const elementSettings = baseElementSettings;
  211. elementSettings.base = $(this).attr('id');
  212. elementSettings.element = this;
  213. // Set the URL to go to the anchor.
  214. if ($(this).attr('href')) {
  215. elementSettings.url = $(this).attr('href');
  216. }
  217. Drupal.ajax(elementSettings);
  218. });
  219. $('div#views-live-preview a')
  220. .once('views-ajax')
  221. .each(function() {
  222. // We don't bind to links without a URL.
  223. if (!$(this).attr('href')) {
  224. return true;
  225. }
  226. const elementSettings = baseElementSettings;
  227. // Set the URL to go to the anchor.
  228. elementSettings.url = $(this).attr('href');
  229. if (
  230. Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
  231. 'admin/structure/views'
  232. ) {
  233. return true;
  234. }
  235. elementSettings.wrapper = 'views-preview-wrapper';
  236. elementSettings.method = 'replaceWith';
  237. elementSettings.base = $(this).attr('id');
  238. elementSettings.element = this;
  239. Drupal.ajax(elementSettings);
  240. });
  241. // Within a live preview, make exposed widget form buttons re-trigger the
  242. // Preview button.
  243. // @todo Revisit this after fixing Views UI to display a Preview outside
  244. // of the main Edit form.
  245. $('div#views-live-preview input[type=submit]')
  246. .once('views-ajax')
  247. .each(function(event) {
  248. $(this).on('click', function() {
  249. this.form.clk = this;
  250. return true;
  251. });
  252. const elementSettings = baseElementSettings;
  253. // Set the URL to go to the anchor.
  254. elementSettings.url = $(this.form).attr('action');
  255. if (
  256. Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
  257. 'admin/structure/views'
  258. ) {
  259. return true;
  260. }
  261. elementSettings.wrapper = 'views-preview-wrapper';
  262. elementSettings.method = 'replaceWith';
  263. elementSettings.event = 'click';
  264. elementSettings.base = $(this).attr('id');
  265. elementSettings.element = this;
  266. Drupal.ajax(elementSettings);
  267. });
  268. },
  269. };
  270. })(jQuery, Drupal, drupalSettings);