-
Notifications
You must be signed in to change notification settings - Fork 253
/
MonthPicker.js
199 lines (172 loc) · 4.94 KB
/
MonthPicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";
import { getFormatLocale } from "@ui5/webcomponents-base/dist/FormatSettings.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import Integer from "@ui5/webcomponents-base/dist/types/Integer.js";
import { isSpace, isEnter } from "@ui5/webcomponents-base/dist/events/PseudoEvents.js";
import LocaleData from "@ui5/webcomponents-utils/dist/sap/ui/core/LocaleData.js";
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
import CalendarType from "@ui5/webcomponents-base/dist/dates/CalendarType.js";
import CalendarDate from "@ui5/webcomponents-base/dist/dates/CalendarDate.js";
import MonthPickerTemplate from "./generated/templates/MonthPickerTemplate.lit.js";
// Styles
import styles from "./generated/themes/MonthPicker.css.js";
/**
* @public
*/
const metadata = {
tag: "ui5-monthpicker",
properties: /** @lends sap.ui.webcomponents.main.MonthPicker.prototype */ {
/**
* A UNIX timestamp - seconds since 00:00:00 UTC on Jan 1, 1970.
* @type {Integer}
* @public
*/
timestamp: {
type: Integer,
},
/**
* Sets a calendar type used for display.
* If not set, the calendar type of the global configuration is used.
* @type {string}
* @public
*/
primaryCalendarType: {
type: CalendarType,
},
_quarters: {
type: Object,
multiple: true,
},
_hidden: {
type: Boolean,
noAttribute: true,
},
},
events: /** @lends sap.ui.webcomponents.main.MonthPicker.prototype */ {
/**
* Fired when the user selects a new Date on the Web Component.
* @public
* @event
*/
selectedMonthChange: {},
},
};
/**
* Month picker component.
*
* @class
*
* Displays months which can be selected.
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.MonthPicker
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-monthpicker
* @public
*/
class MonthPicker extends UI5Element {
static get metadata() {
return metadata;
}
static get render() {
return litRender;
}
static get template() {
return MonthPickerTemplate;
}
static get styles() {
return styles;
}
constructor() {
super();
this._oLocale = getFormatLocale();
this._oLocaleData = new LocaleData(this._oLocale);
this._itemNav = new ItemNavigation(this, { rowSize: 3, cyclic: true });
this._itemNav.getItemsCallback = function getItemsCallback() {
return [].concat(...this._quarters);
}.bind(this);
this._itemNav.setItemsCallback = function setItemsCallback(items) {
this._quarters = items;
}.bind(this);
}
onBeforeRendering() {
const quarters = [];
const oCalDate = CalendarDate.fromTimestamp(new Date().getTime(), this._primaryCalendarType);
let timestamp;
for (let i = 0; i < 12; i++) {
oCalDate.setMonth(i);
timestamp = oCalDate.valueOf() / 1000;
const month = {
timestamp: timestamp.toString(),
id: `${this._id}-m${i}`,
name: this._oLocaleData.getMonths("wide", this._primaryCalendarType)[i],
classes: "ui5-mp-item",
};
if (this._month === i) {
month.classes += " ui5-mp-item--selected";
}
const quarterIndex = parseInt(i / 3);
if (quarters[quarterIndex]) {
quarters[quarterIndex].push(month);
} else {
quarters[quarterIndex] = [month];
}
}
this._quarters = quarters;
}
onAfterRendering() {
this._itemNav.focusCurrent();
}
get _timestamp() {
return this.timestamp !== undefined ? this.timestamp : Math.floor(new Date().getTime() / 1000);
}
get _localDate() {
return new Date(this._timestamp * 1000);
}
get _calendarDate() {
return CalendarDate.fromTimestamp(this._localDate.getTime(), this._primaryCalendarType);
}
get _month() {
return this._calendarDate.getMonth();
}
get _primaryCalendarType() {
return this.primaryCalendarType || getCalendarType() || LocaleData.getInstance(getLocale()).getPreferredCalendarType();
}
_onclick(event) {
if (event.target.className.indexOf("ui5-mp-item") > -1) {
const timestamp = this.getTimestampFromDOM(event.target);
this.timestamp = timestamp;
this._itemNav.current = this._month;
this.fireEvent("selectedMonthChange", { timestamp });
}
}
_onkeydown(event) {
if (isSpace(event) || isEnter(event)) {
this._activateMonth(event);
}
}
_activateMonth(event) {
event.preventDefault();
if (event.target.className.indexOf("ui5-mp-item") > -1) {
const timestamp = this.getTimestampFromDOM(event.target);
this.timestamp = timestamp;
this.fireEvent("selectedMonthChange", { timestamp });
}
}
getTimestampFromDOM(domNode) {
const oMonthDomRef = domNode.getAttribute("data-sap-timestamp");
return parseInt(oMonthDomRef);
}
get styles() {
return {
main: {
display: this._hidden ? "none" : "",
},
};
}
}
MonthPicker.define();
export default MonthPicker;