Skip to content

Commit 9bf1806

Browse files
committed
refactor: extract _updateMonthStyles method
1 parent 0628670 commit 9bf1806

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/main/resources/META-INF/frontend/fc-date-picker/fc-date-picker.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,32 @@ export class FcDatePicker extends DatePicker {
5252

5353
this._styles={};
5454

55+
this._overlayElement.renderer = e => {
56+
this._boundOverlayRenderer.call(this,e);
57+
58+
if (!this._overlayContent._monthScroller.__fcWrapped) {
59+
const createElement = this._overlayContent._monthScroller._createElement;
60+
this._overlayContent._monthScroller.__fcWrapped = true;
61+
this._overlayContent._monthScroller._createElement = () => {
62+
var calendar = createElement();
63+
calendar.addEventListener('dom-change',ev=>{
64+
if (ev.composedPath()[0].as=='week') {
65+
setTimeout(()=> this._updateMonthStyles(calendar));
66+
}
67+
});
68+
return calendar;
69+
}
70+
}
71+
};
72+
}
73+
74+
_updateMonthStyles(element) {
75+
5576
const _clearStyles = function() {
5677
let e = this.shadowRoot.querySelectorAll("[part~='date']");
5778
e.forEach(item => item.removeAttribute('class'));
5879
};
59-
80+
6081
const _setStyleForDay = function(i,className) {
6182
let e = this.shadowRoot.querySelectorAll("[part~='date']:not(:empty)")[i-1];
6283
if (className) {
@@ -65,7 +86,7 @@ export class FcDatePicker extends DatePicker {
6586
e.removeAttribute('class');
6687
}
6788
};
68-
89+
6990
const _getStyle = month => {
7091
const tostr = date => date.toISOString().substr(0,7);
7192
const add = (date, delta) => new Date(date.getFullYear(), date.getMonth()+delta, 1);
@@ -98,36 +119,16 @@ export class FcDatePicker extends DatePicker {
98119
}
99120
return this._styles[key];
100121
}
101-
102-
const _updateMonthStyles = function(element) {
103-
const month = element.month;
104-
_clearStyles.call(element);
105-
_getStyle(month).then(styles=>setTimeout(()=>{
106-
if (element.month===month) {
107-
for (let i in styles) {
108-
_setStyleForDay.call(element,i,styles[i]);
109-
}
110-
}
111-
}));
112-
}
113-
114-
this._overlayElement.renderer = e => {
115-
this._boundOverlayRenderer.call(this,e);
116-
117-
if (!this._overlayContent._monthScroller.__fcWrapped) {
118-
const createElement = this._overlayContent._monthScroller._createElement;
119-
this._overlayContent._monthScroller.__fcWrapped = true;
120-
this._overlayContent._monthScroller._createElement = () => {
121-
var calendar = createElement();
122-
calendar.addEventListener('dom-change',ev=>{
123-
if (ev.composedPath()[0].as=='week') {
124-
setTimeout(()=> _updateMonthStyles(calendar));
125-
}
126-
});
127-
return calendar;
122+
123+
const month = element.month;
124+
_clearStyles.call(element);
125+
_getStyle(month).then(styles=>setTimeout(()=>{
126+
if (element.month===month) {
127+
for (let i in styles) {
128+
_setStyleForDay.call(element,i,styles[i]);
128129
}
129130
}
130-
};
131+
}));
131132
}
132133

133134
_onButtonClick(delta) {

0 commit comments

Comments
 (0)