Mercurial > hg > rr-repo
comparison sites/all/modules/ctools/js/ajax-responder.js @ 0:ff03f76ab3fe
initial version
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Wed, 21 Aug 2013 18:51:11 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ff03f76ab3fe |
---|---|
1 /** | |
2 * @file | |
3 * | |
4 * CTools flexible AJAX responder object. | |
5 */ | |
6 | |
7 (function ($) { | |
8 Drupal.CTools = Drupal.CTools || {}; | |
9 Drupal.CTools.AJAX = Drupal.CTools.AJAX || {}; | |
10 /** | |
11 * Grab the response from the server and store it. | |
12 * | |
13 * @todo restore the warm cache functionality | |
14 */ | |
15 Drupal.CTools.AJAX.warmCache = function () { | |
16 // Store this expression for a minor speed improvement. | |
17 $this = $(this); | |
18 var old_url = $this.attr('href'); | |
19 // If we are currently fetching, or if we have fetched this already which is | |
20 // ideal for things like pagers, where the previous page might already have | |
21 // been seen in the cache. | |
22 if ($this.hasClass('ctools-fetching') || Drupal.CTools.AJAX.commandCache[old_url]) { | |
23 return false; | |
24 } | |
25 | |
26 // Grab all the links that match this url and add the fetching class. | |
27 // This allows the caching system to grab each url once and only once | |
28 // instead of grabbing the url once per <a>. | |
29 var $objects = $('a[href="' + old_url + '"]') | |
30 $objects.addClass('ctools-fetching'); | |
31 try { | |
32 url = old_url.replace(/\/nojs(\/|$)/g, '/ajax$1'); | |
33 $.ajax({ | |
34 type: "POST", | |
35 url: url, | |
36 data: { 'js': 1, 'ctools_ajax': 1}, | |
37 global: true, | |
38 success: function (data) { | |
39 Drupal.CTools.AJAX.commandCache[old_url] = data; | |
40 $objects.addClass('ctools-cache-warmed').trigger('ctools-cache-warm', [data]); | |
41 }, | |
42 complete: function() { | |
43 $objects.removeClass('ctools-fetching'); | |
44 }, | |
45 dataType: 'json' | |
46 }); | |
47 } | |
48 catch (err) { | |
49 $objects.removeClass('ctools-fetching'); | |
50 return false; | |
51 } | |
52 | |
53 return false; | |
54 }; | |
55 | |
56 /** | |
57 * Cachable click handler to fetch the commands out of the cache or from url. | |
58 */ | |
59 Drupal.CTools.AJAX.clickAJAXCacheLink = function () { | |
60 $this = $(this); | |
61 if ($this.hasClass('ctools-fetching')) { | |
62 $this.bind('ctools-cache-warm', function (event, data) { | |
63 Drupal.CTools.AJAX.respond(data); | |
64 }); | |
65 return false; | |
66 } | |
67 else { | |
68 if ($this.hasClass('ctools-cache-warmed') && Drupal.CTools.AJAX.commandCache[$this.attr('href')]) { | |
69 Drupal.CTools.AJAX.respond(Drupal.CTools.AJAX.commandCache[$this.attr('href')]); | |
70 return false; | |
71 } | |
72 else { | |
73 return Drupal.CTools.AJAX.clickAJAXLink.apply(this); | |
74 } | |
75 } | |
76 }; | |
77 | |
78 /** | |
79 * Find a URL for an AJAX button. | |
80 * | |
81 * The URL for this gadget will be composed of the values of items by | |
82 * taking the ID of this item and adding -url and looking for that | |
83 * class. They need to be in the form in order since we will | |
84 * concat them all together using '/'. | |
85 */ | |
86 Drupal.CTools.AJAX.findURL = function(item) { | |
87 var url = ''; | |
88 var url_class = '.' + $(item).attr('id') + '-url'; | |
89 $(url_class).each( | |
90 function() { | |
91 var $this = $(this); | |
92 if (url && $this.val()) { | |
93 url += '/'; | |
94 } | |
95 url += $this.val(); | |
96 }); | |
97 return url; | |
98 }; | |
99 | |
100 // Hide these in a ready to ensure that Drupal.ajax is set up first. | |
101 $(function() { | |
102 Drupal.ajax.prototype.commands.attr = function(ajax, data, status) { | |
103 $(data.selector).attr(data.name, data.value); | |
104 }; | |
105 | |
106 | |
107 Drupal.ajax.prototype.commands.redirect = function(ajax, data, status) { | |
108 if (data.delay > 0) { | |
109 setTimeout(function () { | |
110 location.href = data.url; | |
111 }, data.delay); | |
112 } | |
113 else { | |
114 location.href = data.url; | |
115 } | |
116 }; | |
117 | |
118 Drupal.ajax.prototype.commands.reload = function(ajax, data, status) { | |
119 location.reload(); | |
120 }; | |
121 | |
122 Drupal.ajax.prototype.commands.submit = function(ajax, data, status) { | |
123 $(data.selector).submit(); | |
124 } | |
125 }); | |
126 })(jQuery); |