-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax-responder.js
126 lines (116 loc) · 3.75 KB
/
ajax-responder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @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);