-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-frames.js
109 lines (94 loc) · 3.75 KB
/
jquery-frames.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
(function($){
var pluginName = 'Frames',
defaults = {
'total_frames': 31, //total no of frames
'frame_rate': 40, //frame rate in milliseconds
'auto_start': true, //start the frame animation
};
// The actual plugin constructor
function Plugin( element, options ) {
console.log('new plugin change...');
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype.init = function () {
// Place initialization logic here
// You already have access to the DOM element and
// the options via the instance, e.g. this.element
// and this.options
if(this.options.auto_start){
this._create_frames();
this._run();
}
};
Plugin.prototype._render_frames = function(n){
obj = this;
setTimeout(function(){
if(n <= obj.options.total_frames){
$(obj.element).data('frames')[n](this);
obj._render_frames( n+1 );
}
}, this.options.frame_rate)
}
Plugin.prototype._run = function(){
obj = this;
this.runner = setInterval(function(){
if (!$(obj.element).data('frames_paused'))
obj._render_frames(1);
}, (this.options.total_frames * this.options.frame_rate));
}
Plugin.prototype._create_frames = function(){
frames = []
for(i=1; i <= this.options.total_frames; i++){
frames[i] = function(el){
}
}
$(this.element).data('frames', frames);
$(this.element).data('frames_paused', false)
}
Plugin.prototype.pause = function(){
$(this.element).data('frames_paused', true);
}
Plugin.prototype.resume = function(){
$(this.element).data('frames_paused', false);
}
Plugin.prototype._destroy = function(){
$(this.element).data('frames_paused', null);
$(this.element).data('frames', null);
}
$.fn[pluginName] = function(options){
var args = arguments;
if (options === undefined || typeof options === 'object') {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
}
else if(typeof options === 'string' && options[0] !== '_' && options !== 'init'){
var returns;
returns = this.each(function () {
var instance = $.data(this, 'plugin_' + pluginName);
if (!instance) {
//initializing with empty option asssuming it dont have any options
instance = $.data(this, 'plugin_' + pluginName, new Plugin(this));
}
//vrifiying whether the options given is a function, non private function
//and calls the function with given args
if (instance instanceof Plugin && typeof instance[options] === 'function') {
return instance[options].apply(instance, Array.prototype.slice.call(args, 1));
}
//If option is destroy just destroys the plugin from the element(s)
if (instance instanceof Plugin && options === 'destroy') {
$.data(this, 'plugin_' + pluginName, null);
return instance['_destroy'].apply(instance, Array.prototype.slice.call(args, 1));
}
});
return returns !== undefined ? returns : this;
}
}
}(jQuery));