| 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381 |
1
1
1
1
1
1
1
2263
2263
16150
16150
16150
16150
16150
16150
16150
16150
2263
1
10717
10717
10717
1
155
262
1
3
124
124
124
140
140
124
1
3
207
207
2263
207
207
1
1
27
27
15
27
1
154
3142
1
26
24
1
34
106
1
21
10
1
1
1
1
41
59
1
12
8
1
600
600
994
994
512
88
1
1655
1655
4082
4082
1495
160
1
8
8
23
23
8
1
10
6
1
21
9
1
11
20
1
26
24
1
28
433
1
26
24
1
10
6
1
21
9
1
4
3
3
3
1
26
542
542
3
539
1
12
5
5
1
4
4
1
12
24
24
5
19
1
12
5
5
1
4
4
1
6
10
10
3
7
1
32
151
151
36
115
1
13
5
5
1
4
4
1
19
319
319
131
188
1
16
78
78
4
74
74
1
244
2058
1
27
2703
2703
1
11
19
19
19
1
7
8
8
1
6
9
9
9
1
23
8
23
18
18
18
18
6
12
1
11
16
16
1
4
6
6
3
3
3
10
3
1
41
41
1
40
1
77
1
76
1
22
1
15
29
14
1
1
35
53
53
1
11
15
15
5
10
5
5
1
59
94
94
94
6
88
1
46
46
46
37
37
13
24
| 'use strict';
;require.register("utils/ember_computed", function (exports, require, module) {
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var computed = Em.computed;
var get = Em.get;
var makeArray = Em.makeArray;
var slice = [].slice;
var dataUtils = require('utils/data_manipulation');
/**
* Returns hash with values of name properties from the context
* If <code>propertyName</code> starts with 'App.', <code>App</code> is used as context, <code>self</code> used otherwise
* If some <code>propertyName</code> starts with '!' its value will be inverted
*
* @param {object} self current context
* @param {string[]} propertyNames needed properties
* @returns {object} hash with needed values
*/
function getProperties(self, propertyNames) {
var ret = {};
for (var i = 0; i < propertyNames.length; i++) {
var propertyName = propertyNames[i];
var shouldBeInverted = propertyName.startsWith('!');
propertyName = shouldBeInverted ? propertyName.substr(1) : propertyName;
var isApp = propertyName.startsWith('App.');
var name = isApp ? propertyName.replace('App.', '') : propertyName;
var value = isApp ? App.get(name) : self.get(name);
value = shouldBeInverted ? !value : value;
ret[propertyName] = value;
}
return ret;
}
/**
* Returns value of named property from the context
* If <code>propertyName</code> starts with 'App.', <code>App</code> is used as context, <code>self</code> used otherwise
*
* @param {object} self current context
* @param {string} propertyName needed property
* @returns {*} needed value
*/
function smartGet(self, propertyName) {
var isApp = propertyName.startsWith('App.');
var name = isApp ? propertyName.replace('App.', '') : propertyName;
return isApp ? App.get(name) : self.get(name);
}
/**
* Returns list with values of name properties from the context
* If <code>propertyName</code> starts with 'App.', <code>App</code> is used as context, <code>self</code> used otherwise
*
* @param {object} self current context
* @param {string[]} propertyNames needed properties
* @returns {array} list of needed values
*/
function getValues(self, propertyNames) {
return propertyNames.map(function (propertyName) {
return smartGet(self, propertyName);
});
}
function generateComputedWithKey(macro) {
return function () {
var properties = slice.call(arguments, 1);
var key = arguments[0];
var computedFunc = computed(function () {
var values = getValues(this, properties);
return macro.call(this, key, values);
});
return computedFunc.property.apply(computedFunc, properties);
};
}
function generateComputedWithProperties(macro) {
return function () {
var properties = slice.call(arguments);
var computedFunc = computed(function () {
return macro.apply(this, [getProperties(this, properties)]);
});
var realProperties = properties.slice().invoke('replace', '!', '');
return computedFunc.property.apply(computedFunc, realProperties);
};
}
function generateComputedWithValues(macro) {
return function () {
var properties = slice.call(arguments);
var computedFunc = computed(function () {
return macro.apply(this, [getValues(this, properties)]);
});
return computedFunc.property.apply(computedFunc, properties);
};
}
/**
*
* A computed property that returns true if the provided dependent property
* is equal to the given value.
* App.*-keys are supported
* Example*
* ```javascript
* var Hamster = Ember.Object.extend({
* napTime: Ember.computed.equal('state', 'sleepy')
* });
* var hamster = Hamster.create();
* hamster.get('napTime'); // false
* hamster.set('state', 'sleepy');
* hamster.get('napTime'); // true
* hamster.set('state', 'hungry');
* hamster.get('napTime'); // false
* ```
* @method equal
* @param {String} dependentKey
* @param {String|Number|Object} value
* @return {Ember.ComputedProperty} computed property which returns true if
* the original value for property is equal to the given value.
* @public
*/
computed.equal = function (dependentKey, value) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) === value;
}).cacheable();
};
/**
* A computed property that returns true if the provided dependent property is not equal to the given value
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 'a',
* p2: Em.computed.notEqual('p1', 'a')
* });
* console.log(o.get('p2')); // false
* o.set('p1', 'b');
* console.log(o.get('p2')); // true
* </pre>
*
* @method notEqual
* @param {string} dependentKey
* @param {*} value
* @returns {Ember.ComputedProperty}
*/
computed.notEqual = function (dependentKey, value) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) !== value;
});
};
/**
* A computed property that returns true if provided dependent properties are equal to the each other
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 'a',
* p2: 'b',
* p3: Em.computed.equalProperties('p1', 'p2')
* });
* console.log(o.get('p3')); // false
* o.set('p1', 'b');
* console.log(o.get('p3')); // true
* </pre>
*
* @method equalProperties
* @param {string} dependentKey1
* @param {string} dependentKey2
* @returns {Ember.ComputedProperty}
*/
computed.equalProperties = function (dependentKey1, dependentKey2) {
return computed(dependentKey1, dependentKey2, function () {
return smartGet(this, dependentKey1) === smartGet(this, dependentKey2);
});
};
/**
* A computed property that returns true if provided dependent properties are not equal to the each other
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 'a',
* p2: 'b',
* p3: Em.computed.notEqualProperties('p1', 'p2')
* });
* console.log(o.get('p3')); // true
* o.set('p1', 'b');
* console.log(o.get('p3')); // false
* </pre>
*
* @method notEqualProperties
* @param {string} dependentKey1
* @param {string} dependentKey2
* @returns {Ember.ComputedProperty}
*/
computed.notEqualProperties = function (dependentKey1, dependentKey2) {
return computed(dependentKey1, dependentKey2, function () {
return smartGet(this, dependentKey1) !== smartGet(this, dependentKey2);
});
};
/**
* A computed property that returns grouped collection's items by propertyName-value
*
* @method groupBy
* @param {string} collectionKey
* @param {string} propertyName
* @returns {Ember.ComputedProperty}
*/
computed.groupBy = function (collectionKey, propertyName) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = get(this, collectionKey);
return dataUtils.groupPropertyValues(collection, propertyName);
});
};
/**
* A computed property that returns filtered collection by propertyName values-list
* Wrapper to filterProperty-method that allows using list of values to filter
*
* @method filterByMany
* @param {string} collectionKey
* @param {string} propertyName
* @param {array} valuesToFilter
* @returns {Ember.ComputedProperty}
*/
computed.filterByMany = function (collectionKey, propertyName, valuesToFilter) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = get(this, collectionKey);
return dataUtils.filterPropertyValues(collection, propertyName, makeArray(valuesToFilter));
});
};
/**
* A computed property that returns collection without elements with value that is in <code>valuesToReject</code>
* Exclude objects from <code>collection</code> if its <code>key</code> exist in <code>valuesToReject</code>
*
* @method rejectMany
* @param {string} collectionKey
* @param {string} propertyName
* @param {array} valuesToReject
* @returns {Ember.ComputedProperty}
*/
computed.rejectMany = function (collectionKey, propertyName, valuesToReject) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = get(this, collectionKey);
return dataUtils.rejectPropertyValues(collection, propertyName, makeArray(valuesToReject));
});
};
/**
* A computed property that returns trueValue if dependent value is true and falseValue otherwise
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: true,
* p2: Em.computed.ifThenElse('p1', 'abc', 'cba')
* });
* console.log(o.get('p2')); // 'abc'
* o.set('p1', false);
* console.log(o.get('p2')); // 'cba'
* </pre>
*
* @method ifThenElse
* @param {string} dependentKey
* @param {*} trueValue
* @param {*} falseValue
* @returns {Ember.ComputedProperty}
*/
computed.ifThenElse = function (dependentKey, trueValue, falseValue) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) ? trueValue : falseValue;
});
};
/**
* A computed property that returns value for trueKey property if dependent value is true and falseKey-value otherwise
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: true,
* p2: 1,
* p3: 2,
* p4: Em.computed.ifThenElseByKeys('p1', 'p2', 'p3')
* });
* console.log(o.get('p4')); // 1
* o.set('p1', false);
* console.log(o.get('p4')); // 2
*
* o.set('p3', 3);
* console.log(o.get('p4')); // 3
* </pre>
*
* @method ifThenElseByKeys
* @param {string} dependentKey
* @param {string} trueKey
* @param {string} falseKey
* @returns {Ember.ComputedProperty}
*/
computed.ifThenElseByKeys = function (dependentKey, trueKey, falseKey) {
return computed(dependentKey, trueKey, falseKey, function () {
return smartGet(this, dependentKey) ? smartGet(this, trueKey) : smartGet(this, falseKey);
});
};
/**
* A computed property that is equal to the logical 'and'
* Takes any number of arguments
* Returns true if all of them are truly, false - otherwise
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: true,
* p2: true,
* p3: true,
* p4: Em.computed.and('p1', 'p2', 'p3')
* });
* console.log(o.get('p4')); // true
* o.set('p1', false);
* console.log(o.get('p4')); // false
* </pre>
*
* @method and
* @param {...string} dependentKeys
* @returns {Ember.ComputedProperty}
*/
computed.and = generateComputedWithProperties(function (properties) {
var value;
for (var key in properties) {
value = !!properties[key];
if (properties.hasOwnProperty(key) && !value) {
return false;
}
}
return value;
});
/**
* A computed property that is equal to the logical 'or'
* Takes any number of arguments
* Returns true if at least one of them is truly, false - otherwise
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: false,
* p2: false,
* p3: false,
* p4: Em.computed.or('p1', 'p2', 'p3')
* });
* console.log(o.get('p4')); // false
* o.set('p1', true);
* console.log(o.get('p4')); // true
* </pre>
*
* @method or
* @param {...string} dependentKeys
* @returns {Ember.ComputedProperty}
*/
computed.or = generateComputedWithProperties(function (properties) {
var value;
for (var key in properties) {
value = !!properties[key];
if (properties.hasOwnProperty(key) && value) {
return value;
}
}
return value;
});
/**
* A computed property that returns sum on the dependent properties values
* Takes any number of arguments
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 1,
* p2: 2,
* p3: 3,
* p4: Em.computed.sumProperties('p1', 'p2', 'p3')
* });
* console.log(o.get('p4')); // 6
* o.set('p1', 2);
* console.log(o.get('p4')); // 7
* </pre>
*
* @method sumProperties
* @param {...string} dependentKeys
* @returns {Ember.ComputedProperty}
*/
computed.sumProperties = generateComputedWithProperties(function (properties) {
var sum = 0;
for (var key in properties) {
Eif (properties.hasOwnProperty(key)) {
sum += Number(properties[key]);
}
}
return sum;
});
/**
* A computed property that returns true if dependent value is greater or equal to the needed value
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: Em.computed.gte('p1', 1)
* });
* console.log(o.get('p2')); // true
* o.set('p1', 4);
* console.log(o.get('p2')); // true
* o.set('p1', 5);
* console.log(o.get('p2')); // false
* </pre>
*
* @method gte
* @param {string} dependentKey
* @param {*} value
* @returns {Ember.ComputedProperty}
*/
computed.gte = function (dependentKey, value) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) >= value;
});
};
/**
* A computed property that returns true if first dependent property is greater or equal to the second dependent property
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: 1,
* p3: Em.computed.gteProperties('p1', 'p2')
* });
* console.log(o.get('p3')); // true
* o.set('p2', 4);
* console.log(o.get('p3')); // true
* o.set('p2', 5);
* console.log(o.get('p3')); // false
* </pre>
*
* @method gteProperties
* @param {string} dependentKey1
* @param {string} dependentKey2
* @returns {Ember.ComputedProperty}
*/
computed.gteProperties = function (dependentKey1, dependentKey2) {
return computed(dependentKey1, dependentKey2, function () {
return smartGet(this, dependentKey1) >= smartGet(this, dependentKey2);
});
};
/**
* A computed property that returns true if dependent property is less or equal to the needed value
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: Em.computed.lte('p1', 1)
* });
* console.log(o.get('p2')); // false
* o.set('p1', 4);
* console.log(o.get('p2')); // true
* o.set('p1', 5);
* console.log(o.get('p2')); // true
* </pre>
*
* @method lte
* @param {string} dependentKey
* @param {*} value
* @returns {Ember.ComputedProperty}
*/
computed.lte = function (dependentKey, value) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) <= value;
});
};
/**
* A computed property that returns true if first dependent property is less or equal to the second dependent property
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: 1,
* p3: Em.computed.lteProperties('p1', 'p2')
* });
* console.log(o.get('p3')); // false
* o.set('p2', 4);
* console.log(o.get('p3')); // true
* o.set('p2', 5);
* console.log(o.get('p3')); // true
* </pre>
*
* @method lteProperties
* @param {string} dependentKey1
* @param {string} dependentKey2
* @returns {Ember.ComputedProperty}
*/
computed.lteProperties = function (dependentKey1, dependentKey2) {
return computed(dependentKey1, dependentKey2, function () {
return smartGet(this, dependentKey1) <= smartGet(this, dependentKey2);
});
};
/**
* A computed property that returns true if dependent value is greater than the needed value
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: Em.computed.gt('p1', 1)
* });
* console.log(o.get('p2')); // true
* o.set('p1', 4);
* console.log(o.get('p2')); // false
* o.set('p1', 5);
* console.log(o.get('p2')); // false
* </pre>
*
* @method gt
* @param {string} dependentKey
* @param {*} value
* @returns {Ember.ComputedProperty}
*/
computed.gt = function (dependentKey, value) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) > value;
});
};
/**
* A computed property that returns true if first dependent property is greater than the second dependent property
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: 1,
* p3: Em.computed.gteProperties('p1', 'p2')
* });
* console.log(o.get('p3')); // true
* o.set('p2', 4);
* console.log(o.get('p3')); // false
* o.set('p2', 5);
* console.log(o.get('p3')); // false
* </pre>
*
* @method gtProperties
* @param {string} dependentKey1
* @param {string} dependentKey2
* @returns {Ember.ComputedProperty}
*/
computed.gtProperties = function (dependentKey1, dependentKey2) {
return computed(dependentKey1, dependentKey2, function () {
return smartGet(this, dependentKey1) > smartGet(this, dependentKey2);
});
};
/**
* A computed property that returns true if dependent value is less than the needed value
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: Em.computed.lt('p1', 1)
* });
* console.log(o.get('p2')); // false
* o.set('p1', 4);
* console.log(o.get('p2')); // false
* o.set('p1', 5);
* console.log(o.get('p2')); // true
* </pre>
*
* @method lt
* @param {string} dependentKey
* @param {*} value
* @returns {Ember.ComputedProperty}
*/
computed.lt = function (dependentKey, value) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey) < value;
});
};
/**
* A computed property that returns true if first dependent property is less than the second dependent property
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 4,
* p2: 1,
* p3: Em.computed.ltProperties('p1', 'p2')
* });
* console.log(o.get('p3')); // false
* o.set('p2', 4);
* console.log(o.get('p3')); // false
* o.set('p2', 5);
* console.log(o.get('p3')); // true
* </pre>
*
* @method gtProperties
* @param {string} dependentKey1
* @param {string} dependentKey2
* @returns {Ember.ComputedProperty}
*/
computed.ltProperties = function (dependentKey1, dependentKey2) {
return computed(dependentKey1, dependentKey2, function () {
return smartGet(this, dependentKey1) < smartGet(this, dependentKey2);
});
};
/**
* A computed property that returns true if dependent property is match to the needed regular expression
* <pre>
* var o = Em.Object.create({
* p1: 'abc',
* p2: Em.computed.match('p1', /^a/)
* });
* console.log(o.get('p2')); // true
* o.set('p1', 'bc');
* console.log(o.get('p2')); // false
* </pre>
*
* @method match
* @param {string} dependentKey
* @param {RegExp} regexp
* @returns {Ember.ComputedProperty}
*/
computed.match = function (dependentKey, regexp) {
return computed(dependentKey, function () {
var value = get(this, dependentKey);
Iif (!regexp) {
return false;
}
return regexp.test(value);
});
};
/**
* A computed property that returns true of some collection's item has property with needed value
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 2}, {a: 3}],
* p2: Em.computed.someBy('p1', 'a', 1)
* });
* console.log(o.get('p2')); // true
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // false
* </pre>
*
* @method someBy
* @param {string} collectionKey
* @param {string} propertyName
* @param {*} neededValue
* @returns {Ember.ComputedProperty}
*/
computed.someBy = function (collectionKey, propertyName, neededValue) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return false;
}
return collection.someProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns true of some collection's item has property with needed value
* Needed value is stored in the another property
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 2}, {a: 3}],
* p2: Em.computed.someByKey('p1', 'a', 'v1'),
* v1: 1
* });
* console.log(o.get('p2')); // true
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // false
* </pre>
*
* @method someByKey
* @param {string} collectionKey
* @param {string} propertyName
* @param {string} neededValueKey
* @returns {Ember.ComputedProperty}
*/
computed.someByKey = function (collectionKey, propertyName, neededValueKey) {
return computed(collectionKey + '.@each.' + propertyName, neededValueKey, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return false;
}
var neededValue = smartGet(this, neededValueKey);
return collection.someProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns true of all collection's items have property with needed value
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 1}, {a: 1}],
* p2: Em.computed.everyBy('p1', 'a', 1)
* });
* console.log(o.get('p2')); // true
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // false
* </pre>
*
* @method everyBy
* @param {string} collectionKey
* @param {string} propertyName
* @param {*} neededValue
* @returns {Ember.ComputedProperty}
*/
computed.everyBy = function (collectionKey, propertyName, neededValue) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return false;
}
return collection.everyProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns true of all collection's items have property with needed value
* Needed value is stored in the another property
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 1}, {a: 1}],
* p2: Em.computed.everyByKey('p1', 'a', 'v1'),
* v1: 1
* });
* console.log(o.get('p2')); // true
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // false
* </pre>
*
* @method everyByKey
* @param {string} collectionKey
* @param {string} propertyName
* @param {string} neededValueKey
* @returns {Ember.ComputedProperty}
*/
computed.everyByKey = function (collectionKey, propertyName, neededValueKey) {
return computed(collectionKey + '.@each.' + propertyName, neededValueKey, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return false;
}
var neededValue = smartGet(this, neededValueKey);
return collection.everyProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns array with values of named property on all items in the collection
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 2}, {a: 3}],
* p2: Em.computed.everyBy('p1', 'a')
* });
* console.log(o.get('p2')); // [1, 2, 3]
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // [2, 2, 3]
* </pre>
*
* @method mapBy
* @param {string} collectionKey
* @param {string} propertyName
* @returns {Ember.ComputedProperty}
*/
computed.mapBy = function (collectionKey, propertyName) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return [];
}
return collection.mapProperty(propertyName);
});
};
/**
* A computed property that returns array with collection's items that have needed property value
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 2}, {a: 3}],
* p2: Em.computed.filterBy('p1', 'a', 2)
* });
* console.log(o.get('p2')); // [{a: 2}]
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // [{a: 2}, {a: 2}]
* </pre>
*
* @method filterBy
* @param {string} collectionKey
* @param {string} propertyName
* @param {*} neededValue
* @returns {Ember.ComputedProperty}
*/
computed.filterBy = function (collectionKey, propertyName, neededValue) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return [];
}
return collection.filterProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns array with collection's items that have needed property value
* Needed value is stored in the another property
*
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 2}, {a: 3}],
* p2: Em.computed.filterByKey('p1', 'a', 'v1'),
* v1: 2
* });
* console.log(o.get('p2')); // [{a: 2}]
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // [{a: 2}, {a: 2}]
* </pre>
*
* @method filterByKey
* @param {string} collectionKey
* @param {string} propertyName
* @param {string} neededValueKey
* @returns {Ember.ComputedProperty}
*/
computed.filterByKey = function (collectionKey, propertyName, neededValueKey) {
return computed(collectionKey + '.@each.' + propertyName, neededValueKey, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return [];
}
var neededValue = smartGet(this, neededValueKey);
return collection.filterProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns first collection's item that has needed property value
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}],
* p2: Em.computed.findBy('p1', 'a', 2)
* });
* console.log(o.get('p2')); // [{a: 2, b: 2}]
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // [{a: 2, b: 1}]
* </pre>
*
* @method findBy
* @param {string} collectionKey
* @param {string} propertyName
* @param {*} neededValue
* @returns {Ember.ComputedProperty}
*/
computed.findBy = function (collectionKey, propertyName, neededValue) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return null;
}
return collection.findProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns first collection's item that has needed property value
* Needed value is stored in the another property
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}],
* p2: Em.computed.findByKey('p1', 'a', 'v1'),
* v1: 2
* });
* console.log(o.get('p2')); // [{a: 2, b: 2}]
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // [{a: 2, b: 1}]
* </pre>
*
* @method findByKey
* @param {string} collectionKey
* @param {string} propertyName
* @param {string} neededValueKey
* @returns {Ember.ComputedProperty}
*/
computed.findByKey = function (collectionKey, propertyName, neededValueKey) {
return computed(collectionKey + '.@each.' + propertyName, neededValueKey, function () {
var collection = smartGet(this, collectionKey);
if (!collection) {
return null;
}
var neededValue = smartGet(this, neededValueKey);
return collection.findProperty(propertyName, neededValue);
});
};
/**
* A computed property that returns value equal to the dependent
* Should be used as 'short-name' for deeply-nested values
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: {a: {b: {c: 2}}},
* p2: Em.computed.alias('p1.a.b.c')
* });
* console.log(o.get('p2')); // 2
* o.set('p1.a.b.c', 4);
* console.log(o.get('p2')); // 4
* </pre>
*
* @method alias
* @param {string} dependentKey
* @returns {Ember.ComputedProperty}
*/
computed.alias = function (dependentKey) {
return computed(dependentKey, function () {
return smartGet(this, dependentKey);
});
};
/**
* A computed property that returns true if dependent property exists in the needed values
* <pre>
* var o = Em.Object.create({
* p1: 2,
* p2: Em.computed.existsIn('p1', [1, 2, 3])
* });
* console.log(o.get('p2')); // true
* o.set('p1', 4);
* console.log(o.get('p2')); // false
* </pre>
*
* @method existsIn
* @param {string} dependentKey
* @param {array} neededValues
* @returns {Ember.ComputedProperty}
*/
computed.existsIn = function (dependentKey, neededValues) {
return computed(dependentKey, function () {
var value = smartGet(this, dependentKey);
return makeArray(neededValues).contains(value);
});
};
/**
* A computed property that returns true if dependent property exists in the property with needed values
* <pre>
* var o = Em.Object.create({
* p1: 2,
* p2: Em.computed.existsInByKey('p1', 'p3'),
* p3: [1, 2, 3]
* });
* console.log(o.get('p2')); // true
* o.set('p1', 4);
* console.log(o.get('p2')); // false
* </pre>
*
* @method existsIn
* @param {string} dependentKey
* @param {string} neededValuesKey
* @returns {Ember.ComputedProperty}
*/
computed.existsInByKey = function (dependentKey, neededValuesKey) {
return computed(dependentKey, neededValuesKey + '.[]', function () {
var value = smartGet(this, dependentKey);
var neededValues = smartGet(this, neededValuesKey);
return makeArray(neededValues).contains(value);
});
};
/**
* A computed property that returns true if dependent property doesn't exist in the needed values
* <pre>
* var o = Em.Object.create({
* p1: 2,
* p2: Em.computed.notExistsIn('p1', [1, 2, 3])
* });
* console.log(o.get('p2')); // false
* o.set('p1', 4);
* console.log(o.get('p2')); // true
* </pre>
*
* @method notExistsIn
* @param {string} dependentKey
* @param {array} neededValues
* @returns {Ember.ComputedProperty}
*/
computed.notExistsIn = function (dependentKey, neededValues) {
return computed(dependentKey, function () {
var value = smartGet(this, dependentKey);
return !makeArray(neededValues).contains(value);
});
};
/**
* A computed property that returns true if dependent property doesn't exist in the property with needed values
* <pre>
* var o = Em.Object.create({
* p1: 2,
* p2: Em.computed.notExistsInByKey('p1', 'p3'),
* p3: [1, 2, 3]
* });
* console.log(o.get('p2')); // false
* o.set('p1', 4);
* console.log(o.get('p2')); // true
* </pre>
*
* @method notExistsInByKey
* @param {string} dependentKey
* @param {string} neededValuesKey
* @returns {Ember.ComputedProperty}
*/
computed.notExistsInByKey = function (dependentKey, neededValuesKey) {
return computed(dependentKey, neededValuesKey + '.[]', function () {
var value = smartGet(this, dependentKey);
var neededValues = smartGet(this, neededValuesKey);
return !makeArray(neededValues).contains(value);
});
};
/**
* A computed property that returns result of calculation <code>(dependentProperty1/dependentProperty2 * 100)</code>
* If accuracy is 0 (by default), result is rounded to integer
* Otherwise - result is float with provided accuracy
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 2,
* p2: 4,
* p3: Em.computed.percents('p1', 'p2')
* });
* console.log(o.get('p3')); // 50
* o.set('p2', 5);
* console.log(o.get('p3')); // 40
* </pre>
*
* @method percents
* @param {string} dependentKey1
* @param {string} dependentKey2
* @param {number} [accuracy=0]
* @returns {Ember.ComputedProperty}
*/
computed.percents = function (dependentKey1, dependentKey2, accuracy) {
if (arguments.length < 3) {
accuracy = 0;
}
return computed(dependentKey1, dependentKey2, function () {
var v1 = Number(smartGet(this, dependentKey1));
var v2 = Number(smartGet(this, dependentKey2));
var result = v1 / v2 * 100;
if (0 === accuracy) {
return Math.round(result);
}
return parseFloat(result.toFixed(accuracy));
});
};
/**
* A computed property that returns result of <code>App.format.role</code> for dependent value
* <pre>
* var o = Em.Object.create({
* p1: 'SECONDARY_NAMENODE',
* p3: Em.computed.formatRole('p1', false)
* });
* console.log(o.get('p2')); // 'SNameNode'
* o.set('p1', 'FLUME_HANDLER);
* console.log(o.get('p2')); // 'Flume'
* </pre>
*
* @method formatRole
* @param {string} dependentKey
* @param {boolean} isServiceRole
* @returns {Ember.ComputedProperty}
*/
computed.formatRole = function (dependentKey, isServiceRole) {
return computed(dependentKey, function () {
var value = get(this, dependentKey);
return App.format.role(value, isServiceRole);
});
};
/**
* A computed property that returns sum of the named property in the each collection's item
* <pre>
* var o = Em.Object.create({
* p1: [{a: 1}, {a: 2}, {a: 3}],
* p2: Em.computed.sumBy('p1', 'a')
* });
* console.log(o.get('p2')); // 6
* o.set('p1.0.a', 2);
* console.log(o.get('p2')); // 7
* </pre>
*
* @method sumBy
* @param {string} collectionKey
* @param {string} propertyName
* @returns {Ember.ComputedProperty}
*/
computed.sumBy = function (collectionKey, propertyName) {
return computed(collectionKey + '.@each.' + propertyName, function () {
var collection = smartGet(this, collectionKey);
if (Em.isEmpty(collection)) {
return 0;
}
var sum = 0;
collection.forEach(function (item) {
sum += Number(get(item, propertyName));
});
return sum;
});
};
/**
* A computed property that returns I18n-string formatted with dependent properties
* Takes at least one argument
* App.*-keys are supported
*
* @param {string} key key in the I18n-messages
* @param {...string} dependentKeys
* @method i18nFormat
* @returns {Ember.ComputedProperty}
*/
computed.i18nFormat = generateComputedWithKey(function (key, dependentValues) {
var str = Em.I18n.t(key);
if (!str) {
return '';
}
return str.format.apply(str, dependentValues);
});
/**
* A computed property that returns string formatted with dependent properties
* Takes at least one argument
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 'abc',
* p2: 'cba',
* p3: Em.computed.format('{0} => {1}', 'p1', 'p2')
* });
* console.log(o.get('p3')); // 'abc => cba'
* o.set('p1', 'aaa');
* console.log(o.get('p3')); // 'aaa => cba'
* </pre>
*
* @param {string} str string to format
* @param {...string} dependentKeys
* @method format
* @returns {Ember.ComputedProperty}
*/
computed.format = generateComputedWithKey(function (str, dependentValues) {
if (!str) {
return '';
}
return str.format.apply(str, dependentValues);
});
/**
* A computed property that returns dependent values joined with separator
* Takes at least one argument
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 'abc',
* p2: 'cba',
* p3: Em.computed.concat('|', 'p1', 'p2')
* });
* console.log(o.get('p3')); // 'abc|cba'
* o.set('p1', 'aaa');
* console.log(o.get('p3')); // 'aaa|cba'
* </pre>
*
* @param {string} separator
* @param {...string} dependentKeys
* @method concat
* @return {Ember.ComputedProperty}
*/
computed.concat = generateComputedWithKey(function (separator, dependentValues) {
return dependentValues.join(separator);
});
/**
* A computed property that returns first not blank value from dependent values
* Based on <code>Ember.isBlank</code>
* Takes at least 1 argument
* Dependent values order affects the result
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: null,
* p2: '',
* p3: 'abc'
* p4: Em.computed.firstNotBlank('p1', 'p2', 'p3')
* });
* console.log(o.get('p4')); // 'abc'
* o.set('p1', 'aaa');
* console.log(o.get('p4')); // 'aaa'
* </pre>
*
* @param {...string} dependentKeys
* @method firstNotBlank
* @return {Ember.ComputedProperty}
*/
computed.firstNotBlank = generateComputedWithValues(function (values) {
for (var i = 0; i < values.length; i++) {
if (!Em.isBlank(values[i])) {
return values[i];
}
}
return null;
});
/**
* A computed property that returns dependent value if it is truly or ('0'|0)
* Returns <code>'n/a'</code> otherwise
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 0,
* p2: Em.computed.formatUnavailable('p1')
* });
* console.log(o.get('p2')); // 0
* o.set('p1', 12);
* console.log(o.get('p2')); // 12
* o.set('p1', 'some string');
* console.log(o.get('p2')); // 'n/a'
* </pre>
*
* @param {string} dependentKey
* @method formatUnavailable
* @returns {Ember.ComputedProperty}
*/
computed.formatUnavailable = function (dependentKey) {
return computed(dependentKey, function () {
var value = smartGet(this, dependentKey);
return value || value == 0 ? value : Em.I18n.t('services.service.summary.notAvailable');
});
};
/**
* A computed property that returns one of provided values basing on dependent value
* If dependent value is 0, <code>zeroMsg</code> is returned
* If dependent value is 1, <code>oneMsg</code> is returned
* If dependent value is greater than 1, <code>manyMsg</code> is returned
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: 0,
* p2: Em.computed.formatUnavailable('p1', '0msg', '1msg', '2+msg')
* });
* console.log(o.get('p2')); // '0msg'
* o.set('p1', 1);
* console.log(o.get('p2')); // '1msg'
* o.set('p1', 100500);
* console.log(o.get('p2')); // '2+msg'
* </pre>
*
* @param {string} dependentKey
* @param {string} zeroMsg
* @param {string} oneMsg
* @param {string} manyMsg
* @returns {Ember.ComputedProperty}
* @method countBasedMessage
*/
computed.countBasedMessage = function (dependentKey, zeroMsg, oneMsg, manyMsg) {
return computed(dependentKey, function () {
var value = Number(smartGet(this, dependentKey));
if (value === 0) {
return zeroMsg;
}
if (value > 1) {
return manyMsg;
}
return oneMsg;
});
};
/**
* A computed property that returns property value according to the property key and object key
* App.*-keys are supported
* <pre>
* var o = Em.Object.create({
* p1: {a: 1, b: 2, c: 3},
* p2: 'a',
* p3: Em.computed.getByKey('p1', 'p2')
* });
* console.log(o.get('p3')); // 1
* o.set('p2', 'b');
* console.log(o.get('p3')); // 2
* o.set('p2', 'c');
* console.log(o.get('p3')); // 3
* </pre>
*
* With `defaultValue`
* <pre>
* var o = Em.Object.create({
* p1: {a: 1, b: 2, c: 3},
* p2: 'd',
* p3: Em.computed.getByKey('p1', 'p2', 100500)
* });
* console.log(o.get('p3')); // 100500 - default value is returned, because there is no key `d` in the `p1`
* </pre>
* <b>IMPORTANT!</b> This CP <b>SHOULD NOT</b> be used with for object with values equal to the views (like <code>{a: App.MyViewA, b: App.MyViewB}</code>)
* This restriction exists because views may be undefined on the moment when this CP is calculated (files are not `required` yet)
*
* @param {string} objectKey
* @param {string} propertyKey
* @param {*} [defaultValue]
* @returns {Ember.ComputedProperty}
*/
computed.getByKey = function (objectKey, propertyKey, defaultValue) {
return computed(objectKey, propertyKey, function () {
var object = smartGet(this, objectKey);
var property = smartGet(this, propertyKey);
if (!object) {
return null;
}
return object.hasOwnProperty(property) ? object[property] : defaultValue;
});
};
/**
* A computed property that returns dependent value truncated to the `reduceTo`-size if its length is greater than `maxLength`
* Truncated part may be replaced with `replacer` if it's provided ('...' by default)
* <pre>
* var o = Em.Object.create({
* p1: Em.computed.truncate('p2', 8, 5, '###'),
* p2: 'some string',
* p3: Em.computed.truncate('p2', 8, 5)
* });
* console.log(o.get('p1')); // 'some ###'
* console.log(o.get('p3')); // 'some ...'
* o.set('p2', '123456789');
* console.log(o.get('p1')); // '12345###'
* console.log(o.get('p3')); // '12345...'
* </pre>
*
* @param {string} dependentKey
* @param {number} maxLength
* @param {number} reduceTo
* @param {string} [replacer] default - '...'
* @returns {Ember.ComputedProperty}
*/
computed.truncate = function (dependentKey, maxLength, reduceTo, replacer) {
Em.assert('`reduceTo` should be <=`maxLength`', reduceTo <= maxLength);
var _replacer = arguments.length > 3 ? replacer : '...';
return computed(dependentKey, function () {
var value = smartGet(this, dependentKey) || '';
if (value.length > maxLength) {
return value.substr(0, reduceTo) + _replacer;
}
return value;
});
};
}); |