tltv-timeline-element 1.0.24 → 1.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +148 -10
  2. package/custom-elements.json +4201 -0
  3. package/dist/src/model/DefaultLocaleDataProvider.d.ts +35 -27
  4. package/dist/src/model/DefaultLocaleDataProvider.d.ts.map +1 -0
  5. package/dist/src/model/DefaultLocaleDataProvider.js +78 -111
  6. package/dist/src/model/DefaultLocaleDataProvider.js.map +1 -1
  7. package/dist/src/model/ILocaleDataProvider.d.ts +71 -62
  8. package/dist/src/model/ILocaleDataProvider.d.ts.map +1 -0
  9. package/dist/src/model/ILocaleDataProvider.js +1 -1
  10. package/dist/src/model/IResolutionBlockFiller.d.ts +5 -4
  11. package/dist/src/model/IResolutionBlockFiller.d.ts.map +1 -0
  12. package/dist/src/model/IResolutionBlockFiller.js +1 -1
  13. package/dist/src/model/IResolutionBlockRegisterer.d.ts +4 -3
  14. package/dist/src/model/IResolutionBlockRegisterer.d.ts.map +1 -0
  15. package/dist/src/model/IResolutionBlockRegisterer.js +1 -1
  16. package/dist/src/model/Resolution.d.ts +8 -7
  17. package/dist/src/model/Resolution.d.ts.map +1 -0
  18. package/dist/src/model/Resolution.js +8 -8
  19. package/dist/src/model/Weekday.d.ts +6 -5
  20. package/dist/src/model/Weekday.d.ts.map +1 -0
  21. package/dist/src/model/Weekday.js +6 -6
  22. package/dist/src/model/blockRowData.d.ts +12 -11
  23. package/dist/src/model/blockRowData.d.ts.map +1 -0
  24. package/dist/src/model/blockRowData.js +28 -28
  25. package/dist/src/model/blockRowData.js.map +1 -1
  26. package/dist/src/timeline-element.d.ts +406 -388
  27. package/dist/src/timeline-element.d.ts.map +1 -0
  28. package/dist/src/timeline-element.js +1987 -1987
  29. package/dist/src/timeline-element.js.map +1 -1
  30. package/dist/src/util/dateTimeUtil.d.ts +21 -14
  31. package/dist/src/util/dateTimeUtil.d.ts.map +1 -0
  32. package/dist/src/util/dateTimeUtil.js +42 -55
  33. package/dist/src/util/dateTimeUtil.js.map +1 -1
  34. package/dist/src/util/elementUtil.d.ts +5 -4
  35. package/dist/src/util/elementUtil.d.ts.map +1 -0
  36. package/dist/src/util/elementUtil.js +48 -48
  37. package/dist/src/util/elementUtil.js.map +1 -1
  38. package/dist/src/util/zonedTime.d.ts +85 -0
  39. package/dist/src/util/zonedTime.d.ts.map +1 -0
  40. package/dist/src/util/zonedTime.js +239 -0
  41. package/dist/src/util/zonedTime.js.map +1 -0
  42. package/package.json +53 -24
  43. package/src/model/DefaultLocaleDataProvider.ts +32 -66
  44. package/src/model/ILocaleDataProvider.ts +10 -2
  45. package/src/model/IResolutionBlockFiller.ts +1 -1
  46. package/src/model/IResolutionBlockRegisterer.ts +1 -1
  47. package/src/model/blockRowData.ts +4 -4
  48. package/src/timeline-element.ts +380 -372
  49. package/src/util/dateTimeUtil.ts +16 -29
  50. package/src/util/elementUtil.ts +4 -4
  51. package/src/util/zonedTime.ts +280 -0
  52. package/server.js +0 -18
  53. package/src/index.html +0 -112
  54. package/tsconfig.json +0 -15
  55. package/webpack.config.js +0 -33
