123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * @file
- *
- * CTools flexible AJAX responder object.
- */
- (function ($) {
- Drupal.CTools = Drupal.CTools || {};
- Drupal.CTools.AJAX = Drupal.CTools.AJAX || {};
- /**
- * Grab the response from the server and store it.
- *
- * @todo restore the warm cache functionality
- */
- Drupal.CTools.AJAX.warmCache = function () {
- // Store this expression for a minor speed improvement.
- $this = $(this);
- var old_url = $this.attr('href');
- // If we are currently fetching, or if we have fetched this already which is
- // ideal for things like pagers, where the previous page might already have
- // been seen in the cache.
- if ($this.hasClass('ctools-fetching') || Drupal.CTools.AJAX.commandCache[old_url]) {
- return false;
- }
- // Grab all the links that match this url and add the fetching class.
- // This allows the caching system to grab each url once and only once
- // instead of grabbing the url once per <a>.
- var $objects = $('a[href="' + old_url + '"]')
- $objects.addClass('ctools-fetching');
- try {
- url = old_url.replace(/\/nojs(\/|$)/g, '/ajax$1');
- $.ajax({
- type: "POST",
- url: url,
- data: { 'js': 1, 'ctools_ajax': 1},
- global: true,
- success: function (data) {
- Drupal.CTools.AJAX.commandCache[old_url] = data;
- $objects.addClass('ctools-cache-warmed').trigger('ctools-cache-warm', [data]);
- },
- complete: function() {
- $objects.removeClass('ctools-fetching');
- },
- dataType: 'json'
- });
- }
- catch (err) {
- $objects.removeClass('ctools-fetching');
- return false;
- }
- return false;
- };
- /**
- * Cachable click handler to fetch the commands out of the cache or from url.
- */
- Drupal.CTools.AJAX.clickAJAXCacheLink = function () {
- $this = $(this);
- if ($this.hasClass('ctools-fetching')) {
- $this.bind('ctools-cache-warm', function (event, data) {
- Drupal.CTools.AJAX.respond(data);
- });
- return false;
- }
- else {
- if ($this.hasClass('ctools-cache-warmed') && Drupal.CTools.AJAX.commandCache[$this.attr('href')]) {
- Drupal.CTools.AJAX.respond(Drupal.CTools.AJAX.commandCache[$this.attr('href')]);
- return false;
- }
- else {
- return Drupal.CTools.AJAX.clickAJAXLink.apply(this);
- }
- }
- };
- /**
- * Find a URL for an AJAX button.
- *
- * The URL for this gadget will be composed of the values of items by
- * taking the ID of this item and adding -url and looking for that
- * class. They need to be in the form in order since we will
- * concat them all together using '/'.
- */
- Drupal.CTools.AJAX.findURL = function(item) {
- var url = '';
- var url_class = '.' + $(item).attr('id') + '-url';
- $(url_class).each(
- function() {
- var $this = $(this);
- if (url && $this.val()) {
- url += '/';
- }
- url += $this.val();
- });
- return url;
- };
- // Hide these in a ready to ensure that Drupal.ajax is set up first.
- $(function() {
- Drupal.ajax.prototype.commands.attr = function(ajax, data, status) {
- $(data.selector).attr(data.name, data.value);
- };
- Drupal.ajax.prototype.commands.redirect = function(ajax, data, status) {
- if (data.delay > 0) {
- setTimeout(function () {
- location.href = data.url;
- }, data.delay);
- }
- else {
- location.href = data.url;
- }
- };
- Drupal.ajax.prototype.commands.reload = function(ajax, data, status) {
- location.reload();
- };
- Drupal.ajax.prototype.commands.submit = function(ajax, data, status) {
- $(data.selector).submit();
- }
- });
- })(jQuery);
|