root / drupal7 / misc / ajax.js @ cee0424c
1 | 85ad3d82 | Assos Assos | (function ($) { |
---|---|---|---|
2 | |||
3 | /**
|
||
4 | * Provides Ajax page updating via jQuery $.ajax (Asynchronous JavaScript and XML).
|
||
5 | *
|
||
6 | * Ajax is a method of making a request via JavaScript while viewing an HTML
|
||
7 | * page. The request returns an array of commands encoded in JSON, which is
|
||
8 | * then executed to make any changes that are necessary to the page.
|
||
9 | *
|
||
10 | * Drupal uses this file to enhance form elements with #ajax['path'] and
|
||
11 | * #ajax['wrapper'] properties. If set, this file will automatically be included
|
||
12 | * to provide Ajax capabilities.
|
||
13 | */
|
||
14 | |||
15 | Drupal.ajax = Drupal.ajax || {}; |
||
16 | |||
17 | 08f5d39b | Assos Assos | Drupal.settings.urlIsAjaxTrusted = Drupal.settings.urlIsAjaxTrusted || {}; |
18 | |||
19 | 85ad3d82 | Assos Assos | /**
|
20 | * Attaches the Ajax behavior to each Ajax form element.
|
||
21 | */
|
||
22 | Drupal.behaviors.AJAX = { |
||
23 | attach: function (context, settings) { |
||
24 | // Load all Ajax behaviors specified in the settings.
|
||
25 | for (var base in settings.ajax) { |
||
26 | if (!$('#' + base + '.ajax-processed').length) { |
||
27 | var element_settings = settings.ajax[base];
|
||
28 | |||
29 | if (typeof element_settings.selector == 'undefined') { |
||
30 | element_settings.selector = '#' + base;
|
||
31 | } |
||
32 | $(element_settings.selector).each(function () { |
||
33 | element_settings.element = this;
|
||
34 | Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); |
||
35 | }); |
||
36 | |||
37 | $('#' + base).addClass('ajax-processed'); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | // Bind Ajax behaviors to all items showing the class.
|
||
42 | $('.use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () { |
||
43 | var element_settings = {};
|
||
44 | // Clicked links look better with the throbber than the progress bar.
|
||
45 | element_settings.progress = { 'type': 'throbber' }; |
||
46 | |||
47 | // For anchor tags, these will go to the target of the anchor rather
|
||
48 | // than the usual location.
|
||
49 | if ($(this).attr('href')) { |
||
50 | element_settings.url = $(this).attr('href'); |
||
51 | element_settings.event = 'click';
|
||
52 | } |
||
53 | var base = $(this).attr('id'); |
||
54 | Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); |
||
55 | }); |
||
56 | |||
57 | // This class means to submit the form to the action using Ajax.
|
||
58 | $('.use-ajax-submit:not(.ajax-processed)').addClass('ajax-processed').each(function () { |
||
59 | var element_settings = {};
|
||
60 | |||
61 | // Ajax submits specified in this manner automatically submit to the
|
||
62 | // normal form action.
|
||
63 | element_settings.url = $(this.form).attr('action'); |
||
64 | // Form submit button clicks need to tell the form what was clicked so
|
||
65 | // it gets passed in the POST request.
|
||
66 | element_settings.setClick = true;
|
||
67 | // Form buttons use the 'click' event rather than mousedown.
|
||
68 | element_settings.event = 'click';
|
||
69 | // Clicked form buttons look better with the throbber than the progress bar.
|
||
70 | element_settings.progress = { 'type': 'throbber' }; |
||
71 | |||
72 | var base = $(this).attr('id'); |
||
73 | Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); |
||
74 | }); |
||
75 | } |
||
76 | }; |
||
77 | |||
78 | /**
|
||
79 | * Ajax object.
|
||
80 | *
|
||
81 | * All Ajax objects on a page are accessible through the global Drupal.ajax
|
||
82 | * object and are keyed by the submit button's ID. You can access them from
|
||
83 | * your module's JavaScript file to override properties or functions.
|
||
84 | *
|
||
85 | * For example, if your Ajax enabled button has the ID 'edit-submit', you can
|
||
86 | * redefine the function that is called to insert the new content like this
|
||
87 | * (inside a Drupal.behaviors attach block):
|
||
88 | * @code
|
||
89 | * Drupal.behaviors.myCustomAJAXStuff = {
|
||
90 | * attach: function (context, settings) {
|
||
91 | * Drupal.ajax['edit-submit'].commands.insert = function (ajax, response, status) {
|
||
92 | * new_content = $(response.data);
|
||
93 | * $('#my-wrapper').append(new_content);
|
||
94 | * alert('New content was appended to #my-wrapper');
|
||
95 | * }
|
||
96 | * }
|
||
97 | * };
|
||
98 | * @endcode
|
||
99 | */
|
||
100 | Drupal.ajax = function (base, element, element_settings) { |
||
101 | var defaults = {
|
||
102 | url: 'system/ajax', |
||
103 | event: 'mousedown', |
||
104 | keypress: true, |
||
105 | selector: '#' + base, |
||
106 | effect: 'none', |
||
107 | speed: 'none', |
||
108 | method: 'replaceWith', |
||
109 | progress: {
|
||
110 | type: 'throbber', |
||
111 | message: Drupal.t('Please wait...') |
||
112 | }, |
||
113 | submit: {
|
||
114 | 'js': true |
||
115 | } |
||
116 | }; |
||
117 | |||
118 | $.extend(this, defaults, element_settings); |
||
119 | |||
120 | this.element = element;
|
||
121 | this.element_settings = element_settings;
|
||
122 | |||
123 | // Replacing 'nojs' with 'ajax' in the URL allows for an easy method to let
|
||
124 | // the server detect when it needs to degrade gracefully.
|
||
125 | // There are five scenarios to check for:
|
||
126 | // 1. /nojs/
|
||
127 | // 2. /nojs$ - The end of a URL string.
|
||
128 | // 3. /nojs? - Followed by a query (with clean URLs enabled).
|
||
129 | // E.g.: path/nojs?destination=foobar
|
||
130 | // 4. /nojs& - Followed by a query (without clean URLs enabled).
|
||
131 | // E.g.: ?q=path/nojs&destination=foobar
|
||
132 | // 5. /nojs# - Followed by a fragment.
|
||
133 | // E.g.: path/nojs#myfragment
|
||
134 | this.url = element_settings.url.replace(/\/nojs(\/|$|\?|&|#)/g, '/ajax$1'); |
||
135 | 08f5d39b | Assos Assos | // If the 'nojs' version of the URL is trusted, also trust the 'ajax' version.
|
136 | if (Drupal.settings.urlIsAjaxTrusted[element_settings.url]) {
|
||
137 | Drupal.settings.urlIsAjaxTrusted[this.url] = true; |
||
138 | } |
||
139 | |||
140 | 85ad3d82 | Assos Assos | this.wrapper = '#' + element_settings.wrapper; |
141 | |||
142 | // If there isn't a form, jQuery.ajax() will be used instead, allowing us to
|
||
143 | // bind Ajax to links as well.
|
||
144 | if (this.element.form) { |
||
145 | this.form = $(this.element.form); |
||
146 | } |
||
147 | |||
148 | // Set the options for the ajaxSubmit function.
|
||
149 | // The 'this' variable will not persist inside of the options object.
|
||
150 | var ajax = this; |
||
151 | ajax.options = { |
||
152 | cee0424c | Assos Assos | url: Drupal.sanitizeAjaxUrl(ajax.url),
|
153 | 85ad3d82 | Assos Assos | data: ajax.submit,
|
154 | beforeSerialize: function (element_settings, options) { |
||
155 | return ajax.beforeSerialize(element_settings, options);
|
||
156 | }, |
||
157 | beforeSubmit: function (form_values, element_settings, options) { |
||
158 | ajax.ajaxing = true;
|
||
159 | return ajax.beforeSubmit(form_values, element_settings, options);
|
||
160 | }, |
||
161 | beforeSend: function (xmlhttprequest, options) { |
||
162 | ajax.ajaxing = true;
|
||
163 | return ajax.beforeSend(xmlhttprequest, options);
|
||
164 | }, |
||
165 | 08f5d39b | Assos Assos | success: function (response, status, xmlhttprequest) { |
166 | 85ad3d82 | Assos Assos | // Sanity check for browser support (object expected).
|
167 | // When using iFrame uploads, responses must be returned as a string.
|
||
168 | if (typeof response == 'string') { |
||
169 | response = $.parseJSON(response);
|
||
170 | } |
||
171 | 08f5d39b | Assos Assos | |
172 | // Prior to invoking the response's commands, verify that they can be
|
||
173 | // trusted by checking for a response header. See
|
||
174 | // ajax_set_verification_header() for details.
|
||
175 | // - Empty responses are harmless so can bypass verification. This avoids
|
||
176 | // an alert message for server-generated no-op responses that skip Ajax
|
||
177 | // rendering.
|
||
178 | // - Ajax objects with trusted URLs (e.g., ones defined server-side via
|
||
179 | // #ajax) can bypass header verification. This is especially useful for
|
||
180 | // Ajax with multipart forms. Because IFRAME transport is used, the
|
||
181 | // response headers cannot be accessed for verification.
|
||
182 | if (response !== null && !Drupal.settings.urlIsAjaxTrusted[ajax.url]) { |
||
183 | if (xmlhttprequest.getResponseHeader('X-Drupal-Ajax-Token') !== '1') { |
||
184 | var customMessage = Drupal.t("The response failed verification so will not be processed."); |
||
185 | return ajax.error(xmlhttprequest, ajax.url, customMessage);
|
||
186 | } |
||
187 | } |
||
188 | |||
189 | 85ad3d82 | Assos Assos | return ajax.success(response, status);
|
190 | }, |
||
191 | 08f5d39b | Assos Assos | complete: function (xmlhttprequest, status) { |
192 | 85ad3d82 | Assos Assos | ajax.ajaxing = false;
|
193 | if (status == 'error' || status == 'parsererror') { |
||
194 | 08f5d39b | Assos Assos | return ajax.error(xmlhttprequest, ajax.url);
|
195 | 85ad3d82 | Assos Assos | } |
196 | }, |
||
197 | dataType: 'json', |
||
198 | cee0424c | Assos Assos | jsonp: false, |
199 | 85ad3d82 | Assos Assos | type: 'POST' |
200 | }; |
||
201 | |||
202 | 6b24a280 | Assos Assos | // For multipart forms (e.g., file uploads), jQuery Form targets the form
|
203 | // submission to an iframe instead of using an XHR object. The initial "src"
|
||
204 | // of the iframe, prior to the form submission, is set to options.iframeSrc.
|
||
205 | // "about:blank" is the semantically correct, standards-compliant, way to
|
||
206 | // initialize a blank iframe; however, some old IE versions (possibly only 6)
|
||
207 | // incorrectly report a mixed content warning when iframes with an
|
||
208 | // "about:blank" src are added to a parent document with an https:// origin.
|
||
209 | // jQuery Form works around this by defaulting to "javascript:false" instead,
|
||
210 | // but that breaks on Chrome 83, so here we force the semantically correct
|
||
211 | // behavior for all browsers except old IE.
|
||
212 | // @see https://www.drupal.org/project/drupal/issues/3143016
|
||
213 | // @see https://github.com/jquery-form/form/blob/df9cb101b9c9c085c8d75ad980c7ff1cf62063a1/jquery.form.js#L68
|
||
214 | // @see https://bugs.chromium.org/p/chromium/issues/detail?id=1084874
|
||
215 | // @see https://html.spec.whatwg.org/multipage/browsers.html#creating-browsing-contexts
|
||
216 | // @see https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
|
||
217 | if (navigator.userAgent.indexOf("MSIE") === -1) { |
||
218 | ajax.options.iframeSrc = 'about:blank';
|
||
219 | } |
||
220 | |||
221 | 85ad3d82 | Assos Assos | // Bind the ajaxSubmit function to the element event.
|
222 | $(ajax.element).bind(element_settings.event, function (event) { |
||
223 | 08f5d39b | Assos Assos | if (!Drupal.settings.urlIsAjaxTrusted[ajax.url] && !Drupal.urlIsLocal(ajax.url)) {
|
224 | throw new Error(Drupal.t('The callback URL is not local and not trusted: !url', {'!url': ajax.url})); |
||
225 | } |
||
226 | 85ad3d82 | Assos Assos | return ajax.eventResponse(this, event); |
227 | }); |
||
228 | |||
229 | // If necessary, enable keyboard submission so that Ajax behaviors
|
||
230 | // can be triggered through keyboard input as well as e.g. a mousedown
|
||
231 | // action.
|
||
232 | if (element_settings.keypress) {
|
||
233 | $(ajax.element).keypress(function (event) { |
||
234 | return ajax.keypressResponse(this, event); |
||
235 | }); |
||
236 | } |
||
237 | |||
238 | // If necessary, prevent the browser default action of an additional event.
|
||
239 | // For example, prevent the browser default action of a click, even if the
|
||
240 | // AJAX behavior binds to mousedown.
|
||
241 | if (element_settings.prevent) {
|
||
242 | $(ajax.element).bind(element_settings.prevent, false); |
||
243 | } |
||
244 | }; |
||
245 | |||
246 | /**
|
||
247 | * Handle a key press.
|
||
248 | *
|
||
249 | * The Ajax object will, if instructed, bind to a key press response. This
|
||
250 | * will test to see if the key press is valid to trigger this event and
|
||
251 | * if it is, trigger it for us and prevent other keypresses from triggering.
|
||
252 | * In this case we're handling RETURN and SPACEBAR keypresses (event codes 13
|
||
253 | * and 32. RETURN is often used to submit a form when in a textfield, and
|
||
254 | * SPACE is often used to activate an element without submitting.
|
||
255 | */
|
||
256 | Drupal.ajax.prototype.keypressResponse = function (element, event) { |
||
257 | // Create a synonym for this to reduce code confusion.
|
||
258 | var ajax = this; |
||
259 | |||
260 | // Detect enter key and space bar and allow the standard response for them,
|
||
261 | // except for form elements of type 'text' and 'textarea', where the
|
||
262 | // spacebar activation causes inappropriate activation if #ajax['keypress'] is
|
||
263 | // TRUE. On a text-type widget a space should always be a space.
|
||
264 | if (event.which == 13 || (event.which == 32 && element.type != 'text' && element.type != 'textarea')) { |
||
265 | $(ajax.element_settings.element).trigger(ajax.element_settings.event);
|
||
266 | return false; |
||
267 | } |
||
268 | }; |
||
269 | |||
270 | /**
|
||
271 | * Handle an event that triggers an Ajax response.
|
||
272 | *
|
||
273 | * When an event that triggers an Ajax response happens, this method will
|
||
274 | * perform the actual Ajax call. It is bound to the event using
|
||
275 | * bind() in the constructor, and it uses the options specified on the
|
||
276 | * ajax object.
|
||
277 | */
|
||
278 | Drupal.ajax.prototype.eventResponse = function (element, event) { |
||
279 | // Create a synonym for this to reduce code confusion.
|
||
280 | var ajax = this; |
||
281 | |||
282 | // Do not perform another ajax command if one is already in progress.
|
||
283 | if (ajax.ajaxing) {
|
||
284 | return false; |
||
285 | } |
||
286 | |||
287 | try {
|
||
288 | if (ajax.form) {
|
||
289 | // If setClick is set, we must set this to ensure that the button's
|
||
290 | // value is passed.
|
||
291 | if (ajax.setClick) {
|
||
292 | // Mark the clicked button. 'form.clk' is a special variable for
|
||
293 | // ajaxSubmit that tells the system which element got clicked to
|
||
294 | // trigger the submit. Without it there would be no 'op' or
|
||
295 | // equivalent.
|
||
296 | element.form.clk = element; |
||
297 | } |
||
298 | |||
299 | ajax.form.ajaxSubmit(ajax.options); |
||
300 | } |
||
301 | else {
|
||
302 | ajax.beforeSerialize(ajax.element, ajax.options); |
||
303 | $.ajax(ajax.options);
|
||
304 | } |
||
305 | } |
||
306 | catch (e) {
|
||
307 | // Unset the ajax.ajaxing flag here because it won't be unset during
|
||
308 | // the complete response.
|
||
309 | ajax.ajaxing = false;
|
||
310 | alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message); |
||
311 | } |
||
312 | |||
313 | // For radio/checkbox, allow the default event. On IE, this means letting
|
||
314 | // it actually check the box.
|
||
315 | if (typeof element.type != 'undefined' && (element.type == 'checkbox' || element.type == 'radio')) { |
||
316 | return true; |
||
317 | } |
||
318 | else {
|
||
319 | return false; |
||
320 | } |
||
321 | |||
322 | }; |
||
323 | |||
324 | /**
|
||
325 | * Handler for the form serialization.
|
||
326 | *
|
||
327 | * Runs before the beforeSend() handler (see below), and unlike that one, runs
|
||
328 | * before field data is collected.
|
||
329 | */
|
||
330 | Drupal.ajax.prototype.beforeSerialize = function (element, options) { |
||
331 | // Allow detaching behaviors to update field values before collecting them.
|
||
332 | // This is only needed when field values are added to the POST data, so only
|
||
333 | // when there is a form such that this.form.ajaxSubmit() is used instead of
|
||
334 | // $.ajax(). When there is no form and $.ajax() is used, beforeSerialize()
|
||
335 | // isn't called, but don't rely on that: explicitly check this.form.
|
||
336 | if (this.form) { |
||
337 | var settings = this.settings || Drupal.settings; |
||
338 | Drupal.detachBehaviors(this.form, settings, 'serialize'); |
||
339 | } |
||
340 | |||
341 | // Prevent duplicate HTML ids in the returned markup.
|
||
342 | // @see drupal_html_id()
|
||
343 | options.data['ajax_html_ids[]'] = [];
|
||
344 | $('[id]').each(function () { |
||
345 | options.data['ajax_html_ids[]'].push(this.id); |
||
346 | }); |
||
347 | |||
348 | // Allow Drupal to return new JavaScript and CSS files to load without
|
||
349 | // returning the ones already loaded.
|
||
350 | // @see ajax_base_page_theme()
|
||
351 | // @see drupal_get_css()
|
||
352 | // @see drupal_get_js()
|
||
353 | options.data['ajax_page_state[theme]'] = Drupal.settings.ajaxPageState.theme;
|
||
354 | options.data['ajax_page_state[theme_token]'] = Drupal.settings.ajaxPageState.theme_token;
|
||
355 | for (var key in Drupal.settings.ajaxPageState.css) { |
||
356 | options.data['ajax_page_state[css][' + key + ']'] = 1; |
||
357 | } |
||
358 | for (var key in Drupal.settings.ajaxPageState.js) { |
||
359 | options.data['ajax_page_state[js][' + key + ']'] = 1; |
||
360 | } |
||
361 | }; |
||
362 | |||
363 | /**
|
||
364 | * Modify form values prior to form submission.
|
||
365 | */
|
||
366 | Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) { |
||
367 | // This function is left empty to make it simple to override for modules
|
||
368 | // that wish to add functionality here.
|
||
369 | }; |
||
370 | |||
371 | /**
|
||
372 | * Prepare the Ajax request before it is sent.
|
||
373 | */
|
||
374 | Drupal.ajax.prototype.beforeSend = function (xmlhttprequest, options) { |
||
375 | // For forms without file inputs, the jQuery Form plugin serializes the form
|
||
376 | // values, and then calls jQuery's $.ajax() function, which invokes this
|
||
377 | // handler. In this circumstance, options.extraData is never used. For forms
|
||
378 | // with file inputs, the jQuery Form plugin uses the browser's normal form
|
||
379 | // submission mechanism, but captures the response in a hidden IFRAME. In this
|
||
380 | // circumstance, it calls this handler first, and then appends hidden fields
|
||
381 | // to the form to submit the values in options.extraData. There is no simple
|
||
382 | // way to know which submission mechanism will be used, so we add to extraData
|
||
383 | // regardless, and allow it to be ignored in the former case.
|
||
384 | if (this.form) { |
||
385 | options.extraData = options.extraData || {}; |
||
386 | |||
387 | // Let the server know when the IFRAME submission mechanism is used. The
|
||
388 | // server can use this information to wrap the JSON response in a TEXTAREA,
|
||
389 | // as per http://jquery.malsup.com/form/#file-upload.
|
||
390 | options.extraData.ajax_iframe_upload = '1';
|
||
391 | |||
392 | // The triggering element is about to be disabled (see below), but if it
|
||
393 | // contains a value (e.g., a checkbox, textfield, select, etc.), ensure that
|
||
394 | // value is included in the submission. As per above, submissions that use
|
||
395 | // $.ajax() are already serialized prior to the element being disabled, so
|
||
396 | // this is only needed for IFRAME submissions.
|
||
397 | var v = $.fieldValue(this.element); |
||
398 | if (v !== null) { |
||
399 | f842d52a | Julien Enselme | options.extraData[this.element.name] = Drupal.checkPlain(v);
|
400 | 85ad3d82 | Assos Assos | } |
401 | } |
||
402 | |||
403 | // Disable the element that received the change to prevent user interface
|
||
404 | // interaction while the Ajax request is in progress. ajax.ajaxing prevents
|
||
405 | // the element from triggering a new request, but does not prevent the user
|
||
406 | // from changing its value.
|
||
407 | $(this.element).addClass('progress-disabled').attr('disabled', true); |
||
408 | |||
409 | // Insert progressbar or throbber.
|
||
410 | if (this.progress.type == 'bar') { |
||
411 | var progressBar = new Drupal.progressBar('ajax-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback)); |
||
412 | if (this.progress.message) { |
||
413 | progressBar.setProgress(-1, this.progress.message); |
||
414 | } |
||
415 | if (this.progress.url) { |
||
416 | progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500); |
||
417 | } |
||
418 | this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar'); |
||
419 | this.progress.object = progressBar;
|
||
420 | $(this.element).after(this.progress.element); |
||
421 | } |
||
422 | else if (this.progress.type == 'throbber') { |
||
423 | this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber"> </div></div>'); |
||
424 | if (this.progress.message) { |
||
425 | $('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>'); |
||
426 | } |
||
427 | $(this.element).after(this.progress.element); |
||
428 | } |
||
429 | }; |
||
430 | |||
431 | /**
|
||
432 | * Handler for the form redirection completion.
|
||
433 | */
|
||
434 | Drupal.ajax.prototype.success = function (response, status) { |
||
435 | // Remove the progress element.
|
||
436 | if (this.progress.element) { |
||
437 | $(this.progress.element).remove(); |
||
438 | } |
||
439 | if (this.progress.object) { |
||
440 | this.progress.object.stopMonitoring();
|
||
441 | } |
||
442 | $(this.element).removeClass('progress-disabled').removeAttr('disabled'); |
||
443 | |||
444 | Drupal.freezeHeight(); |
||
445 | |||
446 | for (var i in response) { |
||
447 | if (response.hasOwnProperty(i) && response[i]['command'] && this.commands[response[i]['command']]) { |
||
448 | this.commands[response[i]['command']](this, response[i], status); |
||
449 | } |
||
450 | } |
||
451 | |||
452 | // Reattach behaviors, if they were detached in beforeSerialize(). The
|
||
453 | // attachBehaviors() called on the new content from processing the response
|
||
454 | // commands is not sufficient, because behaviors from the entire form need
|
||
455 | // to be reattached.
|
||
456 | if (this.form) { |
||
457 | var settings = this.settings || Drupal.settings; |
||
458 | Drupal.attachBehaviors(this.form, settings);
|
||
459 | } |
||
460 | |||
461 | Drupal.unfreezeHeight(); |
||
462 | |||
463 | // Remove any response-specific settings so they don't get used on the next
|
||
464 | // call by mistake.
|
||
465 | this.settings = null; |
||
466 | }; |
||
467 | |||
468 | /**
|
||
469 | * Build an effect object which tells us how to apply the effect when adding new HTML.
|
||
470 | */
|
||
471 | Drupal.ajax.prototype.getEffect = function (response) { |
||
472 | var type = response.effect || this.effect; |
||
473 | var speed = response.speed || this.speed; |
||
474 | |||
475 | var effect = {};
|
||
476 | if (type == 'none') { |
||
477 | effect.showEffect = 'show';
|
||
478 | effect.hideEffect = 'hide';
|
||
479 | effect.showSpeed = '';
|
||
480 | } |
||
481 | else if (type == 'fade') { |
||
482 | effect.showEffect = 'fadeIn';
|
||
483 | effect.hideEffect = 'fadeOut';
|
||
484 | effect.showSpeed = speed; |
||
485 | } |
||
486 | else {
|
||
487 | effect.showEffect = type + 'Toggle';
|
||
488 | effect.hideEffect = type + 'Toggle';
|
||
489 | effect.showSpeed = speed; |
||
490 | } |
||
491 | |||
492 | return effect;
|
||
493 | }; |
||
494 | |||
495 | /**
|
||
496 | * Handler for the form redirection error.
|
||
497 | */
|
||
498 | 08f5d39b | Assos Assos | Drupal.ajax.prototype.error = function (xmlhttprequest, uri, customMessage) { |
499 | b0dc3a2e | Julien Enselme | Drupal.displayAjaxError(Drupal.ajaxError(xmlhttprequest, uri, customMessage)); |
500 | 85ad3d82 | Assos Assos | // Remove the progress element.
|
501 | if (this.progress.element) { |
||
502 | $(this.progress.element).remove(); |
||
503 | } |
||
504 | if (this.progress.object) { |
||
505 | this.progress.object.stopMonitoring();
|
||
506 | } |
||
507 | // Undo hide.
|
||
508 | $(this.wrapper).show(); |
||
509 | // Re-enable the element.
|
||
510 | $(this.element).removeClass('progress-disabled').removeAttr('disabled'); |
||
511 | // Reattach behaviors, if they were detached in beforeSerialize().
|
||
512 | if (this.form) { |
||
513 | 08f5d39b | Assos Assos | var settings = this.settings || Drupal.settings; |
514 | 85ad3d82 | Assos Assos | Drupal.attachBehaviors(this.form, settings);
|
515 | } |
||
516 | }; |
||
517 | |||
518 | /**
|
||
519 | * Provide a series of commands that the server can request the client perform.
|
||
520 | */
|
||
521 | Drupal.ajax.prototype.commands = { |
||
522 | /**
|
||
523 | * Command to insert new content into the DOM.
|
||
524 | */
|
||
525 | insert: function (ajax, response, status) { |
||
526 | // Get information from the response. If it is not there, default to
|
||
527 | // our presets.
|
||
528 | var wrapper = response.selector ? $(response.selector) : $(ajax.wrapper); |
||
529 | var method = response.method || ajax.method;
|
||
530 | var effect = ajax.getEffect(response);
|
||
531 | |||
532 | // We don't know what response.data contains: it might be a string of text
|
||
533 | // without HTML, so don't rely on jQuery correctly iterpreting
|
||
534 | // $(response.data) as new HTML rather than a CSS selector. Also, if
|
||
535 | // response.data contains top-level text nodes, they get lost with either
|
||
536 | // $(response.data) or $('<div></div>').replaceWith(response.data).
|
||
537 | var new_content_wrapped = $('<div></div>').html(response.data); |
||
538 | var new_content = new_content_wrapped.contents();
|
||
539 | |||
540 | // For legacy reasons, the effects processing code assumes that new_content
|
||
541 | // consists of a single top-level element. Also, it has not been
|
||
542 | // sufficiently tested whether attachBehaviors() can be successfully called
|
||
543 | // with a context object that includes top-level text nodes. However, to
|
||
544 | // give developers full control of the HTML appearing in the page, and to
|
||
545 | // enable Ajax content to be inserted in places where DIV elements are not
|
||
546 | // allowed (e.g., within TABLE, TR, and SPAN parents), we check if the new
|
||
547 | // content satisfies the requirement of a single top-level element, and
|
||
548 | // only use the container DIV created above when it doesn't. For more
|
||
549 | // information, please see http://drupal.org/node/736066.
|
||
550 | if (new_content.length != 1 || new_content.get(0).nodeType != 1) { |
||
551 | new_content = new_content_wrapped; |
||
552 | } |
||
553 | |||
554 | // If removing content from the wrapper, detach behaviors first.
|
||
555 | switch (method) {
|
||
556 | case 'html': |
||
557 | case 'replaceWith': |
||
558 | case 'replaceAll': |
||
559 | case 'empty': |
||
560 | case 'remove': |
||
561 | var settings = response.settings || ajax.settings || Drupal.settings;
|
||
562 | Drupal.detachBehaviors(wrapper, settings); |
||
563 | } |
||
564 | |||
565 | // Add the new content to the page.
|
||
566 | wrapper[method](new_content); |
||
567 | |||
568 | // Immediately hide the new content if we're using any effects.
|
||
569 | if (effect.showEffect != 'show') { |
||
570 | new_content.hide(); |
||
571 | } |
||
572 | |||
573 | // Determine which effect to use and what content will receive the
|
||
574 | // effect, then show the new content.
|
||
575 | if ($('.ajax-new-content', new_content).length > 0) { |
||
576 | $('.ajax-new-content', new_content).hide(); |
||
577 | new_content.show(); |
||
578 | $('.ajax-new-content', new_content)[effect.showEffect](effect.showSpeed); |
||
579 | } |
||
580 | else if (effect.showEffect != 'show') { |
||
581 | new_content[effect.showEffect](effect.showSpeed); |
||
582 | } |
||
583 | |||
584 | // Attach all JavaScript behaviors to the new content, if it was successfully
|
||
585 | // added to the page, this if statement allows #ajax['wrapper'] to be
|
||
586 | // optional.
|
||
587 | if (new_content.parents('html').length > 0) { |
||
588 | // Apply any settings from the returned JSON if available.
|
||
589 | var settings = response.settings || ajax.settings || Drupal.settings;
|
||
590 | Drupal.attachBehaviors(new_content, settings); |
||
591 | } |
||
592 | }, |
||
593 | |||
594 | /**
|
||
595 | * Command to remove a chunk from the page.
|
||
596 | */
|
||
597 | remove: function (ajax, response, status) { |
||
598 | var settings = response.settings || ajax.settings || Drupal.settings;
|
||
599 | Drupal.detachBehaviors($(response.selector), settings);
|
||
600 | $(response.selector).remove();
|
||
601 | }, |
||
602 | |||
603 | /**
|
||
604 | * Command to mark a chunk changed.
|
||
605 | */
|
||
606 | changed: function (ajax, response, status) { |
||
607 | if (!$(response.selector).hasClass('ajax-changed')) { |
||
608 | $(response.selector).addClass('ajax-changed'); |
||
609 | if (response.asterisk) {
|
||
610 | $(response.selector).find(response.asterisk).append(' <span class="ajax-changed">*</span> '); |
||
611 | } |
||
612 | } |
||
613 | }, |
||
614 | |||
615 | /**
|
||
616 | * Command to provide an alert.
|
||
617 | */
|
||
618 | alert: function (ajax, response, status) { |
||
619 | alert(response.text, response.title); |
||
620 | }, |
||
621 | |||
622 | /**
|
||
623 | * Command to provide the jQuery css() function.
|
||
624 | */
|
||
625 | css: function (ajax, response, status) { |
||
626 | $(response.selector).css(response.argument);
|
||
627 | }, |
||
628 | |||
629 | /**
|
||
630 | * Command to set the settings that will be used for other commands in this response.
|
||
631 | */
|
||
632 | settings: function (ajax, response, status) { |
||
633 | if (response.merge) {
|
||
634 | $.extend(true, Drupal.settings, response.settings); |
||
635 | } |
||
636 | else {
|
||
637 | ajax.settings = response.settings; |
||
638 | } |
||
639 | }, |
||
640 | |||
641 | /**
|
||
642 | * Command to attach data using jQuery's data API.
|
||
643 | */
|
||
644 | data: function (ajax, response, status) { |
||
645 | $(response.selector).data(response.name, response.value);
|
||
646 | }, |
||
647 | |||
648 | /**
|
||
649 | * Command to apply a jQuery method.
|
||
650 | */
|
||
651 | invoke: function (ajax, response, status) { |
||
652 | var $element = $(response.selector); |
||
653 | $element[response.method].apply($element, response.arguments); |
||
654 | }, |
||
655 | |||
656 | /**
|
||
657 | * Command to restripe a table.
|
||
658 | */
|
||
659 | restripe: function (ajax, response, status) { |
||
660 | // :even and :odd are reversed because jQuery counts from 0 and
|
||
661 | // we count from 1, so we're out of sync.
|
||
662 | // Match immediate children of the parent element to allow nesting.
|
||
663 | $('> tbody > tr:visible, > tr:visible', $(response.selector)) |
||
664 | .removeClass('odd even')
|
||
665 | .filter(':even').addClass('odd').end() |
||
666 | .filter(':odd').addClass('even'); |
||
667 | 42e6daf3 | Julien Enselme | }, |
668 | |||
669 | b4adf10d | Assos Assos | /**
|
670 | * Command to add css.
|
||
671 | *
|
||
672 | * Uses the proprietary addImport method if available as browsers which
|
||
673 | * support that method ignore @import statements in dynamically added
|
||
674 | * stylesheets.
|
||
675 | */
|
||
676 | add_css: function (ajax, response, status) { |
||
677 | // Add the styles in the normal way.
|
||
678 | $('head').prepend(response.data); |
||
679 | // Add imports in the styles using the addImport method if available.
|
||
680 | var match, importMatch = /^@import url\("(.*)"\);$/igm; |
||
681 | if (document.styleSheets[0].addImport && importMatch.test(response.data)) { |
||
682 | importMatch.lastIndex = 0;
|
||
683 | while (match = importMatch.exec(response.data)) {
|
||
684 | document.styleSheets[0].addImport(match[1]); |
||
685 | } |
||
686 | } |
||
687 | }, |
||
688 | |||
689 | 42e6daf3 | Julien Enselme | /**
|
690 | * Command to update a form's build ID.
|
||
691 | */
|
||
692 | updateBuildId: function(ajax, response, status) { |
||
693 | 4444412d | Julien Enselme | $('input[name="form_build_id"][value="' + response['old'] + '"]').val(response['new']); |
694 | 85ad3d82 | Assos Assos | } |
695 | }; |
||
696 | |||
697 | })(jQuery); |