sproutcore 0.9.14 → 0.9.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. data/History.txt +43 -0
  2. data/Manifest.txt +12 -3
  3. data/bin/sc-build +19 -3
  4. data/bin/sc-install +5 -0
  5. data/bin/sc-remove +5 -0
  6. data/bin/sc-update +5 -0
  7. data/frameworks/prototype/prototype.js +267 -230
  8. data/frameworks/sproutcore/HISTORY +281 -135
  9. data/frameworks/sproutcore/controllers/array.js +133 -22
  10. data/frameworks/sproutcore/controllers/collection.js +4 -5
  11. data/frameworks/sproutcore/controllers/object.js +8 -2
  12. data/frameworks/sproutcore/core.js +361 -159
  13. data/frameworks/sproutcore/{foundation → debug}/unittest.js +3 -3
  14. data/frameworks/sproutcore/english.lproj/detect-browser +1 -1
  15. data/frameworks/sproutcore/english.lproj/theme.css +2 -2
  16. data/frameworks/sproutcore/foundation/application.js +6 -1
  17. data/frameworks/sproutcore/foundation/benchmark.js +37 -11
  18. data/frameworks/sproutcore/foundation/date.js +1 -1
  19. data/frameworks/sproutcore/foundation/enumerator.js +105 -0
  20. data/frameworks/sproutcore/foundation/object.js +19 -20
  21. data/frameworks/sproutcore/foundation/responder.js +1 -1
  22. data/frameworks/sproutcore/foundation/set.js +164 -57
  23. data/frameworks/sproutcore/foundation/string.js +151 -47
  24. data/frameworks/sproutcore/foundation/utils.js +84 -3
  25. data/frameworks/sproutcore/lib/collection_view.rb +1 -0
  26. data/frameworks/sproutcore/license.js +28 -0
  27. data/frameworks/sproutcore/mixins/array.js +73 -209
  28. data/frameworks/sproutcore/mixins/delegate_support.js +1 -1
  29. data/frameworks/sproutcore/mixins/enumerable.js +1006 -0
  30. data/frameworks/sproutcore/mixins/observable.js +153 -84
  31. data/frameworks/sproutcore/mixins/selection_support.js +13 -1
  32. data/frameworks/sproutcore/models/record.js +74 -27
  33. data/frameworks/sproutcore/models/store.js +7 -3
  34. data/frameworks/sproutcore/server/rails_server.js +82 -0
  35. data/frameworks/sproutcore/server/rest_server.js +178 -0
  36. data/frameworks/sproutcore/{foundation → server}/server.js +101 -48
  37. data/frameworks/sproutcore/tests/core/guidFor.rhtml +114 -0
  38. data/frameworks/sproutcore/tests/foundation/array.rhtml +6 -7
  39. data/frameworks/sproutcore/tests/foundation/set.rhtml +254 -0
  40. data/frameworks/sproutcore/tests/mixins/enumerable.rhtml +421 -0
  41. data/frameworks/sproutcore/tests/mixins/observable.rhtml +127 -0
  42. data/frameworks/sproutcore/tests/models/model.rhtml +23 -22
  43. data/frameworks/sproutcore/tests/views/collection/incremental_rendering.rhtml +2 -2
  44. data/frameworks/sproutcore/tests/views/view/clippingFrame.rhtml +112 -109
  45. data/frameworks/sproutcore/tests/views/view/frame.rhtml +91 -88
  46. data/frameworks/sproutcore/validators/date.js +1 -7
  47. data/frameworks/sproutcore/views/collection/collection.js +7 -2
  48. data/frameworks/sproutcore/views/list_item.js +141 -3
  49. data/frameworks/sproutcore/views/split.js +14 -11
  50. data/frameworks/sproutcore/views/view.js +9 -6
  51. data/lib/sproutcore/build_tools/html_builder.rb +19 -3
  52. data/lib/sproutcore/build_tools/resource_builder.rb +9 -3
  53. data/lib/sproutcore/bundle.rb +21 -0
  54. data/lib/sproutcore/bundle_manifest.rb +64 -20
  55. data/lib/sproutcore/helpers/capture_helper.rb +2 -2
  56. data/lib/sproutcore/library.rb +33 -9
  57. data/lib/sproutcore/merb/bundle_controller.rb +16 -5
  58. data/lib/sproutcore/version.rb +1 -1
  59. data/lib/sproutcore/view_helpers.rb +1 -1
  60. data/{sc-config.rb → sc-config} +5 -2
  61. metadata +24 -5
