-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.function-stack.js
243 lines (188 loc) · 4.99 KB
/
jquery.function-stack.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
/*
* jQuery Function Stack Plugin v1.0.0
* https://github.com/leesherwood/jquery-function-stack
*
* Copyright 2012, "leesherwood" Lee Sherwood
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/MIT
*
* AKA: I couldn't care less what you do with it,
* im just a nice guy and i want to share.
* But it would be nice if you left me a little credit :)
*
* If you use this plugin, then let me know at: [email protected]
*/
;(function($){
"use strict";
var stacks = {};
var undef = "undefined";
/**
* Constructors and Destructors
*/
$.functionStack = (function(){
function create(ref, opts){
/**
* If the stack reference name is an object, presume the options were passed before
* the reference, or an anonymous reference has been asked for. Flip 'em!
*/
if(typeof ref == "object") {
var ref_holder = opts;
opts = ref;
ref = ref_holder;
}
/**
* If the reference is empty then prepare for an anonymous stack
*/
if(typeof ref == undef || ( typeof ref == "string" && ref.length == 0)) {
var uniqueRef=false;
var newref="";
do {
newref = "Anon"+Math.floor( Math.random() * 100000001 );
if(!(newref in stacks))
uniqueRef = true;
} while(!uniqueRef);
ref = newref;
}
if(!(ref in stacks))
stacks[ref] = new $.functionStack._manager(ref, opts);
return stacks[ref];
}
function destroy(ref){
if(ref in stacks) {
stacks[ref].clear(true);
delete stacks[ref];
}
}
var publicFunctions = {
create: create,
destroy: destroy
};
return publicFunctions;
})();
/**
* The initialiser for individual function stacks
*/
$.functionStack._manager = function(ref, opts){
this.reference = ref;
this.stack = [];
this.opts = $.extend({}, $.functionStack.defaults, opts);
};
/**
* Implements all the various methods for our function stacks
*/
$.functionStack._manager.prototype = {
add: function(o) {
/**
* Default the values
*/
var autonext = this.opts.autonext;
var func = false;
/**
* If what we expect to be the function is an object,
* Then maybe the function and autonext values were passed in there
*/
if(typeof o == "object") {
if( "autonext" in o )
autonext = o.autonext;
if( "run" in o )
func = o.run;
}
else if (typeof o == "function") {
func = o;
}
/**
* If your func var is still not a function, then something went wrong and we dont want to add it too the stack
*/
if(typeof func != "function")
return false;
/**
* Build the object that we will push onto the function stack
* This object could just be passed in full to the add function
* ( Maybe thats why i put that check up there ^ ) but im a control freak!
*/
var stackItem = {
"run": func,
"autonext": autonext
};
/**
* push it onto our stack
*/
this._pushToStack(stackItem);
/**
* If the stack is one (i.e. was empty before this), then run this immediately
* N.B: THERE MAY BE TIMES WHEN A FUNCTION NEEDS TO BE ADDED BUT NOT CALLED IMMEDIATELY
* OR THERE MAY BE A FUNCTION FROM THIS STACK ALREADY RUNNING.
* I'LL FIND A WAY TO RESOLVE THOSE SCENARIOS WHEN MY BRAIN ISN'T IN MY ARSE
*/
if(this.count() == 1 && autonext)
this.next();
},
/**
* Adds a stack item object to the stack
*/
_pushToStack: function(o) {
this.stack.push(o);
},
/**
* Run next function on the stack (Just a publicly accesible caller)
*/
next: function(){
this._runNext();
},
/**
* The bit that actually runs the next function on the stack
*/
_runNext: function(){
if(this.isEmpty())
return;
var item = this.stack.shift();
/**
* Run the function passing in the stack as "this.functionStack".
* This will help when manually firing the next stack cycle
*/
item.run.apply({ 'functionStack': this });
/**
* If we have to automagically run the next one, then do so!
*/
if(item.autonext) {
this.next();
}
},
/**
* Returns the length of the stack
*/
count: function() {
return this.stack.length;
},
/**
* Returns wether the stack is empty (can be used as a "has next" method)
*/
isEmpty: function() {
return this.count() > 0 ? false : true;
}
};
/**
* The defaults
*/
$.functionStack.defaults = {
autonext : true
};
/**
* Expose public stack methods to the parent object
*/
$.each($.functionStack._manager.prototype, function(n, fn){
if(n.indexOf('_') === 0 || !$.isFunction(fn)){return;}
$.functionStack[n] = function(ref, o){
if(!stacks[ref]){
if(n === 'add'){
$.functionStack.create(ref, o);
} else {
return;
}
}
var args = Array.prototype.slice.call(arguments, 1);
stacks[ref][n].apply(stacks[ref], args);
};
});
})(jQuery);