This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoupdate.js
111 lines (102 loc) · 3.62 KB
/
autoupdate.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
/*!
* jQuery Templates Plugin
* https://github.com/tropperstyle/jquery-autoupdate
*
* Copyright, Jonathan Tropper.
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* MIT-LICENSE.txt
* GPL-LICENSE.txt
*/
(function($) {
if (!$.tmpl) {
alert('Plugin requires jQuery tmpl plugin');
return;
}
$.widget('cf.autoupdate', {
options: {
interval: 3000,
template: '',
source: '',
blank: 'No Data'
},
_create: function() {
var widget = this;
this.items = [];
this.initial = true;
this.blankElement = this.options.template.tmpl({}).html(this.options.blank).appendTo(this.element).hide();
this.resume();
},
destroy: function() {
this.pause();
$.Widget.prototype.destroy.apply(this, arguments);
},
pause: function() {
window.clearInterval(this.refresher);
},
resume: function() {
if (this.refresher) {
this.pause();
delete this.refresher;
}
var widget = this;
this.__update();
this.refresher = window.setInterval(function() { widget.__update(); }, this.options.interval);
},
update: function(items) {
this.__process(items);
},
_remove: function(item) {
item.element.fadeOut(function() { item.element.remove(); });
},
__update: function() {
$.getJSON(this.options.source, $.proxy(this.__process, this));
},
__process: function(items) {
var lastItem, widget = this, active = {};
this.blankElement[items.length == 0 ? 'show' : 'hide']();
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i], prevItem = widget.items[item.id];
if (prevItem) {
var difference = widget.__compare(prevItem, item);
if ($.isEmptyObject(difference)) {
item.element = prevItem.element;
} else {
widget.__createElement(item);
prevItem.element.remove();
widget.__insert(item, lastItem, difference);
}
delete widget.items[item.id];
} else {
widget.__createElement(item);
widget.__insert(item, lastItem);
}
lastItem = item;
active[item.id] = item;
}
$.each(widget.items, function(id, item) { widget._remove(item); });
widget.items = active;
if (this.initial) { this._trigger('loaded'); this.initial = false; }
},
__compare: function(a, b) {
a = $.extend(true, {}, a);
b = $.extend(true, {}, b);
delete a.element;
delete b.element;
diff = {};
$.each(a, function(k, v) {
if (b[k] != v) { diff[k] = [v, b[k]]; }
});
return diff;
},
__createElement: function(item) {
item.element = this.options.template.tmpl(item);
item.element.data('item', item);
this._trigger('insert', null, item);
},
__insert: function(item, lastItem, difference) {
lastItem ? item.element.insertAfter(lastItem.element) : item.element.prependTo(this.element);
this._trigger('changed', null, { item: item, diff: difference });
}
});
})(jQuery);