c5169e0e
Renato De Donato
a new hope
|
1
|
define([
|
74249687
Luigi Serra
Cross browser con...
|
2
3
4
5
|
"./core",
"./var/rnotwhite"
], function( jQuery, rnotwhite ) {
|
c5169e0e
Renato De Donato
a new hope
|
6
7
8
9
|
// String to Object options format cache
var optionsCache = {};
// Convert String-formatted options into Object-formatted ones and store in cache
|
74249687
Luigi Serra
Cross browser con...
|
10
|
function createOptions( options ) {
|
c5169e0e
Renato De Donato
a new hope
|
11
|
var object = optionsCache[ options ] = {};
|
74249687
Luigi Serra
Cross browser con...
|
12
13
|
jQuery.each( options.match( rnotwhite ) || [], function( _, flag ) {
object[ flag ] = true;
|
c5169e0e
Renato De Donato
a new hope
|
14
|
});
|
74249687
Luigi Serra
Cross browser con...
|
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
|
return object;
}
/*
* Create a callback list using the following parameters:
*
* options: an optional list of space-separated options that will change how
* the callback list behaves or a more traditional option object
*
* By default a callback list will act like an event callback list and can be
* "fired" multiple times.
*
* Possible options:
*
* once: will ensure the callback list can only be fired once (like a Deferred)
*
* memory: will keep track of previous values and will call any callback added
* after the list has been fired right away with the latest "memorized"
* values (like a Deferred)
*
* unique: will ensure a callback can only be added once (no duplicate in the list)
*
* stopOnFalse: interrupt callings when a callback returns false
*
*/
jQuery.Callbacks = function( options ) {
// Convert options from String-formatted to Object-formatted if needed
// (we check in cache first)
options = typeof options === "string" ?
|
c5169e0e
Renato De Donato
a new hope
|
45
|
( optionsCache[ options ] || createOptions( options ) ) :
|
74249687
Luigi Serra
Cross browser con...
|
46
47
|
jQuery.extend( {}, options );
|
c5169e0e
Renato De Donato
a new hope
|
48
|
var // Last fire value (for non-forgettable lists)
|
74249687
Luigi Serra
Cross browser con...
|
49
|
memory,
|
74249687
Luigi Serra
Cross browser con...
|
50
51
|
// Flag to know if list was already fired
fired,
|
c5169e0e
Renato De Donato
a new hope
|
52
53
54
55
56
57
58
59
|
// Flag to know if list is currently firing
firing,
// First callback to fire (used internally by add and fireWith)
firingStart,
// End of the loop when firing
firingLength,
// Index of currently firing callback (modified by remove if needed)
firingIndex,
|
74249687
Luigi Serra
Cross browser con...
|
60
61
|
// Actual callback list
list = [],
|
c5169e0e
Renato De Donato
a new hope
|
62
63
|
// Stack of fire calls for repeatable lists
stack = !options.once && [],
|
74249687
Luigi Serra
Cross browser con...
|
64
|
// Fire callbacks
|
c5169e0e
Renato De Donato
a new hope
|
65
66
67
68
69
70
71
72
73
74
75
|
fire = function( data ) {
memory = options.memory && data;
fired = true;
firingIndex = firingStart || 0;
firingStart = 0;
firingLength = list.length;
firing = true;
for ( ; list && firingIndex < firingLength; firingIndex++ ) {
if ( list[ firingIndex ].apply( data[ 0 ], data[ 1 ] ) === false && options.stopOnFalse ) {
memory = false; // To prevent further calls using add
break;
|
74249687
Luigi Serra
Cross browser con...
|
76
77
|
}
}
|
74249687
Luigi Serra
Cross browser con...
|
78
|
firing = false;
|
c5169e0e
Renato De Donato
a new hope
|
79
80
81
82
83
84
|
if ( list ) {
if ( stack ) {
if ( stack.length ) {
fire( stack.shift() );
}
} else if ( memory ) {
|
74249687
Luigi Serra
Cross browser con...
|
85
|
list = [];
|
74249687
Luigi Serra
Cross browser con...
|
86
|
} else {
|
c5169e0e
Renato De Donato
a new hope
|
87
|
self.disable();
|
74249687
Luigi Serra
Cross browser con...
|
88
89
90
|
}
}
},
|
74249687
Luigi Serra
Cross browser con...
|
91
92
|
// Actual Callbacks object
self = {
|
74249687
Luigi Serra
Cross browser con...
|
93
94
95
|
// Add a callback or a collection of callbacks to the list
add: function() {
if ( list ) {
|
c5169e0e
Renato De Donato
a new hope
|
96
97
98
|
// First, we save the current length
var start = list.length;
(function add( args ) {
|
74249687
Luigi Serra
Cross browser con...
|
99
|
jQuery.each( args, function( _, arg ) {
|
c5169e0e
Renato De Donato
a new hope
|
100
101
|
var type = jQuery.type( arg );
if ( type === "function" ) {
|
74249687
Luigi Serra
Cross browser con...
|
102
103
104
|
if ( !options.unique || !self.has( arg ) ) {
list.push( arg );
}
|
c5169e0e
Renato De Donato
a new hope
|
105
|
} else if ( arg && arg.length && type !== "string" ) {
|
74249687
Luigi Serra
Cross browser con...
|
106
107
108
|
// Inspect recursively
add( arg );
}
|
c5169e0e
Renato De Donato
a new hope
|
109
110
111
112
113
114
115
116
117
118
119
|
});
})( arguments );
// Do we need to add the callbacks to the
// current firing batch?
if ( firing ) {
firingLength = list.length;
// With memory, if we're not firing then
// we should call right away
} else if ( memory ) {
firingStart = start;
fire( memory );
|
74249687
Luigi Serra
Cross browser con...
|
120
121
122
123
|
}
}
return this;
},
|
74249687
Luigi Serra
Cross browser con...
|
124
125
|
// Remove a callback from the list
remove: function() {
|
c5169e0e
Renato De Donato
a new hope
|
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
if ( list ) {
jQuery.each( arguments, function( _, arg ) {
var index;
while ( ( index = jQuery.inArray( arg, list, index ) ) > -1 ) {
list.splice( index, 1 );
// Handle firing indexes
if ( firing ) {
if ( index <= firingLength ) {
firingLength--;
}
if ( index <= firingIndex ) {
firingIndex--;
}
}
|
74249687
Luigi Serra
Cross browser con...
|
140
|
}
|
c5169e0e
Renato De Donato
a new hope
|
141
142
|
});
}
|
74249687
Luigi Serra
Cross browser con...
|
143
144
|
return this;
},
|
74249687
Luigi Serra
Cross browser con...
|
145
146
147
|
// Check if a given callback is in the list.
// If no argument is given, return whether or not list has callbacks attached.
has: function( fn ) {
|
c5169e0e
Renato De Donato
a new hope
|
148
|
return fn ? jQuery.inArray( fn, list ) > -1 : !!( list && list.length );
|
74249687
Luigi Serra
Cross browser con...
|
149
|
},
|
74249687
Luigi Serra
Cross browser con...
|
150
151
|
// Remove all callbacks from the list
empty: function() {
|
c5169e0e
Renato De Donato
a new hope
|
152
153
|
list = [];
firingLength = 0;
|
74249687
Luigi Serra
Cross browser con...
|
154
155
|
return this;
},
|
c5169e0e
Renato De Donato
a new hope
|
156
|
// Have the list do nothing anymore
|
74249687
Luigi Serra
Cross browser con...
|
157
|
disable: function() {
|
c5169e0e
Renato De Donato
a new hope
|
158
|
list = stack = memory = undefined;
|
74249687
Luigi Serra
Cross browser con...
|
159
160
|
return this;
},
|
c5169e0e
Renato De Donato
a new hope
|
161
|
// Is it disabled?
|
74249687
Luigi Serra
Cross browser con...
|
162
163
164
|
disabled: function() {
return !list;
},
|
c5169e0e
Renato De Donato
a new hope
|
165
|
// Lock the list in its current state
|
74249687
Luigi Serra
Cross browser con...
|
166
|
lock: function() {
|
c5169e0e
Renato De Donato
a new hope
|
167
|
stack = undefined;
|
74249687
Luigi Serra
Cross browser con...
|
168
|
if ( !memory ) {
|
c5169e0e
Renato De Donato
a new hope
|
169
|
self.disable();
|
74249687
Luigi Serra
Cross browser con...
|
170
171
172
|
}
return this;
},
|
c5169e0e
Renato De Donato
a new hope
|
173
|
// Is it locked?
|
74249687
Luigi Serra
Cross browser con...
|
174
|
locked: function() {
|
c5169e0e
Renato De Donato
a new hope
|
175
|
return !stack;
|
74249687
Luigi Serra
Cross browser con...
|
176
|
},
|
74249687
Luigi Serra
Cross browser con...
|
177
178
|
// Call all callbacks with the given context and arguments
fireWith: function( context, args ) {
|
c5169e0e
Renato De Donato
a new hope
|
179
|
if ( list && ( !fired || stack ) ) {
|
74249687
Luigi Serra
Cross browser con...
|
180
181
|
args = args || [];
args = [ context, args.slice ? args.slice() : args ];
|
c5169e0e
Renato De Donato
a new hope
|
182
183
184
185
|
if ( firing ) {
stack.push( args );
} else {
fire( args );
|
74249687
Luigi Serra
Cross browser con...
|
186
187
188
189
|
}
}
return this;
},
|
74249687
Luigi Serra
Cross browser con...
|
190
191
192
193
194
|
// Call all the callbacks with the given arguments
fire: function() {
self.fireWith( this, arguments );
return this;
},
|
74249687
Luigi Serra
Cross browser con...
|
195
196
197
198
199
200
201
202
203
204
|
// To know if the callbacks have already been called at least once
fired: function() {
return !!fired;
}
};
return self;
};
return jQuery;
|
c5169e0e
Renato De Donato
a new hope
|
205
|
});
|