-
Notifications
You must be signed in to change notification settings - Fork 2
/
Calendar.js
1385 lines (1131 loc) · 43.3 KB
/
Calendar.js
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
/*
Copyright © 2006, 2008 Apple Inc. All rights reserved.
NOTE: Use of this source code is subject to the terms of the Software
License Agreement for Mac OS X, which accompanies the code. Your use
of this source code signifies your agreement to such license terms and
conditions. Except as expressly granted in the Software License Agreement
for Mac OS X, no other copyright, patent, or other intellectual property
license or right is granted, either expressly or by implication, by Apple.
*/
//
// Terminology:
//
// "Day view" refers to the smallest size, with only the day-of-week and date-of-month number showing.
// "Day/month view" refers to the medium size, which adds a calendar mini-month showing.
// "Day/month/event view" refers to the largest size, which adds a listing of upcoming iCal events.
//
// A "date id" (see idFromMonthDate()) is an html ID of the form "0102" (mmdd) that is used to refer to the
// dynamically-generated selectable calendar days in the grid.
//
var kWidgetDayViewWidth = 167;
var kWidgetDayMonthViewWidth = 333;
var kWidgetDayMonthEventViewWidth = 485;
var kWidgetHeight = 163;
var kEventViewContentHeight = 129;
var kDayViewMiddleContainerWidth = 155;
var kDayViewMiddleFillWidth = 142;
var kDayViewRightContentLeftPosition = 178;
var kDayMonthViewMiddleContainerWidth = 324;
var kDayMonthViewMiddleFillWidth = 311;
var kDayMonthViewRightContentLeftPosition = 151;
var kDayMonthEventViewMiddleContainerWidth = 474;
var kDayMonthEventViewMiddleFillWidth = 461;
var kDayMonthEventViewRightContentLeftPosition = 152;
var kDayView = "kDayView";
var kDayMonthView = "kDayMonthView";
var kDayMonthEventView = "kDayMonthEventView";
var kCurrentViewPrefKey = "currentView";
var gResizeAnimation = {startTime:0, duration:250, positionFrom:0, positionTo:0, positionNow:0, timer:null, elementMiddleContainer:null, elementMiddleFill:null, elementRightSideContent:null, elementMonthContent:null, elementEventContent:null, onfinished:null};
var gDate = new Date ();
var gBroswingJSDate = gDate;
var gBrowsingMonth = gDate.getMonth ();
var gBrowsingYear = gDate.getFullYear ();
var gBrowsingDate = gDate.getDate ();
var gCurrentView = kDayMonthView;
var gUpdateDateTimer = null;
var gButtonBeingTracked = null;
var gWidgetVisible = false;
var gViewChanging = false;
var gEventScrollArea = null;
var gEventScrollBar = null;
var kCFCalendarUnitEra = 1;
var kCFCalendarUnitYear = 2;
var kCFCalendarUnitMonth = 3;
var kCFCalendarUnitDay = 4;
var kCFCalendarUnitHour = 5;
var kCFCalendarUnitMinute = 6;
var kCFCalendarUnitSecond = 7;
var kCFCalendarUnitWeek = 8;
var kCFCalendarUnitWeekday = 9;
var kCFCalendarUnitWeekdayOrdinal = 10;
function keyPressed(e)
{
if (gCurrentView == kDayMonthView || gCurrentView == kDayMonthEventView)
{
if (window.Calendar)
{
var mo = new Date(gBrowsingJSDate);
var dateChanged = false;
switch (e.keyIdentifier) {
case "Up":
mo = new Date(Calendar.addDeltaForUnitToDate(-1, kCFCalendarUnitYear, gBrowsingJSDate.getTime()));
dateChanged = true;
break;
case "Down":
mo = new Date(Calendar.addDeltaForUnitToDate(1, kCFCalendarUnitYear, gBrowsingJSDate.getTime()));
dateChanged = true;
break;
case "Left":
case "PageUp":
mo = new Date(Calendar.addDeltaForUnitToDate(-1, kCFCalendarUnitMonth, gBrowsingJSDate.getTime()));
dateChanged = true;
break;
case "Right":
case "PageDown":
mo = new Date(Calendar.addDeltaForUnitToDate(1, kCFCalendarUnitMonth, gBrowsingJSDate.getTime()));
dateChanged = true;
break;
case "Home":
mo = new Date();
dateChanged = true;
break;
}
}
else
{
if (!gBrowsingDate)
gBrowsingDate = 1;
var mo = new Date(gBrowsingYear, gBrowsingMonth, gBrowsingDate);
var dateChanged = false;
switch (e.keyIdentifier) {
case "Up":
gBrowsingYear--;
if (gBrowsingYear < 1753) // assuming current calendar is Gregorian (not necessarily true), this
gBrowsingYear = 1753; // prevents incorrect viewing of September 1752 and before, including B.C.
mo.setFullYear(gBrowsingYear);
dateChanged = true;
break;
case "Down":
gBrowsingYear++;
mo.setFullYear(gBrowsingYear);
dateChanged = true;
break;
case "Left":
case "PageUp":
gBrowsingMonth--;
if (gBrowsingMonth < 0) {
gBrowsingMonth = 11;
gBrowsingYear--;
if (gBrowsingYear < 1753) // pin backwards monthwise browsing at January 1753; see note above
{
gBrowsingMonth = 0;
gBrowsingYear = 1753;
}
}
mo.setMonth(gBrowsingMonth);
mo.setFullYear(gBrowsingYear);
dateChanged = true;
break;
case "Right":
case "PageDown":
gBrowsingMonth++;
if (gBrowsingMonth > 11) {
gBrowsingMonth = 0;
gBrowsingYear++;
}
mo.setMonth(gBrowsingMonth);
mo.setFullYear(gBrowsingYear);
dateChanged = true;
break;
case "Home":
mo = new Date();
dateChanged = true;
break;
}
}
if (dateChanged)
{
// create the new calendar
if (window.Calendar)
{
gBrowsingYear = Calendar.getUnitForDate(kCFCalendarUnitYear, mo.getTime());
gBrowsingMonth = Calendar.getUnitForDate(kCFCalendarUnitMonth, mo.getTime())-1;
gBrowsingDate = Calendar.getUnitForDate(kCFCalendarUnitDay, mo.getTime());
}
else
{
gBrowsingMonth = mo.getMonth ();
gBrowsingYear = mo.getFullYear ();
gBrowsingDate = mo.getDate ();
}
gBrowsingJSDate = mo;
if (gCurrentView != kDayView)
drawGrid(gBrowsingJSDate);
gBrowsingDate = 0; // note this makes the id of the form mm00yyyy which doesn't correspond to an HTML id
drawText ();
e.stopPropagation();
e.preventDefault();
}
}
}
function limit_3 (a, b, c)
{
return a < b ? b : (a > c ? c : a);
}
function computeNextFloat (from, to, ease)
{
return from + (to - from) * ease;
}
function toggleView (inDoItSlow)
{
// Don't allow an animation to start if a previous one isn't done.
if (gViewChanging) return;
gViewChanging = true;
// Update gCurrentView.
if (gCurrentView == kDayView)
gCurrentView = kDayMonthView;
else if (gCurrentView == kDayMonthView)
gCurrentView = kDayMonthEventView;
else if (gCurrentView == kDayMonthEventView)
gCurrentView = kDayView;
var midContDiv = document.getElementById ("Background-MiddleContainer");
var midFillDiv = document.getElementById ("Background-MiddleFill");
var rightSideContDiv = document.getElementById ("AllContent-RightSide-Container");
var monthContDiv = document.getElementById ("AllContent-Month-Container");
var eventContDiv = document.getElementById ("AllContent-Event-Container");
var timeNow = (new Date).getTime();
var multiplier = (inDoItSlow ? 10 : 1); // enable slo-mo
var startingSize = parseInt (midContDiv.clientWidth, 10); // actual current size
var endingSize;
if (gCurrentView == kDayView) {
// Don't resize window when shrinking widget.
endingSize = kDayViewMiddleContainerWidth;
} else if (gCurrentView == kDayMonthView) {
endingSize = kDayMonthViewMiddleContainerWidth;
window.resizeTo(kWidgetDayMonthViewWidth, kWidgetHeight);
} else if (gCurrentView == kDayMonthEventView) {
endingSize = kDayMonthEventViewMiddleContainerWidth;
window.resizeTo(kWidgetDayMonthEventViewWidth, kWidgetHeight);
}
gResizeAnimation.elementMiddleContainer = midContDiv; // for referral during animation
gResizeAnimation.elementMiddleFill = midFillDiv;
gResizeAnimation.elementRightSideContent = rightSideContDiv;
gResizeAnimation.elementMonthContent = monthContDiv;
gResizeAnimation.elementEventContent = eventContDiv;
gResizeAnimation.duration = 250 * multiplier;
gResizeAnimation.positionFrom = startingSize;
gResizeAnimation.positionTo = endingSize;
gResizeAnimation.startTime = timeNow - 13; // set it back one frame
gResizeAnimation.onfinished = viewDidChange;
gResizeAnimation.timer = setInterval ("toggleViewAnimate();", 13);
toggleViewAnimate ();
}
function toggleViewAnimate ()
{
var T;
var ease;
var time = (new Date).getTime();
var xLoc;
var frame;
var rightContentClippingOnItsLeft, rightContentClippingOnItsRight;
// Note: while the resizing of the widget is in motion due to this routine executing, mouseclicks
// may cause dashboard to be dismissed. It seems to be due to clicks coming into dashboard while
// the elements' width are being changed. This happens whether we set the element.style.width
// directly or if we use element.style.setProperty().
T = limit_3(time-gResizeAnimation.startTime, 0, gResizeAnimation.duration);
ease = 0.5 - (0.5 * Math.cos(Math.PI * T / gResizeAnimation.duration));
if (T >= gResizeAnimation.duration)
{
xLoc = gResizeAnimation.positionTo;
clearInterval (gResizeAnimation.timer);
gResizeAnimation.timer = null;
if (gResizeAnimation.onfinished)
setTimeout ("gResizeAnimation.onfinished();", 0); // call after the last frame is drawn
}
else
xLoc = computeNextFloat(gResizeAnimation.positionFrom, gResizeAnimation.positionTo, ease);
// Convert float to integer; old parseInt was like a round-down (floor), now Math.round rounds up:
//
gResizeAnimation.positionNow = Math.round(xLoc);
// Affect the graphics to cause a frame of horizontal visual change: the main container that carries
// with it the right endcap:
//
gResizeAnimation.elementMiddleContainer.style.width = gResizeAnimation.positionNow + "px";
// The endcap is in the correct place before/during/after the move, but the background, whose width is
// otherwise linked to that of the middle container, would be too wide and stick out beyond it (we don't
// want to define its parent, the middle containter, to be smaller), so adjust it manually:
//
gResizeAnimation.elementMiddleFill.style.width = (gResizeAnimation.positionNow - 13) + "px";
// Clip and move the necessary panes so we get a drawer appearance.
if (gCurrentView == kDayView) {
// Collapse month and event views simultaneously by clipping and moving them both.
// Month view.
gResizeAnimation.elementMonthContent.style.left = (gResizeAnimation.positionNow - 323) + "px";
rightContentClippingOnItsLeft = kDayMonthEventViewMiddleContainerWidth - gResizeAnimation.positionNow;
gResizeAnimation.elementMonthContent.style.clip = "rect(0,999,999," + rightContentClippingOnItsLeft + ")";
// Event view.
gResizeAnimation.elementEventContent.style.left = (gResizeAnimation.positionNow - 154) + "px";
rightContentClippingOnItsLeft = 305 - gResizeAnimation.positionNow;
gResizeAnimation.elementEventContent.style.clip = "rect(0,999,999," + rightContentClippingOnItsLeft + ")";
} else if (gCurrentView == kDayMonthView) {
// Expand month view.
gResizeAnimation.elementMonthContent.style.left = (gResizeAnimation.positionNow - 173) + "px";
rightContentClippingOnItsLeft = kDayMonthViewMiddleContainerWidth - gResizeAnimation.positionNow;
gResizeAnimation.elementMonthContent.style.clip = "rect(0,999,999," + rightContentClippingOnItsLeft + ")";
} else if (gCurrentView == kDayMonthEventView) {
// Expand event view.
gResizeAnimation.elementEventContent.style.left = (gResizeAnimation.positionNow - 154) + "px";
rightContentClippingOnItsLeft = kDayMonthEventViewMiddleContainerWidth - gResizeAnimation.positionNow;
gResizeAnimation.elementEventContent.style.clip = "rect(0,999,999," + rightContentClippingOnItsLeft + ")";
}
}
function viewDidChange ()
{
// Set the overall widget's window size so that screen captures, mouseover for the close box, etc, work
// correctly for the resized widget (for example, don't reference empty space where the expanded view was).
// We do this when the animation is done, rather than during, for smoothness reasons. If we expanded then
// this was already done; if we collapsed then reduce the window size now. We use clipping to simplify animation
// code elsewhere:
//
if (gCurrentView == kDayView) {
document.getElementById("AllContent-Month-Container").style.clip = "rect(0,0,0,0)";
window.resizeTo(kWidgetDayViewWidth, kWidgetHeight);
} else if (gCurrentView == kDayMonthView) {
document.getElementById("AllContent-Event-Container").style.clip = "rect(0,0,0,0)";
} else if (gCurrentView == kDayMonthEventView) {
initEventScrollArea();
drawUpcomingCalEvents();
}
gViewChanging = false;
// After a collapse/expand operation we always want to snap back to the current date:
jumpToToday ();
drawText ();
if (window.widget)
widget.setPreferenceForKey (gCurrentView.toString(), createKey(kCurrentViewPrefKey));
}
function idFromMonthDate (month, dateCount)
{
return ((month<10) ? "0" + month : month.toString()) +
((dateCount<10) ? "0" + dateCount : dateCount.toString());
}
function setupWeekHeader ()
//
// Get localized names of the narrow days of the week from the plugin and populate the Expanded View's calendar-grid's header:
//
{
for (var i = 0; i <= 6; i++)
{
document.getElementById ("DayOfWeek-" + i).innerText = fetchIPrefNarrowDayOfWeekName (i);
}
}
function myGetDayFromDate (inDate)
//
// JavaScript's Date.getDay() is Sunday-based, so we have to fix it up.
//
{
var weekDay;
if (window.Calendar)
{
weekDay = Calendar.getUnitForDate(kCFCalendarUnitWeekday, inDate.getTime())-1;
}
else
{
weekDay = inDate.getDay();
}
var dayIndex = weekDay - fetchIPrefFirstWeekday();
if (dayIndex < 0)
dayIndex += 7;
return dayIndex;
}
function calculateNumberOfDaysInPreviousMonth (inJSDate)
//
// Return the number of days in the month that is previous to the one given, going into the previous year if needed.
//
{
var returnValue;
if (window.Calendar)
{
var priorMonthDate = new Date(Calendar.addDeltaForUnitToDate(-1, kCFCalendarUnitMonth, inJSDate.getTime()));
returnValue = Calendar.getLimitOfUnitForBiggerUnitForDate(kCFCalendarUnitDay, kCFCalendarUnitMonth, priorMonthDate.getTime());
}
else
{
var monthOfInterestYear, monthOfInterestMonth;
var aTrialDate;
if (inMonth != 0)
{
monthOfInterestYear = inYear;
monthOfInterestMonth = inMonth - 1;
}
else
{
monthOfInterestYear = inYear - 1;
monthOfInterestMonth = 11;
}
aTrialDate = new Date (monthOfInterestYear, monthOfInterestMonth, 1);
for (var i = 27; i < 32; i++)
{
aTrialDate.setDate (i);
if (aTrialDate.getMonth () != monthOfInterestMonth)
break;
else
returnValue = i;
}
}
return returnValue;
}
function drawGrid (inJSDate)
//
// Draw the calendar day-names-and-date-numbers grid.
//
{
var i;
// we will fill this DIV in
var monthDiv = document.getElementById ("Calendar-NumberGrid");
// show the names of the days of the week
setupWeekHeader ();
// clear out any old calendar grid numbers out of the DIV
if (monthDiv.hasChildNodes ())
{
var children = monthDiv.childNodes;
var count = children.length;
for (i = 0; i < count; i++)
{
monthDiv.removeChild (children[0]);
}
}
// create the month grid, filling the DIV
if (window.Calendar)
{
var dateForOurMonth = new Date(Calendar.firstDayOfMonthForDate(inJSDate.getTime()));
var zeroBasedFirstDayOfWeekInOurMonth = myGetDayFromDate (dateForOurMonth);
var numberOfDaysInPreviousMonth = calculateNumberOfDaysInPreviousMonth (inJSDate);
var workingDate = new Date(Calendar.addDeltaForUnitToDate(-zeroBasedFirstDayOfWeekInOurMonth, kCFCalendarUnitDay, dateForOurMonth.getTime()));
var ourMonth = Calendar.getUnitForDate(kCFCalendarUnitMonth, dateForOurMonth.getTime())-1;
for (i = 0; i < 42; i++)
{
var dateSpan = document.createElement ("span");
var date = Calendar.getUnitForDate(kCFCalendarUnitDay, workingDate.getTime());
var dateString = Calendar.getUnitForDateAsString(kCFCalendarUnitDay, workingDate.getTime());
dateSpan.innerHTML = dateString;
if (i < zeroBasedFirstDayOfWeekInOurMonth)
{
// show dimmed numbers of the (final days of the previous month) before the begin of our month
dateSpan.setAttribute ("class", "calendar-dayOfMonthNumberDimmed");
}
else
{
if (ourMonth != Calendar.getUnitForDate(kCFCalendarUnitMonth, workingDate.getTime())-1)
{
// show dimmed numbers of the (first days of the next month) after the end of our month
dateSpan.setAttribute ("class", "calendar-dayOfMonthNumberDimmed");
}
else
{
// show normal numbers of the days in our month
// they are clickable: they trigger event handlers when clicked and have
// a formatted setting for the ID attribute that is used by the even handler to identify the date
dateSpan.setAttribute ("class", "calendar-dayOfMonthNumberNormal");
dateSpan.setAttribute ("id", idFromMonthDate (ourMonth, date));
dateSpan.setAttribute("onmousedown", "selectDate(event, this.id);");
dateSpan.setAttribute("onkeydown", "selectDate(event, this.id);");
// increment: bump the valid date number in our month and bump the special date we are using to see
// when our main loop goes beyond the number of days in our month
}
}
monthDiv.appendChild (dateSpan);
workingDate = new Date(Calendar.addDeltaForUnitToDate(1, kCFCalendarUnitDay, workingDate.getTime()));
}
}
else
{
var dateNumberThatIsValidInOurMonth = 1;
var ourMonth = inJSDate.getMonth();
var dateToCheckDayRangeInOurMonth = new Date (inJSDate.getYear(), ourMonth, dateNumberThatIsValidInOurMonth);
var zeroBasedFirstDayOfWeekInOurMonth = myGetDayFromDate (dateToCheckDayRangeInOurMonth);
var numberOfDaysInPreviousMonth = calculateNumberOfDaysInPreviousMonth (inJSDate);
var dateNumberThatIsValidInNextMonth = 1;
for (i = 0; i < 42; i++)
{
var dateSpan = document.createElement ("span");
if (i < zeroBasedFirstDayOfWeekInOurMonth)
{
// show dimmed numbers of the (final days of the previous month) before the begin of our month
dateSpan.innerHTML = (numberOfDaysInPreviousMonth - zeroBasedFirstDayOfWeekInOurMonth) + i + 1;
dateSpan.setAttribute ("class", "calendar-dayOfMonthNumberDimmed");
// increment: we are waiting for the main loop index (i) to get into range of our month, so do nothing else
}
else
{
if (dateToCheckDayRangeInOurMonth.getMonth () != ourMonth)
{
// show dimmed numbers of the (first days of the next month) after the end of our month
dateSpan.innerHTML = dateNumberThatIsValidInNextMonth;
dateSpan.setAttribute ("class", "calendar-dayOfMonthNumberDimmed");
// increment: only the simple 1,2,3... number of next month now that we are in the beyond-state
dateNumberThatIsValidInNextMonth++;
}
else
{
// show normal numbers of the days in our month
// they are clickable: they trigger event handlers when clicked and have
// a formatted setting for the ID attribute that is used by the even handler to identify the date
dateSpan.innerHTML = dateNumberThatIsValidInOurMonth;
dateSpan.setAttribute ("class", "calendar-dayOfMonthNumberNormal");
dateSpan.setAttribute ("id", idFromMonthDate (ourMonth, dateNumberThatIsValidInOurMonth));
dateSpan.setAttribute("onmousedown", "selectDate(event, this.id);");
dateSpan.setAttribute("onkeydown", "selectDate(event, this.id);");
// increment: bump the valid date number in our month and bump the special date we are using to see
// when our main loop goes beyond the number of days in our month
dateNumberThatIsValidInOurMonth++;
dateToCheckDayRangeInOurMonth.setDate (dateNumberThatIsValidInOurMonth);
}
}
monthDiv.appendChild (dateSpan);
}
}
drawHiliteTodayInGrid ();
}
function changeMonth (inEvent, inMoveNext)
{
if (window.Calendar)
{
if (inMoveNext)
{
gBrowsingJSDate = new Date(Calendar.addDeltaForUnitToDate(1, kCFCalendarUnitMonth, gBrowsingJSDate.getTime()));
}
else
{
gBrowsingJSDate = new Date(Calendar.addDeltaForUnitToDate(-1, kCFCalendarUnitMonth, gBrowsingJSDate.getTime()));
}
gBrowsingYear = Calendar.getUnitForDate(kCFCalendarUnitYear, gBrowsingJSDate.getTime());
gBrowsingMonth = Calendar.getUnitForDate(kCFCalendarUnitMonth, gBrowsingJSDate.getTime())-1;
}
else
{
if (inMoveNext)
{
gBrowsingMonth++;
if (gBrowsingMonth > 11)
{
gBrowsingMonth = 0;
gBrowsingYear++;
}
}
else
{
gBrowsingMonth--;
if (gBrowsingMonth < 0)
{
gBrowsingMonth = 11;
gBrowsingYear--;
if (gBrowsingYear < 1753) // pin backwards monthwise browsing at January 1753; see note above
{
gBrowsingMonth = 0;
gBrowsingYear = 1753;
}
}
}
gBrowsingJSDate = new Date (gBrowsingYear, gBrowsingMonth, gBrowsingDate);
}
// create the new calendar
drawGrid (gBrowsingJSDate);
gBrowsingDate = 0; // clear the selected date
drawText ();
}
function drawHiliteTodayInGrid ()
//
// If the current calendar-grid being shown is of the now-month-and-year, highlight the corresponding date-number in it.
// This shows to the user that today's date is in there. Otherwise, don't, ie, if the user is browsing into
// other months.
//
{
var thisYear;
var thisMonth;
var thisDate;
if (window.Calendar)
{
thisYear = Calendar.getUnitForDate(kCFCalendarUnitYear, gDate.getTime());
thisMonth = Calendar.getUnitForDate(kCFCalendarUnitMonth, gDate.getTime())-1;
thisDate = Calendar.getUnitForDate(kCFCalendarUnitDay, gDate.getTime());
}
else
{
thisYear = gDate.getFullYear();
thisMonth = gDate.getMonth();
thisDate = gDate.getDate();
}
if ((gBrowsingYear == thisYear) && (gBrowsingMonth == thisMonth))
{
var todayCalendarNumbersGridElement = document.getElementById (idFromMonthDate (thisMonth, thisDate));
todayCalendarNumbersGridElement.setAttribute ("class", "calendar-dayOfMonthNumberHighlighted");
}
}
function jumpToToday (inEvent)
{
gDate = new Date ();
gBrowsingJSDate = gDate;
if (window.Calendar)
{
gBrowsingYear = Calendar.getUnitForDate(kCFCalendarUnitYear, gBrowsingJSDate.getTime());
gBrowsingMonth = Calendar.getUnitForDate(kCFCalendarUnitMonth, gBrowsingJSDate.getTime())-1;
gBrowsingDate = Calendar.getUnitForDate(kCFCalendarUnitDay, gBrowsingJSDate.getTime());
}
else
{
gBrowsingMonth = gDate.getMonth ();
gBrowsingYear = gDate.getFullYear ();
gBrowsingDate = gDate.getDate ();
}
if (gCurrentView != kDayView)
drawGrid (gBrowsingJSDate);
drawText ();
}
function selectDate (inEvent, inID)
//
// Can be used to take action when a date is clicked upon.
//
{
}
function fetchIPrefFirstWeekday () // returns integer
//
// Fetch data from this widget's plugin, from International Prefs. Do fallback work if no plugin available.
//
// Get the first-day-of-week, 0-based index.
//
{
if (window.Calendar) // plugin available, and if plugin error will return 0
{
return Calendar.firstWeekday ();
}
else // fallback case for if no plugin or plugin error
{
return 0;
}
}
function fetchIPrefNarrowDayOfWeekName (inDayOfWeekIndex) // returns string
//
// Fetch data from this widget's plugin, from International Prefs. Do fallback work if no plugin available.
//
// Get the (localized customized in International Pref Pane) "narrow" day-of-week, which is usually a single
// character such as "S" for Sunday, etc. The inDayOfWeek input is zero-based and follows USA first-day-of-week
// conventions, even if the first-day-of-week is not Sunday for the current localized settings. Thus: 0=Sunday,
// 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, and 6=Saturday, in a single-character format.
//
{
var returnString = undefined;
if (window.Calendar) // plugin available: attempt to fill the string; will fill with undefined if plugin error
{
returnString = Calendar.localizedNarrowDayOfWeekNameForIndex (inDayOfWeekIndex);
}
if (returnString == undefined) // still undefined: fallback if plugin unvailable or plugin returned null (plugin error)
{
switch (inDayOfWeekIndex)
{
case 0: returnString = "S"; break;
case 1: returnString = "M"; break;
case 2: returnString = "T"; break;
case 3: returnString = "W"; break;
case 4: returnString = "T"; break;
case 5: returnString = "F"; break;
case 6: returnString = "S"; break;
}
}
return returnString;
}
function fetchIPrefLongMixedcaseDayOfWeekName (inDayOfWeekIndex) // returns string
//
// Fetch data from this widget's plugin, from International Prefs. Do fallback work if no plugin available.
//
// Get the (localized customized in International Pref Pane) mixed-case long day name,
// zero-based. For example, 0="Sunday", 1="Monday", etc.
//
{
var returnString = undefined;
if (window.Calendar) // plugin available: attempt to fill the string; will fill with undefined if plugin error
{
returnString = Calendar.localizedWideDayOfWeekNameForIndex (inDayOfWeekIndex);
}
if (returnString == undefined) // still undefined: fallback if plugin unvailable or plugin returned null (plugin error)
{
switch (inDayOfWeekIndex)
{
case 0: returnString = "Sunday"; break;
case 1: returnString = "Monday"; break;
case 2: returnString = "Tuesday"; break;
case 3: returnString = "Wednesday"; break;
case 4: returnString = "Thursday"; break;
case 5: returnString = "Friday"; break;
case 6: returnString = "Saturday"; break;
}
}
return returnString;
}
function fetchIPrefLongMonthNameAndYearNumber (inDate) // returns string
//
// Fetch data from this widget's plugin, from International Prefs. Do fallback work if no plugin available.
//
// Get the (localized customized in International Pref Pane) combined string of the form "January 1900", that is,
// the long mixed-case month name followed by a 4-digit year number (for most locales) and a reversed or otherwise
// modified version of the same (for other locales).
//
{
var returnString = undefined;
if (window.Calendar) // plugin available: attempt to fill the string; will fill with undefined if plugin error
{
returnString = Calendar.localizedWideMonthNameAndYearNumberForDate (inDate.getTime());
}
if (returnString == undefined) // still undefined: fallback if plugin unvailable or plugin returned null (plugin error)
{
switch (inDate.getMonth())
{
case 0: returnString = "January"; break;
case 1: returnString = "February"; break;
case 2: returnString = "March"; break;
case 3: returnString = "April"; break;
case 4: returnString = "May"; break;
case 5: returnString = "June"; break;
case 6: returnString = "July"; break;
case 7: returnString = "August"; break;
case 8: returnString = "September"; break;
case 9: returnString = "October"; break;
case 10: returnString = "November"; break;
case 11: returnString = "December"; break;
}
returnString = returnString + " " + inDate.getYear().toString ();
}
return returnString;
}
function setFontSizeToFitAndCenter (inElement, inStartingFontSize, inLeft, inStartingTop, inMaxWidth, inMaxHeight)
//
// Sets the font size of the given element, while trying to ensure that the string contained within that
// element fits without clipping or spilling in the given width/height pixel size.
//
// The starting font size and top position parameters are given by the caller so that the setting always
// proceeds from an absolute basis. If the requested font size cannot be honored, then the font size and
// top position in the element are repeatedly adjusted until the element's inner text fits (up to a limit).
//
// NOTE: The CSS declaration for the given element must not have explicitly specified a "width" property.
// NOTE: The height parameter is useful even for "single line" cases to prevent wrapping to an undesired
// additional line.
//
{
var measuredWidth;
var measuredHeight;
var newFontSize = inStartingFontSize;
var newTop = inStartingTop;
var numReductions = 0;
while (1)
{
inElement.style.setProperty ("font-size", newFontSize + "px", "important");
inElement.style.setProperty ("top", newTop + "px", "important");
measuredWidth = parseInt (document.defaultView.getComputedStyle (inElement, null).getPropertyValue ("width"), 10);
measuredHeight = parseInt (document.defaultView.getComputedStyle (inElement, null).getPropertyValue ("height"), 10);
inElement.style.setProperty ("left", inLeft + (inMaxWidth / 2) - (measuredWidth / 2) + "px", "important");
if ((measuredWidth > inMaxWidth) || (measuredHeight > inMaxHeight))
{
if (newFontSize <= 6)
{
// this is the limit of of how small we will make it:
break;
}
// adjust the size, then loop around one more time to set it; after a
// few iterations, slow down the vertical adjustment a bit to better
// track smaller font sizes' change; ideally this would be measured:
numReductions++;
newFontSize -= 1;
if (numReductions <= 4) newTop += 1; else newTop += 0.8;
}
else
{
// this is where we exit if the size is acceptable:
break;
}
}
}
function setFontSizeToFitAndCenterFix (inElement, inStartingFontSize, inLeft, inStartingTop, inMaxWidth, inMaxHeight)
//
// This is a workaround for a possible quirk with text measurement. It is desirable to fix the
// underlying problem rather than rely on the loop below, because the change causes a visible size
// change in the text size, yet keeping it hidden and having a setTimeout proc to enable visibility
// clutters our code and causes flashing while browsing. The problem is that in setFontSizeToFit, the
// getComputedStyle (inElement, null).getPropertyValue ("width") returns a different (smaller) value
// the first time than subsequent times. This has to do with when things are rendered async; maybe
// set a proc to be called upon render completion?
//
{
for (var i = 1; i <= 3; i++)
{
setFontSizeToFitAndCenter (inElement, inStartingFontSize, inLeft, inStartingTop, inMaxWidth, inMaxHeight);
}
}
function drawText ()
//
// Updates the non-calendar-grid text strings such as the (day-name), (day-of-month-number), and (monthname+yearnumber).
//
{
var dayOfWeekNameElement = document.getElementById ("Message-DayOfWeekName");
var dateOfMonthNumberElement = document.getElementById ("Message-DateOfMonthNumber");
var monthNameAndYearNumberElement = document.getElementById ("Message-MonthNameAndYearNumber");
var dateNumber;
// This is, ie, "Wednesday" (the day-of-week name) on the left side; it always reflects the current time.
// We do shrink-to-fit if needed: in some locales, this string can grow quite long:
//
dayOfWeekNameElement.innerText = fetchIPrefLongMixedcaseDayOfWeekName (myGetDayFromDate (gDate));
setFontSizeToFitAndCenterFix (dayOfWeekNameElement,
/* starting font size: */ 18,
/* starting left position: */ 7,
/* starting top position: */ 9,
/* max width: */ 134,
/* max height: */ 25);
// This is, ie, "31" (the big date number) on the left side; it always reflects the current time.
// We dynamically adjust details of position and kerning to get the big huge number to look right:
//
if (window.Calendar)
{
dateNumber = Calendar.getUnitForDateAsString(kCFCalendarUnitDay, gDate.getTime());
}
else
{
dateNumber = gDate.getDate ();
}
dateOfMonthNumberElement.innerText = dateNumber;
if (dateNumber <= 9) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style1to9");
else
if (dateNumber == 10) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style10to10");
else
if (dateNumber == 11) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style11to11");
else
if ((dateNumber >= 12) && (dateNumber <= 19)) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style12to19");
else
if (dateNumber == 20) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style20to20");
else
if (dateNumber == 21) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style21to21");
else
if ((dateNumber >= 22) && (dateNumber <= 26)) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style22to26");
else
if ( dateNumber == 27) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style27to27");
else
if ((dateNumber >= 28) && (dateNumber <= 29)) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style28to29");
else
if (dateNumber == 30) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style30to30");
else
if (dateNumber == 31) dateOfMonthNumberElement.setAttribute ("class", "message-dateOfMonthNumber-style31to31");
// This is, ie, "January 1900" (the month + year combination) on the right side; it reflects what is currently being browsed.
// We do shrink-to-fit if needed: in some locales, this string can grow quite long:
//
monthNameAndYearNumberElement.innerText = fetchIPrefLongMonthNameAndYearNumber (gBrowsingJSDate);
setFontSizeToFitAndCenterFix (monthNameAndYearNumberElement,
/* starting font size: */ 12,
/* starting left position: */ 23,
/* starting top position: */ 9,
/* max width: */ 120,
/* max height: */ 25);
}
function getButtonPathFileFromIdState (inId, inShowAsPressed)
{
var outName = undefined;
if ("Button-Left" == inId) outName = "bar_left";
if ("Button-Right" == inId) outName = "bar_right";
if (true == inShowAsPressed) outName += "_pressed";
outName = "Images/" + outName + ".png";
return outName;
}
function mouseDownForButton (inEvent, inThis)
{
inEvent.target.src = getButtonPathFileFromIdState (inThis.id, true);
inEvent.stopPropagation();
inEvent.preventDefault();
document.addEventListener ("mouseup", mouseUpForButtonsInsideOutside, true);