decidim-calendar 0.13.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE-AGPLv3.txt +661 -0
  3. data/README.md +9 -0
  4. data/Rakefile +43 -0
  5. data/app/assets/config/decidim_calendar_manifest.css +2 -0
  6. data/app/assets/config/decidim_calendar_manifest.js +2 -0
  7. data/app/assets/images/decidim/calendar/icon.svg +51 -0
  8. data/app/assets/javascripts/decidim/calendar/calendar.js.es6 +6 -0
  9. data/app/assets/javascripts/decidim/calendar/gantt.js.es6 +1 -0
  10. data/app/assets/stylesheets/decidim/calendar/calendar.scss +102 -0
  11. data/app/assets/stylesheets/decidim/calendar/gantt.scss +1 -0
  12. data/app/commands/decidim/calendar/admin/create_external_event.rb +38 -0
  13. data/app/commands/decidim/calendar/admin/destroy_external_event.rb +27 -0
  14. data/app/commands/decidim/calendar/admin/update_external_event.rb +43 -0
  15. data/app/controllers/decidim/calendar/admin/application_controller.rb +23 -0
  16. data/app/controllers/decidim/calendar/admin/external_events_controller.rb +84 -0
  17. data/app/controllers/decidim/calendar/application_controller.rb +21 -0
  18. data/app/controllers/decidim/calendar/calendar_controller.rb +33 -0
  19. data/app/forms/decidim/calendar/admin/external_event_form.rb +21 -0
  20. data/app/helpers/decidim/calendar/calendar_helper.rb +57 -0
  21. data/app/models/decidim/calendar/application_record.rb +9 -0
  22. data/app/models/decidim/calendar/event.rb +25 -0
  23. data/app/models/decidim/calendar/external_event.rb +18 -0
  24. data/app/permissions/decidim/calendar/admin/permissions.rb +22 -0
  25. data/app/presenters/decidim/calendar/event_presenter.rb +104 -0
  26. data/app/queries/decidim/calendar/organization_calendar.rb +15 -0
  27. data/app/services/decidim/calendar/event_to_ical.rb +27 -0
  28. data/app/services/decidim/calendar/general_calendar.rb +13 -0
  29. data/app/services/decidim/calendar/meeting_to_event.rb +8 -0
  30. data/app/services/decidim/meetings/calendar/base_calendar.rb +61 -0
  31. data/app/views/decidim/calendar/admin/external_events/_form.html.erb +22 -0
  32. data/app/views/decidim/calendar/admin/external_events/edit.html.erb +6 -0
  33. data/app/views/decidim/calendar/admin/external_events/index.html.erb +66 -0
  34. data/app/views/decidim/calendar/admin/external_events/new.html.erb +6 -0
  35. data/app/views/decidim/calendar/calendar/gantt.html.erb +22 -0
  36. data/app/views/decidim/calendar/calendar/index.html.erb +63 -0
  37. data/app/views/decidim/shared/_extended_navigation_bar.html.erb +51 -0
  38. data/app/views/layouts/_calendar_navigation.html.erb +18 -0
  39. data/app/views/layouts/calendar.html.erb +4 -0
  40. data/config/locales/ca.yml +38 -0
  41. data/config/locales/de.yml +38 -0
  42. data/config/locales/en.yml +48 -0
  43. data/config/locales/es.yml +38 -0
  44. data/config/locales/fi.yml +38 -0
  45. data/config/locales/fr.yml +38 -0
  46. data/config/locales/it.yml +38 -0
  47. data/config/locales/pl.yml +38 -0
  48. data/config/locales/pt.yml +38 -0
  49. data/config/locales/ro.yml +38 -0
  50. data/config/locales/ru.yml +38 -0
  51. data/config/locales/sv.yml +38 -0
  52. data/config/locales/tr.yml +38 -0
  53. data/config/locales/uk.yml +38 -0
  54. data/db/migrate/20190312132654_create_decidim_calendar_external_events.rb +18 -0
  55. data/lib/decidim/calendar.rb +11 -0
  56. data/lib/decidim/calendar/admin.rb +9 -0
  57. data/lib/decidim/calendar/admin_engine.rb +30 -0
  58. data/lib/decidim/calendar/engine.rb +34 -0
  59. data/lib/decidim/calendar/participatory_space.rb +20 -0
  60. data/lib/decidim/calendar/test/factories.rb +12 -0
  61. data/lib/decidim/calendar/version.rb +10 -0
  62. data/vendor/assets/javascripts/frappe-gantt/frappe-gantt.css +119 -0
  63. data/vendor/assets/javascripts/frappe-gantt/frappe-gantt.js +1883 -0
  64. data/vendor/assets/javascripts/fullcalendar/bootstrap/main.css +33 -0
  65. data/vendor/assets/javascripts/fullcalendar/bootstrap/main.js +90 -0
  66. data/vendor/assets/javascripts/fullcalendar/bootstrap/main.min.css +5 -0
  67. data/vendor/assets/javascripts/fullcalendar/bootstrap/main.min.js +20 -0
  68. data/vendor/assets/javascripts/fullcalendar/core/locales-all.js +1353 -0
  69. data/vendor/assets/javascripts/fullcalendar/core/locales-all.min.js +6 -0
  70. data/vendor/assets/javascripts/fullcalendar/core/locales/af.js +30 -0
  71. data/vendor/assets/javascripts/fullcalendar/core/locales/ar-dz.js +31 -0
  72. data/vendor/assets/javascripts/fullcalendar/core/locales/ar-kw.js +31 -0
  73. data/vendor/assets/javascripts/fullcalendar/core/locales/ar-ly.js +31 -0
  74. data/vendor/assets/javascripts/fullcalendar/core/locales/ar-ma.js +31 -0
  75. data/vendor/assets/javascripts/fullcalendar/core/locales/ar-sa.js +31 -0
  76. data/vendor/assets/javascripts/fullcalendar/core/locales/ar-tn.js +31 -0
  77. data/vendor/assets/javascripts/fullcalendar/core/locales/ar.js +31 -0
  78. data/vendor/assets/javascripts/fullcalendar/core/locales/bg.js +31 -0
  79. data/vendor/assets/javascripts/fullcalendar/core/locales/bs.js +32 -0
  80. data/vendor/assets/javascripts/fullcalendar/core/locales/ca.js +30 -0
  81. data/vendor/assets/javascripts/fullcalendar/core/locales/cs.js +32 -0
  82. data/vendor/assets/javascripts/fullcalendar/core/locales/da.js +30 -0
  83. data/vendor/assets/javascripts/fullcalendar/core/locales/de.js +33 -0
  84. data/vendor/assets/javascripts/fullcalendar/core/locales/el.js +30 -0
  85. data/vendor/assets/javascripts/fullcalendar/core/locales/en-au.js +17 -0
  86. data/vendor/assets/javascripts/fullcalendar/core/locales/en-gb.js +17 -0
  87. data/vendor/assets/javascripts/fullcalendar/core/locales/en-nz.js +17 -0
  88. data/vendor/assets/javascripts/fullcalendar/core/locales/es-us.js +30 -0
  89. data/vendor/assets/javascripts/fullcalendar/core/locales/es.js +30 -0
  90. data/vendor/assets/javascripts/fullcalendar/core/locales/et.js +32 -0
  91. data/vendor/assets/javascripts/fullcalendar/core/locales/eu.js +30 -0
  92. data/vendor/assets/javascripts/fullcalendar/core/locales/fa.js +33 -0
  93. data/vendor/assets/javascripts/fullcalendar/core/locales/fi.js +30 -0
  94. data/vendor/assets/javascripts/fullcalendar/core/locales/fr-ca.js +27 -0
  95. data/vendor/assets/javascripts/fullcalendar/core/locales/fr-ch.js +31 -0
  96. data/vendor/assets/javascripts/fullcalendar/core/locales/fr.js +31 -0
  97. data/vendor/assets/javascripts/fullcalendar/core/locales/gl.js +30 -0
  98. data/vendor/assets/javascripts/fullcalendar/core/locales/he.js +27 -0
  99. data/vendor/assets/javascripts/fullcalendar/core/locales/hi.js +32 -0
  100. data/vendor/assets/javascripts/fullcalendar/core/locales/hr.js +32 -0
  101. data/vendor/assets/javascripts/fullcalendar/core/locales/hu.js +30 -0
  102. data/vendor/assets/javascripts/fullcalendar/core/locales/id.js +30 -0
  103. data/vendor/assets/javascripts/fullcalendar/core/locales/is.js +30 -0
  104. data/vendor/assets/javascripts/fullcalendar/core/locales/it.js +32 -0
  105. data/vendor/assets/javascripts/fullcalendar/core/locales/ja.js +28 -0
  106. data/vendor/assets/javascripts/fullcalendar/core/locales/ka.js +32 -0
  107. data/vendor/assets/javascripts/fullcalendar/core/locales/kk.js +32 -0
  108. data/vendor/assets/javascripts/fullcalendar/core/locales/ko.js +26 -0
  109. data/vendor/assets/javascripts/fullcalendar/core/locales/lb.js +30 -0
  110. data/vendor/assets/javascripts/fullcalendar/core/locales/lt.js +30 -0
  111. data/vendor/assets/javascripts/fullcalendar/core/locales/lv.js +32 -0
  112. data/vendor/assets/javascripts/fullcalendar/core/locales/mk.js +28 -0
  113. data/vendor/assets/javascripts/fullcalendar/core/locales/ms.js +32 -0
  114. data/vendor/assets/javascripts/fullcalendar/core/locales/nb.js +30 -0
  115. data/vendor/assets/javascripts/fullcalendar/core/locales/nl.js +30 -0
  116. data/vendor/assets/javascripts/fullcalendar/core/locales/nn.js +30 -0
  117. data/vendor/assets/javascripts/fullcalendar/core/locales/pl.js +30 -0
  118. data/vendor/assets/javascripts/fullcalendar/core/locales/pt-br.js +28 -0
  119. data/vendor/assets/javascripts/fullcalendar/core/locales/pt.js +30 -0
  120. data/vendor/assets/javascripts/fullcalendar/core/locales/ro.js +32 -0
  121. data/vendor/assets/javascripts/fullcalendar/core/locales/ru.js +32 -0
  122. data/vendor/assets/javascripts/fullcalendar/core/locales/sk.js +32 -0
  123. data/vendor/assets/javascripts/fullcalendar/core/locales/sl.js +30 -0
  124. data/vendor/assets/javascripts/fullcalendar/core/locales/sq.js +32 -0
  125. data/vendor/assets/javascripts/fullcalendar/core/locales/sr-cyrl.js +32 -0
  126. data/vendor/assets/javascripts/fullcalendar/core/locales/sr.js +32 -0
  127. data/vendor/assets/javascripts/fullcalendar/core/locales/sv.js +30 -0
  128. data/vendor/assets/javascripts/fullcalendar/core/locales/th.js +25 -0
  129. data/vendor/assets/javascripts/fullcalendar/core/locales/tr.js +30 -0
  130. data/vendor/assets/javascripts/fullcalendar/core/locales/uk.js +32 -0
  131. data/vendor/assets/javascripts/fullcalendar/core/locales/vi.js +32 -0
  132. data/vendor/assets/javascripts/fullcalendar/core/locales/zh-cn.js +33 -0
  133. data/vendor/assets/javascripts/fullcalendar/core/locales/zh-tw.js +26 -0
  134. data/vendor/assets/javascripts/fullcalendar/core/main.css +900 -0
  135. data/vendor/assets/javascripts/fullcalendar/core/main.js +8791 -0
  136. data/vendor/assets/javascripts/fullcalendar/core/main.min.css +5 -0
  137. data/vendor/assets/javascripts/fullcalendar/core/main.min.js +9 -0
  138. data/vendor/assets/javascripts/fullcalendar/daygrid/main.css +69 -0
  139. data/vendor/assets/javascripts/fullcalendar/daygrid/main.js +1630 -0
  140. data/vendor/assets/javascripts/fullcalendar/daygrid/main.min.css +5 -0
  141. data/vendor/assets/javascripts/fullcalendar/daygrid/main.min.js +20 -0
  142. data/vendor/assets/javascripts/fullcalendar/google-calendar/main.js +169 -0
  143. data/vendor/assets/javascripts/fullcalendar/google-calendar/main.min.js +20 -0
  144. data/vendor/assets/javascripts/fullcalendar/interaction/main.js +2155 -0
  145. data/vendor/assets/javascripts/fullcalendar/interaction/main.min.js +21 -0
  146. data/vendor/assets/javascripts/fullcalendar/list/main.css +101 -0
  147. data/vendor/assets/javascripts/fullcalendar/list/main.js +341 -0
  148. data/vendor/assets/javascripts/fullcalendar/list/main.min.css +5 -0
  149. data/vendor/assets/javascripts/fullcalendar/list/main.min.js +20 -0
  150. data/vendor/assets/javascripts/fullcalendar/luxon/main.js +162 -0
  151. data/vendor/assets/javascripts/fullcalendar/luxon/main.min.js +20 -0
  152. data/vendor/assets/javascripts/fullcalendar/moment-timezone/main.js +64 -0
  153. data/vendor/assets/javascripts/fullcalendar/moment-timezone/main.min.js +20 -0
  154. data/vendor/assets/javascripts/fullcalendar/moment/main.js +103 -0
  155. data/vendor/assets/javascripts/fullcalendar/moment/main.min.js +6 -0
  156. data/vendor/assets/javascripts/fullcalendar/rrule/main.js +127 -0
  157. data/vendor/assets/javascripts/fullcalendar/rrule/main.min.js +20 -0
  158. data/vendor/assets/javascripts/fullcalendar/timegrid/main.css +266 -0
  159. data/vendor/assets/javascripts/fullcalendar/timegrid/main.js +1339 -0
  160. data/vendor/assets/javascripts/fullcalendar/timegrid/main.min.css +5 -0
  161. data/vendor/assets/javascripts/fullcalendar/timegrid/main.min.js +20 -0
  162. metadata +247 -0
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency "decidim/components/namer"
4
+
5
+ Decidim.register_participatory_space(:calendar) do |participatory_space|
6
+ participatory_space.icon = "decidim/calendar/icon.svg"
7
+ participatory_space.stylesheet = "decidim/calendar/calendar"
8
+
9
+ participatory_space.participatory_spaces do |organization|
10
+ Decidim::Calendar::OrganizationCalendar.new(organization).query
11
+ end
12
+
13
+ participatory_space.context(:public) do |context|
14
+ context.engine = Decidim::Calendar::Engine
15
+ end
16
+
17
+ participatory_space.context(:admin) do |context|
18
+ context.engine = Decidim::Calendar::AdminEngine
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ factory :external_event, class: Decidim::Calendar::ExternalEvent do
5
+ title { generate_localized_title }
6
+ start_at { Date.current }
7
+ end_at { Date.current + 1.day }
8
+ url { Faker::Internet.url }
9
+ organization { build(:organization) }
10
+ author { build(:user, organization: organization) }
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Decidim
4
+ # Holds decidim-calendar version
5
+ module Calendar
6
+ def self.version
7
+ "0.13.1"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,119 @@
1
+ .gantt .grid-background {
2
+ fill: none; }
3
+
4
+ .gantt .grid-header {
5
+ fill: #ffffff;
6
+ stroke: #e0e0e0;
7
+ stroke-width: 1.4; }
8
+
9
+ .gantt .grid-row {
10
+ fill: #ffffff; }
11
+
12
+ .gantt .grid-row:nth-child(even) {
13
+ fill: #f5f5f5; }
14
+
15
+ .gantt .row-line {
16
+ stroke: #ebeff2; }
17
+
18
+ .gantt .tick {
19
+ stroke: #e0e0e0;
20
+ stroke-width: 0.2; }
21
+ .gantt .tick.thick {
22
+ stroke-width: 0.4; }
23
+
24
+ .gantt .today-highlight {
25
+ fill: #fcf8e3;
26
+ opacity: 0.5; }
27
+
28
+ .gantt .arrow {
29
+ fill: none;
30
+ stroke: #666;
31
+ stroke-width: 1.4; }
32
+
33
+ .gantt .bar {
34
+ fill: #b8c2cc;
35
+ stroke: #8D99A6;
36
+ stroke-width: 0;
37
+ transition: stroke-width .3s ease;
38
+ user-select: none; }
39
+
40
+ .gantt .bar-progress {
41
+ fill: #a3a3ff; }
42
+
43
+ .gantt .bar-invalid {
44
+ fill: transparent;
45
+ stroke: #8D99A6;
46
+ stroke-width: 1;
47
+ stroke-dasharray: 5; }
48
+ .gantt .bar-invalid ~ .bar-label {
49
+ fill: #555; }
50
+
51
+ .gantt .bar-label {
52
+ fill: #fff;
53
+ dominant-baseline: central;
54
+ text-anchor: middle;
55
+ font-size: 12px;
56
+ font-weight: lighter; }
57
+ .gantt .bar-label.big {
58
+ fill: #555;
59
+ text-anchor: start; }
60
+
61
+ .gantt .handle {
62
+ fill: #ddd;
63
+ cursor: ew-resize;
64
+ opacity: 0;
65
+ visibility: hidden;
66
+ transition: opacity .3s ease; }
67
+
68
+ .gantt .bar-wrapper {
69
+ cursor: pointer;
70
+ outline: none; }
71
+ .gantt .bar-wrapper:hover .bar {
72
+ fill: #a9b5c1; }
73
+ .gantt .bar-wrapper:hover .bar-progress {
74
+ fill: #8a8aff; }
75
+ .gantt .bar-wrapper:hover .handle {
76
+ visibility: visible;
77
+ opacity: 1; }
78
+ .gantt .bar-wrapper.active .bar {
79
+ fill: #a9b5c1; }
80
+ .gantt .bar-wrapper.active .bar-progress {
81
+ fill: #8a8aff; }
82
+
83
+ .gantt .lower-text, .gantt .upper-text {
84
+ font-size: 12px;
85
+ text-anchor: middle; }
86
+
87
+ .gantt .upper-text {
88
+ fill: #555; }
89
+
90
+ .gantt .lower-text {
91
+ fill: #333; }
92
+
93
+ .gantt .hide {
94
+ display: none; }
95
+
96
+ .gantt-container {
97
+ position: relative;
98
+ overflow: auto;
99
+ font-size: 12px; }
100
+ .gantt-container .popup-wrapper {
101
+ position: absolute;
102
+ top: 0;
103
+ left: 0;
104
+ background: rgba(0, 0, 0, 0.8);
105
+ padding: 0;
106
+ color: #959da5;
107
+ border-radius: 3px; }
108
+ .gantt-container .popup-wrapper .title {
109
+ border-bottom: 3px solid #a3a3ff;
110
+ padding: 10px; }
111
+ .gantt-container .popup-wrapper .subtitle {
112
+ padding: 10px;
113
+ color: #dfe2e5; }
114
+ .gantt-container .popup-wrapper .pointer {
115
+ position: absolute;
116
+ height: 5px;
117
+ margin: 0 0 0 -5px;
118
+ border: 5px solid transparent;
119
+ border-top-color: rgba(0, 0, 0, 0.8); }
@@ -0,0 +1,1883 @@
1
+ var Gantt = (function () {
2
+ 'use strict';
3
+
4
+ const YEAR = 'year';
5
+ const MONTH = 'month';
6
+ const DAY = 'day';
7
+ const HOUR = 'hour';
8
+ const MINUTE = 'minute';
9
+ const SECOND = 'second';
10
+ const MILLISECOND = 'millisecond';
11
+
12
+ const month_names = {
13
+ en: [
14
+ 'January',
15
+ 'February',
16
+ 'March',
17
+ 'April',
18
+ 'May',
19
+ 'June',
20
+ 'July',
21
+ 'August',
22
+ 'September',
23
+ 'October',
24
+ 'November',
25
+ 'December'
26
+ ],
27
+ ru: [
28
+ 'Январь',
29
+ 'Февраль',
30
+ 'Март',
31
+ 'Апрель',
32
+ 'Май',
33
+ 'Июнь',
34
+ 'Июль',
35
+ 'Август',
36
+ 'Сентябрь',
37
+ 'Октябрь',
38
+ 'Ноябрь',
39
+ 'Декабрь'
40
+ ]
41
+ };
42
+
43
+ var date_utils = {
44
+ parse(date, date_separator = '-', time_separator = /[.:]/) {
45
+ if (date instanceof Date) {
46
+ return date;
47
+ }
48
+ if (typeof date === 'string') {
49
+ let date_parts, time_parts;
50
+ const parts = date.split(' ');
51
+
52
+ date_parts = parts[0]
53
+ .split(date_separator)
54
+ .map(val => parseInt(val, 10));
55
+ time_parts = parts[1] && parts[1].split(time_separator);
56
+
57
+ // month is 0 indexed
58
+ date_parts[1] = date_parts[1] - 1;
59
+
60
+ let vals = date_parts;
61
+
62
+ if (time_parts && time_parts.length) {
63
+ if (time_parts.length == 4) {
64
+ time_parts[3] = '0.' + time_parts[3];
65
+ time_parts[3] = parseFloat(time_parts[3]) * 1000;
66
+ }
67
+ vals = vals.concat(time_parts);
68
+ }
69
+
70
+ return new Date(...vals);
71
+ }
72
+ },
73
+
74
+ to_string(date, with_time = false) {
75
+ if (!(date instanceof Date)) {
76
+ throw new TypeError('Invalid argument type');
77
+ }
78
+ const vals = this.get_date_values(date).map((val, i) => {
79
+ if (i === 1) {
80
+ // add 1 for month
81
+ val = val + 1;
82
+ }
83
+
84
+ if (i === 6) {
85
+ return padStart(val + '', 3, '0');
86
+ }
87
+
88
+ return padStart(val + '', 2, '0');
89
+ });
90
+ const date_string = `${vals[0]}-${vals[1]}-${vals[2]}`;
91
+ const time_string = `${vals[3]}:${vals[4]}:${vals[5]}.${vals[6]}`;
92
+
93
+ return date_string + (with_time ? ' ' + time_string : '');
94
+ },
95
+
96
+ format(date, format_string = 'YYYY-MM-DD HH:mm:ss.SSS', lang = 'en') {
97
+ const values = this.get_date_values(date).map(d => padStart(d, 2, 0));
98
+ const format_map = {
99
+ YYYY: values[0],
100
+ MM: padStart(+values[1] + 1, 2, 0),
101
+ DD: values[2],
102
+ HH: values[3],
103
+ mm: values[4],
104
+ ss: values[5],
105
+ SSS:values[6],
106
+ D: values[2],
107
+ MMMM: month_names[lang][+values[1]],
108
+ MMM: month_names[lang][+values[1]]
109
+ };
110
+
111
+ let str = format_string;
112
+ const formatted_values = [];
113
+
114
+ Object.keys(format_map)
115
+ .sort((a, b) => b.length - a.length) // big string first
116
+ .forEach(key => {
117
+ if (str.includes(key)) {
118
+ str = str.replace(key, `$${formatted_values.length}`);
119
+ formatted_values.push(format_map[key]);
120
+ }
121
+ });
122
+
123
+ formatted_values.forEach((value, i) => {
124
+ str = str.replace(`$${i}`, value);
125
+ });
126
+
127
+ return str;
128
+ },
129
+
130
+ diff(date_a, date_b, scale = DAY) {
131
+ let milliseconds, seconds, hours, minutes, days, months, years;
132
+
133
+ milliseconds = date_a - date_b;
134
+ seconds = milliseconds / 1000;
135
+ minutes = seconds / 60;
136
+ hours = minutes / 60;
137
+ days = hours / 24;
138
+ months = days / 30;
139
+ years = months / 12;
140
+
141
+ if (!scale.endsWith('s')) {
142
+ scale += 's';
143
+ }
144
+
145
+ return Math.floor(
146
+ {
147
+ milliseconds,
148
+ seconds,
149
+ minutes,
150
+ hours,
151
+ days,
152
+ months,
153
+ years
154
+ }[scale]
155
+ );
156
+ },
157
+
158
+ today() {
159
+ const vals = this.get_date_values(new Date()).slice(0, 3);
160
+ return new Date(...vals);
161
+ },
162
+
163
+ now() {
164
+ return new Date();
165
+ },
166
+
167
+ add(date, qty, scale) {
168
+ qty = parseInt(qty, 10);
169
+ const vals = [
170
+ date.getFullYear() + (scale === YEAR ? qty : 0),
171
+ date.getMonth() + (scale === MONTH ? qty : 0),
172
+ date.getDate() + (scale === DAY ? qty : 0),
173
+ date.getHours() + (scale === HOUR ? qty : 0),
174
+ date.getMinutes() + (scale === MINUTE ? qty : 0),
175
+ date.getSeconds() + (scale === SECOND ? qty : 0),
176
+ date.getMilliseconds() + (scale === MILLISECOND ? qty : 0)
177
+ ];
178
+ return new Date(...vals);
179
+ },
180
+
181
+ start_of(date, scale) {
182
+ const scores = {
183
+ [YEAR]: 6,
184
+ [MONTH]: 5,
185
+ [DAY]: 4,
186
+ [HOUR]: 3,
187
+ [MINUTE]: 2,
188
+ [SECOND]: 1,
189
+ [MILLISECOND]: 0
190
+ };
191
+
192
+ function should_reset(_scale) {
193
+ const max_score = scores[scale];
194
+ return scores[_scale] <= max_score;
195
+ }
196
+
197
+ const vals = [
198
+ date.getFullYear(),
199
+ should_reset(YEAR) ? 0 : date.getMonth(),
200
+ should_reset(MONTH) ? 1 : date.getDate(),
201
+ should_reset(DAY) ? 0 : date.getHours(),
202
+ should_reset(HOUR) ? 0 : date.getMinutes(),
203
+ should_reset(MINUTE) ? 0 : date.getSeconds(),
204
+ should_reset(SECOND) ? 0 : date.getMilliseconds()
205
+ ];
206
+
207
+ return new Date(...vals);
208
+ },
209
+
210
+ clone(date) {
211
+ return new Date(...this.get_date_values(date));
212
+ },
213
+
214
+ get_date_values(date) {
215
+ return [
216
+ date.getFullYear(),
217
+ date.getMonth(),
218
+ date.getDate(),
219
+ date.getHours(),
220
+ date.getMinutes(),
221
+ date.getSeconds(),
222
+ date.getMilliseconds()
223
+ ];
224
+ },
225
+
226
+ get_days_in_month(date) {
227
+ const no_of_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
228
+
229
+ const month = date.getMonth();
230
+
231
+ if (month !== 1) {
232
+ return no_of_days[month];
233
+ }
234
+
235
+ // Feb
236
+ const year = date.getFullYear();
237
+ if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
238
+ return 29;
239
+ }
240
+ return 28;
241
+ }
242
+ };
243
+
244
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
245
+ function padStart(str, targetLength, padString) {
246
+ str = str + '';
247
+ targetLength = targetLength >> 0;
248
+ padString = String(typeof padString !== 'undefined' ? padString : ' ');
249
+ if (str.length > targetLength) {
250
+ return String(str);
251
+ } else {
252
+ targetLength = targetLength - str.length;
253
+ if (targetLength > padString.length) {
254
+ padString += padString.repeat(targetLength / padString.length);
255
+ }
256
+ return padString.slice(0, targetLength) + String(str);
257
+ }
258
+ }
259
+
260
+ function $(expr, con) {
261
+ return typeof expr === 'string'
262
+ ? (con || document).querySelector(expr)
263
+ : expr || null;
264
+ }
265
+
266
+ function createSVG(tag, attrs) {
267
+ const elem = document.createElementNS('http://www.w3.org/2000/svg', tag);
268
+ for (let attr in attrs) {
269
+ if (attr === 'append_to') {
270
+ const parent = attrs.append_to;
271
+ parent.appendChild(elem);
272
+ } else if (attr === 'innerHTML') {
273
+ elem.innerHTML = attrs.innerHTML;
274
+ } else {
275
+ elem.setAttribute(attr, attrs[attr]);
276
+ }
277
+ }
278
+ return elem;
279
+ }
280
+
281
+ function animateSVG(svgElement, attr, from, to) {
282
+ const animatedSvgElement = getAnimationElement(svgElement, attr, from, to);
283
+
284
+ if (animatedSvgElement === svgElement) {
285
+ // triggered 2nd time programmatically
286
+ // trigger artificial click event
287
+ const event = document.createEvent('HTMLEvents');
288
+ event.initEvent('click', true, true);
289
+ event.eventName = 'click';
290
+ animatedSvgElement.dispatchEvent(event);
291
+ }
292
+ }
293
+
294
+ function getAnimationElement(
295
+ svgElement,
296
+ attr,
297
+ from,
298
+ to,
299
+ dur = '0.4s',
300
+ begin = '0.1s'
301
+ ) {
302
+ const animEl = svgElement.querySelector('animate');
303
+ if (animEl) {
304
+ $.attr(animEl, {
305
+ attributeName: attr,
306
+ from,
307
+ to,
308
+ dur,
309
+ begin: 'click + ' + begin // artificial click
310
+ });
311
+ return svgElement;
312
+ }
313
+
314
+ const animateElement = createSVG('animate', {
315
+ attributeName: attr,
316
+ from,
317
+ to,
318
+ dur,
319
+ begin,
320
+ calcMode: 'spline',
321
+ values: from + ';' + to,
322
+ keyTimes: '0; 1',
323
+ keySplines: cubic_bezier('ease-out')
324
+ });
325
+ svgElement.appendChild(animateElement);
326
+
327
+ return svgElement;
328
+ }
329
+
330
+ function cubic_bezier(name) {
331
+ return {
332
+ ease: '.25 .1 .25 1',
333
+ linear: '0 0 1 1',
334
+ 'ease-in': '.42 0 1 1',
335
+ 'ease-out': '0 0 .58 1',
336
+ 'ease-in-out': '.42 0 .58 1'
337
+ }[name];
338
+ }
339
+
340
+ $.on = (element, event, selector, callback) => {
341
+ if (!callback) {
342
+ callback = selector;
343
+ $.bind(element, event, callback);
344
+ } else {
345
+ $.delegate(element, event, selector, callback);
346
+ }
347
+ };
348
+
349
+ $.off = (element, event, handler) => {
350
+ element.removeEventListener(event, handler);
351
+ };
352
+
353
+ $.bind = (element, event, callback) => {
354
+ event.split(/\s+/).forEach(function(event) {
355
+ element.addEventListener(event, callback);
356
+ });
357
+ };
358
+
359
+ $.delegate = (element, event, selector, callback) => {
360
+ element.addEventListener(event, function(e) {
361
+ const delegatedTarget = e.target.closest(selector);
362
+ if (delegatedTarget) {
363
+ e.delegatedTarget = delegatedTarget;
364
+ callback.call(this, e, delegatedTarget);
365
+ }
366
+ });
367
+ };
368
+
369
+ $.closest = (selector, element) => {
370
+ if (!element) return null;
371
+
372
+ if (element.matches(selector)) {
373
+ return element;
374
+ }
375
+
376
+ return $.closest(selector, element.parentNode);
377
+ };
378
+
379
+ $.attr = (element, attr, value) => {
380
+ if (!value && typeof attr === 'string') {
381
+ return element.getAttribute(attr);
382
+ }
383
+
384
+ if (typeof attr === 'object') {
385
+ for (let key in attr) {
386
+ $.attr(element, key, attr[key]);
387
+ }
388
+ return;
389
+ }
390
+
391
+ element.setAttribute(attr, value);
392
+ };
393
+
394
+ class Bar {
395
+ constructor(gantt, task) {
396
+ this.set_defaults(gantt, task);
397
+ this.prepare();
398
+ this.draw();
399
+ this.bind();
400
+ }
401
+
402
+ set_defaults(gantt, task) {
403
+ this.action_completed = false;
404
+ this.gantt = gantt;
405
+ this.task = task;
406
+ }
407
+
408
+ prepare() {
409
+ this.prepare_values();
410
+ this.prepare_helpers();
411
+ }
412
+
413
+ prepare_values() {
414
+ this.invalid = this.task.invalid;
415
+ this.height = this.gantt.options.bar_height;
416
+ this.x = this.compute_x();
417
+ this.y = this.compute_y();
418
+ this.corner_radius = this.gantt.options.bar_corner_radius;
419
+ this.duration =
420
+ date_utils.diff(this.task._end, this.task._start, 'hour') /
421
+ this.gantt.options.step;
422
+ this.width = this.gantt.options.column_width * this.duration;
423
+ this.progress_width =
424
+ this.gantt.options.column_width *
425
+ this.duration *
426
+ (this.task.progress / 100) || 0;
427
+ this.group = createSVG('g', {
428
+ class: 'bar-wrapper ' + (this.task.custom_class || ''),
429
+ 'data-id': this.task.id
430
+ });
431
+ this.bar_group = createSVG('g', {
432
+ class: 'bar-group',
433
+ append_to: this.group
434
+ });
435
+ this.handle_group = createSVG('g', {
436
+ class: 'handle-group',
437
+ append_to: this.group
438
+ });
439
+ }
440
+
441
+ prepare_helpers() {
442
+ SVGElement.prototype.getX = function() {
443
+ return +this.getAttribute('x');
444
+ };
445
+ SVGElement.prototype.getY = function() {
446
+ return +this.getAttribute('y');
447
+ };
448
+ SVGElement.prototype.getWidth = function() {
449
+ return +this.getAttribute('width');
450
+ };
451
+ SVGElement.prototype.getHeight = function() {
452
+ return +this.getAttribute('height');
453
+ };
454
+ SVGElement.prototype.getEndX = function() {
455
+ return this.getX() + this.getWidth();
456
+ };
457
+ }
458
+
459
+ draw() {
460
+ this.draw_bar();
461
+ this.draw_progress_bar();
462
+ this.draw_label();
463
+ this.draw_resize_handles();
464
+ }
465
+
466
+ draw_bar() {
467
+ this.$bar = createSVG('rect', {
468
+ x: this.x,
469
+ y: this.y,
470
+ width: this.width,
471
+ height: this.height,
472
+ rx: this.corner_radius,
473
+ ry: this.corner_radius,
474
+ class: 'bar',
475
+ append_to: this.bar_group
476
+ });
477
+
478
+ animateSVG(this.$bar, 'width', 0, this.width);
479
+
480
+ if (this.invalid) {
481
+ this.$bar.classList.add('bar-invalid');
482
+ }
483
+ }
484
+
485
+ draw_progress_bar() {
486
+ if (this.invalid) return;
487
+ this.$bar_progress = createSVG('rect', {
488
+ x: this.x,
489
+ y: this.y,
490
+ width: this.progress_width,
491
+ height: this.height,
492
+ rx: this.corner_radius,
493
+ ry: this.corner_radius,
494
+ class: 'bar-progress',
495
+ append_to: this.bar_group
496
+ });
497
+
498
+ animateSVG(this.$bar_progress, 'width', 0, this.progress_width);
499
+ }
500
+
501
+ draw_label() {
502
+ createSVG('text', {
503
+ x: this.x + this.width / 2,
504
+ y: this.y + this.height / 2,
505
+ innerHTML: this.task.name,
506
+ class: 'bar-label',
507
+ append_to: this.bar_group
508
+ });
509
+ // labels get BBox in the next tick
510
+ requestAnimationFrame(() => this.update_label_position());
511
+ }
512
+
513
+ draw_resize_handles() {
514
+ if (this.invalid) return;
515
+
516
+ const bar = this.$bar;
517
+ const handle_width = 8;
518
+
519
+ createSVG('rect', {
520
+ x: bar.getX() + bar.getWidth() - 9,
521
+ y: bar.getY() + 1,
522
+ width: handle_width,
523
+ height: this.height - 2,
524
+ rx: this.corner_radius,
525
+ ry: this.corner_radius,
526
+ class: 'handle right',
527
+ append_to: this.handle_group
528
+ });
529
+
530
+ createSVG('rect', {
531
+ x: bar.getX() + 1,
532
+ y: bar.getY() + 1,
533
+ width: handle_width,
534
+ height: this.height - 2,
535
+ rx: this.corner_radius,
536
+ ry: this.corner_radius,
537
+ class: 'handle left',
538
+ append_to: this.handle_group
539
+ });
540
+
541
+ if (this.task.progress && this.task.progress < 100) {
542
+ this.$handle_progress = createSVG('polygon', {
543
+ points: this.get_progress_polygon_points().join(','),
544
+ class: 'handle progress',
545
+ append_to: this.handle_group
546
+ });
547
+ }
548
+ }
549
+
550
+ get_progress_polygon_points() {
551
+ const bar_progress = this.$bar_progress;
552
+ return [
553
+ bar_progress.getEndX() - 5,
554
+ bar_progress.getY() + bar_progress.getHeight(),
555
+ bar_progress.getEndX() + 5,
556
+ bar_progress.getY() + bar_progress.getHeight(),
557
+ bar_progress.getEndX(),
558
+ bar_progress.getY() + bar_progress.getHeight() - 8.66
559
+ ];
560
+ }
561
+
562
+ bind() {
563
+ if (this.invalid) return;
564
+ this.setup_click_event();
565
+ }
566
+
567
+ setup_click_event() {
568
+ $.on(this.group, 'focus ' + this.gantt.options.popup_trigger, e => {
569
+ if (this.action_completed) {
570
+ // just finished a move action, wait for a few seconds
571
+ return;
572
+ }
573
+
574
+ if (e.type === 'click') {
575
+ this.gantt.trigger_event('click', [this.task]);
576
+ }
577
+
578
+ this.gantt.unselect_all();
579
+ this.group.classList.toggle('active');
580
+
581
+ this.show_popup();
582
+ });
583
+ }
584
+
585
+ show_popup() {
586
+ if (this.gantt.bar_being_dragged) return;
587
+
588
+ const start_date = date_utils.format(this.task._start, 'MMM D');
589
+ const end_date = date_utils.format(
590
+ date_utils.add(this.task._end, -1, 'second'),
591
+ 'MMM D'
592
+ );
593
+ const subtitle = start_date + ' - ' + end_date;
594
+
595
+ this.gantt.show_popup({
596
+ target_element: this.$bar,
597
+ title: this.task.name,
598
+ subtitle: subtitle,
599
+ task: this.task
600
+ });
601
+ }
602
+
603
+ update_bar_position({ x = null, width = null }) {
604
+ const bar = this.$bar;
605
+ if (x) {
606
+ // get all x values of parent task
607
+ const xs = this.task.dependencies.map(dep => {
608
+ return this.gantt.get_bar(dep).$bar.getX();
609
+ });
610
+ // child task must not go before parent
611
+ const valid_x = xs.reduce((prev, curr) => {
612
+ return x >= curr;
613
+ }, x);
614
+ if (!valid_x) {
615
+ width = null;
616
+ return;
617
+ }
618
+ this.update_attr(bar, 'x', x);
619
+ }
620
+ if (width && width >= this.gantt.options.column_width) {
621
+ this.update_attr(bar, 'width', width);
622
+ }
623
+ this.update_label_position();
624
+ this.update_handle_position();
625
+ this.update_progressbar_position();
626
+ this.update_arrow_position();
627
+ }
628
+
629
+ date_changed() {
630
+ let changed = false;
631
+ const { new_start_date, new_end_date } = this.compute_start_end_date();
632
+
633
+ if (Number(this.task._start) !== Number(new_start_date)) {
634
+ changed = true;
635
+ this.task._start = new_start_date;
636
+ }
637
+
638
+ if (Number(this.task._end) !== Number(new_end_date)) {
639
+ changed = true;
640
+ this.task._end = new_end_date;
641
+ }
642
+
643
+ if (!changed) return;
644
+
645
+ this.gantt.trigger_event('date_change', [
646
+ this.task,
647
+ new_start_date,
648
+ date_utils.add(new_end_date, -1, 'second')
649
+ ]);
650
+ }
651
+
652
+ progress_changed() {
653
+ const new_progress = this.compute_progress();
654
+ this.task.progress = new_progress;
655
+ this.gantt.trigger_event('progress_change', [this.task, new_progress]);
656
+ }
657
+
658
+ set_action_completed() {
659
+ this.action_completed = true;
660
+ setTimeout(() => (this.action_completed = false), 1000);
661
+ }
662
+
663
+ compute_start_end_date() {
664
+ const bar = this.$bar;
665
+ const x_in_units = bar.getX() / this.gantt.options.column_width;
666
+ const new_start_date = date_utils.add(
667
+ this.gantt.gantt_start,
668
+ x_in_units * this.gantt.options.step,
669
+ 'hour'
670
+ );
671
+ const width_in_units = bar.getWidth() / this.gantt.options.column_width;
672
+ const new_end_date = date_utils.add(
673
+ new_start_date,
674
+ width_in_units * this.gantt.options.step,
675
+ 'hour'
676
+ );
677
+
678
+ return { new_start_date, new_end_date };
679
+ }
680
+
681
+ compute_progress() {
682
+ const progress =
683
+ this.$bar_progress.getWidth() / this.$bar.getWidth() * 100;
684
+ return parseInt(progress, 10);
685
+ }
686
+
687
+ compute_x() {
688
+ const { step, column_width } = this.gantt.options;
689
+ const task_start = this.task._start;
690
+ const gantt_start = this.gantt.gantt_start;
691
+
692
+ const diff = date_utils.diff(task_start, gantt_start, 'hour');
693
+ let x = diff / step * column_width;
694
+
695
+ if (this.gantt.view_is('Month')) {
696
+ const diff = date_utils.diff(task_start, gantt_start, 'day');
697
+ x = diff * column_width / 30;
698
+ }
699
+ return x;
700
+ }
701
+
702
+ compute_y() {
703
+ return (
704
+ this.gantt.options.header_height +
705
+ this.gantt.options.padding +
706
+ this.task._index * (this.height + this.gantt.options.padding)
707
+ );
708
+ }
709
+
710
+ get_snap_position(dx) {
711
+ let odx = dx,
712
+ rem,
713
+ position;
714
+
715
+ if (this.gantt.view_is('Week')) {
716
+ rem = dx % (this.gantt.options.column_width / 7);
717
+ position =
718
+ odx -
719
+ rem +
720
+ (rem < this.gantt.options.column_width / 14
721
+ ? 0
722
+ : this.gantt.options.column_width / 7);
723
+ } else if (this.gantt.view_is('Month')) {
724
+ rem = dx % (this.gantt.options.column_width / 30);
725
+ position =
726
+ odx -
727
+ rem +
728
+ (rem < this.gantt.options.column_width / 60
729
+ ? 0
730
+ : this.gantt.options.column_width / 30);
731
+ } else {
732
+ rem = dx % this.gantt.options.column_width;
733
+ position =
734
+ odx -
735
+ rem +
736
+ (rem < this.gantt.options.column_width / 2
737
+ ? 0
738
+ : this.gantt.options.column_width);
739
+ }
740
+ return position;
741
+ }
742
+
743
+ update_attr(element, attr, value) {
744
+ value = +value;
745
+ if (!isNaN(value)) {
746
+ element.setAttribute(attr, value);
747
+ }
748
+ return element;
749
+ }
750
+
751
+ update_progressbar_position() {
752
+ this.$bar_progress.setAttribute('x', this.$bar.getX());
753
+ this.$bar_progress.setAttribute(
754
+ 'width',
755
+ this.$bar.getWidth() * (this.task.progress / 100)
756
+ );
757
+ }
758
+
759
+ update_label_position() {
760
+ const bar = this.$bar,
761
+ label = this.group.querySelector('.bar-label');
762
+
763
+ if (label.getBBox().width > bar.getWidth()) {
764
+ label.classList.add('big');
765
+ label.setAttribute('x', bar.getX() + bar.getWidth() + 5);
766
+ } else {
767
+ label.classList.remove('big');
768
+ label.setAttribute('x', bar.getX() + bar.getWidth() / 2);
769
+ }
770
+ }
771
+
772
+ update_handle_position() {
773
+ const bar = this.$bar;
774
+ this.handle_group
775
+ .querySelector('.handle.left')
776
+ .setAttribute('x', bar.getX() + 1);
777
+ this.handle_group
778
+ .querySelector('.handle.right')
779
+ .setAttribute('x', bar.getEndX() - 9);
780
+ const handle = this.group.querySelector('.handle.progress');
781
+ handle &&
782
+ handle.setAttribute('points', this.get_progress_polygon_points());
783
+ }
784
+
785
+ update_arrow_position() {
786
+ this.arrows = this.arrows || [];
787
+ for (let arrow of this.arrows) {
788
+ arrow.update();
789
+ }
790
+ }
791
+ }
792
+
793
+ class Arrow {
794
+ constructor(gantt, from_task, to_task) {
795
+ this.gantt = gantt;
796
+ this.from_task = from_task;
797
+ this.to_task = to_task;
798
+
799
+ this.calculate_path();
800
+ this.draw();
801
+ }
802
+
803
+ calculate_path() {
804
+ let start_x =
805
+ this.from_task.$bar.getX() + this.from_task.$bar.getWidth() / 2;
806
+
807
+ const condition = () =>
808
+ this.to_task.$bar.getX() < start_x + this.gantt.options.padding &&
809
+ start_x > this.from_task.$bar.getX() + this.gantt.options.padding;
810
+
811
+ while (condition()) {
812
+ start_x -= 10;
813
+ }
814
+
815
+ const start_y =
816
+ this.gantt.options.header_height +
817
+ this.gantt.options.bar_height +
818
+ (this.gantt.options.padding + this.gantt.options.bar_height) *
819
+ this.from_task.task._index +
820
+ this.gantt.options.padding;
821
+
822
+ const end_x = this.to_task.$bar.getX() - this.gantt.options.padding / 2;
823
+ const end_y =
824
+ this.gantt.options.header_height +
825
+ this.gantt.options.bar_height / 2 +
826
+ (this.gantt.options.padding + this.gantt.options.bar_height) *
827
+ this.to_task.task._index +
828
+ this.gantt.options.padding;
829
+
830
+ const from_is_below_to =
831
+ this.from_task.task._index > this.to_task.task._index;
832
+ const curve = this.gantt.options.arrow_curve;
833
+ const clockwise = from_is_below_to ? 1 : 0;
834
+ const curve_y = from_is_below_to ? -curve : curve;
835
+ const offset = from_is_below_to
836
+ ? end_y + this.gantt.options.arrow_curve
837
+ : end_y - this.gantt.options.arrow_curve;
838
+
839
+ this.path = `
840
+ M ${start_x} ${start_y}
841
+ V ${offset}
842
+ a ${curve} ${curve} 0 0 ${clockwise} ${curve} ${curve_y}
843
+ L ${end_x} ${end_y}
844
+ m -5 -5
845
+ l 5 5
846
+ l -5 5`;
847
+
848
+ if (
849
+ this.to_task.$bar.getX() <
850
+ this.from_task.$bar.getX() + this.gantt.options.padding
851
+ ) {
852
+ const down_1 = this.gantt.options.padding / 2 - curve;
853
+ const down_2 =
854
+ this.to_task.$bar.getY() +
855
+ this.to_task.$bar.getHeight() / 2 -
856
+ curve_y;
857
+ const left = this.to_task.$bar.getX() - this.gantt.options.padding;
858
+
859
+ this.path = `
860
+ M ${start_x} ${start_y}
861
+ v ${down_1}
862
+ a ${curve} ${curve} 0 0 1 -${curve} ${curve}
863
+ H ${left}
864
+ a ${curve} ${curve} 0 0 ${clockwise} -${curve} ${curve_y}
865
+ V ${down_2}
866
+ a ${curve} ${curve} 0 0 ${clockwise} ${curve} ${curve_y}
867
+ L ${end_x} ${end_y}
868
+ m -5 -5
869
+ l 5 5
870
+ l -5 5`;
871
+ }
872
+ }
873
+
874
+ draw() {
875
+ this.element = createSVG('path', {
876
+ d: this.path,
877
+ 'data-from': this.from_task.task.id,
878
+ 'data-to': this.to_task.task.id
879
+ });
880
+ }
881
+
882
+ update() {
883
+ this.calculate_path();
884
+ this.element.setAttribute('d', this.path);
885
+ }
886
+ }
887
+
888
+ class Popup {
889
+ constructor(parent, custom_html) {
890
+ this.parent = parent;
891
+ this.custom_html = custom_html;
892
+ this.make();
893
+ }
894
+
895
+ make() {
896
+ this.parent.innerHTML = `
897
+ <div class="title"></div>
898
+ <div class="subtitle"></div>
899
+ <div class="pointer"></div>
900
+ `;
901
+
902
+ this.hide();
903
+
904
+ this.title = this.parent.querySelector('.title');
905
+ this.subtitle = this.parent.querySelector('.subtitle');
906
+ this.pointer = this.parent.querySelector('.pointer');
907
+ }
908
+
909
+ show(options) {
910
+ if (!options.target_element) {
911
+ throw new Error('target_element is required to show popup');
912
+ }
913
+ if (!options.position) {
914
+ options.position = 'left';
915
+ }
916
+ const target_element = options.target_element;
917
+
918
+ if (this.custom_html) {
919
+ let html = this.custom_html(options.task);
920
+ html += '<div class="pointer"></div>';
921
+ this.parent.innerHTML = html;
922
+ this.pointer = this.parent.querySelector('.pointer');
923
+ } else {
924
+ // set data
925
+ this.title.innerHTML = options.title;
926
+ this.subtitle.innerHTML = options.subtitle;
927
+ this.parent.style.width = this.parent.clientWidth + 'px';
928
+ }
929
+
930
+ // set position
931
+ let position_meta;
932
+ if (target_element instanceof HTMLElement) {
933
+ position_meta = target_element.getBoundingClientRect();
934
+ } else if (target_element instanceof SVGElement) {
935
+ position_meta = options.target_element.getBBox();
936
+ }
937
+
938
+ if (options.position === 'left') {
939
+ this.parent.style.left =
940
+ position_meta.x + (position_meta.width + 10) + 'px';
941
+ this.parent.style.top = position_meta.y + 'px';
942
+
943
+ this.pointer.style.transform = 'rotateZ(90deg)';
944
+ this.pointer.style.left = '-7px';
945
+ this.pointer.style.top = '2px';
946
+ }
947
+
948
+ // show
949
+ this.parent.style.opacity = 1;
950
+ }
951
+
952
+ hide() {
953
+ this.parent.style.opacity = 0;
954
+ }
955
+ }
956
+
957
+ class Gantt {
958
+ constructor(wrapper, tasks, options) {
959
+ this.setup_wrapper(wrapper);
960
+ this.setup_options(options);
961
+ this.setup_tasks(tasks);
962
+ // initialize with default view mode
963
+ this.change_view_mode();
964
+ this.bind_events();
965
+ }
966
+
967
+ setup_wrapper(element) {
968
+ let svg_element, wrapper_element;
969
+
970
+ // CSS Selector is passed
971
+ if (typeof element === 'string') {
972
+ element = document.querySelector(element);
973
+ }
974
+
975
+ // get the SVGElement
976
+ if (element instanceof HTMLElement) {
977
+ wrapper_element = element;
978
+ svg_element = element.querySelector('svg');
979
+ } else if (element instanceof SVGElement) {
980
+ svg_element = element;
981
+ } else {
982
+ throw new TypeError(
983
+ 'Frappé Gantt only supports usage of a string CSS selector,' +
984
+ " HTML DOM element or SVG DOM element for the 'element' parameter"
985
+ );
986
+ }
987
+
988
+ // svg element
989
+ if (!svg_element) {
990
+ // create it
991
+ this.$svg = createSVG('svg', {
992
+ append_to: wrapper_element,
993
+ class: 'gantt'
994
+ });
995
+ } else {
996
+ this.$svg = svg_element;
997
+ this.$svg.classList.add('gantt');
998
+ }
999
+
1000
+ // wrapper element
1001
+ this.$container = document.createElement('div');
1002
+ this.$container.classList.add('gantt-container');
1003
+
1004
+ const parent_element = this.$svg.parentElement;
1005
+ parent_element.appendChild(this.$container);
1006
+ this.$container.appendChild(this.$svg);
1007
+
1008
+ // popup wrapper
1009
+ this.popup_wrapper = document.createElement('div');
1010
+ this.popup_wrapper.classList.add('popup-wrapper');
1011
+ this.$container.appendChild(this.popup_wrapper);
1012
+ }
1013
+
1014
+ setup_options(options) {
1015
+ const default_options = {
1016
+ header_height: 50,
1017
+ column_width: 30,
1018
+ step: 24,
1019
+ view_modes: [
1020
+ 'Quarter Day',
1021
+ 'Half Day',
1022
+ 'Day',
1023
+ 'Week',
1024
+ 'Month',
1025
+ 'Year'
1026
+ ],
1027
+ bar_height: 20,
1028
+ bar_corner_radius: 3,
1029
+ arrow_curve: 5,
1030
+ padding: 18,
1031
+ view_mode: 'Day',
1032
+ date_format: 'YYYY-MM-DD',
1033
+ popup_trigger: 'click',
1034
+ custom_popup_html: null,
1035
+ language: 'en'
1036
+ };
1037
+ this.options = Object.assign({}, default_options, options);
1038
+ }
1039
+
1040
+ setup_tasks(tasks) {
1041
+ // prepare tasks
1042
+ this.tasks = tasks.map((task, i) => {
1043
+ // convert to Date objects
1044
+ task._start = date_utils.parse(task.start);
1045
+ task._end = date_utils.parse(task.end);
1046
+
1047
+ // make task invalid if duration too large
1048
+ if (date_utils.diff(task._end, task._start, 'year') > 10) {
1049
+ task.end = null;
1050
+ }
1051
+
1052
+ // cache index
1053
+ task._index = i;
1054
+
1055
+ // invalid dates
1056
+ if (!task.start && !task.end) {
1057
+ const today = date_utils.today();
1058
+ task._start = today;
1059
+ task._end = date_utils.add(today, 2, 'day');
1060
+ }
1061
+
1062
+ if (!task.start && task.end) {
1063
+ task._start = date_utils.add(task._end, -2, 'day');
1064
+ }
1065
+
1066
+ if (task.start && !task.end) {
1067
+ task._end = date_utils.add(task._start, 2, 'day');
1068
+ }
1069
+
1070
+ // if hours is not set, assume the last day is full day
1071
+ // e.g: 2018-09-09 becomes 2018-09-09 23:59:59
1072
+ const task_end_values = date_utils.get_date_values(task._end);
1073
+ if (task_end_values.slice(3).every(d => d === 0)) {
1074
+ task._end = date_utils.add(task._end, 24, 'hour');
1075
+ }
1076
+
1077
+ // invalid flag
1078
+ if (!task.start || !task.end) {
1079
+ task.invalid = true;
1080
+ }
1081
+
1082
+ // dependencies
1083
+ if (typeof task.dependencies === 'string' || !task.dependencies) {
1084
+ let deps = [];
1085
+ if (task.dependencies) {
1086
+ deps = task.dependencies
1087
+ .split(',')
1088
+ .map(d => d.trim())
1089
+ .filter(d => d);
1090
+ }
1091
+ task.dependencies = deps;
1092
+ }
1093
+
1094
+ // uids
1095
+ if (!task.id) {
1096
+ task.id = generate_id(task);
1097
+ }
1098
+
1099
+ return task;
1100
+ });
1101
+
1102
+ this.setup_dependencies();
1103
+ }
1104
+
1105
+ setup_dependencies() {
1106
+ this.dependency_map = {};
1107
+ for (let t of this.tasks) {
1108
+ for (let d of t.dependencies) {
1109
+ this.dependency_map[d] = this.dependency_map[d] || [];
1110
+ this.dependency_map[d].push(t.id);
1111
+ }
1112
+ }
1113
+ }
1114
+
1115
+ refresh(tasks) {
1116
+ this.setup_tasks(tasks);
1117
+ this.change_view_mode();
1118
+ }
1119
+
1120
+ change_view_mode(mode = this.options.view_mode) {
1121
+ this.update_view_scale(mode);
1122
+ this.setup_dates();
1123
+ this.render();
1124
+ // fire viewmode_change event
1125
+ this.trigger_event('view_change', [mode]);
1126
+ }
1127
+
1128
+ update_view_scale(view_mode) {
1129
+ this.options.view_mode = view_mode;
1130
+
1131
+ if (view_mode === 'Day') {
1132
+ this.options.step = 24;
1133
+ this.options.column_width = 38;
1134
+ } else if (view_mode === 'Half Day') {
1135
+ this.options.step = 24 / 2;
1136
+ this.options.column_width = 38;
1137
+ } else if (view_mode === 'Quarter Day') {
1138
+ this.options.step = 24 / 4;
1139
+ this.options.column_width = 38;
1140
+ } else if (view_mode === 'Week') {
1141
+ this.options.step = 24 * 7;
1142
+ this.options.column_width = 140;
1143
+ } else if (view_mode === 'Month') {
1144
+ this.options.step = 24 * 30;
1145
+ this.options.column_width = 120;
1146
+ } else if (view_mode === 'Year') {
1147
+ this.options.step = 24 * 365;
1148
+ this.options.column_width = 120;
1149
+ }
1150
+ }
1151
+
1152
+ setup_dates() {
1153
+ this.setup_gantt_dates();
1154
+ this.setup_date_values();
1155
+ }
1156
+
1157
+ setup_gantt_dates() {
1158
+ this.gantt_start = this.gantt_end = null;
1159
+
1160
+ for (let task of this.tasks) {
1161
+ // set global start and end date
1162
+ if (!this.gantt_start || task._start < this.gantt_start) {
1163
+ this.gantt_start = task._start;
1164
+ }
1165
+ if (!this.gantt_end || task._end > this.gantt_end) {
1166
+ this.gantt_end = task._end;
1167
+ }
1168
+ }
1169
+
1170
+ this.gantt_start = date_utils.start_of(this.gantt_start, 'day');
1171
+ this.gantt_end = date_utils.start_of(this.gantt_end, 'day');
1172
+
1173
+ // add date padding on both sides
1174
+ if (this.view_is(['Quarter Day', 'Half Day'])) {
1175
+ this.gantt_start = date_utils.add(this.gantt_start, -7, 'day');
1176
+ this.gantt_end = date_utils.add(this.gantt_end, 7, 'day');
1177
+ } else if (this.view_is('Month')) {
1178
+ this.gantt_start = date_utils.start_of(this.gantt_start, 'year');
1179
+ this.gantt_end = date_utils.add(this.gantt_end, 1, 'year');
1180
+ } else if (this.view_is('Year')) {
1181
+ this.gantt_start = date_utils.add(this.gantt_start, -2, 'year');
1182
+ this.gantt_end = date_utils.add(this.gantt_end, 2, 'year');
1183
+ } else {
1184
+ this.gantt_start = date_utils.add(this.gantt_start, -1, 'month');
1185
+ this.gantt_end = date_utils.add(this.gantt_end, 1, 'month');
1186
+ }
1187
+ }
1188
+
1189
+ setup_date_values() {
1190
+ this.dates = [];
1191
+ let cur_date = null;
1192
+
1193
+ while (cur_date === null || cur_date < this.gantt_end) {
1194
+ if (!cur_date) {
1195
+ cur_date = date_utils.clone(this.gantt_start);
1196
+ } else {
1197
+ if (this.view_is('Year')) {
1198
+ cur_date = date_utils.add(cur_date, 1, 'year');
1199
+ } else if (this.view_is('Month')) {
1200
+ cur_date = date_utils.add(cur_date, 1, 'month');
1201
+ } else {
1202
+ cur_date = date_utils.add(
1203
+ cur_date,
1204
+ this.options.step,
1205
+ 'hour'
1206
+ );
1207
+ }
1208
+ }
1209
+ this.dates.push(cur_date);
1210
+ }
1211
+ }
1212
+
1213
+ bind_events() {
1214
+ this.bind_grid_click();
1215
+ this.bind_bar_events();
1216
+ }
1217
+
1218
+ render() {
1219
+ this.clear();
1220
+ this.setup_layers();
1221
+ this.make_grid();
1222
+ this.make_dates();
1223
+ this.make_bars();
1224
+ this.make_arrows();
1225
+ this.map_arrows_on_bars();
1226
+ this.set_width();
1227
+ this.set_scroll_position();
1228
+ }
1229
+
1230
+ setup_layers() {
1231
+ this.layers = {};
1232
+ const layers = ['grid', 'date', 'arrow', 'progress', 'bar', 'details'];
1233
+ // make group layers
1234
+ for (let layer of layers) {
1235
+ this.layers[layer] = createSVG('g', {
1236
+ class: layer,
1237
+ append_to: this.$svg
1238
+ });
1239
+ }
1240
+ }
1241
+
1242
+ make_grid() {
1243
+ this.make_grid_background();
1244
+ this.make_grid_rows();
1245
+ this.make_grid_header();
1246
+ this.make_grid_ticks();
1247
+ this.make_grid_highlights();
1248
+ }
1249
+
1250
+ make_grid_background() {
1251
+ const grid_width = this.dates.length * this.options.column_width;
1252
+ const grid_height =
1253
+ this.options.header_height +
1254
+ this.options.padding +
1255
+ (this.options.bar_height + this.options.padding) *
1256
+ this.tasks.length;
1257
+
1258
+ createSVG('rect', {
1259
+ x: 0,
1260
+ y: 0,
1261
+ width: grid_width,
1262
+ height: grid_height,
1263
+ class: 'grid-background',
1264
+ append_to: this.layers.grid
1265
+ });
1266
+
1267
+ $.attr(this.$svg, {
1268
+ height: grid_height + this.options.padding + 100,
1269
+ width: '100%'
1270
+ });
1271
+ }
1272
+
1273
+ make_grid_rows() {
1274
+ const rows_layer = createSVG('g', { append_to: this.layers.grid });
1275
+ const lines_layer = createSVG('g', { append_to: this.layers.grid });
1276
+
1277
+ const row_width = this.dates.length * this.options.column_width;
1278
+ const row_height = this.options.bar_height + this.options.padding;
1279
+
1280
+ let row_y = this.options.header_height + this.options.padding / 2;
1281
+
1282
+ for (let task of this.tasks) {
1283
+ createSVG('rect', {
1284
+ x: 0,
1285
+ y: row_y,
1286
+ width: row_width,
1287
+ height: row_height,
1288
+ class: 'grid-row',
1289
+ append_to: rows_layer
1290
+ });
1291
+
1292
+ createSVG('line', {
1293
+ x1: 0,
1294
+ y1: row_y + row_height,
1295
+ x2: row_width,
1296
+ y2: row_y + row_height,
1297
+ class: 'row-line',
1298
+ append_to: lines_layer
1299
+ });
1300
+
1301
+ row_y += this.options.bar_height + this.options.padding;
1302
+ }
1303
+ }
1304
+
1305
+ make_grid_header() {
1306
+ const header_width = this.dates.length * this.options.column_width;
1307
+ const header_height = this.options.header_height + 10;
1308
+ createSVG('rect', {
1309
+ x: 0,
1310
+ y: 0,
1311
+ width: header_width,
1312
+ height: header_height,
1313
+ class: 'grid-header',
1314
+ append_to: this.layers.grid
1315
+ });
1316
+ }
1317
+
1318
+ make_grid_ticks() {
1319
+ let tick_x = 0;
1320
+ let tick_y = this.options.header_height + this.options.padding / 2;
1321
+ let tick_height =
1322
+ (this.options.bar_height + this.options.padding) *
1323
+ this.tasks.length;
1324
+
1325
+ for (let date of this.dates) {
1326
+ let tick_class = 'tick';
1327
+ // thick tick for monday
1328
+ if (this.view_is('Day') && date.getDate() === 1) {
1329
+ tick_class += ' thick';
1330
+ }
1331
+ // thick tick for first week
1332
+ if (
1333
+ this.view_is('Week') &&
1334
+ date.getDate() >= 1 &&
1335
+ date.getDate() < 8
1336
+ ) {
1337
+ tick_class += ' thick';
1338
+ }
1339
+ // thick ticks for quarters
1340
+ if (this.view_is('Month') && (date.getMonth() + 1) % 3 === 0) {
1341
+ tick_class += ' thick';
1342
+ }
1343
+
1344
+ createSVG('path', {
1345
+ d: `M ${tick_x} ${tick_y} v ${tick_height}`,
1346
+ class: tick_class,
1347
+ append_to: this.layers.grid
1348
+ });
1349
+
1350
+ if (this.view_is('Month')) {
1351
+ tick_x +=
1352
+ date_utils.get_days_in_month(date) *
1353
+ this.options.column_width /
1354
+ 30;
1355
+ } else {
1356
+ tick_x += this.options.column_width;
1357
+ }
1358
+ }
1359
+ }
1360
+
1361
+ make_grid_highlights() {
1362
+ // highlight today's date
1363
+ if (this.view_is('Day')) {
1364
+ const x =
1365
+ date_utils.diff(date_utils.today(), this.gantt_start, 'hour') /
1366
+ this.options.step *
1367
+ this.options.column_width;
1368
+ const y = 0;
1369
+
1370
+ const width = this.options.column_width;
1371
+ const height =
1372
+ (this.options.bar_height + this.options.padding) *
1373
+ this.tasks.length +
1374
+ this.options.header_height +
1375
+ this.options.padding / 2;
1376
+
1377
+ createSVG('rect', {
1378
+ x,
1379
+ y,
1380
+ width,
1381
+ height,
1382
+ class: 'today-highlight',
1383
+ append_to: this.layers.grid
1384
+ });
1385
+ }
1386
+ }
1387
+
1388
+ make_dates() {
1389
+ for (let date of this.get_dates_to_draw()) {
1390
+ createSVG('text', {
1391
+ x: date.lower_x,
1392
+ y: date.lower_y,
1393
+ innerHTML: date.lower_text,
1394
+ class: 'lower-text',
1395
+ append_to: this.layers.date
1396
+ });
1397
+
1398
+ if (date.upper_text) {
1399
+ const $upper_text = createSVG('text', {
1400
+ x: date.upper_x,
1401
+ y: date.upper_y,
1402
+ innerHTML: date.upper_text,
1403
+ class: 'upper-text',
1404
+ append_to: this.layers.date
1405
+ });
1406
+
1407
+ // remove out-of-bound dates
1408
+ if (
1409
+ $upper_text.getBBox().x2 > this.layers.grid.getBBox().width
1410
+ ) {
1411
+ $upper_text.remove();
1412
+ }
1413
+ }
1414
+ }
1415
+ }
1416
+
1417
+ get_dates_to_draw() {
1418
+ let last_date = null;
1419
+ const dates = this.dates.map((date, i) => {
1420
+ const d = this.get_date_info(date, last_date, i);
1421
+ last_date = date;
1422
+ return d;
1423
+ });
1424
+ return dates;
1425
+ }
1426
+
1427
+ get_date_info(date, last_date, i) {
1428
+ if (!last_date) {
1429
+ last_date = date_utils.add(date, 1, 'year');
1430
+ }
1431
+ const date_text = {
1432
+ 'Quarter Day_lower': date_utils.format(
1433
+ date,
1434
+ 'HH',
1435
+ this.options.language
1436
+ ),
1437
+ 'Half Day_lower': date_utils.format(
1438
+ date,
1439
+ 'HH',
1440
+ this.options.language
1441
+ ),
1442
+ Day_lower:
1443
+ date.getDate() !== last_date.getDate()
1444
+ ? date_utils.format(date, 'D', this.options.language)
1445
+ : '',
1446
+ Week_lower:
1447
+ date.getMonth() !== last_date.getMonth()
1448
+ ? date_utils.format(date, 'D MMM', this.options.language)
1449
+ : date_utils.format(date, 'D', this.options.language),
1450
+ Month_lower: date_utils.format(date, 'MMMM', this.options.language),
1451
+ Year_lower: date_utils.format(date, 'YYYY', this.options.language),
1452
+ 'Quarter Day_upper':
1453
+ date.getDate() !== last_date.getDate()
1454
+ ? date_utils.format(date, 'D MMM', this.options.language)
1455
+ : '',
1456
+ 'Half Day_upper':
1457
+ date.getDate() !== last_date.getDate()
1458
+ ? date.getMonth() !== last_date.getMonth()
1459
+ ? date_utils.format(date, 'D MMM', this.options.language)
1460
+ : date_utils.format(date, 'D', this.options.language)
1461
+ : '',
1462
+ Day_upper:
1463
+ date.getMonth() !== last_date.getMonth()
1464
+ ? date_utils.format(date, 'MMMM', this.options.language)
1465
+ : '',
1466
+ Week_upper:
1467
+ date.getMonth() !== last_date.getMonth()
1468
+ ? date_utils.format(date, 'MMMM', this.options.language)
1469
+ : '',
1470
+ Month_upper:
1471
+ date.getFullYear() !== last_date.getFullYear()
1472
+ ? date_utils.format(date, 'YYYY', this.options.language)
1473
+ : '',
1474
+ Year_upper:
1475
+ date.getFullYear() !== last_date.getFullYear()
1476
+ ? date_utils.format(date, 'YYYY', this.options.language)
1477
+ : ''
1478
+ };
1479
+
1480
+ const base_pos = {
1481
+ x: i * this.options.column_width,
1482
+ lower_y: this.options.header_height,
1483
+ upper_y: this.options.header_height - 25
1484
+ };
1485
+
1486
+ const x_pos = {
1487
+ 'Quarter Day_lower': this.options.column_width * 4 / 2,
1488
+ 'Quarter Day_upper': 0,
1489
+ 'Half Day_lower': this.options.column_width * 2 / 2,
1490
+ 'Half Day_upper': 0,
1491
+ Day_lower: this.options.column_width / 2,
1492
+ Day_upper: this.options.column_width * 30 / 2,
1493
+ Week_lower: 0,
1494
+ Week_upper: this.options.column_width * 4 / 2,
1495
+ Month_lower: this.options.column_width / 2,
1496
+ Month_upper: this.options.column_width * 12 / 2,
1497
+ Year_lower: this.options.column_width / 2,
1498
+ Year_upper: this.options.column_width * 30 / 2
1499
+ };
1500
+
1501
+ return {
1502
+ upper_text: date_text[`${this.options.view_mode}_upper`],
1503
+ lower_text: date_text[`${this.options.view_mode}_lower`],
1504
+ upper_x: base_pos.x + x_pos[`${this.options.view_mode}_upper`],
1505
+ upper_y: base_pos.upper_y,
1506
+ lower_x: base_pos.x + x_pos[`${this.options.view_mode}_lower`],
1507
+ lower_y: base_pos.lower_y
1508
+ };
1509
+ }
1510
+
1511
+ make_bars() {
1512
+ this.bars = this.tasks.map(task => {
1513
+ const bar = new Bar(this, task);
1514
+ this.layers.bar.appendChild(bar.group);
1515
+ return bar;
1516
+ });
1517
+ }
1518
+
1519
+ make_arrows() {
1520
+ this.arrows = [];
1521
+ for (let task of this.tasks) {
1522
+ let arrows = [];
1523
+ arrows = task.dependencies
1524
+ .map(task_id => {
1525
+ const dependency = this.get_task(task_id);
1526
+ if (!dependency) return;
1527
+ const arrow = new Arrow(
1528
+ this,
1529
+ this.bars[dependency._index], // from_task
1530
+ this.bars[task._index] // to_task
1531
+ );
1532
+ this.layers.arrow.appendChild(arrow.element);
1533
+ return arrow;
1534
+ })
1535
+ .filter(Boolean); // filter falsy values
1536
+ this.arrows = this.arrows.concat(arrows);
1537
+ }
1538
+ }
1539
+
1540
+ map_arrows_on_bars() {
1541
+ for (let bar of this.bars) {
1542
+ bar.arrows = this.arrows.filter(arrow => {
1543
+ return (
1544
+ arrow.from_task.task.id === bar.task.id ||
1545
+ arrow.to_task.task.id === bar.task.id
1546
+ );
1547
+ });
1548
+ }
1549
+ }
1550
+
1551
+ set_width() {
1552
+ const cur_width = this.$svg.getBoundingClientRect().width;
1553
+ const actual_width = this.$svg
1554
+ .querySelector('.grid .grid-row')
1555
+ .getAttribute('width');
1556
+ if (cur_width < actual_width) {
1557
+ this.$svg.setAttribute('width', actual_width);
1558
+ }
1559
+ }
1560
+
1561
+ set_scroll_position() {
1562
+ const parent_element = this.$svg.parentElement;
1563
+ if (!parent_element) return;
1564
+
1565
+ const hours_before_first_task = date_utils.diff(
1566
+ this.get_oldest_starting_date(),
1567
+ this.gantt_start,
1568
+ 'hour'
1569
+ );
1570
+
1571
+ const scroll_pos =
1572
+ hours_before_first_task /
1573
+ this.options.step *
1574
+ this.options.column_width -
1575
+ this.options.column_width;
1576
+
1577
+ parent_element.scrollLeft = scroll_pos;
1578
+ }
1579
+
1580
+ bind_grid_click() {
1581
+ $.on(
1582
+ this.$svg,
1583
+ this.options.popup_trigger,
1584
+ '.grid-row, .grid-header',
1585
+ () => {
1586
+ this.unselect_all();
1587
+ this.hide_popup();
1588
+ }
1589
+ );
1590
+ }
1591
+
1592
+ bind_bar_events() {
1593
+ let is_dragging = false;
1594
+ let x_on_start = 0;
1595
+ let y_on_start = 0;
1596
+ let is_resizing_left = false;
1597
+ let is_resizing_right = false;
1598
+ let parent_bar_id = null;
1599
+ let bars = []; // instanceof Bar
1600
+ this.bar_being_dragged = null;
1601
+
1602
+ function action_in_progress() {
1603
+ return is_dragging || is_resizing_left || is_resizing_right;
1604
+ }
1605
+
1606
+ $.on(this.$svg, 'mousedown', '.bar-wrapper, .handle', (e, element) => {
1607
+ const bar_wrapper = $.closest('.bar-wrapper', element);
1608
+
1609
+ if (element.classList.contains('left')) {
1610
+ is_resizing_left = false;
1611
+ } else if (element.classList.contains('right')) {
1612
+ is_resizing_right = false;
1613
+ } else if (element.classList.contains('bar-wrapper')) {
1614
+ is_dragging = false;
1615
+ }
1616
+
1617
+ bar_wrapper.classList.add('active');
1618
+
1619
+ x_on_start = e.offsetX;
1620
+ y_on_start = e.offsetY;
1621
+
1622
+ parent_bar_id = bar_wrapper.getAttribute('data-id');
1623
+ const ids = [
1624
+ parent_bar_id,
1625
+ ...this.get_all_dependent_tasks(parent_bar_id)
1626
+ ];
1627
+ bars = ids.map(id => this.get_bar(id));
1628
+
1629
+ this.bar_being_dragged = parent_bar_id;
1630
+
1631
+ bars.forEach(bar => {
1632
+ const $bar = bar.$bar;
1633
+ $bar.ox = $bar.getX();
1634
+ $bar.oy = $bar.getY();
1635
+ $bar.owidth = $bar.getWidth();
1636
+ $bar.finaldx = 0;
1637
+ });
1638
+ });
1639
+
1640
+ $.on(this.$svg, 'mousemove', e => {
1641
+ if (!action_in_progress()) return;
1642
+ const dx = e.offsetX - x_on_start;
1643
+ const dy = e.offsetY - y_on_start;
1644
+
1645
+ bars.forEach(bar => {
1646
+ const $bar = bar.$bar;
1647
+ $bar.finaldx = this.get_snap_position(dx);
1648
+
1649
+ if (is_resizing_left) {
1650
+ if (parent_bar_id === bar.task.id) {
1651
+ bar.update_bar_position({
1652
+ x: $bar.ox + $bar.finaldx,
1653
+ width: $bar.owidth - $bar.finaldx
1654
+ });
1655
+ } else {
1656
+ bar.update_bar_position({
1657
+ x: $bar.ox + $bar.finaldx
1658
+ });
1659
+ }
1660
+ } else if (is_resizing_right) {
1661
+ if (parent_bar_id === bar.task.id) {
1662
+ bar.update_bar_position({
1663
+ width: $bar.owidth + $bar.finaldx
1664
+ });
1665
+ }
1666
+ } else if (is_dragging) {
1667
+ bar.update_bar_position({ x: $bar.ox + $bar.finaldx });
1668
+ }
1669
+ });
1670
+ });
1671
+
1672
+ document.addEventListener('mouseup', e => {
1673
+ if (is_dragging || is_resizing_left || is_resizing_right) {
1674
+ bars.forEach(bar => bar.group.classList.remove('active'));
1675
+ }
1676
+
1677
+ is_dragging = false;
1678
+ is_resizing_left = false;
1679
+ is_resizing_right = false;
1680
+ });
1681
+
1682
+ $.on(this.$svg, 'mouseup', e => {
1683
+ this.bar_being_dragged = null;
1684
+ bars.forEach(bar => {
1685
+ const $bar = bar.$bar;
1686
+ if (!$bar.finaldx) return;
1687
+ bar.date_changed();
1688
+ bar.set_action_completed();
1689
+ });
1690
+ });
1691
+
1692
+ this.bind_bar_progress();
1693
+ }
1694
+
1695
+ bind_bar_progress() {
1696
+ let x_on_start = 0;
1697
+ let y_on_start = 0;
1698
+ let is_resizing = null;
1699
+ let bar = null;
1700
+ let $bar_progress = null;
1701
+ let $bar = null;
1702
+
1703
+ $.on(this.$svg, 'mousedown', '.handle.progress', (e, handle) => {
1704
+ is_resizing = true;
1705
+ x_on_start = e.offsetX;
1706
+ y_on_start = e.offsetY;
1707
+
1708
+ const $bar_wrapper = $.closest('.bar-wrapper', handle);
1709
+ const id = $bar_wrapper.getAttribute('data-id');
1710
+ bar = this.get_bar(id);
1711
+
1712
+ $bar_progress = bar.$bar_progress;
1713
+ $bar = bar.$bar;
1714
+
1715
+ $bar_progress.finaldx = 0;
1716
+ $bar_progress.owidth = $bar_progress.getWidth();
1717
+ $bar_progress.min_dx = -$bar_progress.getWidth();
1718
+ $bar_progress.max_dx = $bar.getWidth() - $bar_progress.getWidth();
1719
+ });
1720
+
1721
+ $.on(this.$svg, 'mousemove', e => {
1722
+ if (!is_resizing) return;
1723
+ let dx = e.offsetX - x_on_start;
1724
+ let dy = e.offsetY - y_on_start;
1725
+
1726
+ if (dx > $bar_progress.max_dx) {
1727
+ dx = $bar_progress.max_dx;
1728
+ }
1729
+ if (dx < $bar_progress.min_dx) {
1730
+ dx = $bar_progress.min_dx;
1731
+ }
1732
+
1733
+ const $handle = bar.$handle_progress;
1734
+ $.attr($bar_progress, 'width', $bar_progress.owidth + dx);
1735
+ $.attr($handle, 'points', bar.get_progress_polygon_points());
1736
+ $bar_progress.finaldx = dx;
1737
+ });
1738
+
1739
+ $.on(this.$svg, 'mouseup', () => {
1740
+ is_resizing = false;
1741
+ if (!($bar_progress && $bar_progress.finaldx)) return;
1742
+ bar.progress_changed();
1743
+ bar.set_action_completed();
1744
+ });
1745
+ }
1746
+
1747
+ get_all_dependent_tasks(task_id) {
1748
+ let out = [];
1749
+ let to_process = [task_id];
1750
+ while (to_process.length) {
1751
+ const deps = to_process.reduce((acc, curr) => {
1752
+ acc = acc.concat(this.dependency_map[curr]);
1753
+ return acc;
1754
+ }, []);
1755
+
1756
+ out = out.concat(deps);
1757
+ to_process = deps.filter(d => !to_process.includes(d));
1758
+ }
1759
+
1760
+ return out.filter(Boolean);
1761
+ }
1762
+
1763
+ get_snap_position(dx) {
1764
+ let odx = dx,
1765
+ rem,
1766
+ position;
1767
+
1768
+ if (this.view_is('Week')) {
1769
+ rem = dx % (this.options.column_width / 7);
1770
+ position =
1771
+ odx -
1772
+ rem +
1773
+ (rem < this.options.column_width / 14
1774
+ ? 0
1775
+ : this.options.column_width / 7);
1776
+ } else if (this.view_is('Month')) {
1777
+ rem = dx % (this.options.column_width / 30);
1778
+ position =
1779
+ odx -
1780
+ rem +
1781
+ (rem < this.options.column_width / 60
1782
+ ? 0
1783
+ : this.options.column_width / 30);
1784
+ } else {
1785
+ rem = dx % this.options.column_width;
1786
+ position =
1787
+ odx -
1788
+ rem +
1789
+ (rem < this.options.column_width / 2
1790
+ ? 0
1791
+ : this.options.column_width);
1792
+ }
1793
+ return position;
1794
+ }
1795
+
1796
+ unselect_all() {
1797
+ [...this.$svg.querySelectorAll('.bar-wrapper')].forEach(el => {
1798
+ el.classList.remove('active');
1799
+ });
1800
+ }
1801
+
1802
+ view_is(modes) {
1803
+ if (typeof modes === 'string') {
1804
+ return this.options.view_mode === modes;
1805
+ }
1806
+
1807
+ if (Array.isArray(modes)) {
1808
+ return modes.some(mode => this.options.view_mode === mode);
1809
+ }
1810
+
1811
+ return false;
1812
+ }
1813
+
1814
+ get_task(id) {
1815
+ return this.tasks.find(task => {
1816
+ return task.id === id;
1817
+ });
1818
+ }
1819
+
1820
+ get_bar(id) {
1821
+ return this.bars.find(bar => {
1822
+ return bar.task.id === id;
1823
+ });
1824
+ }
1825
+
1826
+ show_popup(options) {
1827
+ if (!this.popup) {
1828
+ this.popup = new Popup(
1829
+ this.popup_wrapper,
1830
+ this.options.custom_popup_html
1831
+ );
1832
+ }
1833
+ this.popup.show(options);
1834
+ }
1835
+
1836
+ hide_popup() {
1837
+ this.popup && this.popup.hide();
1838
+ }
1839
+
1840
+ trigger_event(event, args) {
1841
+ if (this.options['on_' + event]) {
1842
+ this.options['on_' + event].apply(null, args);
1843
+ }
1844
+ }
1845
+
1846
+ /**
1847
+ * Gets the oldest starting date from the list of tasks
1848
+ *
1849
+ * @returns Date
1850
+ * @memberof Gantt
1851
+ */
1852
+ get_oldest_starting_date() {
1853
+ return this.tasks
1854
+ .map(task => task._start)
1855
+ .reduce(
1856
+ (prev_date, cur_date) =>
1857
+ cur_date <= prev_date ? cur_date : prev_date
1858
+ );
1859
+ }
1860
+
1861
+ /**
1862
+ * Clear all elements from the parent svg element
1863
+ *
1864
+ * @memberof Gantt
1865
+ */
1866
+ clear() {
1867
+ this.$svg.innerHTML = '';
1868
+ }
1869
+ }
1870
+
1871
+ function generate_id(task) {
1872
+ return (
1873
+ task.name +
1874
+ '_' +
1875
+ Math.random()
1876
+ .toString(36)
1877
+ .slice(2, 12)
1878
+ );
1879
+ }
1880
+
1881
+ return Gantt;
1882
+
1883
+ }());