2021-08-18 20:51:15 +02:00
|
|
|
"use strict";
|
2020-12-24 23:42:33 +01:00
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
import $ from "jquery";
|
2019-06-08 17:20:51 +02:00
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
import Events from "../_events";
|
2019-06-08 17:20:51 +02:00
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
import "bootstrap-datepicker/dist/js/bootstrap-datepicker.js";
|
|
|
|
import "bootstrap-timepicker/js/bootstrap-timepicker.js";
|
2019-06-08 17:20:51 +02:00
|
|
|
|
|
|
|
const DatetimeUI = (($) => {
|
|
|
|
// Constants
|
|
|
|
const W = window;
|
|
|
|
const D = document;
|
2021-08-18 20:51:15 +02:00
|
|
|
const $Body = $("body");
|
2019-06-08 17:20:51 +02:00
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
const NAME = "jsDatetimeUI";
|
2019-06-08 17:20:51 +02:00
|
|
|
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
|
2021-08-18 20:51:15 +02:00
|
|
|
if ($el.hasClass("date") || $el.attr("type") === "date") {
|
2020-12-24 23:42:33 +01:00
|
|
|
const defaultDate =
|
2021-08-18 20:51:15 +02:00
|
|
|
$el.attr("name").toLowerCase().indexOf("end") !== -1 ? "+4d" : "+3d";
|
2019-06-08 17:20:51 +02:00
|
|
|
|
2021-08-18 20:51:15 +02:00
|
|
|
$el.attr("readonly", "true");
|
2020-12-24 23:42:33 +01:00
|
|
|
$el.datepicker(
|
|
|
|
$.extend(
|
|
|
|
datepickerOptions,
|
|
|
|
{
|
|
|
|
defaultViewDate: defaultDate,
|
2021-08-18 20:51:15 +02:00
|
|
|
multidate: $el.data("multidate"),
|
2020-12-24 23:42:33 +01:00
|
|
|
},
|
2021-08-18 20:51:15 +02:00
|
|
|
$el.data()
|
|
|
|
)
|
2020-12-24 23:42:33 +01:00
|
|
|
);
|
|
|
|
}
|
2019-06-08 17:20:51 +02:00
|
|
|
|
|
|
|
// timepicker
|
2021-08-18 20:51:15 +02:00
|
|
|
else if ($el.hasClass("time") || $el.attr("type") === "time") {
|
|
|
|
$el.attr("readonly", "true");
|
2020-12-24 23:42:33 +01:00
|
|
|
$el
|
|
|
|
.timepicker(
|
|
|
|
$.extend(
|
|
|
|
{
|
|
|
|
snapToStep: true,
|
|
|
|
icons: {
|
2021-08-18 20:51:15 +02:00
|
|
|
up: "fas fa-chevron-up",
|
|
|
|
down: "fas fa-chevron-down",
|
2020-12-24 23:42:33 +01:00
|
|
|
},
|
|
|
|
},
|
2021-08-18 20:51:15 +02:00
|
|
|
$el.data()
|
|
|
|
)
|
2020-12-24 23:42:33 +01:00
|
|
|
)
|
2021-08-18 20:51:15 +02:00
|
|
|
.on("show.timepicker", (e) => {
|
2020-12-24 23:42:33 +01:00
|
|
|
const $el = $(e.currentTarget);
|
2021-08-18 20:51:15 +02:00
|
|
|
const $dropdown = $Body.find(".bootstrap-timepicker-widget");
|
2020-12-24 23:42:33 +01:00
|
|
|
|
|
|
|
if (!$dropdown.find('[data-action="clear"]').length) {
|
|
|
|
$dropdown
|
2021-08-18 20:51:15 +02:00
|
|
|
.find("tbody")
|
2020-12-24 23:42:33 +01:00
|
|
|
.append(
|
2021-08-18 20:51:15 +02:00
|
|
|
'<tr><td colspan="5"><a href="#" data-action="clear">Clear</a></td></tr>'
|
2020-12-24 23:42:33 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const $clearBtn = $dropdown.find('[data-action="clear"]');
|
2021-08-18 20:51:15 +02:00
|
|
|
$clearBtn.on("click", (e) => {
|
2020-12-24 23:42:33 +01:00
|
|
|
e.preventDefault();
|
2021-08-18 20:51:15 +02:00
|
|
|
$el.timepicker("clear");
|
|
|
|
$el.timepicker("hideWidget");
|
2020-12-24 23:42:33 +01:00
|
|
|
});
|
|
|
|
});
|
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
|
|
|
$(
|
2021-08-18 20:51:15 +02:00
|
|
|
'input.date, input.time,input[type="date"], input[type="time"]'
|
2020-12-24 23:42:33 +01:00
|
|
|
).jsDatetimeUI();
|
2019-06-08 17:20:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return DatetimeUI;
|
|
|
|
})($);
|
|
|
|
|
|
|
|
export default DatetimeUI;
|