-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgantt.js
1711 lines (1527 loc) · 77.3 KB
/
gantt.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
(function ($, undefined) {
"use strict";
var UTC_DAY_IN_MS = 24 * 60 * 60 * 1000;
// custom selector `:findday` used to match on specified day in ms.
//
// The selector is passed a date in ms and elements are added to the
// selection filter if the element date matches, as determined by the
// id attribute containing a parsable date in ms.
function findDay(elt, text) {
var cd = new Date(parseInt(text, 10));
cd.setHours(0, 0, 0, 0);
var id = $(elt).attr("id") || "";
var si = id.indexOf("-") + 1;
var ed = new Date(parseInt(id.substring(si, id.length), 10));
ed.setHours(0, 0, 0, 0);
return cd.getTime() === ed.getTime();
}
$.expr.pseudos.findday = $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
return function(elt) {
return findDay(elt, text);
};
}) :
function(elt, i, match) {
return findDay(elt, match[3]);
};
// custom selector `:findweek` used to match on specified week in ms.
function findWeek(elt, text) {
var cd = new Date(parseInt(text, 10));
var y = cd.getFullYear();
var w = cd.getWeekOfYear();
var m = cd.getMonth();
if (m === 11 && w === 1) {
y++;
} else if (!m && w > 51) {
y--;
}
cd = y + "-" + w;
var id = $(elt).attr("id") || "";
var si = id.indexOf("-") + 1;
var ed = id.substring(si, id.length);
return cd === ed;
}
$.expr.pseudos.findweek = $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
return function(elt) {
return findWeek(elt, text);
};
}) :
function(elt, i, match) {
return findWeek(elt, match[3]);
};
// custom selector `:findmonth` used to match on specified month in ms.
function findMonth(elt, text) {
var cd = new Date(parseInt(text, 10));
cd = cd.getFullYear() + "-" + cd.getMonth();
var id = $(elt).attr("id") || "";
var si = id.indexOf("-") + 1;
var ed = id.substring(si, id.length);
return cd === ed;
}
$.expr[':'].findmonth = $.expr.createPseudo ?
$.expr.createPseudo(function(text) {
return function(elt) {
return findMonth(elt, text);
};
}) :
function(elt, i, match) {
return findMonth(elt, match[3]);
};
// Date prototype helpers
// ======================
// `getWeekId` returns a string in the form of 'dh-YYYY-WW', where WW is
// the week # for the year.
// It is used to add an id to the week divs
Date.prototype.getWeekId = function () {
var y = this.getFullYear();
var w = this.getWeekOfYear();
var m = this.getMonth();
if (m === 11 && w === 1) {
y++;
} else if (!m && w > 51) {
y--;
}
return 'dh-' + y + "-" + w;
};
// `getRepDate` returns the milliseconds since the epoch for a given date
// depending on the active scale
Date.prototype.getRepDate = function (scale) {
switch (scale) {
case "hours":
return this.getTime();
case "weeks":
return this.getDayForWeek().getTime();
case "months":
return new Date(this.getFullYear(), this.getMonth(), 1).getTime();
case "days":
/* falls through */
default:
return this.getTime();
}
};
// `getDayOfYear` returns the day number for the year
Date.prototype.getDayOfYear = function () {
var year = this.getFullYear();
return (Date.UTC(year, this.getMonth(), this.getDate()) -
Date.UTC(year, 0, 0)) / UTC_DAY_IN_MS;
};
// Use ISO week by default
//TODO: make these options.
var firstDay = 1; // ISO week starts with Monday (1); use Sunday (0) for, e.g., North America
var weekOneDate = 4; // ISO week one always contains 4 Jan; use 1 Jan for, e.g., North America
// `getWeekOfYear` returns the week number for the year
//TODO: fix bug when firstDay=6/weekOneDate=1 : https://github.com/moment/moment/issues/2115
Date.prototype.getWeekOfYear = function () {
var year = this.getFullYear(),
month = this.getMonth(),
date = this.getDate(),
day = this.getDay();
//var diff = weekOneDate - day + 7 * (day < firstDay ? -1 : 1);
var diff = weekOneDate - day;
if (day < firstDay) {
diff -= 7;
}
if (diff + 7 < weekOneDate - firstDay) {
diff += 7;
}
return Math.ceil(new Date(year, month, date + diff).getDayOfYear() / 7);
};
// `getDayForWeek` returns the first day of this Date's week
Date.prototype.getDayForWeek = function () {
var day = this.getDay();
var diff = (day < firstDay ? -7 : 0) + firstDay - day;
return new Date( this.getFullYear(), this.getMonth(), this.getDate() + diff );
};
$.fn.gantt = function (options) {
var scales = ["hours", "days", "weeks", "months"];
//Default settings
var settings = {
source: [],
holidays: [],
// paging
itemsPerPage: 7,
// localisation
dow: ["S", "M", "T", "W", "T", "F", "S"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
waitText: "Please wait...",
// navigation
navigate: "buttons",
scrollToToday: true,
// cookie options
useCookie: false,
cookieKey: "jquery.fn.gantt",
// scale parameters
scale: "days",
maxScale: "months",
minScale: "hours",
// callbacks
onItemClick: function (data) { return; },
onAddClick: function (dt, rowId) { return; },
onRender: $.noop
};
// read options
$.extend(settings, options);
// can't use cookie if don't have `$.cookie`
settings.useCookie = settings.useCookie && $.isFunction($.cookie);
// Grid management
// ===============
// Core object is responsible for navigation and rendering
var core = {
// Return the element whose topmost point lies under the given point
// Normalizes for old browsers (NOTE: doesn't work when element is outside viewport)
//TODO: https://github.com/taitems/jQuery.Gantt/issues/137
elementFromPoint: (function(){ // IIFE
// version for normal browsers
if (document.compatMode === "CSS1Compat") {
return function (x, y) {
x -= window.pageXOffset;
y -= window.pageYOffset;
return document.elementFromPoint(x, y);
};
}
// version for older browsers
return function (x, y) {
x -= $(document).scrollLeft();
y -= $(document).scrollTop();
return document.elementFromPoint(x, y);
};
})(),
// **Create the chart**
create: function (element) {
// Initialize data with a json object or fetch via an xhr
// request depending on `settings.source`
if (typeof settings.source !== "string") {
element.data = settings.source;
core.init(element);
} else {
$.getJSON(settings.source, function (jsData) {
element.data = jsData;
core.init(element);
});
}
},
// **Setup the initial view**
// Here we calculate the number of rows, pages and visible start
// and end dates once the data are ready
init: function (element) {
element.rowsNum = element.data.length;
element.pageCount = Math.ceil(element.rowsNum / settings.itemsPerPage);
element.rowsOnLastPage = element.rowsNum - (Math.floor(element.rowsNum / settings.itemsPerPage) * settings.itemsPerPage);
element.dateStart = tools.getMinDate(element);
element.dateEnd = tools.getMaxDate(element);
/* core.render(element); */
core.waitToggle(element, function () { core.render(element); });
},
// **Render the grid**
render: function (element) {
var content = $('<div class="fn-content"/>');
var $leftPanel = core.leftPanel(element);
content.append($leftPanel);
var $rightPanel = core.rightPanel(element, $leftPanel);
var pLeft, hPos;
content.append($rightPanel);
content.append(core.navigation(element));
var $dataPanel = $rightPanel.find(".dataPanel");
element.gantt = $('<div class="fn-gantt" />').append(content);
$(element).empty().append(element.gantt);
element.scrollNavigation.panelMargin = parseInt($dataPanel.css("left").replace("px", ""), 10);
element.scrollNavigation.panelMaxPos = ($dataPanel.width() - $rightPanel.width());
element.scrollNavigation.canScroll = ($dataPanel.width() > $rightPanel.width());
core.markNow(element);
core.fillData(element, $dataPanel, $leftPanel);
// Set a cookie to record current position in the view
if (settings.useCookie) {
var sc = $.cookie(settings.cookieKey + "ScrollPos");
if (sc) {
element.hPosition = sc;
}
}
// Scroll the grid to today's date
if (settings.scrollToToday) {
core.navigateTo(element, 'now');
core.scrollPanel(element, 0);
// or, scroll the grid to the left most date in the panel
} else {
if (element.hPosition !== 0) {
if (element.scaleOldWidth) {
pLeft = ($dataPanel.width() - $rightPanel.width());
hPos = pLeft * element.hPosition / element.scaleOldWidth;
element.hPosition = hPos > 0 ? 0 : hPos;
element.scaleOldWidth = null;
}
$dataPanel.css({ "left": element.hPosition });
element.scrollNavigation.panelMargin = element.hPosition;
}
core.repositionLabel(element);
}
$dataPanel.css({ height: $leftPanel.height() });
core.waitToggle(element);
settings.onRender();
},
// Create and return the left panel with labels
leftPanel: function (element) {
/* Left panel */
var ganttLeftPanel = $('<div class="leftPanel"/>')
.append($('<div class="row spacer"/>')
.css("height", tools.getCellSize() * element.headerRows));
var entries = [];
$.each(element.data, function (i, entry) {
if (i >= element.pageNum * settings.itemsPerPage &&
i < (element.pageNum * settings.itemsPerPage + settings.itemsPerPage)) {
var dataId = ('id' in entry) ? '" data-id="' + entry.id : '';
entries.push(
'<div class="row name row' + i +
(entry.desc ? '' : (' fn-wide '+dataId)) +
'" id="rowheader' + i +
'" data-offset="' + i % settings.itemsPerPage * tools.getCellSize() + '">' +
'<span class="fn-label' +
(entry.cssClass ? ' ' + entry.cssClass : '') + '">' +
(entry.name || '') +
'</span>' +
'</div>');
if (entry.desc) {
entries.push(
'<div class="row desc row' + i +
' " id="RowdId_' + i + dataId + '">' +
'<span class="fn-label' +
(entry.cssClass ? ' ' + entry.cssClass : '') + '">' +
entry.desc +
'</span>' +
'</div>');
}
}
});
return ganttLeftPanel.append(entries.join(""));
},
// Create and return the data panel element
dataPanel: function (element, width) {
var dataPanel = $('<div class="dataPanel" style="width: ' + width + 'px;"/>');
// Handle mousewheel events for scrolling the data panel
var wheel = 'onwheel' in element ?
'wheel' : document.onmousewheel !== undefined ?
'mousewheel' : 'DOMMouseScroll';
$(element).on(wheel, function (e) { core.wheelScroll(element, e); });
// Handle click events and dispatch to registered `onAddClick` function
dataPanel.click(function (e) {
e.stopPropagation();
var corrX/* <- never used? */, corrY;
var leftpanel = $(element).find(".fn-gantt .leftPanel");
var datapanel = $(element).find(".fn-gantt .dataPanel");
switch (settings.scale) {
case "months":
corrY = tools.getCellSize();
break;
case "hours":
corrY = tools.getCellSize() * 4;
break;
case "days":
corrY = tools.getCellSize() * 3;
break;
case "weeks":
/* falls through */
default:
corrY = tools.getCellSize() * 2;
}
/* Adjust, so get middle of elm
corrY -= Math.floor(tools.getCellSize() / 2);
*/
// Find column where click occurred
var col = core.elementFromPoint(e.pageX, datapanel.offset().top + corrY);
// Was the label clicked directly?
if (col.className === "fn-label") {
col = $(col.parentNode);
} else {
col = $(col);
}
var dt = col.data("repdate");
// Find row where click occurred
var row = core.elementFromPoint(leftpanel.offset().left + leftpanel.width() - 10, e.pageY);
// Was the label clicked directly?
if (row.className.indexOf("fn-label") === 0) {
row = $(row.parentNode);
} else {
row = $(row);
}
var rowId = row.data('id');
// Dispatch user registered function with the DateTime in ms
// and the id if the clicked object is a row
settings.onAddClick(dt, rowId);
});
return dataPanel;
},
// Creates and return the right panel containing the year/week/day header
rightPanel: function (element, leftPanel /* <- never used? */) {
var range = null;
// Days of the week have a class of one of
// `sn` (Sunday), `sa` (Saturday), or `wd` (Weekday)
var dowClass = ["sn", "wd", "wd", "wd", "wd", "wd", "sa"];
//unused: was someone planning to allow styles to stretch to the bottom of the chart?
//var gridDowClass = [" sn", "", "", "", "", "", " sa"];
var yearArr = [];
var scaleUnitsThisYear = 0;
var monthArr = [];
var scaleUnitsThisMonth = 0;
var dayArr = [];
var hoursInDay = 0;
var dowArr = [];
var horArr = [];
var today = new Date();
today.setHours(0, 0, 0, 0);
// reused variables
var $row = $('<div class="row header"></div>');
var i, len;
var year, month, week, day;
var rday, dayClass;
var dataPanel, dataPanelWidth;
// Setup the headings based on the chosen `settings.scale`
switch (settings.scale) {
// **Hours**
case "hours":
range = tools.parseTimeRange(element.dateStart, element.dateEnd, element.scaleStep);
dataPanelWidth = range.length * tools.getCellSize();
year = range[0].getFullYear();
month = range[0].getMonth();
day = range[0];
for (i = 0, len = range.length; i < len; i++) {
rday = range[i];
// Fill years
var rfy = rday.getFullYear();
if (rfy !== year) {
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * scaleUnitsThisYear +
'px;"><div class="fn-label">' +
year +
'</div></div>');
year = rfy;
scaleUnitsThisYear = 0;
}
scaleUnitsThisYear++;
// Fill months
var rm = rday.getMonth();
if (rm !== month) {
monthArr.push(
'<div class="row month" style="width: ' +
tools.getCellSize() * scaleUnitsThisMonth + 'px"><div class="fn-label">' +
settings.months[month] +
'</div></div>');
month = rm;
scaleUnitsThisMonth = 0;
}
scaleUnitsThisMonth++;
// Fill days & hours
var rgetDay = rday.getDay();
var getDay = day.getDay();
if (rgetDay !== getDay) {
dayClass = (today - day === 0) ?
"today" : tools.isHoliday( day.getTime() ) ?
"holiday" : dowClass[getDay];
dayArr.push(
'<div class="row date ' + dayClass + '" ' +
'style="width: ' + tools.getCellSize() * hoursInDay + 'px;">' +
'<div class="fn-label">' + day.getDate() + '</div></div>');
dowArr.push(
'<div class="row day ' + dayClass + '" ' +
'style="width: ' + tools.getCellSize() * hoursInDay + 'px;">' +
'<div class="fn-label">' + settings.dow[getDay] + '</div></div>');
day = rday;
hoursInDay = 0;
}
hoursInDay++;
dayClass = dowClass[rgetDay];
if (tools.isHoliday(rday)) {
dayClass = "holiday";
}
horArr.push(
'<div class="row day ' +
dayClass +
'" id="dh-' +
rday.getTime() +
'" data-offset="' + i * tools.getCellSize() +
'" data-repdate="' + rday.getRepDate(settings.scale) +
'"><div class="fn-label">' +
rday.getHours() +
'</div></div>');
}
// Last year
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * scaleUnitsThisYear + 'px;"><div class="fn-label">' +
year +
'</div></div>');
// Last month
monthArr.push(
'<div class="row month" style="width: ' +
tools.getCellSize() * scaleUnitsThisMonth + 'px"><div class="fn-label">' +
settings.months[month] +
'</div></div>');
dayClass = dowClass[day.getDay()];
if ( tools.isHoliday(day) ) {
dayClass = "holiday";
}
dayArr.push(
'<div class="row date ' + dayClass + '" ' +
'style="width: ' + tools.getCellSize() * hoursInDay + 'px;">' +
'<div class="fn-label">' + day.getDate() + '</div></div>');
dowArr.push(
'<div class="row day ' + dayClass + '" ' +
'style="width: ' + tools.getCellSize() * hoursInDay + 'px;">' +
'<div class="fn-label">' + settings.dow[day.getDay()] + '</div></div>');
dataPanel = core.dataPanel(element, dataPanelWidth);
// Append panel elements
dataPanel.append(
$row.clone().html(yearArr.join("")),
$row.clone().html(monthArr.join("")),
$row.clone().html(dayArr.join("")),
$row.clone().html(dowArr.join("")),
$row.clone().html(horArr.join(""))
);
break;
// **Weeks**
case "weeks":
range = tools.parseWeeksRange(element.dateStart, element.dateEnd);
dataPanelWidth = range.length * tools.getCellSize();
year = range[0].getFullYear();
month = range[0].getMonth();
week = range[0].getWeekOfYear();
var diff;
for (i = 0, len = range.length; i < len; i++) {
rday = range[i];
// Fill years
if (week > (week = rday.getWeekOfYear())) {
// partial weeks to subtract from year header
diff = rday.getDate() - 1;
// offset one month (December) if week starts in last year
diff -= !rday.getMonth() ? 0 : 31;
diff /= 7;
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * (scaleUnitsThisYear - diff) +
'px;"><div class="fn-label">' +
year +
'</div></div>');
year++;
scaleUnitsThisYear = diff;
}
scaleUnitsThisYear++;
// Fill months
if (rday.getMonth() !== month) {
// partial weeks to subtract from month header
diff = rday.getDate() - 1;
// offset one week if week starts in last month
//diff -= (diff <= 6) ? 0 : 7;
diff /= 7;
monthArr.push(
'<div class="row month" style="width:' +
tools.getCellSize() * (scaleUnitsThisMonth - diff) +
'px;"><div class="fn-label">' +
settings.months[month] +
'</div></div>');
month = rday.getMonth();
scaleUnitsThisMonth = diff;
}
scaleUnitsThisMonth++;
// Fill weeks
dayArr.push(
'<div class="row day wd"' +
' id="' + rday.getWeekId() +
'" data-offset="' + i * tools.getCellSize() +
'" data-repdate="' + rday.getRepDate(settings.scale) + '">' +
'<div class="fn-label">' + week + '</div></div>');
}
// Last year
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * scaleUnitsThisYear + 'px;"><div class="fn-label">' +
year +
'</div></div>');
// Last month
monthArr.push(
'<div class="row month" style="width: ' +
tools.getCellSize() * scaleUnitsThisMonth + 'px"><div class="fn-label">' +
settings.months[month] +
'</div></div>');
dataPanel = core.dataPanel(element, dataPanelWidth);
// Append panel elements
dataPanel.append(
$row.clone().html(yearArr.join("")),
$row.clone().html(monthArr.join("")),
$row.clone().html(dayArr.join(""))
);
break;
// **Months**
case 'months':
range = tools.parseMonthsRange(element.dateStart, element.dateEnd);
dataPanelWidth = range.length * tools.getCellSize();
year = range[0].getFullYear();
month = range[0].getMonth();
for (i = 0, len = range.length; i < len; i++) {
rday = range[i];
// Fill years
if (rday.getFullYear() !== year) {
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * scaleUnitsThisYear +
'px;"><div class="fn-label">' +
year +
'</div></div>');
year = rday.getFullYear();
scaleUnitsThisYear = 0;
}
scaleUnitsThisYear++;
monthArr.push(
'<div class="row day wd" id="dh-' + tools.genId(rday) +
'" data-offset="' + i * tools.getCellSize() +
'" data-repdate="' + rday.getRepDate(settings.scale) + '">' +
(1 + rday.getMonth()) + '</div>');
}
// Last year
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * scaleUnitsThisYear + 'px;"><div class="fn-label">' +
year +
'</div></div>');
dataPanel = core.dataPanel(element, dataPanelWidth);
// Append panel elements
dataPanel.append(
$row.clone().html(yearArr.join("")),
$row.clone().html(monthArr.join(""))
);
break;
// **Days (default)**
default:
range = tools.parseDateRange(element.dateStart, element.dateEnd);
dataPanelWidth = range.length * tools.getCellSize();
year = range[0].getFullYear();
month = range[0].getMonth();
for (i = 0, len = range.length; i < len; i++) {
rday = range[i];
// Fill years
if (rday.getFullYear() !== year) {
yearArr.push(
'<div class="row year" style="width:' +
tools.getCellSize() * scaleUnitsThisYear +
'px;"><div class="fn-label">' +
year +
'</div></div>');
year = rday.getFullYear();
scaleUnitsThisYear = 0;
}
scaleUnitsThisYear++;
// Fill months
if (rday.getMonth() !== month) {
monthArr.push(
'<div class="row month" style="width:' +
tools.getCellSize() * scaleUnitsThisMonth +
'px;"><div class="fn-label">' +
settings.months[month] +
'</div></div>');
month = rday.getMonth();
scaleUnitsThisMonth = 0;
}
scaleUnitsThisMonth++;
day = rday.getDay();
dayClass = dowClass[day];
if ( tools.isHoliday(rday) ) {
dayClass = "holiday";
}
dayArr.push(
'<div class="row date ' + dayClass + '"' +
' id="dh-' + tools.genId(rday) +
'" data-offset="' + i * tools.getCellSize() +
'" data-repdate="' + rday.getRepDate(settings.scale) + '">' +
'<div class="fn-label">' + rday.getDate() + '</div></div>');
dowArr.push(
'<div class="row day ' + dayClass + '"' +
' id="dw-' + tools.genId(rday) +
'" data-repdate="' + rday.getRepDate(settings.scale) + '">' +
'<div class="fn-label">' + settings.dow[day] + '</div></div>');
} //for
// Last year
yearArr.push(
'<div class="row year" style="width: ' +
tools.getCellSize() * scaleUnitsThisYear + 'px;"><div class="fn-label">' +
year +
'</div></div>');
// Last month
monthArr.push(
'<div class="row month" style="width: ' +
tools.getCellSize() * scaleUnitsThisMonth + 'px"><div class="fn-label">' +
settings.months[month] +
'</div></div>');
dataPanel = core.dataPanel(element, dataPanelWidth);
// Append panel elements
dataPanel.append(
$row.clone().html(yearArr.join("")),
$row.clone().html(monthArr.join("")),
$row.clone().html(dayArr.join("")),
$row.clone().html(dowArr.join(""))
);
}
return $('<div class="rightPanel"></div>').append(dataPanel);
},
// **Navigation**
navigation: function (element) {
var ganttNavigate = null;
// Scrolling navigation is provided by setting
// `settings.navigate='scroll'`
if (settings.navigate === "scroll") {
ganttNavigate = $('<div class="navigate" />')
.append($('<div class="nav-slider" />')
.append($('<div class="nav-slider-left" />')
.append($('<button type="button" class="nav-link nav-page-back"/>')
.html('↑')
.click(function () {
core.navigatePage(element, -1);
}))
.append($('<div class="page-number"/>')
.append($('<span/>')
.html(element.pageNum + 1 + ' / ' + element.pageCount)))
.append($('<button type="button" class="nav-link nav-page-next"/>')
.html('↓')
.click(function () {
core.navigatePage(element, 1);
}))
.append($('<button type="button" class="nav-link nav-now"/>')
.html('●')
.click(function () {
core.navigateTo(element, 'now');
}))
.append($('<button type="button" class="nav-link nav-prev-week"/>')
.html('<<')
.click(function () {
if (settings.scale === 'hours') {
core.navigateTo(element, tools.getCellSize() * 8);
} else if (settings.scale === 'days') {
core.navigateTo(element, tools.getCellSize() * 30);
} else if (settings.scale === 'weeks') {
core.navigateTo(element, tools.getCellSize() * 12);
} else if (settings.scale === 'months') {
core.navigateTo(element, tools.getCellSize() * 6);
}
}))
.append($('<button type="button" class="nav-link nav-prev-day"/>')
.html('<')
.click(function () {
if (settings.scale === 'hours') {
core.navigateTo(element, tools.getCellSize() * 4);
} else if (settings.scale === 'days') {
core.navigateTo(element, tools.getCellSize() * 7);
} else if (settings.scale === 'weeks') {
core.navigateTo(element, tools.getCellSize() * 4);
} else if (settings.scale === 'months') {
core.navigateTo(element, tools.getCellSize() * 3);
}
})))
.append($('<div class="nav-slider-content" />')
.append($('<div class="nav-slider-bar" />')
.append($('<a class="nav-slider-button" />')
)
.mousedown(function (e) {
e.preventDefault();
element.scrollNavigation.scrollerMouseDown = true;
core.sliderScroll(element, e);
})
.mousemove(function (e) {
if (element.scrollNavigation.scrollerMouseDown) {
core.sliderScroll(element, e);
}
})
)
)
.append($('<div class="nav-slider-right" />')
.append($('<button type="button" class="nav-link nav-next-day"/>')
.html('>')
.click(function () {
if (settings.scale === 'hours') {
core.navigateTo(element, tools.getCellSize() * -4);
} else if (settings.scale === 'days') {
core.navigateTo(element, tools.getCellSize() * -7);
} else if (settings.scale === 'weeks') {
core.navigateTo(element, tools.getCellSize() * -4);
} else if (settings.scale === 'months') {
core.navigateTo(element, tools.getCellSize() * -3);
}
}))
.append($('<button type="button" class="nav-link nav-next-week"/>')
.html('>>')
.click(function () {
if (settings.scale === 'hours') {
core.navigateTo(element, tools.getCellSize() * -8);
} else if (settings.scale === 'days') {
core.navigateTo(element, tools.getCellSize() * -30);
} else if (settings.scale === 'weeks') {
core.navigateTo(element, tools.getCellSize() * -12);
} else if (settings.scale === 'months') {
core.navigateTo(element, tools.getCellSize() * -6);
}
}))
.append($('<button type="button" class="nav-link nav-zoomIn"/>')
.html('+')
.click(function () {
core.zoomInOut(element, -1);
}))
.append($('<button type="button" class="nav-link nav-zoomOut"/>')
.html('-')
.click(function () {
core.zoomInOut(element, 1);
}))
)
);
$(document).mouseup(function () {
element.scrollNavigation.scrollerMouseDown = false;
});
// Button navigation is provided by setting `settings.navigation='buttons'`
} else {
ganttNavigate = $('<div class="navigate" />')
.append($('<button type="button" class="nav-link nav-page-back"/>')
.html('↑')
.click(function () {
core.navigatePage(element, -1);
}))
.append($('<div class="page-number"/>')
.append($('<span/>')
.html(element.pageNum + 1 + ' / ' + element.pageCount)))
.append($('<button type="button" class="nav-link nav-page-next"/>')
.html('↓')
.click(function () {
core.navigatePage(element, 1);
}))
.append($('<button type="button" class="nav-link nav-begin"/>')
.html('|<')
.click(function () {
core.navigateTo(element, 'begin');
}))
.append($('<button type="button" class="nav-link nav-prev-week"/>')
.html('<<')
.click(function () {
core.navigateTo(element, tools.getCellSize() * 7);
}))
.append($('<button type="button" class="nav-link nav-prev-day"/>')
.html('<')
.click(function () {
core.navigateTo(element, tools.getCellSize());
}))
.append($('<button type="button" class="nav-link nav-now"/>')
.html('●')
.click(function () {
core.navigateTo(element, 'now');
}))
.append($('<button type="button" class="nav-link nav-next-day"/>')
.html('>')
.click(function () {
core.navigateTo(element, tools.getCellSize() * -1);
}))
.append($('<button type="button" class="nav-link nav-next-week"/>')
.html('>>')
.click(function () {
core.navigateTo(element, tools.getCellSize() * -7);
}))
.append($('<button type="button" class="nav-link nav-end"/>')
.html('>|')
.click(function () {
core.navigateTo(element, 'end');
}))
.append($('<button type="button" class="nav-link nav-zoomIn"/>')
.html('+')
.click(function () {
core.zoomInOut(element, -1);
}))
.append($('<button type="button" class="nav-link nav-zoomOut"/>')
.html('-')
.click(function () {
core.zoomInOut(element, 1);
}));
}
return $('<div class="bottom"></div>').append(ganttNavigate);
},
// **Progress Bar**
// Return an element representing a progress of position within the entire chart
createProgressBar: function (label, desc, classNames, dataObj) {
label = label || "";
var bar = $('<div class="bar"><div class="fn-label">' + label + '</div></div>')
.data("dataObj", dataObj);
if (desc) {
bar
.mouseenter(function (e) {
var hint = $('<div class="fn-gantt-hint" />').html(desc);
$("body").append(hint);
hint.css("left", e.pageX);
hint.css("top", e.pageY);
hint.show();
})
.mouseleave(function () {
$(".fn-gantt-hint").remove();
})
.mousemove(function (e) {
$(".fn-gantt-hint").css("left", e.pageX);
$(".fn-gantt-hint").css("top", e.pageY + 15);
});
}
if (classNames) {
bar.addClass(classNames);
}
bar.click(function (e) {
e.stopPropagation();
settings.onItemClick($(this).data("dataObj"));
});
return bar;
},
// Remove the `wd` (weekday) class and add `today` class to the
// current day/week/month (depending on the current scale)
markNow: function (element) {
var cd = new Date().setHours(0, 0, 0, 0);
switch (settings.scale) {
case "weeks":
$(element).find(':findweek("' + cd + '")').removeClass('wd').addClass('today');
break;
case "months":
$(element).find(':findmonth("' + cd + '")').removeClass('wd').addClass('today');
break;
case "days":
/* falls through */
case "hours":
/* falls through */
default:
$(element).find(':findday("' + cd + '")').removeClass('wd').addClass('today');
}
},
// **Fill the Chart**
// Parse the data and fill the data panel
fillData: function (element, datapanel, leftpanel /* <- never used? */) {
var cellWidth = tools.getCellSize();
var barOffset = (cellWidth - 18) / 2;