-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartupdater.js
270 lines (218 loc) · 7.5 KB
/
smartupdater.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* smartupdater - jQuery Plugin
*
* Version - 3.2.00.beta
* Copyright (c) 2010 - 2011 Vadim Kiryukhin
* vkiryukhin @ gmail.com
*
* http://www.eslinstructor.net/smartupdater3/
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* USAGE:
*
* $("#myObject").smartupdater({
* url : "foo.php"
* }, function (data) {
* //process data here;
* }
* );
*
* Public functions:
* $("#myObject").smartupdaterStop();
* $("#myObject").smartupdaterRestart();
* $("#myObject").smartupdaterSetTimeout();
* $("#myObject").smartupdaterAlterUrl();
* $("#myObject").smartupdaterAlterCallback();
*
* Public Attributes:
* var smStatus = $("#myObject")[0].smartupdaterStatus.state;
* var smTimeout = $("#myObject")[0].smartupdaterStatus.timeout;
*
**/
(function(jQuery) {
jQuery.fn.smartupdater = function (options, callback) {
return this.each(function () {
var elem = this,
es = {};
elem.settings = jQuery.extend(true,{
url : '', // see jQuery.ajax for details
type : 'get', // see jQuery.ajax for details
data : '', // see jQuery.ajax for details
dataType : 'text', // see jQuery.ajax for details
minTimeout : 60000, // 1 minute
maxFailedRequests : 10, // max. number of consecutive ajax failures by default
maxFailedRequestsCb : false, // falure callback function by default
httpCache : false, // http cache
rCallback : false, // remote callback functions
selfStart : true, // start automatically after initializing
smartStop : { active: false, //disabled by default
monitorTimeout: 2500, // 2.5 seconds
minHeight: 1, // 1px
minWidth: 1 // 1px
}
}, options);
elem.smartupdaterStatus = {state:'',timeout:0};
es = elem.settings;
es.prevContent = '';
es.failedRequests = 0;
es.etag = '0';
es.lastModified = '0';
es.callback = callback;
es.origReq = {url:es.url,data:es.data,callback:callback};
es.stopFlag = false;
function start() {
/* check if element has been deleted and clean it up */
if(!$(elem).parents('body').length) {
clearInterval(elem.smartupdaterStatus.smartStop);
clearTimeout(elem.settings.h);
elem = {};
return;
}
$.ajax({
url : es.url,
type : es.type,
data : es.data,
dataType: es.dataType,
cache : false, // MUST be set to false to prevent IE caching bug.
success: function (data, statusText, xhr) {
var dataNotModified = false,
rCallback = false,
xSmart = jQuery.parseJSON(xhr.getResponseHeader("X-Smartupdater")),
xhrEtag, xhrLM;
if(xSmart) { // remote control
/* remote timeout */
es.minTimeout = xSmart.timeout ? xSmart.timeout : es.minTimeout;
/* remote callback */
rCallback = xSmart.callback ? xSmart.callback : false;
}
if(es.httpCache) { // http cache process here
xhrEtag = xhr.getResponseHeader("ETag");
xhrLM = xhr.getResponseHeader("Last-Modified");
dataNotModified = (es.etag == xhrEtag || es.lastModified == xhrLM) ? true : false;
es.etag = xhrEtag ? xhrEtag : es.etag;
es.lastModified = xhrLM ? xhrLM : es.lastModified;
}
if ( dataNotModified ||
es.prevContent == xhr.responseText ||
xhr.status == 304 ) { // data is not changed
if(!es.stopFlag) {
clearTimeout(es.h);
es.h = setTimeout(start, es.minTimeout);
}
} else { // data is changed
/* cache response data */
es.prevContent = xhr.responseText;
/* reset timeout */
if(!es.stopFlag) {
clearTimeout(es.h);
es.h = setTimeout(start, es.minTimeout);
}
/* run callback function */
if(es.rCallback && rCallback && es.rCallback.search(rCallback) != -1) {
window[rCallback](data);
} else {
es.callback(data);
}
}
elem.smartupdaterStatus.timeout = es.minTimeout;
es.failedRequests = 0;
},
error: function(xhr, textStatus, errorThrown) {
if ( ++es.failedRequests < es.maxFailedRequests ) {
/* increment falure counter and reset timeout */
if(!es.stopFlag) {
clearTimeout(es.h);
es.h = setTimeout(start, es.minTimeout);
elem.smartupdaterStatus.timeout = es.minTimeout;
}
} else {
/* stop smartupdater */
clearTimeout(es.h);
elem.smartupdaterStatus.state = 'OFF';
if( typeof(es.maxFailedRequestsCb)==='function') {
es.maxFailedRequestsCb(xhr, textStatus, errorThrown);
}
}
},
beforeSend: function(xhr, settings) {
if(es.httpCache) {
/* set http cache-related headers */
xhr.setRequestHeader("If-None-Match", es.etag );
xhr.setRequestHeader("If-Modified-Since", es.lastModified );
}
/* Feedback: Smartupdater sends it's current timeout to server */
xhr.setRequestHeader("X-Smartupdater", '{"timeout":"'+elem.smartupdaterStatus.timeout+'"}');
}
});
elem.smartupdaterStatus.state = 'ON';
}
es.fnStart = start;
if(es.selfStart) {
start();
}
if(es.smartStop.active) {
elem.smartupdaterStatus.smartStop = setInterval(function(){
// check if object has been deleted
if(!$(elem).parents('body').length) {
clearInterval(elem.smartupdaterStatus.smartStop);
clearTimeout(elem.settings.h);
elem = {};
return;
}
var $elem = $(elem);
var width = $elem.width(),
height = $elem.height(),
hidden = $elem.is(":hidden");
//element has been expanded, so smartupdater should be re-started.
if(!hidden && height > es.smartStop.minHeight && width > es.smartStop.minWidth
&& elem.smartupdaterStatus.state=="OFF") {
$elem.smartupdaterRestart();
} else
//element has been minimized, so smartupdater should be stopped.
if( (hidden || height <= es.smartStop.minHeight || width <= es.smartStop.minWidth)
&& elem.smartupdaterStatus.state=="ON") {
$elem.smartupdaterStop();
}
},es.smartStop.monitorTimeout);
}
});
};
jQuery.fn.smartupdaterStop = function () {
return this.each(function () {
this.settings.stopFlag = true;
clearTimeout(this.settings.h);
this.smartupdaterStatus.state = 'OFF';
});
};
jQuery.fn.smartupdaterRestart = function () {
return this.each(function () {
this.settings.stopFlag = false;
clearTimeout(this.settings.h);
this.settings.failedRequests = 0;
this.settings.etag = "0";
this.settings.lastModified = "0";
this.settings.fnStart();
});
};
jQuery.fn.smartupdaterSetTimeout = function (period) {
return this.each(function () {
clearTimeout(this.settings.h);
this.settings.minTimeout = period;
this.settings.fnStart();
});
};
jQuery.fn.smartupdaterAlterCallback = function (callback) {
return this.each(function () {
this.settings.callback = callback ? callback : this.settings.origReq.callback;
});
};
jQuery.fn.smartupdaterAlterUrl = function (url,data) {
return this.each(function () {
this.settings.url = url ? url : this.settings.origReq.url;
this.settings.data = data ? data : this.settings.origReq.data;
});
};
})(jQuery);