UNPKG

7.69 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DateRangeSelectionStrategy = void 0;
19var core_1 = require("@blueprintjs/core");
20var dateUtils_1 = require("./common/dateUtils");
21/* eslint-disable-next-line @typescript-eslint/no-extraneous-class */
22var DateRangeSelectionStrategy = /** @class */ (function () {
23 function DateRangeSelectionStrategy() {
24 }
25 /**
26 * Returns the new date-range and the boundary that would be affected if `day` were clicked. The
27 * affected boundary may be different from the provided `boundary` in some cases. For example,
28 * clicking a particular boundary's selected date will always deselect it regardless of which
29 * `boundary` you provide to this function (because it's simply a more intuitive interaction).
30 */
31 DateRangeSelectionStrategy.getNextState = function (currentRange, day, allowSingleDayRange, boundary) {
32 if (boundary != null) {
33 return this.getNextStateForBoundary(currentRange, day, allowSingleDayRange, boundary);
34 }
35 else {
36 return this.getDefaultNextState(currentRange, day, allowSingleDayRange);
37 }
38 };
39 DateRangeSelectionStrategy.getNextStateForBoundary = function (currentRange, day, allowSingleDayRange, boundary) {
40 var boundaryDate = this.getBoundaryDate(boundary, currentRange);
41 var otherBoundary = this.getOtherBoundary(boundary);
42 var otherBoundaryDate = this.getBoundaryDate(otherBoundary, currentRange);
43 var nextBoundary;
44 var nextDateRange;
45 if (boundaryDate == null && otherBoundaryDate == null) {
46 nextBoundary = boundary;
47 nextDateRange = this.createRangeForBoundary(boundary, day, null);
48 }
49 else if (boundaryDate != null && otherBoundaryDate == null) {
50 var nextBoundaryDate = (0, dateUtils_1.areSameDay)(boundaryDate, day) ? null : day;
51 nextBoundary = boundary;
52 nextDateRange = this.createRangeForBoundary(boundary, nextBoundaryDate, null);
53 }
54 else if (boundaryDate == null && otherBoundaryDate != null) {
55 if ((0, dateUtils_1.areSameDay)(day, otherBoundaryDate)) {
56 var nextDate = void 0;
57 if (allowSingleDayRange) {
58 nextBoundary = boundary;
59 nextDate = otherBoundaryDate;
60 }
61 else {
62 nextBoundary = otherBoundary;
63 nextDate = null;
64 }
65 nextDateRange = this.createRangeForBoundary(boundary, nextDate, nextDate);
66 }
67 else if (this.isOverlappingOtherBoundary(boundary, day, otherBoundaryDate)) {
68 nextBoundary = otherBoundary;
69 nextDateRange = this.createRangeForBoundary(boundary, otherBoundaryDate, day);
70 }
71 else {
72 nextBoundary = boundary;
73 nextDateRange = this.createRangeForBoundary(boundary, day, otherBoundaryDate);
74 }
75 }
76 else {
77 // both boundaryDate and otherBoundaryDate are already defined
78 if ((0, dateUtils_1.areSameDay)(boundaryDate, day)) {
79 var isSingleDayRangeSelected = (0, dateUtils_1.areSameDay)(boundaryDate, otherBoundaryDate);
80 var nextOtherBoundaryDate = isSingleDayRangeSelected ? null : otherBoundaryDate;
81 nextBoundary = boundary;
82 nextDateRange = this.createRangeForBoundary(boundary, null, nextOtherBoundaryDate);
83 }
84 else if ((0, dateUtils_1.areSameDay)(day, otherBoundaryDate)) {
85 var _a = allowSingleDayRange
86 ? [otherBoundaryDate, otherBoundaryDate]
87 : [boundaryDate, null], nextBoundaryDate = _a[0], nextOtherBoundaryDate = _a[1];
88 nextBoundary = allowSingleDayRange ? boundary : otherBoundary;
89 nextDateRange = this.createRangeForBoundary(boundary, nextBoundaryDate, nextOtherBoundaryDate);
90 }
91 else if (this.isOverlappingOtherBoundary(boundary, day, otherBoundaryDate)) {
92 nextBoundary = boundary;
93 nextDateRange = this.createRangeForBoundary(boundary, day, null);
94 }
95 else {
96 // extend the date range with an earlier boundaryDate date
97 nextBoundary = boundary;
98 nextDateRange = this.createRangeForBoundary(boundary, day, otherBoundaryDate);
99 }
100 }
101 return { dateRange: nextDateRange, boundary: nextBoundary };
102 };
103 DateRangeSelectionStrategy.getDefaultNextState = function (selectedRange, day, allowSingleDayRange) {
104 var start = selectedRange[0], end = selectedRange[1];
105 var nextDateRange;
106 if (start == null && end == null) {
107 nextDateRange = [day, null];
108 }
109 else if (start != null && end == null) {
110 nextDateRange = this.createRange(day, start, allowSingleDayRange);
111 }
112 else if (start == null && end != null) {
113 nextDateRange = this.createRange(day, end, allowSingleDayRange);
114 }
115 else {
116 var isStart = (0, dateUtils_1.areSameDay)(start, day);
117 var isEnd = (0, dateUtils_1.areSameDay)(end, day);
118 if (isStart && isEnd) {
119 nextDateRange = [null, null];
120 }
121 else if (isStart) {
122 nextDateRange = [null, end];
123 }
124 else if (isEnd) {
125 nextDateRange = [start, null];
126 }
127 else {
128 nextDateRange = [day, null];
129 }
130 }
131 return { dateRange: nextDateRange };
132 };
133 DateRangeSelectionStrategy.getOtherBoundary = function (boundary) {
134 return boundary === core_1.Boundary.START ? core_1.Boundary.END : core_1.Boundary.START;
135 };
136 DateRangeSelectionStrategy.getBoundaryDate = function (boundary, dateRange) {
137 return boundary === core_1.Boundary.START ? dateRange[0] : dateRange[1];
138 };
139 DateRangeSelectionStrategy.isOverlappingOtherBoundary = function (boundary, boundaryDate, otherBoundaryDate) {
140 return boundary === core_1.Boundary.START ? boundaryDate > otherBoundaryDate : boundaryDate < otherBoundaryDate;
141 };
142 DateRangeSelectionStrategy.createRangeForBoundary = function (boundary, boundaryDate, otherBoundaryDate) {
143 return boundary === core_1.Boundary.START
144 ? [boundaryDate, otherBoundaryDate]
145 : [otherBoundaryDate, boundaryDate];
146 };
147 DateRangeSelectionStrategy.createRange = function (a, b, allowSingleDayRange) {
148 // clicking the same date again will clear it
149 if (!allowSingleDayRange && (0, dateUtils_1.areSameDay)(a, b)) {
150 return [null, null];
151 }
152 return a < b ? [a, b] : [b, a];
153 };
154 return DateRangeSelectionStrategy;
155}());
156exports.DateRangeSelectionStrategy = DateRangeSelectionStrategy;
157//# sourceMappingURL=dateRangeSelectionStrategy.js.map
\No newline at end of file