@@ -0,0 +1,1006 @@
1
+ // ==========================================================================
2
+ // SproutCore -- JavaScript Application Framework
3
+ // copyright 2006-2008, Sprout Systems, Inc. and contributors.
4
+ // ==========================================================================
5
+
6
+ require('core') ;
7
+
8
+ /**
9
+ @namespace
10
+
11
+ This mixin defines the common interface implemented by enumerable objects
12
+ in SproutCore. Most of these methods follow the standard Array iteration
13
+ API defined up to JavaScript 1.8 (excluding language-specific features that
14
+ cannot be emulated in older versions of JavaScript).
15
+
16
+ This mixin is applied automatically to the Array class on page load, so you
17
+ can use any of these methods on simple arrays. If Array already implements
18
+ one of these methods, the mixin will not override them.
19
+
20
+ h3. Writing Your Own Enumerable
21
+
22
+ To make your own custom class enumerable, you need two items:
23
+
24
+ 1. You must have a length property. This property should change whenever
25
+ the number of items in your enumerable object changes. If you using this
26
+ with an SC.Object subclass, you should be sure to change the length
27
+ property using set().
28
+
29
+ 2. If you must implement nextObject(). See documentation.
30
+
31
+ Once you have these two methods implement, apply the SC.Enumerable mixin
32
+ to your class and you will be able to enumerate the contents of your object
33
+ like any other collection.
34
+
35
+ h3. Using SproutCore Enumeration with Other Libraries
36
+
37
+ Many other libraries provide some kind of iterator or enumeration like
38
+ facility. This is often where the most common API conflicts occur.
39
+ SproutCore's API is designed to be as friendly as possible with other
40
+ libraries by implementing only methods that mostly correspond to the
41
+ JavaScript 1.8 API.
42
+
43
+ @static
44
+ @since SproutCore 1.0
45
+ */
46
+ SC.Enumerable = {
47
+
48
+ /**
49
+ Implement this method to make your class enumerable.
50
+
51
+ This method will be call repeatedly during enumeration. The index value
52
+ will always begin with 0 and increment monotonically. You don't have to
53
+ rely on the index value to determine what object to return, but you should
54
+ always check the value and start from the beginning when you see the
55
+ requested index is 0.
56
+
57
+ The previousObject is the object that was returned from the last call
58
+ to nextObject for the current iteration. This is a useful way to
59
+ manage iteration if you are tracing a linked list, for example.
60
+
61
+ Finally the context paramter will always contain a hash you can use as
62
+ a "scratchpad" to maintain any other state you need in order to iterate
63
+ properly. The context object is reused and is not reset between
64
+ iterations so make sure you setup the context with a fresh state whenever
65
+ the index parameter is 0.
66
+
67
+ Generally iterators will continue to call nextObject until the index
68
+ reaches the your current length-1. If you run out of data before this
69
+ time for some reason, you should simply return undefined.
70
+
71
+ The default impementation of this method simply looks up the index.
72
+ This works great on any Array-like objects.
73
+
74
+ @param index {Number} the current index of the iteration
75
+ @param previousObject {Object} the value returned by the last call to nextObject.
76
+ @param context {Object} a context object you can use to maintain state.
77
+ @returns {Object} the next object in the iteration or undefined
78
+ */
79
+ nextObject: function(index, previousObject, context) {
80
+ return (this.objectAt) ? this.objectAt(index) : this[index] ;
81
+ },
82
+
83
+ /**
84
+ Returns a new enumerator for this object. See SC.Enumerator for
85
+ documentation on how to use this object. Enumeration is an alternative
86
+ to using one of the other iterators described here.
87
+
88
+ @returns {SC.Enumerator} an enumerator for the receiver
89
+ */
90
+ enumerator: function() { return SC.Enumerator.create(this); },
91
+
92
+ /**
93
+ Iterates through the enumerable, calling the passed function on each
94
+ item. This method corresponds to the forEach() method defined in
95
+ JavaScript 1.6.
96
+
97
+ The callback method you provide should have the following signature (all
98
+ parameters are optional):
99
+
100
+ {{{
101
+ function(item, index, enumerable) ;
102
+ }}}
103
+
104
+ - *item* is the current item in the iteration.
105
+ - *index* is the current index in the iteration
106
+ - *enumerable* is the enumerable object itself.
107
+
108
+ Note that in addition to a callback, you can also pass an optional target
109
+ object that will be set as "this" on the context. This is a good way
110
+ to give your iterator function access to the current object.
111
+
112
+ @params callback {Function} the callback to execute
113
+ @params target {Object} the target object to use
114
+ @returns {Object} this
115
+ */
116
+ forEach: function(callback, target) {
117
+ if (typeof callback !== "function") throw new TypeError() ;
118
+ var len = (this.get) ? this.get('length') : this.length ;
119
+ if (target === undefined) target = null;
120
+
121
+ var last = null ;
122
+ var context = SC.Enumerator._popContext();
123
+ for(var idx=0;idx<len;idx++) {
124
+ var next = this.nextObject(idx, last, context) ;
125
+ callback.call(target, next, idx, this);
126
+ last = next ;
127
+ }
128
+ last = null ;
129
+ context = SC.Enumerator._pushContext(context);
130
+ return this ;
131
+ },
132
+
133
+ /**
134
+ Maps all of the items in the enumeration to another value, returning
135
+ a new array. This method corresponds to map() defined in JavaScript 1.6.
136
+
137
+ The callback method you provide should have the following signature (all
138
+ parameters are optional):
139
+
140
+ {{{
141
+ function(item, index, enumerable) ;
142
+ }}}
143
+
144
+ - *item* is the current item in the iteration.
145
+ - *index* is the current index in the iteration
146
+ - *enumerable* is the enumerable object itself.
147
+
148
+ It should return the mapped value.
149
+
150
+ Note that in addition to a callback, you can also pass an optional target
151
+ object that will be set as "this" on the context. This is a good way
152
+ to give your iterator function access to the current object.
153
+
154
+ @params callback {Function} the callback to execute
155
+ @params target {Object} the target object to use
156
+ @returns {Array} The mapped array.
157
+ */
158
+ map: function(callback, target) {
159
+ if (typeof callback !== "function") throw new TypeError() ;
160
+ var len = (this.get) ? this.get('length') : this.length ;
161
+ if (target === undefined) target = null;
162
+
163
+ var ret = [];
164
+ var last = null ;
165
+ var context = SC.Enumerator._popContext();
166
+ for(var idx=0;idx<len;idx++) {
167
+ var next = this.nextObject(idx, last, context) ;
168
+ ret[idx] = callback.call(target, next, idx, this) ;
169
+ last = next ;
170
+ }
171
+ last = null ;
172
+ context = SC.Enumerator._pushContext(context);
173
+ return ret ;
174
+ },
175
+
176
+ /**
177
+ Similar to map, this specialized function returns the value of the named
178
+ property on all items in the enumeration.
179
+
180
+ @params key {String} name of the property
181
+ @returns {Array} The mapped array.
182
+ */
183
+ mapProperty: function(key) {
184
+ var len = (this.get) ? this.get('length') : this.length ;
185
+ var ret = [];
186
+ var last = null ;
187
+ var context = SC.Enumerator._popContext();
188
+ for(var idx=0;idx<len;idx++) {
189
+ var next = this.nextObject(idx, last, context) ;
190
+ ret[idx] = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
191
+ last = next ;
192
+ }
193
+ last = null ;
194
+ context = SC.Enumerator._pushContext(context);
195
+ return ret ;
196
+ },
197
+
198
+ /**
199
+ Returns an array with all of the items in the enumeration that the passed
200
+ function returns YES for. This method corresponds to filter() defined in
201
+ JavaScript 1.6.
202
+
203
+ The callback method you provide should have the following signature (all
204
+ parameters are optional):
205
+
206
+ {{{
207
+ function(item, index, enumerable) ;
208
+ }}}
209
+
210
+ - *item* is the current item in the iteration.
211
+ - *index* is the current index in the iteration
212
+ - *enumerable* is the enumerable object itself.
213
+
214
+ It should return the YES to include the item in the results, NO otherwise.
215
+
216
+ Note that in addition to a callback, you can also pass an optional target
217
+ object that will be set as "this" on the context. This is a good way
218
+ to give your iterator function access to the current object.
219
+
220
+ @params callback {Function} the callback to execute
221
+ @params target {Object} the target object to use
222
+ @returns {Array} A filtered array.
223
+ */
224
+ filter: function(callback, target) {
225
+ if (typeof callback !== "function") throw new TypeError() ;
226
+ var len = (this.get) ? this.get('length') : this.length ;
227
+ if (target === undefined) target = null;
228
+
229
+ var ret = [];
230
+ var last = null ;
231
+ var context = SC.Enumerator._popContext();
232
+ for(var idx=0;idx<len;idx++) {
233
+ var next = this.nextObject(idx, last, context) ;
234
+ if(callback.call(target, next, idx, this)) ret.push(next) ;
235
+ last = next ;
236
+ }
237
+ last = null ;
238
+ context = SC.Enumerator._pushContext(context);
239
+ return ret ;
240
+ },
241
+
242
+ /**
243
+ Returns an array with just the items with the matched property. You
244
+ can pass an optional second argument with the target value. Otherwise
245
+ this will match any property that evaluates to true.
246
+
247
+ @params key {String} the property to test
248
+ @param value {String} optional value to test against.
249
+ @returns {Array} filtered array
250
+ */
251
+ filterProperty: function(key, value) {
252
+ var len = (this.get) ? this.get('length') : this.length ;
253
+ var ret = [];
254
+ var last = null ;
255
+ var context = SC.Enumerator._popContext();
256
+ for(var idx=0;idx<len;idx++) {
257
+ var next = this.nextObject(idx, last, context) ;
258
+ var cur = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
259
+ var matched = (value === undefined) ? !!cur : SC.isEqual(cur, value);
260
+ if (matched) ret.push(next) ;
261
+ last = next ;
262
+ }
263
+ last = null ;
264
+ context = SC.Enumerator._pushContext(context);
265
+ return ret ;
266
+ },
267
+
268
+ /**
269
+ Returns YES if the passed function returns YES for every item in the
270
+ enumeration. This corresponds with the every() method in JavaScript 1.6.
271
+
272
+ The callback method you provide should have the following signature (all
273
+ parameters are optional):
274
+
275
+ {{{
276
+ function(item, index, enumerable) ;
277
+ }}}
278
+
279
+ - *item* is the current item in the iteration.
280
+ - *index* is the current index in the iteration
281
+ - *enumerable* is the enumerable object itself.
282
+
283
+ It should return the YES or NO.
284
+
285
+ Note that in addition to a callback, you can also pass an optional target
286
+ object that will be set as "this" on the context. This is a good way
287
+ to give your iterator function access to the current object.
288
+
289
+ h4. Example Usage
290
+
291
+ {{{
292
+ if (people.every(isEngineer)) { Paychecks.addBigBonus(); }
293
+ }}}
294
+
295
+ @params callback {Function} the callback to execute
296
+ @params target {Object} the target object to use
297
+ @returns {Boolean}
298
+ */
299
+ every: function(callback, target) {
300
+ if (typeof callback !== "function") throw new TypeError() ;
301
+ var len = (this.get) ? this.get('length') : this.length ;
302
+ if (target === undefined) target = null;
303
+
304
+ var ret = YES;
305
+ var last = null ;
306
+ var context = SC.Enumerator._popContext();
307
+ for(var idx=0;ret && (idx<len);idx++) {
308
+ var next = this.nextObject(idx, last, context) ;
309
+ if(!callback.call(target, next, idx, this)) ret = NO ;
310
+ last = next ;
311
+ }
312
+ last = null ;
313
+ context = SC.Enumerator._pushContext(context);
314
+ return ret ;
315
+ },
316
+
317
+ /**
318
+ Returns YES if the passed property resolves to true for all items in the
319
+ enumerable. This method is often simpler/faster than using a callback.
320
+
321
+ @params key {String} the property to test
322
+ @param value {String} optional value to test against.
323
+ @returns {Array} filtered array
324
+ */
325
+ everyProperty: function(key, value) {
326
+ var len = (this.get) ? this.get('length') : this.length ;
327
+ var ret = YES;
328
+ var last = null ;
329
+ var context = SC.Enumerator._popContext();
330
+ for(var idx=0;ret && (idx<len);idx++) {
331
+ var next = this.nextObject(idx, last, context) ;
332
+ var cur = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
333
+ ret = (value === undefined) ? !!cur : SC.isEqual(cur, value);
334
+ last = next ;
335
+ }
336
+ last = null ;
337
+ context = SC.Enumerator._pushContext(context);
338
+ return ret ;
339
+ },
340
+
341
+
342
+ /**
343
+ Returns YES if the passed function returns true for any item in the
344
+ enumeration. This corresponds with the every() method in JavaScript 1.6.
345
+
346
+ The callback method you provide should have the following signature (all
347
+ parameters are optional):
348
+
349
+ {{{
350
+ function(item, index, enumerable) ;
351
+ }}}
352
+
353
+ - *item* is the current item in the iteration.
354
+ - *index* is the current index in the iteration
355
+ - *enumerable* is the enumerable object itself.
356
+
357
+ It should return the YES to include the item in the results, NO otherwise.
358
+
359
+ Note that in addition to a callback, you can also pass an optional target
360
+ object that will be set as "this" on the context. This is a good way
361
+ to give your iterator function access to the current object.
362
+
363
+ h4. Usage Example
364
+
365
+ {{{
366
+ if (people.some(isManager)) { Paychecks.addBiggerBonus(); }
367
+ }}}
368
+
369
+ @params callback {Function} the callback to execute
370
+ @params target {Object} the target object to use
371
+ @returns {Array} A filtered array.
372
+ */
373
+ some: function(callback, target) {
374
+ if (typeof callback !== "function") throw new TypeError() ;
375
+ var len = (this.get) ? this.get('length') : this.length ;
376
+ if (target === undefined) target = null;
377
+
378
+ var ret = NO;
379
+ var last = null ;
380
+ var context = SC.Enumerator._popContext();
381
+ for(var idx=0;(!ret) && (idx<len);idx++) {
382
+ var next = this.nextObject(idx, last, context) ;
383
+ if(callback.call(target, next, idx, this)) ret = YES ;
384
+ last = next ;
385
+ }
386
+ last = null ;
387
+ context = SC.Enumerator._pushContext(context);
388
+ return ret ;
389
+ },
390
+
391
+ /**
392
+ Returns YES if the passed property resolves to true for any item in the
393
+ enumerable. This method is often simpler/faster than using a callback.
394
+
395
+ @params key {String} the property to test
396
+ @param value {String} optional value to test against.
397
+ @returns {Boolean} YES
398
+ */
399
+ someProperty: function(key, value) {
400
+ var len = (this.get) ? this.get('length') : this.length ;
401
+ var ret = NO;
402
+ var last = null ;
403
+ var context = SC.Enumerator._popContext();
404
+ for(var idx=0; !ret && (idx<len); idx++) {
405
+ var next = this.nextObject(idx, last, context) ;
406
+ var cur = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
407
+ ret = (value === undefined) ? !!cur : SC.isEqual(cur, value);
408
+ last = next ;
409
+ }
410
+ last = null ;
411
+ context = SC.Enumerator._pushContext(context);
412
+ return ret ; // return the invert
413
+ },
414
+
415
+ /**
416
+ This will combine the values of the enumerator into a single value. It
417
+ is a useful way to collect a summary value from an enumeration. This
418
+ corresponds to the reduce() method defined in JavaScript 1.8.
419
+
420
+ The callback method you provide should have the following signature (all
421
+ parameters are optional):
422
+
423
+ {{{
424
+ function(previousValue, item, index, enumerable) ;
425
+ }}}
426
+
427
+ - *previousValue* is the value returned by the last call to the iterator.
428
+ - *item* is the current item in the iteration.
429
+ - *index* is the current index in the iteration
430
+ - *enumerable* is the enumerable object itself.
431
+
432
+ Return the new cumulative value.
433
+
434
+ In addition to the callback you can also pass an initialValue. An error
435
+ will be raised if you do not pass an initial value and the enumerator is
436
+ empty.
437
+
438
+ Note that unlike the other methods, this method does not allow you to
439
+ pass a target object to set as this for the callback. It's part of the
440
+ spec. Sorry.
441
+
442
+ @params callback {Function} the callback to execute
443
+ @params initialValue {Object} initial value for the reduce
444
+ @params reducerProperty {String} internal use only. May not be available.
445
+ @returns {Array} A filtered array.
446
+ */
447
+ reduce: function(callback, initialValue, reducerProperty) {
448
+ if (typeof callback !== "function") throw new TypeError() ;
449
+ var len = (this.get) ? this.get('length') : this.length ;
450
+
451
+ // no value to return if no initial value & empty
452
+ if (len===0 && initialValue === undefined) throw new TypeError();
453
+
454
+ var ret = initialValue;
455
+ var last = null ;
456
+ var context = SC.Enumerator._popContext();
457
+ for(var idx=0;idx<len;idx++) {
458
+ var next = this.nextObject(idx, last, context) ;
459
+
460
+ // while ret is still undefined, just set the first value we get as ret.
461
+ // this is not the ideal behavior actually but it matches the FireFox
462
+ // implementation... :(
463
+ if (next != null) {
464
+ if (ret === undefined) {
465
+ ret = next ;
466
+ } else {
467
+ ret = callback.call(null, ret, next, idx, this, reducerProperty);
468
+ }
469
+ }
470
+ last = next ;
471
+ }
472
+ last = null ;
473
+ context = SC.Enumerator._pushContext(context);
474
+
475
+ // uh oh...we never found a value!
476
+ if (ret === undefined) throw new TypeError() ;
477
+ return ret ;
478
+ },
479
+
480
+ /**
481
+ Invokes the named method on every object in the receiver that
482
+ implements it. This method corresponds to the implementation in
483
+ Prototype 1.6.
484
+
485
+ @param methodName {String} the name of the method
486
+ @param args {Object...} optional arguments to pass as well.
487
+ @returns {Array} return values from calling invoke.
488
+ */
489
+ invoke: function(methodName) {
490
+ var len = (this.get) ? this.get('length') : this.length ;
491
+ if (len <= 0) return [] ; // nothing to invoke....
492
+
493
+ // collect the arguments
494
+ var args = null ;
495
+ var alen = arguments.length ;
496
+ if (alen > 1) {
497
+ args = [] ;
498
+ for(var idx=1;idx<alen;idx++) args.push(arguments[idx]) ;
499
+ }
500
+
501
+ // call invoke
502
+ var ret = [] ;
503
+ var last = null ;
504
+ var context = SC.Enumerator._popContext();
505
+ for(var idx=0;idx<len;idx++) {
506
+ var next = this.nextObject(idx, last, context) ;
507
+ var method = (next) ? next[methodName] : null ;
508
+ if (method) ret[idx] = method.apply(next, args) ;
509
+ last = next ;
510
+ }
511
+ last = null ;
512
+ context = SC.Enumerator._pushContext(context);
513
+ return ret ;
514
+ },
515
+
516
+ /**
517
+ Invokes the passed method and optional arguments on the receiver elements
518
+ as long as the methods return value matches the target value. This is
519
+ a useful way to attempt to apply changes to a collection of objects unless
520
+ or until one fails.
521
+
522
+ @param targetValue {Object} the target return value
523
+ @param methodName {String} the name of the method
524
+ @param args {Object...} optional arguments to pass as well.
525
+ @returns {Array} return values from calling invoke.
526
+ */
527
+ invokeWhile: function(targetValue, methodName) {
528
+ var len = (this.get) ? this.get('length') : this.length ;
529
+ if (len <= 0) return null; // nothing to invoke....
530
+
531
+ // collect the arguments
532
+ var args = null ;
533
+ var alen = arguments.length ;
534
+ if (alen > 2) {
535
+ args = [] ;
536
+ for(var idx=2;idx<alen;idx++) args.push(arguments[idx]) ;
537
+ }
538
+
539
+ // call invoke
540
+ var ret = targetValue ;
541
+ var last = null ;
542
+ var context = SC.Enumerator._popContext();
543
+ for(var idx=0;(ret === targetValue) && (idx<len);idx++) {
544
+ var next = this.nextObject(idx, last, context) ;
545
+ var method = (next) ? next[methodName] : null ;
546
+ if (method) ret = method.apply(next, args) ;
547
+ last = next ;
548
+ }
549
+ last = null ;
550
+ context = SC.Enumerator._pushContext(context);
551
+ return ret ;
552
+ },
553
+
554
+ /**
555
+ Simply converts the enumerable into a genuine array. The order, of
556
+ course, is not gauranteed. Corresponds to the method implemented by
557
+ Prototype.
558
+
559
+ You can also call Array.from().
560
+
561
+ @returns {Array} the enumerable as an array.
562
+ */
563
+ toArray: function() {
564
+ var len = (this.get) ? this.get('length') : this.length ;
565
+ if (len <= 0) return [] ; // nothing to invoke....
566
+
567
+ // call invoke
568
+ var ret = [] ;
569
+ var last = null ;
570
+ var context = SC.Enumerator._popContext();
571
+ for(var idx=0;idx<len;idx++) {
572
+ var next = this.nextObject(idx, last, context) ;
573
+ ret.push(next) ;
574
+ last = next ;
575
+ }
576
+ last = null ;
577
+ context = SC.Enumerator._pushContext(context);
578
+ return ret ;
579
+ }
580
+ } ;
581
+
582
+ // Build in a separate function to avoid unintential leaks through closures...
583
+ SC._buildReducerFor = function(reducerKey, reducerProperty) {
584
+ return function(key, value) {
585
+ var reducer = this[reducerKey] ;
586
+ if (SC.typeOf(reducer) !== T_FUNCTION) {
587
+ return (this.unknownProperty) ? this.unknownProperty(key, value) : null;
588
+ } else {
589
+ // Invoke the reduce method defined in enumerable instead of using the
590
+ // one implemented in the receiver. The receiver might be a native
591
+ // implementation that does not support reducerProperty.
592
+ return SC.Enumerable.reduce.call(this, reducer, null, reducerProperty) ;
593
+ }
594
+ }.property('[]') ;
595
+ };
596
+
597
+ SC.Reducers = {
598
+ /**
599
+ This property will trigger anytime the enumerable's content changes.
600
+ You can observe this property to be notified of changes to the enumerables
601
+ content.
602
+
603
+ For plain enumerables, this property is read only. SC.Array overrides
604
+ this method.
605
+ */
606
+ '[]': function(key, value) { return this ; }.property(),
607
+
608
+ /**
609
+ Invoke this method when the contents of your enumerable has changed.
610
+ This will notify any observers watching for content changes.
611
+ */
612
+ enumerableContentDidChange: function() {
613
+ this.notifyPropertyChange('[]') ;
614
+ if (this.ownerRecord && this.ownerRecord.recordDidChange) {
615
+ this.ownerRecord.recordDidChange(this) ;
616
+ }
617
+ },
618
+
619
+ /**
620
+ Call this method from your unknownProperty() handler to implement
621
+ automatic reduced properties. A reduced property is a property that
622
+ collects its contents dynamically from your array contents. Reduced
623
+ properties always begin with "@". Getting this property will call
624
+ reduce() on your array with the function matching the key name as the
625
+ processor.
626
+
627
+ The return value of this will be either the return value from the
628
+ reduced property or undefined, which means this key is not a reduced
629
+ property. You can call this at the top of your unknownProperty handler
630
+ like so:
631
+
632
+ {{{
633
+ unknownProperty: function(key, value) {
634
+ var ret = this.handleReduceProperty(key, value) ;
635
+ if (ret === undefined) {
636
+ // process like normal
637
+ }
638
+ }
639
+ }}}
640
+
641
+ @param key {String} the reduce property key
642
+ @param value {Object} a value or undefined.
643
+ @param generateProperty {Boolean} only set to false if you do not want an
644
+ optimized computed property handler generated for this. Not common.
645
+ @returns {Object} the reduced property or undefined
646
+ */
647
+ reducedProperty: function(key, value, generateProperty) {
648
+
649
+ if (key[0] !== '@') return undefined ; // not a reduced property
650
+
651
+ // get the reducer key and the reducer
652
+ var matches = key.match(/^@([^(]*)(\(([^)]*)\))?$/) ;
653
+ if (!matches || matches.length < 2) return undefined ; // no match
654
+
655
+ var reducerKey = matches[1]; // = 'max' if key = '@max(balance)'
656
+ var reducerProperty = matches[3] ; // = 'balance' if key = '@max(balance)'
657
+ var reducerKey = "reduce%@".fmt(reducerKey.capitalize()) ;
658
+ var reducer = this[reducerKey] ;
659
+
660
+ // if there is no reduce function defined for this key, then we can't
661
+ // build a reducer for it.
662
+ if (SC.typeOf(reducer) !== T_FUNCTION) return undefined;
663
+
664
+ // if we can't generate the property, just run reduce
665
+ if (generateProperty === NO) {
666
+ return SC.Enumerable.reduce.call(this, reducer, null, reducerProperty) ;
667
+ }
668
+
669
+ // ok, found the reducer. Let's build the computed property and install
670
+ var func = SC._buildReducerFor(reducerKey, reducerProperty);
671
+ var p = (this._type) ? this._type : this.constructor.prototype ;
672
+
673
+ if (p) {
674
+ p[key] = func ;
675
+ this.registerDependentKey(key, '[]') ;
676
+ }
677
+
678
+ // and reduce anyway...
679
+ return SC.Enumerable.reduce.call(this, reducer, null, reducerProperty) ;
680
+ },
681
+
682
+ /**
683
+ Reducer for @max reduced property.
684
+ */
685
+ reduceMax: function(previousValue, item, index, e, reducerProperty) {
686
+ if (reducerProperty && item) {
687
+ item = (item.get) ? item.get(reducerProperty) : item[reducerProperty];
688
+ }
689
+ if (previousValue == null) return item ;
690
+ return (item > previousValue) ? item : previousValue ;
691
+ },
692
+
693
+ /**
694
+ Reducer for @maxObject reduced property.
695
+ */
696
+ reduceMaxObject: function(previousItem, item, index, e, reducerProperty) {
697
+
698
+ // get the value for both the previous and current item. If no
699
+ // reducerProperty was supplied, use the items themselves.
700
+ var previousValue = previousItem, itemValue = item ;
701
+ if (reducerProperty) {
702
+ if (item) {
703
+ itemValue = (item.get) ? item.get(reducerProperty) : item[reducerProperty] ;
704
+ }
705
+
706
+ if (previousItem) {
707
+ previousItemValue = (previousItem.get) ? previousItem.get(reducerProperty) : previousItem[reducerProperty] ;
708
+ }
709
+ }
710
+ if (previousValue == null) return item ;
711
+ return (itemValue > previousValue) ? item : previousItem ;
712
+ },
713
+
714
+ /**
715
+ Reducer for @min reduced property.
716
+ */
717
+ reduceMin: function(previousValue, item, index, e, reducerProperty) {
718
+ if (reducerProperty && item) {
719
+ item = (item.get) ? item.get(reducerProperty) : item[reducerProperty];
720
+ }
721
+ if (previousValue == null) return item ;
722
+ return (item < previousValue) ? item : previousValue ;
723
+ },
724
+
725
+ /**
726
+ Reducer for @maxObject reduced property.
727
+ */
728
+ reduceMinObject: function(previousItem, item, index, e, reducerProperty) {
729
+
730
+ // get the value for both the previous and current item. If no
731
+ // reducerProperty was supplied, use the items themselves.
732
+ var previousValue = previousItem, itemValue = item ;
733
+ if (reducerProperty) {
734
+ if (item) {
735
+ itemValue = (item.get) ? item.get(reducerProperty) : item[reducerProperty] ;
736
+ }
737
+
738
+ if (previousItem) {
739
+ previousItemValue = (previousItem.get) ? previousItem.get(reducerProperty) : previousItem[reducerProperty] ;
740
+ }
741
+ }
742
+ if (previousValue == null) return item ;
743
+ return (itemValue < previousValue) ? item : previousItem ;
744
+ },
745
+
746
+ /**
747
+ Reducer for @average reduced property.
748
+ */
749
+ reduceAverage: function(previousValue, item, index, e, reducerProperty) {
750
+ if (reducerProperty && item) {
751
+ item = (item.get) ? item.get(reducerProperty) : item[reducerProperty];
752
+ }
753
+ var ret = (previousValue || 0) + item ;
754
+ var len = (e.get) ? e.get('length') : e.length;
755
+ if (index >= len-1) ret = ret / len; //avg after last item.
756
+ return ret ;
757
+ },
758
+
759
+ /**
760
+ Reducer for @sum reduced property.
761
+ */
762
+ reduceSum: function(previousValue, item, index, e, reducerProperty) {
763
+ if (reducerProperty && item) {
764
+ item = (item.get) ? item.get(reducerProperty) : item[reducerProperty];
765
+ }
766
+ return (previousValue == null) ? item : previousValue + item ;
767
+ }
768
+ } ;
769
+
770
+ // Apply reducers...
771
+ SC.mixin(SC.Enumerable, SC.Reducers) ;
772
+ SC.mixin(Array.prototype, SC.Reducers) ;
773
+
774
+ // ......................................................
775
+ // ARRAY SUPPORT
776
+ //
777
+
778
+ // Implement the same enhancements on Array. We use specialized methods
779
+ // because working with arrays are so common.
780
+ (function() {
781
+
782
+ // These methods will be applied even if they already exist b/c we do it
783
+ // better.
784
+ var alwaysMixin = {
785
+
786
+ // this is supported so you can get an enumerator. The rest of the
787
+ // methods do not use this just to squeeze every last ounce of perf as
788
+ // possible.
789
+ nextObject: SC.Enumerable.nextObject,
790
+ enumerator: SC.Enumerable.enumerator,
791
+
792
+ // see above...
793
+ mapProperty: function(key) {
794
+ var len = this.length ;
795
+ var ret = [];
796
+ for(var idx=0;idx<len;idx++) {
797
+ var next = this[idx] ;
798
+ ret[idx] = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
799
+ }
800
+ return ret ;
801
+ },
802
+
803
+ filterProperty: function(key, value) {
804
+ var len = this.length ;
805
+ var ret = [];
806
+ for(var idx=0;idx<len;idx++) {
807
+ var next = this[idx] ;
808
+ var cur = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
809
+ var matched = (value === undefined) ? !!cur : SC.isEqual(cur, value);
810
+ if (matched) ret.push(next) ;
811
+ }
812
+ return ret ;
813
+ },
814
+
815
+ everyProperty: function(key, value) {
816
+ var len = this.length ;
817
+ var ret = YES;
818
+ for(var idx=0;ret && (idx<len);idx++) {
819
+ var next = this[idx] ;
820
+ var cur = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
821
+ ret = (value === undefined) ? !!cur : SC.isEqual(cur, value);
822
+ }
823
+ return ret ;
824
+ },
825
+
826
+ someProperty: function(key, value) {
827
+ var len = this.length ;
828
+ var ret = NO;
829
+ for(var idx=0; !ret && (idx<len); idx++) {
830
+ var next = this[idx] ;
831
+ var cur = (next) ? ((next.get) ? next.get(key) : next[key]) : null;
832
+ ret = (value === undefined) ? !!cur : SC.isEqual(cur, value);
833
+ }
834
+ return ret ; // return the invert
835
+ },
836
+
837
+ invoke: function(methodName) {
838
+ var len = this.length ;
839
+ if (len <= 0) return [] ; // nothing to invoke....
840
+
841
+ // collect the arguments
842
+ var args = null ;
843
+ var alen = arguments.length ;
844
+ if (alen > 1) {
845
+ args = [] ;
846
+ for(var idx=1;idx<alen;idx++) args.push(arguments[idx]) ;
847
+ }
848
+
849
+ // call invoke
850
+ var ret = [] ;
851
+ for(var idx=0;idx<len;idx++) {
852
+ var next = this[idx] ;
853
+ var method = (next) ? next[methodName] : null ;
854
+ if (method) ret[idx] = method.apply(next, args) ;
855
+ }
856
+ return ret ;
857
+ },
858
+
859
+ invokeWhile: function(targetValue, methodName) {
860
+ var len = this.length ;
861
+ if (len <= 0) return null ; // nothing to invoke....
862
+
863
+ // collect the arguments
864
+ var args = null ;
865
+ var alen = arguments.length ;
866
+ if (alen > 2) {
867
+ args = [] ;
868
+ for(var idx=2;idx<alen;idx++) args.push(arguments[idx]) ;
869
+ }
870
+
871
+ // call invoke
872
+ var ret = targetValue ;
873
+ for(var idx=0;(ret === targetValue) && (idx<len);idx++) {
874
+ var next = this[idx] ;
875
+ var method = (next) ? next[methodName] : null ;
876
+ if (method) ret = method.apply(next, args) ;
877
+ }
878
+ return ret ;
879
+ },
880
+
881
+ toArray: function() {
882
+ var len = this.length ;
883
+ if (len <= 0) return [] ; // nothing to invoke....
884
+
885
+ // call invoke
886
+ var ret = [] ;
887
+ for(var idx=0;idx<len;idx++) {
888
+ var next = this[idx] ;
889
+ ret.push(next) ;
890
+ }
891
+ return ret ;
892
+ }
893
+ };
894
+
895
+ // These methods will only be applied if they are not already defined b/c
896
+ // the browser is probably getting it.
897
+ var mixinIfMissing = {
898
+
899
+ forEach: function(callback, target) {
900
+ if (typeof callback !== "function") throw new TypeError() ;
901
+ var len = this.length ;
902
+ if (target === undefined) target = null;
903
+
904
+ for(var idx=0;idx<len;idx++) {
905
+ var next = this[idx] ;
906
+ callback.call(target, next, idx, this);
907
+ }
908
+ return this ;
909
+ },
910
+
911
+ map: function(callback, target) {
912
+ if (typeof callback !== "function") throw new TypeError() ;
913
+ var len = this.length ;
914
+ if (target === undefined) target = null;
915
+
916
+ var ret = [];
917
+ for(var idx=0;idx<len;idx++) {
918
+ var next = this[idx] ;
919
+ ret[idx] = callback.call(target, next, idx, this) ;
920
+ }
921
+ return ret ;
922
+ },
923
+
924
+ filter: function(callback, target) {
925
+ if (typeof callback !== "function") throw new TypeError() ;
926
+ var len = this.length ;
927
+ if (target === undefined) target = null;
928
+
929
+ var ret = [];
930
+ for(var idx=0;idx<len;idx++) {
931
+ var next = this[idx] ;
932
+ if(callback.call(target, next, idx, this)) ret.push(next) ;
933
+ }
934
+ return ret ;
935
+ },
936
+
937
+ every: function(callback, target) {
938
+ if (typeof callback !== "function") throw new TypeError() ;
939
+ var len = this.length ;
940
+ if (target === undefined) target = null;
941
+
942
+ var ret = YES;
943
+ for(var idx=0;ret && (idx<len);idx++) {
944
+ var next = this[idx] ;
945
+ if(!callback.call(target, next, idx, this)) ret = NO ;
946
+ }
947
+ return ret ;
948
+ },
949
+
950
+ some: function(callback, target) {
951
+ if (typeof callback !== "function") throw new TypeError() ;
952
+ var len = this.length ;
953
+ if (target === undefined) target = null;
954
+
955
+ var ret = NO;
956
+ for(var idx=0;(!ret) && (idx<len);idx++) {
957
+ var next = this[idx] ;
958
+ if(callback.call(target, next, idx, this)) ret = YES ;
959
+ }
960
+ return ret ;
961
+ },
962
+
963
+ reduce: function(callback, initialValue, reducerProperty) {
964
+ if (typeof callback !== "function") throw new TypeError() ;
965
+ var len = this.length ;
966
+
967
+ // no value to return if no initial value & empty
968
+ if (len===0 && initialValue === undefined) throw new TypeError();
969
+
970
+ var ret = initialValue;
971
+ for(var idx=0;idx<len;idx++) {
972
+ var next = this[idx] ;
973
+
974
+ // while ret is still undefined, just set the first value we get as
975
+ // ret. this is not the ideal behavior actually but it matches the
976
+ // FireFox implementation... :(
977
+ if (next != null) {
978
+ if (ret === undefined) {
979
+ ret = next ;
980
+ } else {
981
+ ret = callback.call(null, ret, next, idx, this, reducerProperty);
982
+ }
983
+ }
984
+ }
985
+
986
+ // uh oh...we never found a value!
987
+ if (ret === undefined) throw new TypeError() ;
988
+ return ret ;
989
+ }
990
+ };
991
+
992
+ // Apply methods if missing...
993
+ // Also override prototype for now. Our methods are functionally identical
994
+ // and don't break the browsers.
995
+ for(var key in mixinIfMissing) {
996
+ if (!mixinIfMissing.hasOwnProperty(key)) continue ;
997
+ if (!Array.prototype[key] || (Prototype && Prototype.Version.match(/^1\.6/))) {
998
+ Array.prototype[key] = mixinIfMissing[key] ;
999
+ }
1000
+ }
1001
+
1002
+ // Apply other methods...
1003
+ SC.mixin(Array.prototype, alwaysMixin) ;
1004
+
1005
+ })() ;
1006
+