| 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
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883 |
1
1
1
1
1
1
92
92
92
92
53
92
1
1
884
173
174
173
173
1
172
711
985
985
21
21
21
1
1
26
1
29
25
23
24
18
10
5
1
1
54
13
1
178
178
2
1
1
171
169
169
169
169
169
11
11
9
9
9
13
9
9
9
16
9
9
9
9
1
7
1
6
6
6
6
6
6
6
4
1
44
44
1
1
1
34
32
32
1
1
1
31
30
30
30
31
31
31
15
12
8
6
21
21
21
13
21
14
14
5
21
6
21
19
17
17
1
1
1
44
41
1
1
1
8
8
1
1
1
1
23
1
40
40
40
13
40
9
9
9
1
19
19
7
19
19
19
8
8
8
8
8
8
7
7
7
7
7
3
4
4
1
1
1
12
12
12
12
1
259
259
67
259
259
259
256
259
259
259
11
259
259
7
7
7
7
259
259
259
207
207
207
207
207
20
20
20
20
20
20
20
20
20
20
20
20
1
1
8
8
8
8
8
8
89
1
1
89
1
1
6
6
6
6
6
6
6
6
6
1
2
2
2
2
2
2
1
166
166
166
166
166
1
1
4
4
4
4
1
16
16
16
16
16
16
16
16
16
16
1
1
1
1
7
7
7
7
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
20
20
20
20
20
20
1
13
13
6
6
8
8
24
8
13
13
13
13
13
13
13
13
13
13
13
25
25
17
17
17
8
8
8
8
8
13
13
4
4
4
1
1
1
1
1
1
1
1
1
1
9
9
1
1
1
10
10
10
10
10
10
10
10
12
12
24
24
18
18
18
6
6
6
18
18
3
10
10
10
1
181
181
181
181
181
16
16
15
7
26
1
3
990
990
975
966
1
1
191
191
6
191
191
200
191
8
8
8
8
8
8
8
191
6
6
189
175
175
175
184
175
175
175
86
86
86
86
1
13
13
14
14
14
14
14
14
14
1
37
37
15
37
37
4
4
4
4
37
37
37
37
37
37
3
3
12
12
8
8
8
8
1
5
4
3
1
3
2
8
8
8
8
5
31
31
31
31
31
30
1
31
31
31
31
31
1
12
19
19
19
1
9
9
1
9
5
5
5
2
2
1
41
41
32
41
41
105
105
41
41
41
41
41
41
41
41
41
78
3
15
15
4
4
11
11
11
11
11
11
11
9
2
11
7
2
2
2
5
5
5
5
5
30
73
30
60
60
30
30
30
30
67
67
67
156
156
67
67
67
2
6
6
2
14
1
13
13
13
13
8
3
13
16
13
4
4
3
1
3
3
1
1
9
9
9
13
13
13
26
26
26
26
17
6
6
6
6
6
5
5
5
2
2
2
2
2
2
2
3
2
2
6
6
6
6
6
6
2
2
2
2
6
1
8
8
1
7
7
7
7
7
7
7
7
7
7
7
7
7
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
4
4
4
4
4
4
4
4
4
1
1
1
1
1
| /*!
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2014 Jimmy Yuen Ho Wong and contributors <wyuenho@gmail.com>
Licensed under the MIT license.
*/
(function (factory) {
// CommonJS
Iif (typeof exports == "object") {
module.exports = factory(module.exports,
require("underscore"),
require("backbone"));
}
// Browser
else factory(this, this._, this.Backbone);
}(function (root, _, Backbone) {
"use strict";
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
// Copyright 2009, 2010 Kristopher Michael Kowal
// https://github.com/kriskowal/es5-shim
// ES5 15.5.4.20
// http://es5.github.com/#x15.5.4.20
var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" +
"\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" +
"\u2029\uFEFF";
Iif (!String.prototype.trim || ws.trim()) {
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
// http://perfectionkills.com/whitespace-deviations/
ws = "[" + ws + "]";
var trimBeginRegexp = new RegExp("^" + ws + ws + "*"),
trimEndRegexp = new RegExp(ws + ws + "*$");
String.prototype.trim = function trim() {
if (this === undefined || this === null) {
throw new TypeError("can't convert " + this + " to object");
}
return String(this)
.replace(trimBeginRegexp, "")
.replace(trimEndRegexp, "");
};
}
function lpad(str, length, padstr) {
var paddingLen = length - (str + '').length;
paddingLen = paddingLen < 0 ? 0 : paddingLen;
var padding = '';
for (var i = 0; i < paddingLen; i++) {
padding = padding + padstr;
}
return padding + str;
}
var $ = Backbone.$;
var Backgrid = root.Backgrid = {
Extension: {},
resolveNameToClass: function (name, suffix) {
if (_.isString(name)) {
var key = _.map(name.split('-'), function (e) {
return e.slice(0, 1).toUpperCase() + e.slice(1);
}).join('') + suffix;
var klass = Backgrid[key] || Backgrid.Extension[key];
if (_.isUndefined(klass)) {
throw new ReferenceError("Class '" + key + "' not found");
}
return klass;
}
return name;
},
callByNeed: function () {
var value = arguments[0];
if (!_.isFunction(value)) return value;
var context = arguments[1];
var args = [].slice.call(arguments, 2);
return value.apply(context, !!(args + '') ? args : []);
}
};
_.extend(Backgrid, Backbone.Events);
/**
Command translates a DOM Event into commands that Backgrid
recognizes. Interested parties can listen on selected Backgrid events that
come with an instance of this class and act on the commands.
It is also possible to globally rebind the keyboard shortcuts by replacing
the methods in this class' prototype.
@class Backgrid.Command
@constructor
*/
var Command = Backgrid.Command = function (evt) {
_.extend(this, {
altKey: !!evt.altKey,
"char": evt["char"],
charCode: evt.charCode,
ctrlKey: !!evt.ctrlKey,
key: evt.key,
keyCode: evt.keyCode,
locale: evt.locale,
location: evt.location,
metaKey: !!evt.metaKey,
repeat: !!evt.repeat,
shiftKey: !!evt.shiftKey,
which: evt.which
});
};
_.extend(Command.prototype, {
/**
Up Arrow
@member Backgrid.Command
*/
moveUp: function () { return this.keyCode == 38; },
/**
Down Arrow
@member Backgrid.Command
*/
moveDown: function () { return this.keyCode === 40; },
/**
Shift Tab
@member Backgrid.Command
*/
moveLeft: function () { return this.shiftKey && this.keyCode === 9; },
/**
Tab
@member Backgrid.Command
*/
moveRight: function () { return !this.shiftKey && this.keyCode === 9; },
/**
Enter
@member Backgrid.Command
*/
save: function () { return this.keyCode === 13; },
/**
Esc
@member Backgrid.Command
*/
cancel: function () { return this.keyCode === 27; },
/**
None of the above.
@member Backgrid.Command
*/
passThru: function () {
return !(this.moveUp() || this.moveDown() || this.moveLeft() ||
this.moveRight() || this.save() || this.cancel());
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
Just a convenient class for interested parties to subclass.
The default Cell classes don't require the formatter to be a subclass of
Formatter as long as the fromRaw(rawData) and toRaw(formattedData) methods
are defined.
@abstract
@class Backgrid.CellFormatter
@constructor
*/
var CellFormatter = Backgrid.CellFormatter = function () {};
_.extend(CellFormatter.prototype, {
/**
Takes a raw value from a model and returns an optionally formatted string
for display. The default implementation simply returns the supplied value
as is without any type conversion.
@member Backgrid.CellFormatter
@param {*} rawData
@param {Backbone.Model} model Used for more complicated formatting
@return {*}
*/
fromRaw: function (rawData, model) {
return rawData;
},
/**
Takes a formatted string, usually from user input, and returns a
appropriately typed value for persistence in the model.
If the user input is invalid or unable to be converted to a raw value
suitable for persistence in the model, toRaw must return `undefined`.
@member Backgrid.CellFormatter
@param {string} formattedData
@param {Backbone.Model} model Used for more complicated formatting
@return {*|undefined}
*/
toRaw: function (formattedData, model) {
return formattedData;
}
});
/**
A floating point number formatter. Doesn't understand scientific notation at
the moment.
@class Backgrid.NumberFormatter
@extends Backgrid.CellFormatter
@constructor
@throws {RangeError} If decimals < 0 or > 20.
*/
var NumberFormatter = Backgrid.NumberFormatter = function (options) {
_.extend(this, this.defaults, options || {});
if (this.decimals < 0 || this.decimals > 20) {
throw new RangeError("decimals must be between 0 and 20");
}
};
NumberFormatter.prototype = new CellFormatter();
_.extend(NumberFormatter.prototype, {
/**
@member Backgrid.NumberFormatter
@cfg {Object} options
@cfg {number} [options.decimals=2] Number of decimals to display. Must be an integer.
@cfg {string} [options.decimalSeparator='.'] The separator to use when
displaying decimals.
@cfg {string} [options.orderSeparator=','] The separator to use to
separator thousands. May be an empty string.
*/
defaults: {
decimals: 2,
decimalSeparator: '.',
orderSeparator: ','
},
HUMANIZED_NUM_RE: /(\d)(?=(?:\d{3})+$)/g,
/**
Takes a floating point number and convert it to a formatted string where
every thousand is separated by `orderSeparator`, with a `decimal` number of
decimals separated by `decimalSeparator`. The number returned is rounded
the usual way.
@member Backgrid.NumberFormatter
@param {number} number
@param {Backbone.Model} model Used for more complicated formatting
@return {string}
*/
fromRaw: function (number, model) {
if (_.isNull(number) || _.isUndefined(number)) return '';
number = number.toFixed(~~this.decimals);
var parts = number.split('.');
var integerPart = parts[0];
var decimalPart = parts[1] ? (this.decimalSeparator || '.') + parts[1] : '';
return integerPart.replace(this.HUMANIZED_NUM_RE, '$1' + this.orderSeparator) + decimalPart;
},
/**
Takes a string, possibly formatted with `orderSeparator` and/or
`decimalSeparator`, and convert it back to a number.
@member Backgrid.NumberFormatter
@param {string} formattedData
@param {Backbone.Model} model Used for more complicated formatting
@return {number|undefined} Undefined if the string cannot be converted to
a number.
*/
toRaw: function (formattedData, model) {
formattedData = formattedData.trim();
if (formattedData === '') return null;
var rawData = '';
var thousands = formattedData.split(this.orderSeparator);
for (var i = 0; i < thousands.length; i++) {
rawData += thousands[i];
}
var decimalParts = rawData.split(this.decimalSeparator);
rawData = '';
for (var i = 0; i < decimalParts.length; i++) {
rawData = rawData + decimalParts[i] + '.';
}
Eif (rawData[rawData.length - 1] === '.') {
rawData = rawData.slice(0, rawData.length - 1);
}
var result = (rawData * 1).toFixed(~~this.decimals) * 1;
if (_.isNumber(result) && !_.isNaN(result)) return result;
}
});
/**
A number formatter that converts a floating point number, optionally
multiplied by a multiplier, to a percentage string and vice versa.
@class Backgrid.PercentFormatter
@extends Backgrid.NumberFormatter
@constructor
@throws {RangeError} If decimals < 0 or > 20.
*/
var PercentFormatter = Backgrid.PercentFormatter = function () {
Backgrid.NumberFormatter.apply(this, arguments);
};
PercentFormatter.prototype = new Backgrid.NumberFormatter(),
_.extend(PercentFormatter.prototype, {
/**
@member Backgrid.PercentFormatter
@cfg {Object} options
@cfg {number} [options.multiplier=1] The number used to multiply the model
value for display.
@cfg {string} [options.symbol='%'] The symbol to append to the percentage
string.
*/
defaults: _.extend({}, NumberFormatter.prototype.defaults, {
multiplier: 1,
symbol: "%"
}),
/**
Takes a floating point number, where the number is first multiplied by
`multiplier`, then converted to a formatted string like
NumberFormatter#fromRaw, then finally append `symbol` to the end.
@member Backgrid.PercentFormatter
@param {number} rawValue
@param {Backbone.Model} model Used for more complicated formatting
@return {string}
*/
fromRaw: function (number, model) {
var args = [].slice.call(arguments, 1);
args.unshift(number * this.multiplier);
return (NumberFormatter.prototype.fromRaw.apply(this, args) || "0") + this.symbol;
},
/**
Takes a string, possibly appended with `symbol` and/or `decimalSeparator`,
and convert it back to a number for the model like NumberFormatter#toRaw,
and then dividing it by `multiplier`.
@member Backgrid.PercentFormatter
@param {string} formattedData
@param {Backbone.Model} model Used for more complicated formatting
@return {number|undefined} Undefined if the string cannot be converted to
a number.
*/
toRaw: function (formattedValue, model) {
var tokens = formattedValue.split(this.symbol);
Eif (tokens && tokens[0] && tokens[1] === "" || tokens[1] == null) {
var rawValue = NumberFormatter.prototype.toRaw.call(this, tokens[0]);
if (_.isUndefined(rawValue)) return rawValue;
return rawValue / this.multiplier;
}
}
});
/**
Formatter to converts between various datetime formats.
This class only understands ISO-8601 formatted datetime strings and UNIX
offset (number of milliseconds since UNIX Epoch). See
Backgrid.Extension.MomentFormatter if you need a much more flexible datetime
formatter.
@class Backgrid.DatetimeFormatter
@extends Backgrid.CellFormatter
@constructor
@throws {Error} If both `includeDate` and `includeTime` are false.
*/
var DatetimeFormatter = Backgrid.DatetimeFormatter = function (options) {
_.extend(this, this.defaults, options || {});
if (!this.includeDate && !this.includeTime) {
throw new Error("Either includeDate or includeTime must be true");
}
};
DatetimeFormatter.prototype = new CellFormatter();
_.extend(DatetimeFormatter.prototype, {
/**
@member Backgrid.DatetimeFormatter
@cfg {Object} options
@cfg {boolean} [options.includeDate=true] Whether the values include the
date part.
@cfg {boolean} [options.includeTime=true] Whether the values include the
time part.
@cfg {boolean} [options.includeMilli=false] If `includeTime` is true,
whether to include the millisecond part, if it exists.
*/
defaults: {
includeDate: true,
includeTime: true,
includeMilli: false
},
DATE_RE: /^([+\-]?\d{4})-(\d{2})-(\d{2})$/,
TIME_RE: /^(\d{2}):(\d{2}):(\d{2})(\.(\d{3}))?$/,
ISO_SPLITTER_RE: /T|Z| +/,
_convert: function (data, validate) {
if ((data + '').trim() === '') return null;
var date, time = null;
if (_.isNumber(data)) {
var jsDate = new Date(data);
date = lpad(jsDate.getUTCFullYear(), 4, 0) + '-' + lpad(jsDate.getUTCMonth() + 1, 2, 0) + '-' + lpad(jsDate.getUTCDate(), 2, 0);
time = lpad(jsDate.getUTCHours(), 2, 0) + ':' + lpad(jsDate.getUTCMinutes(), 2, 0) + ':' + lpad(jsDate.getUTCSeconds(), 2, 0);
}
else {
data = data.trim();
var parts = data.split(this.ISO_SPLITTER_RE) || [];
date = this.DATE_RE.test(parts[0]) ? parts[0] : '';
time = date && parts[1] ? parts[1] : this.TIME_RE.test(parts[0]) ? parts[0] : '';
}
var YYYYMMDD = this.DATE_RE.exec(date) || [];
var HHmmssSSS = this.TIME_RE.exec(time) || [];
if (validate) {
if (this.includeDate && _.isUndefined(YYYYMMDD[0])) return;
if (this.includeTime && _.isUndefined(HHmmssSSS[0])) return;
if (!this.includeDate && date) return;
if (!this.includeTime && time) return;
}
var jsDate = new Date(Date.UTC(YYYYMMDD[1] * 1 || 0,
YYYYMMDD[2] * 1 - 1 || 0,
YYYYMMDD[3] * 1 || 0,
HHmmssSSS[1] * 1 || null,
HHmmssSSS[2] * 1 || null,
HHmmssSSS[3] * 1 || null,
HHmmssSSS[5] * 1 || null));
var result = '';
if (this.includeDate) {
result = lpad(jsDate.getUTCFullYear(), 4, 0) + '-' + lpad(jsDate.getUTCMonth() + 1, 2, 0) + '-' + lpad(jsDate.getUTCDate(), 2, 0);
}
if (this.includeTime) {
result = result + (this.includeDate ? 'T' : '') + lpad(jsDate.getUTCHours(), 2, 0) + ':' + lpad(jsDate.getUTCMinutes(), 2, 0) + ':' + lpad(jsDate.getUTCSeconds(), 2, 0);
if (this.includeMilli) {
result = result + '.' + lpad(jsDate.getUTCMilliseconds(), 3, 0);
}
}
if (this.includeDate && this.includeTime) {
result += "Z";
}
return result;
},
/**
Converts an ISO-8601 formatted datetime string to a datetime string, date
string or a time string. The timezone is ignored if supplied.
@member Backgrid.DatetimeFormatter
@param {string} rawData
@param {Backbone.Model} model Used for more complicated formatting
@return {string|null|undefined} ISO-8601 string in UTC. Null and undefined
values are returned as is.
*/
fromRaw: function (rawData, model) {
if (_.isNull(rawData) || _.isUndefined(rawData)) return '';
return this._convert(rawData);
},
/**
Converts an ISO-8601 formatted datetime string to a datetime string, date
string or a time string. The timezone is ignored if supplied. This method
parses the input values exactly the same way as
Backgrid.Extension.MomentFormatter#fromRaw(), in addition to doing some
sanity checks.
@member Backgrid.DatetimeFormatter
@param {string} formattedData
@param {Backbone.Model} model Used for more complicated formatting
@return {string|undefined} ISO-8601 string in UTC. Undefined if a date is
found when `includeDate` is false, or a time is found when `includeTime` is
false, or if `includeDate` is true and a date is not found, or if
`includeTime` is true and a time is not found.
*/
toRaw: function (formattedData, model) {
return this._convert(formattedData, true);
}
});
/**
Formatter to convert any value to string.
@class Backgrid.StringFormatter
@extends Backgrid.CellFormatter
@constructor
*/
var StringFormatter = Backgrid.StringFormatter = function () {};
StringFormatter.prototype = new CellFormatter();
_.extend(StringFormatter.prototype, {
/**
Converts any value to a string using Ecmascript's implicit type
conversion. If the given value is `null` or `undefined`, an empty string is
returned instead.
@member Backgrid.StringFormatter
@param {*} rawValue
@param {Backbone.Model} model Used for more complicated formatting
@return {string}
*/
fromRaw: function (rawValue, model) {
if (_.isUndefined(rawValue) || _.isNull(rawValue)) return '';
return rawValue + '';
}
});
/**
Simple email validation formatter.
@class Backgrid.EmailFormatter
@extends Backgrid.CellFormatter
@constructor
*/
var EmailFormatter = Backgrid.EmailFormatter = function () {};
EmailFormatter.prototype = new CellFormatter();
_.extend(EmailFormatter.prototype, {
/**
Return the input if it is a string that contains an '@' character and if
the strings before and after '@' are non-empty. If the input does not
validate, `undefined` is returned.
@member Backgrid.EmailFormatter
@param {*} formattedData
@param {Backbone.Model} model Used for more complicated formatting
@return {string|undefined}
*/
toRaw: function (formattedData, model) {
var parts = formattedData.trim().split("@");
if (parts.length === 2 && _.all(parts)) {
return formattedData;
}
}
});
/**
Formatter for SelectCell.
If the type of a model value is not a string, it is expected that a subclass
of this formatter is provided to the SelectCell, with #toRaw overridden to
convert the string value returned from the DOM back to whatever value is
expected in the model.
@class Backgrid.SelectFormatter
@extends Backgrid.CellFormatter
@constructor
*/
var SelectFormatter = Backgrid.SelectFormatter = function () {};
SelectFormatter.prototype = new CellFormatter();
_.extend(SelectFormatter.prototype, {
/**
Normalizes raw scalar or array values to an array.
@member Backgrid.SelectFormatter
@param {*} rawValue
@param {Backbone.Model} model Used for more complicated formatting
@return {Array.<*>}
*/
fromRaw: function (rawValue, model) {
return _.isArray(rawValue) ? rawValue : rawValue != null ? [rawValue] : [];
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
Generic cell editor base class. Only defines an initializer for a number of
required parameters.
@abstract
@class Backgrid.CellEditor
@extends Backbone.View
*/
var CellEditor = Backgrid.CellEditor = Backbone.View.extend({
/**
Initializer.
@param {Object} options
@param {Backgrid.CellFormatter} options.formatter
@param {Backgrid.Column} options.column
@param {Backbone.Model} options.model
@throws {TypeError} If `formatter` is not a formatter instance, or when
`model` or `column` are undefined.
*/
initialize: function (options) {
this.formatter = options.formatter;
this.column = options.column;
if (!(this.column instanceof Column)) {
this.column = new Column(this.column);
}
this.listenTo(this.model, "backgrid:editing", this.postRender);
},
/**
Post-rendering setup and initialization. Focuses the cell editor's `el` in
this default implementation. **Should** be called by Cell classes after
calling Backgrid.CellEditor#render.
*/
postRender: function (model, column) {
Eif (column == null || column.get("name") == this.column.get("name")) {
this.$el.focus();
}
return this;
}
});
/**
InputCellEditor the cell editor type used by most core cell types. This cell
editor renders a text input box as its editor. The input will render a
placeholder if the value is empty on supported browsers.
@class Backgrid.InputCellEditor
@extends Backgrid.CellEditor
*/
var InputCellEditor = Backgrid.InputCellEditor = CellEditor.extend({
/** @property */
tagName: "input",
/** @property */
attributes: {
type: "text"
},
/** @property */
events: {
"blur": "saveOrCancel",
"keydown": "saveOrCancel"
},
/**
Initializer. Removes this `el` from the DOM when a `done` event is
triggered.
@param {Object} options
@param {Backgrid.CellFormatter} options.formatter
@param {Backgrid.Column} options.column
@param {Backbone.Model} options.model
@param {string} [options.placeholder]
*/
initialize: function (options) {
InputCellEditor.__super__.initialize.apply(this, arguments);
if (options.placeholder) {
this.$el.attr("placeholder", options.placeholder);
}
},
/**
Renders a text input with the cell value formatted for display, if it
exists.
*/
render: function () {
var model = this.model;
this.$el.val(this.formatter.fromRaw(model.get(this.column.get("name")), model));
return this;
},
/**
If the key pressed is `enter`, `tab`, `up`, or `down`, converts the value
in the editor to a raw value for saving into the model using the formatter.
If the key pressed is `esc` the changes are undone.
If the editor goes out of focus (`blur`) but the value is invalid, the
event is intercepted and cancelled so the cell remains in focus pending for
further action. The changes are saved otherwise.
Triggers a Backbone `backgrid:edited` event from the model when successful,
and `backgrid:error` if the value cannot be converted. Classes listening to
the `error` event, usually the Cell classes, should respond appropriately,
usually by rendering some kind of error feedback.
@param {Event} e
*/
saveOrCancel: function (e) {
var formatter = this.formatter;
var model = this.model;
var column = this.column;
var command = new Command(e);
var blurred = e.type === "blur";
if (command.moveUp() || command.moveDown() || command.moveLeft() || command.moveRight() ||
command.save() || blurred) {
e.preventDefault();
e.stopPropagation();
var val = this.$el.val();
var newValue = formatter.toRaw(val, model);
if (_.isUndefined(newValue)) {
model.trigger("backgrid:error", model, column, val);
}
else {
model.set(column.get("name"), newValue);
model.trigger("backgrid:edited", model, column, command);
}
}
// esc
else Eif (command.cancel()) {
// undo
e.stopPropagation();
model.trigger("backgrid:edited", model, column, command);
}
},
postRender: function (model, column) {
Eif (column == null || column.get("name") == this.column.get("name")) {
// move the cursor to the end on firefox if text is right aligned
Iif (this.$el.css("text-align") === "right") {
var val = this.$el.val();
this.$el.focus().val(null).val(val);
}
else this.$el.focus();
}
return this;
}
});
/**
The super-class for all Cell types. By default, this class renders a plain
table cell with the model value converted to a string using the
formatter. The table cell is clickable, upon which the cell will go into
editor mode, which is rendered by a Backgrid.InputCellEditor instance by
default. Upon encountering any formatting errors, this class will add an
`error` CSS class to the table cell.
@abstract
@class Backgrid.Cell
@extends Backbone.View
*/
var Cell = Backgrid.Cell = Backbone.View.extend({
/** @property */
tagName: "td",
/**
@property {Backgrid.CellFormatter|Object|string} [formatter=CellFormatter]
*/
formatter: CellFormatter,
/**
@property {Backgrid.CellEditor} [editor=Backgrid.InputCellEditor] The
default editor for all cell instances of this class. This value must be a
class, it will be automatically instantiated upon entering edit mode.
See Backgrid.CellEditor
*/
editor: InputCellEditor,
/** @property */
events: {
"click": "enterEditMode"
},
/**
Initializer.
@param {Object} options
@param {Backbone.Model} options.model
@param {Backgrid.Column} options.column
@throws {ReferenceError} If formatter is a string but a formatter class of
said name cannot be found in the Backgrid module.
*/
initialize: function (options) {
this.column = options.column;
if (!(this.column instanceof Column)) {
this.column = new Column(this.column);
}
var column = this.column, model = this.model, $el = this.$el;
var formatter = Backgrid.resolveNameToClass(column.get("formatter") ||
this.formatter, "Formatter");
if (!_.isFunction(formatter.fromRaw) && !_.isFunction(formatter.toRaw)) {
formatter = new formatter();
}
this.formatter = formatter;
this.editor = Backgrid.resolveNameToClass(this.editor, "CellEditor");
this.listenTo(model, "change:" + column.get("name"), function () {
if (!$el.hasClass("editor")) this.render();
});
this.listenTo(model, "backgrid:error", this.renderError);
this.listenTo(column, "change:editable change:sortable change:renderable",
function (column) {
var changed = column.changedAttributes();
for (var key in changed) {
Eif (changed.hasOwnProperty(key)) {
$el.toggleClass(key, changed[key]);
}
}
});
if (Backgrid.callByNeed(column.editable(), column, model)) $el.addClass("editable");
if (Backgrid.callByNeed(column.sortable(), column, model)) $el.addClass("sortable");
if (Backgrid.callByNeed(column.renderable(), column, model)) $el.addClass("renderable");
},
/**
Render a text string in a table cell. The text is converted from the
model's raw value for this cell's column.
*/
render: function () {
this.$el.empty();
var model = this.model;
this.$el.text(this.formatter.fromRaw(model.get(this.column.get("name")), model));
this.delegateEvents();
return this;
},
/**
If this column is editable, a new CellEditor instance is instantiated with
its required parameters. An `editor` CSS class is added to the cell upon
entering edit mode.
This method triggers a Backbone `backgrid:edit` event from the model when
the cell is entering edit mode and an editor instance has been constructed,
but before it is rendered and inserted into the DOM. The cell and the
constructed cell editor instance are sent as event parameters when this
event is triggered.
When this cell has finished switching to edit mode, a Backbone
`backgrid:editing` event is triggered from the model. The cell and the
constructed cell instance are also sent as parameters in the event.
When the model triggers a `backgrid:error` event, it means the editor is
unable to convert the current user input to an apprpriate value for the
model's column, and an `error` CSS class is added to the cell accordingly.
*/
enterEditMode: function () {
var model = this.model;
var column = this.column;
var editable = Backgrid.callByNeed(column.editable(), column, model);
Eif (editable) {
this.currentEditor = new this.editor({
column: this.column,
model: this.model,
formatter: this.formatter
});
model.trigger("backgrid:edit", model, column, this, this.currentEditor);
// Need to redundantly undelegate events for Firefox
this.undelegateEvents();
this.$el.empty();
this.$el.append(this.currentEditor.$el);
this.currentEditor.render();
this.$el.addClass("editor");
model.trigger("backgrid:editing", model, column, this, this.currentEditor);
}
},
/**
Put an `error` CSS class on the table cell.
*/
renderError: function (model, column) {
Eif (column == null || column.get("name") == this.column.get("name")) {
this.$el.addClass("error");
}
},
/**
Removes the editor and re-render in display mode.
*/
exitEditMode: function () {
this.$el.removeClass("error");
this.currentEditor.remove();
this.stopListening(this.currentEditor);
delete this.currentEditor;
this.$el.removeClass("editor");
this.render();
},
/**
Clean up this cell.
@chainable
*/
remove: function () {
if (this.currentEditor) {
this.currentEditor.remove.apply(this.currentEditor, arguments);
delete this.currentEditor;
}
return Cell.__super__.remove.apply(this, arguments);
}
});
/**
StringCell displays HTML escaped strings and accepts anything typed in.
@class Backgrid.StringCell
@extends Backgrid.Cell
*/
var StringCell = Backgrid.StringCell = Cell.extend({
/** @property */
className: "string-cell",
formatter: StringFormatter
});
/**
UriCell renders an HTML `<a>` anchor for the value and accepts URIs as user
input values. No type conversion or URL validation is done by the formatter
of this cell. Users who need URL validation are encourage to subclass UriCell
to take advantage of the parsing capabilities of the HTMLAnchorElement
available on HTML5-capable browsers or using a third-party library like
[URI.js](https://github.com/medialize/URI.js).
@class Backgrid.UriCell
@extends Backgrid.Cell
*/
var UriCell = Backgrid.UriCell = Cell.extend({
/** @property */
className: "uri-cell",
/**
@property {string} [title] The title attribute of the generated anchor. It
uses the display value formatted by the `formatter.fromRaw` by default.
*/
title: null,
/**
@property {string} [target="_blank"] The target attribute of the generated
anchor.
*/
target: "_blank",
initialize: function (options) {
UriCell.__super__.initialize.apply(this, arguments);
this.title = options.title || this.title;
this.target = options.target || this.target;
},
render: function () {
this.$el.empty();
var rawValue = this.model.get(this.column.get("name"));
var formattedValue = this.formatter.fromRaw(rawValue, this.model);
this.$el.append($("<a>", {
tabIndex: -1,
href: rawValue,
title: this.title || formattedValue,
target: this.target
}).text(formattedValue));
this.delegateEvents();
return this;
}
});
/**
Like Backgrid.UriCell, EmailCell renders an HTML `<a>` anchor for the
value. The `href` in the anchor is prefixed with `mailto:`. EmailCell will
complain if the user enters a string that doesn't contain the `@` sign.
@class Backgrid.EmailCell
@extends Backgrid.StringCell
*/
var EmailCell = Backgrid.EmailCell = StringCell.extend({
/** @property */
className: "email-cell",
formatter: EmailFormatter,
render: function () {
this.$el.empty();
var model = this.model;
var formattedValue = this.formatter.fromRaw(model.get(this.column.get("name")), model);
this.$el.append($("<a>", {
tabIndex: -1,
href: "mailto:" + formattedValue,
title: formattedValue
}).text(formattedValue));
this.delegateEvents();
return this;
}
});
/**
NumberCell is a generic cell that renders all numbers. Numbers are formatted
using a Backgrid.NumberFormatter.
@class Backgrid.NumberCell
@extends Backgrid.Cell
*/
var NumberCell = Backgrid.NumberCell = Cell.extend({
/** @property */
className: "number-cell",
/**
@property {number} [decimals=2] Must be an integer.
*/
decimals: NumberFormatter.prototype.defaults.decimals,
/** @property {string} [decimalSeparator='.'] */
decimalSeparator: NumberFormatter.prototype.defaults.decimalSeparator,
/** @property {string} [orderSeparator=','] */
orderSeparator: NumberFormatter.prototype.defaults.orderSeparator,
/** @property {Backgrid.CellFormatter} [formatter=Backgrid.NumberFormatter] */
formatter: NumberFormatter,
/**
Initializes this cell and the number formatter.
@param {Object} options
@param {Backbone.Model} options.model
@param {Backgrid.Column} options.column
*/
initialize: function (options) {
NumberCell.__super__.initialize.apply(this, arguments);
var formatter = this.formatter;
formatter.decimals = this.decimals;
formatter.decimalSeparator = this.decimalSeparator;
formatter.orderSeparator = this.orderSeparator;
}
});
/**
An IntegerCell is just a Backgrid.NumberCell with 0 decimals. If a floating
point number is supplied, the number is simply rounded the usual way when
displayed.
@class Backgrid.IntegerCell
@extends Backgrid.NumberCell
*/
var IntegerCell = Backgrid.IntegerCell = NumberCell.extend({
/** @property */
className: "integer-cell",
/**
@property {number} decimals Must be an integer.
*/
decimals: 0
});
/**
A PercentCell is another Backgrid.NumberCell that takes a floating number,
optionally multiplied by a multiplier and display it as a percentage.
@class Backgrid.PercentCell
@extends Backgrid.NumberCell
*/
var PercentCell = Backgrid.PercentCell = NumberCell.extend({
/** @property */
className: "percent-cell",
/** @property {number} [multiplier=1] */
multiplier: PercentFormatter.prototype.defaults.multiplier,
/** @property {string} [symbol='%'] */
symbol: PercentFormatter.prototype.defaults.symbol,
/** @property {Backgrid.CellFormatter} [formatter=Backgrid.PercentFormatter] */
formatter: PercentFormatter,
/**
Initializes this cell and the percent formatter.
@param {Object} options
@param {Backbone.Model} options.model
@param {Backgrid.Column} options.column
*/
initialize: function () {
PercentCell.__super__.initialize.apply(this, arguments);
var formatter = this.formatter;
formatter.multiplier = this.multiplier;
formatter.symbol = this.symbol;
}
});
/**
DatetimeCell is a basic cell that accepts datetime string values in RFC-2822
or W3C's subset of ISO-8601 and displays them in ISO-8601 format. For a much
more sophisticated date time cell with better datetime formatting, take a
look at the Backgrid.Extension.MomentCell extension.
@class Backgrid.DatetimeCell
@extends Backgrid.Cell
See:
- Backgrid.Extension.MomentCell
- Backgrid.DatetimeFormatter
*/
var DatetimeCell = Backgrid.DatetimeCell = Cell.extend({
/** @property */
className: "datetime-cell",
/**
@property {boolean} [includeDate=true]
*/
includeDate: DatetimeFormatter.prototype.defaults.includeDate,
/**
@property {boolean} [includeTime=true]
*/
includeTime: DatetimeFormatter.prototype.defaults.includeTime,
/**
@property {boolean} [includeMilli=false]
*/
includeMilli: DatetimeFormatter.prototype.defaults.includeMilli,
/** @property {Backgrid.CellFormatter} [formatter=Backgrid.DatetimeFormatter] */
formatter: DatetimeFormatter,
/**
Initializes this cell and the datetime formatter.
@param {Object} options
@param {Backbone.Model} options.model
@param {Backgrid.Column} options.column
*/
initialize: function (options) {
DatetimeCell.__super__.initialize.apply(this, arguments);
var formatter = this.formatter;
formatter.includeDate = this.includeDate;
formatter.includeTime = this.includeTime;
formatter.includeMilli = this.includeMilli;
var placeholder = this.includeDate ? "YYYY-MM-DD" : "";
placeholder += (this.includeDate && this.includeTime) ? "T" : "";
placeholder += this.includeTime ? "HH:mm:ss" : "";
placeholder += (this.includeTime && this.includeMilli) ? ".SSS" : "";
this.editor = this.editor.extend({
attributes: _.extend({}, this.editor.prototype.attributes, this.editor.attributes, {
placeholder: placeholder
})
});
}
});
/**
DateCell is a Backgrid.DatetimeCell without the time part.
@class Backgrid.DateCell
@extends Backgrid.DatetimeCell
*/
var DateCell = Backgrid.DateCell = DatetimeCell.extend({
/** @property */
className: "date-cell",
/** @property */
includeTime: false
});
/**
TimeCell is a Backgrid.DatetimeCell without the date part.
@class Backgrid.TimeCell
@extends Backgrid.DatetimeCell
*/
var TimeCell = Backgrid.TimeCell = DatetimeCell.extend({
/** @property */
className: "time-cell",
/** @property */
includeDate: false
});
/**
BooleanCellEditor renders a checkbox as its editor.
@class Backgrid.BooleanCellEditor
@extends Backgrid.CellEditor
*/
var BooleanCellEditor = Backgrid.BooleanCellEditor = CellEditor.extend({
/** @property */
tagName: "input",
/** @property */
attributes: {
tabIndex: -1,
type: "checkbox"
},
/** @property */
events: {
"mousedown": function () {
this.mouseDown = true;
},
"blur": "enterOrExitEditMode",
"mouseup": function () {
this.mouseDown = false;
},
"change": "saveOrCancel",
"keydown": "saveOrCancel"
},
/**
Renders a checkbox and check it if the model value of this column is true,
uncheck otherwise.
*/
render: function () {
var model = this.model;
var val = this.formatter.fromRaw(model.get(this.column.get("name")), model);
this.$el.prop("checked", val);
return this;
},
/**
Event handler. Hack to deal with the case where `blur` is fired before
`change` and `click` on a checkbox.
*/
enterOrExitEditMode: function (e) {
if (!this.mouseDown) {
var model = this.model;
model.trigger("backgrid:edited", model, this.column, new Command(e));
}
},
/**
Event handler. Save the value into the model if the event is `change` or
one of the keyboard navigation key presses. Exit edit mode without saving
if `escape` was pressed.
*/
saveOrCancel: function (e) {
var model = this.model;
var column = this.column;
var formatter = this.formatter;
var command = new Command(e);
// skip ahead to `change` when space is pressed
Iif (command.passThru() && e.type != "change") return true;
Iif (command.cancel()) {
e.stopPropagation();
model.trigger("backgrid:edited", model, column, command);
}
var $el = this.$el;
Iif (command.save() || command.moveLeft() || command.moveRight() || command.moveUp() ||
command.moveDown()) {
e.preventDefault();
e.stopPropagation();
var val = formatter.toRaw($el.prop("checked"), model);
model.set(column.get("name"), val);
model.trigger("backgrid:edited", model, column, command);
}
else Eif (e.type == "change") {
var val = formatter.toRaw($el.prop("checked"), model);
model.set(column.get("name"), val);
$el.focus();
}
}
});
/**
BooleanCell renders a checkbox both during display mode and edit mode. The
checkbox is checked if the model value is true, unchecked otherwise.
@class Backgrid.BooleanCell
@extends Backgrid.Cell
*/
var BooleanCell = Backgrid.BooleanCell = Cell.extend({
/** @property */
className: "boolean-cell",
/** @property */
editor: BooleanCellEditor,
/** @property */
events: {
"click": "enterEditMode"
},
/**
Renders a checkbox and check it if the model value of this column is true,
uncheck otherwise.
*/
render: function () {
this.$el.empty();
var model = this.model, column = this.column;
var editable = Backgrid.callByNeed(column.editable(), column, model);
this.$el.append($("<input>", {
tabIndex: -1,
type: "checkbox",
checked: this.formatter.fromRaw(model.get(column.get("name")), model),
disabled: !editable
}));
this.delegateEvents();
return this;
}
});
/**
SelectCellEditor renders an HTML `<select>` fragment as the editor.
@class Backgrid.SelectCellEditor
@extends Backgrid.CellEditor
*/
var SelectCellEditor = Backgrid.SelectCellEditor = CellEditor.extend({
/** @property */
tagName: "select",
/** @property */
events: {
"change": "save",
"blur": "close",
"keydown": "close"
},
/** @property {function(Object, ?Object=): string} template */
template: _.template('<option value="<%- value %>" <%= selected ? \'selected="selected"\' : "" %>><%- text %></option>', null, {variable: null}),
setOptionValues: function (optionValues) {
this.optionValues = optionValues;
this.optionValues = _.result(this, "optionValues");
},
setMultiple: function (multiple) {
this.multiple = multiple;
this.$el.prop("multiple", multiple);
},
_renderOptions: function (nvps, selectedValues) {
var options = '';
for (var i = 0; i < nvps.length; i++) {
options = options + this.template({
text: nvps[i][0],
value: nvps[i][1],
selected: _.indexOf(selectedValues, nvps[i][1]) > -1
});
}
return options;
},
/**
Renders the options if `optionValues` is a list of name-value pairs. The
options are contained inside option groups if `optionValues` is a list of
object hashes. The name is rendered at the option text and the value is the
option value. If `optionValues` is a function, it is called without a
parameter.
*/
render: function () {
this.$el.empty();
var optionValues = _.result(this, "optionValues");
var model = this.model;
var selectedValues = this.formatter.fromRaw(model.get(this.column.get("name")), model);
Iif (!_.isArray(optionValues)) throw new TypeError("optionValues must be an array");
var optionValue = null;
var optionText = null;
var optionValue = null;
var optgroupName = null;
var optgroup = null;
for (var i = 0; i < optionValues.length; i++) {
var optionValue = optionValues[i];
if (_.isArray(optionValue)) {
optionText = optionValue[0];
optionValue = optionValue[1];
this.$el.append(this.template({
text: optionText,
value: optionValue,
selected: _.indexOf(selectedValues, optionValue) > -1
}));
}
else Eif (_.isObject(optionValue)) {
optgroupName = optionValue.name;
optgroup = $("<optgroup></optgroup>", { label: optgroupName });
optgroup.append(this._renderOptions.call(this, optionValue.values, selectedValues));
this.$el.append(optgroup);
}
else {
throw new TypeError("optionValues elements must be a name-value pair or an object hash of { name: 'optgroup label', value: [option name-value pairs] }");
}
}
this.delegateEvents();
return this;
},
/**
Saves the value of the selected option to the model attribute.
*/
save: function (e) {
var model = this.model;
var column = this.column;
model.set(column.get("name"), this.formatter.toRaw(this.$el.val(), model));
},
/**
Triggers a `backgrid:edited` event from the model so the body can close
this editor.
*/
close: function (e) {
var model = this.model;
var column = this.column;
var command = new Command(e);
Iif (command.cancel()) {
e.stopPropagation();
model.trigger("backgrid:edited", model, column, new Command(e));
}
else Eif (command.save() || command.moveLeft() || command.moveRight() ||
command.moveUp() || command.moveDown() || e.type == "blur") {
e.preventDefault();
e.stopPropagation();
this.save(e);
model.trigger("backgrid:edited", model, column, new Command(e));
}
}
});
/**
SelectCell is also a different kind of cell in that upon going into edit mode
the cell renders a list of options to pick from, as opposed to an input box.
SelectCell cannot be referenced by its string name when used in a column
definition because it requires an `optionValues` class attribute to be
defined. `optionValues` can either be a list of name-value pairs, to be
rendered as options, or a list of object hashes which consist of a key *name*
which is the option group name, and a key *values* which is a list of
name-value pairs to be rendered as options under that option group.
In addition, `optionValues` can also be a parameter-less function that
returns one of the above. If the options are static, it is recommended the
returned values to be memoized. `_.memoize()` is a good function to help with
that.
During display mode, the default formatter will normalize the raw model value
to an array of values whether the raw model value is a scalar or an
array. Each value is compared with the `optionValues` values using
Ecmascript's implicit type conversion rules. When exiting edit mode, no type
conversion is performed when saving into the model. This behavior is not
always desirable when the value type is anything other than string. To
control type conversion on the client-side, you should subclass SelectCell to
provide a custom formatter or provide the formatter to your column
definition.
See:
[$.fn.val()](http://api.jquery.com/val/)
@class Backgrid.SelectCell
@extends Backgrid.Cell
*/
var SelectCell = Backgrid.SelectCell = Cell.extend({
/** @property */
className: "select-cell",
/** @property */
editor: SelectCellEditor,
/** @property */
multiple: false,
/** @property */
formatter: SelectFormatter,
/**
@property {Array.<Array>|Array.<{name: string, values: Array.<Array>}>} optionValues
*/
optionValues: undefined,
/** @property */
delimiter: ', ',
/**
Initializer.
@param {Object} options
@param {Backbone.Model} options.model
@param {Backgrid.Column} options.column
@throws {TypeError} If `optionsValues` is undefined.
*/
initialize: function (options) {
SelectCell.__super__.initialize.apply(this, arguments);
this.listenTo(this.model, "backgrid:edit", function (model, column, cell, editor) {
Eif (column.get("name") == this.column.get("name")) {
editor.setOptionValues(this.optionValues);
editor.setMultiple(this.multiple);
}
});
},
/**
Renders the label using the raw value as key to look up from `optionValues`.
@throws {TypeError} If `optionValues` is malformed.
*/
render: function () {
this.$el.empty();
var optionValues = _.result(this, "optionValues");
var model = this.model;
var rawData = this.formatter.fromRaw(model.get(this.column.get("name")), model);
var selectedText = [];
try {
Iif (!_.isArray(optionValues) || _.isEmpty(optionValues)) throw new TypeError;
for (var k = 0; k < rawData.length; k++) {
var rawDatum = rawData[k];
for (var i = 0; i < optionValues.length; i++) {
var optionValue = optionValues[i];
if (_.isArray(optionValue)) {
var optionText = optionValue[0];
var optionValue = optionValue[1];
if (optionValue == rawDatum) selectedText.push(optionText);
}
else Eif (_.isObject(optionValue)) {
var optionGroupValues = optionValue.values;
for (var j = 0; j < optionGroupValues.length; j++) {
var optionGroupValue = optionGroupValues[j];
if (optionGroupValue[1] == rawDatum) {
selectedText.push(optionGroupValue[0]);
}
}
}
else {
throw new TypeError;
}
}
}
this.$el.append(selectedText.join(this.delimiter));
}
catch (ex) {
if (ex instanceof TypeError) {
throw new TypeError("'optionValues' must be of type {Array.<Array>|Array.<{name: string, values: Array.<Array>}>}");
}
throw ex;
}
this.delegateEvents();
return this;
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
A Column is a placeholder for column metadata.
You usually don't need to create an instance of this class yourself as a
collection of column instances will be created for you from a list of column
attributes in the Backgrid.js view class constructors.
@class Backgrid.Column
@extends Backbone.Model
*/
var Column = Backgrid.Column = Backbone.Model.extend({
/**
@cfg {Object} defaults Column defaults. To override any of these default
values, you can either change the prototype directly to override
Column.defaults globally or extend Column and supply the custom class to
Backgrid.Grid:
// Override Column defaults globally
Column.prototype.defaults.sortable = false;
// Override Column defaults locally
var MyColumn = Column.extend({
defaults: _.defaults({
editable: false
}, Column.prototype.defaults)
});
var grid = new Backgrid.Grid(columns: new Columns([{...}, {...}], {
model: MyColumn
}));
@cfg {string} [defaults.name] The default name of the model attribute.
@cfg {string} [defaults.label] The default label to show in the header.
@cfg {string|Backgrid.Cell} [defaults.cell] The default cell type. If this
is a string, the capitalized form will be used to look up a cell class in
Backbone, i.e.: string => StringCell. If a Cell subclass is supplied, it is
initialized with a hash of parameters. If a Cell instance is supplied, it
is used directly.
@cfg {string|Backgrid.HeaderCell} [defaults.headerCell] The default header
cell type.
@cfg {boolean|string|function(): boolean} [defaults.sortable=true] Whether
this column is sortable. If the value is a string, a method will the same
name will be looked up from the column instance to determine whether the
column should be sortable. The method's signature must be `function
(Backgrid.Column, Backbone.Model): boolean`.
@cfg {boolean|string|function(): boolean} [defaults.editable=true] Whether
this column is editable. If the value is a string, a method will the same
name will be looked up from the column instance to determine whether the
column should be editable. The method's signature must be `function
(Backgrid.Column, Backbone.Model): boolean`.
@cfg {boolean|string|function(): boolean} [defaults.renderable=true]
Whether this column is renderable. If the value is a string, a method will
the same name will be looked up from the column instance to determine
whether the column should be renderable. The method's signature must be
`function (Backrid.Column, Backbone.Model): boolean`.
@cfg {Backgrid.CellFormatter | Object | string} [defaults.formatter] The
formatter to use to convert between raw model values and user input.
@cfg {"toggle"|"cycle"} [defaults.sortType="cycle"] Whether sorting will
toggle between ascending and descending order, or cycle between insertion
order, ascending and descending order.
@cfg {(function(Backbone.Model, string): *) | string} [defaults.sortValue]
The function to use to extract a value from the model for comparison during
sorting. If this value is a string, a method with the same name will be
looked up from the column instance.
@cfg {"ascending"|"descending"|null} [defaults.direction=null] The initial
sorting direction for this column. The default is ordered by
Backbone.Model.cid, which usually means the collection is ordered by
insertion order.
*/
defaults: {
name: undefined,
label: undefined,
sortable: true,
editable: true,
renderable: true,
formatter: undefined,
sortType: "cycle",
sortValue: undefined,
direction: null,
cell: undefined,
headerCell: undefined
},
/**
Initializes this Column instance.
@param {Object} attrs
@param {string} attrs.name The model attribute this column is responsible
for.
@param {string|Backgrid.Cell} attrs.cell The cell type to use to render
this column.
@param {string} [attrs.label]
@param {string|Backgrid.HeaderCell} [attrs.headerCell]
@param {boolean|string|function(): boolean} [attrs.sortable=true]
@param {boolean|string|function(): boolean} [attrs.editable=true]
@param {boolean|string|function(): boolean} [attrs.renderable=true]
@param {Backgrid.CellFormatter | Object | string} [attrs.formatter]
@param {"toggle"|"cycle"} [attrs.sortType="cycle"]
@param {(function(Backbone.Model, string): *) | string} [attrs.sortValue]
@throws {TypeError} If attrs.cell or attrs.options are not supplied.
@throws {ReferenceError} If formatter is a string but a formatter class of
said name cannot be found in the Backgrid module.
See:
- Backgrid.Column.defaults
- Backgrid.Cell
- Backgrid.CellFormatter
*/
initialize: function () {
Eif (!this.has("label")) {
this.set({ label: this.get("name") }, { silent: true });
}
var headerCell = Backgrid.resolveNameToClass(this.get("headerCell"), "HeaderCell");
var cell = Backgrid.resolveNameToClass(this.get("cell"), "Cell");
this.set({cell: cell, headerCell: headerCell}, { silent: true });
},
/**
Returns an appropriate value extraction function from a model for sorting.
If the column model contains an attribute `sortValue`, if it is a string, a
method from the column instance identifified by the `sortValue` string is
returned. If it is a function, it it returned as is. If `sortValue` isn't
found from the column model's attributes, a default value extraction
function is returned which will compare according to the natural order of
the value's type.
@return {function(Backbone.Model, string): *}
*/
sortValue: function () {
var sortValue = this.get("sortValue");
if (_.isString(sortValue)) return this[sortValue];
else if (_.isFunction(sortValue)) return sortValue;
return function (model, colName) {
return model.get(colName);
};
}
/**
@member Backgrid.Column
@protected
@method sortable
@return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
*/
/**
@member Backgrid.Column
@protected
@method editable
@return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
*/
/**
@member Backgrid.Column
@protected
@method renderable
@return {function(Backgrid.Column, Backbone.Model): boolean | boolean}
*/
});
_.each(["sortable", "renderable", "editable"], function (key) {
Column.prototype[key] = function () {
var value = this.get(key);
if (_.isString(value)) return this[value];
else if (_.isFunction(value)) return value;
return !!value;
};
});
/**
A Backbone collection of Column instances.
@class Backgrid.Columns
@extends Backbone.Collection
*/
var Columns = Backgrid.Columns = Backbone.Collection.extend({
/**
@property {Backgrid.Column} model
*/
model: Column
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
Row is a simple container view that takes a model instance and a list of
column metadata describing how each of the model's attribute is to be
rendered, and apply the appropriate cell to each attribute.
@class Backgrid.Row
@extends Backbone.View
*/
var Row = Backgrid.Row = Backbone.View.extend({
/** @property */
tagName: "tr",
/**
Initializes a row view instance.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
@param {Backbone.Model} options.model The model instance to render.
@throws {TypeError} If options.columns or options.model is undefined.
*/
initialize: function (options) {
var columns = this.columns = options.columns;
if (!(columns instanceof Backbone.Collection)) {
columns = this.columns = new Columns(columns);
}
var cells = this.cells = [];
for (var i = 0; i < columns.length; i++) {
cells.push(this.makeCell(columns.at(i), options));
}
this.listenTo(columns, "add", function (column, columns) {
var i = columns.indexOf(column);
var cell = this.makeCell(column, options);
cells.splice(i, 0, cell);
var $el = this.$el;
Iif (i === 0) {
$el.prepend(cell.render().$el);
}
else Eif (i === columns.length - 1) {
$el.append(cell.render().$el);
}
else {
$el.children().eq(i).before(cell.render().$el);
}
});
this.listenTo(columns, "remove", function (column, columns, opts) {
cells[opts.index].remove();
cells.splice(opts.index, 1);
});
},
/**
Factory method for making a cell. Used by #initialize internally. Override
this to provide an appropriate cell instance for a custom Row subclass.
@protected
@param {Backgrid.Column} column
@param {Object} options The options passed to #initialize.
@return {Backgrid.Cell}
*/
makeCell: function (column) {
return new (column.get("cell"))({
column: column,
model: this.model
});
},
/**
Renders a row of cells for this row's model.
*/
render: function () {
this.$el.empty();
var fragment = document.createDocumentFragment();
for (var i = 0; i < this.cells.length; i++) {
fragment.appendChild(this.cells[i].render().el);
}
this.el.appendChild(fragment);
this.delegateEvents();
return this;
},
/**
Clean up this row and its cells.
@chainable
*/
remove: function () {
for (var i = 0; i < this.cells.length; i++) {
var cell = this.cells[i];
cell.remove.apply(cell, arguments);
}
return Backbone.View.prototype.remove.apply(this, arguments);
}
});
/**
EmptyRow is a simple container view that takes a list of column and render a
row with a single column.
@class Backgrid.EmptyRow
@extends Backbone.View
*/
var EmptyRow = Backgrid.EmptyRow = Backbone.View.extend({
/** @property */
tagName: "tr",
/** @property {string|function(): string} */
emptyText: null,
/**
Initializer.
@param {Object} options
@param {string|function(): string} options.emptyText
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
*/
initialize: function (options) {
this.emptyText = options.emptyText;
this.columns = options.columns;
},
/**
Renders an empty row.
*/
render: function () {
this.$el.empty();
var td = document.createElement("td");
td.setAttribute("colspan", this.columns.length);
td.appendChild(document.createTextNode(_.result(this, "emptyText")));
this.el.className = "empty";
this.el.appendChild(td);
return this;
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
HeaderCell is a special cell class that renders a column header cell. If the
column is sortable, a sorter is also rendered and will trigger a table
refresh after sorting.
@class Backgrid.HeaderCell
@extends Backbone.View
*/
var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
/** @property */
tagName: "th",
/** @property */
events: {
"click a": "onClick"
},
/**
Initializer.
@param {Object} options
@param {Backgrid.Column|Object} options.column
@throws {TypeError} If options.column or options.collection is undefined.
*/
initialize: function (options) {
this.column = options.column;
if (!(this.column instanceof Column)) {
this.column = new Column(this.column);
}
var column = this.column, collection = this.collection, $el = this.$el;
this.listenTo(column, "change:editable change:sortable change:renderable",
function (column) {
var changed = column.changedAttributes();
for (var key in changed) {
Eif (changed.hasOwnProperty(key)) {
$el.toggleClass(key, changed[key]);
}
}
});
this.listenTo(column, "change:direction", this.setCellDirection);
this.listenTo(column, "change:name change:label", this.render);
if (Backgrid.callByNeed(column.editable(), column, collection)) $el.addClass("editable");
if (Backgrid.callByNeed(column.sortable(), column, collection)) $el.addClass("sortable");
if (Backgrid.callByNeed(column.renderable(), column, collection)) $el.addClass("renderable");
this.listenTo(collection.fullCollection || collection, "sort", this.removeCellDirection);
},
/**
Event handler for the collection's `sort` event. Removes all the CSS
direction classes.
*/
removeCellDirection: function () {
this.$el.removeClass("ascending").removeClass("descending");
this.column.set("direction", null);
},
/**
Event handler for the column's `change:direction` event. If this
HeaderCell's column is being sorted on, it applies the direction given as a
CSS class to the header cell. Removes all the CSS direction classes
otherwise.
*/
setCellDirection: function (column, direction) {
this.$el.removeClass("ascending").removeClass("descending");
Eif (column.cid == this.column.cid) this.$el.addClass(direction);
},
/**
Event handler for the `click` event on the cell's anchor. If the column is
sortable, clicking on the anchor will cycle through 3 sorting orderings -
`ascending`, `descending`, and default.
*/
onClick: function (e) {
e.preventDefault();
var column = this.column;
var collection = this.collection;
var event = "backgrid:sort";
function cycleSort(header, col) {
if (column.get("direction") === "ascending") collection.trigger(event, col, "descending");
else if (column.get("direction") === "descending") collection.trigger(event, col, null);
else collection.trigger(event, col, "ascending");
}
function toggleSort(header, col) {
if (column.get("direction") === "ascending") collection.trigger(event, col, "descending");
else collection.trigger(event, col, "ascending");
}
var sortable = Backgrid.callByNeed(column.sortable(), column, this.collection);
Eif (sortable) {
var sortType = column.get("sortType");
if (sortType === "toggle") toggleSort(this, column);
else cycleSort(this, column);
}
},
/**
Renders a header cell with a sorter, a label, and a class name for this
column.
*/
render: function () {
this.$el.empty();
var column = this.column;
var sortable = Backgrid.callByNeed(column.sortable(), column, this.collection);
var label;
if(sortable){
label = $("<a>").text(column.get("label")).append("<b class='sort-caret'></b>");
} else {
label = document.createTextNode(column.get("label"));
}
this.$el.append(label);
this.$el.addClass(column.get("name"));
this.$el.addClass(column.get("direction"));
this.delegateEvents();
return this;
}
});
/**
HeaderRow is a controller for a row of header cells.
@class Backgrid.HeaderRow
@extends Backgrid.Row
*/
var HeaderRow = Backgrid.HeaderRow = Backgrid.Row.extend({
requiredOptions: ["columns", "collection"],
/**
Initializer.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
@param {Backgrid.HeaderCell} [options.headerCell] Customized default
HeaderCell for all the columns. Supply a HeaderCell class or instance to a
the `headerCell` key in a column definition for column-specific header
rendering.
@throws {TypeError} If options.columns or options.collection is undefined.
*/
initialize: function () {
Backgrid.Row.prototype.initialize.apply(this, arguments);
},
makeCell: function (column, options) {
var headerCell = column.get("headerCell") || options.headerCell || HeaderCell;
headerCell = new headerCell({
column: column,
collection: this.collection
});
return headerCell;
}
});
/**
Header is a special structural view class that renders a table head with a
single row of header cells.
@class Backgrid.Header
@extends Backbone.View
*/
var Header = Backgrid.Header = Backbone.View.extend({
/** @property */
tagName: "thead",
/**
Initializer. Initializes this table head view to contain a single header
row view.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
@param {Backbone.Model} options.model The model instance to render.
@throws {TypeError} If options.columns or options.model is undefined.
*/
initialize: function (options) {
this.columns = options.columns;
if (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Columns(this.columns);
}
this.row = new Backgrid.HeaderRow({
columns: this.columns,
collection: this.collection
});
},
/**
Renders this table head with a single row of header cells.
*/
render: function () {
this.$el.append(this.row.render().$el);
this.delegateEvents();
return this;
},
/**
Clean up this header and its row.
@chainable
*/
remove: function () {
this.row.remove.apply(this.row, arguments);
return Backbone.View.prototype.remove.apply(this, arguments);
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
Body is the table body which contains the rows inside a table. Body is
responsible for refreshing the rows after sorting, insertion and removal.
@class Backgrid.Body
@extends Backbone.View
*/
var Body = Backgrid.Body = Backbone.View.extend({
/** @property */
tagName: "tbody",
/**
Initializer.
@param {Object} options
@param {Backbone.Collection} options.collection
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
Column metadata.
@param {Backgrid.Row} [options.row=Backgrid.Row] The Row class to use.
@param {string|function(): string} [options.emptyText] The text to display in the empty row.
@throws {TypeError} If options.columns or options.collection is undefined.
See Backgrid.Row.
*/
initialize: function (options) {
this.columns = options.columns;
if (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Columns(this.columns);
}
this.row = options.row || Row;
this.rows = this.collection.map(function (model) {
var row = new this.row({
columns: this.columns,
model: model
});
return row;
}, this);
this.emptyText = options.emptyText;
this._unshiftEmptyRowMayBe();
var collection = this.collection;
this.listenTo(collection, "add", this.insertRow);
this.listenTo(collection, "remove", this.removeRow);
this.listenTo(collection, "sort", this.refresh);
this.listenTo(collection, "reset", this.refresh);
this.listenTo(collection, "backgrid:sort", this.sort);
this.listenTo(collection, "backgrid:edited", this.moveToNextCell);
},
_unshiftEmptyRowMayBe: function () {
if (this.rows.length === 0 && this.emptyText != null) {
this.rows.unshift(new EmptyRow({
emptyText: this.emptyText,
columns: this.columns
}));
}
},
/**
This method can be called either directly or as a callback to a
[Backbone.Collecton#add](http://backbonejs.org/#Collection-add) event.
When called directly, it accepts a model or an array of models and an
option hash just like
[Backbone.Collection#add](http://backbonejs.org/#Collection-add) and
delegates to it. Once the model is added, a new row is inserted into the
body and automatically rendered.
When called as a callback of an `add` event, splices a new row into the
body and renders it.
@param {Backbone.Model} model The model to render as a row.
@param {Backbone.Collection} collection When called directly, this
parameter is actually the options to
[Backbone.Collection#add](http://backbonejs.org/#Collection-add).
@param {Object} options When called directly, this must be null.
See:
- [Backbone.Collection#add](http://backbonejs.org/#Collection-add)
*/
insertRow: function (model, collection, options) {
if (this.rows[0] instanceof EmptyRow) this.rows.pop().remove();
// insertRow() is called directly
if (!(collection instanceof Backbone.Collection) && !options) {
this.collection.add(model, (options = collection));
return;
}
var row = new this.row({
columns: this.columns,
model: model
});
var index = collection.indexOf(model);
this.rows.splice(index, 0, row);
var $el = this.$el;
var $children = $el.children();
var $rowEl = row.render().$el;
if (index >= $children.length) {
$el.append($rowEl);
}
else {
$children.eq(index).before($rowEl);
}
return this;
},
/**
The method can be called either directly or as a callback to a
[Backbone.Collection#remove](http://backbonejs.org/#Collection-remove)
event.
When called directly, it accepts a model or an array of models and an
option hash just like
[Backbone.Collection#remove](http://backbonejs.org/#Collection-remove) and
delegates to it. Once the model is removed, a corresponding row is removed
from the body.
When called as a callback of a `remove` event, splices into the rows and
removes the row responsible for rendering the model.
@param {Backbone.Model} model The model to remove from the body.
@param {Backbone.Collection} collection When called directly, this
parameter is actually the options to
[Backbone.Collection#remove](http://backbonejs.org/#Collection-remove).
@param {Object} options When called directly, this must be null.
See:
- [Backbone.Collection#remove](http://backbonejs.org/#Collection-remove)
*/
removeRow: function (model, collection, options) {
// removeRow() is called directly
if (!options) {
this.collection.remove(model, (options = collection));
this._unshiftEmptyRowMayBe();
return;
}
Eif (_.isUndefined(options.render) || options.render) {
this.rows[options.index].remove();
}
this.rows.splice(options.index, 1);
this._unshiftEmptyRowMayBe();
return this;
},
/**
Reinitialize all the rows inside the body and re-render them. Triggers a
Backbone `backgrid:refresh` event from the collection along with the body
instance as its sole parameter when done.
*/
refresh: function () {
for (var i = 0; i < this.rows.length; i++) {
this.rows[i].remove();
}
this.rows = this.collection.map(function (model) {
var row = new this.row({
columns: this.columns,
model: model
});
return row;
}, this);
this._unshiftEmptyRowMayBe();
this.render();
this.collection.trigger("backgrid:refresh", this);
return this;
},
/**
Renders all the rows inside this body. If the collection is empty and
`options.emptyText` is defined and not null in the constructor, an empty
row is rendered, otherwise no row is rendered.
*/
render: function () {
this.$el.empty();
var fragment = document.createDocumentFragment();
for (var i = 0; i < this.rows.length; i++) {
var row = this.rows[i];
fragment.appendChild(row.render().el);
}
this.el.appendChild(fragment);
this.delegateEvents();
return this;
},
/**
Clean up this body and it's rows.
@chainable
*/
remove: function () {
for (var i = 0; i < this.rows.length; i++) {
var row = this.rows[i];
row.remove.apply(row, arguments);
}
return Backbone.View.prototype.remove.apply(this, arguments);
},
/**
If the underlying collection is a Backbone.PageableCollection in
server-mode or infinite-mode, a page of models is fetched after sorting is
done on the server.
If the underlying collection is a Backbone.PageableCollection in
client-mode, or any
[Backbone.Collection](http://backbonejs.org/#Collection) instance, sorting
is done on the client side. If the collection is an instance of a
Backbone.PageableCollection, sorting will be done globally on all the pages
and the current page will then be returned.
Triggers a Backbone `backgrid:sorted` event from the collection when done
with the column, direction and a reference to the collection.
@param {Backgrid.Column} column
@param {null|"ascending"|"descending"} direction
See [Backbone.Collection#comparator](http://backbonejs.org/#Collection-comparator)
*/
sort: function (column, direction) {
if (!_.contains(["ascending", "descending", null], direction)) {
throw new RangeError('direction must be one of "ascending", "descending" or `null`');
}
if (_.isString(column)) column = this.columns.findWhere({name: column});
var collection = this.collection;
var order;
if (direction === "ascending") order = -1;
else if (direction === "descending") order = 1;
else order = null;
var comparator = this.makeComparator(column.get("name"), order,
order ?
column.sortValue() :
function (model) {
return model.cid.replace('c', '') * 1;
});
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection) {
collection.setSorting(order && column.get("name"), order,
{sortValue: column.sortValue()});
if (collection.fullCollection) {
// If order is null, pageable will remove the comparator on both sides,
// in this case the default insertion order comparator needs to be
// attached to get back to the order before sorting.
if (collection.fullCollection.comparator == null) {
collection.fullCollection.comparator = comparator;
}
collection.fullCollection.sort();
collection.trigger("backgrid:sorted", column, direction, collection);
}
else collection.fetch({reset: true, success: function () {
collection.trigger("backgrid:sorted", column, direction, collection);
}});
}
else {
collection.comparator = comparator;
collection.sort();
collection.trigger("backgrid:sorted", column, direction, collection);
}
column.set("direction", direction);
return this;
},
makeComparator: function (attr, order, func) {
return function (left, right) {
// extract the values from the models
var l = func(left, attr), r = func(right, attr), t;
// if descending order, swap left and right
if (order === 1) t = l, l = r, r = t;
// compare as usual
Iif (l === r) return 0;
else if (l < r) return -1;
return 1;
};
},
/**
Moves focus to the next renderable and editable cell and return the
currently editing cell to display mode.
Triggers a `backgrid:next` event on the model with the indices of the row
and column the user *intended* to move to, and whether the intended move
was going to go out of bounds. Note that *out of bound* always means an
attempt to go past the end of the last row.
@param {Backbone.Model} model The originating model
@param {Backgrid.Column} column The originating model column
@param {Backgrid.Command} command The Command object constructed from a DOM
event
*/
moveToNextCell: function (model, column, command) {
var i = this.collection.indexOf(model);
var j = this.columns.indexOf(column);
var cell, renderable, editable, m, n;
this.rows[i].cells[j].exitEditMode();
if (command.moveUp() || command.moveDown() || command.moveLeft() ||
command.moveRight() || command.save()) {
var l = this.columns.length;
var maxOffset = l * this.collection.length;
if (command.moveUp() || command.moveDown()) {
m = i + (command.moveUp() ? -1 : 1);
var row = this.rows[m];
Eif (row) {
cell = row.cells[j];
Eif (Backgrid.callByNeed(cell.column.editable(), cell.column, model)) {
cell.enterEditMode();
model.trigger("backgrid:next", m, j, false);
}
}
else model.trigger("backgrid:next", m, j, true);
}
else if (command.moveLeft() || command.moveRight()) {
var right = command.moveRight();
for (var offset = i * l + j + (right ? 1 : -1);
offset >= 0 && offset < maxOffset;
right ? offset++ : offset--) {
m = ~~(offset / l);
n = offset - m * l;
cell = this.rows[m].cells[n];
renderable = Backgrid.callByNeed(cell.column.renderable(), cell.column, cell.model);
editable = Backgrid.callByNeed(cell.column.editable(), cell.column, model);
if (renderable && editable) {
cell.enterEditMode();
model.trigger("backgrid:next", m, n, false);
break;
}
}
Iif (offset == maxOffset) {
model.trigger("backgrid:next", ~~(offset / l), offset - m * l, true);
}
}
}
return this;
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
A Footer is a generic class that only defines a default tag `tfoot` and
number of required parameters in the initializer.
@abstract
@class Backgrid.Footer
@extends Backbone.View
*/
var Footer = Backgrid.Footer = Backbone.View.extend({
/** @property */
tagName: "tfoot",
/**
Initializer.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
Column metadata.
@param {Backbone.Collection} options.collection
@throws {TypeError} If options.columns or options.collection is undefined.
*/
initialize: function (options) {
this.columns = options.columns;
Iif (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Backgrid.Columns(this.columns);
}
}
});
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
Grid represents a data grid that has a header, body and an optional footer.
By default, a Grid treats each model in a collection as a row, and each
attribute in a model as a column. To render a grid you must provide a list of
column metadata and a collection to the Grid constructor. Just like any
Backbone.View class, the grid is rendered as a DOM node fragment when you
call render().
var grid = Backgrid.Grid({
columns: [{ name: "id", label: "ID", type: "string" },
// ...
],
collections: books
});
$("#table-container").append(grid.render().el);
Optionally, if you want to customize the rendering of the grid's header and
footer, you may choose to extend Backgrid.Header and Backgrid.Footer, and
then supply that class or an instance of that class to the Grid constructor.
See the documentation for Header and Footer for further details.
var grid = Backgrid.Grid({
columns: [{ name: "id", label: "ID", type: "string" }],
collections: books,
header: Backgrid.Header.extend({
//...
}),
footer: Backgrid.Paginator
});
Finally, if you want to override how the rows are rendered in the table body,
you can supply a Body subclass as the `body` attribute that uses a different
Row class.
@class Backgrid.Grid
@extends Backbone.View
See:
- Backgrid.Column
- Backgrid.Header
- Backgrid.Body
- Backgrid.Row
- Backgrid.Footer
*/
var Grid = Backgrid.Grid = Backbone.View.extend({
/** @property */
tagName: "table",
/** @property */
className: "backgrid",
/** @property */
header: Header,
/** @property */
body: Body,
/** @property */
footer: null,
/**
Initializes a Grid instance.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Columns>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
@param {Backbone.Collection} options.collection The collection of tabular model data to display.
@param {Backgrid.Header} [options.header=Backgrid.Header] An optional Header class to override the default.
@param {Backgrid.Body} [options.body=Backgrid.Body] An optional Body class to override the default.
@param {Backgrid.Row} [options.row=Backgrid.Row] An optional Row class to override the default.
@param {Backgrid.Footer} [options.footer=Backgrid.Footer] An optional Footer class.
*/
initialize: function (options) {
// Convert the list of column objects here first so the subviews don't have
// to.
Eif (!(options.columns instanceof Backbone.Collection)) {
options.columns = new Columns(options.columns);
}
this.columns = options.columns;
var filteredOptions = _.omit(options, ["el", "id", "attributes",
"className", "tagName", "events"]);
// must construct body first so it listens to backgrid:sort first
this.body = options.body || this.body;
this.body = new this.body(filteredOptions);
this.header = options.header || this.header;
Eif (this.header) {
this.header = new this.header(filteredOptions);
}
this.footer = options.footer || this.footer;
Eif (this.footer) {
this.footer = new this.footer(filteredOptions);
}
this.listenTo(this.columns, "reset", function () {
Eif (this.header) {
this.header = new (this.header.remove().constructor)(filteredOptions);
}
this.body = new (this.body.remove().constructor)(filteredOptions);
Eif (this.footer) {
this.footer = new (this.footer.remove().constructor)(filteredOptions);
}
this.render();
});
},
/**
Delegates to Backgrid.Body#insertRow.
*/
insertRow: function () {
this.body.insertRow.apply(this.body, arguments);
return this;
},
/**
Delegates to Backgrid.Body#removeRow.
*/
removeRow: function () {
this.body.removeRow.apply(this.body, arguments);
return this;
},
/**
Delegates to Backgrid.Columns#add for adding a column. Subviews can listen
to the `add` event from their internal `columns` if rerendering needs to
happen.
@param {Object} [options] Options for `Backgrid.Columns#add`.
*/
insertColumn: function () {
this.columns.add.apply(this.columns, arguments);
return this;
},
/**
Delegates to Backgrid.Columns#remove for removing a column. Subviews can
listen to the `remove` event from the internal `columns` if rerendering
needs to happen.
@param {Object} [options] Options for `Backgrid.Columns#remove`.
*/
removeColumn: function () {
this.columns.remove.apply(this.columns, arguments);
return this;
},
/**
Delegates to Backgrid.Body#sort.
*/
sort: function () {
this.body.sort.apply(this.body, arguments);
return this;
},
/**
Renders the grid's header, then footer, then finally the body. Triggers a
Backbone `backgrid:rendered` event along with a reference to the grid when
the it has successfully been rendered.
*/
render: function () {
this.$el.empty();
Eif (this.header) {
this.$el.append(this.header.render().$el);
}
Eif (this.footer) {
this.$el.append(this.footer.render().$el);
}
this.$el.append(this.body.render().$el);
this.delegateEvents();
this.trigger("backgrid:rendered", this);
return this;
},
/**
Clean up this grid and its subviews.
@chainable
*/
remove: function () {
this.header && this.header.remove.apply(this.header, arguments);
this.body.remove.apply(this.body, arguments);
this.footer && this.footer.remove.apply(this.footer, arguments);
return Backbone.View.prototype.remove.apply(this, arguments);
}
});
return Backgrid;
})); |