/
kronolith.js
7141 lines (6610 loc) · 268 KB
/
kronolith.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
/**
* kronolith.js - Base application logic.
*
* TODO: loadingImg()
*
* Copyright 2008-2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @author Jan Schneider <jan@horde.org>
*/
/* Kronolith object. */
KronolithCore = {
// Vars used and defaulting to null/false:
// weekSizes, daySizes,
// groupLoading, colorPicker, duration, timeMarker, monthDays,
// allDays, eventsWeek, initialized
view: '',
ecache: $H(),
cacheStart: null,
cacheEnd: null,
holidays: [],
tcache: $H(),
eventsLoading: {},
loading: 0,
viewLoading: [],
fbLoading: 0,
redBoxLoading: false,
date: Date.today(),
tasktype: 'incomplete',
knl: {},
wrongFormat: $H(),
mapMarker: null,
map: null,
mapInitialized: false,
freeBusy: $H(),
search: 'future',
effectDur: 0.4,
macos: navigator.appVersion.indexOf('Mac') != -1,
orstart: null,
orend: null,
lastRecurType: 'None',
uatts: null,
ucb: null,
resourceACCache: { choices: [], map: $H() },
paramsCache: null,
attendees: [],
resources: [],
/**
* The location that was open before the current location.
*
* @var string
*/
lastLocation: '',
/**
* The currently open location.
*
* @var string
*/
openLocation: '',
/**
* The current (main) location.
*
* This is different from openLocation as it isn't updated for any
* locations that are opened in a popup view, e.g. events.
*
* @var string
*/
currentLocation: '',
kronolithBody: $('kronolithBody'),
onException: function(parentfunc, r, e)
{
/* Make sure loading images are closed. */
this.loading--;
if (!this.loading) {
$('kronolithLoading').hide();
}
this.closeRedBox();
HordeCore.notify(HordeCore.text.ajax_error, 'horde.error');
parentfunc(r, e);
},
setTitle: function(title)
{
document.title = Kronolith.conf.name + ' :: ' + title;
return title;
},
// url = (string) URL to redirect to
// hash = (boolean) If true, url is treated as hash information to alter
// on the current page
redirect: function(url, hash)
{
if (hash) {
window.location.hash = escape(url);
window.location.reload();
} else {
HordeCore.redirect(url);
}
},
go: function(fullloc, data)
{
if (!this.initialized) {
this.go.bind(this, fullloc, data).defer();
return;
}
if (this.viewLoading.size()) {
this.viewLoading.push([ fullloc, data ]);
return;
}
var locParts = fullloc.split(':');
var loc = locParts.shift();
if (this.openLocation == fullloc) {
return;
}
this.viewLoading.push([ fullloc, data ]);
if (loc != 'search') {
HordeTopbar.searchGhost.reset();
}
this.switchTaskView(false);
switch (loc) {
case 'day':
case 'week':
case 'workweek':
case 'month':
case 'year':
case 'agenda':
case 'tasks':
this.closeView(loc);
var locCap = loc.capitalize();
$('kronolithNav' + locCap).up().addClassName('horde-active');
switch (loc) {
case 'day':
case 'agenda':
case 'week':
case 'workweek':
case 'month':
case 'year':
var date = locParts.shift();
if (date) {
date = this.parseDate(date);
} else {
date = this.date;
}
if (this.view != 'agenda' &&
this.view == loc &&
date.getYear() == this.date.getYear() &&
((loc == 'year') ||
(loc == 'month' && date.getMonth() == this.date.getMonth()) ||
((loc == 'week' || loc == 'workweek') && date.getRealWeek() == this.date.getRealWeek()) ||
((loc == 'day' || loc == 'agenda') && date.dateString() == this.date.dateString()))) {
this.setViewTitle(date, loc);
this.addHistory(fullloc);
this.loadNextView();
return;
}
this.addHistory(fullloc);
this.view = loc;
this.date = date;
this.updateView(date, loc);
var dates = this.viewDates(date, loc);
this.loadEvents(dates[0], dates[1], loc);
$('kronolithView' + locCap).appear({
duration: this.effectDur,
queue: 'end',
afterFinish: function() {
if (loc == 'week' || loc == 'workweek' || loc == 'day') {
this.calculateRowSizes(loc + 'Sizes', 'kronolithView' + locCap);
if ($('kronolithTimeMarker')) {
this.positionTimeMarker();
}
if ($('kronolithTimeMarker')) {
$('kronolithTimeMarker').show();
}
// Scroll to the work day start time.
$('kronolithView' + locCap).down('.kronolithViewBody').scrollTop = 9 * this[loc + 'Sizes'].height;
}
this.loadNextView();
}.bind(this)
});
$('kronolithLoading' + loc).insert($('kronolithLoading').remove());
this.updateMinical(date, loc);
break;
case 'tasks':
var tasktype = locParts.shift() || this.tasktype;
this.switchTaskView(true);
$('kronolithCurrent')
.update(this.setTitle(Kronolith.text.tasks));
if (this.view == loc && this.tasktype == tasktype) {
this.addHistory(fullloc);
this.loadNextView();
return;
}
if (!$w('all complete incomplete future').include(tasktype)) {
this.loadNextView();
return;
}
this.addHistory(fullloc);
this.view = loc;
this.tasktype = tasktype;
$w('All Complete Incomplete Future').each(function(tasktype) {
$('kronolithTasks' + tasktype).up().removeClassName('horde-active');
});
$('kronolithTasks' + this.tasktype.capitalize()).up().addClassName('horde-active');
this.loadTasks(this.tasktype);
$('kronolithView' + locCap).appear({
duration: this.effectDur,
queue: 'end',
afterFinish: function() {
this.loadNextView();
}.bind(this) });
$('kronolithLoading' + loc).insert($('kronolithLoading').remove());
this.updateMinical(this.date);
break;
default:
if (!$('kronolithView' + locCap)) {
break;
}
this.addHistory(fullloc);
this.view = loc;
$('kronolithView' + locCap).appear({
duration: this.effectDur,
queue: 'end',
afterFinish: function() {
this.loadNextView();
}.bind(this) });
break;
}
break;
case 'search':
var cals = [], time = locParts[0], term = locParts[1],
query = Object.toJSON({ title: term });
if (!($w('all past future').include(time))) {
this.loadNextView();
return;
}
this.addHistory(fullloc);
this.search = time;
$w('All Past Future').each(function(time) {
$('kronolithSearch' + time).up().removeClassName('horde-active');
});
$('kronolithSearch' + this.search.capitalize()).up().addClassName('horde-active');
this.closeView('agenda');
this.view = 'agenda';
this.updateView(null, 'search', term);
$H(Kronolith.conf.calendars).each(function(type) {
$H(type.value).each(function(calendar) {
if (calendar.value.show) {
cals.push(type.key + '|' + calendar.key);
}
});
});
$('kronolithAgendaNoItems').hide();
this.startLoading('search', query);
HordeCore.doAction('searchEvents', {
cals: Object.toJSON(cals),
query: query,
time: this.search
}, {
callback: function(r) {
// Hide spinner.
this.loading--;
if (!this.loading) {
$('kronolithLoading').hide();
}
if (r.view != 'search' ||
r.query != this.eventsLoading.search) {
return;
}
if (Object.isUndefined(r.events)) {
$('kronolithAgendaNoItems').show();
return;
}
delete this.eventsLoading.search;
$H(r.events).each(function(calendars) {
$H(calendars.value).each(function(events) {
this.createAgendaDay(events.key);
$H(events.value).each(function(event) {
event.value.calendar = calendars.key;
event.value.start = Date.parse(event.value.s);
event.value.end = Date.parse(event.value.e);
this.insertEvent(event, events.key, 'agenda');
}, this);
}, this);
}, this);
}.bind(this)
});
$('kronolithViewAgenda').appear({
duration: this.effectDur,
queue: 'end',
afterFinish: function() {
this.loadNextView();
}.bind(this) });
$('kronolithLoadingagenda').insert($('kronolithLoading').remove());
this.updateMinical(this.date);
break;
case 'event':
// Load view first if necessary.
if (!this.view ) {
this.viewLoading.pop();
this.go(Kronolith.conf.login_view);
this.go.bind(this, fullloc, data).defer();
return;
}
if (this.currentLocation == fullloc) {
this.loadNextView();
return;
}
this.addHistory(fullloc, false);
switch (locParts.length) {
case 0:
// New event.
this.editEvent();
break;
case 1:
// New event on a certain date.
this.editEvent(null, null, locParts[0]);
break;
default:
// Editing event.
var date = locParts.pop(),
event = locParts.pop(),
calendar = locParts.join(':');
this.editEvent(calendar, event, date);
break;
}
this.loadNextView();
break;
case 'task':
// Load view first if necessary.
if (!this.view ) {
this.viewLoading.pop();
this.go('tasks');
this.go.bind(this, fullloc, data).defer();
return;
}
this.switchTaskView(true);
switch (locParts.length) {
case 0:
this.addHistory(fullloc, false);
this.editTask();
break;
case 2:
this.addHistory(fullloc, false);
this.editTask(locParts[0], locParts[1]);
break;
}
this.loadNextView();
break;
case 'calendar':
if (!this.view) {
this.viewLoading.pop();
this.go(Kronolith.conf.login_view);
this.go.bind(this, fullloc, data).defer();
return;
}
this.addHistory(fullloc, false);
this.editCalendar(locParts.join(':'));
this.loadNextView();
break;
default:
this.loadNextView();
break;
}
},
/**
* Removes the last loaded view from the stack and loads the last added
* view, if the stack is still not empty.
*
* We want to load views from a LIFO queue, because the queue is only
* building up if the user switches to another view while the current view
* still loads. In that case we can go directly to the most recently
* clicked view and drop the remaining queue.
*/
loadNextView: function()
{
var current = this.viewLoading.shift();
if (this.viewLoading.size()) {
var next = this.viewLoading.pop();
this.viewLoading = [];
if (current[0] != next[0] || current[1] || next[1]) {
this.go(next[0], next[1]);
}
}
},
/**
* Rebuilds one of the calendar views for a new date.
*
* @param Date date The date to show in the calendar.
* @param string view The view that's rebuilt.
* @param mixed data Any additional data that might be required.
*/
updateView: function(date, view, data)
{
this.holidays = [];
switch (view) {
case 'day':
var today = Date.today();
this.dayEvents = [];
this.dayGroups = [];
this.allDayEvents = [];
$('kronolithCurrent')
.update(this.setViewTitle(date, view, data));
$('kronolithViewDay')
.down('.kronolithAllDayContainer')
.store('date', date.dateString());
$('kronolithEventsDay').store('date', date.dateString());
if (date.equals(today)) {
this.addTimeMarker('kronolithEventsDay');
}
break;
case 'week':
case 'workweek':
this.dayEvents = [];
this.dayGroups = [];
this.allDayEvents = [];
this.allDays = {};
this.eventsWeek = {};
var what = view == 'week' ? 'Week' : 'Workweek',
div = $('kronolithEvents' + what).down('div'),
th = $('kronolithView' + what + 'Head').down('.kronolithWeekDay'),
td = $('kronolithView' + what + 'Head').down('tbody td').next('td'),
hourRow = $('kronolithView' + what + 'Body').down('tr'),
dates = this.viewDates(date, view),
today = Date.today(),
day, dateString, i, hourCol;
$('kronolithCurrent')
.update(this.setViewTitle(date, view, data));
for (i = 0; i < 24; i++) {
day = dates[0].clone();
hourCol = hourRow.down('td').next('td');
while (hourCol) {
hourCol.removeClassName('kronolith-today');
if (day.equals(today)) {
hourCol.addClassName('kronolith-today');
}
hourCol = hourCol.next('td');
day.next().day();
}
hourRow = hourRow.next('tr');
}
day = dates[0].clone();
for (i = 0; i < (view == 'week' ? 7 : 5); i++) {
dateString = day.dateString();
this.allDays['kronolithAllDay' + dateString] = td.down('div');
this.eventsWeek['kronolithEvents' + what + dateString] = div;
div.store('date', dateString)
.writeAttribute('id', 'kronolithEvents' + what + dateString);
th.store('date', dateString)
.down('span').update(day.toString('dddd, d'));
td.removeClassName('kronolith-today');
this.allDays['kronolithAllDay' + dateString]
.writeAttribute('id', 'kronolithAllDay' + dateString)
.store('date', dateString);
if (day.equals(today)) {
td.addClassName('kronolith-today');
this.addTimeMarker('kronolithEvents' + what + dateString);
}
new Drop(td.down('div'));
div = div.next('div');
th = th.next('td');
td = td.next('td');
day.next().day();
}
break;
case 'month':
var tbody = $('kronolith-month-body'),
dates = this.viewDates(date, view),
day = dates[0].clone();
$('kronolithCurrent')
.update(this.setViewTitle(date, view, data));
// Remove old rows. Maybe we should only rebuild the calendars if
// necessary.
tbody.childElements().each(function(row) {
if (row.identify() != 'kronolithRowTemplate') {
row.purge();
row.remove();
}
});
// Build new calendar view.
this.monthDays = {};
while (!day.isAfter(dates[1])) {
tbody.insert(this.createWeekRow(day, date.getMonth(), dates).show());
day.next().week();
}
this.equalRowHeights(tbody);
break;
case 'year':
var month;
$('kronolithCurrent').update(this.setViewTitle(date, view, data));
// Build new calendar view.
for (month = 0; month < 12; month++) {
$('kronolithYear' + month).update(this.createYearMonth(date.getFullYear(), month, 'kronolithYear').show());
}
break;
case 'agenda':
case 'search':
// Agenda days are only created on demand, if there are any events
// to add.
if (view == 'agenda') {
var dates = this.viewDates(date, view);
$('kronolithCurrent')
.update(this.setViewTitle(date, view, data));
$('kronolithSearchNavigation').up().up().hide();
} else {
$('kronolithCurrent')
.update(this.setViewTitle(date, view, data));
$('kronolithSearchNavigation').up().up().show();
}
// Remove old rows. Maybe we should only rebuild the calendars if
// necessary.
tbody = $('kronolithViewAgendaBody').childElements().each(function(row) {
if (row.identify() != 'kronolithAgendaTemplate' &&
row.identify() != 'kronolithAgendaNoItems') {
row.purge();
row.remove();
}
});
break;
}
},
/**
* Sets the browser title of the calendar views.
*
* @param Date date The date to show in the calendar.
* @param string view The view that's displayed.
* @param mixed data Any additional data that might be required.
*/
setViewTitle: function(date, view, data)
{
switch (view) {
case 'day':
return this.setTitle(date.toString('D'));
case 'week':
case 'workweek':
var dates = this.viewDates(date, view);
return this.setTitle(dates[0].toString(Kronolith.conf.date_format) + ' - ' + dates[1].toString(Kronolith.conf.date_format));
case 'month':
return this.setTitle(date.toString('MMMM yyyy'));
case 'year':
return this.setTitle(date.toString('yyyy'));
case 'agenda':
var dates = this.viewDates(date, view);
return this.setTitle(dates[0].toString(Kronolith.conf.date_format) + ' - ' + dates[1].toString(Kronolith.conf.date_format));
case 'search':
return this.setTitle(Kronolith.text.searching.interpolate({ term: data })).escapeHTML();
}
},
/**
* Closes the currently active view.
*/
closeView: function(loc)
{
$w('Day Workweek Week Month Year Tasks Agenda').each(function(a) {
a = $('kronolithNav' + a);
if (a) {
a.up().removeClassName('horde-active');
}
});
if (this.view && this.view != loc) {
$('kronolithView' + this.view.capitalize()).fade({
duration: this.effectDur,
queue: 'end'
});
this.view = null;
}
},
/**
* Creates a single row of day cells for usage in the month and multi-week
* views.
*
* @param Date date The first day to show in the row.
* @param integer month The current month. Days not from the current
* month get the kronolith-other-month CSS class
* assigned.
* @param array viewDates Array of Date objects with the start and end
* dates of the view.
*
* @return Element The element rendering a week row.
*/
createWeekRow: function(date, month, viewDates)
{
var day = date.clone(), today = new Date().dateString(),
row, cell, dateString;
// Create a copy of the row template.
row = $('kronolithRowTemplate').clone(true);
row.removeAttribute('id');
// Fill week number and day cells.
cell = row.down()
.setText(date.getRealWeek())
.store('date', date.dateString())
.next();
while (cell) {
dateString = day.dateString();
this.monthDays['kronolithMonthDay' + dateString] = cell;
cell.id = 'kronolithMonthDay' + dateString;
cell.store('date', dateString);
cell.removeClassName('kronolith-other-month').removeClassName('kronolith-today');
if (day.getMonth() != month) {
cell.addClassName('kronolith-other-month');
}
if (dateString == today) {
cell.addClassName('kronolith-today');
}
new Drop(cell);
cell.store('date', dateString)
.down('.kronolith-day')
.store('date', dateString)
.update(day.getDate());
cell = cell.next();
day.add(1).day();
}
return row;
},
/**
* Creates a table row for a single day in the agenda view, if it doesn't
* exist yet.
*
* @param string date The day to show in the row.
*
* @return Element The element rendering a week row.
*/
createAgendaDay: function(date)
{
// Exit if row exists already.
if ($('kronolithAgendaDay' + date)) {
return;
}
// Create a copy of the row template.
var body = $('kronolithViewAgendaBody'),
row = $('kronolithAgendaTemplate').clone(true);
// Fill week number and day cells.
row.store('date', date)
.down()
.setText(this.parseDate(date).toString('D'))
.next()
.writeAttribute('id', 'kronolithAgendaDay' + date);
row.removeAttribute('id');
// Insert row.
var nextRow;
body.childElements().each(function(elm) {
if (elm.retrieve('date') > date) {
nextRow = elm;
throw $break;
}
});
if (nextRow) {
nextRow.insert({ before: row.show() });
} else {
body.insert(row.show());
}
return row;
},
/**
* Creates a table for a single month in the year view.
*
* @param integer year The year.
* @param integer month The month.
* @param string idPrefix If present, each day will get a DOM ID with this
* prefix
*
* @return Element The element rendering a month table.
*/
createYearMonth: function(year, month, idPrefix)
{
// Create a copy of the month template.
var table = $('kronolithYearTemplate').clone(true),
tbody = table.down('tbody');
table.removeAttribute('id');
tbody.writeAttribute('id', 'kronolithYearTable' + month);
// Set month name.
table.down('tr.kronolith-minical-nav th')
.store('date', year.toPaddedString(4) + (month + 1).toPaddedString(2) + '01')
.update(Date.CultureInfo.monthNames[month]);
// Build month table.
this.buildMinical(tbody, new Date(year, month, 1), null, idPrefix, year);
return table;
},
equalRowHeights: function(tbody)
{
var children = tbody.childElements();
children.invoke('setStyle', { height: (100 / (children.size() - 1)) + '%' });
},
/**
* Calculates some dimensions for the day and week view.
*
* @param string storage Property name where the dimensions are stored.
* @param string view DOM node ID of the view.
*/
calculateRowSizes: function(storage, view)
{
if (!Object.isUndefined(this[storage])) {
return;
}
var td = $(view).down('.kronolithViewBody tr td').next('td'),
layout = td.getLayout(),
spacing = td.up('table').getStyle('borderSpacing');
// FIXME: spacing is hardcoded for IE 7 because it doesn't know about
// border-spacing, but still uses it. WTF?
spacing = spacing ? parseInt($w(spacing)[1], 10) : 2;
this[storage] = {};
this[storage].height = layout.get('margin-box-height') + spacing;
this[storage].spacing = this[storage].height - layout.get('padding-box-height') - layout.get('border-bottom');
},
/**
* Adds a horizontal ruler representing the current time to the specified
* container.
*
* @param string|Element The container of the current day.
*/
addTimeMarker: function(container)
{
if ($('kronolithTimeMarker')) {
$('kronolithTimeMarker').remove();
this.timeMarker.stop();
}
$(container).insert(new Element('div', { id: 'kronolithTimeMarker' }).setStyle({ position: 'absolute' }).hide());
this.timeMarker = new PeriodicalExecuter(this.positionTimeMarker.bind(this), 60);
},
/**
* Updates the horizontal ruler representing the current time.
*/
positionTimeMarker: function()
{
var today = Date.today(), now;
switch (this.view) {
case 'day':
if (!this.date.equals(today)) {
$('kronolithTimeMarker').remove();
this.timeMarker.stop();
return;
}
break;
case 'week':
case 'workweek':
if ($('kronolithTimeMarker').up().retrieve('date') != today.dateString()) {
var newContainer = this.eventsWeek['kronolithEvents' + (this.view == 'week' ? 'Week' : 'Workweek') + today.dateString()];
$('kronolithTimeMarker').remove();
if (newContainer) {
this.addTimeMarker(newContainer);
} else {
this.timeMarker.stop();
}
return;
}
break;
default:
$('kronolithTimeMarker').remove();
this.timeMarker.stop();
return;
}
now = new Date();
$('kronolithTimeMarker').setStyle({ top: ((now.getHours() * 60 + now.getMinutes()) * this[this.view + 'Sizes'].height / 60 | 0) + 'px' });
},
/**
* Rebuilds the mini calendar.
*
* @param Date date The date to show in the calendar.
* @param string view The view that's displayed, determines which days in
* the mini calendar are highlighted.
*/
updateMinical: function(date, view)
{
// Update header.
$('kronolithMinicalDate')
.store('date', date.dateString())
.update(date.toString('MMMM yyyy'));
this.buildMinical($('kronolith-minical').down('tbody'), date, view);
},
/**
* Creates a mini calendar suitable for the navigation calendar and the
* year view.
*
* @param Element tbody The table body to add the days to.
* @param Date date The date to show in the calendar.
* @param string view The view that's displayed, determines which days
* in the mini calendar are highlighted.
* @param string idPrefix If present, each day will get a DOM ID with this
* prefix
* @param integer year If present, generating mini calendars for the
* year view of this year.
*/
buildMinical: function(tbody, date, view, idPrefix, year)
{
var dates = this.viewDates(date, 'month'),
day = dates[0].clone(),
date7 = date.clone().add(1).week(),
today = Date.today(),
week = this.viewDates(this.date, 'week'),
workweek = this.viewDates(this.date, 'workweek'),
dateString, td, tr, i;
// Remove old calendar rows. Maybe we should only rebuild the minical
// if necessary.
tbody.childElements().invoke('remove');
for (i = 0; i < 42; i++) {
dateString = day.dateString();
// Create calendar row and insert week number.
if (day.getDay() == Kronolith.conf.week_start) {
tr = new Element('tr');
tbody.insert(tr);
td = new Element('td', { className: 'kronolith-minical-week' })
.store('weekdate', dateString);
td.update(day.getRealWeek());
tr.insert(td);
weekStart = day.clone();
weekEnd = day.clone();
weekEnd.add(6).days();
}
// Insert day cell.
td = new Element('td').store('date', dateString);
if (day.getMonth() != date.getMonth()) {
td.addClassName('kronolith-other-month');
} else if (!Object.isUndefined(idPrefix)) {
td.id = idPrefix + dateString;
}
// Highlight days currently being displayed.
if (view &&
((view == 'month' && this.date.between(dates[0], dates[1])) ||
(view == 'week' && day.between(week[0], week[1])) ||
(view == 'workweek' && day.between(workweek[0], workweek[1])) ||
(view == 'day' && day.equals(this.date)) ||
(view == 'agenda' && !day.isBefore(date) && day.isBefore(date7)))) {
td.addClassName('kronolith-selected');
}
// Highlight today.
if (day.equals(today) &&
(Object.isUndefined(year) ||
(day.getYear() + 1900 == year &&
date.getMonth() == day.getMonth()))) {
td.addClassName('kronolith-today');
}
td.insert(new Element('a').update(day.getDate()));
tr.insert(td);
day.next().day();
}
},
/**
* Inserts a calendar entry in the sidebar menu.
*
* @param string type The calendar type.
* @param string id The calendar id.
* @param object cal The calendar object.
* @param Element div Container DIV where to add the entry (optional).
*/
insertCalendarInList: function(type, id, cal, div)
{
var noItems, calendar, link;
if (!div) {
div = this.getCalendarList(type, cal.owner);
}
noItems = div.previous();
if (noItems &&
noItems.tagName == 'DIV' &&
noItems.className == 'horde-info') {
noItems.hide();
}
link = new Element('span', { className: type != 'resourcegroup' ? (cal.show ? 'horde-resource-on' : 'horde-resource-off') : 'horde-resource-none' })
.insert(cal.name.escapeHTML());
calendar = new Element('div')
.store('calendar', id)
.store('calendarclass', type)
.setStyle({ backgroundColor: cal.bg, color: cal.fg });
if (type != 'holiday' && type != 'external') {
calendar.insert(
new Element('span', { className: 'horde-resource-edit-' + cal.fg.substring(1) })
.setStyle({ backgroundColor: cal.bg, color: cal.fg })
.insert('►'));
}
calendar.insert(
new Element('div', { className: 'horde-resource-link' })
.insert(link));
this.addShareIcon(cal, link);
div.insert(calendar);
if (cal.show) {
this.addCalendarLegend(type, id, cal);
}
},
/**
* Add the share icon after the calendar name in the calendar list.
*
* @param object cal A calendar object from Kronolith.conf.calendars.
* @param Element element The calendar element in the list.
*/
addShareIcon: function(cal, element)
{
if (cal.owner && cal.perms) {
$H(cal.perms).each(function(perm) {
if (perm.key != 'type' &&
((Object.isArray(perm.value) && perm.value.size()) ||
(!Object.isArray(perm.value) && perm.value))) {
element.insert(' ').insert(new Element('img', { src: Kronolith.conf.images.attendees.replace(/fff/, cal.fg.substring(1)), title: Kronolith.text.shared }));
throw $break;
}
});
}
},
/**
* Rebuilds the list of calendars.
*/
updateCalendarList: function()
{
var ext = $H(), extNames = $H(),