annotate core/modules/views_ui/js/ajax.es6.js @ 19:fa3358dc1485 tip

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