webpack-bootstrap-ui-kit/src/js_old/_components/_ui.form.datetime.js

129 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-12-24 23:42:33 +01:00
'use strict';
2019-06-08 17:20:51 +02:00
import $ from 'jquery';
import Events from '../_events';
import 'bootstrap-datepicker/dist/js/bootstrap-datepicker.js';
import 'bootstrap-timepicker/js/bootstrap-timepicker.js';
const DatetimeUI = (($) => {
// Constants
const W = window;
const D = document;
const $Body = $('body');
const NAME = 'jsDatetimeUI';
const DATA_KEY = NAME;
const datepickerOptions = {
autoclose: true,
startDate: 0,
//todayBtn: true,
todayHighlight: true,
clearBtn: true,
};
class DatetimeUI {
constructor(el) {
2020-12-24 23:42:33 +01:00
console.log(`${NAME}: init`);
2020-09-09 17:40:58 +02:00
2019-06-08 17:20:51 +02:00
const ui = this;
const $el = $(el);
ui._el = el;
// datepicker
if ($el.hasClass('date') || $el.attr('type') === 'date') {
2020-12-24 23:42:33 +01:00
const defaultDate =
$el.attr('name').toLowerCase().indexOf('end') !== -1 ? '+4d' : '+3d';
2019-06-08 17:20:51 +02:00
$el.attr('readonly', 'true');
2020-12-24 23:42:33 +01:00
$el.datepicker(
$.extend(
datepickerOptions,
{
defaultViewDate: defaultDate,
multidate: $el.data('multidate'),
},
$el.data(),
),
);
}
2019-06-08 17:20:51 +02:00
// timepicker
2020-12-24 23:42:33 +01:00
else if ($el.hasClass('time') || $el.attr('type') === 'time') {
2019-06-08 17:20:51 +02:00
$el.attr('readonly', 'true');
2020-12-24 23:42:33 +01:00
$el
.timepicker(
$.extend(
{
snapToStep: true,
icons: {
up: 'fas fa-chevron-up',
down: 'fas fa-chevron-down',
},
},
$el.data(),
),
)
.on('show.timepicker', (e) => {
const $el = $(e.currentTarget);
const $dropdown = $Body.find('.bootstrap-timepicker-widget');
if (!$dropdown.find('[data-action="clear"]').length) {
$dropdown
.find('tbody')
.append(
'<tr><td colspan="5"><a href="#" data-action="clear">Clear</a></td></tr>',
);
}
const $clearBtn = $dropdown.find('[data-action="clear"]');
$clearBtn.on('click', (e) => {
e.preventDefault();
$el.timepicker('clear');
$el.timepicker('hideWidget');
});
});
2019-06-08 17:20:51 +02:00
}
}
static dispose() {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: dispose`);
2019-06-08 17:20:51 +02:00
}
static _jQueryInterface() {
2020-12-24 23:42:33 +01:00
return this.each(function () {
2019-06-08 17:20:51 +02:00
// attach functionality to element
const $el = $(this);
let data = $el.data(DATA_KEY);
if (!data) {
data = new DatetimeUI(this);
$el.data(DATA_KEY, data);
}
});
}
}
// jQuery interface
$.fn[NAME] = DatetimeUI._jQueryInterface;
$.fn[NAME].Constructor = DatetimeUI;
2020-12-24 23:42:33 +01:00
$.fn[NAME].noConflict = function () {
2019-06-08 17:20:51 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return DatetimeUI._jQueryInterface;
};
// auto-apply
2020-09-09 17:40:58 +02:00
$(window).on(`${NAME}.init ${Events.AJAX} ${Events.LOADED}`, () => {
2020-12-24 23:42:33 +01:00
$(
'input.date, input.time,input[type="date"], input[type="time"]',
).jsDatetimeUI();
2019-06-08 17:20:51 +02:00
});
return DatetimeUI;
})($);
export default DatetimeUI;