@ohos-ports/flowbite-datepicker 2.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.js +73 -0
- package/.stylelintrc +12 -0
- package/CHANGELOG.md +72 -0
- package/LICENSE +21 -0
- package/README.md +71 -0
- package/demo/index.html +582 -0
- package/demo/live-demo.js +471 -0
- package/dist/DateRangePicker.cjs.js +2996 -0
- package/dist/DateRangePicker.esm.js +2994 -0
- package/dist/DateRangePicker.umd.js +3002 -0
- package/dist/Datepicker.cjs.js +2778 -0
- package/dist/Datepicker.esm.js +2776 -0
- package/dist/Datepicker.umd.js +2784 -0
- package/dist/css/datepicker-bs4.css +276 -0
- package/dist/css/datepicker-bs4.min.css +1 -0
- package/dist/css/datepicker-bs5.css +276 -0
- package/dist/css/datepicker-bs5.min.css +1 -0
- package/dist/css/datepicker-bulma.css +258 -0
- package/dist/css/datepicker-bulma.min.css +1 -0
- package/dist/css/datepicker-foundation.css +262 -0
- package/dist/css/datepicker-foundation.min.css +1 -0
- package/dist/css/datepicker.css +306 -0
- package/dist/css/datepicker.min.css +1403 -0
- package/dist/js/datepicker-full.js +2777 -0
- package/dist/js/datepicker-full.min.js +1 -0
- package/dist/js/datepicker.js +2569 -0
- package/dist/js/datepicker.min.js +1 -0
- package/dist/js/locales/ar-tn.js +15 -0
- package/dist/js/locales/ar.js +15 -0
- package/dist/js/locales/az.js +14 -0
- package/dist/js/locales/bg.js +14 -0
- package/dist/js/locales/bm.js +18 -0
- package/dist/js/locales/bn.js +19 -0
- package/dist/js/locales/br.js +18 -0
- package/dist/js/locales/bs.js +15 -0
- package/dist/js/locales/ca.js +18 -0
- package/dist/js/locales/cs.js +19 -0
- package/dist/js/locales/cy.js +14 -0
- package/dist/js/locales/da.js +19 -0
- package/dist/js/locales/de.js +18 -0
- package/dist/js/locales/el.js +16 -0
- package/dist/js/locales/en-AU.js +18 -0
- package/dist/js/locales/en-CA.js +18 -0
- package/dist/js/locales/en-GB.js +18 -0
- package/dist/js/locales/en-IE.js +17 -0
- package/dist/js/locales/en-NZ.js +17 -0
- package/dist/js/locales/en-ZA.js +17 -0
- package/dist/js/locales/eo.js +17 -0
- package/dist/js/locales/es.js +18 -0
- package/dist/js/locales/et.js +18 -0
- package/dist/js/locales/eu.js +18 -0
- package/dist/js/locales/fa.js +17 -0
- package/dist/js/locales/fi.js +17 -0
- package/dist/js/locales/fo.js +15 -0
- package/dist/js/locales/fr-CH.js +21 -0
- package/dist/js/locales/fr.js +18 -0
- package/dist/js/locales/gl.js +16 -0
- package/dist/js/locales/he.js +15 -0
- package/dist/js/locales/hi.js +18 -0
- package/dist/js/locales/hr.js +13 -0
- package/dist/js/locales/hu.js +18 -0
- package/dist/js/locales/hy.js +18 -0
- package/dist/js/locales/id.js +15 -0
- package/dist/js/locales/is.js +14 -0
- package/dist/js/locales/it-CH.js +20 -0
- package/dist/js/locales/it.js +18 -0
- package/dist/js/locales/ja.js +17 -0
- package/dist/js/locales/ka.js +17 -0
- package/dist/js/locales/kk.js +15 -0
- package/dist/js/locales/km.js +15 -0
- package/dist/js/locales/ko.js +18 -0
- package/dist/js/locales/lt.js +19 -0
- package/dist/js/locales/lv.js +18 -0
- package/dist/js/locales/me.js +17 -0
- package/dist/js/locales/mk.js +15 -0
- package/dist/js/locales/mn.js +17 -0
- package/dist/js/locales/mr.js +18 -0
- package/dist/js/locales/ms.js +15 -0
- package/dist/js/locales/nl-BE.js +18 -0
- package/dist/js/locales/nl.js +18 -0
- package/dist/js/locales/no.js +18 -0
- package/dist/js/locales/oc.js +17 -0
- package/dist/js/locales/pl.js +17 -0
- package/dist/js/locales/pt-BR.js +17 -0
- package/dist/js/locales/pt.js +18 -0
- package/dist/js/locales/ro.js +17 -0
- package/dist/js/locales/ru.js +18 -0
- package/dist/js/locales/si.js +18 -0
- package/dist/js/locales/sk.js +18 -0
- package/dist/js/locales/sl.js +15 -0
- package/dist/js/locales/sq.js +18 -0
- package/dist/js/locales/sr-latn.js +16 -0
- package/dist/js/locales/sr.js +16 -0
- package/dist/js/locales/sv.js +17 -0
- package/dist/js/locales/sw.js +15 -0
- package/dist/js/locales/ta.js +18 -0
- package/dist/js/locales/tg.js +19 -0
- package/dist/js/locales/th.js +14 -0
- package/dist/js/locales/tk.js +18 -0
- package/dist/js/locales/tr.js +17 -0
- package/dist/js/locales/uk.js +17 -0
- package/dist/js/locales/uz-cyrl.js +18 -0
- package/dist/js/locales/uz-latn.js +18 -0
- package/dist/js/locales/vi.js +16 -0
- package/dist/js/locales/zh-CN.js +19 -0
- package/dist/js/locales/zh-TW.js +18 -0
- package/dist/locales/index.cjs.js +1028 -0
- package/dist/locales/index.esm.js +1028 -0
- package/dist/main.cjs.js +2999 -0
- package/dist/main.esm.js +2994 -0
- package/dist/main.umd.js +3005 -0
- package/docs/.nojekyll +0 -0
- package/docs/README.md +266 -0
- package/docs/_sidebar.md +6 -0
- package/docs/api.md +284 -0
- package/docs/date-string+format.md +90 -0
- package/docs/i18n.md +99 -0
- package/docs/images/datepicker-inline.jpg +0 -0
- package/docs/images/datepicker.jpg +0 -0
- package/docs/images/multidate.jpg +0 -0
- package/docs/images/picker-structure.jpg +0 -0
- package/docs/images/rangepicker.jpg +0 -0
- package/docs/images/view-days.jpg +0 -0
- package/docs/images/view-decades.jpg +0 -0
- package/docs/images/view-months.jpg +0 -0
- package/docs/images/view-years.jpg +0 -0
- package/docs/index.html +26 -0
- package/docs/options.md +354 -0
- package/docs/overview.md +174 -0
- package/extras/twoDigitToFullYear/test/twoDigitToFullYear.js +58 -0
- package/extras/twoDigitToFullYear/twoDigitToFullYear.js +26 -0
- package/js/DateRangePicker.js +210 -0
- package/js/Datepicker.js +487 -0
- package/js/datepicker-full.js +5 -0
- package/js/events/functions.js +48 -0
- package/js/events/inputFieldListeners.js +206 -0
- package/js/events/otherListeners.js +15 -0
- package/js/events/pickerListeners.js +70 -0
- package/js/i18n/base-locales.js +13 -0
- package/js/i18n/locales/ar-tn.js +15 -0
- package/js/i18n/locales/ar.js +15 -0
- package/js/i18n/locales/az.js +14 -0
- package/js/i18n/locales/bg.js +14 -0
- package/js/i18n/locales/bm.js +18 -0
- package/js/i18n/locales/bn.js +19 -0
- package/js/i18n/locales/br.js +18 -0
- package/js/i18n/locales/bs.js +15 -0
- package/js/i18n/locales/ca.js +18 -0
- package/js/i18n/locales/cs.js +19 -0
- package/js/i18n/locales/cy.js +14 -0
- package/js/i18n/locales/da.js +19 -0
- package/js/i18n/locales/de.js +18 -0
- package/js/i18n/locales/el.js +16 -0
- package/js/i18n/locales/en-AU.js +18 -0
- package/js/i18n/locales/en-CA.js +18 -0
- package/js/i18n/locales/en-GB.js +18 -0
- package/js/i18n/locales/en-IE.js +17 -0
- package/js/i18n/locales/en-NZ.js +17 -0
- package/js/i18n/locales/en-ZA.js +17 -0
- package/js/i18n/locales/eo.js +17 -0
- package/js/i18n/locales/es.js +18 -0
- package/js/i18n/locales/et.js +18 -0
- package/js/i18n/locales/eu.js +18 -0
- package/js/i18n/locales/fa.js +17 -0
- package/js/i18n/locales/fi.js +17 -0
- package/js/i18n/locales/fo.js +15 -0
- package/js/i18n/locales/fr-CH.js +21 -0
- package/js/i18n/locales/fr.js +18 -0
- package/js/i18n/locales/gl.js +16 -0
- package/js/i18n/locales/he.js +15 -0
- package/js/i18n/locales/hi.js +18 -0
- package/js/i18n/locales/hr.js +13 -0
- package/js/i18n/locales/hu.js +18 -0
- package/js/i18n/locales/hy.js +18 -0
- package/js/i18n/locales/id.js +15 -0
- package/js/i18n/locales/is.js +14 -0
- package/js/i18n/locales/it-CH.js +20 -0
- package/js/i18n/locales/it.js +18 -0
- package/js/i18n/locales/ja.js +17 -0
- package/js/i18n/locales/ka.js +17 -0
- package/js/i18n/locales/kk.js +15 -0
- package/js/i18n/locales/km.js +15 -0
- package/js/i18n/locales/ko.js +18 -0
- package/js/i18n/locales/lt.js +19 -0
- package/js/i18n/locales/lv.js +18 -0
- package/js/i18n/locales/me.js +17 -0
- package/js/i18n/locales/mk.js +15 -0
- package/js/i18n/locales/mn.js +17 -0
- package/js/i18n/locales/mr.js +18 -0
- package/js/i18n/locales/ms.js +15 -0
- package/js/i18n/locales/nl-BE.js +18 -0
- package/js/i18n/locales/nl.js +18 -0
- package/js/i18n/locales/no.js +18 -0
- package/js/i18n/locales/oc.js +17 -0
- package/js/i18n/locales/pl.js +17 -0
- package/js/i18n/locales/pt-BR.js +17 -0
- package/js/i18n/locales/pt.js +18 -0
- package/js/i18n/locales/ro.js +17 -0
- package/js/i18n/locales/ru.js +18 -0
- package/js/i18n/locales/si.js +18 -0
- package/js/i18n/locales/sk.js +18 -0
- package/js/i18n/locales/sl.js +15 -0
- package/js/i18n/locales/sq.js +18 -0
- package/js/i18n/locales/sr-latn.js +16 -0
- package/js/i18n/locales/sr.js +16 -0
- package/js/i18n/locales/sv.js +17 -0
- package/js/i18n/locales/sw.js +15 -0
- package/js/i18n/locales/ta.js +18 -0
- package/js/i18n/locales/tg.js +19 -0
- package/js/i18n/locales/th.js +14 -0
- package/js/i18n/locales/tk.js +18 -0
- package/js/i18n/locales/tr.js +17 -0
- package/js/i18n/locales/uk.js +17 -0
- package/js/i18n/locales/uz-cyrl.js +18 -0
- package/js/i18n/locales/uz-latn.js +18 -0
- package/js/i18n/locales/vi.js +16 -0
- package/js/i18n/locales/zh-CN.js +19 -0
- package/js/i18n/locales/zh-TW.js +18 -0
- package/js/lib/date-format.js +185 -0
- package/js/lib/date.js +82 -0
- package/js/lib/dom.js +55 -0
- package/js/lib/event.js +68 -0
- package/js/lib/utils.js +61 -0
- package/js/main.js +4 -0
- package/js/options/defaultOptions.js +38 -0
- package/js/options/processOptions.js +263 -0
- package/js/picker/Picker.js +334 -0
- package/js/picker/templates/calendarWeeksTemplate.js +8 -0
- package/js/picker/templates/daysTemplate.js +8 -0
- package/js/picker/templates/pickerTemplate.js +23 -0
- package/js/picker/views/DaysView.js +250 -0
- package/js/picker/views/MonthsView.js +198 -0
- package/js/picker/views/View.js +55 -0
- package/js/picker/views/YearsView.js +170 -0
- package/package.json +81 -0
- package/postcss.config.js +5 -0
- package/rollup.config.js +75 -0
- package/sass/_mixins.scss +12 -0
- package/sass/datepicker-bs4.scss +91 -0
- package/sass/datepicker-bs5.scss +91 -0
- package/sass/datepicker-bulma.scss +69 -0
- package/sass/datepicker-foundation.scss +66 -0
- package/sass/datepicker.scss +436 -0
- package/sass/index-bs4.scss +5 -0
- package/sass/index-bs5.scss +5 -0
- package/sass/index-bulma.scss +6 -0
- package/sass/index-foundation.scss +3 -0
- package/scripts/build-css.js +39 -0
- package/scripts/build-locales.js +57 -0
- package/scripts/convert-bsdp-locales.js +65 -0
- package/scripts/minify-css.js +31 -0
- package/scripts/minify-js.js +17 -0
- package/style.css +10 -0
- package/tailwind.config.js +16 -0
- package/tailwind.css +1389 -0
- package/test/DateRangePicker/DateRangePicker.js +547 -0
- package/test/DateRangePicker/api-methods.js +415 -0
- package/test/DateRangePicker/date-selection.js +472 -0
- package/test/DateRangePicker/options.js +308 -0
- package/test/Datepicker/Datepicker-object.js +236 -0
- package/test/Datepicker/api-methods.js +328 -0
- package/test/_utils/date.js +102 -0
- package/test/_utils/dom.js +70 -0
- package/test/events.js +785 -0
- package/test/inline-mode.js +45 -0
- package/test/keyboard-operation/arrow-down.js +293 -0
- package/test/keyboard-operation/arrow-left.js +255 -0
- package/test/keyboard-operation/arrow-right.js +215 -0
- package/test/keyboard-operation/arrow-up.js +461 -0
- package/test/keyboard-operation/edit-mode.js +458 -0
- package/test/keyboard-operation/keyboard-operation.js +146 -0
- package/test/mouse-operation.js +580 -0
- package/test/options/before-show.js +635 -0
- package/test/options/bulma-datepicker.sublime-workspace +1280 -0
- package/test/options/buttons.js +383 -0
- package/test/options/date-restrictions.js +802 -0
- package/test/options/format.js +281 -0
- package/test/options/multidate.js +380 -0
- package/test/options/options.js +727 -0
- package/test/options/orientation.js +410 -0
- package/test/options/pick-levle+view.js +357 -0
- package/test/test.html +94 -0
- package/test/unit/DateRangePicker.js +192 -0
- package/test/unit/Datepicker.js +307 -0
- package/test/unit/_setup.js +28 -0
- package/test/unit/lib/date-format.js +310 -0
- package/test/unit/lib/date.js +211 -0
- package/test/unit/lib/dom.js +142 -0
- package/test/unit/lib/event.js +207 -0
- package/test/unit/lib/utils.js +135 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {twoDigitToFullYear} from '../twoDigitToFullYear.js';
|
|
2
|
+
|
|
3
|
+
const expect = require('unexpected');
|
|
4
|
+
const sinon = require('sinon');
|
|
5
|
+
|
|
6
|
+
describe('twoDigitToFullYear()', function () {
|
|
7
|
+
it('converts 2-digit year to full year using 50 for the future years range by default', function () {
|
|
8
|
+
let clock = sinon.useFakeTimers({now: new Date().setFullYear(2020)});
|
|
9
|
+
|
|
10
|
+
expect(twoDigitToFullYear(0), 'to be', 2000);
|
|
11
|
+
expect(twoDigitToFullYear(20), 'to be', 2020);
|
|
12
|
+
expect(twoDigitToFullYear(70), 'to be', 2070);
|
|
13
|
+
expect(twoDigitToFullYear(71), 'to be', 1971);
|
|
14
|
+
|
|
15
|
+
clock.restore();
|
|
16
|
+
clock = sinon.useFakeTimers({now: new Date().setFullYear(2080)});
|
|
17
|
+
|
|
18
|
+
expect(twoDigitToFullYear(0), 'to be', 2100);
|
|
19
|
+
expect(twoDigitToFullYear(30), 'to be', 2130);
|
|
20
|
+
expect(twoDigitToFullYear(31), 'to be', 2031);
|
|
21
|
+
expect(twoDigitToFullYear(80), 'to be', 2080);
|
|
22
|
+
|
|
23
|
+
clock.restore();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('uses sepecified future year range if it is given', function () {
|
|
27
|
+
let clock = sinon.useFakeTimers({now: new Date().setFullYear(2020)});
|
|
28
|
+
|
|
29
|
+
expect(twoDigitToFullYear(40, 20), 'to be', 2040);
|
|
30
|
+
expect(twoDigitToFullYear(40, 19), 'to be', 1940);
|
|
31
|
+
expect(twoDigitToFullYear(95, 75), 'to be', 2095);
|
|
32
|
+
expect(twoDigitToFullYear(95, 74), 'to be', 1995);
|
|
33
|
+
|
|
34
|
+
clock.restore();
|
|
35
|
+
clock = sinon.useFakeTimers({now: new Date().setFullYear(2080)});
|
|
36
|
+
|
|
37
|
+
expect(twoDigitToFullYear(0, 20), 'to be', 2100);
|
|
38
|
+
expect(twoDigitToFullYear(0, 19), 'to be', 2000);
|
|
39
|
+
expect(twoDigitToFullYear(55, 75), 'to be', 2155);
|
|
40
|
+
expect(twoDigitToFullYear(55, 74), 'to be', 2055);
|
|
41
|
+
|
|
42
|
+
clock.restore();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('uses the century of baseDate for conversion if it is given', function () {
|
|
46
|
+
const year2020 = new Date(2020, 0, 1);
|
|
47
|
+
expect(twoDigitToFullYear(40, 20, year2020), 'to be', 2040);
|
|
48
|
+
expect(twoDigitToFullYear(40, 19, year2020), 'to be', 1940);
|
|
49
|
+
expect(twoDigitToFullYear(95, 75, year2020), 'to be', 2095);
|
|
50
|
+
expect(twoDigitToFullYear(95, 74, year2020), 'to be', 1995);
|
|
51
|
+
|
|
52
|
+
const year2280 = new Date(2280, 0, 1);
|
|
53
|
+
expect(twoDigitToFullYear(0, 20, year2280), 'to be', 2300);
|
|
54
|
+
expect(twoDigitToFullYear(0, 19, year2280), 'to be', 2200);
|
|
55
|
+
expect(twoDigitToFullYear(55, 75, year2280), 'to be', 2355);
|
|
56
|
+
expect(twoDigitToFullYear(55, 74, year2280), 'to be', 2255);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert 2-digit year to 4-digit year
|
|
3
|
+
* By default, 2-digit year will be converted to a year in the 100-year
|
|
4
|
+
* period ending 50 years after the current year. The target 100-year period
|
|
5
|
+
* can be changed by specifying base date and the length of the future side.
|
|
6
|
+
* @param {Number} year - 2-digit year
|
|
7
|
+
* @param {Number} [futureSideSpan] - number of yeas in the future side of
|
|
8
|
+
* the target 100-year period. Default: 50
|
|
9
|
+
* @param {Date} [baseDate] - base date to caliculate the target 100-year
|
|
10
|
+
* period. if omitted, the current date is used.
|
|
11
|
+
* @return {Number} 4-digit year
|
|
12
|
+
*/
|
|
13
|
+
export function twoDigitToFullYear(year, futureSideSpan = 50, baseDate = undefined) {
|
|
14
|
+
if (isNaN(year) || year > 99 || year < 0) {
|
|
15
|
+
return year;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const date = baseDate instanceof Date && !isNaN(baseDate) ? baseDate : new Date();
|
|
19
|
+
const currentYear = date.getFullYear();
|
|
20
|
+
const endOfCurrent = currentYear + futureSideSpan;
|
|
21
|
+
let century = Math.floor(endOfCurrent / 100) * 100;
|
|
22
|
+
if (year > endOfCurrent % 100) {
|
|
23
|
+
century -= 100;
|
|
24
|
+
}
|
|
25
|
+
return year + century;
|
|
26
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import {registerListeners, unregisterListeners} from './lib/event.js';
|
|
2
|
+
import {formatDate} from './lib/date-format.js';
|
|
3
|
+
import Datepicker from './Datepicker.js';
|
|
4
|
+
|
|
5
|
+
// filter out the config options inapproprite to pass to Datepicker
|
|
6
|
+
function filterOptions(options) {
|
|
7
|
+
const newOpts = Object.assign({}, options);
|
|
8
|
+
|
|
9
|
+
delete newOpts.inputs;
|
|
10
|
+
delete newOpts.allowOneSidedRange;
|
|
11
|
+
delete newOpts.maxNumberOfDates; // to ensure each datepicker handles a single date
|
|
12
|
+
|
|
13
|
+
return newOpts;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function setupDatepicker(rangepicker, changeDateListener, el, options) {
|
|
17
|
+
registerListeners(rangepicker, [
|
|
18
|
+
[el, 'changeDate', changeDateListener],
|
|
19
|
+
]);
|
|
20
|
+
new Datepicker(el, options, rangepicker);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function onChangeDate(rangepicker, ev) {
|
|
24
|
+
// to prevent both datepickers trigger the other side's update each other
|
|
25
|
+
if (rangepicker._updating) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
rangepicker._updating = true;
|
|
29
|
+
|
|
30
|
+
const target = ev.target;
|
|
31
|
+
if (target.datepicker === undefined) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const datepickers = rangepicker.datepickers;
|
|
36
|
+
const setDateOptions = {render: false};
|
|
37
|
+
const changedSide = rangepicker.inputs.indexOf(target);
|
|
38
|
+
const otherSide = changedSide === 0 ? 1 : 0;
|
|
39
|
+
const changedDate = datepickers[changedSide].dates[0];
|
|
40
|
+
const otherDate = datepickers[otherSide].dates[0];
|
|
41
|
+
|
|
42
|
+
if (changedDate !== undefined && otherDate !== undefined) {
|
|
43
|
+
// if the start of the range > the end, swap them
|
|
44
|
+
if (changedSide === 0 && changedDate > otherDate) {
|
|
45
|
+
datepickers[0].setDate(otherDate, setDateOptions);
|
|
46
|
+
datepickers[1].setDate(changedDate, setDateOptions);
|
|
47
|
+
} else if (changedSide === 1 && changedDate < otherDate) {
|
|
48
|
+
datepickers[0].setDate(changedDate, setDateOptions);
|
|
49
|
+
datepickers[1].setDate(otherDate, setDateOptions);
|
|
50
|
+
}
|
|
51
|
+
} else if (!rangepicker.allowOneSidedRange) {
|
|
52
|
+
// to prevent the range from becoming one-sided, copy changed side's
|
|
53
|
+
// selection (no matter if it's empty) to the other side
|
|
54
|
+
if (changedDate !== undefined || otherDate !== undefined) {
|
|
55
|
+
setDateOptions.clear = true;
|
|
56
|
+
datepickers[otherSide].setDate(datepickers[changedSide].dates, setDateOptions);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
datepickers[0].picker.update().render();
|
|
60
|
+
datepickers[1].picker.update().render();
|
|
61
|
+
delete rangepicker._updating;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Class representing a date range picker
|
|
66
|
+
*/
|
|
67
|
+
export default class DateRangePicker {
|
|
68
|
+
/**
|
|
69
|
+
* Create a date range picker
|
|
70
|
+
* @param {Element} element - element to bind a date range picker
|
|
71
|
+
* @param {Object} [options] - config options
|
|
72
|
+
*/
|
|
73
|
+
constructor(element, options = {}) {
|
|
74
|
+
const inputs = Array.isArray(options.inputs)
|
|
75
|
+
? options.inputs
|
|
76
|
+
: Array.from(element.querySelectorAll('input'));
|
|
77
|
+
if (inputs.length < 2) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
element.rangepicker = this;
|
|
82
|
+
this.element = element;
|
|
83
|
+
this.inputs = inputs.slice(0, 2);
|
|
84
|
+
this.allowOneSidedRange = !!options.allowOneSidedRange;
|
|
85
|
+
|
|
86
|
+
const changeDateListener = onChangeDate.bind(null, this);
|
|
87
|
+
const cleanOptions = filterOptions(options);
|
|
88
|
+
// in order for initial date setup to work right when pcicLvel > 0,
|
|
89
|
+
// let Datepicker constructor add the instance to the rangepicker
|
|
90
|
+
const datepickers = [];
|
|
91
|
+
Object.defineProperty(this, 'datepickers', {
|
|
92
|
+
get() {
|
|
93
|
+
return datepickers;
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
setupDatepicker(this, changeDateListener, this.inputs[0], cleanOptions);
|
|
97
|
+
setupDatepicker(this, changeDateListener, this.inputs[1], cleanOptions);
|
|
98
|
+
Object.freeze(datepickers);
|
|
99
|
+
// normalize the range if inital dates are given
|
|
100
|
+
if (datepickers[0].dates.length > 0) {
|
|
101
|
+
onChangeDate(this, {target: this.inputs[0]});
|
|
102
|
+
} else if (datepickers[1].dates.length > 0) {
|
|
103
|
+
onChangeDate(this, {target: this.inputs[1]});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @type {Array} - selected date of the linked date pickers
|
|
109
|
+
*/
|
|
110
|
+
get dates() {
|
|
111
|
+
return this.datepickers.length === 2
|
|
112
|
+
? [
|
|
113
|
+
this.datepickers[0].dates[0],
|
|
114
|
+
this.datepickers[1].dates[0],
|
|
115
|
+
]
|
|
116
|
+
: undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Set new values to the config options
|
|
121
|
+
* @param {Object} options - config options to update
|
|
122
|
+
*/
|
|
123
|
+
setOptions(options) {
|
|
124
|
+
this.allowOneSidedRange = !!options.allowOneSidedRange;
|
|
125
|
+
|
|
126
|
+
const cleanOptions = filterOptions(options);
|
|
127
|
+
this.datepickers[0].setOptions(cleanOptions);
|
|
128
|
+
this.datepickers[1].setOptions(cleanOptions);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Destroy the DateRangePicker instance
|
|
133
|
+
* @return {DateRangePicker} - the instance destroyed
|
|
134
|
+
*/
|
|
135
|
+
destroy() {
|
|
136
|
+
this.datepickers[0].destroy();
|
|
137
|
+
this.datepickers[1].destroy();
|
|
138
|
+
unregisterListeners(this);
|
|
139
|
+
delete this.element.rangepicker;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Get the start and end dates of the date range
|
|
144
|
+
*
|
|
145
|
+
* The method returns Date objects by default. If format string is passed,
|
|
146
|
+
* it returns date strings formatted in given format.
|
|
147
|
+
* The result array always contains 2 items (start date/end date) and
|
|
148
|
+
* undefined is used for unselected side. (e.g. If none is selected,
|
|
149
|
+
* the result will be [undefined, undefined]. If only the end date is set
|
|
150
|
+
* when allowOneSidedRange config option is true, [undefined, endDate] will
|
|
151
|
+
* be returned.)
|
|
152
|
+
*
|
|
153
|
+
* @param {String} [format] - Format string to stringify the dates
|
|
154
|
+
* @return {Array} - Start and end dates
|
|
155
|
+
*/
|
|
156
|
+
getDates(format = undefined) {
|
|
157
|
+
const callback = format
|
|
158
|
+
? date => formatDate(date, format, this.datepickers[0].config.locale)
|
|
159
|
+
: date => new Date(date);
|
|
160
|
+
|
|
161
|
+
return this.dates.map(date => date === undefined ? date : callback(date));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Set the start and end dates of the date range
|
|
166
|
+
*
|
|
167
|
+
* The method calls datepicker.setDate() internally using each of the
|
|
168
|
+
* arguments in start→end order.
|
|
169
|
+
*
|
|
170
|
+
* When a clear: true option object is passed instead of a date, the method
|
|
171
|
+
* clears the date.
|
|
172
|
+
*
|
|
173
|
+
* If an invalid date, the same date as the current one or an option object
|
|
174
|
+
* without clear: true is passed, the method considers that argument as an
|
|
175
|
+
* "ineffective" argument because calling datepicker.setDate() with those
|
|
176
|
+
* values makes no changes to the date selection.
|
|
177
|
+
*
|
|
178
|
+
* When the allowOneSidedRange config option is false, passing {clear: true}
|
|
179
|
+
* to clear the range works only when it is done to the last effective
|
|
180
|
+
* argument (in other words, passed to rangeEnd or to rangeStart along with
|
|
181
|
+
* ineffective rangeEnd). This is because when the date range is changed,
|
|
182
|
+
* it gets normalized based on the last change at the end of the changing
|
|
183
|
+
* process.
|
|
184
|
+
*
|
|
185
|
+
* @param {Date|Number|String|Object} rangeStart - Start date of the range
|
|
186
|
+
* or {clear: true} to clear the date
|
|
187
|
+
* @param {Date|Number|String|Object} rangeEnd - End date of the range
|
|
188
|
+
* or {clear: true} to clear the date
|
|
189
|
+
*/
|
|
190
|
+
setDates(rangeStart, rangeEnd) {
|
|
191
|
+
const [datepicker0, datepicker1] = this.datepickers;
|
|
192
|
+
const origDates = this.dates;
|
|
193
|
+
|
|
194
|
+
// If range normalization runs on every change, we can't set a new range
|
|
195
|
+
// that starts after the end of the current range correctly because the
|
|
196
|
+
// normalization process swaps start↔︎end right after setting the new start
|
|
197
|
+
// date. To prevent this, the normalization process needs to run once after
|
|
198
|
+
// both of the new dates are set.
|
|
199
|
+
this._updating = true;
|
|
200
|
+
datepicker0.setDate(rangeStart);
|
|
201
|
+
datepicker1.setDate(rangeEnd);
|
|
202
|
+
delete this._updating;
|
|
203
|
+
|
|
204
|
+
if (datepicker1.dates[0] !== origDates[1]) {
|
|
205
|
+
onChangeDate(this, {target: this.inputs[1]});
|
|
206
|
+
} else if (datepicker0.dates[0] !== origDates[0]) {
|
|
207
|
+
onChangeDate(this, {target: this.inputs[0]});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|