UNPKG

@narmafzam/jalali-datepicker

Version:

just a jalali-datepicker

276 lines (255 loc) 8.85 kB
class State { constructor(model) { this.model = model; this.filetredDate = (this.model.options.minDate || this.model.options.maxDate); this.viewModeList = this.model.options._viewModeList; this.viewMode = (this.viewModeList.indexOf(model.options.viewMode) > 0) ? model.options.viewMode : this.viewModeList[0]; this.viewModeIndex = (this.viewModeList.indexOf(model.options.viewMode) > 0) ? this.viewModeList.indexOf(model.options.viewMode) : 0; // defaul 'day' this.filterDate = { start: { year: 0, month: 0, date: 0, hour: 0, minute: 0, second: 0, unixDate: 0 }, end: { year: 0, month: 0, date: 0, hour: 0, minute: 0, second: 0, unixDate: 0 } }; this.view = { year: 0, month: 0, date: 0, hour: 0, minute: 0, second: 0, unixDate: 0, dateObject: null, meridian: 'AM' }; this.selected = { year: 0, month: 0, date: 0, hour: 0, hour12: 0, minute: 0, second: 0, unixDate: 0, dateObject: null }; this.ui = { isOpen: false, isInline: this.model.options.inline }; this._setFilterDate(this.model.options.minDate, this.model.options.maxDate); return this; } _setFilterDate(minDate, maxDate) { let self = this; if (!minDate) { minDate = -2000000000000000; } if (!maxDate) { maxDate = 2000000000000000; } let pd = self.model.JalaliDate.date(minDate); self.filterDate.start.unixDate = minDate; self.filterDate.start.hour = pd.hour(); self.filterDate.start.minute = pd.minute(); self.filterDate.start.second = pd.second(); self.filterDate.start.month = pd.month(); self.filterDate.start.date = pd.date(); self.filterDate.start.year = pd.year(); let pdEnd = self.model.JalaliDate.date(maxDate); self.filterDate.end.unixDate = maxDate; self.filterDate.end.hour = pdEnd.hour(); self.filterDate.end.minute = pdEnd.minute(); self.filterDate.end.second = pdEnd.second(); self.filterDate.end.month = pdEnd.month(); self.filterDate.end.date = pdEnd.date(); self.filterDate.end.year = pdEnd.year(); } navigate(nav) { if (nav == 'next') { if (this.viewMode == 'year') { this.setViewDateTime('year', this.view.year + 12); } if (this.viewMode == 'month') { let newYear = this.view.year + 1; if (newYear === 0) { newYear = 1; } this.setViewDateTime('year', newYear); } if (this.viewMode == 'day') { let newYear = this.view.year + 1; if (newYear === 0) { newYear = 1; } if ((this.view.month + 1) == 13) { this.setViewDateTime('year', newYear); this.setViewDateTime('month', 1); } else { this.setViewDateTime('month', this.view.month + 1); } } } else { if (this.viewMode == 'year') { this.setViewDateTime('year', this.view.year - 12); } if (this.viewMode == 'month') { let newYear = this.view.year - 1; if (newYear === 0) { newYear = -1; } this.setViewDateTime('year', newYear); } if (this.viewMode == 'day') { if ((this.view.month - 1) <= 0) { let newYear = this.view.year - 1; if (newYear === 0) { newYear = -1; } this.setViewDateTime('year', newYear); this.setViewDateTime('month', 12); } else { this.setViewDateTime('month', this.view.month - 1); } } } } switchViewMode() { this.viewModeIndex = ((this.viewModeIndex + 1) >= this.viewModeList.length) ? 0 : (this.viewModeIndex + 1); this.viewMode = (this.viewModeList[this.viewModeIndex]) ? (this.viewModeList[this.viewModeIndex]) : (this.viewModeList[0]); this._setViewDateTimeUnix(); return this; } switchViewModeTo(viewMode) { if (this.viewModeList.indexOf(viewMode) >= 0) { this.viewMode = viewMode; this.viewModeIndex = this.viewModeList.indexOf(viewMode); } } setSelectedDateTime(key, value) { let that = this; switch (key) { case 'unix': that.selected.unixDate = value; let pd = this.model.JalaliDate.date(value); that.selected.year = pd.year(); that.selected.month = pd.month(); that.selected.date = pd.date(); that.selected.hour = pd.hour(); that.selected.hour12 = pd.format('hh'); that.selected.minute = pd.minute(); that.selected.second = pd.second(); break; case 'year': this.selected.year = value; break; case 'month': this.selected.month = value; break; case 'date': this.selected.date = value; break; case 'hour': this.selected.hour = value; break; case 'minute': this.selected.minute = value; break; case 'second': this.selected.second = value; break; } that._updateSelectedUnix(); return this; } _updateSelectedUnix() { this.selected.dateObject = this.model.JalaliDate.date([ this.selected.year, this.selected.month, this.selected.date, this.view.hour, this.view.minute, this.view.second ]); this.selected.unixDate = this.selected.dateObject.valueOf(); this.model.updateInput(this.selected.unixDate); return this; } _setViewDateTimeUnix() { this.view.dateObject = this.model.JalaliDate.date([ this.view.year, this.view.month, this.view.date, this.view.hour, this.view.minute, this.view.second ]); this.view.year = this.view.dateObject.year(); this.view.month = this.view.dateObject.month(); this.view.date = this.view.dateObject.date(); this.view.hour = this.view.dateObject.hour(); this.view.hour12 = this.view.dateObject.format('hh'); this.view.minute = this.view.dateObject.minute(); this.view.second = this.view.dateObject.second(); this.view.unixDate = this.view.dateObject.valueOf(); return this; } setViewDateTime(key, value) { let self = this; switch (key) { case 'unix': let pd = this.model.JalaliDate.date(value); self.view.year = pd.year(); self.view.month = pd.month(); self.view.date = pd.date(); self.view.hour = pd.hour(); self.view.minute = pd.minute(); self.view.second = pd.second(); break; case 'year': this.view.year = value; break; case 'month': this.view.month = value; break; case 'date': this.view.date = value; break; case 'hour': this.view.hour = value; break; case 'minute': this.view.minute = value; break; case 'second': this.view.second = value; break; } this._setViewDateTimeUnix(); return this; } meridianToggle() { let self = this; if (self.view.meridian === 'AM') { self.view.meridian = 'PM'; } else if (self.view.meridian === 'PM') { self.view.meridian = 'AM'; } } } module.exports = State;