@@ -1,2014 +1,2014 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- var TimelineElement_1;
8
- import { LitElement, html, css, nothing } from 'lit';
9
- import { property, customElement } from 'lit/decorators.js';
10
- import { toDate } from 'date-fns-tz';
11
- import { Resolution } from './model/Resolution';
12
- import { Weekday } from './model/Weekday';
13
- import { BlockRowData } from './model/blockRowData';
14
- import * as DateUtil from './util/dateTimeUtil';
15
- import { DateTimeConstants } from './util/dateTimeUtil';
16
- import * as ElementUtil from './util/elementUtil';
17
- import { DefaultLocaleDataProvider } from './model/DefaultLocaleDataProvider';
18
- import { getISOWeek } from 'date-fns';
19
- import { query } from 'lit-element/decorators.js';
20
- /**
21
- * Scalable timeline web component that supports more than one
22
- * resolutions ({@link Resolution}). When timeline element doesn't overflow
23
- * horizontally in it's parent element, it scales the content width up to fit in
24
- * the space available.
25
- * <p>
26
- * When this component scales up, all widths are calculated as percentages.
27
- * Pixel widths are used otherwise. Some browsers may not support percentages
28
- * accurately enough, and for those it's best to call
29
- * {@link #setAlwaysCalculatePixelWidths(boolean)} with 'true' to disable
30
- * percentage values.
31
- * <p>
32
- * There's always a minimum width calculated and updated to the timeline
33
- * element. Percentage values set some limitation for the component's width.
34
- * Wider the component (&gt; 4000px), bigger the chance to get year, month and
35
- * date blocks not being vertically in-line with each others.
36
- * <p>
37
- * Supports setting a scroll left position.
38
- * <p>
39
- * After construction, attach the component to it's parent and call update
40
- * method with a required parameters and the timeline is ready. After that, all
41
- * widths are calculated and all other API methods available can be used safely.
42
- *
43
- */
44
- export let TimelineElement = TimelineElement_1 = class TimelineElement extends LitElement {
45
- constructor() {
46
- super(...arguments);
47
- this.resolutionWeekDayblockWidth = 4;
48
- /* Default minimum width (px) of one day inside a month resolution block. */
49
- this.resolutionMonthDayblockWidth = 4;
50
- /* Default minimum width (px) of one day inside a year resolution block. */
51
- this.resolutionYearDayblockWidth = 1;
52
- this.resolution = Resolution.Day;
53
- /** Timezone for date and time formatting. Doesn't match with actual start and end Date object's time zone. */
54
- this.timeZone = "Europe/London";
55
- this.locale = "en-US";
56
- this.firstDayOfWeek = 1; // sunday;
57
- this.twelveHourClock = false;
58
- this.monthRowVisible = true;
59
- this.yearRowVisible = true;
60
- /* When true, the resolution block (and the day row block) covering the current date/time is highlighted with 'current' style. */
61
- this.currentDateVisible = false;
62
- /* Current day formatted as 'yyyy-MM-dd' in the timeline's time zone. Refreshed before each fill. */
63
- this.currentDayString = null;
64
- this.currentHourString = null;
65
- /*
66
- * number of blocks in resolution range. Days for Day/Week/Month/Year
67
- * resolution, Hours for hour resolution..
68
- */
69
- this.blocksInRange = 0;
70
- /*
71
- * number of elements in resolution range. Same as blocksInRange for
72
- * Day/Hour resolution. blocksInRange / 7 for Week resolution. Number of
73
- * (partial) months/years for Month/Year resolution.
74
- */
75
- this.resolutionBlockCount = 0;
76
- this.firstResBlockCount = 0;
77
- this.lastResBlockCount = 0;
78
- /*
79
- * Month/Year resolution only: number of days in each resolution block, the
80
- * date of the first day in each block and a key ('yyyy-MM' or 'yyyy')
81
- * identifying the block. Blocks have variable length (28-31 or 365-366 days,
82
- * first and last block may be partial).
83
- */
84
- this.resBlockDayCounts = [];
85
- this.resBlockStartDates = [];
86
- this.resBlockKeys = [];
87
- this.spacerBlocks = [];
88
- this.yearRowData = new BlockRowData();
89
- this.monthRowData = new BlockRowData();
90
- // days/daysLength are needed only with resolutions smaller than Day.
91
- this.dayRowData = new BlockRowData();
92
- /*
93
- * Currently active widths. Updated each time when timeline column widths
94
- * are updated.
95
- */
96
- this.dayWidthPercentage = 0;
97
- this.dayOrHourWidthPx = 0;
98
- this.resBlockMinWidthPx = 0;
99
- this.resBlockWidthPx = 0;
100
- this.resBlockWidthPercentage = 0;
101
- this.minResolutionWidth = -1;
102
- this.calcPixels = false;
103
- this.positionLeft = 0;
104
- this.setPositionForEachBlock = false;
105
- this.firstWeekBlockHidden = false;
106
- this.ie = false; // deprecated property
107
- /* directlyInsideScrollContainer:
108
- true: timeline element is a child element inside a container with scroll bar.
109
- false: timeline.style.left is adjusted by scrollHandler. */
110
- this.directlyInsideScrollContainer = true;
111
- this.previousContainerScrollLeft = 0;
112
- this.previousContainerScrollTop = 0;
113
- }
114
- connectedCallback() {
115
- super.connectedCallback();
116
- if (this.scrollContainer && this.scrollHandler) {
117
- this.scrollContainer.addEventListener('scroll', this.scrollHandler);
118
- }
119
- }
120
- disconnectedCallback() {
121
- super.disconnectedCallback();
122
- if (this.scrollContainer && this.scrollHandler) {
123
- this.scrollContainer.removeEventListener('scroll', this.scrollHandler);
124
- }
125
- }
126
- static get styles() {
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var TimelineElement_1;
8
+ import { LitElement, html, css, nothing } from 'lit';
9
+ import { customElement, property, query } from 'lit/decorators.js';
10
+ import { Resolution } from './model/Resolution.js';
11
+ import { Weekday } from './model/Weekday.js';
12
+ import { BlockRowData } from './model/blockRowData.js';
13
+ import * as DateUtil from './util/dateTimeUtil.js';
14
+ import { DateTimeConstants } from './util/dateTimeUtil.js';
15
+ import * as ElementUtil from './util/elementUtil.js';
16
+ import { addDaysInZone, getZonedFields, isoWeekOfYear, parseWallTime, zonedWallTimeToInstant } from './util/zonedTime.js';
17
+ import { DefaultLocaleDataProvider } from './model/DefaultLocaleDataProvider.js';
18
+ /* Attribute converter: truncates a date-time to hour accuracy ("yyyy-MM-ddTHH"). */
19
+ function toHourAccuracy(value) {
20
+ return (value && value.length > 13) ? value.substring(0, 13) : value;
21
+ }
22
+ /* Attribute converter for default-true flags: only the value "false" turns them off. */
23
+ function visibleUnlessFalse(value) {
24
+ return value !== 'false';
25
+ }
26
+ /**
27
+ * Scalable, time zone aware timeline header. Renders a date range as year,
28
+ * month and day rows above a resolution row of hours, days, weeks, months or
29
+ * years ({@link Resolution}), and provides an API for converting between dates
30
+ * and horizontal positions.
31
+ *
32
+ * When the range fits in the parent element, the timeline stretches to fill it
33
+ * and widths are calculated as percentages. Otherwise blocks get their minimum
34
+ * width, the timeline scrolls horizontally with its scroll container and only
35
+ * the visible resolution blocks are rendered.
36
+ *
37
+ * Set `startDateTime`, `endDateTime` and `resolution`; the timeline renders and
38
+ * calculates its widths when attached. The positioning API can be used after
39
+ * the first update has completed.
40
+ *
41
+ * @summary Scalable, time zone aware timeline header for scheduling views.
42
+ *
43
+ * @cssprop [--timeline-row-background=#d0d0d0] - Background of header rows (year, month, day).
44
+ * @cssprop [--timeline-row-even-background=#ddd] - Background of every even month and day block in the header.
45
+ * @cssprop [--timeline-row-height=15px] - Height of a header row.
46
+ * @cssprop [--timeline-row-font-size=10px] - Font size of a header row.
47
+ * @cssprop [--timeline-col-background-color=#ddd] - Background of resolution blocks.
48
+ * @cssprop [--timeline-col-even-background-color=#ccc] - Background of even resolution blocks.
49
+ * @cssprop [--timeline-col-weekend=#ccc] - Background of weekend resolution blocks.
50
+ * @cssprop [--timeline-col-font-size=10px] - Font size of resolution blocks.
51
+ * @cssprop [--timeline-col-current=#ffe082] - Background of the current-date highlight.
52
+ * @cssprop --timeline-col-min-width - Minimum width of a day or hour block. Default: width of the text "MM".
53
+ * @cssprop --timeline-col-week-min-width - Minimum width of a week block. Default: 4px per day.
54
+ * @cssprop --timeline-col-month-min-width - Minimum width of a 30 day month block. Default: 4px per day.
55
+ * @cssprop --timeline-col-year-min-width - Minimum width of a 365 day year block. Default: 1px per day.
56
+ */
57
+ let TimelineElement = TimelineElement_1 = class TimelineElement extends LitElement {
58
+ constructor() {
59
+ super(...arguments);
60
+ this.resolutionWeekDayblockWidth = 4;
61
+ /* Default minimum width (px) of one day inside a month resolution block. */
62
+ this.resolutionMonthDayblockWidth = 4;
63
+ /* Default minimum width (px) of one day inside a year resolution block. */
64
+ this.resolutionYearDayblockWidth = 1;
65
+ this.resolution = Resolution.Day;
66
+ /** Timezone for date and time formatting. Doesn't match with actual start and end Date object's time zone. */
67
+ this.timeZone = "Europe/London";
68
+ this.locale = "en-US";
69
+ /** First day of the week, 1 (Sunday) to 7 (Saturday). */
70
+ this.firstDayOfWeek = 1;
71
+ this.twelveHourClock = false;
72
+ /** Show the month header row. Visible by default; hide with `monthrowvisible="false"`. */
73
+ this.monthRowVisible = true;
74
+ /** Show the year header row. Visible by default; hide with `yearrowvisible="false"`. */
75
+ this.yearRowVisible = true;
76
+ /* When true, the resolution block (and the day row block) covering the current date/time is highlighted with 'current' style. */
77
+ this.currentDateVisible = false;
78
+ /** Log diagnostic messages to the browser console. */
79
+ this.debug = false;
80
+ /* Current day formatted as 'yyyy-MM-dd' in the timeline's time zone. Refreshed before each fill. */
81
+ this.currentDayString = null;
82
+ this.currentHourString = null;
83
+ /*
84
+ * number of blocks in resolution range. Days for Day/Week/Month/Year
85
+ * resolution, Hours for hour resolution..
86
+ */
87
+ this.blocksInRange = 0;
88
+ /*
89
+ * number of elements in resolution range. Same as blocksInRange for
90
+ * Day/Hour resolution. blocksInRange / 7 for Week resolution. Number of
91
+ * (partial) months/years for Month/Year resolution.
92
+ */
93
+ this.resolutionBlockCount = 0;
94
+ this.firstResBlockCount = 0;
95
+ this.lastResBlockCount = 0;
96
+ /*
97
+ * Month/Year resolution only: number of days in each resolution block, the
98
+ * date of the first day in each block and a key ('yyyy-MM' or 'yyyy')
99
+ * identifying the block. Blocks have variable length (28-31 or 365-366 days,
100
+ * first and last block may be partial).
101
+ */
102
+ this.resBlockDayCounts = [];
103
+ this.resBlockStartDates = [];
104
+ this.resBlockKeys = [];
105
+ this.spacerBlocks = [];
106
+ this.yearRowData = new BlockRowData();
107
+ this.monthRowData = new BlockRowData();
108
+ // days/daysLength are needed only with resolutions smaller than Day.
109
+ this.dayRowData = new BlockRowData();
110
+ /*
111
+ * Currently active widths. Updated each time when timeline column widths
112
+ * are updated.
113
+ */
114
+ this.dayWidthPercentage = 0;
115
+ this.dayOrHourWidthPx = 0;
116
+ this.resBlockMinWidthPx = 0;
117
+ this.resBlockWidthPx = 0;
118
+ this.resBlockWidthPercentage = 0;
119
+ this.minResolutionWidth = -1;
120
+ this.calcPixels = false;
121
+ this.positionLeft = 0;
122
+ this.setPositionForEachBlock = false;
123
+ this.firstWeekBlockHidden = false;
124
+ this.ie = false; // deprecated property
125
+ /* directlyInsideScrollContainer:
126
+ true: timeline element is a child element inside a container with scroll bar.
127
+ false: timeline.style.left is adjusted by scrollHandler. */
128
+ this.directlyInsideScrollContainer = true;
129
+ this.previousContainerScrollLeft = 0;
130
+ this.previousContainerScrollTop = 0;
131
+ }
132
+ connectedCallback() {
133
+ super.connectedCallback();
134
+ if (this.scrollContainer && this.scrollHandler) {
135
+ this.scrollContainer.addEventListener('scroll', this.scrollHandler);
136
+ }
137
+ }
138
+ disconnectedCallback() {
139
+ super.disconnectedCallback();
140
+ if (this.scrollContainer && this.scrollHandler) {
141
+ this.scrollContainer.removeEventListener('scroll', this.scrollHandler);
142
+ }
143
+ }
144
+ static get styles() {
127
145
  return css `
128
146
  :host {
129
147
  display: block;
130
148
  overflow: hidden;
131
149
  position: relative;
132
-
133
- --no-user-select: {
134
- -webkit-user-select: none;
135
- -khtml-user-select: none;
136
- -moz-user-select: none;
137
- -ms-user-select: none;
138
- user-select: none;
139
- }
140
150
  }
141
151
  :host([hidden]) {
142
152
  display: none;
143
153
  }
144
-
154
+
145
155
  .year,
146
- .month,
147
- .day {
148
- padding-left: 2px;
149
- text-overflow: ellipsis;
150
- white-space: nowrap;
151
- border-right: 1px solid #A9A9A9;
152
- box-sizing: border-box;
153
- -moz-box-sizing: border-box;
154
- -webkit-box-sizing: border-box;
155
- }
156
- .year.spacer,
157
- .month.spacer,
158
- .day.spacer {
159
- padding-left: 0px;
160
- }
156
+ .month,
157
+ .day {
158
+ padding-left: 2px;
159
+ text-overflow: ellipsis;
160
+ white-space: nowrap;
161
+ border-right: 1px solid #A9A9A9;
162
+ box-sizing: border-box;
163
+ }
164
+ .year.spacer,
165
+ .month.spacer,
166
+ .day.spacer {
167
+ padding-left: 0px;
168
+ }
161
169
 
162
- .month:nth-of-type(even),
163
- .day:nth-of-type(even) {
164
- background-color: var(--timeline-row-even-background, #ddd);
165
- }
166
- .col.even {
167
- background-color: var(--timeline-col-even-background-color, #ccc);
168
- }
170
+ .month:nth-of-type(even),
171
+ .day:nth-of-type(even) {
172
+ background-color: var(--timeline-row-even-background, #ddd);
173
+ }
174
+ .col.even {
175
+ background-color: var(--timeline-col-even-background-color, #ccc);
176
+ }
169
177
 
170
- .col {
178
+ .col {
171
179
  position: var(--timeline-col-position);
172
- left: var(--timeline-col-left);
173
- height: 100%;
174
- float: left;
175
- overflow: hidden;
176
- border-right: 1px solid #A9A9A9;
177
- background-color: var(--timeline-col-background-color, #ddd);
178
- font-size: var(--timeline-col-font-size, 10px);
179
- text-align: center;
180
- box-sizing: border-box;
181
- -moz-box-sizing: border-box;
182
- -webkit-box-sizing: border-box;
183
- -webkit-touch-callout: none;
184
- @apply --no-user-select;
185
- }
180
+ left: var(--timeline-col-left);
181
+ height: 100%;
182
+ float: left;
183
+ overflow: hidden;
184
+ border-right: 1px solid #A9A9A9;
185
+ background-color: var(--timeline-col-background-color, #ddd);
186
+ font-size: var(--timeline-col-font-size, 10px);
187
+ text-align: center;
188
+ box-sizing: border-box;
189
+ -webkit-touch-callout: none;
190
+ -webkit-user-select: none;
191
+ user-select: none;
192
+ }
186
193
 
187
194
  .c-col {
188
- width: var(--timeline-col-center-width);
189
- }
195
+ width: var(--timeline-col-center-width);
196
+ }
190
197
 
191
- .f-col {
192
- width: var(--timeline-col-first-width);
193
- }
198
+ .f-col {
199
+ width: var(--timeline-col-first-width);
200
+ }
194
201
 
195
- .l-col {
196
- width: var(--timeline-col-last-width);
202
+ .l-col {
203
+ width: var(--timeline-col-last-width);
197
204
  }
198
-
199
- .col.w,
200
- .col.m,
201
- .col.y {
202
- text-align: left;
203
- }
204
205
 
205
- .col.weekend {
206
- background-color: var(--timeline-col-weekend, #ccc);
207
- }
206
+ .col.w,
207
+ .col.m,
208
+ .col.y {
209
+ text-align: left;
210
+ }
208
211
 
209
- .col.current,
210
- .day.current {
211
- background-color: var(--timeline-col-current, #ffe082);
212
- }
212
+ .col.weekend {
213
+ background-color: var(--timeline-col-weekend, #ccc);
214
+ }
213
215
 
214
- .col.measure {
215
- // Change min-width to adjust grid's cell width with day and hour-resolution.
216
- //min-width: 40px;
217
- }
218
- .col.w.measure {
219
- // Change min-width to adjust grid's cell width with week-resolution.
220
- //min-width: 70px;
221
- }
222
- .col.m.measure {
223
- // Change min-width to adjust grid's cell width with month-resolution (width of a 30 day month).
224
- //min-width: 120px;
225
- }
226
- .col.y.measure {
227
- // Change min-width to adjust grid's cell width with year-resolution (width of a 365 day year).
228
- //min-width: 365px;
229
- }
216
+ .col.current,
217
+ .day.current {
218
+ background-color: var(--timeline-col-current, #ffe082);
219
+ }
230
220
 
231
- .row {
232
- width: 100%;
233
- float: left;
234
- overflow: hidden;
235
- height: var(--timeline-row-height, 15px);
236
- font-size: var(--timeline-row-font-size, 10px);
237
- background-color: var(--timeline-row-background, #d0d0d0);
238
- -ms-flex-pack: justify;
239
- -webkit-touch-callout: none;
240
- @apply --no-user-select;
241
- }
242
- `;
243
- }
244
- render() {
221
+ /*
222
+ * Minimum block widths are measured from these hidden elements (see
223
+ * calculateResolutionMinWidth()). An unset custom property leaves
224
+ * min-width unset, so the defaults apply.
225
+ */
226
+ .col.measure {
227
+ /* Day and hour block. */
228
+ min-width: var(--timeline-col-min-width);
229
+ }
230
+ .col.w.measure {
231
+ /* One week block. */
232
+ min-width: var(--timeline-col-week-min-width);
233
+ }
234
+ .col.m.measure {
235
+ /* A 30 day month block. */
236
+ min-width: var(--timeline-col-month-min-width);
237
+ }
238
+ .col.y.measure {
239
+ /* A 365 day year block. */
240
+ min-width: var(--timeline-col-year-min-width);
241
+ }
242
+
243
+ .row {
244
+ width: 100%;
245
+ float: left;
246
+ overflow: hidden;
247
+ height: var(--timeline-row-height, 15px);
248
+ font-size: var(--timeline-row-font-size, 10px);
249
+ background-color: var(--timeline-row-background, #d0d0d0);
250
+ -webkit-touch-callout: none;
251
+ -webkit-user-select: none;
252
+ user-select: none;
253
+ }
254
+ `;
255
+ }
256
+ render() {
245
257
  return html `
246
258
  ${this.yearBlocks()}
247
259
  ${this.monthBlocks()}
248
260
  ${this.dayBlocks()}
249
- <div id="resolutionDiv" class="row resolution"></div>`;
250
- }
251
- yearBlocks() {
252
- if (this.isYearRowVisible()) {
253
- return this.timelineBlocks(this.yearRowData, TimelineElement_1.STYLE_YEAR);
254
- }
255
- return nothing;
256
- }
257
- monthBlocks() {
258
- if (this.isMonthRowVisible()) {
259
- return this.timelineBlocks(this.monthRowData, TimelineElement_1.STYLE_MONTH);
260
- }
261
- return nothing;
262
- }
263
- dayBlocks() {
264
- if (this.isDayRowVisible) {
265
- return this.timelineBlocks(this.dayRowData, TimelineElement_1.STYLE_DAY);
266
- }
267
- return nothing;
268
- }
269
- shouldUpdate(changedProperties) {
270
- return changedProperties.has('resolution')
271
- || changedProperties.has('startDateTime')
272
- || changedProperties.has('endDateTime')
273
- || changedProperties.has('locale')
274
- || changedProperties.has('timeZone')
275
- || changedProperties.has('firstDayOfWeek')
276
- || changedProperties.has('twelveHourClock')
277
- || changedProperties.has('yearRowVisible')
278
- || changedProperties.has('monthRowVisible')
279
- || changedProperties.has('monthNames')
280
- || changedProperties.has('weekdayNames')
281
- || changedProperties.has('currentDateVisible');
282
- }
283
- willUpdate(changedProps) {
284
- if (changedProps.has('resolution')) {
285
- this.minResolutionWidth = -1;
286
- }
287
- if (changedProps.has('resolution') || changedProps.has('startDateTime') || changedProps.has('timeZone')) {
288
- this.firstDayOfRange = null;
289
- if (this.resolution === Resolution.Hour) {
290
- this.internalInclusiveStartDateTime = toDate(this.startDateTime, { timeZone: this.timeZone });
291
- }
292
- else {
293
- this.internalInclusiveStartDateTime = toDate(this.startDateTime.substring(0, 10) + 'T00:00:00.000', { timeZone: this.timeZone });
294
- }
295
- }
296
- if (changedProps.has('resolution') || changedProps.has('endDateTime') || changedProps.has('timeZone')) {
297
- // given time must be always exact hour in millisecod accuracy 1AM means exactly "01:00:00.000".
298
- if (this.resolution === Resolution.Hour) {
299
- // convert given time to last millisecond in the given hour. 1AM becomes "01:59:59.999" set to internalEndDateTime.
300
- this.internalInclusiveEndDateTime = toDate(this.endDateTime.substring(0, 13) + ':59:59.999', { timeZone: this.timeZone });
301
- }
302
- else {
303
- this.internalInclusiveEndDateTime = toDate(this.endDateTime.substring(0, 10) + 'T23:59:59.999', { timeZone: this.timeZone });
304
- }
305
- }
306
- this.updateTimeLine(this.resolution, this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime, new DefaultLocaleDataProvider(this.locale, this.timeZone, this.firstDayOfWeek, this.twelveHourClock));
307
- }
308
- updated(changedProps) {
309
- if (!(this.resolution) || !this.internalInclusiveStartDateTime || !this.internalInclusiveEndDateTime) {
310
- return;
311
- }
312
- if (this.resolution !== Resolution.Day && this.resolution !== Resolution.Week && this.resolution !== Resolution.Hour
313
- && !this.isMonthOrYearResolution()) {
314
- console.log("TimelineElement resolution " + (this.resolution ? Resolution[this.resolution] : "null")
315
- + " is not supported");
316
- return;
317
- }
318
- console.log("TimelineElement Constructed content.");
319
- this.updateWidths();
320
- console.log("TimelineElement is updated for resolution " + Resolution[this.resolution] + ".");
321
- this.registerScrollHandler();
322
- }
323
- /**
324
- * <p>
325
- * Updates the content of this component. Builds the time-line and calculates
326
- * width and heights for the content (calls in the end
327
- * {@link #updateWidths()}). This should be called explicitly. Otherwise the
328
- * component will be empty.
329
- * <p>
330
- * Date values should always follow specification in {@link Date#getTime()}.
331
- * Start and end date is always required.
332
- *
333
- * @param resolution
334
- * Resolution enum (not null)
335
- * @param startDate
336
- * Time-line's start date. (inclusive; not null)
337
- * @param endDate
338
- * Time-line's end date. (inclusive; not null)
339
- * @param localeDataProvider
340
- * Data provider for locale specific data. month names, first day
341
- * of week etc.
342
- *
343
- */
344
- updateTimeLine(resolution, startDate, endDate, localeDataProvider) {
345
- if (!localeDataProvider) {
346
- console.log("TimelineElement requires ILocaleDataProvider. Can't complete update(...) operation.");
347
- return;
348
- }
349
- this.clear();
350
- console.log("TimelineElement content cleared.");
351
- if (!(resolution) || !startDate || !endDate) {
352
- return;
353
- }
354
- console.log("TimelineElement Updating content.");
355
- this.localeDataProvider = localeDataProvider;
356
- this.resolution = resolution;
357
- this.resetDateRange(startDate, endDate);
358
- this.lastDayOfWeek = (localeDataProvider.getFirstDayOfWeek() == 1) ? 7 : Math.max((localeDataProvider.getFirstDayOfWeek() - 1) % 8, 1);
359
- this.monthNames = this.monthNames || localeDataProvider.getMonthNames();
360
- this.weekdayNames = this.weekdayNames || localeDataProvider.getWeekdayNames();
361
- if (this.minResolutionWidth < 0) {
362
- this.minResolutionWidth = this.calculateResolutionMinWidth();
363
- }
364
- if (this.resolution === Resolution.Day || this.resolution === Resolution.Week || this.isMonthOrYearResolution()) {
365
- this.prepareTimelineForDayOrWeekResolution(this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime);
366
- }
367
- else if (this.resolution === Resolution.Hour) {
368
- this.prepareTimelineForHourResolution(this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime);
369
- }
370
- }
371
- resetDateRange(startDate, endDate) {
372
- this.internalInclusiveStartDateTime = startDate;
373
- this.internalInclusiveEndDateTime = endDate;
374
- this.normalStartDate = this.toNormalDate(this.internalInclusiveStartDateTime);
375
- this.normalEndDate = this.toNormalDate(this.internalInclusiveEndDateTime);
376
- // Date#getDay() is zero-based: Sunday = 0, Monday = 1, ...
377
- // this.firstDayOfRange is 1-based (Sunday = 1).
378
- this.firstDayOfRange = this.firstDayOfRange || this.internalInclusiveStartDateTime.getDay() + 1; // TODO is weekday still correct here?
379
- this.firstHourOfRange = this.firstHourOfRange || parseInt(this.localeDataProvider.formatTime(this.internalInclusiveStartDateTime, "HH"));
380
- }
381
- registerScrollHandler() {
382
- if (this.scrollHandler) {
383
- return;
384
- }
385
- let timeline = this;
386
- this.scrollContainer = this.setupScrollContainer();
387
- this.scrollHandler = function (e) {
388
- window.requestAnimationFrame(function () {
389
- let container = timeline.scrollContainer;
390
- let sl = container.scrollLeft || container.scrollX;
391
- let st = container.scrollTop || container.scrollY;
392
- if (sl != timeline.previousContainerScrollLeft) {
393
- timeline.setScrollLeft(sl);
394
- timeline.previousContainerScrollLeft = sl;
395
- }
396
- if (st != timeline.previousContainerScrollTop) {
397
- timeline.previousContainerScrollTop = st;
398
- }
399
- });
400
- };
401
- this.scrollContainer.addEventListener('scroll', this.scrollHandler);
402
- }
403
- setupScrollContainer() {
404
- let scrollContainer;
405
- if (this.scrollContainerId) {
406
- scrollContainer = this.getParentElement(this).querySelector('#' + this.scrollContainerId);
407
- if (!scrollContainer) {
408
- scrollContainer = document.querySelector('#' + this.scrollContainerId);
409
- }
410
- if (scrollContainer) {
411
- scrollContainer.style.overflowX = "auto";
412
- }
413
- }
414
- if (!scrollContainer) {
415
- scrollContainer = this.getParentElement(this);
416
- this.directlyInsideScrollContainer = true;
417
- if (scrollContainer === document.body) {
418
- return window; // window scrolls by default, not body
419
- }
420
- }
421
- return scrollContainer;
422
- }
423
- clear() {
424
- this.spacerBlocks = [];
425
- this.yearRowData.clear();
426
- this.monthRowData.clear();
427
- this.dayRowData.clear();
428
- }
429
- calculateResolutionMinWidth() {
430
- let resDivMeasure = document.createElement('div');
431
- resDivMeasure.classList.add(TimelineElement_1.STYLE_ROW, TimelineElement_1.STYLE_RESOLUTION);
432
- let resBlockMeasure = document.createElement('div');
433
- if (this.resolution === Resolution.Week) {
434
- // configurable with '.col.w.measure' selector
435
- resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_WEEK, TimelineElement_1.STYLE_MEASURE);
436
- }
437
- else if (this.resolution === Resolution.Month) {
438
- // configurable with '.col.m.measure' selector
439
- resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_MONTH_RES, TimelineElement_1.STYLE_MEASURE);
440
- }
441
- else if (this.resolution === Resolution.Year) {
442
- // configurable with '.col.y.measure' selector
443
- resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_YEAR_RES, TimelineElement_1.STYLE_MEASURE);
444
- }
445
- else {
446
- // measure for text 'MM'
447
- resBlockMeasure.innerText = "MM";
448
- // configurable with '.col.measure' selector
449
- resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_MEASURE);
450
- }
451
- resDivMeasure.appendChild(resBlockMeasure);
452
- this.shadowRoot.appendChild(resDivMeasure);
453
- let width = resBlockMeasure.clientWidth;
454
- if (this.resolution === Resolution.Week) {
455
- // divide given width by number of days in week
456
- width = width / DateTimeConstants.DAYS_IN_WEEK;
457
- }
458
- let minDayWidth = this.resolutionWeekDayblockWidth;
459
- if (this.resolution === Resolution.Month) {
460
- // divide given width by average number of days in month
461
- width = width / TimelineElement_1.AVERAGE_DAYS_IN_MONTH;
462
- minDayWidth = this.resolutionMonthDayblockWidth;
463
- }
464
- else if (this.resolution === Resolution.Year) {
465
- // divide given width by number of days in year
466
- width = width / TimelineElement_1.AVERAGE_DAYS_IN_YEAR;
467
- minDayWidth = this.resolutionYearDayblockWidth;
468
- }
469
- width = (width < minDayWidth) ? minDayWidth : width;
470
- this.shadowRoot.removeChild(resDivMeasure);
471
- return width;
472
- }
473
- registerHourResolutionBlock() {
474
- this.blocksInRange++;
475
- this.resolutionBlockCount++;
476
- }
477
- registerDayResolutionBlock() {
478
- this.blocksInRange++;
479
- this.resolutionBlockCount++;
480
- }
481
- registerWeekResolutionBlock(index, weekDay, lastBlock, firstWeek) {
482
- if (index == 0 || weekDay === Weekday.First) {
483
- this.resolutionBlockCount++;
484
- }
485
- if (firstWeek && (weekDay === Weekday.Last || lastBlock)) {
486
- this.firstResBlockCount = index + 1;
487
- }
488
- else if (lastBlock) {
489
- this.lastResBlockCount = (index + 1 - this.firstResBlockCount) % 7;
490
- }
491
- this.blocksInRange++;
492
- }
493
- /*
494
- * Registers one day for Month or Year resolution. A new resolution block is
495
- * started whenever the month (or year) of the given date changes.
496
- */
497
- registerMonthOrYearResolutionBlock(index, date) {
498
- let key = this.getMonthOrYearResolutionBlockKey(date);
499
- let lastIndex = this.resBlockKeys.length - 1;
500
- if (index == 0 || lastIndex < 0 || this.resBlockKeys[lastIndex] !== key) {
501
- this.resolutionBlockCount++;
502
- this.resBlockKeys.push(key);
503
- this.resBlockStartDates.push(date);
504
- this.resBlockDayCounts.push(1);
505
- }
506
- else {
507
- this.resBlockDayCounts[lastIndex]++;
508
- }
509
- this.firstResBlockCount = this.resBlockDayCounts[0];
510
- this.lastResBlockCount = this.resBlockDayCounts[this.resBlockDayCounts.length - 1];
511
- this.blocksInRange++;
512
- }
513
- /* Returns 'yyyy-MM' key for Month resolution and 'yyyy' key for Year resolution. */
514
- getMonthOrYearResolutionBlockKey(date) {
515
- return this.localeDataProvider.formatDate(date, this.resolution === Resolution.Year ? "yyyy" : "yyyy-MM");
516
- }
517
- isMonthOrYearResolution() {
518
- return this.resolution === Resolution.Month || this.resolution === Resolution.Year;
519
- }
520
- /* Year row is hidden with Year resolution as the resolution row shows years already. */
521
- isYearRowVisible() {
522
- return this.yearRowVisible && this.resolution !== Resolution.Year;
523
- }
524
- /* Month row is hidden with Month and Year resolutions: Month resolution row shows months already,
525
- * and Year resolution shows a single year row. */
526
- isMonthRowVisible() {
527
- return this.monthRowVisible && !this.isMonthOrYearResolution();
528
- }
529
- timelineBlocks(rowData, style) {
530
- const itemTemplates = [];
531
- for (let entry of rowData.getBlockEntries()) {
532
- itemTemplates.push(html `${entry[1]}`);
533
- }
534
- if (this.isAlwaysCalculatePixelWidths()) {
535
- itemTemplates.push(html `${this.createSpacerBlock(style)}`);
536
- }
537
- return itemTemplates;
538
- }
539
- /**
540
- * Returns true if Widget is set to calculate widths by itself. Default is
541
- * false.
542
- *
543
- * @return
544
- */
545
- isAlwaysCalculatePixelWidths() {
546
- return this.calcPixels;
547
- }
548
- createSpacerBlock(className) {
549
- let block = document.createElement('div');
550
- block.classList.add(TimelineElement_1.STYLE_ROW, TimelineElement_1.STYLE_YEAR, TimelineElement_1.STYLE_SPACER);
551
- block.innerText = " ";
552
- block.style.display = "none"; // not visible by default
553
- this.spacerBlocks.push(block);
554
- return block;
555
- }
556
- /** Clears Daylight saving time adjustment from the given time. */
557
- toNormalDate(zonedDate) {
558
- return DateUtil.toNormalDate(zonedDate, this.localeDataProvider.getDaylightAdjustment(zonedDate));
559
- }
560
- getDSTAdjustedDate(previousIsDST, zonedDate) {
561
- return DateUtil.getDSTAdjustedDate(previousIsDST, zonedDate, this.localeDataProvider.getDaylightAdjustment(zonedDate));
562
- }
563
- getParentElement(node) {
564
- var parent = node.parentNode;
565
- if (!parent || parent.nodeType != 1) {
566
- parent = null;
567
- }
568
- return parent;
569
- }
570
- getDay(date) {
571
- // by adjusting the date to the middle of the day before formatting is a
572
- // workaround to avoid DST issues with DateTimeFormatter.
573
- let adjusted = DateUtil.adjustToMiddleOfDay(date, this.localeDataProvider.getTimeZone());
574
- return this.localeDataProvider.formatDate(adjusted, "d");
575
- }
576
- getYear(date) {
577
- return this.localeDataProvider.formatDate(date, "yyyy");
578
- }
579
- getMonth(date) {
580
- let m = this.localeDataProvider.formatDate(date, "M");
581
- return parseInt(m) - 1;
582
- }
583
- isWeekEnd(dayCounter) {
584
- return dayCounter == 1 || dayCounter == 7;
585
- }
586
- key(prefix, rowData) {
587
- return prefix + "_" + (rowData.size());
588
- }
589
- newKey(prefix, rowData) {
590
- return prefix + "_" + (rowData.size() + 1);
591
- }
592
- addBlock(current, target, date, rowData, operation) {
593
- let key;
594
- if (target !== current) {
595
- current = target;
596
- key = this.newKey("" + current, rowData);
597
- operation(target, key, date);
598
- }
599
- else {
600
- key = this.key("" + current, rowData);
601
- rowData.setBlockLength(key, rowData.getBlockLength(key) + 1);
602
- }
603
- return current;
604
- }
605
- addDayBlock(currentDay, date) {
606
- let day = this.getDay(date);
607
- return this.addBlock(currentDay, day, date, this.dayRowData, (day, key, date) => {
608
- let block = this.addDayBlockElement(key, this.formatDayCaption(day, date));
609
- block.dataset.day = this.localeDataProvider.formatDate(date, TimelineElement_1.CURRENT_DAY_FORMAT);
610
- });
611
- }
612
- addMonthBlock(currentMonth, date) {
613
- let month = this.getMonth(date);
614
- return this.addBlock(currentMonth, "" + month, date, this.monthRowData, (target, key, date) => {
615
- this.addMonthBlockElement(key, this.formatMonthCaption(month, date));
616
- });
617
- }
618
- addYearBlock(currentYear, date) {
619
- let year = this.getYear(date);
620
- return this.addBlock(currentYear, year, date, this.yearRowData, (year, key, date) => {
621
- this.addYearBlockElement(key, this.formatYearCaption(year, date));
622
- });
623
- }
624
- addMonthBlockElement(key, text) {
625
- this.createTimelineBlock(key, text, TimelineElement_1.STYLE_MONTH, this.monthRowData);
626
- }
627
- addYearBlockElement(key, text) {
628
- this.createTimelineBlock(key, text, TimelineElement_1.STYLE_YEAR, this.yearRowData);
629
- }
630
- addDayBlockElement(key, text) {
631
- return this.createTimelineBlock(key, text, TimelineElement_1.STYLE_DAY, this.dayRowData);
632
- }
633
- createTimelineBlock(key, text, styleSuffix, rowData) {
634
- let div = document.createElement('div');
635
- div.classList.add(TimelineElement_1.STYLE_ROW, styleSuffix);
636
- div.innerText = text;
637
- rowData.setBlockLength(key, 1);
638
- rowData.setBlock(key, div);
639
- return div;
640
- }
641
- formatDayCaption(day, date) {
642
- if (!this.dayFormat || this.dayFormat === "") {
643
- return day;
644
- }
645
- return this.localeDataProvider.formatDate(date, this.dayFormat);
646
- }
647
- formatYearCaption(year, date) {
648
- if (!this.yearFormat || this.yearFormat === "") {
649
- return year;
650
- }
651
- return this.localeDataProvider.formatDate(date, this.yearFormat);
652
- }
653
- formatWeekCaption(date) {
654
- if (!this.weekFormat || this.weekFormat === "") {
655
- return "" + getISOWeek(date);
656
- }
657
- return this.localeDataProvider.formatDate(date, this.weekFormat);
658
- }
659
- formatMonthCaption(month, date) {
660
- if (!this.monthFormat || this.monthFormat === "") {
661
- return this.monthNames[month];
662
- }
663
- return this.localeDataProvider.formatDate(date, this.monthFormat);
664
- }
665
- getWeekday(dayCounter) {
666
- if (dayCounter === this.localeDataProvider.getFirstDayOfWeek()) {
667
- return Weekday.First;
668
- }
669
- if (dayCounter === this.lastDayOfWeek) {
670
- return Weekday.Last;
671
- }
672
- return Weekday.Between;
673
- }
674
- prepareTimelineForHourResolution(startDate, endDate) {
675
- let timeline = this;
676
- this.firstDay = true;
677
- let hourCounter = this.firstHourOfRange;
678
- this.prepareTimelineForHour(DateTimeConstants.HOUR_INTERVAL, startDate, endDate, {
679
- registerResolutionBlock(index, date, currentYear, lastTimelineBlock) {
680
- timeline.registerHourResolutionBlock();
681
- hourCounter = Math.max((hourCounter + 1) % 25, 1);
682
- }
683
- });
684
- }
685
- prepareTimelineForHour(interval, startDate, endDate, resBlockRegisterer) {
686
- this.blocksInRange = 0;
687
- this.resolutionBlockCount = 0;
688
- this.firstResBlockCount = 0;
689
- this.lastResBlockCount = 0;
690
- let currentYear = null;
691
- let currentMonth = null;
692
- let currentDay = null;
693
- let pos = startDate;
694
- let end = endDate;
695
- let index = 0;
696
- let lastTimelineBlock = false;
697
- let date;
698
- while (pos.getTime() <= end.getTime()) {
699
- date = pos;
700
- let nextHour = new Date(pos.getTime() + interval);
701
- lastTimelineBlock = nextHour.getTime() > end.getTime();
702
- resBlockRegisterer.registerResolutionBlock(index, date, currentYear, lastTimelineBlock);
703
- if (this.yearRowVisible) {
704
- currentYear = this.addYearBlock(currentYear, date);
705
- }
706
- if (this.monthRowVisible) {
707
- currentMonth = this.addMonthBlock(currentMonth, date);
708
- }
709
- if (this.isDayRowVisible()) {
710
- currentDay = this.addDayBlock(currentDay, date);
711
- }
712
- pos = nextHour;
713
- index++;
714
- }
715
- }
716
- prepareTimelineForDayOrWeekResolution(startDate, endDate) {
717
- let timeline = this;
718
- let dayCounter = this.firstDayOfRange;
719
- let weekday;
720
- let firstWeek = true;
721
- this.resBlockDayCounts = [];
722
- this.resBlockStartDates = [];
723
- this.resBlockKeys = [];
724
- this.prepareTimelineForDayOrWeek(DateTimeConstants.DAY_INTERVAL, startDate, endDate, {
725
- registerResolutionBlock: function (index, date, currentYear, lastTimelineBlock) {
726
- weekday = timeline.getWeekday(dayCounter);
727
- if (timeline.isMonthOrYearResolution()) {
728
- timeline.registerMonthOrYearResolutionBlock(index, date);
729
- }
730
- else if (timeline.resolution === Resolution.Week) {
731
- timeline.registerWeekResolutionBlock(index, weekday, lastTimelineBlock, firstWeek);
732
- if (firstWeek && (weekday === Weekday.Last || lastTimelineBlock)) {
733
- firstWeek = false;
734
- }
735
- }
736
- else {
737
- timeline.registerDayResolutionBlock();
738
- }
739
- dayCounter = Math.max((dayCounter + 1) % 8, 1);
740
- }
741
- });
742
- }
743
- prepareTimelineForDayOrWeek(interval, startDate, endDate, resBlockRegisterer) {
744
- this.blocksInRange = 0;
745
- this.resolutionBlockCount = 0;
746
- this.firstResBlockCount = 0;
747
- this.lastResBlockCount = 0;
748
- let currentYear = null;
749
- let currentMonth = null;
750
- let currentDay = null;
751
- let pos = DateUtil.adjustToMiddleOfDay(startDate, this.localeDataProvider.getTimeZone());
752
- let end = endDate;
753
- let index = 0;
754
- let lastTimelineBlock = false;
755
- let date;
756
- let isDST = false;
757
- let isPreviousDst = this.localeDataProvider.isDaylightTime(startDate);
758
- while (!lastTimelineBlock) {
759
- let date = DateUtil.getDSTAdjustedDate(isPreviousDst, pos, this.localeDataProvider.getDaylightAdjustment(pos));
760
- pos = date;
761
- isDST = this.localeDataProvider.isDaylightTime(date);
762
- let d = new Date(date.getTime() + interval);
763
- lastTimelineBlock = DateUtil.getDSTAdjustedDate(isDST, d, this.localeDataProvider.getDaylightAdjustment(d)).getTime() > end.getTime();
764
- resBlockRegisterer.registerResolutionBlock(index, date, currentYear, lastTimelineBlock);
765
- if (this.isYearRowVisible()) {
766
- currentYear = this.addYearBlock(currentYear, date);
767
- }
768
- if (this.isMonthRowVisible()) {
769
- currentMonth = this.addMonthBlock(currentMonth, date);
770
- }
771
- if (this.isDayRowVisible()) {
772
- currentDay = this.addDayBlock(currentDay, date);
773
- }
774
- isPreviousDst = isDST;
775
- pos = new Date(pos.getTime() + interval);
776
- index++;
777
- }
778
- }
779
- isDayRowVisible() {
780
- return this.resolution === Resolution.Hour;
781
- }
782
- /**
783
- * Get actual width of the timeline.
784
- *
785
- * @return
786
- */
787
- getResolutionWidth() {
788
- if (!this.isTimelineOverflowingHorizontally()) {
789
- return this.calculateTimelineWidth();
790
- }
791
- let width = this.getResolutionDivWidth();
792
- if (this.isAlwaysCalculatePixelWidths() && this.containsResBlockSpacer()) {
793
- width = width - ElementUtil.getWidth(this.resSpacerDiv);
794
- }
795
- return width;
796
- }
797
- /**
798
- * Calculate the exact width of the timeline. Excludes any spacers in the
799
- * end.
800
- *
801
- * @return
802
- */
803
- calculateTimelineWidth() {
804
- let last = this.getLastResolutionElement();
805
- if (last === null) {
806
- return 0.0;
807
- }
808
- let r = ElementUtil.getRight(last);
809
- let l = ElementUtil.getLeft(this.getFirstResolutionElement());
810
- let timelineRealWidth = r - l;
811
- return timelineRealWidth;
812
- }
813
- /*
814
- * Get width of the resolution div element.
815
- */
816
- getResolutionDivWidth() {
817
- if (!this.isTimelineOverflowingHorizontally()) {
818
- return ElementUtil.getWidth(this.resolutionDiv);
819
- }
820
- return this.blocksInRange * this.minResolutionWidth;
821
- }
822
- /**
823
- * Calculate matching left offset in percentage for a date (
824
- * {@link Date#getTime()}).
825
- *
826
- * @param date
827
- * Target date in milliseconds.
828
- * @param contentWidth
829
- * Width of the content that the given 'date' is relative to.
830
- * @return Left offset in percentage.
831
- */
832
- getLeftPositionPercentageForDate(date, contentWidth) {
833
- let timelineLeft = this.getLeftPositionForDate(date);
834
- let relativeLeft = this.convertRelativeLeftPosition(timelineLeft, contentWidth);
835
- let width = this.getResolutionWidth();
836
- return (100.0 / width) * relativeLeft;
837
- }
838
- /**
839
- * Calculate CSS value for 'left' property matching left offset in
840
- * percentage for a date ( {@link Date#getTime()}).
841
- * <p>
842
- * May return '2.123456%' or 'calc(2.123456%)' if IE;
843
- *
844
- * @param date
845
- * Target date in milliseconds.
846
- * @param contentWidth
847
- * Width of the content that the given 'date' is relative to.
848
- * @return Left offset as a String value.
849
- */
850
- getLeftPositionPercentageStringForDate(date, contentWidth) {
851
- let timelineLeft = this.getLeftPositionForDate(date);
852
- let relativeLeft = this.convertRelativeLeftPosition(timelineLeft, contentWidth);
853
- let width = this.getResolutionWidth();
854
- let calc = this.createCalcCssValue(width, relativeLeft);
855
- if (calc != null) {
856
- return calc;
857
- }
858
- return (100.0 / width) * relativeLeft + "%";
859
- }
860
- getLeftPositionPercentageStringForDateRange(date, rangeWidth, rangeStartDate, rangeEndDate) {
861
- let rangeLeft = this.getLeftPositionForDateRange(date, rangeWidth, rangeStartDate, rangeEndDate);
862
- let width = rangeWidth;
863
- let calc = this.createCalcCssValue(width, rangeLeft);
864
- if (calc != null) {
865
- return calc;
866
- }
867
- return (100.0 / width) * rangeLeft + "%";
868
- }
869
- /**
870
- * Calculate CSS value for 'width' property matching date interval inside
871
- * the time-line. Returns percentage value. Interval is in milliseconds.
872
- * <p>
873
- * May return '2.123456%' or 'calc(2.123456%)' if IE;
874
- *
875
- * @param interval
876
- * Date interval in milliseconds.
877
- * @return
878
- */
879
- getWidthPercentageStringForDateInterval(interval) {
880
- let range = this.internalInclusiveEndDateTime.getTime() - this.internalInclusiveStartDateTime.getTime();
881
- return this.getWidthPercentageStringForDateIntervalForRange(interval, range);
882
- }
883
- /** @see #getWidthPercentageStringForDateInterval(long) */
884
- getWidthPercentageStringForDateIntervalForRange(interval, range) {
885
- let calc = this.createCalcCssValue(range, interval);
886
- if (calc != null) {
887
- return calc;
888
- }
889
- return (100.0 / range) * interval + "%";
890
- }
891
- /**
892
- * Calculate matching left offset in pixels for a date (
893
- * {@link Date#getTime()}).
894
- *
895
- * @param date
896
- * Target date in milliseconds.
897
- * @return Left offset in pixels.
898
- */
899
- getLeftPositionForDate(date) {
900
- return this.getLeftPositionForDateRange(date, this.getResolutionWidth(), this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime);
901
- }
902
- getLeftPositionForDateRange(date, rangeWidth, rangeStartDate, rangeEndDate) {
903
- let width = rangeWidth;
904
- let range = rangeEndDate.getTime() - rangeStartDate.getTime();
905
- if (range <= 0) {
906
- return 0;
907
- }
908
- let p = width / range;
909
- let offset = date.getTime() - rangeStartDate.getTime();
910
- let left = p * offset;
911
- return left;
912
- }
913
- /**
914
- * Calculate matching date ({@link Date#getTime()}) for the target left
915
- * pixel offset.
916
- *
917
- * @param left
918
- * Left offset in pixels.
919
- * @return Date in a milliseconds or null if timeline width is invalid (<=0).
920
- */
921
- getDateForLeftPosition(left) {
922
- return this.getDateForLeftPositionNoticeDST(left, this.resolution === Resolution.Hour);
923
- }
924
- getDateForLeftPositionNoticeDST(left, noticeDST) {
925
- let width = this.getResolutionWidth();
926
- if (width <= 0) {
927
- return null;
928
- }
929
- let range = this.normalEndDate.getTime() - this.normalStartDate.getTime();
930
- if (noticeDST) {
931
- range = this.adjustDateRangeByDST(range);
932
- }
933
- let p = range / width;
934
- let offset = p * left;
935
- let date = new Date(this.internalInclusiveStartDateTime.getTime() + offset);
936
- console.log("Zoned: " + this.localeDataProvider.formatDate(date, "dd. HH:mm") + " DST: "
937
- + this.localeDataProvider.getDaylightAdjustment(date) / 60000);
938
- return date;
939
- }
940
- /**
941
- * Convert left position for other relative target width.
942
- *
943
- * @param left
944
- * @param contentWidthToConvertFor
945
- * @return
946
- */
947
- convertRelativeLeftPosition(left, contentWidthToConvertFor) {
948
- let width = this.getResolutionWidth();
949
- if (width <= 0 || contentWidthToConvertFor <= 0) {
950
- return 0;
951
- }
952
- let relativePosition = (1.0 / contentWidthToConvertFor) * left;
953
- let timelineLeft = relativePosition * width;
954
- return timelineLeft;
955
- }
956
- adjustDateRangeByDST(range) {
957
- /*
958
- * Notice extra block(s) or missing block(s) in range when start time is
959
- * in DST and end time is not, or vice versa.
960
- */
961
- let dstStart = this.localeDataProvider.getDaylightAdjustment(this.internalInclusiveStartDateTime);
962
- let dstEnd = this.localeDataProvider.getDaylightAdjustment(this.internalInclusiveEndDateTime);
963
- if (dstStart > dstEnd) {
964
- range -= Math.abs(dstStart - dstEnd);
965
- }
966
- else if (dstEnd > dstStart) {
967
- range += Math.abs(dstEnd - dstStart);
968
- }
969
- return range;
970
- }
971
- /**
972
- * Set horizontal scroll position for the time-line.
973
- *
974
- * @param left
975
- * Scroll position in pixels.
976
- */
977
- setScrollLeft(left) {
978
- if (this.positionLeft === left) {
979
- return;
980
- }
981
- this.positionLeft = left || 0;
982
- if (!this.directlyInsideScrollContainer) {
983
- this.style.left = -this.positionLeft + "px";
984
- }
985
- this.lazyResolutionPaint = setTimeout(() => this.fillVisibleTimeline(), 20);
986
- }
987
- /**
988
- * Re-calculates required widths for this widget.
989
- * <p>
990
- * Re-creates and fills the visible part of the resolution element.
991
- */
992
- updateWidths() {
993
- if (this.resolutionDiv == null) {
994
- console.log("TimelineElement is not ready for updateWidths() call. Call update(...) instead.");
995
- return;
996
- }
997
- console.log("TimelineElement Started updating widths.");
998
- // start by clearing old content in resolution element
999
- while (this.resolutionDiv.firstChild) {
1000
- this.resolutionDiv.removeChild(this.resolutionDiv.lastChild);
1001
- }
1002
- this.setMinWidth(this.blocksInRange * this.minResolutionWidth);
1003
- // update horizontal overflow state here, after min-width is updated.
1004
- this.updateTimelineOverflowingHorizontally();
1005
- this.createTimelineElementsOnVisibleArea();
1006
- // fill timeline
1007
- this.fillVisibleTimeline();
1008
- // remove spacer block if it exist
1009
- this.removeResolutionSpacerBlock();
1010
- // calculate new block width for day-resolution.
1011
- // Year and month blocks are vertically in-line with days.
1012
- this.dayWidthPercentage = 100.0 / this.blocksInRange;
1013
- this.dayOrHourWidthPx = this.calculateDayOrHourResolutionBlockWidthPx(this.blocksInRange);
1014
- // calculate block width for currently selected resolution
1015
- // (day,week,...)
1016
- // resolution div's content may not be vertically in-line with
1017
- // year/month blocks. This is the case for example with Week resolution.
1018
- this.resBlockMinWidthPx = this.minResolutionWidth;
1019
- this.resBlockWidthPx = this.calculateActualResolutionBlockWidthPx(this.dayOrHourWidthPx);
1020
- this.resBlockWidthPercentage = 100.0 / this.resolutionBlockCount;
1021
- let pct = this.createCalcCssValue(this.resolutionBlockCount, null);
1022
- if (this.resolution === Resolution.Week) {
1023
- this.resBlockMinWidthPx = DateTimeConstants.DAYS_IN_WEEK * this.minResolutionWidth;
1024
- this.resBlockWidthPercentage = this.dayWidthPercentage * DateTimeConstants.DAYS_IN_WEEK;
1025
- pct = this.createCalcCssValue(this.blocksInRange, DateTimeConstants.DAYS_IN_WEEK);
1026
- }
1027
- else if (this.isMonthOrYearResolution()) {
1028
- // Month and year blocks have variable widths; these values represent an average block.
1029
- this.resBlockMinWidthPx = this.calculateMinimumResolutionBlockWidth();
1030
- this.resBlockWidthPercentage = 100.0 / this.resolutionBlockCount;
1031
- }
1032
- // update resolution block widths
1033
- if (this.isMonthOrYearResolution()) {
1034
- this.updateMonthOrYearResolutionBlockWidths();
1035
- }
1036
- else {
1037
- this.updateResolutionBlockWidths(pct);
1038
- }
1039
- if (this.isYearRowVisible()) {
1040
- // update year block widths
1041
- this.updateBlockWidths(this.yearRowData);
1042
- }
1043
- if (this.isMonthRowVisible()) {
1044
- // update month block widths
1045
- this.updateBlockWidths(this.monthRowData);
1046
- }
1047
- if (this.isDayRowVisible()) {
1048
- this.updateBlockWidths(this.dayRowData);
1049
- }
1050
- if (this.isAlwaysCalculatePixelWidths()) {
1051
- this.updateSpacerBlocks(this.dayOrHourWidthPx);
1052
- }
1053
- console.log("TimelineElement Widths are updated.");
1054
- }
1055
- updateBlockWidths(rowData) {
1056
- for (let entry of rowData.getBlockEntries()) {
1057
- this.setWidth(entry[1], rowData.getBlockLength(entry[0]));
1058
- }
1059
- }
1060
- updateSpacerBlocks(dayWidthPx) {
1061
- let spaceLeft = this.getResolutionDivWidth() - (this.blocksInRange * dayWidthPx);
1062
- if (spaceLeft > 0) {
1063
- for (let e of this.spacerBlocks) {
1064
- e.style.removeProperty("display");
1065
- e.style.width = spaceLeft + "px";
1066
- }
1067
- this.resSpacerDiv = this.createResolutionBlock();
1068
- this.resSpacerDiv.classList.add(TimelineElement_1.STYLE_SPACER);
1069
- this.resSpacerDiv.style.width = spaceLeft + "px";
1070
- this.resSpacerDiv.innerText = " ";
1071
- this.resolutionDiv.appendChild(this.resSpacerDiv);
1072
- }
1073
- else {
1074
- this.hideSpacerBlocks();
1075
- }
1076
- }
1077
- hideSpacerBlocks() {
1078
- for (let e of this.spacerBlocks) {
1079
- e.style.display = "none";
1080
- }
1081
- }
1082
- /**
1083
- * Set minimum width (pixels) of this widget's root DIV element. Default is
1084
- * -1. Notice that
1085
- * {@link #update(Resolution, long, long, int, int, LocaleDataProvider)}
1086
- * will calculate min-width and call this internally.
1087
- *
1088
- * @param minWidth
1089
- * Minimum width in pixels.
1090
- */
1091
- setMinWidth(minWidth) {
1092
- this.minWidth = minWidth;
1093
- this.style.minWidth = this.minWidth + "px";
1094
- this.resolutionDiv.style.minWidth = this.minWidth + "px";
1095
- }
1096
- /**
1097
- * Returns true if the timeline is overflowing the parent's width. This
1098
- * works only when this widget is attached to some parent.
1099
- *
1100
- * @return True when timeline width is more than the parent's width (@see
1101
- * {@link Element#getClientWidth()}).
1102
- */
1103
- isTimelineOverflowingHorizontally() {
1104
- return this.timelineOverflowingHorizontally;
1105
- }
1106
- /**
1107
- * Update horizontal overflow state.
1108
- */
1109
- updateTimelineOverflowingHorizontally() {
1110
- this.timelineOverflowingHorizontally = (ElementUtil.getWidth(this.resolutionDiv) > ElementUtil.getWidth(this.getParentElement(this)));
1111
- }
1112
- createTimelineElementsOnVisibleArea() {
1113
- // create place holder elements that represents weeks/days/hours
1114
- // depending on the resolution in the timeline.
1115
- // Only visible blocks are created, and only once, content will change
1116
- // on scroll.
1117
- // first: detect how many blocks we can fit in the screen
1118
- let blocks = this.resolutionBlockCount;
1119
- // Month and Year resolutions have variable width blocks and only a few of
1120
- // them even for long ranges, so all of them are created.
1121
- if (this.isTimelineOverflowingHorizontally() && !this.isMonthOrYearResolution()) {
1122
- blocks = Math.floor((ElementUtil.getWidth(this.getParentElement(this))
1123
- / this.calculateMinimumResolutionBlockWidth()));
1124
- if (this.resolutionBlockCount < blocks) {
1125
- // blocks need to be scaled up to fit the screen
1126
- blocks = this.resolutionBlockCount;
1127
- }
1128
- else {
1129
- blocks += 2;
1130
- }
1131
- }
1132
- let element = null;
1133
- for (let i = 0; i < blocks; i++) {
1134
- switch (this.resolution) {
1135
- case Resolution.Hour:
1136
- element = this.createHourResolutionBlock();
1137
- break;
1138
- case Resolution.Day:
1139
- element = this.createDayResolutionBlock();
1140
- break;
1141
- case Resolution.Week:
1142
- element = this.createWeekResolutionBlock();
1143
- break;
1144
- case Resolution.Month:
1145
- element = this.createMonthResolutionBlock();
1146
- break;
1147
- case Resolution.Year:
1148
- element = this.createYearResolutionBlock();
1149
- break;
1150
- }
1151
- this.resolutionDiv.appendChild(element);
1152
- }
1153
- console.log(`TimelineElement Added ${blocks} visible timeline elements for resolution ${Resolution[this.resolution]}`);
1154
- }
1155
- calculateMinimumResolutionBlockWidth() {
1156
- if (this.resolution === Resolution.Week) {
1157
- return DateTimeConstants.DAYS_IN_WEEK * this.minResolutionWidth;
1158
- }
1159
- else if (this.resolution === Resolution.Month) {
1160
- return TimelineElement_1.AVERAGE_DAYS_IN_MONTH * this.minResolutionWidth;
1161
- }
1162
- else if (this.resolution === Resolution.Year) {
1163
- return TimelineElement_1.AVERAGE_DAYS_IN_YEAR * this.minResolutionWidth;
1164
- }
1165
- return this.minResolutionWidth;
1166
- }
1167
- createResolutionBlock() {
1168
- let resBlock = document.createElement('div');
1169
- resBlock.classList.add("col");
1170
- return resBlock;
1171
- }
1172
- createHourResolutionBlock() {
1173
- let resBlock = this.createResolutionBlock();
1174
- resBlock.classList.add("h", TimelineElement_1.STYLE_CENTER);
1175
- return resBlock;
1176
- }
1177
- createDayResolutionBlock() {
1178
- let resBlock = this.createResolutionBlock();
1179
- resBlock.classList.add(TimelineElement_1.STYLE_CENTER);
1180
- return resBlock;
1181
- }
1182
- createWeekResolutionBlock() {
1183
- let resBlock = this.createResolutionBlock();
1184
- resBlock.classList.add("w", TimelineElement_1.STYLE_CENTER);
1185
- return resBlock;
1186
- }
1187
- createMonthResolutionBlock() {
1188
- // width is set explicitly for each block, see updateMonthOrYearResolutionBlockWidths()
1189
- let resBlock = this.createResolutionBlock();
1190
- resBlock.classList.add(TimelineElement_1.STYLE_MONTH_RES);
1191
- return resBlock;
1192
- }
1193
- createYearResolutionBlock() {
1194
- // width is set explicitly for each block, see updateMonthOrYearResolutionBlockWidths()
1195
- let resBlock = this.createResolutionBlock();
1196
- resBlock.classList.add(TimelineElement_1.STYLE_YEAR_RES);
1197
- return resBlock;
1198
- }
1199
- fillVisibleTimeline() {
1200
- if (this.isMonthOrYearResolution()) {
1201
- // all month/year blocks exist and are positioned statically; scrolling needs no re-positioning.
1202
- this.showAllResolutionBlocks();
1203
- }
1204
- else if (this.isTimelineOverflowingHorizontally()) {
1205
- this.showResolutionBlocksOnView();
1206
- }
1207
- else {
1208
- this.showAllResolutionBlocks();
1209
- }
1210
- }
1211
- showResolutionBlocksOnView() {
1212
- let positionLeftSnapshot = this.positionLeft;
1213
- let datePos = positionLeftSnapshot;
1214
- this.firstWeekBlockHidden = false;
1215
- let left = Math.floor(positionLeftSnapshot);
1216
- if (positionLeftSnapshot > 0 && this.resBlockWidthPx > 0) {
1217
- let overflow = 0.0;
1218
- let firstResBlockShort = this.isFirstResBlockShort();
1219
- overflow = this.getScrollOverflowForResolutionBlock(positionLeftSnapshot, left, firstResBlockShort);
1220
- left = Math.floor(positionLeftSnapshot - overflow);
1221
- datePos = this.adjustLeftPositionForDateDetection(left);
1222
- }
1223
- if (datePos < 0.0) {
1224
- datePos = positionLeftSnapshot;
1225
- }
1226
- let leftDate;
1227
- let noticeDst = this.resolution === Resolution.Hour;
1228
- leftDate = this.getDateForLeftPositionNoticeDST(datePos, noticeDst);
1229
- let containerWidth = ElementUtil.getWidth(this.getParentElement(this));
1230
- this.fillTimelineForResolution(leftDate, new Date(Math.min(this.internalInclusiveEndDateTime.getTime(), this.getDateForLeftPositionNoticeDST(datePos + containerWidth, noticeDst).getTime())), left);
1231
- this.style.setProperty("--timeline-col-position", "relative");
1232
- this.style.setProperty("--timeline-col-left", left + "px");
1233
- console.log(`TimelineElement Updated visible timeline elements for horizontal scroll position ${left} (plus ${datePos - left} to center-of-first-block)`);
1234
- }
1235
- showAllResolutionBlocks() {
1236
- this.style.setProperty("--timeline-col-position", "relative");
1237
- this.style.setProperty("--timeline-col-left", "0px");
1238
- this.fillTimelineForResolution(this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime, 0);
1239
- }
1240
- /**
1241
- * Re-applies 'current' style to the currently shown resolution blocks and day
1242
- * row blocks without rebuilding the timeline. Call this when the current day
1243
- * or hour changes.
1244
- */
1245
- refreshCurrentDate() {
1246
- if (!this.localeDataProvider || !this.resolutionDiv || !this.internalInclusiveStartDateTime
1247
- || !this.internalInclusiveEndDateTime || this.resolutionDiv.childElementCount === 0) {
1248
- return;
1249
- }
1250
- this.fillVisibleTimeline();
1251
- }
1252
- updateCurrentDayString() {
1253
- this.currentDayString = (this.currentDateVisible && this.localeDataProvider)
1254
- ? this.localeDataProvider.formatDate(new Date(), TimelineElement_1.CURRENT_DAY_FORMAT)
1255
- : null;
1256
- this.currentHourString = (this.currentDateVisible && this.localeDataProvider)
1257
- ? this.localeDataProvider.formatDate(new Date(), TimelineElement_1.CURRENT_HOUR_FORMAT)
1258
- : null;
1259
- }
1260
- isCurrentDay(date) {
1261
- return this.currentDayString != null && date != null
1262
- && this.localeDataProvider.formatDate(date, TimelineElement_1.CURRENT_DAY_FORMAT) === this.currentDayString;
1263
- }
1264
- isCurrentHour(date) {
1265
- // Compare the hour bucket in the configured time zone, so the result does
1266
- // not depend on where inside the hour the given date falls.
1267
- return this.currentHourString != null && date != null
1268
- && this.localeDataProvider.formatDate(date, TimelineElement_1.CURRENT_HOUR_FORMAT) === this.currentHourString;
1269
- }
1270
- setCurrentStyle(element, current) {
1271
- if (current) {
1272
- element.classList.add(TimelineElement_1.STYLE_CURRENT);
1273
- }
1274
- else {
1275
- element.classList.remove(TimelineElement_1.STYLE_CURRENT);
1276
- }
1277
- }
1278
- updateDayRowCurrentStyles() {
1279
- if (!this.isDayRowVisible()) {
1280
- return;
1281
- }
1282
- for (let entry of this.dayRowData.getBlockEntries()) {
1283
- let block = entry[1];
1284
- this.setCurrentStyle(block, this.currentDayString != null && block.dataset.day === this.currentDayString);
1285
- }
1286
- }
1287
- fillTimelineForResolution(startDate, endDate, left) {
1288
- this.updateCurrentDayString();
1289
- this.updateDayRowCurrentStyles();
1290
- if (this.isMonthOrYearResolution()) {
1291
- this.fillTimelineForMonthOrYearResolution();
1292
- }
1293
- else if (this.resolution === Resolution.Day || this.resolution === Resolution.Week) {
1294
- this.fillTimelineForDayResolution(startDate, endDate, left);
1295
- }
1296
- else if (this.resolution == Resolution.Hour) {
1297
- this.fillTimelineForHourResolution(startDate, endDate, left);
1298
- }
1299
- else {
1300
- console.log("TimelineElement resolution " + (this.resolution != null ? Resolution[this.resolution] : "null")
1301
- + " is not supported");
1302
- return;
1303
- }
1304
- console.log("TimelineElement Filled new data and styles to visible timeline elements");
1305
- }
1306
- isFirstResBlockShort() {
1307
- return this.firstResBlockCount > 0 && ((this.resolution === Resolution.Week && this.firstResBlockCount < DateTimeConstants.DAYS_IN_WEEK));
1308
- }
1309
- isLastResBlockShort() {
1310
- return this.lastResBlockCount > 0 && ((this.resolution === Resolution.Week && this.lastResBlockCount < DateTimeConstants.DAYS_IN_WEEK));
1311
- }
1312
- getScrollOverflowForResolutionBlock(positionLeftSnapshot, left, firstResBlockShort) {
1313
- let overflow;
1314
- if (firstResBlockShort && left <= this.getFirstResolutionElementWidth()) {
1315
- overflow = this.getScrollOverflowForShortFirstResolutionBlock(positionLeftSnapshot);
1316
- }
1317
- else {
1318
- overflow = this.getScrollOverflowForRegularResoultionBlock(positionLeftSnapshot, firstResBlockShort);
1319
- }
1320
- return overflow;
1321
- }
1322
- getScrollOverflowForRegularResoultionBlock(positionLeftSnapshot, firstResBlockShort) {
1323
- let overflow;
1324
- let firstBlockWidth = this.getFirstResolutionElementWidth();
1325
- let positionLeft = (positionLeftSnapshot - (firstResBlockShort ? firstBlockWidth : 0));
1326
- overflow = positionLeft % this.resBlockWidthPx;
1327
- if (firstResBlockShort) {
1328
- overflow += firstBlockWidth;
1329
- this.firstWeekBlockHidden = true;
1330
- }
1331
- return overflow;
1332
- }
1333
- getScrollOverflowForShortFirstResolutionBlock(positionLeftSnapshot) {
1334
- let overflow;
1335
- // need to notice a short resolution block due to timeline's
1336
- // start date which is in middle of a week.
1337
- overflow = positionLeftSnapshot % this.getFirstResolutionElementWidth();
1338
- if (overflow == 0.0) {
1339
- overflow = this.getFirstResolutionElementWidth();
1340
- }
1341
- return overflow;
1342
- }
1343
- /**
1344
- * Returns a width of the first resolution block.
1345
- *
1346
- * @return
1347
- */
1348
- getFirstResolutionElementWidth() {
1349
- if (this.isMonthOrYearResolution()) {
1350
- if (this.isTimelineOverflowingHorizontally()) {
1351
- return this.firstResBlockCount * this.minResolutionWidth;
1352
- }
1353
- else {
1354
- return ElementUtil.getWidth(this.getFirstResolutionElement());
1355
- }
1356
- }
1357
- if (this.isFirstResBlockShort()) {
1358
- if (this.isTimelineOverflowingHorizontally()) {
1359
- return this.firstResBlockCount * this.minResolutionWidth;
1360
- }
1361
- else {
1362
- return ElementUtil.getWidth(this.getFirstResolutionElement());
1363
- }
1364
- }
1365
- else {
1366
- if (this.isTimelineOverflowingHorizontally()) {
1367
- return this.resBlockMinWidthPx;
1368
- }
1369
- else {
1370
- return ElementUtil.getWidth(this.getFirstResolutionElement());
1371
- }
1372
- }
1373
- }
1374
- getFirstResolutionElement() {
1375
- if (this.resolutionDiv.hasChildNodes()) {
1376
- return this.resolutionDiv.firstElementChild;
1377
- }
1378
- return null;
1379
- }
1380
- getLastResolutionElement() {
1381
- let div = this.resolutionDiv;
1382
- if (!div) {
1383
- return null;
1384
- }
1385
- let nodeList = div.childNodes;
1386
- if (!nodeList) {
1387
- return null;
1388
- }
1389
- let blockCount = nodeList.length;
1390
- if (blockCount < 1) {
1391
- return null;
1392
- }
1393
- if (this.containsResBlockSpacer()) {
1394
- let index = blockCount - 2;
1395
- if (blockCount > 1 && index >= 0) {
1396
- return this.resolutionDiv.childNodes.item(index);
1397
- }
1398
- return null;
1399
- }
1400
- return this.resolutionDiv.lastChild;
1401
- }
1402
- containsResBlockSpacer() {
1403
- return this.resSpacerDiv != null && this.resSpacerDiv.parentElement
1404
- && this.resSpacerDiv.parentElement === this.resolutionDiv;
1405
- }
1406
- removeResolutionSpacerBlock() {
1407
- if (this.containsResBlockSpacer()) {
1408
- this.resSpacerDiv.parentNode.removeChild(this.resSpacerDiv);
1409
- }
1410
- }
1411
- /*
1412
- * Calculates either day or hour resolution block width depending on the
1413
- * current resolution.
1414
- */
1415
- calculateDayOrHourResolutionBlockWidthPx(blockCount) {
1416
- let dayOrHourWidthPx = Math.round(this.resolutionDiv.clientWidth / blockCount);
1417
- while ((blockCount * dayOrHourWidthPx) < this.resolutionDiv.clientWidth) {
1418
- dayOrHourWidthPx++;
1419
- }
1420
- return dayOrHourWidthPx;
1421
- }
1422
- /*
1423
- * Calculates the actual width of one resolution block element. For example:
1424
- * week resolution will return 7 * dayOrHourBlockWidthPx.
1425
- */
1426
- calculateActualResolutionBlockWidthPx(dayOrHourBlockWidthPx) {
1427
- if (this.resolution === Resolution.Week) {
1428
- return DateTimeConstants.DAYS_IN_WEEK * dayOrHourBlockWidthPx;
1429
- }
1430
- else if (this.resolution === Resolution.Month) {
1431
- // average width; actual month block widths vary by number of days.
1432
- return TimelineElement_1.AVERAGE_DAYS_IN_MONTH * dayOrHourBlockWidthPx;
1433
- }
1434
- else if (this.resolution === Resolution.Year) {
1435
- // average width; actual year block widths vary by number of days.
1436
- return TimelineElement_1.AVERAGE_DAYS_IN_YEAR * dayOrHourBlockWidthPx;
1437
- }
1438
- return dayOrHourBlockWidthPx;
1439
- }
1440
- /**
1441
- * Adjust left position for optimal position to detect accurate date with
1442
- * the current resolution.
1443
- */
1444
- adjustLeftPositionForDateDetection(left) {
1445
- let datePos;
1446
- if (this.resolution === Resolution.Week) {
1447
- // detect date from the center of the first day block inside the
1448
- // week block.
1449
- datePos = left + this.dayOrHourWidthPx / 2;
1450
- }
1451
- else {
1452
- // detect date from the center of the block (day/hour)
1453
- datePos = left + this.resBlockWidthPx / 2;
1454
- }
1455
- return datePos;
1456
- }
1457
- createCalcCssValue(v, multiplier) {
1458
- if (this.ie) {
1459
- // see comments in createCalcCssValue(int, Integer)
1460
- let percents = 100.0 / v * multiplier;
1461
- return "calc(" + percents + "%)";
1462
- }
1463
- return null;
1464
- }
1465
- updateResolutionBlockWidths(pct) {
1466
- if (this.setPositionForEachBlock) {
1467
- if (!this.isTimelineOverflowingHorizontally()) {
1468
- this.resolutionDiv.style.display = "flex";
1469
- }
1470
- else {
1471
- this.resolutionDiv.style.removeProperty("display");
1472
- }
1473
- let firstResBlockIsShort = this.isFirstResBlockShort();
1474
- let lastResBlockIsShort = this.isLastResBlockShort();
1475
- // when setPositionForEachBlock is true, set width for each block explicitly.
1476
- let count = this.resolutionDiv.childElementCount;
1477
- if (this.containsResBlockSpacer()) {
1478
- count--;
1479
- }
1480
- let lastIndex = count - 1;
1481
- let i;
1482
- let resBlock;
1483
- for (i = 0; i < count; i++) {
1484
- resBlock = this.resolutionDiv.childNodes.item(i);
1485
- // first and last week blocks may be thinner than other
1486
- // resolution blocks.
1487
- if (firstResBlockIsShort && i == 0) {
1488
- this.setWidth(resBlock, this.firstResBlockCount);
1489
- }
1490
- else if (lastResBlockIsShort && i == lastIndex) {
1491
- this.setWidth(resBlock, this.lastResBlockCount);
1492
- }
1493
- else {
1494
- this.setWidthPct(this.resBlockWidthPx, pct, resBlock);
1495
- }
1496
- }
1497
- }
1498
- else {
1499
- // set widths by updating injected styles in one place. Faster than
1500
- // setting widths explicitly for each element.
1501
- let center = this.getWidthStyleValue(pct);
1502
- let first = center;
1503
- let last = center;
1504
- if (this.isFirstResBlockShort()) {
1505
- first = this.getWidth(this.firstResBlockCount);
1506
- }
1507
- if (this.isLastResBlockShort()) {
1508
- last = this.getWidth(this.lastResBlockCount);
1509
- }
1510
- this.style.setProperty("--timeline-col-center-width", center);
1511
- this.style.setProperty("--timeline-col-first-width", first);
1512
- this.style.setProperty("--timeline-col-last-width", last);
1513
- }
1514
- }
1515
- /*
1516
- * Sets the width explicitly for each Month/Year resolution block based on
1517
- * the number of days in the block. Blocks are kept vertically in-line with
1518
- * the day based year and month rows.
1519
- */
1520
- updateMonthOrYearResolutionBlockWidths() {
1521
- this.resolutionDiv.style.removeProperty("display");
1522
- let count = Math.min(this.resolutionDiv.childElementCount, this.resBlockDayCounts.length);
1523
- for (let i = 0; i < count; i++) {
1524
- let resBlock = this.resolutionDiv.children.item(i);
1525
- if (resBlock === this.resSpacerDiv) {
1526
- continue;
1527
- }
1528
- this.setWidth(resBlock, this.resBlockDayCounts[i]);
1529
- }
1530
- }
1531
- getWidth(multiplier) {
1532
- if (this.isTimelineOverflowingHorizontally()) {
1533
- return (multiplier * this.minResolutionWidth) + "px";
1534
- }
1535
- else {
1536
- if (this.isAlwaysCalculatePixelWidths()) {
1537
- return multiplier * this.dayOrHourWidthPx + "px";
1538
- }
1539
- else {
1540
- return this.getCssPercentageWidth(this.blocksInRange, this.dayWidthPercentage, multiplier);
1541
- }
1542
- }
1543
- }
1544
- setWidth(element, multiplier) {
1545
- if (this.isTimelineOverflowingHorizontally()) {
1546
- element.style.width = (multiplier * this.minResolutionWidth) + "px";
1547
- }
1548
- else {
1549
- if (this.isAlwaysCalculatePixelWidths()) {
1550
- element.style.width = (multiplier * this.dayOrHourWidthPx) + "px";
1551
- }
1552
- else {
1553
- this.setCssPercentageWidth(element, this.blocksInRange, this.dayWidthPercentage, multiplier);
1554
- }
1555
- }
1556
- }
1557
- setWidthPct(resBlockWidthPx, pct, element) {
1558
- if (this.isTimelineOverflowingHorizontally()) {
1559
- element.style.width = this.resBlockMinWidthPx + "px";
1560
- }
1561
- else {
1562
- if (this.isAlwaysCalculatePixelWidths()) {
1563
- element.style.width = resBlockWidthPx + "px";
1564
- }
1565
- else {
1566
- if (this.ie) {
1567
- element.style.flex = "1";
1568
- }
1569
- this.setCssPercentageWidthFor(element, this.resBlockWidthPercentage, pct);
1570
- }
1571
- }
1572
- }
1573
- setCssPercentageWidth(element, daysInRange, width, position) {
1574
- let pct = this.createCalcCssValue(daysInRange, position);
1575
- this.setCssPercentageWidthFor(element, position * width, pct);
1576
- }
1577
- getCssPercentageWidth(daysInRange, width, position) {
1578
- let pct = this.createCalcCssValue(daysInRange, position);
1579
- return this.getPercentageWidthString(position * width, pct);
1580
- }
1581
- setCssPercentageWidthFor(element, nValue, pct) {
1582
- if (pct) {
1583
- element.style.width = pct;
1584
- }
1585
- else {
1586
- element.style.width = nValue + "%";
1587
- }
1588
- }
1589
- getPercentageWidthString(nValue, pct) {
1590
- if (pct) {
1591
- return pct;
1592
- }
1593
- else {
1594
- return nValue + "%";
1595
- }
1596
- }
1597
- getWidthStyleValue(pct) {
1598
- if (this.isTimelineOverflowingHorizontally()) {
1599
- return this.resBlockMinWidthPx + "px";
1600
- }
1601
- else {
1602
- if (this.isAlwaysCalculatePixelWidths()) {
1603
- return this.resBlockWidthPx + "px";
1604
- }
1605
- else {
1606
- return this.getPercentageWidthString(this.resBlockWidthPercentage, pct);
1607
- }
1608
- }
1609
- }
1610
- /*
1611
- * Fills captions and styles for all Month/Year resolution blocks. Uses block
1612
- * data collected in prepareTimelineForDayOrWeekResolution().
1613
- */
1614
- fillTimelineForMonthOrYearResolution() {
1615
- let childCount = this.resolutionDiv.childElementCount;
1616
- // currentDayString is 'yyyy-MM-dd'; take 'yyyy-MM' or 'yyyy' part of it.
1617
- let currentKey = null;
1618
- if (this.currentDayString != null) {
1619
- currentKey = this.currentDayString.substring(0, this.resolution === Resolution.Year ? 4 : 7);
1620
- }
1621
- for (let i = 0; i < this.resBlockStartDates.length; i++) {
1622
- if (!this.isValidChildIndex(i, childCount)) {
1623
- this.logIndexOutOfBounds(this.resolution === Resolution.Year ? "year" : "month", i, childCount);
1624
- return;
1625
- }
1626
- let resBlock = this.resolutionDiv.children.item(i);
1627
- let date = this.resBlockStartDates[i];
1628
- this.fillMonthOrYearResolutionBlock(resBlock, date, i % 2 === 1, currentKey != null && this.resBlockKeys[i] === currentKey);
1629
- }
1630
- }
1631
- fillMonthOrYearResolutionBlock(resBlock, date, even, current) {
1632
- if (this.resolution === Resolution.Year) {
1633
- resBlock.innerText = this.formatYearCaption(this.getYear(date), date);
1634
- }
1635
- else {
1636
- resBlock.innerText = this.formatMonthCaption(this.getMonth(date), date);
1637
- }
1638
- if (even) {
1639
- resBlock.classList.add(TimelineElement_1.STYLE_EVEN);
1640
- }
1641
- else {
1642
- resBlock.classList.remove(TimelineElement_1.STYLE_EVEN);
1643
- }
1644
- this.setCurrentStyle(resBlock, current);
1645
- }
1646
- fillTimelineForHourResolution(startDate, endDate, left) {
1647
- let timeline = this;
1648
- this.firstDay = true;
1649
- let hourCounter;
1650
- let even;
1651
- this.fillTimelineForHour(DateTimeConstants.HOUR_INTERVAL, startDate, endDate, {
1652
- setup() {
1653
- hourCounter = this.getFirstHourOfVisibleRange(startDate);
1654
- even = this.isEven(startDate);
1655
- },
1656
- fillResolutionBlock(index, date, currentYear, lastTimelineBlock) {
1657
- let childCount = timeline.resolutionDiv.childElementCount;
1658
- if (timeline.isValidChildIndex(index, childCount)) {
1659
- let resBlock = timeline.resolutionDiv.childNodes.item(index);
1660
- timeline.fillHourResolutionBlock(resBlock, date, index, hourCounter, lastTimelineBlock, left, even);
1661
- hourCounter = (hourCounter + 1) % 24;
1662
- even = !even;
1663
- }
1664
- else {
1665
- timeline.logIndexOutOfBounds("hour", index, childCount);
1666
- return;
1667
- }
1668
- },
1669
- isEven(startDate) {
1670
- let normalDate = timeline.toNormalDate(startDate);
1671
- if (timeline.normalStartDate.getTime() < normalDate.getTime()) {
1672
- let hours = Math.floor(((normalDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.HOUR_INTERVAL));
1673
- return (hours % 2) == 1;
1674
- }
1675
- return false;
1676
- },
1677
- getFirstHourOfVisibleRange(startDate) {
1678
- let normalDate = timeline.toNormalDate(startDate);
1679
- if (timeline.normalStartDate.getTime() < normalDate.getTime()) {
1680
- let hours = Math.floor(((normalDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.HOUR_INTERVAL));
1681
- return ((timeline.firstHourOfRange + hours) % 24);
1682
- }
1683
- return timeline.firstHourOfRange;
1684
- }
1685
- });
1686
- }
1687
- fillTimelineForDayResolution(startDate, endDate, left) {
1688
- let timeline = this;
1689
- let dayCounter;
1690
- let even;
1691
- let firstWeek = true;
1692
- let weekIndex = 0;
1693
- let weekday;
1694
- this.fillTimelineForDayOrWeek(DateTimeConstants.DAY_INTERVAL, startDate, endDate, {
1695
- setup: function () {
1696
- dayCounter = this.getFirstDayOfVisibleRange(startDate);
1697
- even = this.isEven(startDate, timeline.firstDayOfRange);
1698
- },
1699
- fillResolutionBlock: function (index, date, currentYear, lastTimelineBlock) {
1700
- try {
1701
- weekday = timeline.getWeekday(dayCounter);
1702
- if (timeline.resolution === Resolution.Week) {
1703
- this.fillWeekBlock(left, index, date, lastTimelineBlock);
1704
- }
1705
- else {
1706
- this.fillDayBlock(left, index, date);
1707
- }
1708
- }
1709
- finally {
1710
- dayCounter = Math.max((dayCounter + 1) % 8, 1);
1711
- }
1712
- },
1713
- fillDayBlock: function (left, index, date) {
1714
- let childCount = timeline.resolutionDiv.childElementCount;
1715
- if (timeline.isValidChildIndex(index, childCount)) {
1716
- let resBlock = timeline.resolutionDiv.childNodes.item(index);
1717
- timeline.fillDayResolutionBlock(resBlock, date, index, timeline.isWeekEnd(dayCounter), left);
1718
- }
1719
- else {
1720
- timeline.logIndexOutOfBounds("day", index, childCount);
1721
- return;
1722
- }
1723
- },
1724
- fillWeekBlock: function (left, index, date, lastTimelineBlock) {
1725
- let resBlock = null;
1726
- if (index > 0 && weekday == Weekday.First) {
1727
- weekIndex++;
1728
- firstWeek = false;
1729
- even = !even;
1730
- }
1731
- if (index == 0 || weekday == Weekday.First) {
1732
- let childCount = timeline.resolutionDiv.childElementCount;
1733
- if (timeline.isValidChildIndex(weekIndex, childCount)) {
1734
- resBlock = timeline.resolutionDiv.childNodes.item(weekIndex);
1735
- }
1736
- else {
1737
- timeline.logIndexOutOfBounds("week", weekIndex, childCount);
1738
- return;
1739
- }
1740
- }
1741
- timeline.fillWeekResolutionBlock(resBlock, date, weekIndex, weekday, firstWeek, lastTimelineBlock, left, even);
1742
- },
1743
- calcDaysLeftInFirstWeek: function (startDay) {
1744
- let daysLeftInWeek = 0;
1745
- if (startDay != timeline.firstDayOfWeek) {
1746
- for (let i = startDay;; i++) {
1747
- daysLeftInWeek++;
1748
- if (Math.max(i % 8, 1) === timeline.lastDayOfWeek) {
1749
- break;
1750
- }
1751
- }
1752
- }
1753
- return daysLeftInWeek;
1754
- },
1755
- isEven: function (startDate, startDay) {
1756
- let visibleRangeNormalStartDate = timeline.toNormalDate(startDate);
1757
- if (timeline.normalStartDate.getTime() < visibleRangeNormalStartDate.getTime()) {
1758
- let daysHidden = Math.floor(((visibleRangeNormalStartDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.DAY_INTERVAL));
1759
- console.log("Days hidden: " + daysHidden);
1760
- console.log("firstWeekBlockHidden = " + timeline.firstWeekBlockHidden);
1761
- if (daysHidden === 0) {
1762
- return false;
1763
- }
1764
- let daysLeftInFirstWeek = this.calcDaysLeftInFirstWeek(startDay);
1765
- if (daysHidden > daysLeftInFirstWeek) {
1766
- daysHidden -= daysLeftInFirstWeek;
1767
- }
1768
- let weeks = daysHidden / DateTimeConstants.DAYS_IN_WEEK;
1769
- let even = (weeks % 2) === 1;
1770
- return (timeline.firstWeekBlockHidden) ? !even : even;
1771
- }
1772
- return false;
1773
- },
1774
- getFirstDayOfVisibleRange: function (startDate) {
1775
- let visibleRangeNormalStartDate = timeline.toNormalDate(startDate);
1776
- if (timeline.normalStartDate.getTime() < visibleRangeNormalStartDate.getTime()) {
1777
- let days = Math.floor(((visibleRangeNormalStartDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.DAY_INTERVAL));
1778
- return ((timeline.firstDayOfRange - 1 + days) % 7) + 1;
1779
- }
1780
- return timeline.firstDayOfRange;
1781
- }
1782
- });
1783
- }
1784
- logIndexOutOfBounds(indexName, index, childCount) {
1785
- console.log("${indexName} index ${index} out of bounds with childCount ${childCount}. Can't fill content.");
1786
- }
1787
- fillTimelineForHour(interval, startDate, endDate, resBlockFiller) {
1788
- let currentYear = null;
1789
- let pos = startDate;
1790
- let end = endDate;
1791
- let index = 0;
1792
- let lastTimelineBlock = false;
1793
- let date;
1794
- resBlockFiller.setup();
1795
- while (pos <= end) {
1796
- date = pos;
1797
- let nextHour = new Date(pos.getTime() + interval);
1798
- lastTimelineBlock = nextHour.getTime() > end.getTime();
1799
- resBlockFiller.fillResolutionBlock(index, date, currentYear, lastTimelineBlock);
1800
- pos = nextHour;
1801
- index++;
1802
- }
1803
- }
1804
- fillTimelineForDayOrWeek(interval, startDate, endDate, resBlockFiller) {
1805
- let currentYear = null;
1806
- let pos = startDate;
1807
- pos = DateUtil.adjustToMiddleOfDay(pos, this.localeDataProvider.getTimeZone());
1808
- let end = endDate;
1809
- let index = 0;
1810
- let lastTimelineBlock = false;
1811
- let date;
1812
- let isDST = false;
1813
- let previousIsDST = this.localeDataProvider.isDaylightTime(startDate);
1814
- resBlockFiller.setup();
1815
- while (!lastTimelineBlock) {
1816
- let dstAdjusted = this.getDSTAdjustedDate(previousIsDST, pos);
1817
- date = dstAdjusted;
1818
- pos = dstAdjusted;
1819
- isDST = this.localeDataProvider.isDaylightTime(date);
1820
- lastTimelineBlock = this.getDSTAdjustedDate(isDST, new Date(date.getTime() + interval)).getTime() > end.getTime();
1821
- resBlockFiller.fillResolutionBlock(index, date, currentYear, lastTimelineBlock);
1822
- previousIsDST = isDST;
1823
- pos = new Date(pos.getTime() + interval);
1824
- index++;
1825
- }
1826
- }
1827
- isValidChildIndex(index, childCount) {
1828
- return (index >= 0) && (index < childCount);
1829
- }
1830
- fillDayResolutionBlock(resBlock, date, index, weekend, left) {
1831
- resBlock.innerText = this.localeDataProvider.formatDate(date, "d");
1832
- if (weekend) {
1833
- resBlock.classList.add(TimelineElement_1.STYLE_WEEKEND);
1834
- }
1835
- else {
1836
- resBlock.classList.remove(TimelineElement_1.STYLE_WEEKEND);
1837
- }
1838
- this.setCurrentStyle(resBlock, this.isCurrentDay(date));
1839
- if (this.setPositionForEachBlock && this.isTimelineOverflowingHorizontally()) {
1840
- resBlock.style.position = "relative";
1841
- resBlock.style.left = left + "px";
1842
- }
1843
- }
1844
- fillWeekResolutionBlock(resBlock, date, index, weekDay, firstWeek, lastBlock, left, even) {
1845
- if (resBlock != null) {
1846
- resBlock.innerText = this.formatWeekCaption(date);
1847
- if (even) {
1848
- resBlock.classList.add(TimelineElement_1.STYLE_EVEN);
1849
- }
1850
- else {
1851
- resBlock.classList.remove(TimelineElement_1.STYLE_EVEN);
1852
- }
1853
- if (this.setPositionForEachBlock && this.isTimelineOverflowingHorizontally()) {
1854
- resBlock.style.position = "relative";
1855
- resBlock.style.left = left + "px";
1856
- }
1857
- resBlock.classList.remove(TimelineElement_1.STYLE_FIRST, TimelineElement_1.STYLE_LAST);
1858
- // block is (re)filled starting from its first visible day; clear old state.
1859
- this.setCurrentStyle(resBlock, false);
1860
- }
1861
- if (this.isCurrentDay(date) && this.isValidChildIndex(index, this.resolutionDiv.childElementCount)) {
1862
- this.setCurrentStyle(this.resolutionDiv.childNodes.item(index), true);
1863
- }
1864
- if (firstWeek && (weekDay === Weekday.Last || lastBlock)) {
1865
- let firstEl = this.resolutionDiv.firstElementChild;
1866
- if (!firstEl.classList.contains(TimelineElement_1.STYLE_FIRST)) {
1867
- firstEl.classList.add(TimelineElement_1.STYLE_FIRST);
1868
- }
1869
- }
1870
- else if (lastBlock) {
1871
- let lastEl = this.resolutionDiv.lastChild;
1872
- if (!lastEl.classList.contains(TimelineElement_1.STYLE_LAST)) {
1873
- lastEl.classList.add(TimelineElement_1.STYLE_LAST);
1874
- }
1875
- }
1876
- }
1877
- fillHourResolutionBlock(resBlock, date, index, hourCounter, lastBlock, left, even) {
1878
- if (this.localeDataProvider.isTwelveHourClock()) {
1879
- resBlock.innerText = this.localeDataProvider.formatTime(date, "h");
1880
- }
1881
- else {
1882
- resBlock.innerText = this.localeDataProvider.formatTime(date, "HH");
1883
- }
1884
- if (even) {
1885
- resBlock.classList.add(TimelineElement_1.STYLE_EVEN);
1886
- }
1887
- else {
1888
- resBlock.classList.remove(TimelineElement_1.STYLE_EVEN);
1889
- }
1890
- this.setCurrentStyle(resBlock, this.isCurrentHour(date));
1891
- if (this.firstDay && (hourCounter == 24 || lastBlock)) {
1892
- this.firstDay = false;
1893
- this.firstResBlockCount = index + 1;
1894
- }
1895
- else if (lastBlock) {
1896
- this.lastResBlockCount = (index + 1 - this.firstResBlockCount) % 24;
1897
- }
1898
- if (this.setPositionForEachBlock && this.isTimelineOverflowingHorizontally()) {
1899
- resBlock.style.position = "relative";
1900
- resBlock.style.left = left + "px";
1901
- }
1902
- }
1903
- };
1904
- TimelineElement.STYLE_ROW = "row";
1905
- TimelineElement.STYLE_COL = "col";
1906
- TimelineElement.STYLE_MONTH = "month";
1907
- TimelineElement.STYLE_YEAR = "year";
1908
- TimelineElement.STYLE_DAY = "day";
1909
- TimelineElement.STYLE_WEEK = "w";
1910
- TimelineElement.STYLE_MONTH_RES = "m";
1911
- TimelineElement.STYLE_YEAR_RES = "y";
1912
- TimelineElement.STYLE_RESOLUTION = "resolution";
1913
- TimelineElement.STYLE_EVEN = "even";
1914
- TimelineElement.STYLE_WEEKEND = "weekend";
1915
- TimelineElement.STYLE_SPACER = "spacer";
1916
- TimelineElement.STYLE_FIRST = "f-col";
1917
- TimelineElement.STYLE_CENTER = "c-col";
1918
- TimelineElement.STYLE_LAST = "l-col";
1919
- TimelineElement.STYLE_MEASURE = "measure";
1920
- TimelineElement.STYLE_CURRENT = "current";
1921
- TimelineElement.CURRENT_DAY_FORMAT = "yyyy-MM-dd";
1922
- // Offset is included to tell apart the repeated hour when daylight saving time ends.
1923
- TimelineElement.CURRENT_HOUR_FORMAT = "yyyy-MM-dd HH xxx";
1924
- /* Average day counts used to convert a measured month/year block width to a day width. */
1925
- TimelineElement.AVERAGE_DAYS_IN_MONTH = 30;
1926
- TimelineElement.AVERAGE_DAYS_IN_YEAR = 365;
1927
- __decorate([
1928
- property({
1929
- reflect: true,
1930
- converter: {
1931
- fromAttribute: (value, type) => {
1932
- return Resolution[value];
1933
- },
1934
- toAttribute: (value, type) => {
1935
- return Resolution[value];
1936
- }
1937
- }
1938
- })
1939
- ], TimelineElement.prototype, "resolution", void 0);
1940
- __decorate([
1941
- property({
1942
- reflect: true,
1943
- converter: {
1944
- fromAttribute: (value, type) => {
1945
- return (value) ? (value.length > 13) ? value.substring(0, 13) : value : value;
1946
- },
1947
- }
1948
- })
1949
- ], TimelineElement.prototype, "startDateTime", void 0);
1950
- __decorate([
1951
- property({
1952
- reflect: true,
1953
- converter: {
1954
- fromAttribute: (value, type) => {
1955
- return (value) ? (value.length > 13) ? value.substring(0, 13) : value : value;
1956
- },
1957
- }
1958
- })
1959
- ], TimelineElement.prototype, "endDateTime", void 0);
1960
- __decorate([
1961
- property({ reflect: true })
1962
- ], TimelineElement.prototype, "timeZone", void 0);
1963
- __decorate([
1964
- property({ reflect: true })
1965
- ], TimelineElement.prototype, "locale", void 0);
1966
- __decorate([
1967
- property({ reflect: true })
1968
- ], TimelineElement.prototype, "firstDayOfWeek", void 0);
1969
- __decorate([
1970
- property({ reflect: true, type: Boolean })
1971
- ], TimelineElement.prototype, "twelveHourClock", void 0);
1972
- __decorate([
1973
- property()
1974
- ], TimelineElement.prototype, "minWidth", void 0);
1975
- __decorate([
1976
- property()
1977
- ], TimelineElement.prototype, "normalStartDate", void 0);
1978
- __decorate([
1979
- property()
1980
- ], TimelineElement.prototype, "normalEndDate", void 0);
1981
- __decorate([
1982
- property()
1983
- ], TimelineElement.prototype, "lastDayOfWeek", void 0);
1984
- __decorate([
1985
- property()
1986
- ], TimelineElement.prototype, "firstDayOfRange", void 0);
1987
- __decorate([
1988
- property()
1989
- ], TimelineElement.prototype, "firstHourOfRange", void 0);
1990
- __decorate([
1991
- property({ reflect: true })
1992
- ], TimelineElement.prototype, "scrollContainerId", void 0);
1993
- __decorate([
1994
- property()
1995
- ], TimelineElement.prototype, "monthRowVisible", void 0);
1996
- __decorate([
1997
- property()
1998
- ], TimelineElement.prototype, "yearRowVisible", void 0);
1999
- __decorate([
2000
- property()
2001
- ], TimelineElement.prototype, "monthNames", void 0);
2002
- __decorate([
2003
- property()
2004
- ], TimelineElement.prototype, "weekdayNames", void 0);
2005
- __decorate([
2006
- property({ reflect: true, type: Boolean })
2007
- ], TimelineElement.prototype, "currentDateVisible", void 0);
2008
- __decorate([
2009
- query('#resolutionDiv')
2010
- ], TimelineElement.prototype, "resolutionDiv", void 0);
2011
- TimelineElement = TimelineElement_1 = __decorate([
2012
- customElement('timeline-element')
2013
- ], TimelineElement);
261
+ <div id="resolutionDiv" class="row resolution"></div>`;
262
+ }
263
+ yearBlocks() {
264
+ if (this.isYearRowVisible()) {
265
+ return this.timelineBlocks(this.yearRowData, TimelineElement_1.STYLE_YEAR);
266
+ }
267
+ return nothing;
268
+ }
269
+ monthBlocks() {
270
+ if (this.isMonthRowVisible()) {
271
+ return this.timelineBlocks(this.monthRowData, TimelineElement_1.STYLE_MONTH);
272
+ }
273
+ return nothing;
274
+ }
275
+ dayBlocks() {
276
+ if (this.isDayRowVisible()) {
277
+ return this.timelineBlocks(this.dayRowData, TimelineElement_1.STYLE_DAY);
278
+ }
279
+ return nothing;
280
+ }
281
+ shouldUpdate(changedProperties) {
282
+ return changedProperties.has('resolution')
283
+ || changedProperties.has('startDateTime')
284
+ || changedProperties.has('endDateTime')
285
+ || changedProperties.has('locale')
286
+ || changedProperties.has('timeZone')
287
+ || changedProperties.has('firstDayOfWeek')
288
+ || changedProperties.has('twelveHourClock')
289
+ || changedProperties.has('yearRowVisible')
290
+ || changedProperties.has('monthRowVisible')
291
+ || changedProperties.has('monthNames')
292
+ || changedProperties.has('weekdayNames')
293
+ || changedProperties.has('currentDateVisible');
294
+ }
295
+ willUpdate(changedProps) {
296
+ if (changedProps.has('resolution')) {
297
+ this.minResolutionWidth = -1;
298
+ }
299
+ if (changedProps.has('resolution') || changedProps.has('startDateTime') || changedProps.has('timeZone')) {
300
+ this.firstDayOfRange = null;
301
+ this.firstHourOfRange = null;
302
+ // Start is the first instant of the given hour/day (like Temporal's
303
+ // startOfDay): a repeated hour starts at its first occurrence, and a
304
+ // skipped midnight/hour starts at the first wall time that exists.
305
+ const start = parseWallTime(this.startDateTime.substring(0, this.resolution === Resolution.Hour ? 13 : 10));
306
+ this.internalInclusiveStartDateTime = zonedWallTimeToInstant(start, this.timeZone);
307
+ }
308
+ if (changedProps.has('resolution') || changedProps.has('endDateTime') || changedProps.has('timeZone')) {
309
+ // End is the last millisecond of the given hour/day, i.e. 1 ms before the next
310
+ // hour/day starts: 1AM becomes "01:59:59.999". A repeated end hour includes
311
+ // both occurrences; a skipped end hour adds nothing after the previous hour.
312
+ const end = parseWallTime(this.endDateTime.substring(0, this.resolution === Resolution.Hour ? 13 : 10));
313
+ const next = this.resolution === Resolution.Hour ? { ...end, hour: end.hour + 1 } : { ...end, day: end.day + 1 };
314
+ this.internalInclusiveEndDateTime = new Date(zonedWallTimeToInstant(next, this.timeZone).getTime() - 1);
315
+ }
316
+ this.updateTimeLine(this.resolution, this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime, new DefaultLocaleDataProvider(this.locale, this.timeZone, this.firstDayOfWeek, this.twelveHourClock));
317
+ }
318
+ updated() {
319
+ if (!(this.resolution) || !this.internalInclusiveStartDateTime || !this.internalInclusiveEndDateTime) {
320
+ return;
321
+ }
322
+ if (this.resolution !== Resolution.Day && this.resolution !== Resolution.Week && this.resolution !== Resolution.Hour
323
+ && !this.isMonthOrYearResolution()) {
324
+ console.warn("TimelineElement resolution " + (this.resolution ? Resolution[this.resolution] : "null")
325
+ + " is not supported");
326
+ return;
327
+ }
328
+ this.log("TimelineElement Constructed content.");
329
+ this.updateWidths();
330
+ this.log("TimelineElement is updated for resolution " + Resolution[this.resolution] + ".");
331
+ this.registerScrollHandler();
332
+ }
333
+ /**
334
+ * <p>
335
+ * Updates the content of this component. Builds the time-line and calculates
336
+ * width and heights for the content (calls in the end
337
+ * {@link #updateWidths()}). This should be called explicitly. Otherwise the
338
+ * component will be empty.
339
+ * <p>
340
+ * Date values should always follow specification in {@link Date#getTime()}.
341
+ * Start and end date is always required.
342
+ *
343
+ * @param resolution
344
+ * Resolution enum (not null)
345
+ * @param startDate
346
+ * Time-line's start date. (inclusive; not null)
347
+ * @param endDate
348
+ * Time-line's end date. (inclusive; not null)
349
+ * @param localeDataProvider
350
+ * Data provider for locale specific data. month names, first day
351
+ * of week etc.
352
+ *
353
+ */
354
+ updateTimeLine(resolution, startDate, endDate, localeDataProvider) {
355
+ if (!localeDataProvider) {
356
+ console.warn("TimelineElement requires ILocaleDataProvider. Can't complete update(...) operation.");
357
+ return;
358
+ }
359
+ this.clear();
360
+ this.log("TimelineElement content cleared.");
361
+ if (!(resolution) || !startDate || !endDate) {
362
+ return;
363
+ }
364
+ this.log("TimelineElement Updating content.");
365
+ this.localeDataProvider = localeDataProvider;
366
+ this.resolution = resolution;
367
+ this.resetDateRange(startDate, endDate);
368
+ this.lastDayOfWeek = (localeDataProvider.getFirstDayOfWeek() == 1) ? 7 : Math.max((localeDataProvider.getFirstDayOfWeek() - 1) % 8, 1);
369
+ this.monthNames = this.monthNames || localeDataProvider.getMonthNames();
370
+ this.weekdayNames = this.weekdayNames || localeDataProvider.getWeekdayNames();
371
+ if (this.minResolutionWidth < 0) {
372
+ this.minResolutionWidth = this.calculateResolutionMinWidth();
373
+ }
374
+ if (this.resolution === Resolution.Day || this.resolution === Resolution.Week || this.isMonthOrYearResolution()) {
375
+ this.prepareTimelineForDayOrWeekResolution(this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime);
376
+ }
377
+ else if (this.resolution === Resolution.Hour) {
378
+ this.prepareTimelineForHourResolution(this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime);
379
+ }
380
+ }
381
+ resetDateRange(startDate, endDate) {
382
+ this.internalInclusiveStartDateTime = startDate;
383
+ this.internalInclusiveEndDateTime = endDate;
384
+ this.normalStartDate = this.toNormalDate(this.internalInclusiveStartDateTime);
385
+ this.normalEndDate = this.toNormalDate(this.internalInclusiveEndDateTime);
386
+ // Weekday and hour in the timeline's time zone (not the browser's).
387
+ const zonedStart = getZonedFields(this.internalInclusiveStartDateTime, this.localeDataProvider.getTimeZone());
388
+ // zonedStart.dayOfWeek is ISO (Monday = 1 ... Sunday = 7); firstDayOfRange is 1-based from Sunday (Sunday = 1).
389
+ this.firstDayOfRange = this.firstDayOfRange || (zonedStart.dayOfWeek % 7) + 1;
390
+ this.firstHourOfRange = this.firstHourOfRange || zonedStart.hour;
391
+ }
392
+ registerScrollHandler() {
393
+ if (this.scrollHandler) {
394
+ return;
395
+ }
396
+ this.scrollContainer = this.setupScrollContainer();
397
+ this.scrollHandler = () => {
398
+ window.requestAnimationFrame(() => {
399
+ const container = this.scrollContainer;
400
+ const sl = container instanceof Window ? container.scrollX : container.scrollLeft;
401
+ const st = container instanceof Window ? container.scrollY : container.scrollTop;
402
+ if (sl != this.previousContainerScrollLeft) {
403
+ this.setScrollLeft(sl);
404
+ this.previousContainerScrollLeft = sl;
405
+ }
406
+ if (st != this.previousContainerScrollTop) {
407
+ this.previousContainerScrollTop = st;
408
+ }
409
+ });
410
+ };
411
+ this.scrollContainer.addEventListener('scroll', this.scrollHandler);
412
+ }
413
+ setupScrollContainer() {
414
+ let scrollContainer;
415
+ if (this.scrollContainerId) {
416
+ scrollContainer = this.getParentElement(this).querySelector('#' + this.scrollContainerId);
417
+ if (!scrollContainer) {
418
+ scrollContainer = document.querySelector('#' + this.scrollContainerId);
419
+ }
420
+ if (scrollContainer) {
421
+ scrollContainer.style.overflowX = "auto";
422
+ }
423
+ }
424
+ if (!scrollContainer) {
425
+ scrollContainer = this.getParentElement(this);
426
+ this.directlyInsideScrollContainer = true;
427
+ if (scrollContainer === document.body) {
428
+ return window; // window scrolls by default, not body
429
+ }
430
+ }
431
+ return scrollContainer;
432
+ }
433
+ clear() {
434
+ this.spacerBlocks = [];
435
+ this.yearRowData.clear();
436
+ this.monthRowData.clear();
437
+ this.dayRowData.clear();
438
+ }
439
+ calculateResolutionMinWidth() {
440
+ const resDivMeasure = document.createElement('div');
441
+ resDivMeasure.classList.add(TimelineElement_1.STYLE_ROW, TimelineElement_1.STYLE_RESOLUTION);
442
+ const resBlockMeasure = document.createElement('div');
443
+ if (this.resolution === Resolution.Week) {
444
+ // configurable with '.col.w.measure' selector
445
+ resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_WEEK, TimelineElement_1.STYLE_MEASURE);
446
+ }
447
+ else if (this.resolution === Resolution.Month) {
448
+ // configurable with '.col.m.measure' selector
449
+ resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_MONTH_RES, TimelineElement_1.STYLE_MEASURE);
450
+ }
451
+ else if (this.resolution === Resolution.Year) {
452
+ // configurable with '.col.y.measure' selector
453
+ resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_YEAR_RES, TimelineElement_1.STYLE_MEASURE);
454
+ }
455
+ else {
456
+ // measure for text 'MM'
457
+ resBlockMeasure.innerText = "MM";
458
+ // configurable with '.col.measure' selector
459
+ resBlockMeasure.classList.add(TimelineElement_1.STYLE_COL, TimelineElement_1.STYLE_MEASURE);
460
+ }
461
+ resDivMeasure.appendChild(resBlockMeasure);
462
+ this.shadowRoot.appendChild(resDivMeasure);
463
+ let width = resBlockMeasure.clientWidth;
464
+ if (this.resolution === Resolution.Week) {
465
+ // divide given width by number of days in week
466
+ width = width / DateTimeConstants.DAYS_IN_WEEK;
467
+ }
468
+ let minDayWidth = this.resolutionWeekDayblockWidth;
469
+ if (this.resolution === Resolution.Month) {
470
+ // divide given width by average number of days in month
471
+ width = width / TimelineElement_1.AVERAGE_DAYS_IN_MONTH;
472
+ minDayWidth = this.resolutionMonthDayblockWidth;
473
+ }
474
+ else if (this.resolution === Resolution.Year) {
475
+ // divide given width by number of days in year
476
+ width = width / TimelineElement_1.AVERAGE_DAYS_IN_YEAR;
477
+ minDayWidth = this.resolutionYearDayblockWidth;
478
+ }
479
+ width = (width < minDayWidth) ? minDayWidth : width;
480
+ this.shadowRoot.removeChild(resDivMeasure);
481
+ return width;
482
+ }
483
+ registerHourResolutionBlock() {
484
+ this.blocksInRange++;
485
+ this.resolutionBlockCount++;
486
+ }
487
+ registerDayResolutionBlock() {
488
+ this.blocksInRange++;
489
+ this.resolutionBlockCount++;
490
+ }
491
+ registerWeekResolutionBlock(index, weekDay, lastBlock, firstWeek) {
492
+ if (index == 0 || weekDay === Weekday.First) {
493
+ this.resolutionBlockCount++;
494
+ }
495
+ if (firstWeek && (weekDay === Weekday.Last || lastBlock)) {
496
+ this.firstResBlockCount = index + 1;
497
+ }
498
+ else if (lastBlock) {
499
+ this.lastResBlockCount = (index + 1 - this.firstResBlockCount) % 7;
500
+ }
501
+ this.blocksInRange++;
502
+ }
503
+ /*
504
+ * Registers one day for Month or Year resolution. A new resolution block is
505
+ * started whenever the month (or year) of the given date changes.
506
+ */
507
+ registerMonthOrYearResolutionBlock(index, date) {
508
+ const key = this.getMonthOrYearResolutionBlockKey(date);
509
+ const lastIndex = this.resBlockKeys.length - 1;
510
+ if (index == 0 || lastIndex < 0 || this.resBlockKeys[lastIndex] !== key) {
511
+ this.resolutionBlockCount++;
512
+ this.resBlockKeys.push(key);
513
+ this.resBlockStartDates.push(date);
514
+ this.resBlockDayCounts.push(1);
515
+ }
516
+ else {
517
+ this.resBlockDayCounts[lastIndex]++;
518
+ }
519
+ this.firstResBlockCount = this.resBlockDayCounts[0];
520
+ this.lastResBlockCount = this.resBlockDayCounts[this.resBlockDayCounts.length - 1];
521
+ this.blocksInRange++;
522
+ }
523
+ /* Returns 'yyyy-MM' key for Month resolution and 'yyyy' key for Year resolution. */
524
+ getMonthOrYearResolutionBlockKey(date) {
525
+ return this.localeDataProvider.formatDate(date, this.resolution === Resolution.Year ? "yyyy" : "yyyy-MM");
526
+ }
527
+ isMonthOrYearResolution() {
528
+ return this.resolution === Resolution.Month || this.resolution === Resolution.Year;
529
+ }
530
+ /* Year row is hidden with Year resolution as the resolution row shows years already. */
531
+ isYearRowVisible() {
532
+ return this.yearRowVisible && this.resolution !== Resolution.Year;
533
+ }
534
+ /* Month row is hidden with Month and Year resolutions: Month resolution row shows months already,
535
+ * and Year resolution shows a single year row. */
536
+ isMonthRowVisible() {
537
+ return this.monthRowVisible && !this.isMonthOrYearResolution();
538
+ }
539
+ timelineBlocks(rowData, style) {
540
+ const itemTemplates = [];
541
+ for (const entry of rowData.getBlockEntries()) {
542
+ itemTemplates.push(html `${entry[1]}`);
543
+ }
544
+ if (this.isAlwaysCalculatePixelWidths()) {
545
+ itemTemplates.push(html `${this.createSpacerBlock(style)}`);
546
+ }
547
+ return itemTemplates;
548
+ }
549
+ /**
550
+ * Returns true if Widget is set to calculate widths by itself. Default is
551
+ * false.
552
+ *
553
+ * @return
554
+ */
555
+ isAlwaysCalculatePixelWidths() {
556
+ return this.calcPixels;
557
+ }
558
+ createSpacerBlock(className) {
559
+ const block = document.createElement('div');
560
+ block.classList.add(TimelineElement_1.STYLE_ROW, className, TimelineElement_1.STYLE_SPACER);
561
+ block.innerText = " ";
562
+ block.style.display = "none"; // not visible by default
563
+ this.spacerBlocks.push(block);
564
+ return block;
565
+ }
566
+ /** Clears Daylight saving time adjustment from the given time. */
567
+ toNormalDate(zonedDate) {
568
+ return DateUtil.toNormalDate(zonedDate, this.localeDataProvider.getDaylightAdjustment(zonedDate));
569
+ }
570
+ getParentElement(node) {
571
+ const parent = node.parentNode;
572
+ if (!parent || parent.nodeType != Node.ELEMENT_NODE) {
573
+ return null;
574
+ }
575
+ return parent;
576
+ }
577
+ /* Logs a diagnostic message when the debug property is set. */
578
+ log(message) {
579
+ if (this.debug) {
580
+ console.log(message);
581
+ }
582
+ }
583
+ getDay(date) {
584
+ // by adjusting the date to the middle of the day before formatting is a
585
+ // workaround to avoid DST issues with DateTimeFormatter.
586
+ const adjusted = DateUtil.adjustToMiddleOfDay(date, this.localeDataProvider.getTimeZone());
587
+ return this.localeDataProvider.formatDate(adjusted, "d");
588
+ }
589
+ getYear(date) {
590
+ return this.localeDataProvider.formatDate(date, "yyyy");
591
+ }
592
+ getMonth(date) {
593
+ const m = this.localeDataProvider.formatDate(date, "M");
594
+ return parseInt(m) - 1;
595
+ }
596
+ isWeekEnd(dayCounter) {
597
+ return dayCounter == 1 || dayCounter == 7;
598
+ }
599
+ key(prefix, rowData) {
600
+ return prefix + "_" + (rowData.size());
601
+ }
602
+ newKey(prefix, rowData) {
603
+ return prefix + "_" + (rowData.size() + 1);
604
+ }
605
+ addBlock(current, target, date, rowData, operation) {
606
+ let key;
607
+ if (target !== current) {
608
+ current = target;
609
+ key = this.newKey("" + current, rowData);
610
+ operation(target, key, date);
611
+ }
612
+ else {
613
+ key = this.key("" + current, rowData);
614
+ rowData.setBlockLength(key, rowData.getBlockLength(key) + 1);
615
+ }
616
+ return current;
617
+ }
618
+ addDayBlock(currentDay, date) {
619
+ const day = this.getDay(date);
620
+ return this.addBlock(currentDay, day, date, this.dayRowData, (day, key, date) => {
621
+ const block = this.addDayBlockElement(key, this.formatDayCaption(day, date));
622
+ block.dataset.day = this.localeDataProvider.formatDate(date, TimelineElement_1.CURRENT_DAY_FORMAT);
623
+ });
624
+ }
625
+ addMonthBlock(currentMonth, date) {
626
+ const month = this.getMonth(date);
627
+ return this.addBlock(currentMonth, "" + month, date, this.monthRowData, (target, key, date) => {
628
+ this.addMonthBlockElement(key, this.formatMonthCaption(month, date));
629
+ });
630
+ }
631
+ addYearBlock(currentYear, date) {
632
+ const year = this.getYear(date);
633
+ return this.addBlock(currentYear, year, date, this.yearRowData, (year, key, date) => {
634
+ this.addYearBlockElement(key, this.formatYearCaption(year, date));
635
+ });
636
+ }
637
+ addMonthBlockElement(key, text) {
638
+ this.createTimelineBlock(key, text, TimelineElement_1.STYLE_MONTH, this.monthRowData);
639
+ }
640
+ addYearBlockElement(key, text) {
641
+ this.createTimelineBlock(key, text, TimelineElement_1.STYLE_YEAR, this.yearRowData);
642
+ }
643
+ addDayBlockElement(key, text) {
644
+ return this.createTimelineBlock(key, text, TimelineElement_1.STYLE_DAY, this.dayRowData);
645
+ }
646
+ createTimelineBlock(key, text, styleSuffix, rowData) {
647
+ const div = document.createElement('div');
648
+ div.classList.add(TimelineElement_1.STYLE_ROW, styleSuffix);
649
+ div.innerText = text;
650
+ rowData.setBlockLength(key, 1);
651
+ rowData.setBlock(key, div);
652
+ return div;
653
+ }
654
+ formatDayCaption(day, date) {
655
+ if (!this.dayFormat || this.dayFormat === "") {
656
+ return day;
657
+ }
658
+ return this.localeDataProvider.formatDate(date, this.dayFormat);
659
+ }
660
+ formatYearCaption(year, date) {
661
+ if (!this.yearFormat || this.yearFormat === "") {
662
+ return year;
663
+ }
664
+ return this.localeDataProvider.formatDate(date, this.yearFormat);
665
+ }
666
+ formatWeekCaption(date) {
667
+ if (!this.weekFormat || this.weekFormat === "") {
668
+ // ISO week of the calendar date in the timeline's zone.
669
+ const { year, month, day } = getZonedFields(date, this.localeDataProvider.getTimeZone());
670
+ return "" + isoWeekOfYear(year, month, day);
671
+ }
672
+ return this.localeDataProvider.formatDate(date, this.weekFormat);
673
+ }
674
+ formatMonthCaption(month, date) {
675
+ if (!this.monthFormat || this.monthFormat === "") {
676
+ return this.monthNames[month];
677
+ }
678
+ return this.localeDataProvider.formatDate(date, this.monthFormat);
679
+ }
680
+ getWeekday(dayCounter) {
681
+ if (dayCounter === this.localeDataProvider.getFirstDayOfWeek()) {
682
+ return Weekday.First;
683
+ }
684
+ if (dayCounter === this.lastDayOfWeek) {
685
+ return Weekday.Last;
686
+ }
687
+ return Weekday.Between;
688
+ }
689
+ prepareTimelineForHourResolution(startDate, endDate) {
690
+ const timeline = this;
691
+ this.firstDay = true;
692
+ let hourCounter = this.firstHourOfRange;
693
+ this.prepareTimelineForHour(DateTimeConstants.HOUR_INTERVAL, startDate, endDate, {
694
+ registerResolutionBlock(index, date, currentYear, lastTimelineBlock) {
695
+ timeline.registerHourResolutionBlock();
696
+ hourCounter = Math.max((hourCounter + 1) % 25, 1);
697
+ }
698
+ });
699
+ }
700
+ prepareTimelineForHour(interval, startDate, endDate, resBlockRegisterer) {
701
+ this.blocksInRange = 0;
702
+ this.resolutionBlockCount = 0;
703
+ this.firstResBlockCount = 0;
704
+ this.lastResBlockCount = 0;
705
+ let currentYear = null;
706
+ let currentMonth = null;
707
+ let currentDay = null;
708
+ let pos = startDate;
709
+ const end = endDate;
710
+ let index = 0;
711
+ let lastTimelineBlock;
712
+ let date;
713
+ while (pos.getTime() <= end.getTime()) {
714
+ date = pos;
715
+ const nextHour = new Date(pos.getTime() + interval);
716
+ lastTimelineBlock = nextHour.getTime() > end.getTime();
717
+ resBlockRegisterer.registerResolutionBlock(index, date, currentYear, lastTimelineBlock);
718
+ if (this.yearRowVisible) {
719
+ currentYear = this.addYearBlock(currentYear, date);
720
+ }
721
+ if (this.monthRowVisible) {
722
+ currentMonth = this.addMonthBlock(currentMonth, date);
723
+ }
724
+ if (this.isDayRowVisible()) {
725
+ currentDay = this.addDayBlock(currentDay, date);
726
+ }
727
+ pos = nextHour;
728
+ index++;
729
+ }
730
+ }
731
+ prepareTimelineForDayOrWeekResolution(startDate, endDate) {
732
+ const timeline = this;
733
+ let dayCounter = this.firstDayOfRange;
734
+ let weekday;
735
+ let firstWeek = true;
736
+ this.resBlockDayCounts = [];
737
+ this.resBlockStartDates = [];
738
+ this.resBlockKeys = [];
739
+ this.prepareTimelineForDayOrWeek(DateTimeConstants.DAY_INTERVAL, startDate, endDate, {
740
+ registerResolutionBlock: function (index, date, currentYear, lastTimelineBlock) {
741
+ weekday = timeline.getWeekday(dayCounter);
742
+ if (timeline.isMonthOrYearResolution()) {
743
+ timeline.registerMonthOrYearResolutionBlock(index, date);
744
+ }
745
+ else if (timeline.resolution === Resolution.Week) {
746
+ timeline.registerWeekResolutionBlock(index, weekday, lastTimelineBlock, firstWeek);
747
+ if (firstWeek && (weekday === Weekday.Last || lastTimelineBlock)) {
748
+ firstWeek = false;
749
+ }
750
+ }
751
+ else {
752
+ timeline.registerDayResolutionBlock();
753
+ }
754
+ dayCounter = Math.max((dayCounter + 1) % 8, 1);
755
+ }
756
+ });
757
+ }
758
+ prepareTimelineForDayOrWeek(interval, startDate, endDate, resBlockRegisterer) {
759
+ this.blocksInRange = 0;
760
+ this.resolutionBlockCount = 0;
761
+ this.firstResBlockCount = 0;
762
+ this.lastResBlockCount = 0;
763
+ let currentYear = null;
764
+ let currentMonth = null;
765
+ let currentDay = null;
766
+ const timeZone = this.localeDataProvider.getTimeZone();
767
+ // One block per calendar day, positioned at 12:00 wall-clock time. Stepping by
768
+ // calendar days in the zone handles 23h/25h DST switch days.
769
+ let date = DateUtil.adjustToMiddleOfDay(startDate, timeZone);
770
+ const end = endDate;
771
+ let index = 0;
772
+ let lastTimelineBlock = false;
773
+ while (!lastTimelineBlock) {
774
+ const next = addDaysInZone(date, 1, timeZone);
775
+ lastTimelineBlock = next.getTime() > end.getTime();
776
+ resBlockRegisterer.registerResolutionBlock(index, date, currentYear, lastTimelineBlock);
777
+ if (this.isYearRowVisible()) {
778
+ currentYear = this.addYearBlock(currentYear, date);
779
+ }
780
+ if (this.isMonthRowVisible()) {
781
+ currentMonth = this.addMonthBlock(currentMonth, date);
782
+ }
783
+ if (this.isDayRowVisible()) {
784
+ currentDay = this.addDayBlock(currentDay, date);
785
+ }
786
+ date = next;
787
+ index++;
788
+ }
789
+ }
790
+ isDayRowVisible() {
791
+ return this.resolution === Resolution.Hour;
792
+ }
793
+ /**
794
+ * Get actual width of the timeline.
795
+ *
796
+ * @return
797
+ */
798
+ getResolutionWidth() {
799
+ if (!this.isTimelineOverflowingHorizontally()) {
800
+ return this.calculateTimelineWidth();
801
+ }
802
+ let width = this.getResolutionDivWidth();
803
+ if (this.isAlwaysCalculatePixelWidths() && this.containsResBlockSpacer()) {
804
+ width = width - ElementUtil.getWidth(this.resSpacerDiv);
805
+ }
806
+ return width;
807
+ }
808
+ /**
809
+ * Calculate the exact width of the timeline. Excludes any spacers in the
810
+ * end.
811
+ *
812
+ * @return
813
+ */
814
+ calculateTimelineWidth() {
815
+ const last = this.getLastResolutionElement();
816
+ if (last === null) {
817
+ return 0.0;
818
+ }
819
+ const r = ElementUtil.getRight(last);
820
+ const l = ElementUtil.getLeft(this.getFirstResolutionElement());
821
+ const timelineRealWidth = r - l;
822
+ return timelineRealWidth;
823
+ }
824
+ /*
825
+ * Get width of the resolution div element.
826
+ */
827
+ getResolutionDivWidth() {
828
+ if (!this.isTimelineOverflowingHorizontally()) {
829
+ return ElementUtil.getWidth(this.resolutionDiv);
830
+ }
831
+ return this.blocksInRange * this.minResolutionWidth;
832
+ }
833
+ /**
834
+ * Calculate matching left offset in percentage for a date (
835
+ * {@link Date#getTime()}).
836
+ *
837
+ * @param date
838
+ * Target date in milliseconds.
839
+ * @param contentWidth
840
+ * Width of the content that the given 'date' is relative to.
841
+ * @return Left offset in percentage.
842
+ */
843
+ getLeftPositionPercentageForDate(date, contentWidth) {
844
+ const timelineLeft = this.getLeftPositionForDate(date);
845
+ const relativeLeft = this.convertRelativeLeftPosition(timelineLeft, contentWidth);
846
+ const width = this.getResolutionWidth();
847
+ if (width <= 0) {
848
+ return 0; // not rendered (yet)
849
+ }
850
+ return (100.0 / width) * relativeLeft;
851
+ }
852
+ /**
853
+ * Calculate CSS value for 'left' property matching left offset in
854
+ * percentage for a date ( {@link Date#getTime()}).
855
+ * <p>
856
+ * May return '2.123456%' or 'calc(2.123456%)' if IE;
857
+ *
858
+ * @param date
859
+ * Target date in milliseconds.
860
+ * @param contentWidth
861
+ * Width of the content that the given 'date' is relative to.
862
+ * @return Left offset as a String value.
863
+ */
864
+ getLeftPositionPercentageStringForDate(date, contentWidth) {
865
+ const timelineLeft = this.getLeftPositionForDate(date);
866
+ const relativeLeft = this.convertRelativeLeftPosition(timelineLeft, contentWidth);
867
+ const width = this.getResolutionWidth();
868
+ if (width <= 0) {
869
+ return "0%"; // not rendered (yet)
870
+ }
871
+ const calc = this.createCalcCssValue(width, relativeLeft);
872
+ if (calc != null) {
873
+ return calc;
874
+ }
875
+ return (100.0 / width) * relativeLeft + "%";
876
+ }
877
+ getLeftPositionPercentageStringForDateRange(date, rangeWidth, rangeStartDate, rangeEndDate) {
878
+ if (!(rangeWidth > 0)) {
879
+ return "0%"; // e.g. parent element not rendered (yet)
880
+ }
881
+ const rangeLeft = this.getLeftPositionForDateRange(date, rangeWidth, rangeStartDate, rangeEndDate);
882
+ const width = rangeWidth;
883
+ const calc = this.createCalcCssValue(width, rangeLeft);
884
+ if (calc != null) {
885
+ return calc;
886
+ }
887
+ return (100.0 / width) * rangeLeft + "%";
888
+ }
889
+ /**
890
+ * Calculate CSS value for 'width' property matching date interval inside
891
+ * the time-line. Returns percentage value. Interval is in milliseconds.
892
+ * <p>
893
+ * May return '2.123456%' or 'calc(2.123456%)' if IE;
894
+ *
895
+ * @param interval
896
+ * Date interval in milliseconds.
897
+ * @return
898
+ */
899
+ getWidthPercentageStringForDateInterval(interval) {
900
+ const range = this.internalInclusiveEndDateTime.getTime() - this.internalInclusiveStartDateTime.getTime();
901
+ return this.getWidthPercentageStringForDateIntervalForRange(interval, range);
902
+ }
903
+ /** @see #getWidthPercentageStringForDateInterval(long) */
904
+ getWidthPercentageStringForDateIntervalForRange(interval, range) {
905
+ if (!(range > 0)) {
906
+ return "0%"; // e.g. zero-length parent range
907
+ }
908
+ const calc = this.createCalcCssValue(range, interval);
909
+ if (calc != null) {
910
+ return calc;
911
+ }
912
+ return (100.0 / range) * interval + "%";
913
+ }
914
+ /**
915
+ * Calculate matching left offset in pixels for a date (
916
+ * {@link Date#getTime()}).
917
+ *
918
+ * @param date
919
+ * Target date in milliseconds.
920
+ * @return Left offset in pixels.
921
+ */
922
+ getLeftPositionForDate(date) {
923
+ return this.getLeftPositionForDateRange(date, this.getResolutionWidth(), this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime);
924
+ }
925
+ getLeftPositionForDateRange(date, rangeWidth, rangeStartDate, rangeEndDate) {
926
+ const width = rangeWidth;
927
+ const range = rangeEndDate.getTime() - rangeStartDate.getTime();
928
+ if (range <= 0) {
929
+ return 0;
930
+ }
931
+ const p = width / range;
932
+ const offset = date.getTime() - rangeStartDate.getTime();
933
+ const left = p * offset;
934
+ return left;
935
+ }
936
+ /**
937
+ * Calculate matching date ({@link Date#getTime()}) for the target left
938
+ * pixel offset.
939
+ *
940
+ * @param left
941
+ * Left offset in pixels.
942
+ * @return Date in a milliseconds or null if timeline width is invalid (<=0).
943
+ */
944
+ getDateForLeftPosition(left) {
945
+ return this.getDateForLeftPositionNoticeDST(left, this.resolution === Resolution.Hour);
946
+ }
947
+ getDateForLeftPositionNoticeDST(left, noticeDST) {
948
+ const width = this.getResolutionWidth();
949
+ if (width <= 0) {
950
+ return null;
951
+ }
952
+ let range = this.normalEndDate.getTime() - this.normalStartDate.getTime();
953
+ if (noticeDST) {
954
+ range = this.adjustDateRangeByDST(range);
955
+ }
956
+ const p = range / width;
957
+ const offset = p * left;
958
+ const date = new Date(this.internalInclusiveStartDateTime.getTime() + offset);
959
+ if (this.debug) {
960
+ this.log("Zoned: " + this.localeDataProvider.formatDate(date, "dd. HH:mm") + " DST: "
961
+ + this.localeDataProvider.getDaylightAdjustment(date) / 60000);
962
+ }
963
+ return date;
964
+ }
965
+ /**
966
+ * Convert left position for other relative target width.
967
+ *
968
+ * @param left
969
+ * @param contentWidthToConvertFor
970
+ * @return
971
+ */
972
+ convertRelativeLeftPosition(left, contentWidthToConvertFor) {
973
+ const width = this.getResolutionWidth();
974
+ if (width <= 0 || contentWidthToConvertFor <= 0) {
975
+ return 0;
976
+ }
977
+ const relativePosition = (1.0 / contentWidthToConvertFor) * left;
978
+ const timelineLeft = relativePosition * width;
979
+ return timelineLeft;
980
+ }
981
+ adjustDateRangeByDST(range) {
982
+ /*
983
+ * Notice extra block(s) or missing block(s) in range when start time is
984
+ * in DST and end time is not, or vice versa.
985
+ */
986
+ // 'range' is a wall-clock range (see toNormalDate()); remove the DST
987
+ // difference between start and end to get the real elapsed range.
988
+ const dstStart = this.localeDataProvider.getDaylightAdjustment(this.internalInclusiveStartDateTime);
989
+ const dstEnd = this.localeDataProvider.getDaylightAdjustment(this.internalInclusiveEndDateTime);
990
+ return range - (dstEnd - dstStart);
991
+ }
992
+ /**
993
+ * Set horizontal scroll position for the time-line.
994
+ *
995
+ * @param left
996
+ * Scroll position in pixels.
997
+ */
998
+ setScrollLeft(left) {
999
+ if (this.positionLeft === left) {
1000
+ return;
1001
+ }
1002
+ this.positionLeft = left || 0;
1003
+ if (!this.directlyInsideScrollContainer) {
1004
+ this.style.left = -this.positionLeft + "px";
1005
+ }
1006
+ this.lazyResolutionPaint = setTimeout(() => this.fillVisibleTimeline(), 20);
1007
+ }
1008
+ /**
1009
+ * Re-calculates required widths for this widget.
1010
+ * <p>
1011
+ * Re-creates and fills the visible part of the resolution element.
1012
+ */
1013
+ updateWidths() {
1014
+ if (this.resolutionDiv == null) {
1015
+ this.log("TimelineElement is not ready for updateWidths() call. Call update(...) instead.");
1016
+ return;
1017
+ }
1018
+ this.log("TimelineElement Started updating widths.");
1019
+ // start by clearing old content in resolution element
1020
+ while (this.resolutionDiv.firstChild) {
1021
+ this.resolutionDiv.removeChild(this.resolutionDiv.lastChild);
1022
+ }
1023
+ this.setMinWidth(this.blocksInRange * this.minResolutionWidth);
1024
+ // update horizontal overflow state here, after min-width is updated.
1025
+ this.updateTimelineOverflowingHorizontally();
1026
+ this.createTimelineElementsOnVisibleArea();
1027
+ // fill timeline
1028
+ this.fillVisibleTimeline();
1029
+ // remove spacer block if it exist
1030
+ this.removeResolutionSpacerBlock();
1031
+ // calculate new block width for day-resolution.
1032
+ // Year and month blocks are vertically in-line with days.
1033
+ this.dayWidthPercentage = 100.0 / this.blocksInRange;
1034
+ this.dayOrHourWidthPx = this.calculateDayOrHourResolutionBlockWidthPx(this.blocksInRange);
1035
+ // calculate block width for currently selected resolution
1036
+ // (day,week,...)
1037
+ // resolution div's content may not be vertically in-line with
1038
+ // year/month blocks. This is the case for example with Week resolution.
1039
+ this.resBlockMinWidthPx = this.minResolutionWidth;
1040
+ this.resBlockWidthPx = this.calculateActualResolutionBlockWidthPx(this.dayOrHourWidthPx);
1041
+ this.resBlockWidthPercentage = 100.0 / this.resolutionBlockCount;
1042
+ let pct = this.createCalcCssValue(this.resolutionBlockCount, null);
1043
+ if (this.resolution === Resolution.Week) {
1044
+ this.resBlockMinWidthPx = DateTimeConstants.DAYS_IN_WEEK * this.minResolutionWidth;
1045
+ this.resBlockWidthPercentage = this.dayWidthPercentage * DateTimeConstants.DAYS_IN_WEEK;
1046
+ pct = this.createCalcCssValue(this.blocksInRange, DateTimeConstants.DAYS_IN_WEEK);
1047
+ }
1048
+ else if (this.isMonthOrYearResolution()) {
1049
+ // Month and year blocks have variable widths; these values represent an average block.
1050
+ this.resBlockMinWidthPx = this.calculateMinimumResolutionBlockWidth();
1051
+ this.resBlockWidthPercentage = 100.0 / this.resolutionBlockCount;
1052
+ }
1053
+ // update resolution block widths
1054
+ if (this.isMonthOrYearResolution()) {
1055
+ this.updateMonthOrYearResolutionBlockWidths();
1056
+ }
1057
+ else {
1058
+ this.updateResolutionBlockWidths(pct);
1059
+ }
1060
+ if (this.isYearRowVisible()) {
1061
+ // update year block widths
1062
+ this.updateBlockWidths(this.yearRowData);
1063
+ }
1064
+ if (this.isMonthRowVisible()) {
1065
+ // update month block widths
1066
+ this.updateBlockWidths(this.monthRowData);
1067
+ }
1068
+ if (this.isDayRowVisible()) {
1069
+ this.updateBlockWidths(this.dayRowData);
1070
+ }
1071
+ if (this.isAlwaysCalculatePixelWidths()) {
1072
+ this.updateSpacerBlocks(this.dayOrHourWidthPx);
1073
+ }
1074
+ this.log("TimelineElement Widths are updated.");
1075
+ }
1076
+ updateBlockWidths(rowData) {
1077
+ for (const entry of rowData.getBlockEntries()) {
1078
+ this.setWidth(entry[1], rowData.getBlockLength(entry[0]));
1079
+ }
1080
+ }
1081
+ updateSpacerBlocks(dayWidthPx) {
1082
+ const spaceLeft = this.getResolutionDivWidth() - (this.blocksInRange * dayWidthPx);
1083
+ if (spaceLeft > 0) {
1084
+ for (const e of this.spacerBlocks) {
1085
+ e.style.removeProperty("display");
1086
+ e.style.width = spaceLeft + "px";
1087
+ }
1088
+ this.resSpacerDiv = this.createResolutionBlock();
1089
+ this.resSpacerDiv.classList.add(TimelineElement_1.STYLE_SPACER);
1090
+ this.resSpacerDiv.style.width = spaceLeft + "px";
1091
+ this.resSpacerDiv.innerText = " ";
1092
+ this.resolutionDiv.appendChild(this.resSpacerDiv);
1093
+ }
1094
+ else {
1095
+ this.hideSpacerBlocks();
1096
+ }
1097
+ }
1098
+ hideSpacerBlocks() {
1099
+ for (const e of this.spacerBlocks) {
1100
+ e.style.display = "none";
1101
+ }
1102
+ }
1103
+ /**
1104
+ * Set minimum width (pixels) of this widget's root DIV element. Default is
1105
+ * -1. Notice that
1106
+ * {@link #update(Resolution, long, long, int, int, LocaleDataProvider)}
1107
+ * will calculate min-width and call this internally.
1108
+ *
1109
+ * @param minWidth
1110
+ * Minimum width in pixels.
1111
+ */
1112
+ setMinWidth(minWidth) {
1113
+ this.minWidth = minWidth;
1114
+ this.style.minWidth = this.minWidth + "px";
1115
+ this.resolutionDiv.style.minWidth = this.minWidth + "px";
1116
+ }
1117
+ /**
1118
+ * Returns true if the timeline is overflowing the parent's width. This
1119
+ * works only when this widget is attached to some parent.
1120
+ *
1121
+ * @return True when timeline width is more than the parent's width (@see
1122
+ * {@link Element#getClientWidth()}).
1123
+ */
1124
+ isTimelineOverflowingHorizontally() {
1125
+ return this.timelineOverflowingHorizontally;
1126
+ }
1127
+ /**
1128
+ * Update horizontal overflow state.
1129
+ */
1130
+ updateTimelineOverflowingHorizontally() {
1131
+ this.timelineOverflowingHorizontally = (ElementUtil.getWidth(this.resolutionDiv) > ElementUtil.getWidth(this.getParentElement(this)));
1132
+ }
1133
+ createTimelineElementsOnVisibleArea() {
1134
+ // create place holder elements that represents weeks/days/hours
1135
+ // depending on the resolution in the timeline.
1136
+ // Only visible blocks are created, and only once, content will change
1137
+ // on scroll.
1138
+ // first: detect how many blocks we can fit in the screen
1139
+ let blocks = this.resolutionBlockCount;
1140
+ // Month and Year resolutions have variable width blocks and only a few of
1141
+ // them even for long ranges, so all of them are created.
1142
+ if (this.isTimelineOverflowingHorizontally() && !this.isMonthOrYearResolution()) {
1143
+ blocks = Math.floor((ElementUtil.getWidth(this.getParentElement(this))
1144
+ / this.calculateMinimumResolutionBlockWidth()));
1145
+ if (this.resolutionBlockCount < blocks) {
1146
+ // blocks need to be scaled up to fit the screen
1147
+ blocks = this.resolutionBlockCount;
1148
+ }
1149
+ else {
1150
+ blocks += 2;
1151
+ }
1152
+ }
1153
+ let element = null;
1154
+ for (let i = 0; i < blocks; i++) {
1155
+ switch (this.resolution) {
1156
+ case Resolution.Hour:
1157
+ element = this.createHourResolutionBlock();
1158
+ break;
1159
+ case Resolution.Day:
1160
+ element = this.createDayResolutionBlock();
1161
+ break;
1162
+ case Resolution.Week:
1163
+ element = this.createWeekResolutionBlock();
1164
+ break;
1165
+ case Resolution.Month:
1166
+ element = this.createMonthResolutionBlock();
1167
+ break;
1168
+ case Resolution.Year:
1169
+ element = this.createYearResolutionBlock();
1170
+ break;
1171
+ }
1172
+ this.resolutionDiv.appendChild(element);
1173
+ }
1174
+ this.log(`TimelineElement Added ${blocks} visible timeline elements for resolution ${Resolution[this.resolution]}`);
1175
+ }
1176
+ calculateMinimumResolutionBlockWidth() {
1177
+ if (this.resolution === Resolution.Week) {
1178
+ return DateTimeConstants.DAYS_IN_WEEK * this.minResolutionWidth;
1179
+ }
1180
+ else if (this.resolution === Resolution.Month) {
1181
+ return TimelineElement_1.AVERAGE_DAYS_IN_MONTH * this.minResolutionWidth;
1182
+ }
1183
+ else if (this.resolution === Resolution.Year) {
1184
+ return TimelineElement_1.AVERAGE_DAYS_IN_YEAR * this.minResolutionWidth;
1185
+ }
1186
+ return this.minResolutionWidth;
1187
+ }
1188
+ createResolutionBlock() {
1189
+ const resBlock = document.createElement('div');
1190
+ resBlock.classList.add("col");
1191
+ return resBlock;
1192
+ }
1193
+ createHourResolutionBlock() {
1194
+ const resBlock = this.createResolutionBlock();
1195
+ resBlock.classList.add("h", TimelineElement_1.STYLE_CENTER);
1196
+ return resBlock;
1197
+ }
1198
+ createDayResolutionBlock() {
1199
+ const resBlock = this.createResolutionBlock();
1200
+ resBlock.classList.add(TimelineElement_1.STYLE_CENTER);
1201
+ return resBlock;
1202
+ }
1203
+ createWeekResolutionBlock() {
1204
+ const resBlock = this.createResolutionBlock();
1205
+ resBlock.classList.add("w", TimelineElement_1.STYLE_CENTER);
1206
+ return resBlock;
1207
+ }
1208
+ createMonthResolutionBlock() {
1209
+ // width is set explicitly for each block, see updateMonthOrYearResolutionBlockWidths()
1210
+ const resBlock = this.createResolutionBlock();
1211
+ resBlock.classList.add(TimelineElement_1.STYLE_MONTH_RES);
1212
+ return resBlock;
1213
+ }
1214
+ createYearResolutionBlock() {
1215
+ // width is set explicitly for each block, see updateMonthOrYearResolutionBlockWidths()
1216
+ const resBlock = this.createResolutionBlock();
1217
+ resBlock.classList.add(TimelineElement_1.STYLE_YEAR_RES);
1218
+ return resBlock;
1219
+ }
1220
+ fillVisibleTimeline() {
1221
+ if (this.isMonthOrYearResolution()) {
1222
+ // all month/year blocks exist and are positioned statically; scrolling needs no re-positioning.
1223
+ this.showAllResolutionBlocks();
1224
+ }
1225
+ else if (this.isTimelineOverflowingHorizontally()) {
1226
+ this.showResolutionBlocksOnView();
1227
+ }
1228
+ else {
1229
+ this.showAllResolutionBlocks();
1230
+ }
1231
+ }
1232
+ showResolutionBlocksOnView() {
1233
+ const positionLeftSnapshot = this.positionLeft;
1234
+ let datePos = positionLeftSnapshot;
1235
+ this.firstWeekBlockHidden = false;
1236
+ let left = Math.floor(positionLeftSnapshot);
1237
+ if (positionLeftSnapshot > 0 && this.resBlockWidthPx > 0) {
1238
+ const firstResBlockShort = this.isFirstResBlockShort();
1239
+ const overflow = this.getScrollOverflowForResolutionBlock(positionLeftSnapshot, left, firstResBlockShort);
1240
+ left = Math.floor(positionLeftSnapshot - overflow);
1241
+ datePos = this.adjustLeftPositionForDateDetection(left);
1242
+ }
1243
+ if (datePos < 0.0) {
1244
+ datePos = positionLeftSnapshot;
1245
+ }
1246
+ const noticeDst = this.resolution === Resolution.Hour;
1247
+ const leftDate = this.getDateForLeftPositionNoticeDST(datePos, noticeDst);
1248
+ const containerWidth = ElementUtil.getWidth(this.getParentElement(this));
1249
+ this.fillTimelineForResolution(leftDate, new Date(Math.min(this.internalInclusiveEndDateTime.getTime(), this.getDateForLeftPositionNoticeDST(datePos + containerWidth, noticeDst).getTime())), left);
1250
+ this.style.setProperty("--timeline-col-position", "relative");
1251
+ this.style.setProperty("--timeline-col-left", left + "px");
1252
+ this.log(`TimelineElement Updated visible timeline elements for horizontal scroll position ${left} (plus ${datePos - left} to center-of-first-block)`);
1253
+ }
1254
+ showAllResolutionBlocks() {
1255
+ this.style.setProperty("--timeline-col-position", "relative");
1256
+ this.style.setProperty("--timeline-col-left", "0px");
1257
+ this.fillTimelineForResolution(this.internalInclusiveStartDateTime, this.internalInclusiveEndDateTime, 0);
1258
+ }
1259
+ /**
1260
+ * Re-applies 'current' style to the currently shown resolution blocks and day
1261
+ * row blocks without rebuilding the timeline. Call this when the current day
1262
+ * or hour changes.
1263
+ */
1264
+ refreshCurrentDate() {
1265
+ if (!this.localeDataProvider || !this.resolutionDiv || !this.internalInclusiveStartDateTime
1266
+ || !this.internalInclusiveEndDateTime || this.resolutionDiv.childElementCount === 0) {
1267
+ return;
1268
+ }
1269
+ this.fillVisibleTimeline();
1270
+ }
1271
+ updateCurrentDayString() {
1272
+ this.currentDayString = (this.currentDateVisible && this.localeDataProvider)
1273
+ ? this.localeDataProvider.formatDate(new Date(), TimelineElement_1.CURRENT_DAY_FORMAT)
1274
+ : null;
1275
+ this.currentHourString = (this.currentDateVisible && this.localeDataProvider)
1276
+ ? this.localeDataProvider.formatDate(new Date(), TimelineElement_1.CURRENT_HOUR_FORMAT)
1277
+ : null;
1278
+ }
1279
+ isCurrentDay(date) {
1280
+ return this.currentDayString != null && date != null
1281
+ && this.localeDataProvider.formatDate(date, TimelineElement_1.CURRENT_DAY_FORMAT) === this.currentDayString;
1282
+ }
1283
+ isCurrentHour(date) {
1284
+ // Compare the hour bucket in the configured time zone, so the result does
1285
+ // not depend on where inside the hour the given date falls.
1286
+ return this.currentHourString != null && date != null
1287
+ && this.localeDataProvider.formatDate(date, TimelineElement_1.CURRENT_HOUR_FORMAT) === this.currentHourString;
1288
+ }
1289
+ setCurrentStyle(element, current) {
1290
+ if (current) {
1291
+ element.classList.add(TimelineElement_1.STYLE_CURRENT);
1292
+ }
1293
+ else {
1294
+ element.classList.remove(TimelineElement_1.STYLE_CURRENT);
1295
+ }
1296
+ }
1297
+ updateDayRowCurrentStyles() {
1298
+ if (!this.isDayRowVisible()) {
1299
+ return;
1300
+ }
1301
+ for (const entry of this.dayRowData.getBlockEntries()) {
1302
+ const block = entry[1];
1303
+ this.setCurrentStyle(block, this.currentDayString != null && block.dataset.day === this.currentDayString);
1304
+ }
1305
+ }
1306
+ fillTimelineForResolution(startDate, endDate, left) {
1307
+ this.updateCurrentDayString();
1308
+ this.updateDayRowCurrentStyles();
1309
+ if (this.isMonthOrYearResolution()) {
1310
+ this.fillTimelineForMonthOrYearResolution();
1311
+ }
1312
+ else if (this.resolution === Resolution.Day || this.resolution === Resolution.Week) {
1313
+ this.fillTimelineForDayResolution(startDate, endDate, left);
1314
+ }
1315
+ else if (this.resolution == Resolution.Hour) {
1316
+ this.fillTimelineForHourResolution(startDate, endDate, left);
1317
+ }
1318
+ else {
1319
+ console.warn("TimelineElement resolution " + (this.resolution != null ? Resolution[this.resolution] : "null")
1320
+ + " is not supported");
1321
+ return;
1322
+ }
1323
+ this.log("TimelineElement Filled new data and styles to visible timeline elements");
1324
+ }
1325
+ isFirstResBlockShort() {
1326
+ return this.firstResBlockCount > 0 && ((this.resolution === Resolution.Week && this.firstResBlockCount < DateTimeConstants.DAYS_IN_WEEK));
1327
+ }
1328
+ isLastResBlockShort() {
1329
+ return this.lastResBlockCount > 0 && ((this.resolution === Resolution.Week && this.lastResBlockCount < DateTimeConstants.DAYS_IN_WEEK));
1330
+ }
1331
+ getScrollOverflowForResolutionBlock(positionLeftSnapshot, left, firstResBlockShort) {
1332
+ let overflow;
1333
+ if (firstResBlockShort && left <= this.getFirstResolutionElementWidth()) {
1334
+ overflow = this.getScrollOverflowForShortFirstResolutionBlock(positionLeftSnapshot);
1335
+ }
1336
+ else {
1337
+ overflow = this.getScrollOverflowForRegularResoultionBlock(positionLeftSnapshot, firstResBlockShort);
1338
+ }
1339
+ return overflow;
1340
+ }
1341
+ getScrollOverflowForRegularResoultionBlock(positionLeftSnapshot, firstResBlockShort) {
1342
+ let overflow;
1343
+ const firstBlockWidth = this.getFirstResolutionElementWidth();
1344
+ const positionLeft = (positionLeftSnapshot - (firstResBlockShort ? firstBlockWidth : 0));
1345
+ overflow = positionLeft % this.resBlockWidthPx;
1346
+ if (firstResBlockShort) {
1347
+ overflow += firstBlockWidth;
1348
+ this.firstWeekBlockHidden = true;
1349
+ }
1350
+ return overflow;
1351
+ }
1352
+ getScrollOverflowForShortFirstResolutionBlock(positionLeftSnapshot) {
1353
+ let overflow;
1354
+ // need to notice a short resolution block due to timeline's
1355
+ // start date which is in middle of a week.
1356
+ overflow = positionLeftSnapshot % this.getFirstResolutionElementWidth();
1357
+ if (overflow == 0.0) {
1358
+ overflow = this.getFirstResolutionElementWidth();
1359
+ }
1360
+ return overflow;
1361
+ }
1362
+ /**
1363
+ * Returns a width of the first resolution block.
1364
+ *
1365
+ * @return
1366
+ */
1367
+ getFirstResolutionElementWidth() {
1368
+ if (this.isMonthOrYearResolution()) {
1369
+ if (this.isTimelineOverflowingHorizontally()) {
1370
+ return this.firstResBlockCount * this.minResolutionWidth;
1371
+ }
1372
+ else {
1373
+ return ElementUtil.getWidth(this.getFirstResolutionElement());
1374
+ }
1375
+ }
1376
+ if (this.isFirstResBlockShort()) {
1377
+ if (this.isTimelineOverflowingHorizontally()) {
1378
+ return this.firstResBlockCount * this.minResolutionWidth;
1379
+ }
1380
+ else {
1381
+ return ElementUtil.getWidth(this.getFirstResolutionElement());
1382
+ }
1383
+ }
1384
+ else {
1385
+ if (this.isTimelineOverflowingHorizontally()) {
1386
+ return this.resBlockMinWidthPx;
1387
+ }
1388
+ else {
1389
+ return ElementUtil.getWidth(this.getFirstResolutionElement());
1390
+ }
1391
+ }
1392
+ }
1393
+ getFirstResolutionElement() {
1394
+ if (this.resolutionDiv.hasChildNodes()) {
1395
+ return this.resolutionDiv.firstElementChild;
1396
+ }
1397
+ return null;
1398
+ }
1399
+ getLastResolutionElement() {
1400
+ const div = this.resolutionDiv;
1401
+ if (!div) {
1402
+ return null;
1403
+ }
1404
+ const nodeList = div.childNodes;
1405
+ if (!nodeList) {
1406
+ return null;
1407
+ }
1408
+ const blockCount = nodeList.length;
1409
+ if (blockCount < 1) {
1410
+ return null;
1411
+ }
1412
+ if (this.containsResBlockSpacer()) {
1413
+ const index = blockCount - 2;
1414
+ if (blockCount > 1 && index >= 0) {
1415
+ return this.resolutionDiv.childNodes.item(index);
1416
+ }
1417
+ return null;
1418
+ }
1419
+ return this.resolutionDiv.lastChild;
1420
+ }
1421
+ containsResBlockSpacer() {
1422
+ return this.resSpacerDiv != null && this.resSpacerDiv.parentElement
1423
+ && this.resSpacerDiv.parentElement === this.resolutionDiv;
1424
+ }
1425
+ removeResolutionSpacerBlock() {
1426
+ if (this.containsResBlockSpacer()) {
1427
+ this.resSpacerDiv.parentNode.removeChild(this.resSpacerDiv);
1428
+ }
1429
+ }
1430
+ /*
1431
+ * Calculates either day or hour resolution block width depending on the
1432
+ * current resolution.
1433
+ */
1434
+ calculateDayOrHourResolutionBlockWidthPx(blockCount) {
1435
+ let dayOrHourWidthPx = Math.round(this.resolutionDiv.clientWidth / blockCount);
1436
+ while ((blockCount * dayOrHourWidthPx) < this.resolutionDiv.clientWidth) {
1437
+ dayOrHourWidthPx++;
1438
+ }
1439
+ return dayOrHourWidthPx;
1440
+ }
1441
+ /*
1442
+ * Calculates the actual width of one resolution block element. For example:
1443
+ * week resolution will return 7 * dayOrHourBlockWidthPx.
1444
+ */
1445
+ calculateActualResolutionBlockWidthPx(dayOrHourBlockWidthPx) {
1446
+ if (this.resolution === Resolution.Week) {
1447
+ return DateTimeConstants.DAYS_IN_WEEK * dayOrHourBlockWidthPx;
1448
+ }
1449
+ else if (this.resolution === Resolution.Month) {
1450
+ // average width; actual month block widths vary by number of days.
1451
+ return TimelineElement_1.AVERAGE_DAYS_IN_MONTH * dayOrHourBlockWidthPx;
1452
+ }
1453
+ else if (this.resolution === Resolution.Year) {
1454
+ // average width; actual year block widths vary by number of days.
1455
+ return TimelineElement_1.AVERAGE_DAYS_IN_YEAR * dayOrHourBlockWidthPx;
1456
+ }
1457
+ return dayOrHourBlockWidthPx;
1458
+ }
1459
+ /**
1460
+ * Adjust left position for optimal position to detect accurate date with
1461
+ * the current resolution.
1462
+ */
1463
+ adjustLeftPositionForDateDetection(left) {
1464
+ let datePos;
1465
+ if (this.resolution === Resolution.Week) {
1466
+ // detect date from the center of the first day block inside the
1467
+ // week block.
1468
+ datePos = left + this.dayOrHourWidthPx / 2;
1469
+ }
1470
+ else {
1471
+ // detect date from the center of the block (day/hour)
1472
+ datePos = left + this.resBlockWidthPx / 2;
1473
+ }
1474
+ return datePos;
1475
+ }
1476
+ createCalcCssValue(v, multiplier) {
1477
+ if (this.ie) {
1478
+ // see comments in createCalcCssValue(int, Integer)
1479
+ const percents = 100.0 / v * multiplier;
1480
+ return "calc(" + percents + "%)";
1481
+ }
1482
+ return null;
1483
+ }
1484
+ updateResolutionBlockWidths(pct) {
1485
+ if (this.setPositionForEachBlock) {
1486
+ if (!this.isTimelineOverflowingHorizontally()) {
1487
+ this.resolutionDiv.style.display = "flex";
1488
+ }
1489
+ else {
1490
+ this.resolutionDiv.style.removeProperty("display");
1491
+ }
1492
+ const firstResBlockIsShort = this.isFirstResBlockShort();
1493
+ const lastResBlockIsShort = this.isLastResBlockShort();
1494
+ // when setPositionForEachBlock is true, set width for each block explicitly.
1495
+ let count = this.resolutionDiv.childElementCount;
1496
+ if (this.containsResBlockSpacer()) {
1497
+ count--;
1498
+ }
1499
+ const lastIndex = count - 1;
1500
+ let i;
1501
+ let resBlock;
1502
+ for (i = 0; i < count; i++) {
1503
+ resBlock = this.resolutionDiv.childNodes.item(i);
1504
+ // first and last week blocks may be thinner than other
1505
+ // resolution blocks.
1506
+ if (firstResBlockIsShort && i == 0) {
1507
+ this.setWidth(resBlock, this.firstResBlockCount);
1508
+ }
1509
+ else if (lastResBlockIsShort && i == lastIndex) {
1510
+ this.setWidth(resBlock, this.lastResBlockCount);
1511
+ }
1512
+ else {
1513
+ this.setWidthPct(this.resBlockWidthPx, pct, resBlock);
1514
+ }
1515
+ }
1516
+ }
1517
+ else {
1518
+ // set widths by updating injected styles in one place. Faster than
1519
+ // setting widths explicitly for each element.
1520
+ const center = this.getWidthStyleValue(pct);
1521
+ let first = center;
1522
+ let last = center;
1523
+ if (this.isFirstResBlockShort()) {
1524
+ first = this.getWidth(this.firstResBlockCount);
1525
+ }
1526
+ if (this.isLastResBlockShort()) {
1527
+ last = this.getWidth(this.lastResBlockCount);
1528
+ }
1529
+ this.style.setProperty("--timeline-col-center-width", center);
1530
+ this.style.setProperty("--timeline-col-first-width", first);
1531
+ this.style.setProperty("--timeline-col-last-width", last);
1532
+ }
1533
+ }
1534
+ /*
1535
+ * Sets the width explicitly for each Month/Year resolution block based on
1536
+ * the number of days in the block. Blocks are kept vertically in-line with
1537
+ * the day based year and month rows.
1538
+ */
1539
+ updateMonthOrYearResolutionBlockWidths() {
1540
+ this.resolutionDiv.style.removeProperty("display");
1541
+ const count = Math.min(this.resolutionDiv.childElementCount, this.resBlockDayCounts.length);
1542
+ for (let i = 0; i < count; i++) {
1543
+ const resBlock = this.resolutionDiv.children.item(i);
1544
+ if (resBlock === this.resSpacerDiv) {
1545
+ continue;
1546
+ }
1547
+ this.setWidth(resBlock, this.resBlockDayCounts[i]);
1548
+ }
1549
+ }
1550
+ getWidth(multiplier) {
1551
+ if (this.isTimelineOverflowingHorizontally()) {
1552
+ return (multiplier * this.minResolutionWidth) + "px";
1553
+ }
1554
+ else {
1555
+ if (this.isAlwaysCalculatePixelWidths()) {
1556
+ return multiplier * this.dayOrHourWidthPx + "px";
1557
+ }
1558
+ else {
1559
+ return this.getCssPercentageWidth(this.blocksInRange, this.dayWidthPercentage, multiplier);
1560
+ }
1561
+ }
1562
+ }
1563
+ setWidth(element, multiplier) {
1564
+ if (this.isTimelineOverflowingHorizontally()) {
1565
+ element.style.width = (multiplier * this.minResolutionWidth) + "px";
1566
+ }
1567
+ else {
1568
+ if (this.isAlwaysCalculatePixelWidths()) {
1569
+ element.style.width = (multiplier * this.dayOrHourWidthPx) + "px";
1570
+ }
1571
+ else {
1572
+ this.setCssPercentageWidth(element, this.blocksInRange, this.dayWidthPercentage, multiplier);
1573
+ }
1574
+ }
1575
+ }
1576
+ setWidthPct(resBlockWidthPx, pct, element) {
1577
+ if (this.isTimelineOverflowingHorizontally()) {
1578
+ element.style.width = this.resBlockMinWidthPx + "px";
1579
+ }
1580
+ else {
1581
+ if (this.isAlwaysCalculatePixelWidths()) {
1582
+ element.style.width = resBlockWidthPx + "px";
1583
+ }
1584
+ else {
1585
+ if (this.ie) {
1586
+ element.style.flex = "1";
1587
+ }
1588
+ this.setCssPercentageWidthFor(element, this.resBlockWidthPercentage, pct);
1589
+ }
1590
+ }
1591
+ }
1592
+ setCssPercentageWidth(element, daysInRange, width, position) {
1593
+ const pct = this.createCalcCssValue(daysInRange, position);
1594
+ this.setCssPercentageWidthFor(element, position * width, pct);
1595
+ }
1596
+ getCssPercentageWidth(daysInRange, width, position) {
1597
+ const pct = this.createCalcCssValue(daysInRange, position);
1598
+ return this.getPercentageWidthString(position * width, pct);
1599
+ }
1600
+ setCssPercentageWidthFor(element, nValue, pct) {
1601
+ if (pct) {
1602
+ element.style.width = pct;
1603
+ }
1604
+ else {
1605
+ element.style.width = nValue + "%";
1606
+ }
1607
+ }
1608
+ getPercentageWidthString(nValue, pct) {
1609
+ if (pct) {
1610
+ return pct;
1611
+ }
1612
+ else {
1613
+ return nValue + "%";
1614
+ }
1615
+ }
1616
+ getWidthStyleValue(pct) {
1617
+ if (this.isTimelineOverflowingHorizontally()) {
1618
+ return this.resBlockMinWidthPx + "px";
1619
+ }
1620
+ else {
1621
+ if (this.isAlwaysCalculatePixelWidths()) {
1622
+ return this.resBlockWidthPx + "px";
1623
+ }
1624
+ else {
1625
+ return this.getPercentageWidthString(this.resBlockWidthPercentage, pct);
1626
+ }
1627
+ }
1628
+ }
1629
+ /*
1630
+ * Fills captions and styles for all Month/Year resolution blocks. Uses block
1631
+ * data collected in prepareTimelineForDayOrWeekResolution().
1632
+ */
1633
+ fillTimelineForMonthOrYearResolution() {
1634
+ const childCount = this.resolutionDiv.childElementCount;
1635
+ // currentDayString is 'yyyy-MM-dd'; take 'yyyy-MM' or 'yyyy' part of it.
1636
+ let currentKey = null;
1637
+ if (this.currentDayString != null) {
1638
+ currentKey = this.currentDayString.substring(0, this.resolution === Resolution.Year ? 4 : 7);
1639
+ }
1640
+ for (let i = 0; i < this.resBlockStartDates.length; i++) {
1641
+ if (!this.isValidChildIndex(i, childCount)) {
1642
+ this.logIndexOutOfBounds(this.resolution === Resolution.Year ? "year" : "month", i, childCount);
1643
+ return;
1644
+ }
1645
+ const resBlock = this.resolutionDiv.children.item(i);
1646
+ const date = this.resBlockStartDates[i];
1647
+ this.fillMonthOrYearResolutionBlock(resBlock, date, i % 2 === 1, currentKey != null && this.resBlockKeys[i] === currentKey);
1648
+ }
1649
+ }
1650
+ fillMonthOrYearResolutionBlock(resBlock, date, even, current) {
1651
+ if (this.resolution === Resolution.Year) {
1652
+ resBlock.innerText = this.formatYearCaption(this.getYear(date), date);
1653
+ }
1654
+ else {
1655
+ resBlock.innerText = this.formatMonthCaption(this.getMonth(date), date);
1656
+ }
1657
+ if (even) {
1658
+ resBlock.classList.add(TimelineElement_1.STYLE_EVEN);
1659
+ }
1660
+ else {
1661
+ resBlock.classList.remove(TimelineElement_1.STYLE_EVEN);
1662
+ }
1663
+ this.setCurrentStyle(resBlock, current);
1664
+ }
1665
+ fillTimelineForHourResolution(startDate, endDate, left) {
1666
+ const timeline = this;
1667
+ this.firstDay = true;
1668
+ let hourCounter;
1669
+ let even;
1670
+ this.fillTimelineForHour(DateTimeConstants.HOUR_INTERVAL, startDate, endDate, {
1671
+ setup() {
1672
+ hourCounter = this.getFirstHourOfVisibleRange(startDate);
1673
+ even = this.isEven(startDate);
1674
+ },
1675
+ fillResolutionBlock(index, date, currentYear, lastTimelineBlock) {
1676
+ const childCount = timeline.resolutionDiv.childElementCount;
1677
+ if (timeline.isValidChildIndex(index, childCount)) {
1678
+ const resBlock = timeline.resolutionDiv.childNodes.item(index);
1679
+ timeline.fillHourResolutionBlock(resBlock, date, index, hourCounter, lastTimelineBlock, left, even);
1680
+ hourCounter = (hourCounter + 1) % 24;
1681
+ even = !even;
1682
+ }
1683
+ else {
1684
+ timeline.logIndexOutOfBounds("hour", index, childCount);
1685
+ return;
1686
+ }
1687
+ },
1688
+ isEven(startDate) {
1689
+ const normalDate = timeline.toNormalDate(startDate);
1690
+ if (timeline.normalStartDate.getTime() < normalDate.getTime()) {
1691
+ const hours = Math.floor(((normalDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.HOUR_INTERVAL));
1692
+ return (hours % 2) == 1;
1693
+ }
1694
+ return false;
1695
+ },
1696
+ getFirstHourOfVisibleRange(startDate) {
1697
+ const normalDate = timeline.toNormalDate(startDate);
1698
+ if (timeline.normalStartDate.getTime() < normalDate.getTime()) {
1699
+ const hours = Math.floor(((normalDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.HOUR_INTERVAL));
1700
+ return ((timeline.firstHourOfRange + hours) % 24);
1701
+ }
1702
+ return timeline.firstHourOfRange;
1703
+ }
1704
+ });
1705
+ }
1706
+ fillTimelineForDayResolution(startDate, endDate, left) {
1707
+ const timeline = this;
1708
+ let dayCounter;
1709
+ let even;
1710
+ let firstWeek = true;
1711
+ let weekIndex = 0;
1712
+ let weekday;
1713
+ this.fillTimelineForDayOrWeek(DateTimeConstants.DAY_INTERVAL, startDate, endDate, {
1714
+ setup: function () {
1715
+ dayCounter = this.getFirstDayOfVisibleRange(startDate);
1716
+ even = this.isEven(startDate, timeline.firstDayOfRange);
1717
+ },
1718
+ fillResolutionBlock: function (index, date, currentYear, lastTimelineBlock) {
1719
+ try {
1720
+ weekday = timeline.getWeekday(dayCounter);
1721
+ if (timeline.resolution === Resolution.Week) {
1722
+ this.fillWeekBlock(left, index, date, lastTimelineBlock);
1723
+ }
1724
+ else {
1725
+ this.fillDayBlock(left, index, date);
1726
+ }
1727
+ }
1728
+ finally {
1729
+ dayCounter = Math.max((dayCounter + 1) % 8, 1);
1730
+ }
1731
+ },
1732
+ fillDayBlock: function (left, index, date) {
1733
+ const childCount = timeline.resolutionDiv.childElementCount;
1734
+ if (timeline.isValidChildIndex(index, childCount)) {
1735
+ const resBlock = timeline.resolutionDiv.childNodes.item(index);
1736
+ timeline.fillDayResolutionBlock(resBlock, date, index, timeline.isWeekEnd(dayCounter), left);
1737
+ }
1738
+ else {
1739
+ timeline.logIndexOutOfBounds("day", index, childCount);
1740
+ return;
1741
+ }
1742
+ },
1743
+ fillWeekBlock: function (left, index, date, lastTimelineBlock) {
1744
+ let resBlock = null;
1745
+ if (index > 0 && weekday == Weekday.First) {
1746
+ weekIndex++;
1747
+ firstWeek = false;
1748
+ even = !even;
1749
+ }
1750
+ if (index == 0 || weekday == Weekday.First) {
1751
+ const childCount = timeline.resolutionDiv.childElementCount;
1752
+ if (timeline.isValidChildIndex(weekIndex, childCount)) {
1753
+ resBlock = timeline.resolutionDiv.childNodes.item(weekIndex);
1754
+ }
1755
+ else {
1756
+ timeline.logIndexOutOfBounds("week", weekIndex, childCount);
1757
+ return;
1758
+ }
1759
+ }
1760
+ timeline.fillWeekResolutionBlock(resBlock, date, weekIndex, weekday, firstWeek, lastTimelineBlock, left, even);
1761
+ },
1762
+ calcDaysLeftInFirstWeek: function (startDay) {
1763
+ let daysLeftInWeek = 0;
1764
+ if (startDay != timeline.firstDayOfWeek) {
1765
+ for (let i = startDay;; i++) {
1766
+ daysLeftInWeek++;
1767
+ if (Math.max(i % 8, 1) === timeline.lastDayOfWeek) {
1768
+ break;
1769
+ }
1770
+ }
1771
+ }
1772
+ return daysLeftInWeek;
1773
+ },
1774
+ isEven: function (startDate, startDay) {
1775
+ const visibleRangeNormalStartDate = timeline.toNormalDate(startDate);
1776
+ if (timeline.normalStartDate.getTime() < visibleRangeNormalStartDate.getTime()) {
1777
+ let daysHidden = Math.floor(((visibleRangeNormalStartDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.DAY_INTERVAL));
1778
+ timeline.log("Days hidden: " + daysHidden);
1779
+ timeline.log("firstWeekBlockHidden = " + timeline.firstWeekBlockHidden);
1780
+ if (daysHidden === 0) {
1781
+ return false;
1782
+ }
1783
+ const daysLeftInFirstWeek = this.calcDaysLeftInFirstWeek(startDay);
1784
+ if (daysHidden > daysLeftInFirstWeek) {
1785
+ daysHidden -= daysLeftInFirstWeek;
1786
+ }
1787
+ const weeks = daysHidden / DateTimeConstants.DAYS_IN_WEEK;
1788
+ const even = (weeks % 2) === 1;
1789
+ return (timeline.firstWeekBlockHidden) ? !even : even;
1790
+ }
1791
+ return false;
1792
+ },
1793
+ getFirstDayOfVisibleRange: function (startDate) {
1794
+ const visibleRangeNormalStartDate = timeline.toNormalDate(startDate);
1795
+ if (timeline.normalStartDate.getTime() < visibleRangeNormalStartDate.getTime()) {
1796
+ const days = Math.floor(((visibleRangeNormalStartDate.getTime() - timeline.normalStartDate.getTime()) / DateTimeConstants.DAY_INTERVAL));
1797
+ return ((timeline.firstDayOfRange - 1 + days) % 7) + 1;
1798
+ }
1799
+ return timeline.firstDayOfRange;
1800
+ }
1801
+ });
1802
+ }
1803
+ logIndexOutOfBounds(indexName, index, childCount) {
1804
+ this.log(`${indexName} index ${index} out of bounds with childCount ${childCount}. Can't fill content.`);
1805
+ }
1806
+ fillTimelineForHour(interval, startDate, endDate, resBlockFiller) {
1807
+ const currentYear = null;
1808
+ let pos = startDate;
1809
+ const end = endDate;
1810
+ let index = 0;
1811
+ let lastTimelineBlock;
1812
+ let date;
1813
+ resBlockFiller.setup();
1814
+ while (pos <= end) {
1815
+ date = pos;
1816
+ const nextHour = new Date(pos.getTime() + interval);
1817
+ lastTimelineBlock = nextHour.getTime() > end.getTime();
1818
+ resBlockFiller.fillResolutionBlock(index, date, currentYear, lastTimelineBlock);
1819
+ pos = nextHour;
1820
+ index++;
1821
+ }
1822
+ }
1823
+ fillTimelineForDayOrWeek(interval, startDate, endDate, resBlockFiller) {
1824
+ const currentYear = null;
1825
+ const timeZone = this.localeDataProvider.getTimeZone();
1826
+ // Same calendar-day stepping as prepareTimelineForDayOrWeek().
1827
+ let date = DateUtil.adjustToMiddleOfDay(startDate, timeZone);
1828
+ const end = endDate;
1829
+ let index = 0;
1830
+ let lastTimelineBlock = false;
1831
+ resBlockFiller.setup();
1832
+ while (!lastTimelineBlock) {
1833
+ const next = addDaysInZone(date, 1, timeZone);
1834
+ lastTimelineBlock = next.getTime() > end.getTime();
1835
+ resBlockFiller.fillResolutionBlock(index, date, currentYear, lastTimelineBlock);
1836
+ date = next;
1837
+ index++;
1838
+ }
1839
+ }
1840
+ isValidChildIndex(index, childCount) {
1841
+ return (index >= 0) && (index < childCount);
1842
+ }
1843
+ fillDayResolutionBlock(resBlock, date, index, weekend, left) {
1844
+ resBlock.innerText = this.localeDataProvider.formatDate(date, "d");
1845
+ if (weekend) {
1846
+ resBlock.classList.add(TimelineElement_1.STYLE_WEEKEND);
1847
+ }
1848
+ else {
1849
+ resBlock.classList.remove(TimelineElement_1.STYLE_WEEKEND);
1850
+ }
1851
+ this.setCurrentStyle(resBlock, this.isCurrentDay(date));
1852
+ if (this.setPositionForEachBlock && this.isTimelineOverflowingHorizontally()) {
1853
+ resBlock.style.position = "relative";
1854
+ resBlock.style.left = left + "px";
1855
+ }
1856
+ }
1857
+ fillWeekResolutionBlock(resBlock, date, index, weekDay, firstWeek, lastBlock, left, even) {
1858
+ if (resBlock != null) {
1859
+ resBlock.innerText = this.formatWeekCaption(date);
1860
+ if (even) {
1861
+ resBlock.classList.add(TimelineElement_1.STYLE_EVEN);
1862
+ }
1863
+ else {
1864
+ resBlock.classList.remove(TimelineElement_1.STYLE_EVEN);
1865
+ }
1866
+ if (this.setPositionForEachBlock && this.isTimelineOverflowingHorizontally()) {
1867
+ resBlock.style.position = "relative";
1868
+ resBlock.style.left = left + "px";
1869
+ }
1870
+ resBlock.classList.remove(TimelineElement_1.STYLE_FIRST, TimelineElement_1.STYLE_LAST);
1871
+ // block is (re)filled starting from its first visible day; clear old state.
1872
+ this.setCurrentStyle(resBlock, false);
1873
+ }
1874
+ if (this.isCurrentDay(date) && this.isValidChildIndex(index, this.resolutionDiv.childElementCount)) {
1875
+ this.setCurrentStyle(this.resolutionDiv.childNodes.item(index), true);
1876
+ }
1877
+ if (firstWeek && (weekDay === Weekday.Last || lastBlock)) {
1878
+ const firstEl = this.resolutionDiv.firstElementChild;
1879
+ if (!firstEl.classList.contains(TimelineElement_1.STYLE_FIRST)) {
1880
+ firstEl.classList.add(TimelineElement_1.STYLE_FIRST);
1881
+ }
1882
+ }
1883
+ else if (lastBlock) {
1884
+ const lastEl = this.resolutionDiv.lastChild;
1885
+ if (!lastEl.classList.contains(TimelineElement_1.STYLE_LAST)) {
1886
+ lastEl.classList.add(TimelineElement_1.STYLE_LAST);
1887
+ }
1888
+ }
1889
+ }
1890
+ fillHourResolutionBlock(resBlock, date, index, hourCounter, lastBlock, left, even) {
1891
+ if (this.localeDataProvider.isTwelveHourClock()) {
1892
+ resBlock.innerText = this.localeDataProvider.formatTime(date, "h");
1893
+ }
1894
+ else {
1895
+ resBlock.innerText = this.localeDataProvider.formatTime(date, "HH");
1896
+ }
1897
+ if (even) {
1898
+ resBlock.classList.add(TimelineElement_1.STYLE_EVEN);
1899
+ }
1900
+ else {
1901
+ resBlock.classList.remove(TimelineElement_1.STYLE_EVEN);
1902
+ }
1903
+ this.setCurrentStyle(resBlock, this.isCurrentHour(date));
1904
+ if (this.firstDay && (hourCounter == 24 || lastBlock)) {
1905
+ this.firstDay = false;
1906
+ this.firstResBlockCount = index + 1;
1907
+ }
1908
+ else if (lastBlock) {
1909
+ this.lastResBlockCount = (index + 1 - this.firstResBlockCount) % 24;
1910
+ }
1911
+ if (this.setPositionForEachBlock && this.isTimelineOverflowingHorizontally()) {
1912
+ resBlock.style.position = "relative";
1913
+ resBlock.style.left = left + "px";
1914
+ }
1915
+ }
1916
+ };
1917
+ TimelineElement.STYLE_ROW = "row";
1918
+ TimelineElement.STYLE_COL = "col";
1919
+ TimelineElement.STYLE_MONTH = "month";
1920
+ TimelineElement.STYLE_YEAR = "year";
1921
+ TimelineElement.STYLE_DAY = "day";
1922
+ TimelineElement.STYLE_WEEK = "w";
1923
+ TimelineElement.STYLE_MONTH_RES = "m";
1924
+ TimelineElement.STYLE_YEAR_RES = "y";
1925
+ TimelineElement.STYLE_RESOLUTION = "resolution";
1926
+ TimelineElement.STYLE_EVEN = "even";
1927
+ TimelineElement.STYLE_WEEKEND = "weekend";
1928
+ TimelineElement.STYLE_SPACER = "spacer";
1929
+ TimelineElement.STYLE_FIRST = "f-col";
1930
+ TimelineElement.STYLE_CENTER = "c-col";
1931
+ TimelineElement.STYLE_LAST = "l-col";
1932
+ TimelineElement.STYLE_MEASURE = "measure";
1933
+ TimelineElement.STYLE_CURRENT = "current";
1934
+ TimelineElement.CURRENT_DAY_FORMAT = "yyyy-MM-dd";
1935
+ // Offset is included to tell apart the repeated hour when daylight saving time ends.
1936
+ TimelineElement.CURRENT_HOUR_FORMAT = "yyyy-MM-dd HH xxx";
1937
+ /* Average day counts used to convert a measured month/year block width to a day width. */
1938
+ TimelineElement.AVERAGE_DAYS_IN_MONTH = 30;
1939
+ TimelineElement.AVERAGE_DAYS_IN_YEAR = 365;
1940
+ __decorate([
1941
+ property({
1942
+ reflect: true,
1943
+ converter: {
1944
+ // Attribute holds the enum name, e.g. "Week".
1945
+ fromAttribute: (value) => Resolution[value],
1946
+ toAttribute: (value) => Resolution[value],
1947
+ }
1948
+ })
1949
+ ], TimelineElement.prototype, "resolution", void 0);
1950
+ __decorate([
1951
+ property({ reflect: true, converter: { fromAttribute: toHourAccuracy } })
1952
+ ], TimelineElement.prototype, "startDateTime", void 0);
1953
+ __decorate([
1954
+ property({ reflect: true, converter: { fromAttribute: toHourAccuracy } })
1955
+ ], TimelineElement.prototype, "endDateTime", void 0);
1956
+ __decorate([
1957
+ property({ reflect: true })
1958
+ ], TimelineElement.prototype, "timeZone", void 0);
1959
+ __decorate([
1960
+ property({ reflect: true })
1961
+ ], TimelineElement.prototype, "locale", void 0);
1962
+ __decorate([
1963
+ property({ reflect: true, type: Number })
1964
+ ], TimelineElement.prototype, "firstDayOfWeek", void 0);
1965
+ __decorate([
1966
+ property({ reflect: true, type: Boolean })
1967
+ ], TimelineElement.prototype, "twelveHourClock", void 0);
1968
+ __decorate([
1969
+ property()
1970
+ ], TimelineElement.prototype, "minWidth", void 0);
1971
+ __decorate([
1972
+ property()
1973
+ ], TimelineElement.prototype, "normalStartDate", void 0);
1974
+ __decorate([
1975
+ property()
1976
+ ], TimelineElement.prototype, "normalEndDate", void 0);
1977
+ __decorate([
1978
+ property()
1979
+ ], TimelineElement.prototype, "lastDayOfWeek", void 0);
1980
+ __decorate([
1981
+ property()
1982
+ ], TimelineElement.prototype, "firstDayOfRange", void 0);
1983
+ __decorate([
1984
+ property()
1985
+ ], TimelineElement.prototype, "firstHourOfRange", void 0);
1986
+ __decorate([
1987
+ property({ reflect: true })
1988
+ ], TimelineElement.prototype, "scrollContainerId", void 0);
1989
+ __decorate([
1990
+ property({ converter: { fromAttribute: visibleUnlessFalse } })
1991
+ ], TimelineElement.prototype, "monthRowVisible", void 0);
1992
+ __decorate([
1993
+ property({ converter: { fromAttribute: visibleUnlessFalse } })
1994
+ ], TimelineElement.prototype, "yearRowVisible", void 0);
1995
+ __decorate([
1996
+ property()
1997
+ ], TimelineElement.prototype, "monthNames", void 0);
1998
+ __decorate([
1999
+ property()
2000
+ ], TimelineElement.prototype, "weekdayNames", void 0);
2001
+ __decorate([
2002
+ property({ reflect: true, type: Boolean })
2003
+ ], TimelineElement.prototype, "currentDateVisible", void 0);
2004
+ __decorate([
2005
+ property({ type: Boolean })
2006
+ ], TimelineElement.prototype, "debug", void 0);
2007
+ __decorate([
2008
+ query('#resolutionDiv')
2009
+ ], TimelineElement.prototype, "resolutionDiv", void 0);
2010
+ TimelineElement = TimelineElement_1 = __decorate([
2011
+ customElement('timeline-element')
2012
+ ], TimelineElement);
2013
+ export { TimelineElement };
2014
2014
  //# sourceMappingURL=timeline-element.js.map