jssignals-rails 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  Dir["LICENSE"] +
23
23
  Dir["Rakefile"] +
24
24
  Dir["README.md"] +
25
+ Dir["**/*.js"] +
25
26
  Dir["**/*.rb"]
26
27
  s.require_paths = ["lib"]
27
28
  end
@@ -1,6 +1,6 @@
1
1
  module Jssignals
2
2
  module Rails
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  JSSIGNALS_VERSION = "0.7.4";
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Jssignals::Rails do
4
- it { Jssignals::Rails::VERSION.should == "1.0.0" }
4
+ it { Jssignals::Rails::VERSION.should == "1.0.1" }
5
5
  it { Jssignals::Rails::JSSIGNALS_VERSION.should == "0.7.4" }
6
6
  end
@@ -0,0 +1,418 @@
1
+ /*jslint onevar:true, undef:true, newcap:true, regexp:true, bitwise:true, maxerr:50, indent:4, white:false, nomen:false, plusplus:false */
2
+ /*global define:false, require:false, exports:false, module:false*/
3
+
4
+ /** @license
5
+ * JS Signals <http://millermedeiros.github.com/js-signals/>
6
+ * Released under the MIT license
7
+ * Author: Miller Medeiros
8
+ * Version: 0.7.4 - Build: 252 (2012/02/24 10:30 PM)
9
+ */
10
+
11
+ (function(global){
12
+
13
+ /**
14
+ * @namespace Signals Namespace - Custom event/messaging system based on AS3 Signals
15
+ * @name signals
16
+ */
17
+ var signals = /** @lends signals */{
18
+ /**
19
+ * Signals Version Number
20
+ * @type String
21
+ * @const
22
+ */
23
+ VERSION : '0.7.4'
24
+ };
25
+
26
+
27
+ // SignalBinding -------------------------------------------------
28
+ //================================================================
29
+
30
+ /**
31
+ * Object that represents a binding between a Signal and a listener function.
32
+ * <br />- <strong>This is an internal constructor and shouldn't be called by regular users.</strong>
33
+ * <br />- inspired by Joa Ebert AS3 SignalBinding and Robert Penner's Slot classes.
34
+ * @author Miller Medeiros
35
+ * @constructor
36
+ * @internal
37
+ * @name signals.SignalBinding
38
+ * @param {signals.Signal} signal Reference to Signal object that listener is currently bound to.
39
+ * @param {Function} listener Handler function bound to the signal.
40
+ * @param {boolean} isOnce If binding should be executed just once.
41
+ * @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
42
+ * @param {Number} [priority] The priority level of the event listener. (default = 0).
43
+ */
44
+ function SignalBinding(signal, listener, isOnce, listenerContext, priority) {
45
+
46
+ /**
47
+ * Handler function bound to the signal.
48
+ * @type Function
49
+ * @private
50
+ */
51
+ this._listener = listener;
52
+
53
+ /**
54
+ * If binding should be executed just once.
55
+ * @type boolean
56
+ * @private
57
+ */
58
+ this._isOnce = isOnce;
59
+
60
+ /**
61
+ * Context on which listener will be executed (object that should represent the `this` variable inside listener function).
62
+ * @memberOf signals.SignalBinding.prototype
63
+ * @name context
64
+ * @type Object|undefined|null
65
+ */
66
+ this.context = listenerContext;
67
+
68
+ /**
69
+ * Reference to Signal object that listener is currently bound to.
70
+ * @type signals.Signal
71
+ * @private
72
+ */
73
+ this._signal = signal;
74
+
75
+ /**
76
+ * Listener priority
77
+ * @type Number
78
+ * @private
79
+ */
80
+ this._priority = priority || 0;
81
+ }
82
+
83
+ SignalBinding.prototype = /** @lends signals.SignalBinding.prototype */ {
84
+
85
+ /**
86
+ * If binding is active and should be executed.
87
+ * @type boolean
88
+ */
89
+ active : true,
90
+
91
+ /**
92
+ * Default parameters passed to listener during `Signal.dispatch` and `SignalBinding.execute`. (curried parameters)
93
+ * @type Array|null
94
+ */
95
+ params : null,
96
+
97
+ /**
98
+ * Call listener passing arbitrary parameters.
99
+ * <p>If binding was added using `Signal.addOnce()` it will be automatically removed from signal dispatch queue, this method is used internally for the signal dispatch.</p>
100
+ * @param {Array} [paramsArr] Array of parameters that should be passed to the listener
101
+ * @return {*} Value returned by the listener.
102
+ */
103
+ execute : function (paramsArr) {
104
+ var handlerReturn, params;
105
+ if (this.active && !!this._listener) {
106
+ params = this.params? this.params.concat(paramsArr) : paramsArr;
107
+ handlerReturn = this._listener.apply(this.context, params);
108
+ if (this._isOnce) {
109
+ this.detach();
110
+ }
111
+ }
112
+ return handlerReturn;
113
+ },
114
+
115
+ /**
116
+ * Detach binding from signal.
117
+ * - alias to: mySignal.remove(myBinding.getListener());
118
+ * @return {Function|null} Handler function bound to the signal or `null` if binding was previously detached.
119
+ */
120
+ detach : function () {
121
+ return this.isBound()? this._signal.remove(this._listener, this.context) : null;
122
+ },
123
+
124
+ /**
125
+ * @return {Boolean} `true` if binding is still bound to the signal and have a listener.
126
+ */
127
+ isBound : function () {
128
+ return (!!this._signal && !!this._listener);
129
+ },
130
+
131
+ /**
132
+ * @return {Function} Handler function bound to the signal.
133
+ */
134
+ getListener : function () {
135
+ return this._listener;
136
+ },
137
+
138
+ /**
139
+ * Delete instance properties
140
+ * @private
141
+ */
142
+ _destroy : function () {
143
+ delete this._signal;
144
+ delete this._listener;
145
+ delete this.context;
146
+ },
147
+
148
+ /**
149
+ * @return {boolean} If SignalBinding will only be executed once.
150
+ */
151
+ isOnce : function () {
152
+ return this._isOnce;
153
+ },
154
+
155
+ /**
156
+ * @return {string} String representation of the object.
157
+ */
158
+ toString : function () {
159
+ return '[SignalBinding isOnce:' + this._isOnce +', isBound:'+ this.isBound() +', active:' + this.active + ']';
160
+ }
161
+
162
+ };
163
+
164
+
165
+ /*global signals:false, SignalBinding:false*/
166
+
167
+ // Signal --------------------------------------------------------
168
+ //================================================================
169
+
170
+ function validateListener(listener, fnName) {
171
+ if (typeof listener !== 'function') {
172
+ throw new Error( 'listener is a required param of {fn}() and should be a Function.'.replace('{fn}', fnName) );
173
+ }
174
+ }
175
+
176
+ /**
177
+ * Custom event broadcaster
178
+ * <br />- inspired by Robert Penner's AS3 Signals.
179
+ * @author Miller Medeiros
180
+ * @constructor
181
+ */
182
+ signals.Signal = function () {
183
+ /**
184
+ * @type Array.<SignalBinding>
185
+ * @private
186
+ */
187
+ this._bindings = [];
188
+ this._prevParams = null;
189
+ };
190
+
191
+ signals.Signal.prototype = {
192
+
193
+ /**
194
+ * If Signal should keep record of previously dispatched parameters and
195
+ * automatically execute listener during `add()`/`addOnce()` if Signal was
196
+ * already dispatched before.
197
+ * @type boolean
198
+ */
199
+ memorize : false,
200
+
201
+ /**
202
+ * @type boolean
203
+ * @private
204
+ */
205
+ _shouldPropagate : true,
206
+
207
+ /**
208
+ * If Signal is active and should broadcast events.
209
+ * <p><strong>IMPORTANT:</strong> Setting this property during a dispatch will only affect the next dispatch, if you want to stop the propagation of a signal use `halt()` instead.</p>
210
+ * @type boolean
211
+ */
212
+ active : true,
213
+
214
+ /**
215
+ * @param {Function} listener
216
+ * @param {boolean} isOnce
217
+ * @param {Object} [listenerContext]
218
+ * @param {Number} [priority]
219
+ * @return {SignalBinding}
220
+ * @private
221
+ */
222
+ _registerListener : function (listener, isOnce, listenerContext, priority) {
223
+
224
+ var prevIndex = this._indexOfListener(listener, listenerContext),
225
+ binding;
226
+
227
+ if (prevIndex !== -1) {
228
+ binding = this._bindings[prevIndex];
229
+ if (binding.isOnce() !== isOnce) {
230
+ throw new Error('You cannot add'+ (isOnce? '' : 'Once') +'() then add'+ (!isOnce? '' : 'Once') +'() the same listener without removing the relationship first.');
231
+ }
232
+ } else {
233
+ binding = new SignalBinding(this, listener, isOnce, listenerContext, priority);
234
+ this._addBinding(binding);
235
+ }
236
+
237
+ if(this.memorize && this._prevParams){
238
+ binding.execute(this._prevParams);
239
+ }
240
+
241
+ return binding;
242
+ },
243
+
244
+ /**
245
+ * @param {SignalBinding} binding
246
+ * @private
247
+ */
248
+ _addBinding : function (binding) {
249
+ //simplified insertion sort
250
+ var n = this._bindings.length;
251
+ do { --n; } while (this._bindings[n] && binding._priority <= this._bindings[n]._priority);
252
+ this._bindings.splice(n + 1, 0, binding);
253
+ },
254
+
255
+ /**
256
+ * @param {Function} listener
257
+ * @return {number}
258
+ * @private
259
+ */
260
+ _indexOfListener : function (listener, context) {
261
+ var n = this._bindings.length,
262
+ cur;
263
+ while (n--) {
264
+ cur = this._bindings[n];
265
+ if (cur._listener === listener && cur.context === context) {
266
+ return n;
267
+ }
268
+ }
269
+ return -1;
270
+ },
271
+
272
+ /**
273
+ * Check if listener was attached to Signal.
274
+ * @param {Function} listener
275
+ * @param {Object} [context]
276
+ * @return {boolean} if Signal has the specified listener.
277
+ */
278
+ has : function (listener, context) {
279
+ return this._indexOfListener(listener, context) !== -1;
280
+ },
281
+
282
+ /**
283
+ * Add a listener to the signal.
284
+ * @param {Function} listener Signal handler function.
285
+ * @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
286
+ * @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
287
+ * @return {SignalBinding} An Object representing the binding between the Signal and listener.
288
+ */
289
+ add : function (listener, listenerContext, priority) {
290
+ validateListener(listener, 'add');
291
+ return this._registerListener(listener, false, listenerContext, priority);
292
+ },
293
+
294
+ /**
295
+ * Add listener to the signal that should be removed after first execution (will be executed only once).
296
+ * @param {Function} listener Signal handler function.
297
+ * @param {Object} [listenerContext] Context on which listener will be executed (object that should represent the `this` variable inside listener function).
298
+ * @param {Number} [priority] The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
299
+ * @return {SignalBinding} An Object representing the binding between the Signal and listener.
300
+ */
301
+ addOnce : function (listener, listenerContext, priority) {
302
+ validateListener(listener, 'addOnce');
303
+ return this._registerListener(listener, true, listenerContext, priority);
304
+ },
305
+
306
+ /**
307
+ * Remove a single listener from the dispatch queue.
308
+ * @param {Function} listener Handler function that should be removed.
309
+ * @param {Object} [context] Execution context (since you can add the same handler multiple times if executing in a different context).
310
+ * @return {Function} Listener handler function.
311
+ */
312
+ remove : function (listener, context) {
313
+ validateListener(listener, 'remove');
314
+
315
+ var i = this._indexOfListener(listener, context);
316
+ if (i !== -1) {
317
+ this._bindings[i]._destroy(); //no reason to a SignalBinding exist if it isn't attached to a signal
318
+ this._bindings.splice(i, 1);
319
+ }
320
+ return listener;
321
+ },
322
+
323
+ /**
324
+ * Remove all listeners from the Signal.
325
+ */
326
+ removeAll : function () {
327
+ var n = this._bindings.length;
328
+ while (n--) {
329
+ this._bindings[n]._destroy();
330
+ }
331
+ this._bindings.length = 0;
332
+ },
333
+
334
+ /**
335
+ * @return {number} Number of listeners attached to the Signal.
336
+ */
337
+ getNumListeners : function () {
338
+ return this._bindings.length;
339
+ },
340
+
341
+ /**
342
+ * Stop propagation of the event, blocking the dispatch to next listeners on the queue.
343
+ * <p><strong>IMPORTANT:</strong> should be called only during signal dispatch, calling it before/after dispatch won't affect signal broadcast.</p>
344
+ * @see signals.Signal.prototype.disable
345
+ */
346
+ halt : function () {
347
+ this._shouldPropagate = false;
348
+ },
349
+
350
+ /**
351
+ * Dispatch/Broadcast Signal to all listeners added to the queue.
352
+ * @param {...*} [params] Parameters that should be passed to each handler.
353
+ */
354
+ dispatch : function (params) {
355
+ if (! this.active) {
356
+ return;
357
+ }
358
+
359
+ var paramsArr = Array.prototype.slice.call(arguments),
360
+ n = this._bindings.length,
361
+ bindings;
362
+
363
+ if (this.memorize) {
364
+ this._prevParams = paramsArr;
365
+ }
366
+
367
+ if (! n) {
368
+ //should come after memorize
369
+ return;
370
+ }
371
+
372
+ bindings = this._bindings.slice(); //clone array in case add/remove items during dispatch
373
+ this._shouldPropagate = true; //in case `halt` was called before dispatch or during the previous dispatch.
374
+
375
+ //execute all callbacks until end of the list or until a callback returns `false` or stops propagation
376
+ //reverse loop since listeners with higher priority will be added at the end of the list
377
+ do { n--; } while (bindings[n] && this._shouldPropagate && bindings[n].execute(paramsArr) !== false);
378
+ },
379
+
380
+ /**
381
+ * Forget memorized arguments.
382
+ * @see signals.Signal.memorize
383
+ */
384
+ forget : function(){
385
+ this._prevParams = null;
386
+ },
387
+
388
+ /**
389
+ * Remove all bindings from signal and destroy any reference to external objects (destroy Signal object).
390
+ * <p><strong>IMPORTANT:</strong> calling any method on the signal instance after calling dispose will throw errors.</p>
391
+ */
392
+ dispose : function () {
393
+ this.removeAll();
394
+ delete this._bindings;
395
+ delete this._prevParams;
396
+ },
397
+
398
+ /**
399
+ * @return {string} String representation of the object.
400
+ */
401
+ toString : function () {
402
+ return '[Signal active:'+ this.active +' numListeners:'+ this.getNumListeners() +']';
403
+ }
404
+
405
+ };
406
+
407
+
408
+ //exports to multiple environments
409
+ if(typeof define === 'function' && define.amd){ //AMD
410
+ define(signals);
411
+ } else if (typeof module !== 'undefined' && module.exports){ //node
412
+ module.exports = signals;
413
+ } else { //browser
414
+ //use string because of Google closure compiler ADVANCED_MODE
415
+ global['signals'] = signals;
416
+ }
417
+
418
+ }(this));
@@ -0,0 +1,13 @@
1
+ /*
2
+
3
+ JS Signals <http://millermedeiros.github.com/js-signals/>
4
+ Released under the MIT license
5
+ Author: Miller Medeiros
6
+ Version: 0.7.4 - Build: 252 (2012/02/24 10:30 PM)
7
+ */
8
+ (function(h){function g(a,b,c,d,e){this._listener=b;this._isOnce=c;this.context=d;this._signal=a;this._priority=e||0}function f(a,b){if(typeof a!=="function")throw Error("listener is a required param of {fn}() and should be a Function.".replace("{fn}",b));}var e={VERSION:"0.7.4"};g.prototype={active:!0,params:null,execute:function(a){var b;this.active&&this._listener&&(a=this.params?this.params.concat(a):a,b=this._listener.apply(this.context,a),this._isOnce&&this.detach());return b},detach:function(){return this.isBound()?
9
+ this._signal.remove(this._listener,this.context):null},isBound:function(){return!!this._signal&&!!this._listener},getListener:function(){return this._listener},_destroy:function(){delete this._signal;delete this._listener;delete this.context},isOnce:function(){return this._isOnce},toString:function(){return"[SignalBinding isOnce:"+this._isOnce+", isBound:"+this.isBound()+", active:"+this.active+"]"}};e.Signal=function(){this._bindings=[];this._prevParams=null};e.Signal.prototype={memorize:!1,_shouldPropagate:!0,
10
+ active:!0,_registerListener:function(a,b,c,d){var e=this._indexOfListener(a,c);if(e!==-1){if(a=this._bindings[e],a.isOnce()!==b)throw Error("You cannot add"+(b?"":"Once")+"() then add"+(!b?"":"Once")+"() the same listener without removing the relationship first.");}else a=new g(this,a,b,c,d),this._addBinding(a);this.memorize&&this._prevParams&&a.execute(this._prevParams);return a},_addBinding:function(a){var b=this._bindings.length;do--b;while(this._bindings[b]&&a._priority<=this._bindings[b]._priority);
11
+ this._bindings.splice(b+1,0,a)},_indexOfListener:function(a,b){for(var c=this._bindings.length,d;c--;)if(d=this._bindings[c],d._listener===a&&d.context===b)return c;return-1},has:function(a,b){return this._indexOfListener(a,b)!==-1},add:function(a,b,c){f(a,"add");return this._registerListener(a,!1,b,c)},addOnce:function(a,b,c){f(a,"addOnce");return this._registerListener(a,!0,b,c)},remove:function(a,b){f(a,"remove");var c=this._indexOfListener(a,b);c!==-1&&(this._bindings[c]._destroy(),this._bindings.splice(c,
12
+ 1));return a},removeAll:function(){for(var a=this._bindings.length;a--;)this._bindings[a]._destroy();this._bindings.length=0},getNumListeners:function(){return this._bindings.length},halt:function(){this._shouldPropagate=!1},dispatch:function(a){if(this.active){var b=Array.prototype.slice.call(arguments),c=this._bindings.length,d;if(this.memorize)this._prevParams=b;if(c){d=this._bindings.slice();this._shouldPropagate=!0;do c--;while(d[c]&&this._shouldPropagate&&d[c].execute(b)!==!1)}}},forget:function(){this._prevParams=
13
+ null},dispose:function(){this.removeAll();delete this._bindings;delete this._prevParams},toString:function(){return"[Signal active:"+this.active+" numListeners:"+this.getNumListeners()+"]"}};typeof define==="function"&&define.amd?define(e):typeof module!=="undefined"&&module.exports?module.exports=e:h.signals=e})(this);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jssignals-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &23104200 !ruby/object:Gem::Requirement
16
+ requirement: &23624004 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *23104200
24
+ version_requirements: *23624004
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &23103732 !ruby/object:Gem::Requirement
27
+ requirement: &23623452 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '2.0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *23103732
35
+ version_requirements: *23623452
36
36
  description: Provides js-signals for use with Rails 3
37
37
  email: philostler@gmail.com
38
38
  executables: []
@@ -46,6 +46,8 @@ files:
46
46
  - LICENSE
47
47
  - Rakefile
48
48
  - README.md
49
+ - vendor/assets/javascripts/signals.js
50
+ - vendor/assets/javascripts/signals.min.js
49
51
  - lib/generators/jssignals/install/install_generator.rb
50
52
  - lib/jssignals/rails/engine.rb
51
53
  - lib/jssignals/rails/railtie.rb