root / drupal7 / sites / all / themes / bootstrap / js / misc / ajax.js @ 5024cef7
1 | 87dbc3bf | Benjamin Luce | (function ($) { |
---|---|---|---|
2 | |||
3 | /**
|
||
4 | * Override Drupal's AJAX prototype beforeSend function so it can append the
|
||
5 | * throbber inside the pager links.
|
||
6 | */
|
||
7 | Drupal.ajax.prototype.beforeSend = function (xmlhttprequest, options) { |
||
8 | // For forms without file inputs, the jQuery Form plugin serializes the form
|
||
9 | // values, and then calls jQuery's $.ajax() function, which invokes this
|
||
10 | // handler. In this circumstance, options.extraData is never used. For forms
|
||
11 | // with file inputs, the jQuery Form plugin uses the browser's normal form
|
||
12 | // submission mechanism, but captures the response in a hidden IFRAME. In this
|
||
13 | // circumstance, it calls this handler first, and then appends hidden fields
|
||
14 | // to the form to submit the values in options.extraData. There is no simple
|
||
15 | // way to know which submission mechanism will be used, so we add to extraData
|
||
16 | // regardless, and allow it to be ignored in the former case.
|
||
17 | if (this.form) { |
||
18 | options.extraData = options.extraData || {}; |
||
19 | |||
20 | // Let the server know when the IFRAME submission mechanism is used. The
|
||
21 | // server can use this information to wrap the JSON response in a TEXTAREA,
|
||
22 | // as per http://jquery.malsup.com/form/#file-upload.
|
||
23 | options.extraData.ajax_iframe_upload = '1';
|
||
24 | |||
25 | // The triggering element is about to be disabled (see below), but if it
|
||
26 | // contains a value (e.g., a checkbox, textfield, select, etc.), ensure that
|
||
27 | // value is included in the submission. As per above, submissions that use
|
||
28 | // $.ajax() are already serialized prior to the element being disabled, so
|
||
29 | // this is only needed for IFRAME submissions.
|
||
30 | var v = $.fieldValue(this.element); |
||
31 | if (v !== null) { |
||
32 | options.extraData[this.element.name] = v;
|
||
33 | } |
||
34 | } |
||
35 | |||
36 | caf16a48 | Assos Assos | var $element = $(this.element); |
37 | |||
38 | 87dbc3bf | Benjamin Luce | // Disable the element that received the change to prevent user interface
|
39 | // interaction while the Ajax request is in progress. ajax.ajaxing prevents
|
||
40 | // the element from triggering a new request, but does not prevent the user
|
||
41 | // from changing its value.
|
||
42 | caf16a48 | Assos Assos | $element.addClass('progress-disabled').attr('disabled', true); |
43 | 87dbc3bf | Benjamin Luce | |
44 | // Insert progressbar or throbber.
|
||
45 | if (this.progress.type == 'bar') { |
||
46 | var progressBar = new Drupal.progressBar('ajax-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback)); |
||
47 | if (this.progress.message) { |
||
48 | progressBar.setProgress(-1, this.progress.message); |
||
49 | } |
||
50 | if (this.progress.url) { |
||
51 | caf16a48 | Assos Assos | progressBar.startMonitoring(this.progress.url, this.progress.interval || 500); |
52 | 87dbc3bf | Benjamin Luce | } |
53 | this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar'); |
||
54 | this.progress.object = progressBar;
|
||
55 | 38c269d5 | Assos Assos | if (!$element.closest('.file-widget,.form-item').length) { |
56 | $element.before(this.progress.element); |
||
57 | } |
||
58 | else {
|
||
59 | $element.closest('.file-widget,.form-item').after(this.progress.element); |
||
60 | } |
||
61 | 87dbc3bf | Benjamin Luce | } |
62 | else if (this.progress.type == 'throbber') { |
||
63 | this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><i class="glyphicon glyphicon-refresh glyphicon-spin"></i></div>'); |
||
64 | caf16a48 | Assos Assos | if (this.progress.message) { |
65 | $('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>'); |
||
66 | } |
||
67 | |||
68 | 87dbc3bf | Benjamin Luce | // If element is an input type, append after.
|
69 | caf16a48 | Assos Assos | if ($element.is('input')) { |
70 | $element.after(this.progress.element); |
||
71 | } |
||
72 | else if ($element.is('select')) { |
||
73 | var $inputGroup = $element.closest('.form-item').find('.input-group-addon, .input-group-btn'); |
||
74 | if (!$inputGroup.length) { |
||
75 | $element.wrap('<div class="input-group">'); |
||
76 | $inputGroup = $('<span class="input-group-addon">'); |
||
77 | $element.after($inputGroup); |
||
78 | 87dbc3bf | Benjamin Luce | } |
79 | caf16a48 | Assos Assos | $inputGroup.append(this.progress.element); |
80 | 87dbc3bf | Benjamin Luce | } |
81 | caf16a48 | Assos Assos | // Otherwise append the throbber inside the element.
|
82 | 87dbc3bf | Benjamin Luce | else {
|
83 | caf16a48 | Assos Assos | $element.append(this.progress.element); |
84 | 87dbc3bf | Benjamin Luce | } |
85 | } |
||
86 | }; |
||
87 | |||
88 | })(jQuery); |