Skip to content

Commit 60e3868

Browse files
committed
feat(Calendar, Date Picker, Date Range Picker, Picker, Time Picker): initial release
1 parent ca5eac8 commit 60e3868

11 files changed

+531
-292
lines changed

js/src/calendar.js

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* --------------------------------------------------------------------------
99
*/
1010

11-
import { defineJQueryPlugin, isElement, typeCheckConfig } from './util/index'
11+
import { defineJQueryPlugin, typeCheckConfig } from './util/index'
1212
import EventHandler from './dom/event-handler'
1313
import Manipulator from './dom/manipulator'
1414
import {
@@ -24,7 +24,6 @@ import {
2424
isStartDate,
2525
isEndDate } from './util/calendar'
2626
import BaseComponent from './base-component'
27-
// import CalendarTimepicker from './time-picker'
2827

2928
/**
3029
* ------------------------------------------------------------------------
@@ -58,10 +57,10 @@ const Default = {
5857
locale: navigator.language,
5958
maxDate: null,
6059
minDate: null,
61-
order: 'first',
6260
range: true,
6361
startDate: null,
64-
selectEndDate: false
62+
selectEndDate: false,
63+
weekdayLength: 2
6564
}
6665

6766
const DefaultType = {
@@ -72,10 +71,10 @@ const DefaultType = {
7271
locale: 'string',
7372
maxDate: '(date|string|null)',
7473
minDate: '(date|string|null)',
75-
order: 'string',
7674
range: 'boolean',
7775
startDate: '(date|string|null)',
78-
selectEndDate: 'boolean'
76+
selectEndDate: 'boolean',
77+
weekdayLength: 'number'
7978
}
8079

8180
/**
@@ -117,10 +116,6 @@ class Calendar extends BaseComponent {
117116
EventHandler.on(this._element, 'click', SELECTOR_CALENDAR_CELL_INNER, event => {
118117
event.preventDefault()
119118
if (event.target.classList.contains('day')) {
120-
if (event.target.parentElement.classList.contains('disabled')) {
121-
return
122-
}
123-
124119
this._selectDate(Manipulator.getDataAttribute(event.target, 'date'))
125120
}
126121

@@ -221,59 +216,74 @@ class Calendar extends BaseComponent {
221216
this._updateCalendar()
222217
}
223218

219+
_setEndDate(date, selectEndDate = false) {
220+
this._endDate = new Date(date)
221+
EventHandler.trigger(this._element, EVENT_END_DATE_CHANGE, {
222+
date: this._endDate,
223+
selectEndDate
224+
})
225+
}
226+
227+
_setStartDate(date, selectEndDate = true) {
228+
this._startDate = new Date(date)
229+
EventHandler.trigger(this._element, EVENT_START_DATE_CHANGE, {
230+
date: this._startDate,
231+
selectEndDate
232+
})
233+
}
234+
224235
_selectDate(date) {
225-
if (this._selectEndDate) {
226-
this._endDate = new Date(date)
227-
this._selectEndDate = false
228-
EventHandler.trigger(this._element, EVENT_END_DATE_CHANGE, {
229-
date: this._endDate,
230-
selectEndDate: this._selectEndDate
231-
})
236+
if (isDisabled(date, this._config.minDate, this._config.maxDate, this._config.disabledDates)) {
232237
return
233238
}
234239

235240
if (this._config.range) {
236-
this._selectEndDate = true
241+
if (this._selectEndDate) {
242+
if (this._startDate && this._startDate > new Date(date)) {
243+
this._setEndDate(this._startDate)
244+
this._setStartDate(date)
245+
} else {
246+
this._setEndDate(date)
247+
}
248+
} else {
249+
this._setStartDate(date, true)
250+
}
251+
} else {
252+
this._setStartDate(date)
237253
}
238-
239-
this._startDate = new Date(date)
240-
EventHandler.trigger(this._element, EVENT_START_DATE_CHANGE, {
241-
date: this._startDate,
242-
selectEndDate: this._selectEndDate
243-
})
244254
}
245255

246256
_createCalendar() {
247-
const { firstDayOfWeek, locale } = this._config
257+
const { firstDayOfWeek, locale, weekdayLength } = this._config
248258
const year = this._calendarDate.getFullYear()
249259
const month = this._calendarDate.getMonth()
250260

251261
// Create navigation
252262
const navigationElement = document.createElement('div')
253-
navigationElement.classList.add('calendar-navigation')
263+
navigationElement.classList.add('calendar-nav')
254264
navigationElement.innerHTML = `
255-
<div class="calendar-navigation-prev">
265+
<div class="calendar-nav-prev">
256266
<button class="btn btn-transparent btn-sm btn-double-prev">
257-
<span class="double-prev-icon"></span>
267+
<span class="calendar-nav-icon calendar-nav-icon-double-prev"></span>
258268
</button>
259269
<button class="btn btn-transparent btn-sm btn-prev">
260-
<span class="prev-icon"></span>
270+
<span class="calendar-nav-icon calendar-nav-icon-prev"></span>
261271
</button>
262272
</div>
263-
<div class="calendar-navigation-date">
273+
<div class="calendar-nav-date">
264274
<button class="btn btn-transparent btn-sm btn-month">
265275
${this._calendarDate.toLocaleDateString(locale, { month: 'long' })}
266276
</button>
267277
<button class="btn btn-transparent btn-sm btn-year">
268278
${this._calendarDate.toLocaleDateString(locale, { year: 'numeric' })}
269279
</button>
270280
</div>
271-
<div class="calendar-navigation-next">
281+
<div class="calendar-nav-next">
272282
<button class="btn btn-transparent btn-sm btn-next">
273-
<span class="next-icon"></span>
283+
<span class="calendar-nav-icon calendar-nav-icon-next"></span>
274284
</button>
275285
<button class="btn btn-transparent btn-sm btn-double-next">
276-
<span class="double-next-icon"></span>
286+
<span class="calendar-nav-icon calendar-nav-icon-double-next"></span>
277287
</button>
278288
</div>
279289
`
@@ -288,7 +298,13 @@ class Calendar extends BaseComponent {
288298
${this._view === 'days' ? `
289299
<thead>
290300
<tr>
291-
${weekDays.map(({ date }) => (`<th class="calendar-cell">${date.toLocaleDateString(locale, { weekday: 'narrow' })}</th>`)).join('')}
301+
${weekDays.map(({ date }) => (
302+
`<th class="calendar-cell">
303+
<div class="calendar-header-cell-inner">
304+
${date.toLocaleDateString(locale, { weekday: 'long' }).slice(0, weekdayLength)}
305+
</div>
306+
</th>`
307+
)).join('')}
292308
</tr>
293309
</thead>` : ''}
294310
<tbody>
@@ -330,7 +346,6 @@ class Calendar extends BaseComponent {
330346
_updateCalendar() {
331347
this._element.innerHTML = ''
332348
this._createCalendar()
333-
// this._createCalendarTimepicker()
334349
}
335350

336351
_dayClassNames(date, month) {
@@ -354,15 +369,7 @@ class Calendar extends BaseComponent {
354369
...config
355370
}
356371

357-
typeCheckConfig(NAME, config, this.constructor.DefaultType)
358-
359-
if (typeof config.reference === 'object' && !isElement(config.reference) &&
360-
typeof config.reference.getBoundingClientRect !== 'function'
361-
) {
362-
// Popper virtual elements require a getBoundingClientRect method
363-
throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
364-
}
365-
372+
typeCheckConfig(NAME, config, DefaultType)
366373
return config
367374
}
368375

js/src/date-picker.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* --------------------------------------------------------------------------
66
*/
77

8-
import { defineJQueryPlugin, typeCheckConfig } from './util/index'
8+
import { defineJQueryPlugin } from './util/index'
99
import EventHandler from './dom/event-handler'
10-
import Manipulator from './dom/manipulator'
1110
import SelectorEngine from './dom/selector-engine'
1211
import DateRangePicker from './date-range-picker'
1312

@@ -30,7 +29,6 @@ const SELECTOR_DATA_TOGGLE = '[data-coreui-toggle="date-picker"]'
3029
const Default = {
3130
...DateRangePicker.Default,
3231
calendars: 1,
33-
date: null,
3432
placeholder: ['Select date'],
3533
range: false
3634
}
@@ -47,10 +45,6 @@ const DefaultType = {
4745
*/
4846

4947
class DatePicker extends DateRangePicker {
50-
constructor(element, config) {
51-
super(element, config)
52-
this._startDate = this._config.date
53-
}
5448
// Getters
5549

5650
static get Default() {
@@ -83,17 +77,6 @@ class DatePicker extends DateRangePicker {
8377
})
8478
}
8579

86-
_getConfig(config) {
87-
config = {
88-
...this.constructor.Default,
89-
...Manipulator.getDataAttributes(this._element),
90-
...(typeof config === 'object' ? config : {})
91-
}
92-
93-
typeCheckConfig(NAME, config, DefaultType)
94-
return config
95-
}
96-
9780
// Static
9881

9982
static datePickerInterface(element, config) {

0 commit comments

Comments
 (0)