cd4492c13c44cd1d0f27bb3f15cb27de850d535b
[emels.git] / emels / static / django_extensions / js / jquery.ajaxQueue.js
1 /**
2  * Ajax Queue Plugin
3  *
4  * Homepage: http://jquery.com/plugins/project/ajaxqueue
5  * Documentation: http://docs.jquery.com/AjaxQueue
6  */
7
8 /**
9
10 <script>
11 $(function(){
12         jQuery.ajaxQueue({
13                 url: "test.php",
14                 success: function(html){ jQuery("ul").append(html); }
15         });
16         jQuery.ajaxQueue({
17                 url: "test.php",
18                 success: function(html){ jQuery("ul").append(html); }
19         });
20         jQuery.ajaxSync({
21                 url: "test.php",
22                 success: function(html){ jQuery("ul").append("<b>"+html+"</b>"); }
23         });
24         jQuery.ajaxSync({
25                 url: "test.php",
26                 success: function(html){ jQuery("ul").append("<b>"+html+"</b>"); }
27         });
28 });
29 </script>
30 <ul style="position: absolute; top: 5px; right: 5px;"></ul>
31
32  */
33 /*
34  * Queued Ajax requests.
35  * A new Ajax request won't be started until the previous queued
36  * request has finished.
37  */
38
39 /*
40  * Synced Ajax requests.
41  * The Ajax request will happen as soon as you call this method, but
42  * the callbacks (success/error/complete) won't fire until all previous
43  * synced requests have been completed.
44  */
45
46
47 (function(jQuery) {
48
49         var ajax = jQuery.ajax;
50
51         var pendingRequests = {};
52
53         var synced = [];
54         var syncedData = [];
55
56         jQuery.ajax = function(settings) {
57                 // create settings for compatibility with ajaxSetup
58                 settings = jQuery.extend(settings, jQuery.extend({}, jQuery.ajaxSettings, settings));
59
60                 var port = settings.port;
61
62                 switch(settings.mode) {
63                 case "abort":
64                         if ( pendingRequests[port] ) {
65                                 pendingRequests[port].abort();
66                         }
67                         return pendingRequests[port] = ajax.apply(this, arguments);
68                 case "queue":
69                         var _old = settings.complete;
70                         settings.complete = function(){
71                                 if ( _old )
72                                         _old.apply( this, arguments );
73                                 jQuery([ajax]).dequeue("ajax" + port );;
74                         };
75
76                         jQuery([ ajax ]).queue("ajax" + port, function(){
77                                 ajax( settings );
78                         });
79                         return;
80                 case "sync":
81                         var pos = synced.length;
82
83                         synced[ pos ] = {
84                                 error: settings.error,
85                                 success: settings.success,
86                                 complete: settings.complete,
87                                 done: false
88                         };
89
90                         syncedData[ pos ] = {
91                                 error: [],
92                                 success: [],
93                                 complete: []
94                         };
95
96                         settings.error = function(){ syncedData[ pos ].error = arguments; };
97                         settings.success = function(){ syncedData[ pos ].success = arguments; };
98                         settings.complete = function(){
99                                 syncedData[ pos ].complete = arguments;
100                                 synced[ pos ].done = true;
101
102                                 if ( pos == 0 || !synced[ pos-1 ] )
103                                         for ( var i = pos; i < synced.length && synced[i].done; i++ ) {
104                                                 if ( synced[i].error ) synced[i].error.apply( jQuery, syncedData[i].error );
105                                                 if ( synced[i].success ) synced[i].success.apply( jQuery, syncedData[i].success );
106                                                 if ( synced[i].complete ) synced[i].complete.apply( jQuery, syncedData[i].complete );
107
108                                                 synced[i] = null;
109                                                 syncedData[i] = null;
110                                         }
111                         };
112                 }
113                 return ajax.apply(this, arguments);
114         };
115
116 })((typeof window.jQuery == 'undefined' && typeof window.django != 'undefined')
117   ? django.jQuery
118   : jQuery
119 );