-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepage-reloader.js
126 lines (113 loc) · 3.09 KB
/
depage-reloader.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
/**
* @require framework/shared/jquery-1.8.3.js
*
* @file depage-reloader.js
*
* Adds a dynamic AJAX content reloader to elements.
*
* Continually reloads an element by wrapping the jquery load method with a timer.
*
* Options include:
* - url - url to reload: ?ajax=true auto appended
* - interval - repeat interval in seconds
* - selector - load a page fragment by appending a selector - http://api.jquery.com/load/
*
* Triggers an "on_reload" event
*
* copyright (c) 2006-2012 Frank Hellenkamp [[email protected]]
*
* @author Ben Wallis
*/
(function($){
if(!$.depage){
$.depage = {};
}
var timer = null;
/**
* Reloader
*
* @param el - file input
* @param index
* @param options
*/
$.depage.reloader = function(el, index, options){
// To avoid scope issues, use 'base' instead of 'this' to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("depage.reloader", base);
// {{{ init
/**
* Init
*
* Get the plugin options.
* Build URL.
* Init reload.
*
* @return void
*/
base.init = function(){
base.options = $.extend({}, $.depage.reloader.defaultOptions, options);
// build the url
base.options.url = base.options.url + "?ajax=true";
if (base.options.selector) {
base.options.url = base.options.url + ' ' + base.options.selector;
}
base.reload();
};
// }}}
// {{{ reload
/**
* Reload
*
* Create timer polling on elements with ajax reload-url.
*
* @return void
*/
base.reload = function(){
base.$el.load(base.options.url, null, function() {
base.$el.trigger('on_reload');
});
timer = setTimeout(function() {
base.reload();
}, base.options.interval * 1000);
};
/// }}}
// {{{ clear
/**
* Clear
*
* Public function to clear the timer.
*
* @return void
*/
base.clear = function(){
clearInterval(timer);
timer = null;
};
// }}}
base.init();
return base;
};
// }}}
/**
* Options
*
* url - url to reload: ?ajax=true auto appended
* interval - repeat interval in seconds
* selector - load a page fragment by appending a selector - http://api.jquery.com/load/
*/
$.depage.reloader.defaultOptions = {
url: null,
interval: 10,
selector: null
};
$.fn.depageReloader = function(options){
return this.each(function(index){
(new $.depage.reloader(this, index, options));
});
};
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */