123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- (function ($) {
- Drupal.ajax = Drupal.ajax || {};
- Drupal.settings.urlIsAjaxTrusted = Drupal.settings.urlIsAjaxTrusted || {};
- Drupal.behaviors.AJAX = {
- attach: function (context, settings) {
-
- for (var base in settings.ajax) {
- if (!$('#' + base + '.ajax-processed').length) {
- var element_settings = settings.ajax[base];
- if (typeof element_settings.selector == 'undefined') {
- element_settings.selector = '#' + base;
- }
- $(element_settings.selector).each(function () {
- element_settings.element = this;
- Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
- });
- $('#' + base).addClass('ajax-processed');
- }
- }
-
- $('.use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () {
- var element_settings = {};
-
- element_settings.progress = { 'type': 'throbber' };
-
-
- if ($(this).attr('href')) {
- element_settings.url = $(this).attr('href');
- element_settings.event = 'click';
- }
- var base = $(this).attr('id');
- Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
- });
-
- $('.use-ajax-submit:not(.ajax-processed)').addClass('ajax-processed').each(function () {
- var element_settings = {};
-
-
- element_settings.url = $(this.form).attr('action');
-
-
- element_settings.setClick = true;
-
- element_settings.event = 'click';
-
- element_settings.progress = { 'type': 'throbber' };
- var base = $(this).attr('id');
- Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
- });
- }
- };
- Drupal.ajax = function (base, element, element_settings) {
- var defaults = {
- url: 'system/ajax',
- event: 'mousedown',
- keypress: true,
- selector: '#' + base,
- effect: 'none',
- speed: 'none',
- method: 'replaceWith',
- progress: {
- type: 'throbber',
- message: Drupal.t('Please wait...')
- },
- submit: {
- 'js': true
- }
- };
- $.extend(this, defaults, element_settings);
- this.element = element;
- this.element_settings = element_settings;
-
-
-
-
-
-
-
-
-
-
-
- this.url = element_settings.url.replace(/\/nojs(\/|$|\?|&|#)/g, '/ajax$1');
-
- if (Drupal.settings.urlIsAjaxTrusted[element_settings.url]) {
- Drupal.settings.urlIsAjaxTrusted[this.url] = true;
- }
- this.wrapper = '#' + element_settings.wrapper;
-
-
- if (this.element.form) {
- this.form = $(this.element.form);
- }
-
-
- var ajax = this;
- ajax.options = {
- url: ajax.url,
- data: ajax.submit,
- beforeSerialize: function (element_settings, options) {
- return ajax.beforeSerialize(element_settings, options);
- },
- beforeSubmit: function (form_values, element_settings, options) {
- ajax.ajaxing = true;
- return ajax.beforeSubmit(form_values, element_settings, options);
- },
- beforeSend: function (xmlhttprequest, options) {
- ajax.ajaxing = true;
- return ajax.beforeSend(xmlhttprequest, options);
- },
- success: function (response, status, xmlhttprequest) {
-
-
- if (typeof response == 'string') {
- response = $.parseJSON(response);
- }
-
-
-
-
-
-
-
-
-
-
- if (response !== null && !Drupal.settings.urlIsAjaxTrusted[ajax.url]) {
- if (xmlhttprequest.getResponseHeader('X-Drupal-Ajax-Token') !== '1') {
- var customMessage = Drupal.t("The response failed verification so will not be processed.");
- return ajax.error(xmlhttprequest, ajax.url, customMessage);
- }
- }
- return ajax.success(response, status);
- },
- complete: function (xmlhttprequest, status) {
- ajax.ajaxing = false;
- if (status == 'error' || status == 'parsererror') {
- return ajax.error(xmlhttprequest, ajax.url);
- }
- },
- dataType: 'json',
- type: 'POST'
- };
-
- $(ajax.element).bind(element_settings.event, function (event) {
- if (!Drupal.settings.urlIsAjaxTrusted[ajax.url] && !Drupal.urlIsLocal(ajax.url)) {
- throw new Error(Drupal.t('The callback URL is not local and not trusted: !url', {'!url': ajax.url}));
- }
- return ajax.eventResponse(this, event);
- });
-
-
-
- if (element_settings.keypress) {
- $(ajax.element).keypress(function (event) {
- return ajax.keypressResponse(this, event);
- });
- }
-
-
-
- if (element_settings.prevent) {
- $(ajax.element).bind(element_settings.prevent, false);
- }
- };
- Drupal.ajax.prototype.keypressResponse = function (element, event) {
-
- var ajax = this;
-
-
-
-
- if (event.which == 13 || (event.which == 32 && element.type != 'text' && element.type != 'textarea')) {
- $(ajax.element_settings.element).trigger(ajax.element_settings.event);
- return false;
- }
- };
- Drupal.ajax.prototype.eventResponse = function (element, event) {
-
- var ajax = this;
-
- if (ajax.ajaxing) {
- return false;
- }
- try {
- if (ajax.form) {
-
-
- if (ajax.setClick) {
-
-
-
-
- element.form.clk = element;
- }
- ajax.form.ajaxSubmit(ajax.options);
- }
- else {
- ajax.beforeSerialize(ajax.element, ajax.options);
- $.ajax(ajax.options);
- }
- }
- catch (e) {
-
-
- ajax.ajaxing = false;
- alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message);
- }
-
-
- if (typeof element.type != 'undefined' && (element.type == 'checkbox' || element.type == 'radio')) {
- return true;
- }
- else {
- return false;
- }
- };
- Drupal.ajax.prototype.beforeSerialize = function (element, options) {
-
-
-
-
-
- if (this.form) {
- var settings = this.settings || Drupal.settings;
- Drupal.detachBehaviors(this.form, settings, 'serialize');
- }
-
-
- options.data['ajax_html_ids[]'] = [];
- $('[id]').each(function () {
- options.data['ajax_html_ids[]'].push(this.id);
- });
-
-
-
-
-
- options.data['ajax_page_state[theme]'] = Drupal.settings.ajaxPageState.theme;
- options.data['ajax_page_state[theme_token]'] = Drupal.settings.ajaxPageState.theme_token;
- for (var key in Drupal.settings.ajaxPageState.css) {
- options.data['ajax_page_state[css][' + key + ']'] = 1;
- }
- for (var key in Drupal.settings.ajaxPageState.js) {
- options.data['ajax_page_state[js][' + key + ']'] = 1;
- }
- };
- Drupal.ajax.prototype.beforeSubmit = function (form_values, element, options) {
-
-
- };
- Drupal.ajax.prototype.beforeSend = function (xmlhttprequest, options) {
-
-
-
-
-
-
-
-
-
- if (this.form) {
- options.extraData = options.extraData || {};
-
-
-
- options.extraData.ajax_iframe_upload = '1';
-
-
-
-
-
- var v = $.fieldValue(this.element);
- if (v !== null) {
- options.extraData[this.element.name] = Drupal.checkPlain(v);
- }
- }
-
-
-
-
- $(this.element).addClass('progress-disabled').attr('disabled', true);
-
- if (this.progress.type == 'bar') {
- var progressBar = new Drupal.progressBar('ajax-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback));
- if (this.progress.message) {
- progressBar.setProgress(-1, this.progress.message);
- }
- if (this.progress.url) {
- progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500);
- }
- this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar');
- this.progress.object = progressBar;
- $(this.element).after(this.progress.element);
- }
- else if (this.progress.type == 'throbber') {
- this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber"> </div></div>');
- if (this.progress.message) {
- $('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>');
- }
- $(this.element).after(this.progress.element);
- }
- };
- Drupal.ajax.prototype.success = function (response, status) {
-
- if (this.progress.element) {
- $(this.progress.element).remove();
- }
- if (this.progress.object) {
- this.progress.object.stopMonitoring();
- }
- $(this.element).removeClass('progress-disabled').removeAttr('disabled');
- Drupal.freezeHeight();
- for (var i in response) {
- if (response.hasOwnProperty(i) && response[i]['command'] && this.commands[response[i]['command']]) {
- this.commands[response[i]['command']](this, response[i], status);
- }
- }
-
-
-
-
- if (this.form) {
- var settings = this.settings || Drupal.settings;
- Drupal.attachBehaviors(this.form, settings);
- }
- Drupal.unfreezeHeight();
-
-
- this.settings = null;
- };
- Drupal.ajax.prototype.getEffect = function (response) {
- var type = response.effect || this.effect;
- var speed = response.speed || this.speed;
- var effect = {};
- if (type == 'none') {
- effect.showEffect = 'show';
- effect.hideEffect = 'hide';
- effect.showSpeed = '';
- }
- else if (type == 'fade') {
- effect.showEffect = 'fadeIn';
- effect.hideEffect = 'fadeOut';
- effect.showSpeed = speed;
- }
- else {
- effect.showEffect = type + 'Toggle';
- effect.hideEffect = type + 'Toggle';
- effect.showSpeed = speed;
- }
- return effect;
- };
- Drupal.ajax.prototype.error = function (xmlhttprequest, uri, customMessage) {
- Drupal.displayAjaxError(Drupal.ajaxError(xmlhttprequest, uri, customMessage));
-
- if (this.progress.element) {
- $(this.progress.element).remove();
- }
- if (this.progress.object) {
- this.progress.object.stopMonitoring();
- }
-
- $(this.wrapper).show();
-
- $(this.element).removeClass('progress-disabled').removeAttr('disabled');
-
- if (this.form) {
- var settings = this.settings || Drupal.settings;
- Drupal.attachBehaviors(this.form, settings);
- }
- };
- Drupal.ajax.prototype.commands = {
-
- insert: function (ajax, response, status) {
-
-
- var wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
- var method = response.method || ajax.method;
- var effect = ajax.getEffect(response);
-
-
-
-
-
- var new_content_wrapped = $('<div></div>').html(response.data);
- var new_content = new_content_wrapped.contents();
-
-
-
-
-
-
-
-
-
-
- if (new_content.length != 1 || new_content.get(0).nodeType != 1) {
- new_content = new_content_wrapped;
- }
-
- switch (method) {
- case 'html':
- case 'replaceWith':
- case 'replaceAll':
- case 'empty':
- case 'remove':
- var settings = response.settings || ajax.settings || Drupal.settings;
- Drupal.detachBehaviors(wrapper, settings);
- }
-
- wrapper[method](new_content);
-
- if (effect.showEffect != 'show') {
- new_content.hide();
- }
-
-
- if ($('.ajax-new-content', new_content).length > 0) {
- $('.ajax-new-content', new_content).hide();
- new_content.show();
- $('.ajax-new-content', new_content)[effect.showEffect](effect.showSpeed);
- }
- else if (effect.showEffect != 'show') {
- new_content[effect.showEffect](effect.showSpeed);
- }
-
-
-
- if (new_content.parents('html').length > 0) {
-
- var settings = response.settings || ajax.settings || Drupal.settings;
- Drupal.attachBehaviors(new_content, settings);
- }
- },
-
- remove: function (ajax, response, status) {
- var settings = response.settings || ajax.settings || Drupal.settings;
- Drupal.detachBehaviors($(response.selector), settings);
- $(response.selector).remove();
- },
-
- changed: function (ajax, response, status) {
- if (!$(response.selector).hasClass('ajax-changed')) {
- $(response.selector).addClass('ajax-changed');
- if (response.asterisk) {
- $(response.selector).find(response.asterisk).append(' <span class="ajax-changed">*</span> ');
- }
- }
- },
-
- alert: function (ajax, response, status) {
- alert(response.text, response.title);
- },
-
- css: function (ajax, response, status) {
- $(response.selector).css(response.argument);
- },
-
- settings: function (ajax, response, status) {
- if (response.merge) {
- $.extend(true, Drupal.settings, response.settings);
- }
- else {
- ajax.settings = response.settings;
- }
- },
-
- data: function (ajax, response, status) {
- $(response.selector).data(response.name, response.value);
- },
-
- invoke: function (ajax, response, status) {
- var $element = $(response.selector);
- $element[response.method].apply($element, response.arguments);
- },
-
- restripe: function (ajax, response, status) {
-
-
-
- $('> tbody > tr:visible, > tr:visible', $(response.selector))
- .removeClass('odd even')
- .filter(':even').addClass('odd').end()
- .filter(':odd').addClass('even');
- },
-
- add_css: function (ajax, response, status) {
-
- $('head').prepend(response.data);
-
- var match, importMatch = /^@import url\("(.*)"\);$/igm;
- if (document.styleSheets[0].addImport && importMatch.test(response.data)) {
- importMatch.lastIndex = 0;
- while (match = importMatch.exec(response.data)) {
- document.styleSheets[0].addImport(match[1]);
- }
- }
- },
-
- updateBuildId: function(ajax, response, status) {
- $('input[name="form_build_id"][value="' + response['old'] + '"]').val(response['new']);
- }
- };
- })(jQuery);
|