summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/content/calendar-multiday-view.js
blob: 041c8ae3355f6ac31bcf8f89c15995803af98a2b (plain)
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
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/* import-globals-from widgets/mouseoverPreviews.js */
/* import-globals-from calendar-ui-utils.js */

/* global calendarNavigationBar, currentView, gCurrentMode, getSelectedCalendar,
   invokeEventDragSession, MozElements, MozXULElement, timeIndicator */

// Wrap in a block to prevent leaking to window scope.
{
  const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
  const MINUTES_IN_DAY = 24 * 60;

  /**
   * Get the nearest or next snap point for the given minute. The set of snap
   * points is given by `n * snapInterval`, where `n` is some integer.
   *
   * @param {number} minute - The minute to snap.
   * @param {number} snapInterval - The integer number of minutes between snap
   *   points.
   * @param {"nearest","forward","backward"} [direction="nearest"] - Where to
   *   find the snap point. "nearest" will return the closest snap point,
   *   "forward" will return the closest snap point that is greater (and not
   *   equal), and "backward" will return the closest snap point that is lower
   *   (and not equal).
   *
   * @returns {number} - The nearest snap point.
   */
  function snapMinute(minute, snapInterval, direction = "nearest") {
    switch (direction) {
      case "forward":
        return Math.floor((minute + snapInterval) / snapInterval) * snapInterval;
      case "backward":
        return Math.ceil((minute - snapInterval) / snapInterval) * snapInterval;
      case "nearest":
        return Math.round(minute / snapInterval) * snapInterval;
      default:
        throw new RangeError(`"${direction}" is not one of the allowed values for the direction`);
    }
  }

  /**
   * Determine whether the given event item can be edited by the user.
   *
   * @param {calItemBase} eventItem - The event item.
   *
   * @returns {boolean} - Whether the given event can be edited by the user.
   */
  function canEditEventItem(eventItem) {
    return (
      cal.acl.isCalendarWritable(eventItem.calendar) &&
      cal.acl.userCanModifyItem(eventItem) &&
      !(
        eventItem.calendar instanceof Ci.calISchedulingSupport &&
        eventItem.calendar.isInvitation(eventItem)
      ) &&
      eventItem.calendar.getProperty("capabilities.events.supported") !== false
    );
  }

  /**
   * The MozCalendarEventColumn widget used for displaying event boxes in one column per day.
   * It is used to make the week view layout in the calendar. It manages the layout of the
   * events given via add/deleteEvent.
   */
  class MozCalendarEventColumn extends MozXULElement {
    static get inheritedAttributes() {
      return {
        ".multiday-events-list": "context",
        ".timeIndicator": "orient",
      };
    }

    /**
     * The background hour box elements this event column owns, ordered and
     * indexed by their starting hour.
     *
     * @type {Element[]}
     */
    hourBoxes = [];

    /**
     * The date of the day this event column represents.
     *
     * @type {calIDateTime}
     */
    date;

    connectedCallback() {
      if (this.delayConnectedCallback() || this.hasChildNodes()) {
        return;
      }
      this.appendChild(
        MozXULElement.parseXULToFragment(`
          <stack class="multiday-column-box-stack" flex="1">
            <html:div class="multiday-hour-box-container"></html:div>
            <html:ol class="multiday-events-list"></html:ol>
            <box class="timeIndicator" hidden="true"/>
            <box class="fgdragcontainer" flex="1">
              <box class="fgdragspacer">
                <spacer flex="1"/>
                <label class="fgdragbox-label fgdragbox-startlabel"/>
              </box>
              <box class="fgdragbox"/>
              <label class="fgdragbox-label fgdragbox-endlabel"/>
            </box>
          </stack>
          <calendar-event-box hidden="true"/>
        `)
      );
      this.hourBoxContainer = this.querySelector(".multiday-hour-box-container");
      for (let hour = 0; hour < 24; hour++) {
        let hourBox = document.createElement("div");
        hourBox.classList.add("multiday-hour-box");
        this.hourBoxContainer.appendChild(hourBox);
        this.hourBoxes.push(hourBox);
      }

      this.eventsListElement = this.querySelector(".multiday-events-list");

      this.addEventListener("dblclick", event => {
        if (event.button != 0) {
          return;
        }

        if (this.calendarView.controller) {
          event.stopPropagation();
          this.calendarView.controller.createNewEvent(null, this.getMouseDateTime(event), null);
        }
      });

      this.addEventListener("click", event => {
        if (event.button != 0 || event.ctrlKey || event.metaKey) {
          return;
        }
        this.calendarView.setSelectedItems([]);
        this.focus();
      });

      // Mouse down handler, in empty event column regions.  Starts sweeping out a new event.
      this.addEventListener("mousedown", event => {
        // Select this column.
        this.calendarView.selectedDay = this.date;

        // If the selected calendar is readOnly, we don't want any sweeping.
        let calendar = getSelectedCalendar();
        if (
          !cal.acl.isCalendarWritable(calendar) ||
          calendar.getProperty("capabilities.events.supported") === false
        ) {
          return;
        }

        if (event.button == 2) {
          // Set a selected datetime for the context menu.
          this.calendarView.selectedDateTime = this.getMouseDateTime(event);
          return;
        }
        // Only start sweeping out an event if the left button was clicked.
        if (event.button != 0) {
          return;
        }

        this.mDragState = {
          origColumn: this,
          dragType: "new",
          mouseMinuteOffset: 0,
          offset: null,
          shadows: null,
          limitStartMin: null,
          limitEndMin: null,
          jumpedColumns: 0,
        };

        // Snap interval: 15 minutes or 1 minute if modifier key is pressed.
        this.mDragState.origMin = snapMinute(
          this.getMouseMinute(event),
          event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey ? 1 : 15
        );

        if (this.getAttribute("orient") == "vertical") {
          this.mDragState.origLoc = event.clientY;
          this.mDragState.limitEndMin = this.mDragState.origMin;
          this.mDragState.limitStartMin = this.mDragState.origMin;
          this.fgboxes.dragspacer.setAttribute(
            "height",
            this.mDragState.origMin * this.pixelsPerMinute
          );
        } else {
          this.mDragState.origLoc = event.clientX;
          this.fgboxes.dragspacer.setAttribute(
            "width",
            this.mDragState.origMin * this.pixelsPerMinute
          );
        }

        document.calendarEventColumnDragging = this;

        window.addEventListener("mousemove", this.onEventSweepMouseMove);
        window.addEventListener("mouseup", this.onEventSweepMouseUp);
        window.addEventListener("keypress", this.onEventSweepKeypress);
      });

      /**
       * An internal collection of data for events.
       *
       * @typedef {object} EventData
       * @property {calItemBase} eventItem - The event item.
       * @property {Element} element - The displayed event in this column.
       * @property {boolean} selected - Whether the event is selected.
       * @property {boolean} needsUpdate - True whilst the eventItem has changed
       *   and we are still pending updating the 'element' property.
       */
      /**
       * Event data for all the events displayed in this column.
       *
       * @type {Map<string, EventData} - A map from an event item's hashId to
       *   its data.
       */
      this.eventDataMap = new Map();

      this.mCalendarView = null;

      this.mDragState = null;

      this.mLayoutBatchCount = 0;

      // Since we'll often be getting many events in rapid succession, this
      // timer helps ensure that we don't re-compute the event map too many
      // times in a short interval, and therefore improves performance.
      this.mEventMapTimeout = null;

      // Whether the next added event should be created in the editing state.
      this.newEventNeedsEditing = false;
      // The hashId of the event we should set to editing in the next relayout.
      this.eventToEdit = null;

      this.mSelected = false;

      this.mFgboxes = null;

      this.initializeAttributeInheritance();
    }

    /**
     * The number of pixels that a one minute duration should occupy in the
     * column.
     *
     * @type {number}
     */
    set pixelsPerMinute(val) {
      this._pixelsPerMinute = val;
      this.relayout();
    }

    get pixelsPerMinute() {
      return this._pixelsPerMinute;
    }

    set calendarView(val) {
      this.mCalendarView = val;
    }

    get calendarView() {
      return this.mCalendarView;
    }

    get fgboxes() {
      if (this.mFgboxes == null) {
        this.mFgboxes = {
          box: this.querySelector(".fgdragcontainer"),
          dragbox: this.querySelector(".fgdragbox"),
          dragspacer: this.querySelector(".fgdragspacer"),
          startlabel: this.querySelector(".fgdragbox-startlabel"),
          endlabel: this.querySelector(".fgdragbox-endlabel"),
        };
      }
      return this.mFgboxes;
    }

    get timeIndicatorBox() {
      return this.querySelector(".timeIndicator");
    }

    get events() {
      return this.methods;
    }

    /**
     * Set whether the calendar-event-box element for the given event item
     * should be displayed as selected or unselected.
     *
     * @param {calItemBase} eventItem - The event item.
     * @param {boolean} select - Whether to show the corresponding event element
     *   as selected.
     */
    selectEvent(eventItem, select) {
      let data = this.eventDataMap.get(eventItem.hashId);
      if (!data) {
        return;
      }
      data.selected = select;
      if (data.element) {
        // There is a small window between an event item being added and it
        // actually having an element. If it doesn't have an element yet, it
        // will be selected on its creation instead.
        data.element.selected = select;
      }
    }

    /**
     * Return the displayed calendar-event-box element for the given event item.
     *
     * @param {calItemBase} eventItem - The event item.
     *
     * @returns {Element} - The corresponding element, or undefined if none.
     */
    findElementForEventItem(eventItem) {
      return this.eventDataMap.get(eventItem.hashId)?.element;
    }

    /**
     * Return all the event items that are displayed in this columns.
     *
     * @returns {calItemBase[]} - An array of all the displayed event items.
     */
    getAllEventItems() {
      return Array.from(this.eventDataMap.values(), data => data.eventItem);
    }

    startLayoutBatchChange() {
      this.mLayoutBatchCount++;
    }

    endLayoutBatchChange() {
      this.mLayoutBatchCount--;
      if (this.mLayoutBatchCount == 0) {
        this.relayout();
      }
    }

    setAttribute(attr, val) {
      // this should be done using lookupMethod(), see bug 286629
      let ret = super.setAttribute(attr, val);

      if (attr == "orient" && this.getAttribute("orient") != val) {
        this.relayout();
      }

      return ret;
    }

    /**
     * Create or update a displayed calendar-event-box element for the given
     * event item.
     *
     * @param {calItemBase} eventItem - The event item to create or update an
     *   element for.
     */
    addEvent(eventItem) {
      let eventData = this.eventDataMap.get(eventItem.hashId);
      if (!eventData) {
        // New event with no pre-existing data.
        eventData = { selected: false };
        this.eventDataMap.set(eventItem.hashId, eventData);
      }
      eventData.needsUpdate = true;

      // We set the eventItem property here, the rest will be updated in
      // relayout().
      // NOTE: If we already have an event with the given hashId, then the
      // eventData.element will still refer to the previous display of the event
      // until we call relayout().
      eventData.eventItem = eventItem;

      if (this.mEventMapTimeout) {
        clearTimeout(this.mEventMapTimeout);
      }

      if (this.newEventNeedsEditing) {
        this.eventToEdit = eventItem.hashId;
        this.newEventNeedsEditing = false;
      }

      this.mEventMapTimeout = setTimeout(() => this.relayout(), 5);
    }

    /**
     * Remove the displayed calendar-event-box element for the given event item
     * from this column
     *
     * @param {calItemBase} eventItem - The event item to remove the element of.
     */
    deleteEvent(eventItem) {
      if (this.eventDataMap.delete(eventItem.hashId)) {
        this.relayout();
      }
    }

    _clearElements() {
      while (this.eventsListElement.hasChildNodes()) {
        this.eventsListElement.lastChild.remove();
      }
    }

    /**
     * Clear the column of all events.
     */
    clear() {
      this._clearElements();
      this.eventDataMap.clear();
    }

    relayout() {
      if (this.mLayoutBatchCount > 0) {
        return;
      }
      this._clearElements();

      let orient = this.getAttribute("orient");

      let configBox = this.querySelector("calendar-event-box");
      configBox.removeAttribute("hidden");
      let minSize = configBox.getOptimalMinSize(orient);
      configBox.setAttribute("hidden", "true");
      // The minimum event duration in minutes that would give at least the
      // desired minSize in the layout.
      let minDuration = Math.ceil(minSize / this.pixelsPerMinute);

      let dayPx = `${MINUTES_IN_DAY * this.pixelsPerMinute}px`;
      if (orient == "vertical") {
        this.hourBoxContainer.style.height = dayPx;
        this.hourBoxContainer.style.width = null;
      } else {
        this.hourBoxContainer.style.width = dayPx;
        this.hourBoxContainer.style.height = null;
      }

      // 'fgbox' is used for dragging events.
      this.fgboxes.box.setAttribute("orient", orient);
      this.querySelector(".fgdragspacer").setAttribute("orient", orient);

      for (let eventData of this.eventDataMap.values()) {
        if (!eventData.needsUpdate) {
          continue;
        }
        eventData.needsUpdate = false;
        // Create a new wrapper.
        let eventElement = document.createElement("li");
        eventElement.classList.add("multiday-event-listitem");
        // Set up the event box.
        let eventBox = document.createXULElement("calendar-event-box");
        eventElement.appendChild(eventBox);

        // Trigger connectedCallback
        this.eventsListElement.appendChild(eventElement);

        eventBox.setAttribute(
          "context",
          this.getAttribute("item-context") || this.getAttribute("context")
        );

        eventBox.calendarView = this.calendarView;
        eventBox.occurrence = eventData.eventItem;
        eventBox.parentColumn = this;
        // An event item can technically be 'selected' between a call to
        // addEvent and this method (because of the setTimeout). E.g. clicking
        // the event in the unifinder tree will select the item through
        // selectEvent. If the element wasn't yet created in that method, we set
        // the selected status here as well.
        //
        // Similarly, if an event has the same hashId, we maintain its
        // selection.
        // NOTE: In this latter case we are relying on the fact that
        // eventData.element.selected is never out of sync with
        // eventData.selected.
        eventBox.selected = eventData.selected;
        eventData.element = eventBox;

        // Remove the element to be added again later.
        eventElement.remove();
      }

      let eventLayoutList = this.computeEventLayoutInfo(minDuration);

      for (let eventInfo of eventLayoutList) {
        // Note that we store the calendar-event-box in the eventInfo, so we
        // grab its parent to get the wrapper list item.
        // NOTE: This may be a newly created element or a non-updated element
        // that was removed from the eventsListElement in _clearElements. We
        // still hold a reference to it, so we can re-add it in the new ordering
        // and change its dimensions.
        let eventElement = eventInfo.element.parentNode;
        // FIXME: offset and length should be in % of parent's dimension, so we
        // can avoid pixelsPerMinute.
        let offset = `${eventInfo.start * this.pixelsPerMinute}px`;
        let length = `${(eventInfo.end - eventInfo.start) * this.pixelsPerMinute}px`;
        let secondaryOffset = `${eventInfo.secondaryOffset * 100}%`;
        let secondaryLength = `${eventInfo.secondaryLength * 100}%`;
        if (orient == "vertical") {
          eventElement.style.height = length;
          eventElement.style.width = secondaryLength;
          eventElement.style.insetBlockStart = offset;
          eventElement.style.insetInlineStart = secondaryOffset;
        } else {
          eventElement.style.width = length;
          eventElement.style.height = secondaryLength;
          eventElement.style.insetInlineStart = offset;
          eventElement.style.insetBlockStart = secondaryOffset;
        }
        this.eventsListElement.appendChild(eventElement);
      }

      let boxToEdit = this.eventDataMap.get(this.eventToEdit)?.element;
      if (boxToEdit) {
        boxToEdit.startEditing();
      }
      this.eventToEdit = null;
    }

    /**
     * Layout information for displaying an event in the calendar column. The
     * calendar column has two dimensions: a primary-dimension, in minutes,
     * that runs from the start of the day to the end of the day; and a
     * secondary-dimension which runs from 0 to 1. This object describes how
     * an event can be placed on these axes.
     *
     * @typedef {object} EventLayoutInfo
     * @property {MozCalendarEventBox} element - The displayed event.
     * @property {number} start - The number of minutes from the start of this
     *   column's day to when the event should start.
     * @property {number} end - The number of minutes from the start of this
     *   column's day to when the event ends.
     * @property {number} secondaryOffset - The position of the event on the
     *   secondary axis (between 0 and 1).
     * @property {number} secondaryLength - The length of the event on the
     *   secondary axis (between 0 and 1).
     */
    /**
     * Get an ordered list of events and their layout information. The list is
     * ordered relative to the event's layout.
     *
     * @param {number} minDuration - The minimum number of minutes that an event
     *   should be *shown* to last. This should be large enough to ensure that
     *   events are readable in the layout.
     *
     * @returns {EventLayoutInfo[]} - An ordered list of event layout
     *   information.
     */
    computeEventLayoutInfo(minDuration) {
      if (!this.eventDataMap.size) {
        return [];
      }

      function sortByStart(aEventInfo, bEventInfo) {
        // If you pass in tasks without both entry and due dates, I will
        // kill you.
        let startComparison = aEventInfo.startDate.compare(bEventInfo.startDate);
        if (startComparison == 0) {
          // If the items start at the same time, return the longer one
          // first.
          return bEventInfo.endDate.compare(aEventInfo.endDate);
        }
        return startComparison;
      }

      // Construct the ordered list of EventLayoutInfo objects that we will
      // eventually return.
      // To begin, we construct the objects with a 'startDate' and 'endDate'
      // properties, as opposed to using minutes from the start of the day
      // because we want to sort the events relative to their absolute start
      // times.
      let eventList = Array.from(this.eventDataMap.values(), eventData => {
        let element = eventData.element;
        let { startDate, endDate, startMinute, endMinute } = element.updateRelativeStartEndDates(
          this.date
        );
        // If there is no startDate, we use the element's endDate for both the
        // start and the end times. Similarly if there is no endDate. Such items
        // will automatically have the minimum duration.
        if (!startDate) {
          startDate = endDate;
          startMinute = endMinute;
        } else if (!endDate) {
          endDate = startDate;
          endMinute = startMinute;
        }
        // Any events that start or end on a different day are clipped to the
        // start/end minutes of this day instead.
        let start = Math.max(startMinute, 0);
        // NOTE: The end can overflow the end of the day due to the minDuration.
        let end = Math.max(start + minDuration, Math.min(endMinute, MINUTES_IN_DAY));
        return { element, startDate, endDate, start, end };
      });
      eventList.sort(sortByStart);

      // Some Events in the calendar column will overlap in time. When they do,
      // we want them to share the horizontal space (assuming the column is
      // vertical).
      //
      // To do this, we split the events into Blocks, each of which contains a
      // variable number of Columns, each of which contain non-overlapping
      // Events.
      //
      // Note that the end time of one event is equal to the start time of
      // another, we consider them non-overlapping.
      //
      // We choose each Block to form a continuous block of time in the
      // calendar column. Specifically, two Events are in the same Block if and
      // only if there exists some sequence of pairwise overlapping Events that
      // includes them both. This ensures that no Block will overlap another
      // Block, and each contains the least number of Events possible.
      //
      // Each Column will share the same horizontal width, and will be placed
      // adjacent to each other.
      //
      // Note that each Block may have a different number of Columns, and then
      // may not share a common factor, so the Columns may not line up in the
      // view.

      // All the event Blocks in this calendar column, ordered by their start
      // time. Each Block will be an array of Columns, which will in turn be an
      // array of Events.
      let allEventBlocks = [];
      // The current Block.
      let blockColumns = [];
      let blockEnd = eventList[0].end;

      for (let eventInfo of eventList) {
        let start = eventInfo.start;
        if (blockColumns.length && start >= blockEnd) {
          // There is a gap between this Event and the end of the Block. We also
          // know from the ordering of eventList that all other Events start at
          // the same time or later. So there are no more Events that can be
          // added to this Block. So we finish it and start a new one.
          allEventBlocks.push(blockColumns);
          blockColumns = [];
        }

        if (eventInfo.end > blockEnd) {
          blockEnd = eventInfo.end;
        }

        // Find the earliest Column that the Event fits in.
        let foundCol = false;
        for (let column of blockColumns) {
          // We know from the ordering of eventList that all Events already in a
          // Column have a start time that is equal to or earlier than this
          // Event's start time. Therefore, in order for this Event to not
          // overlap anything else in this Column, it must have a start time
          // that is later than or equal to the end time of the last Event in
          // this column.
          let colEnd = column[column.length - 1].end;
          if (start >= colEnd) {
            // It fits in this Column, so we push it to the end (preserving the
            // eventList ordering within the Column).
            column.push(eventInfo);
            foundCol = true;
            break;
          }
        }

        if (!foundCol) {
          // This Event doesn't fit in any column, so we create a new one.
          blockColumns.push([eventInfo]);
        }
      }
      if (blockColumns.length) {
        allEventBlocks.push(blockColumns);
      }

      for (let blockColumns of allEventBlocks) {
        let totalCols = blockColumns.length;
        for (let colIndex = 0; colIndex < totalCols; colIndex++) {
          for (let eventInfo of blockColumns[colIndex]) {
            if (eventInfo.processed) {
              // Already processed this Event in an earlier Column.
              continue;
            }
            let { start, end } = eventInfo;
            let colSpan = 1;
            // Currently, the Event is only contained in one Column. We want to
            // first try and stretch it across several continuous columns.
            // For this Event, we go through each later Column one by one and
            // see if there is a gap in it that it can fit in.
            // Note, we only look forward in the Columns because we already know
            // that we did not fit in the previous Columns.
            for (
              let neighbourColIndex = colIndex + 1;
              neighbourColIndex < totalCols;
              neighbourColIndex++
            ) {
              let neighbourColumn = blockColumns[neighbourColIndex];
              // Test if this Event overlaps any of the other Events in the
              // neighbouring Column.
              let overlapsCol = false;
              let indexInCol;
              for (indexInCol = 0; indexInCol < neighbourColumn.length; indexInCol++) {
                let otherEventInfo = neighbourColumn[indexInCol];
                if (end <= otherEventInfo.start) {
                  // The end of this Event is before or equal to the start of
                  // the other Event, so it cannot overlap.
                  // Moreover, the rest of the Events in this neighbouring
                  // Column have a later or equal start time, so we know that
                  // this Event cannot overlap any of them. So we can break
                  // early.
                  // We also know that indexInCol now points to the *first*
                  // Event in this neighbouring Column that starts after this
                  // Event.
                  break;
                } else if (start < otherEventInfo.end) {
                  // The end of this Event is after the start of the other
                  // Event, and the start of this Event is before the end of
                  // the other Event. So they must overlap.
                  overlapsCol = true;
                  break;
                }
              }
              if (overlapsCol) {
                // An Event must span continuously across Columns, so we must
                // break.
                break;
              }
              colSpan++;
              // Add this Event to the Column. Note that indexInCol points to
              // the *first* other Event that is later than this Event, or
              // points to the end of the Column. So we place ourselves there to
              // preserve the ordering.
              neighbourColumn.splice(indexInCol, 0, eventInfo);
            }
            eventInfo.processed = true;
            eventInfo.secondaryOffset = colIndex / totalCols;
            eventInfo.secondaryLength = colSpan / totalCols;
          }
        }
      }
      return eventList;
    }

    /**
     * Get information about which columns, relative to this column, are
     * covered by the given time interval.
     *
     * @param {number} start - The starting time of the interval, in minutes
     *   from the start of this column's day. Should be negative for times on
     *   previous days. This must be on this column's day or earlier.
     * @param {number} end - The ending time of the interval, in minutes from
     *   the start of this column's day. This can go beyond the end of this day.
     *   This must be greater than 'start' and on this column's day or later.
     *
     * @returns {object} - Data determining which columns are covered by the
     *   interval. Each column that is in the given range is covered from the
     *   start of the day to the end, apart from the first and last columns.
     * @property {number} shadows - The number of columns that have some cover.
     * @property {number} offset - The number of columns before this column that
     *   have some cover. For example, if 'start' is the day before, this is 1.
     * @property {number} startMin - The starting time of the time interval, in
     *   minutes relative to the start of the first column's day.
     * @property {number} endMin - The ending time of the time interval, in
     *   minutes relative to the start of the last column's day.
     */
    getShadowElements(start, end) {
      let shadows = 1;
      let offset = 0;
      let startMin;
      if (start < 0) {
        offset = Math.ceil(Math.abs(start) / MINUTES_IN_DAY);
        shadows += offset;
        let remainder = Math.abs(start) % MINUTES_IN_DAY;
        startMin = remainder ? MINUTES_IN_DAY - remainder : 0;
      } else {
        startMin = start;
      }
      shadows += Math.floor(end / MINUTES_IN_DAY);
      return { shadows, offset, startMin, endMin: end % MINUTES_IN_DAY };
    }

    /**
     * Clear a dragging sequence that is owned by this column.
     */
    clearDragging() {
      for (let col of this.calendarView.getEventColumns()) {
        col.fgboxes.dragbox.removeAttribute("dragging");
        col.fgboxes.box.removeAttribute("dragging");
        // We remove the height and width attributes as well.
        // In particular, this means we won't accidentally preserve the height
        // attribute if we switch to the rotated view, or the width if we
        // switch back.
        col.fgboxes.dragbox.removeAttribute("width");
        col.fgboxes.dragbox.removeAttribute("height");
        col.fgboxes.dragspacer.removeAttribute("width");
        col.fgboxes.dragspacer.removeAttribute("height");
      }

      window.removeEventListener("mousemove", this.onEventSweepMouseMove);
      window.removeEventListener("mouseup", this.onEventSweepMouseUp);
      window.removeEventListener("keypress", this.onEventSweepKeypress);
      document.calendarEventColumnDragging = null;
      this.mDragState = null;
    }

    /**
     * Update the shown drag state of all event columns in the same view using
     * the mDragState of the current column.
     */
    updateColumnShadows() {
      let startStr;
      // Tasks without Entry or Due date have a string as first label
      // instead of the time.
      let item = this.mDragState.dragOccurrence;
      if (item?.isTodo()) {
        if (!item.dueDate) {
          startStr = cal.l10n.getCalString("dragLabelTasksWithOnlyEntryDate");
        } else if (!item.entryDate) {
          startStr = cal.l10n.getCalString("dragLabelTasksWithOnlyDueDate");
        }
      }

      let { startMin, endMin, offset, shadows } = this.mDragState;
      let jsTime = new Date();
      let formatter = cal.dtz.formatter;
      if (!startStr) {
        jsTime.setHours(0, startMin, 0);
        startStr = formatter.formatTime(cal.dtz.jsDateToDateTime(jsTime, cal.dtz.floating));
      }
      jsTime.setHours(0, endMin, 0);
      let endStr = formatter.formatTime(cal.dtz.jsDateToDateTime(jsTime, cal.dtz.floating));

      let allColumns = this.calendarView.getEventColumns();
      let thisIndex = allColumns.indexOf(this);
      // NOTE: startIndex and endIndex be before or after the start and end of
      // the week, respectively, if the event spans multiple days.
      let startIndex = thisIndex - offset;
      let endIndex = startIndex + shadows - 1;

      // All columns have the same orient and pixels per minutes.
      let sizeProp = this.getAttribute("orient") == "vertical" ? "height" : "width";
      let pixPerMin = this.pixelsPerMinute;

      for (let i = 0; i < allColumns.length; i++) {
        let fgboxes = allColumns[i].fgboxes;
        if (i == startIndex) {
          fgboxes.dragbox.setAttribute("dragging", "true");
          fgboxes.box.setAttribute("dragging", "true");
          fgboxes.dragspacer.style[sizeProp] = `${startMin * pixPerMin}px`;
          fgboxes.dragbox.style[sizeProp] = `${
            ((i == endIndex ? endMin : MINUTES_IN_DAY) - startMin) * pixPerMin
          }px`;
          fgboxes.startlabel.value = startStr;
          fgboxes.endlabel.value = i == endIndex ? endStr : "";
        } else if (i == endIndex) {
          fgboxes.dragbox.setAttribute("dragging", "true");
          fgboxes.box.setAttribute("dragging", "true");
          fgboxes.dragspacer.style[sizeProp] = "0";
          fgboxes.dragbox.style[sizeProp] = `${endMin * pixPerMin}px`;
          fgboxes.startlabel.value = "";
          fgboxes.endlabel.value = endStr;
        } else if (i > startIndex && i < endIndex) {
          fgboxes.dragbox.setAttribute("dragging", "true");
          fgboxes.box.setAttribute("dragging", "true");
          fgboxes.dragspacer.style[sizeProp] = "0";
          fgboxes.dragbox.style[sizeProp] = `${MINUTES_IN_DAY * pixPerMin}px`;
          fgboxes.startlabel.value = "";
          fgboxes.endlabel.value = "";
        } else {
          fgboxes.dragbox.removeAttribute("dragging");
          fgboxes.box.removeAttribute("dragging");
        }
      }
    }

    onEventSweepKeypress(event) {
      let col = document.calendarEventColumnDragging;
      if (col && event.key == "Escape") {
        col.clearDragging();
      }
    }

    // Event sweep handlers.
    onEventSweepMouseMove(event) {
      let col = document.calendarEventColumnDragging;
      if (!col) {
        return;
      }

      let dragState = col.mDragState;

      // FIXME: Use mouseenter and mouseleave to detect column changes since
      // they fire when scrolling changes the mouse target, but mousemove does
      // not.
      let newcol = col.calendarView.findEventColumnThatContains(event.target);
      // If we leave the view, then stop our internal sweeping and start a
      // real drag session. Someday we need to fix the sweep to soely be a
      // drag session, no sweeping.
      if (dragState.dragType == "move" && !newcol) {
        // Remove the drag state.
        col.clearDragging();

        let item = dragState.dragOccurrence;

        // The multiday view currently exhibits a less than optimal strategy
        // in terms of item selection. items don't get automatically selected
        // when clicked and dragged, as to differentiate inline editing from
        // the act of selecting an event. but the application internal drop
        // targets will ask for selected items in order to pull the data from
        // the packets. that's why we need to make sure at least the currently
        // dragged event is contained in the set of selected items.
        let selectedItems = this.getSelectedItems();
        if (!selectedItems.some(aItem => aItem.hashId == item.hashId)) {
          col.calendarView.setSelectedItems([event.ctrlKey ? item.parentItem : item]);
        }
        // NOTE: Dragging to the allday header will fail (bug 1675056).
        invokeEventDragSession(dragState.dragOccurrence, col);
        return;
      }

      // Snap interval: 15 minutes or 1 minute if modifier key is pressed.
      dragState.snapIntMin =
        event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey ? 1 : 15;

      // Check if we need to jump a column.
      if (newcol && newcol != col) {
        // Find how many columns we are jumping by subtracting the dates.
        let dur = newcol.date.subtractDate(col.date);
        let jumpedColumns = dur.isNegative ? -dur.days : dur.days;
        if (dragState.dragType == "modify-start") {
          // Prevent dragging the start date after the end date in a new column.
          let limitEndMin = dragState.limitEndMin - MINUTES_IN_DAY * jumpedColumns;
          if (limitEndMin < 0) {
            return;
          }
          dragState.limitEndMin = limitEndMin;
        } else if (dragState.dragType == "modify-end") {
          let limitStartMin = dragState.limitStartMin - MINUTES_IN_DAY * jumpedColumns;
          // Prevent dragging the end date before the start date in a new column.
          if (limitStartMin > MINUTES_IN_DAY) {
            return;
          }
          dragState.limitStartMin = limitStartMin;
        } else if (dragState.dragType == "new") {
          dragState.limitEndMin -= MINUTES_IN_DAY * jumpedColumns;
          dragState.limitStartMin -= MINUTES_IN_DAY * jumpedColumns;
          dragState.jumpedColumns += jumpedColumns;
        }

        // Move drag state to the new column.
        col.mDragState = null;
        newcol.mDragState = dragState;
        document.calendarEventColumnDragging = newcol;
        // The same event handlers are still valid,
        // because they use document.calendarEventColumnDragging.
      }

      col.updateDragPosition(event.clientX, event.clientY);
    }

    /**
     * Update the drag position to point to the given client position.
     *
     * Note, this method will not switch the drag state between columns.
     *
     * @param {number} clientX - The x position.
     * @param {number} clientY - The y position.
     */
    updateDragPosition(clientX, clientY) {
      let col = document.calendarEventColumnDragging;
      if (!col) {
        return;
      }
      // If we scroll, we call this method again using the same mouse positions.
      // NOTE: if the magic scroll makes the mouse move over a different column,
      // this won't be updated until another mousemove.
      this.calendarView.setupMagicScroll(clientX, clientY, () =>
        this.updateDragPosition(clientX, clientY)
      );

      let dragState = col.mDragState;

      let mouseMinute = this.getMouseMinute({ clientX, clientY });
      if (mouseMinute < 0) {
        mouseMinute = 0;
      } else if (mouseMinute > MINUTES_IN_DAY) {
        mouseMinute = MINUTES_IN_DAY;
      }
      let snappedMouseMinute = snapMinute(
        mouseMinute - dragState.mouseMinuteOffset,
        dragState.snapIntMin
      );

      let deltamin = snappedMouseMinute - dragState.origMin;

      let shadowElements;
      if (dragState.dragType == "new") {
        // Extend deltamin in a linear way over the columns.
        deltamin += MINUTES_IN_DAY * dragState.jumpedColumns;
        if (deltamin < 0) {
          // Create a new event modifying the start. End time is fixed.
          shadowElements = {
            shadows: 1 - dragState.jumpedColumns,
            offset: 0,
            startMin: snappedMouseMinute,
            endMin: dragState.origMin,
          };
        } else {
          // Create a new event modifying the end. Start time is fixed.
          shadowElements = {
            shadows: dragState.jumpedColumns + 1,
            offset: dragState.jumpedColumns,
            startMin: dragState.origMin,
            endMin: snappedMouseMinute,
          };
        }
        dragState.startMin = shadowElements.startMin;
        dragState.endMin = shadowElements.endMin;
      } else if (dragState.dragType == "move") {
        // If we're moving, we modify startMin and endMin of the shadow.
        shadowElements = col.getShadowElements(
          dragState.origMinStart + deltamin,
          dragState.origMinEnd + deltamin
        );
        dragState.startMin = shadowElements.startMin;
        dragState.endMin = shadowElements.endMin;
        // Keep track of the last start position because it will help to
        // build the event at the end of the drag session.
        dragState.lastStart = dragState.origMinStart + deltamin;
      } else if (dragState.dragType == "modify-start") {
        // If we're modifying the start, the end time is fixed.
        shadowElements = col.getShadowElements(dragState.origMin + deltamin, dragState.limitEndMin);
        dragState.startMin = shadowElements.startMin;
        dragState.endMin = shadowElements.endMin;

        // But we need to not go past the end; if we hit
        // the end, then we'll clamp to the previous snap interval minute.
        if (dragState.startMin >= dragState.limitEndMin) {
          dragState.startMin = snapMinute(dragState.limitEndMin, dragState.snapIntMin, "backward");
        }
      } else if (dragState.dragType == "modify-end") {
        // If we're modifying the end, the start time is fixed.
        shadowElements = col.getShadowElements(
          dragState.limitStartMin,
          dragState.origMin + deltamin
        );
        dragState.startMin = shadowElements.startMin;
        dragState.endMin = shadowElements.endMin;

        // But we need to not go past the start; if we hit
        // the start, then we'll clamp to the next snap interval minute.
        if (dragState.endMin <= dragState.limitStartMin) {
          dragState.endMin = snapMinute(dragState.limitStartMin, dragState.snapIntMin, "forward");
        }
      }
      dragState.offset = shadowElements.offset;
      dragState.shadows = shadowElements.shadows;

      // Now we can update the shadow boxes position and size.
      col.updateColumnShadows();
    }

    onEventSweepMouseUp(event) {
      let col = document.calendarEventColumnDragging;
      if (!col) {
        return;
      }

      let dragState = col.mDragState;

      col.clearDragging();
      col.calendarView.clearMagicScroll();

      // If the user didn't sweep out at least a few pixels, ignore
      // unless we're in a different column.
      if (dragState.origColumn == col) {
        let position = col.getAttribute("orient") == "vertical" ? event.clientY : event.clientX;
        if (Math.abs(position - dragState.origLoc) < 3) {
          return;
        }
      }

      let newStart;
      let newEnd;
      let startTZ;
      let endTZ;
      let dragDay = col.date;
      if (dragState.dragType != "new") {
        let oldStart =
          dragState.dragOccurrence.startDate ||
          dragState.dragOccurrence.entryDate ||
          dragState.dragOccurrence.dueDate;
        let oldEnd =
          dragState.dragOccurrence.endDate ||
          dragState.dragOccurrence.dueDate ||
          dragState.dragOccurrence.entryDate;
        newStart = oldStart.clone();
        newEnd = oldEnd.clone();

        // Our views are pegged to the default timezone.  If the event
        // isn't also in the timezone, we're going to need to do some
        // tweaking. We could just do this for every event but
        // getInTimezone is slow, so it's much better to only do this
        // when the timezones actually differ from the view's.
        if (col.date.timezone != newStart.timezone || col.date.timezone != newEnd.timezone) {
          startTZ = newStart.timezone;
          endTZ = newEnd.timezone;
          newStart = newStart.getInTimezone(col.date.timezone);
          newEnd = newEnd.getInTimezone(col.date.timezone);
        }
      }

      if (dragState.dragType == "modify-start") {
        newStart.resetTo(
          dragDay.year,
          dragDay.month,
          dragDay.day,
          0,
          dragState.startMin,
          0,
          newStart.timezone
        );
      } else if (dragState.dragType == "modify-end") {
        newEnd.resetTo(
          dragDay.year,
          dragDay.month,
          dragDay.day,
          0,
          dragState.endMin,
          0,
          newEnd.timezone
        );
      } else if (dragState.dragType == "new") {
        let startDay = dragState.origColumn.date;
        let draggedForward = dragDay.compare(startDay) > 0;
        newStart = draggedForward ? startDay.clone() : dragDay.clone();
        newEnd = draggedForward ? dragDay.clone() : startDay.clone();
        newStart.isDate = false;
        newEnd.isDate = false;
        newStart.resetTo(
          newStart.year,
          newStart.month,
          newStart.day,
          0,
          dragState.startMin,
          0,
          newStart.timezone
        );
        newEnd.resetTo(
          newEnd.year,
          newEnd.month,
          newEnd.day,
          0,
          dragState.endMin,
          0,
          newEnd.timezone
        );

        // Edit the event title on the first of the new event's occurrences
        // FIXME: This newEventNeedsEditing flag is read and unset in addEvent,
        // but this is only called after some delay: after the event creation
        // transaction completes. So there is a race between this creation and
        // other actions that call addEvent.
        // Bug 1710985 would be a way to address this: i.e. at this point we
        // immediately create an element that the user can type a title into
        // without creating a calendar item until they submit the title. Then
        // we won't need any special flag for addEvent.
        if (draggedForward) {
          dragState.origColumn.newEventNeedsEditing = true;
        } else {
          col.newEventNeedsEditing = true;
        }
      } else if (dragState.dragType == "move") {
        // Figure out the new date-times of the event by adding the duration
        // of the total movement (days and minutes) to the old dates.
        let duration = dragDay.subtractDate(dragState.origColumn.date);
        let minutes = dragState.lastStart - dragState.realStart;

        // Since both boxDate and beginMove are dates (note datetimes),
        // subtractDate will only give us a non-zero number of hours on
        // DST changes. While strictly speaking, subtractDate's behavior
        // is correct, we need to move the event a discrete number of
        // days here. There is no need for normalization here, since
        // addDuration does the job for us. Also note, the duration used
        // here is only used to move over multiple days. Moving on the
        // same day uses the minutes from the dragState.
        if (duration.hours == 23) {
          // Entering DST.
          duration.hours++;
        } else if (duration.hours == 1) {
          // Leaving DST.
          duration.hours--;
        }

        if (duration.isNegative) {
          // Adding negative minutes to a negative duration makes the
          // duration more positive, but we want more negative, and
          // vice versa.
          minutes *= -1;
        }
        duration.minutes = minutes;
        duration.normalize();

        newStart.addDuration(duration);
        newEnd.addDuration(duration);
      }

      // If we tweaked tzs, put times back in their original ones.
      if (startTZ) {
        newStart = newStart.getInTimezone(startTZ);
      }
      if (endTZ) {
        newEnd = newEnd.getInTimezone(endTZ);
      }

      if (dragState.dragType == "new") {
        // We won't pass a calendar, since the display calendar is the
        // composite anyway. createNewEvent() will use the selected
        // calendar.
        col.calendarView.controller.createNewEvent(null, newStart, newEnd);
      } else if (
        dragState.dragType == "move" ||
        dragState.dragType == "modify-start" ||
        dragState.dragType == "modify-end"
      ) {
        col.calendarView.controller.modifyOccurrence(dragState.dragOccurrence, newStart, newEnd);
      }
    }

    /**
     * Start modifying an item through a mouse motion.
     *
     * @param {calItemBase} eventItem - The event item to start modifying.
     * @param {"start"|"end"|"middle"} where - Whether to modify the starting
     *   time, ending time, or moving the entire event (modify the start and
     *   end, but preserve the duration).
     * @param {object} position - The mouse position of the event that
     *   initialized* the motion.
     * @param {number} position.clientX - The client x position.
     * @param {number} position.clientY - The client y position.
     * @param {number} position.offsetStartMinute - The minute offset of the
     *   mouse relative to the event item's starting time edge.
     * @param {number} [snapIntMin=15] - The snapping interval to apply to the
     *   mouse position, in minutes.
     */
    startSweepingToModifyEvent(eventItem, where, position, snapIntMin = 15) {
      if (!canEditEventItem(eventItem)) {
        return;
      }

      this.mDragState = {
        origColumn: this,
        dragOccurrence: eventItem,
        mouseMinuteOffset: 0,
        offset: null,
        shadows: null,
        limitStartMin: null,
        lastStart: 0,
        jumpedColumns: 0,
      };

      if (this.getAttribute("orient") == "vertical") {
        this.mDragState.origLoc = position.clientY;
      } else {
        this.mDragState.origLoc = position.clientX;
      }

      let stdate = eventItem.startDate || eventItem.entryDate || eventItem.dueDate;
      let enddate = eventItem.endDate || eventItem.dueDate || eventItem.entryDate;

      // Get the start and end times in minutes, relative to the start of the
      // day. This may be negative or exceed the length of the day if the event
      // spans more than one day.
      let realStart = Math.floor(stdate.subtractDate(this.date).inSeconds / 60);
      let realEnd = Math.floor(enddate.subtractDate(this.date).inSeconds / 60);

      if (where == "start") {
        this.mDragState.dragType = "modify-start";
        // We have to use "realEnd" as fixed end value.
        this.mDragState.limitEndMin = realEnd;

        // Snap start.
        // Since we are modifying the start, we know the event starts on this
        // day, so realStart is not negative.
        this.mDragState.origMin = snapMinute(realStart, snapIntMin);

        // Show the shadows and drag labels when clicking on gripbars.
        let shadowElements = this.getShadowElements(
          this.mDragState.origMin,
          this.mDragState.limitEndMin
        );
        this.mDragState.startMin = shadowElements.startMin;
        this.mDragState.endMin = shadowElements.endMin;
        this.mDragState.shadows = shadowElements.shadows;
        this.mDragState.offset = shadowElements.offset;
        this.updateColumnShadows();
      } else if (where == "end") {
        this.mDragState.dragType = "modify-end";
        // We have to use "realStart" as fixed end value.
        this.mDragState.limitStartMin = realStart;

        // Snap end.
        // Since we are modifying the end, we know the event end on this day,
        // so realEnd is before midnight on this day.
        this.mDragState.origMin = snapMinute(realEnd, snapIntMin);

        // Show the shadows and drag labels when clicking on gripbars.
        let shadowElements = this.getShadowElements(
          this.mDragState.limitStartMin,
          this.mDragState.origMin
        );
        this.mDragState.startMin = shadowElements.startMin;
        this.mDragState.endMin = shadowElements.endMin;
        this.mDragState.shadows = shadowElements.shadows;
        this.mDragState.offset = shadowElements.offset;
        this.updateColumnShadows();
      } else if (where == "middle") {
        this.mDragState.dragType = "move";
        // In a move, origMin will be the start minute of the element where
        // the drag occurs. Along with mouseMinuteOffset, it allows to track the
        // shadow position. origMinStart and origMinEnd allow to figure out
        // the real shadow size.
        this.mDragState.mouseMinuteOffset = position.offsetStartMinute;
        // We use origMin to get the number of minutes since the start of *this*
        // day, which is 0 if realStart is negative.
        this.mDragState.origMin = Math.max(0, snapMinute(realStart, snapIntMin));
        // We snap to the start and add the real duration to find the end.
        this.mDragState.origMinStart = snapMinute(realStart, snapIntMin);
        this.mDragState.origMinEnd = realEnd + this.mDragState.origMinStart - realStart;
        // Keep also track of the real Start, it will be used at the end
        // of the drag session to calculate the new start and end datetimes.
        this.mDragState.realStart = realStart;

        let shadowElements = this.getShadowElements(
          this.mDragState.origMinStart,
          this.mDragState.origMinEnd
        );
        this.mDragState.shadows = shadowElements.shadows;
        this.mDragState.offset = shadowElements.offset;
        // Do not show the shadow yet.
      } else {
        // Invalid grabbed element.
      }

      document.calendarEventColumnDragging = this;

      window.addEventListener("mousemove", this.onEventSweepMouseMove);
      window.addEventListener("mouseup", this.onEventSweepMouseUp);
      window.addEventListener("keypress", this.onEventSweepKeypress);
    }

    /**
     * Set the hours when the day starts and ends.
     *
     * @param {number} dayStartHour - Hour at which the day starts.
     * @param {number} dayEndHour - Hour at which the day ends.
     */
    setDayStartEndHours(dayStartHour, dayEndHour) {
      if (dayStartHour < 0 || dayStartHour > dayEndHour || dayEndHour > 24) {
        throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
      }
      for (let [hour, hourBox] of this.hourBoxes.entries()) {
        hourBox.classList.toggle(
          "multiday-hour-box-off-time",
          hour < dayStartHour || hour >= dayEndHour
        );
      }
    }

    /**
     * Get the minute since the starting edge of the given element that a mouse
     * event points to.
     *
     * @param {{clientX: number, clientY: number}} mouseEvent - The pointer
     *   position in the viewport.
     * @param {Element} [element] - The element to use the starting edge of as
     *   reference. Defaults to using the starting edge of the column itself,
     *   such that the returned minute is the number of minutes since the start
     *   of the day.
     *
     * @returns {number} - The number of minutes since the starting edge of
     *   'element' that this event points to.
     */
    getMouseMinute(mouseEvent, element = this) {
      let rect = element.getBoundingClientRect();
      let pos;
      if (this.getAttribute("orient") == "vertical") {
        pos = mouseEvent.clientY - rect.top;
      } else if (document.dir == "rtl") {
        pos = rect.right - mouseEvent.clientX;
      } else {
        pos = mouseEvent.clientX - rect.left;
      }
      return pos / this.pixelsPerMinute;
    }

    /**
     * Get the datetime that the mouse event points to, snapped to the nearest
     * 15 minutes.
     *
     * @param {MouseEvent} mouseEvent - The pointer event.
     *
     * @returns {calDateTime} - A new datetime that the mouseEvent points to.
     */
    getMouseDateTime(mouseEvent) {
      let clickMinute = this.getMouseMinute(mouseEvent);
      let newStart = this.date.clone();
      newStart.isDate = false;
      newStart.hour = 0;
      // Round to nearest 15 minutes.
      newStart.minute = snapMinute(clickMinute, 15);
      return newStart;
    }
  }

  customElements.define("calendar-event-column", MozCalendarEventColumn);

  /**
   * Implements the Drag and Drop class for the Calendar Header Container.
   *
   * @augments {MozElements.CalendarDnDContainer}
   */
  class CalendarHeaderContainer extends MozElements.CalendarDnDContainer {
    /**
     * The date of the day this header represents.
     *
     * @type {calIDateTime}
     */
    date;

    constructor() {
      super();
      this.addEventListener("dblclick", this.onDblClick);
      this.addEventListener("mousedown", this.onMouseDown);
      this.addEventListener("click", this.onClick);
    }

    connectedCallback() {
      if (this.delayConnectedCallback() || this.hasConnected) {
        return;
      }
      // this.hasConnected is set to true in super.connectedCallback.
      super.connectedCallback();

      // Map from an event item's hashId to its calendar-editable-item.
      this.eventElements = new Map();

      this.eventsListElement = document.createElement("ol");
      this.eventsListElement.classList.add("allday-events-list");
      this.appendChild(this.eventsListElement);
    }

    /**
     * Return the displayed calendar-editable-item element for the given event
     * item.
     *
     * @param {calItemBase} eventItem - The event item.
     *
     * @returns {Element} - The corresponding element, or undefined if none.
     */
    findElementForEventItem(eventItem) {
      return this.eventElements.get(eventItem.hashId);
    }

    /**
     * Return all the event items that are displayed in this columns.
     *
     * @returns {calItemBase[]} - An array of all the displayed event items.
     */
    getAllEventItems() {
      return Array.from(this.eventElements.values(), element => element.occurrence);
    }

    /**
     * Create or update a displayed calendar-editable-item element for the given
     * event item.
     *
     * @param {calItemBase} eventItem - The event item to create or update an
     *   element for.
     */
    addEvent(eventItem) {
      let existing = this.eventElements.get(eventItem.hashId);
      if (existing) {
        // Remove the wrapper list item. We'll insert a replacement below.
        existing.parentNode.remove();
      }

      let itemBox = document.createXULElement("calendar-editable-item");
      let listItemWrapper = document.createElement("li");
      listItemWrapper.classList.add("allday-event-listitem");
      listItemWrapper.appendChild(itemBox);
      cal.data.binaryInsertNode(
        this.eventsListElement,
        listItemWrapper,
        eventItem,
        cal.view.compareItems,
        false,
        wrapper => wrapper.firstChild.occurrence
      );

      itemBox.calendarView = this.calendarView;
      itemBox.occurrence = eventItem;
      itemBox.setAttribute(
        "context",
        this.calendarView.getAttribute("item-context") || this.calendarView.getAttribute("context")
      );

      if (eventItem.hashId in this.calendarView.mFlashingEvents) {
        itemBox.setAttribute("flashing", "true");
      }

      this.eventElements.set(eventItem.hashId, itemBox);

      itemBox.parentBox = this;
    }

    /**
     * Remove the displayed calendar-editable-item element for the given event
     * item from this column
     *
     * @param {calItemBase} eventItem - The event item to remove the element of.
     */
    deleteEvent(eventItem) {
      let current = this.eventElements.get(eventItem.hashId);
      if (current) {
        // Need to remove the wrapper list item.
        current.parentNode.remove();
        this.eventElements.delete(eventItem.hashId);
      }
    }

    /**
     * Clear the header of all events.
     */
    clear() {
      this.eventElements.clear();
      while (this.eventsListElement.hasChildNodes()) {
        this.eventsListElement.lastChild.remove();
      }
    }

    /**
     * Set whether to show a drop shadow in the event list.
     *
     * @param {boolean} on - True to show the drop shadow, otherwise hides the
     *   drop shadow.
     */
    setDropShadow(on) {
      // NOTE: Adding or removing drop shadows may change our size, but we won't
      // let the calendar view know about these since they are temporary and we
      // don't want the view to be re-adjusting on every hover.
      let existing = this.eventsListElement.querySelector(".dropshadow");
      if (on) {
        if (!existing) {
          // Insert an empty list item.
          let dropshadow = document.createElement("li");
          dropshadow.classList.add("dropshadow", "allday-event-listitem");
          this.eventsListElement.insertBefore(dropshadow, this.eventsListElement.firstElementChild);
        }
      } else if (existing) {
        existing.remove();
      }
    }

    onDropItem(aItem) {
      let newItem = cal.item.moveToDate(aItem, this.date);
      newItem = cal.item.setToAllDay(newItem, true);
      return newItem;
    }

    /**
     * Set whether the calendar-editable-item element for the given event item
     * should be displayed as selected or unselected.
     *
     * @param {calItemBase} eventItem - The event item.
     * @param {boolean} select - Whether to show the corresponding event element
     *   as selected.
     */
    selectEvent(eventItem, select) {
      let element = this.eventElements.get(eventItem.hashId);
      if (!element) {
        return;
      }
      element.selected = select;
    }

    onDblClick(event) {
      if (event.button == 0) {
        this.calendarView.controller.createNewEvent(null, this.date, null, true);
      }
    }

    onMouseDown(event) {
      this.calendarView.selectedDay = this.date;
    }

    onClick(event) {
      if (event.button == 0) {
        if (!(event.ctrlKey || event.metaKey)) {
          this.calendarView.setSelectedItems([]);
        }
      }
      if (event.button == 2) {
        let newStart = this.calendarView.selectedDay.clone();
        newStart.isDate = true;
        this.calendarView.selectedDateTime = newStart;
        event.stopPropagation();
      }
    }

    /**
     * Determine whether the given wheel event is above a scrollable area and
     * matches the scroll direction.
     *
     * @param {WheelEvent} - The wheel event.
     *
     * @returns {boolean} - True if this event is above a scrollable area and
     *   matches its scroll direction.
     */
    wheelOnScrollableArea(event) {
      let scrollArea = this.eventsListElement;
      return (
        event.deltaY &&
        scrollArea.contains(event.target) &&
        scrollArea.scrollHeight != scrollArea.clientHeight
      );
    }
  }
  customElements.define("calendar-header-container", CalendarHeaderContainer);

  /**
   * The MozCalendarMonthDayBoxItem widget is used as event item in the
   * Day and Week views of the calendar. It displays the event name,
   * alarm icon and the category type color. It also displays the gripbar
   * components on hovering over the event. It is used to change the event
   * timings.
   *
   * @augments {MozElements.MozCalendarEditableItem}
   */
  class MozCalendarEventBox extends MozElements.MozCalendarEditableItem {
    static get inheritedAttributes() {
      return {
        ".alarm-icons-box": "flashing",
      };
    }
    constructor() {
      super();
      this.addEventListener("mousedown", event => {
        if (event.button != 0) {
          return;
        }

        event.stopPropagation();

        if (this.mEditing) {
          return;
        }

        this.parentColumn.calendarView.selectedDay = this.parentColumn.date;

        this.mouseDownPosition = {
          clientX: event.clientX,
          clientY: event.clientY,
          // We calculate the offsetStartMinute here because the clientX and
          // clientY coordinates might become 'stale' by the time we actually
          // call startItemDrag. E.g. if we scroll the view.
          offsetStartMinute: this.parentColumn.getMouseMinute(
            event,
            // We use the listitem wrapper, since that is positioned relative to
            // the event's start time.
            this.closest(".multiday-event-listitem")
          ),
        };

        let side;
        if (this.startGripbar.contains(event.target)) {
          side = "start";
        } else if (this.endGripbar.contains(event.target)) {
          side = "end";
        }

        if (side) {
          this.calendarView.setSelectedItems([
            event.ctrlKey ? this.mOccurrence.parentItem : this.mOccurrence,
          ]);

          // Start edge resize drag
          this.parentColumn.startSweepingToModifyEvent(
            this.mOccurrence,
            side,
            this.mouseDownPosition,
            event.shiftKey && !event.ctrlKey && !event.altKey && !event.metaKey ? 1 : 15
          );
        } else {
          // May be click or drag,
          // So wait for mousemove (or mouseout if fast) to start item move drag.
          this.mInMouseDown = true;
        }
      });

      this.addEventListener("mousemove", event => {
        if (!this.mInMouseDown) {
          return;
        }

        let deltaX = Math.abs(event.clientX - this.mouseDownPosition.clientX);
        let deltaY = Math.abs(event.clientY - this.mouseDownPosition.clientY);
        // More than a 3 pixel move?
        const movedMoreThan3Pixels = deltaX * deltaX + deltaY * deltaY > 9;
        if (movedMoreThan3Pixels && this.parentColumn) {
          this.startItemDrag();
        }
      });

      this.addEventListener("mouseout", event => {
        if (!this.mEditing && this.mInMouseDown && this.parentColumn) {
          this.startItemDrag();
        }
      });

      this.addEventListener("mouseup", event => {
        if (!this.mEditing) {
          this.mInMouseDown = false;
        }
      });

      this.addEventListener("mouseover", event => {
        if (this.calendarView && this.calendarView.controller) {
          event.stopPropagation();
          onMouseOverItem(event);
        }
      });

      this.addEventListener("mouseenter", event => {
        // Update the event-readonly class to determine whether to show the
        // gripbars, which are otherwise shown on hover.
        this.classList.toggle("event-readonly", !canEditEventItem(this.occurrence));
      });

      // We have two event listeners for dragstart. This event listener is for the capturing phase
      // where we are setting up the document.monthDragEvent which will be used in the event listener
      // in the bubbling phase which is set up in the calendar-editable-item.
      this.addEventListener(
        "dragstart",
        event => {
          document.monthDragEvent = this;
        },
        true
      );
    }

    connectedCallback() {
      if (this.delayConnectedCallback() || this.hasChildNodes()) {
        return;
      }

      this.appendChild(
        MozXULElement.parseXULToFragment(`
          <!-- NOTE: The following div is the same markup as EditableItem. -->
          <html:div class="calendar-item-container">
            <html:div class="calendar-item-flex">
              <html:img class="item-type-icon" alt="" />
              <html:div class="event-name-label"></html:div>
              <html:input class="plain event-name-input"
                          hidden="hidden"
                          placeholder='${cal.l10n.getCalString("newEvent")}'/>
              <html:div class="alarm-icons-box"></html:div>
              <html:img class="item-classification-icon" />
              <html:img class="item-recurrence-icon" />
            </html:div>
            <html:div class="location-desc"></html:div>
            <html:div class="calendar-category-box"></html:div>
          </html:div>
        `)
      );

      this.startGripbar = this.createGripbar("start");
      this.endGripbar = this.createGripbar("end");
      this.appendChild(this.startGripbar);
      this.appendChild(this.endGripbar);

      this.classList.add("calendar-color-box");

      this.style.pointerEvents = "auto";
      this.setAttribute("tooltip", "itemTooltip");

      this.addEventNameTextboxListener();
      this.initializeAttributeInheritance();
    }

    /**
     * Create one of the box's gripbars that can be dragged to resize the event.
     *
     * @param {"start"|"end"} side - The side the gripbar controls.
     *
     * @returns {Element} - A newly created gripbar.
     */
    createGripbar(side) {
      let gripbar = document.createElement("div");
      gripbar.classList.add(side == "start" ? "gripbar-start" : "gripbar-end");
      let img = document.createElement("img");
      img.setAttribute("src", "chrome://calendar/skin/shared/event-grippy.png");
      /* Make sure the img doesn't interfere with dragging the gripbar to
       * resize. */
      img.setAttribute("draggable", "false");
      img.setAttribute("alt", "");
      gripbar.appendChild(img);
      return gripbar;
    }

    /**
     * Update and retrieve the event's start and end dates relative to the given
     * day. This updates the gripbars.
     *
     * @param {calIDateTime} day - The day that this event is shown on.
     *
     * @returns {object} - The start and end time information.
     * @property {calIDateTime|undefined} startDate - The start date-time of the
     *   event in the timezone of the given day. Or the entry date-time for
     *   tasks, if they have one.
     * @property {calIDateTime|undefined} endDate - The end date-time of the
     *   event in the timezone of the given day. Or the due date-time for
     *   tasks, if they have one.
     * @property {number} startMinute - The number of minutes since the start of
     *   the given day that the event starts.
     * @property {number} endMinute - The number of minutes since the end of the
     *   given day that the event ends.
     */
    updateRelativeStartEndDates(day) {
      let item = this.occurrence;

      // Get closed bounds for the day. I.e. inclusive of midnight the next day.
      let closedDayStart = day.clone();
      closedDayStart.isDate = false;
      let closedDayEnd = day.clone();
      closedDayEnd.day++;
      closedDayEnd.isDate = false;

      function relativeTime(date) {
        if (!date) {
          return null;
        }
        date = date.getInTimezone(day.timezone);
        return {
          date,
          minute: date.subtractDate(closedDayStart).inSeconds / 60,
          withinClosedDay: date.compare(closedDayStart) >= 0 && date.compare(closedDayEnd) <= 0,
        };
      }

      let start;
      let end;
      if (item.isEvent()) {
        start = relativeTime(item.startDate);
        end = relativeTime(item.endDate);
      } else {
        start = relativeTime(item.entryDate);
        end = relativeTime(item.dueDate);
      }

      this.startGripbar.hidden = !(end && start?.withinClosedDay);
      this.endGripbar.hidden = !(start && end?.withinClosedDay);

      return {
        startDate: start?.date,
        endDate: end?.date,
        startMinute: start?.minute,
        endMinute: end?.minute,
      };
    }

    getOptimalMinSize(orient) {
      let label = this.querySelector(".event-name-label");
      if (orient == "vertical") {
        let minHeight =
          getOptimalMinimumHeight(label) +
          getSummarizedStyleValues(label.parentNode, ["padding-bottom", "padding-top"]) +
          getSummarizedStyleValues(this, ["border-bottom-width", "border-top-width"]);
        this.style.minHeight = minHeight + "px";
        this.style.minWidth = "1px";
        return minHeight;
      }
      label.style.minWidth = "2em";
      let minWidth = getOptimalMinimumWidth(this.eventNameLabel);
      this.style.minWidth = minWidth + "px";
      this.style.minHeight = "1px";
      return minWidth;
    }

    startItemDrag() {
      if (this.editingTimer) {
        clearTimeout(this.editingTimer);
        this.editingTimer = null;
      }

      this.calendarView.setSelectedItems([this.mOccurrence]);

      this.mEditing = false;

      this.parentColumn.startSweepingToModifyEvent(
        this.mOccurrence,
        "middle",
        this.mouseDownPosition
      );
      this.mInMouseDown = false;
    }
  }

  customElements.define("calendar-event-box", MozCalendarEventBox);

  /**
   * Abstract class used for the day and week calendar view elements. (Not month or multiweek.)
   *
   * @implements {calICalendarView}
   * @augments {MozElements.CalendarBaseView}
   * @abstract
   */
  class CalendarMultidayBaseView extends MozElements.CalendarBaseView {
    // mDateList will always be sorted before being set.
    mDateList = null;

    /**
     * A column in the view representing a particular date.
     *
     * @typedef {object} DayColumn
     * @property {calIDateTime} date - The day's date.
     * @property {Element} container - The container that holds the other
     *   elements.
     * @property {Element} headingContainer - The day heading. This holds both
     *   the short and long headings, with only one being visible at any given
     *   time.
     * @property {Element} longHeading - The day heading that uses the full
     *   day of the week. For example, "Monday".
     * @property {Element} shortHeading - The day heading that uses an
     *   abbreviation for the day of the week. For example, "Mon".
     * @property {number} longHeadingContentAreaWidth - The content area width
     *   of the headingContainer when the long heading is shown.
     * @property {Element} column - A calendar-event-column where regular
     *   (not "all day") events appear.
     * @property {Element} header - A calendar-header-container where allday
     *   events appear.
     */
    /**
     * An ordered list of the shown day columns.
     *
     * @type {DayColumn[]}
     */
    dayColumns = [];

    /**
     * Whether the number of headings, or the heading dates have changed, and
     * the view still needs to be adjusted accordingly.
     *
     * @type {boolean}
     */
    headingDatesChanged = true;
    /**
     * Whether the view has been rotated and the view still needs to be fully
     * adjusted.
     *
     * @type {boolean}
     */
    rotationChanged = true;

    mSelectedDayCol = null;
    mSelectedDay = null;

    /**
     * The hour that a 'day' starts. Any time before this is considered
     * off-time.
     *
     * @type {number}
     */
    dayStartHour = 0;
    /**
     * The hour that a 'day' ends. Any time equal to or after this is
     * considered off-time.
     *
     * @type {number}
     */
    dayEndHour = 0;

    /**
     * How many hours to show in the scrollable area.
     *
     * @type {number}
     */
    visibleHours = 9;

    /**
     * The number of pixels that a one minute duration should occupy in the
     * view.
     *
     * @type {number}
     */
    pixelsPerMinute;

    /**
     * The timebar hour box elements in this view, ordered and indexed by their
     * starting hour.
     *
     * @type {Element[]}
     */
    hourBoxes = [];

    mClickedTime = null;

    mTimeIndicatorInterval = 15;
    mTimeIndicatorMinutes = 0;

    mModeHandler = null;
    scrollMinute = 0;

    connectedCallback() {
      if (this.delayConnectedCallback() || this.hasConnected) {
        return;
      }
      super.connectedCallback();

      // Get day start/end hour from prefs and set on the view.
      // This happens here to keep tests happy.
      this.setDayStartEndHours(
        Services.prefs.getIntPref("calendar.view.daystarthour", 8),
        Services.prefs.getIntPref("calendar.view.dayendhour", 17)
      );

      // We set the scrollMinute, so that when onResize is eventually triggered
      // by refresh, we will scroll to this.
      // FIXME: Find a cleaner solution.
      this.scrollMinute = this.dayStartHour * 60;
    }

    ensureInitialized() {
      if (this.isInitialized) {
        return;
      }

      this.grid = document.createElement("div");
      this.grid.classList.add("multiday-grid");
      this.appendChild(this.grid);

      this.headerCorner = document.createElement("div");
      this.headerCorner.classList.add("multiday-header-corner");

      this.grid.appendChild(this.headerCorner);

      this.timebar = document.createElement("div");
      this.timebar.classList.add("multiday-timebar", "multiday-hour-box-container");
      this.nowIndicator = document.createElement("div");
      this.nowIndicator.classList.add("multiday-timebar-now-indicator");
      this.nowIndicator.hidden = true;
      this.timebar.appendChild(this.nowIndicator);

      let formatter = cal.dtz.formatter;
      let jsTime = new Date();
      for (let hour = 0; hour < 24; hour++) {
        let hourBox = document.createElement("div");
        hourBox.classList.add("multiday-hour-box", "multiday-timebar-time");
        // Set the time label.
        jsTime.setHours(hour, 0, 0);
        hourBox.textContent = formatter.formatTime(
          cal.dtz.jsDateToDateTime(jsTime, cal.dtz.floating)
        );
        this.timebar.appendChild(hourBox);
        this.hourBoxes.push(hourBox);
      }
      this.grid.appendChild(this.timebar);

      this.endBorder = document.createElement("div");
      this.endBorder.classList.add("multiday-end-border");
      this.grid.appendChild(this.endBorder);

      this.initializeAttributeInheritance();

      // super.connectedCallback has to be called after the time bar is added to the DOM.
      super.ensureInitialized();

      this.addEventListener("click", event => {
        if (event.button != 2) {
          return;
        }
        this.selectedDateTime = null;
      });

      this.addEventListener("wheel", event => {
        // Only shift hours if no modifier is pressed.
        if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
          return;
        }
        let deltaTime = this.getAttribute("orient") == "horizontal" ? event.deltaX : event.deltaY;
        if (!deltaTime) {
          // Scroll is not in the same direction as the time axis, so just do
          // the default scroll (if any).
          return;
        }
        if (
          this.headerCorner.contains(event.target) ||
          this.dayColumns.some(col => col.headingContainer.contains(event.target))
        ) {
          // Prevent any scrolling in these sticky headers.
          event.preventDefault();
          return;
        }
        let header = this.dayColumns.find(col => col.header.contains(event.target))?.header;
        if (header) {
          if (!header.wheelOnScrollableArea(event)) {
            // Prevent any scrolling in this header.
            event.preventDefault();
            // Otherwise, we let the default wheel handler scroll the header.
            // NOTE: We have the CSS overscroll-behavior set to "none", to stop
            // the default wheel handler from scrolling the parent if the header
            // is already at its scrolling edge.
          }
          return;
        }
        let minute = this.scrollMinute;
        if (event.deltaMode == event.DOM_DELTA_LINE) {
          // We snap from the current hour to the next one.
          let scrollHour = deltaTime < 0 ? Math.floor(minute / 60) : Math.ceil(minute / 60);
          if (Math.abs(scrollHour * 60 - minute) < 10) {
            // If the change in minutes would be less than 10 minutes, go to the
            // next hour. This means that anything in the close neighbourhood of
            // the hour line will scroll to the same hour.
            scrollHour += Math.sign(deltaTime);
          }
          minute = scrollHour * 60;
        } else if (event.deltaMode == event.DOM_DELTA_PIXEL) {
          let minDiff = deltaTime / this.pixelsPerMinute;
          minute += minDiff < 0 ? Math.floor(minDiff) : Math.ceil(minDiff);
        } else {
          return;
        }
        event.preventDefault();
        this.scrollToMinute(minute);
      });

      this.grid.addEventListener("scroll", event => {
        if (!this.clientHeight) {
          // Hidden, so don't store the scroll position.
          // FIXME: We don't expect scrolling whilst we are hidden, so we should
          // try and remove. This is only seems to happen in mochitests.
          return;
        }
        let scrollPx;
        if (this.getAttribute("orient") == "horizontal") {
          scrollPx = document.dir == "rtl" ? -this.grid.scrollLeft : this.grid.scrollLeft;
        } else {
          scrollPx = this.grid.scrollTop;
        }
        this.scrollMinute = Math.round(scrollPx / this.pixelsPerMinute);
      });

      // Get visible hours from prefs and set on the view.
      this.setVisibleHours(Services.prefs.getIntPref("calendar.view.visiblehours", 9));
    }

    // calICalendarView Properties

    get supportsZoom() {
      return true;
    }

    get supportsRotation() {
      return true;
    }

    get supportsDisjointDates() {
      return true;
    }

    get hasDisjointDates() {
      return this.mDateList != null;
    }

    set selectedDay(day) {
      // Ignore if just 1 visible, it's always selected, but we don't indicate it.
      if (this.numVisibleDates == 1) {
        this.fireEvent("dayselect", day);
        return;
      }

      if (this.mSelectedDayCol) {
        this.mSelectedDayCol.container.classList.remove("day-column-selected");
      }

      if (day) {
        this.mSelectedDayCol = this.findColumnForDate(day);
        if (this.mSelectedDayCol) {
          this.mSelectedDay = this.mSelectedDayCol.date;
          this.mSelectedDayCol.container.classList.add("day-column-selected");
        } else {
          this.mSelectedDay = day;
        }
      }
      this.fireEvent("dayselect", day);
    }

    get selectedDay() {
      let selected;
      if (this.numVisibleDates == 1) {
        selected = this.dayColumns[0].date;
      } else if (this.mSelectedDay) {
        selected = this.mSelectedDay;
      } else if (this.mSelectedDayCol) {
        selected = this.mSelectedDayCol.date;
      }

      // TODO Make sure the selected day is valid.
      // TODO Select now if it is in the range?
      return selected;
    }

    // End calICalendarView Properties

    set selectedDateTime(dateTime) {
      this.mClickedTime = dateTime;
    }

    get selectedDateTime() {
      return this.mClickedTime;
    }

    // Private

    get numVisibleDates() {
      if (this.mDateList) {
        return this.mDateList.length;
      }

      let count = 0;

      if (!this.mStartDate || !this.mEndDate) {
        // The view has not been initialized, so there are 0 visible dates.
        return count;
      }

      const date = this.mStartDate.clone();
      while (date.compare(this.mEndDate) <= 0) {
        count++;
        date.day += 1;
      }

      return count;
    }

    /**
     * Update the position of the time indicator.
     */
    updateTimeIndicatorPosition() {
      // Calculate the position of the indicator based on how far into the day
      // it is and the size of the current view.
      const now = cal.dtz.now();
      const nowMinutes = now.hour * 60 + now.minute;

      let position = `${this.pixelsPerMinute * nowMinutes - 1}px`;
      let isVertical = this.getAttribute("orient") == "vertical";

      // Control the position of the dot in the time bar, which is present even
      // when the view does not show the current day. Inline start controls
      // horizontal position of the dot, block controls vertical.
      this.nowIndicator.style.insetInlineStart = isVertical ? null : position;
      this.nowIndicator.style.insetBlockStart = isVertical ? position : null;

      // Control the position of the bar, which should be visible only for the
      // current day.
      const todayIndicator = this.findColumnForDate(this.today())?.column.timeIndicatorBox;
      if (todayIndicator) {
        todayIndicator.style.marginInlineStart = isVertical ? null : position;
        todayIndicator.style.marginBlockStart = isVertical ? position : null;
      }
    }

    /**
     * Handle preference changes. Typically called by a preference observer.
     *
     * @param {object} subject - The subject, a prefs object.
     * @param {string} topic - The notification topic.
     * @param {string} preference - The preference to handle.
     */
    handlePreference(subject, topic, preference) {
      subject.QueryInterface(Ci.nsIPrefBranch);
      switch (preference) {
        case "calendar.view.daystarthour":
          this.setDayStartEndHours(subject.getIntPref(preference), this.dayEndHour);
          break;

        case "calendar.view.dayendhour":
          this.setDayStartEndHours(this.dayStartHour, subject.getIntPref(preference));
          break;

        case "calendar.view.visiblehours":
          this.setVisibleHours(subject.getIntPref(preference));
          this.readjustView(true, true, this.scrollMinute);
          break;

        default:
          this.handleCommonPreference(subject, topic, preference);
          break;
      }
    }

    /**
     * Handle resizing by adjusting the view to the new size.
     */
    onResize() {
      // Assume resize in both directions.
      this.readjustView(true, true, this.scrollMinute);
    }

    /**
     * Perform an operation on the header that may cause it to resize, such that
     * the view can adjust itself accordingly.
     *
     * @param {Element} header - The header that may resize.
     * @param {Function} operation - An operation to run.
     */
    doResizingHeaderOperation(header, operation) {
      // Capture scrollMinute before we potentially change the size of the view.
      let scrollMinute = this.scrollMinute;
      let beforeRect = header.getBoundingClientRect();

      operation();

      let afterRect = header.getBoundingClientRect();
      this.readjustView(
        beforeRect.height != afterRect.height,
        beforeRect.width != afterRect.width,
        scrollMinute
      );
    }

    /**
     * Adjust the view based an a change in rotation, layout, view size, or
     * header size.
     *
     * Note, this method will do nothing whilst the view is hidden, so must be
     * called again once it is shown.
     *
     * @param {boolean} verticalResize - There may have been a change in the
     *   vertical direction.
     * @param {boolean} horizontalResize - There may have been a change in the
     *   horizontal direction.
     * @param {number} scrollMinute - The minute we should scroll after
     *   adjusting the view in the time-direction.
     */
    readjustView(verticalResize, horizontalResize, scrollMinute) {
      if (!this.clientHeight || !this.clientWidth) {
        // Do nothing if we have zero width or height since we cannot measure
        // elements. Should be called again once we can.
        return;
      }

      let isHorizontal = this.getAttribute("orient") == "horizontal";

      // Adjust the headings. We do this before measuring the pixels per minute
      // because this may adjust the size of the headings.
      if (this.headingDatesChanged) {
        this.shortHeadingContentWidth = 0;
        for (let dayCol of this.dayColumns) {
          // Make sure both headings are visible for measuring.
          // We will hide one of them again further below.
          dayCol.shortHeading.hidden = false;
          dayCol.longHeading.hidden = false;

          // We can safely measure the widths of the short and long headings
          // because their headingContainer does not grow or shrink them.
          let longHeadingRect = dayCol.longHeading.getBoundingClientRect();
          if (!this.headingContentHeight) {
            // We assume this is constant and the same for each heading.
            this.headingContentHeight = longHeadingRect.height;
          }

          dayCol.longHeadingContentAreaWidth = longHeadingRect.width;
          this.shortHeadingContentWidth = Math.max(
            this.shortHeadingContentWidth,
            dayCol.shortHeading.getBoundingClientRect().width
          );
        }
        // Unset the other properties that use these values.
        // NOTE: We do not calculate new values for these properties here
        // because they can only be measured in one of the rotated or
        // non-rotated states. So we will calculate them as needed.
        delete this.rotatedHeadingWidth;
        delete this.minHeadingWidth;
      }

      // Whether the headings need readjusting.
      let adjustHeadingPositioning = this.headingDatesChanged || this.rotationChanged;
      // Position headers.
      if (isHorizontal) {
        // We're in the rotated state, so we can measure the corresponding
        // header dimensions.
        // NOTE: we always use short headings in the rotated view.
        if (!this.rotatedHeadingWidth) {
          // Width is shared by all headings in the rotated view, so we set it
          // so that its large enough to fit the text of each heading.
          if (!this.rotatedHeadingContentToBorderWidthOffset) {
            // We cache the value since we assume it is constant within the
            // rotated view.
            this.rotatedHeadingContentToBorderOffset = this.measureHeadingContentToBorderOffset();
          }
          this.rotatedHeadingWidth =
            this.shortHeadingContentWidth + this.rotatedHeadingContentToBorderOffset.inline;
          adjustHeadingPositioning = true;
        }
        if (adjustHeadingPositioning) {
          for (let dayCol of this.dayColumns) {
            // The header is sticky, so we need to position it. We want a constant
            // position, so we offset the header by the heading width.
            // NOTE: We assume there is no margin between the two.
            dayCol.header.style.insetBlockStart = null;
            dayCol.header.style.insetInlineStart = `${this.rotatedHeadingWidth}px`;
            // NOTE: The heading must have its box-sizing set to border-box for
            // this to work properly.
            dayCol.headingContainer.style.width = `${this.rotatedHeadingWidth}px`;
            dayCol.headingContainer.style.minWidth = null;
          }
        }
      } else {
        // We're in the non-rotated state, so we can measure the corresponding
        // header dimensions.
        if (!this.headingContentToBorderOffset) {
          // We cache the value since we assume it is constant within the
          // non-rotated view.
          this.headingContentToBorderOffset = this.measureHeadingContentToBorderOffset();
        }
        if (!this.headingHeight) {
          this.headingHeight = this.headingContentHeight + this.headingContentToBorderOffset.block;
        }
        if (!this.minHeadingWidth) {
          // Make the minimum width large enough to fit the short heading.
          this.minHeadingWidth =
            this.shortHeadingContentWidth + this.headingContentToBorderOffset.inline;
          adjustHeadingPositioning = true;
        }
        if (adjustHeadingPositioning) {
          for (let dayCol of this.dayColumns) {
            // We offset the header by the heading height.
            dayCol.header.style.insetBlockStart = `${this.headingHeight}px`;
            dayCol.header.style.insetInlineStart = null;
            dayCol.headingContainer.style.minWidth = `${this.minHeadingWidth}px`;
            dayCol.headingContainer.style.width = null;
          }
        }
      }

      // If the view is horizontal, we always use the short headings.
      // We do this before calculating the pixelsPerMinute since the width of
      // the heading is important to determining the size of the scroll area.
      // We only need to do this when the view has been rotated, or when new
      // headings have been added. adjustHeadingPosition covers both of these.
      if (isHorizontal && adjustHeadingPositioning) {
        for (let dayCol of this.dayColumns) {
          dayCol.shortHeading.hidden = false;
          dayCol.longHeading.hidden = true;
        }
      }
      // Otherwise, if the view is vertical, we determine whether to use short
      // or long headings after changing the pixelsPerMinute, which can change
      // the amount of horizontal space.
      // NOTE: when the view is vertical, both the short and long headings
      // should take up the same vertical space, so this shouldn't effect the
      // pixelsPerMinute calculation.

      if (this.rotationChanged) {
        // Clear the set widths/heights or positions before calculating the
        // scroll area. Otherwise they will remain extended in the wrong
        // direction, and keep the grid content larger than necessary, which can
        // cause the grid content to overflow, which in turn shrinks the
        // calculated scroll area due to extra scrollbars.
        // The timebar will be corrected when the pixelsPerMinute is calculated.
        this.timebar.style.width = null;
        this.timebar.style.height = null;
        // The time indicators will be corrected in updateTimeIndicatorPosition.
        this.nowIndicator.style.insetInlineStart = null;
        this.nowIndicator.style.insetBlockStart = null;
        let todayIndicator = this.findColumnForDate(this.today())?.column.timeIndicatorBox;
        if (todayIndicator) {
          todayIndicator.style.marginInlineStart = null;
          todayIndicator.style.marginBlockStart = null;
        }
      }

      // Adjust pixels per minute.
      let ppmHasChanged = false;
      if (
        adjustHeadingPositioning ||
        (isHorizontal && horizontalResize) ||
        (!isHorizontal && verticalResize)
      ) {
        if (isHorizontal && !this.timebarMinWidth) {
          // Measure the minimum width such that the time labels do not overflow
          // and are equal width.
          this.timebar.style.height = null;
          this.timebar.style.width = "min-content";
          let maxWidth = 0;
          for (let hourBox of this.hourBoxes) {
            maxWidth = Math.max(maxWidth, hourBox.getBoundingClientRect().width);
          }
          // NOTE: We assume no margin between the boxes.
          this.timebarMinWidth = maxWidth * this.hourBoxes.length;
          // width should be set to the correct value below when the
          // pixelsPerMinute changes.
        } else if (!isHorizontal && !this.timebarMinHeight) {
          // Measure the minimum height such that the time labels do not
          // overflow and are equal height.
          this.timebar.style.width = null;
          this.timebar.style.height = "min-content";
          let maxHeight = 0;
          for (let hourBox of this.hourBoxes) {
            maxHeight = Math.max(maxHeight, hourBox.getBoundingClientRect().height);
          }
          // NOTE: We assume no margin between the boxes.
          this.timebarMinHeight = maxHeight * this.hourBoxes.length;
          // height should be set to the correct value below when the
          // pixelsPerMinute changes.
        }

        // We want to know how much visible space is available in the
        // "time-direction" of this view's scrollable area, which will be used
        // to show 'this.visibleHour' hours in the timebar.
        // NOTE: The area returned by getScrollAreaRect is the *current*
        // scrollable area. We are working with the assumption that the length
        // in the time-direction will not change when we change the pixels per
        // minute. This assumption is broken if the changes cause the
        // non-time-direction to switch from overflowing to not, or vis versa,
        // which adds or removes a scrollbar. Since we are only changing the
        // content length in the time-direction, this should only happen in edge
        // cases (e.g. scrollbar being added from a time-direction overflow also
        // causes the non-time-direction to overflow).
        let scrollArea = this.getScrollAreaRect();
        let dayScale = 24 / this.visibleHours;
        let dayPixels = isHorizontal
          ? Math.max((scrollArea.right - scrollArea.left) * dayScale, this.timebarMinWidth)
          : Math.max((scrollArea.bottom - scrollArea.top) * dayScale, this.timebarMinHeight);
        let pixelsPerMinute = dayPixels / MINUTES_IN_DAY;
        if (this.rotationChanged || pixelsPerMinute != this.pixelsPerMinute) {
          ppmHasChanged = true;
          this.pixelsPerMinute = pixelsPerMinute;

          // Use the same calculation as in the event columns.
          let dayPx = `${MINUTES_IN_DAY * pixelsPerMinute}px`;
          if (isHorizontal) {
            this.timebar.style.width = dayPx;
            this.timebar.style.height = null;
          } else {
            this.timebar.style.height = dayPx;
            this.timebar.style.width = null;
          }

          for (const col of this.dayColumns) {
            col.column.pixelsPerMinute = pixelsPerMinute;
          }
        }

        // Scroll to the given minute.
        this.scrollToMinute(scrollMinute);
        // A change in pixels per minute can cause a scrollbar to appear or
        // disappear, which can change the available space for headers.
        if (ppmHasChanged) {
          verticalResize = true;
          horizontalResize = true;
        }
      }

      // Decide whether to use short headings.
      if (!isHorizontal && (horizontalResize || adjustHeadingPositioning)) {
        // Use short headings if *any* heading would horizontally overflow with
        // a long heading.
        let widthOffset = this.headingContentToBorderOffset.inline;
        let useShortHeadings = this.dayColumns.some(
          col =>
            col.headingContainer.getBoundingClientRect().width <
            col.longHeadingContentAreaWidth + widthOffset
        );
        for (let dayCol of this.dayColumns) {
          dayCol.shortHeading.hidden = !useShortHeadings;
          dayCol.longHeading.hidden = useShortHeadings;
        }
      }

      this.updateTimeIndicatorPosition();

      // The changes have now been handled.
      this.headingDatesChanged = false;
      this.rotationChanged = false;
    }

    /**
     * Measure the total offset between the content width and border width of
     * the day headings.
     *
     * @returns {{inline: number, block: number}} - The offsets in their
     *   respective directions.
     */
    measureHeadingContentToBorderOffset() {
      if (!this.dayColumns.length) {
        // undefined properties.
        return {};
      }
      // We cache the offset. We expect these styles to differ between the
      // rotated and non-rotated views, but to otherwise be constant.
      let style = getComputedStyle(this.dayColumns[0].headingContainer);
      return {
        inline:
          parseFloat(style.paddingInlineStart) +
          parseFloat(style.paddingInlineEnd) +
          parseFloat(style.borderInlineStartWidth) +
          parseFloat(style.borderInlineEndWidth),
        block:
          parseFloat(style.paddingBlockStart) +
          parseFloat(style.paddingBlockEnd) +
          parseFloat(style.borderBlockStartWidth) +
          parseFloat(style.borderBlockEndWidth),
      };
    }

    /**
     * Make a calendar item flash or stop flashing. Called when the item's alarm fires.
     *
     * @param {calIItemBase} item - The calendar item.
     * @param {boolean} stop - Whether to stop the item from flashing.
     */
    flashAlarm(item, stop) {
      function setFlashingAttribute(box) {
        if (stop) {
          box.removeAttribute("flashing");
        } else {
          box.setAttribute("flashing", "true");
        }
      }

      const showIndicator = Services.prefs.getBoolPref("calendar.alarms.indicator.show", true);
      const totaltime = Services.prefs.getIntPref("calendar.alarms.indicator.totaltime", 3600);

      if (!stop && (!showIndicator || totaltime < 1)) {
        // No need to animate if the indicator should not be shown.
        return;
      }

      // Make sure the flashing attribute is set or reset on all visible boxes.
      const columns = this.findColumnsForItem(item);
      for (const col of columns) {
        const colBox = col.column.findElementForEventItem(item);
        const headerBox = col.header.findElementForEventItem(item);

        if (colBox) {
          setFlashingAttribute(colBox);
        }
        if (headerBox) {
          setFlashingAttribute(headerBox);
        }
      }

      if (stop) {
        // We are done flashing, prevent newly created event boxes from flashing.
        delete this.mFlashingEvents[item.hashId];
      } else {
        // Set up a timer to stop the flashing after the total time.
        this.mFlashingEvents[item.hashId] = item;
        setTimeout(() => this.flashAlarm(item, true), totaltime);
      }
    }

    // calICalendarView Methods

    showDate(date) {
      const targetDate = date.getInTimezone(this.mTimezone);
      targetDate.isDate = true;

      if (this.mStartDate.timezone.tzid == date.timezone.tzid) {
        if (this.mStartDate && this.mEndDate) {
          if (this.mStartDate.compare(targetDate) <= 0 && this.mEndDate.compare(targetDate) >= 0) {
            return;
          }
        } else if (this.mDateList) {
          for (const listDate of this.mDateList) {
            // If date is already visible, nothing to do.
            if (listDate.compare(targetDate) == 0) {
              return;
            }
          }
        }
      }

      // If we're only showing one date, then continue
      // to only show one date; otherwise, show the week.
      if (this.numVisibleDates == 1) {
        this.setDateRange(date, date);
      } else {
        this.setDateRange(date.startOfWeek, date.endOfWeek);
      }

      this.selectedDay = targetDate;
    }

    setDateRange(startDate, endDate) {
      this.rangeStartDate = startDate;
      this.rangeEndDate = endDate;

      const viewStart = startDate.getInTimezone(this.mTimezone);
      const viewEnd = endDate.getInTimezone(this.mTimezone);

      viewStart.isDate = true;
      viewStart.makeImmutable();
      viewEnd.isDate = true;
      viewEnd.makeImmutable();

      this.mStartDate = viewStart;
      this.mEndDate = viewEnd;

      // The start and end dates to query calendars with (in CalendarFilteredViewMixin).
      this.startDate = viewStart;
      let viewEndPlusOne = viewEnd.clone();
      viewEndPlusOne.day++;
      this.endDate = viewEndPlusOne;

      // First, check values of tasksInView, workdaysOnly, showCompleted.
      // Their status will determine the value of toggleStatus, which is
      // saved to this.mToggleStatus during last call to relayout()
      let toggleStatus = 0;

      if (this.mTasksInView) {
        toggleStatus |= this.mToggleStatusFlag.TasksInView;
      }
      if (this.mWorkdaysOnly) {
        toggleStatus |= this.mToggleStatusFlag.WorkdaysOnly;
      }
      if (this.mShowCompleted) {
        toggleStatus |= this.mToggleStatusFlag.ShowCompleted;
      }

      // Update the navigation bar only when changes are related to the current view.
      if (this.isVisible()) {
        calendarNavigationBar.setDateRange(viewStart, viewEnd);
      }

      // Check whether view range has been changed since last call to relayout().
      if (
        !this.mViewStart ||
        !this.mViewEnd ||
        this.mViewStart.timezone.tzid != viewStart.timezone.tzid ||
        this.mViewEnd.compare(viewEnd) != 0 ||
        this.mViewStart.compare(viewStart) != 0 ||
        this.mToggleStatus != toggleStatus
      ) {
        this.relayout({ dates: true });
      }
    }

    getDateList() {
      const dates = [];
      if (this.mStartDate && this.mEndDate) {
        const date = this.mStartDate.clone();
        while (date.compare(this.mEndDate) <= 0) {
          dates.push(date.clone());
          date.day += 1;
        }
      } else if (this.mDateList) {
        for (const date of this.mDateList) {
          dates.push(date.clone());
        }
      }

      return dates;
    }

    setSelectedItems(items, suppressEvent) {
      if (this.mSelectedItems) {
        for (const item of this.mSelectedItems) {
          for (const occ of this.getItemOccurrencesInView(item)) {
            const cols = this.findColumnsForItem(occ);
            for (const col of cols) {
              col.header.selectEvent(occ, false);
              col.column.selectEvent(occ, false);
            }
          }
        }
      }
      this.mSelectedItems = items || [];

      for (const item of this.mSelectedItems) {
        for (const occ of this.getItemOccurrencesInView(item)) {
          const cols = this.findColumnsForItem(occ);
          if (cols.length == 0) {
            continue;
          }
          const start = item.startDate || item.entryDate || item.dueDate;
          for (const col of cols) {
            if (start.isDate) {
              col.header.selectEvent(occ, true);
            } else {
              col.column.selectEvent(occ, true);
            }
          }
        }
      }

      if (!suppressEvent) {
        this.fireEvent("itemselect", this.mSelectedItems);
      }
    }

    centerSelectedItems() {
      const displayTZ = cal.dtz.defaultTimezone;
      let lowMinute = MINUTES_IN_DAY;
      let highMinute = 0;

      for (const item of this.mSelectedItems) {
        const startDateProperty = cal.dtz.startDateProp(item);
        const endDateProperty = cal.dtz.endDateProp(item);

        let occs = [];
        if (item.recurrenceInfo) {
          // If selected a parent item, show occurrence(s) in view range.
          occs = item.getOccurrencesBetween(this.startDate, this.queryEndDate);
        } else {
          occs = [item];
        }

        for (const occ of occs) {
          let occStart = occ[startDateProperty];
          let occEnd = occ[endDateProperty];
          // Must have at least one of start or end.
          if (!occStart && !occEnd) {
            // Task with no dates.
            continue;
          }

          // If just has single datetime, treat as zero duration item
          // (such as task with due datetime or start datetime only).
          occStart = occStart || occEnd;
          occEnd = occEnd || occStart;
          // Now both occStart and occEnd are datetimes.

          // Skip occurrence if all-day: it won't show in time view.
          if (occStart.isDate || occEnd.isDate) {
            continue;
          }

          // Trim dates to view.  (Not mutated so just reuse view dates.)
          if (this.startDate.compare(occStart) > 0) {
            occStart = this.startDate;
          }
          if (this.queryEndDate.compare(occEnd) < 0) {
            occEnd = this.queryEndDate;
          }

          // Convert to display timezone if different.
          if (occStart.timezone != displayTZ) {
            occStart = occStart.getInTimezone(displayTZ);
          }
          if (occEnd.timezone != displayTZ) {
            occEnd = occEnd.getInTimezone(displayTZ);
          }
          // If crosses midnight in current TZ, set end just
          // before midnight after start so start/title usually visible.
          if (!cal.dtz.sameDay(occStart, occEnd)) {
            occEnd = occStart.clone();
            occEnd.day = occStart.day;
            occEnd.hour = 23;
            occEnd.minute = 59;
          }

          // Ensure range shows occ.
          lowMinute = Math.min(occStart.hour * 60 + occStart.minute, lowMinute);
          highMinute = Math.max(occEnd.hour * 60 + occEnd.minute, highMinute);
        }
      }

      let halfDurationMinutes = (highMinute - lowMinute) / 2;
      if (this.mSelectedItems.length && halfDurationMinutes >= 0) {
        let halfVisibleMinutes = this.visibleHours * 30;
        if (halfDurationMinutes <= halfVisibleMinutes) {
          // If the full duration fits in the view, then center the middle of
          // the region.
          this.scrollToMinute(lowMinute + halfDurationMinutes - halfVisibleMinutes);
        } else if (this.mSelectedItems.length == 1) {
          // Else, if only one event is selected, then center the start.
          this.scrollToMinute(lowMinute - halfVisibleMinutes);
        }
        // Else, don't scroll.
      }
    }

    zoomIn(level) {
      let visibleHours = Services.prefs.getIntPref("calendar.view.visiblehours", 9);
      visibleHours += level || 1;

      Services.prefs.setIntPref("calendar.view.visiblehours", Math.min(visibleHours, 24));
    }

    zoomOut(level) {
      let visibleHours = Services.prefs.getIntPref("calendar.view.visiblehours", 9);
      visibleHours -= level || 1;

      Services.prefs.setIntPref("calendar.view.visiblehours", Math.max(1, visibleHours));
    }

    zoomReset() {
      Services.prefs.setIntPref("calendar.view.visiblehours", 9);
    }

    // End calICalendarView Methods

    /**
     * Return all the occurrences of a given item that are currently displayed in the view.
     *
     * @param {calIItemBase} item - A calendar item.
     * @returns {calIItemBase[]} An array of occurrences.
     */
    getItemOccurrencesInView(item) {
      if (item.recurrenceInfo && item.recurrenceStartDate) {
        // If a parent item is selected, show occurrence(s) in view range.
        return item.getOccurrencesBetween(this.startDate, this.queryEndDate);
      } else if (item.recurrenceStartDate) {
        return [item];
      }
      // Undated todo.
      return [];
    }

    /**
     * Set an attribute on the view element, and do re-orientation and re-layout if needed.
     *
     * @param {string} attr - The attribute to set.
     * @param {string} value - The value to set.
     */
    setAttribute(attr, value) {
      let rotated = attr == "orient" && this.getAttribute("orient") != value;
      let context = attr == "context" || attr == "item-context";

      // This should be done using lookupMethod(), see bug 286629.
      const ret = XULElement.prototype.setAttribute.call(this, attr, value);

      if (rotated || context) {
        this.relayout({ rotated, context });
      }

      return ret;
    }

    /**
     * Re-render the view based on the given changes.
     *
     * Note, changing the dates will wipe the columns of all events, otherwise
     * the current events are kept in place.
     *
     * @param {object} [changes] - The relevant changes to the view. Defaults to
     *   all changes.
     * @property {boolean} dates - A change in the column dates.
     * @property {boolean} rotated - A change in the rotation.
     * @property {boolean} context - A change in the context menu.
     */
    relayout(changes) {
      if (!this.mStartDate || !this.mEndDate) {
        return;
      }
      if (!changes) {
        changes = { dates: true, rotated: true, context: true };
      }
      let scrollMinute = this.scrollMinute;

      const orient = this.getAttribute("orient") || "vertical";
      this.grid.classList.toggle("multiday-grid-rotated", orient == "horizontal");

      let context = this.getAttribute("context");
      let itemContext = this.getAttribute("item-context") || context;

      for (let dayCol of this.dayColumns) {
        dayCol.column.startLayoutBatchChange();
      }

      if (changes.dates) {
        const computedDateList = [];
        const startDate = this.mStartDate.clone();
        while (startDate.compare(this.mEndDate) <= 0) {
          const workday = startDate.clone();
          workday.makeImmutable();

          if (this.mDisplayDaysOff || !this.mDaysOffArray.includes(startDate.weekday)) {
            computedDateList.push(workday);
          }
          startDate.day += 1;
        }
        this.mDateList = computedDateList;

        this.grid.style.setProperty("--multiday-num-days", computedDateList.length);

        // Deselect the previously selected event upon switching views,
        // otherwise those events will stay selected forever, if other events
        // are selected after changing the view.
        this.setSelectedItems([], true);

        // Get today's date.
        let today = this.today();

        let dateFormatter = cal.dtz.formatter;

        // Assume the heading widths are no longer valid because the displayed
        // dates are likely to change.
        // We do not measure them here since we may be hidden. Instead we do so
        // in readjustView.
        this.headingDatesChanged = true;
        let colIndex;
        for (colIndex = 0; colIndex < computedDateList.length; colIndex++) {
          let dayDate = computedDateList[colIndex];
          let dayCol = this.dayColumns[colIndex];
          if (dayCol) {
            dayCol.column.clear();
            dayCol.header.clear();
          } else {
            dayCol = {};
            dayCol.container = document.createElement("article");
            dayCol.container.classList.add("day-column-container");
            this.grid.insertBefore(dayCol.container, this.endBorder);

            dayCol.headingContainer = document.createElement("h2");
            dayCol.headingContainer.classList.add("day-column-heading");
            dayCol.longHeading = document.createElement("span");
            dayCol.shortHeading = document.createElement("span");
            dayCol.headingContainer.appendChild(dayCol.longHeading);
            dayCol.headingContainer.appendChild(dayCol.shortHeading);
            dayCol.container.appendChild(dayCol.headingContainer);

            dayCol.header = document.createXULElement("calendar-header-container");
            dayCol.header.setAttribute("orient", "vertical");
            dayCol.container.appendChild(dayCol.header);
            dayCol.header.calendarView = this;

            dayCol.column = document.createXULElement("calendar-event-column");
            dayCol.container.appendChild(dayCol.column);
            dayCol.column.calendarView = this;
            dayCol.column.startLayoutBatchChange();
            dayCol.column.pixelsPerMinute = this.pixelsPerMinute;
            dayCol.column.setDayStartEndHours(this.dayStartHour, this.dayEndHour);
            dayCol.column.setAttribute("orient", orient);
            dayCol.column.setAttribute("context", context);
            dayCol.column.setAttribute("item-context", itemContext);

            this.dayColumns[colIndex] = dayCol;
          }
          dayCol.date = dayDate.clone();
          dayCol.date.isDate = true;
          dayCol.date.makeImmutable();

          /* Set up day of the week headings. */
          dayCol.shortHeading.textContent = cal.l10n.getCalString("dayHeaderLabel", [
            dateFormatter.shortDayName(dayDate.weekday),
            dateFormatter.formatDateWithoutYear(dayDate),
          ]);
          dayCol.longHeading.textContent = cal.l10n.getCalString("dayHeaderLabel", [
            dateFormatter.dayName(dayDate.weekday),
            dateFormatter.formatDateWithoutYear(dayDate),
          ]);

          /* Set up all-day header. */
          dayCol.header.date = dayDate;

          /* Set up event column. */
          dayCol.column.date = dayDate;

          /* Set up styling classes for day-off and today. */
          dayCol.container.classList.toggle(
            "day-column-weekend",
            this.mDaysOffArray.includes(dayDate.weekday)
          );

          let isToday = dayDate.compare(today) == 0;
          dayCol.column.timeIndicatorBox.hidden = !isToday;
          dayCol.container.classList.toggle("day-column-today", isToday);
        }
        // Remove excess columns.
        for (let dayCol of this.dayColumns.splice(colIndex)) {
          dayCol.column.endLayoutBatchChange();
          dayCol.container.remove();
        }
      }

      if (changes.rotated) {
        this.rotationChanged = true;
        for (let dayCol of this.dayColumns) {
          dayCol.column.setAttribute("orient", orient);
        }
      }

      if (changes.context) {
        for (let dayCol of this.dayColumns) {
          dayCol.column.setAttribute("context", context);
          dayCol.column.setAttribute("item-context", itemContext);
        }
      }

      // Let the columns relayout themselves before we readjust the view.
      for (let dayCol of this.dayColumns) {
        dayCol.column.endLayoutBatchChange();
      }

      if (changes.dates || changes.rotated) {
        // Fix pixels-per-minute and headers, now or when next visible.
        this.readjustView(false, false, scrollMinute);
      }

      // Store the start and end of current view. Next time when
      // setDateRange is called, it will use mViewStart and mViewEnd to
      // check if view range has been changed.
      this.mViewStart = this.mStartDate;
      this.mViewEnd = this.mEndDate;

      let toggleStatus = 0;

      if (this.mTasksInView) {
        toggleStatus |= this.mToggleStatusFlag.TasksInView;
      }
      if (this.mWorkdaysOnly) {
        toggleStatus |= this.mToggleStatusFlag.WorkdaysOnly;
      }
      if (this.mShowCompleted) {
        toggleStatus |= this.mToggleStatusFlag.ShowCompleted;
      }

      this.mToggleStatus = toggleStatus;
      if (changes.dates) {
        // Fetch new items for the new dates.
        this.refreshItems(true);
      }
    }

    /**
     * Return the column object for a given date.
     *
     * @param {calIDateTime} date - A date.
     * @returns {?DateColumn} A column object.
     */
    findColumnForDate(date) {
      for (const col of this.dayColumns) {
        if (col.date.compare(date) == 0) {
          return col;
        }
      }
      return null;
    }

    /**
     * Return the day box (column header) for a given date.
     *
     * @param {calIDateTime} date - A date.
     * @returns {Element} A `calendar-header-container` where "all day" events appear.
     */
    findDayBoxForDate(date) {
      const col = this.findColumnForDate(date);
      return col && col.header;
    }

    /**
     * Return the column objects for a given calendar item.
     *
     * @param {calIItemBase} item - A calendar item.
     * @returns {DateColumn[]} An array of column objects.
     */
    findColumnsForItem(item) {
      const columns = [];

      if (!this.dayColumns.length) {
        return columns;
      }

      // Note that these may be dates or datetimes.
      const startDate = item.startDate || item.entryDate || item.dueDate;
      if (!startDate) {
        return columns;
      }
      const timezone = this.dayColumns[0].date.timezone;
      let targetDate = startDate.getInTimezone(timezone);
      let finishDate = (item.endDate || item.dueDate || item.entryDate || startDate).getInTimezone(
        timezone
      );

      if (targetDate.compare(this.mStartDate) < 0) {
        targetDate = this.mStartDate.clone();
      }

      if (finishDate.compare(this.mEndDate) > 0) {
        finishDate = this.mEndDate.clone();
        finishDate.day++;
      }

      // Set the time to 00:00 so that we get all the boxes.
      targetDate.isDate = false;
      targetDate.hour = 0;
      targetDate.minute = 0;
      targetDate.second = 0;

      if (targetDate.compare(finishDate) == 0) {
        // We have also to handle zero length events in particular for
        // tasks without entry or due date.
        const col = this.findColumnForDate(targetDate);
        if (col) {
          columns.push(col);
        }
      }

      while (targetDate.compare(finishDate) == -1) {
        const col = this.findColumnForDate(targetDate);

        // This might not exist if the event spans the view start or end.
        if (col) {
          columns.push(col);
        }
        targetDate.day += 1;
      }

      return columns;
    }

    /**
     * Get an ordered list of all the calendar-event-column elements in this
     * view.
     *
     * @returns {MozCalendarEventColumn[]} - The columns in this view.
     */
    getEventColumns() {
      return Array.from(this.dayColumns, col => col.column);
    }

    /**
     * Find the calendar-event-column that contains the given node.
     *
     * @param {Node} node - The node to search for.
     *
     * @returns {?MozCalendarEventColumn} - The column that contains the node, or
     *   null if none do.
     */
    findEventColumnThatContains(node) {
      return this.dayColumns.find(col => col.column.contains(node))?.column;
    }

    /**
     * Display a calendar item.
     *
     * @param {calIItemBase} event - A calendar item.
     */
    doAddItem(event) {
      const cols = this.findColumnsForItem(event);
      if (!cols.length) {
        return;
      }

      for (const col of cols) {
        const estart = event.startDate || event.entryDate || event.dueDate;

        if (estart.isDate) {
          this.doResizingHeaderOperation(col.header, () => col.header.addEvent(event));
        } else {
          col.column.addEvent(event);
        }
      }
    }

    /**
     * Remove a calendar item so it is no longer displayed.
     *
     * @param {calIItemBase} event - A calendar item.
     */
    doRemoveItem(event) {
      const cols = this.findColumnsForItem(event);
      if (!cols.length) {
        return;
      }

      const oldLength = this.mSelectedItems.length;
      this.mSelectedItems = this.mSelectedItems.filter(item => {
        return item.hashId != event.hashId;
      });

      for (const col of cols) {
        const estart = event.startDate || event.entryDate || event.dueDate;

        if (estart.isDate) {
          this.doResizingHeaderOperation(col.header, () => col.header.deleteEvent(event));
        } else {
          col.column.deleteEvent(event);
        }
      }

      // If a deleted event was selected, we need to announce that the selection changed.
      if (oldLength != this.mSelectedItems.length) {
        this.fireEvent("itemselect", this.mSelectedItems);
      }
    }

    // CalendarFilteredViewMixin implementation.

    /**
     * Removes all items so they are no longer displayed.
     */
    clearItems() {
      for (let dayCol of this.dayColumns) {
        dayCol.column.clear();
        dayCol.header.clear();
      }
    }

    /**
     * Remove all items for a given calendar so they are no longer displayed.
     *
     * @param {string} calendarId - The ID of the calendar to remove items from.
     */
    removeItemsFromCalendar(calendarId) {
      for (const col of this.dayColumns) {
        // Get all-day events in column header and events within the column.
        const colEvents = col.header.getAllEventItems().concat(col.column.getAllEventItems());

        for (const event of colEvents) {
          if (event.calendar.id == calendarId) {
            this.doRemoveItem(event);
          }
        }
      }
    }

    // End of CalendarFilteredViewMixin implementation.

    /**
     * Clear the pending magic scroll update method.
     */
    clearMagicScroll() {
      if (this.magicScrollTimer) {
        clearTimeout(this.magicScrollTimer);
        this.magicScrollTimer = null;
      }
    }

    /**
     * Get the amount to scroll the view by.
     *
     * @param {number} startDiff - The number of pixels the mouse is from the
     *   starting edge.
     * @param {number} endDiff - The number of pixels the mouse is from the
     *   ending edge.
     * @param {number} scrollzone - The number of pixels from the edge at which
     *   point scrolling is triggered.
     * @param {number} factor - The number of pixels to scroll by if touching
     *   the edge.
     *
     * @returns {number} - The number of pixels to scroll by scaled by the depth
     *   within the scrollzone. Zero if outside the scrollzone, negative if
     *   we're closer to the starting edge and positive if we're closer to the
     *   ending edge.
     */
    getScrollBy(startDiff, endDiff, scrollzone, factor) {
      if (startDiff >= scrollzone && endDiff >= scrollzone) {
        return 0;
      } else if (startDiff < endDiff) {
        return Math.floor((-1 + startDiff / scrollzone) * factor);
      }
      return Math.ceil((1 - endDiff / scrollzone) * factor);
    }

    /**
     * Start scrolling the view if the given positions are close to or beyond
     * its edge.
     *
     * Note, any pending updater sent to this method previously will be
     * cancelled.
     *
     * @param {number} clientX - The horizontal viewport position.
     * @param {number} clientY - The vertical viewport position.
     * @param {Function} updater - A method to call, with some delay, if we
     *   scroll successfully.
     */
    setupMagicScroll(clientX, clientY, updater) {
      this.clearMagicScroll();

      // If we are at the bottom or top of the view (or left/right when
      // rotated), calculate the difference and start accelerating the
      // scrollbar.
      let scrollArea = this.getScrollAreaRect();

      // Distance the mouse is from the edge.
      let diffTop = Math.max(clientY - scrollArea.top, 0);
      let diffBottom = Math.max(scrollArea.bottom - clientY, 0);
      let diffLeft = Math.max(clientX - scrollArea.left, 0);
      let diffRight = Math.max(scrollArea.right - clientX, 0);

      // How close to the edge we need to be to trigger scrolling.
      let primaryZone = 50;
      let secondaryZone = 20;
      // How many pixels to scroll by.
      let primaryFactor = Math.max(4 * this.pixelsPerMinute, 8);
      let secondaryFactor = 4;

      let left;
      let top;
      if (this.getAttribute("orient") == "horizontal") {
        left = this.getScrollBy(diffLeft, diffRight, primaryZone, primaryFactor);
        top = this.getScrollBy(diffTop, diffBottom, secondaryZone, secondaryFactor);
      } else {
        top = this.getScrollBy(diffTop, diffBottom, primaryZone, primaryFactor);
        left = this.getScrollBy(diffLeft, diffRight, secondaryZone, secondaryFactor);
      }

      if (top || left) {
        this.grid.scrollBy({ top, left, behaviour: "smooth" });
        this.magicScrollTimer = setTimeout(updater, 20);
      }
    }

    /**
     * Get the position of the view's scrollable area (the padding area minus
     * sticky headers and scrollbars) in the viewport.
     *
     * @returns {{top: number, bottom: number, left: number, right: number}} -
     *   The viewport positions of the respective scrollable area edges.
     */
    getScrollAreaRect() {
      // We want the viewport coordinates of the view's scrollable area. This is
      // the same as the padding area minus the sticky headers and scrollbars.
      let scrollTop;
      let scrollBottom;
      let scrollLeft;
      let scrollRight;
      let view = this.grid;
      let viewRect = view.getBoundingClientRect();
      let headerRect = this.headerCorner.getBoundingClientRect();

      // paddingTop is the top of the view's padding area. We translate from
      // the border area of the view to the padding area by adding clientTop,
      // which is the view's top border width.
      let paddingTop = viewRect.top + view.clientTop;
      // The top of the scroll area is the bottom of the sticky header.
      scrollTop = headerRect.bottom;
      // To get the bottom we add the clientHeight, which is the height of the
      // padding area minus the scrollbar.
      scrollBottom = paddingTop + view.clientHeight;

      // paddingLeft is the left of the view's padding area. We translate from
      // the border area to the padding area by adding clientLeft, which is the
      // left border width (plus the scrollbar in right-to-left).
      let paddingLeft = viewRect.left + view.clientLeft;
      if (document.dir == "rtl") {
        scrollLeft = paddingLeft;
        // The right of the scroll area is the left of the sticky header.
        scrollRight = headerRect.left;
      } else {
        // The left of the scroll area is the right of the sticky header.
        scrollLeft = headerRect.right;
        // To get the right we add the clientWidth, which is the width of the
        // padding area minus the scrollbar.
        scrollRight = paddingLeft + view.clientWidth;
      }
      return { top: scrollTop, bottom: scrollBottom, left: scrollLeft, right: scrollRight };
    }

    /**
     * Scroll the view to a given minute.
     *
     * @param {number} minute - The minute to scroll to.
     */
    scrollToMinute(minute) {
      let pos = Math.round(Math.max(0, minute) * this.pixelsPerMinute);
      if (this.getAttribute("orient") == "horizontal") {
        this.grid.scrollLeft = document.dir == "rtl" ? -pos : pos;
      } else {
        this.grid.scrollTop = pos;
      }
      // NOTE: this.scrollMinute is set by the "scroll" callback.
      // This means that if we tried to scroll further than possible, the
      // scrollMinute will be capped.
      // Also, if pixelsPerMinute < 1, then scrollMinute may differ from the
      // given 'minute' due to rounding errors.
    }

    /**
     * Set the hours when the day starts and ends.
     *
     * @param {number} dayStartHour - Hour at which the day starts.
     * @param {number} dayEndHour - Hour at which the day ends.
     */
    setDayStartEndHours(dayStartHour, dayEndHour) {
      if (dayStartHour < 0 || dayStartHour > dayEndHour || dayEndHour > 24) {
        throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
      }
      this.dayStartHour = dayStartHour;
      this.dayEndHour = dayEndHour;
      // Also update on the timebar.
      for (let [hour, hourBox] of this.hourBoxes.entries()) {
        hourBox.classList.toggle(
          "multiday-hour-box-off-time",
          hour < dayStartHour || hour >= dayEndHour
        );
      }
      for (let dayCol of this.dayColumns) {
        dayCol.column.setDayStartEndHours(dayStartHour, dayEndHour);
      }
    }

    /**
     * Set how many hours are visible in the scrollable area.
     *
     * @param {number} hours - The number of visible hours.
     */
    setVisibleHours(hours) {
      if (hours <= 0 || hours > 24) {
        throw Components.Exception("", Cr.NS_ERROR_INVALID_ARG);
      }
      this.visibleHours = hours;
    }
  }

  MozElements.CalendarMultidayBaseView = CalendarMultidayBaseView;
}