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

129 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2020-12-24 23:42:33 +01:00
2022-05-03 20:50:57 +02:00
import $ from 'jquery'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
import Events from '../_events'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +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
2022-05-03 20:50:57 +02:00
const W = window
const D = document
const $Body = $('body')
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
const NAME = 'jsDatetimeUI'
const DATA_KEY = NAME
2019-06-08 17:20:51 +02:00
const datepickerOptions = {
autoclose: true,
startDate: 0,
2022-05-03 20:50:57 +02:00
// todayBtn: true,
2019-06-08 17:20:51 +02:00
todayHighlight: true,
clearBtn: true,
2022-05-03 20:50:57 +02:00
}
2019-06-08 17:20:51 +02:00
class DatetimeUI {
2022-05-03 20:50:57 +02:00
constructor (el) {
console.log(`${NAME}: init`)
2020-09-09 17:40:58 +02:00
2022-05-03 20:50:57 +02:00
const ui = this
const $el = $(el)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
ui._el = el
2019-06-08 17:20:51 +02:00
// datepicker
2022-05-03 20:50:57 +02:00
if ($el.hasClass('date') || $el.attr('type') === 'date') {
2020-12-24 23:42:33 +01:00
const defaultDate =
2022-05-03 20:50:57 +02:00
$el.attr('name').toLowerCase().indexOf('end') !== -1 ? '+4d' : '+3d'
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
$el.attr('readonly', 'true')
2020-12-24 23:42:33 +01:00
$el.datepicker(
$.extend(
datepickerOptions,
{
defaultViewDate: defaultDate,
2022-05-03 20:50:57 +02:00
multidate: $el.data('multidate'),
2020-12-24 23:42:33 +01:00
},
2021-08-18 20:51:15 +02:00
$el.data()
)
2022-05-03 20:50:57 +02:00
)
2020-12-24 23:42:33 +01:00
}
2019-06-08 17:20:51 +02:00
// timepicker
2022-05-03 20:50:57 +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: {
2022-05-03 20:50:57 +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
)
2022-05-03 20:50:57 +02:00
.on('show.timepicker', (e) => {
const $el = $(e.currentTarget)
const $dropdown = $Body.find('.bootstrap-timepicker-widget')
2020-12-24 23:42:33 +01:00
if (!$dropdown.find('[data-action="clear"]').length) {
$dropdown
2022-05-03 20:50:57 +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>'
2022-05-03 20:50:57 +02:00
)
2020-12-24 23:42:33 +01:00
}
2022-05-03 20:50:57 +02:00
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
}
}
2022-05-03 20:50:57 +02:00
static dispose () {
console.log(`${NAME}: dispose`)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +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
2022-05-03 20:50:57 +02:00
const $el = $(this)
let data = $el.data(DATA_KEY)
2019-06-08 17:20:51 +02:00
if (!data) {
2022-05-03 20:50:57 +02:00
data = new DatetimeUI(this)
$el.data(DATA_KEY, data)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-06-08 17:20:51 +02:00
}
}
// jQuery interface
2022-05-03 20:50:57 +02:00
$.fn[NAME] = DatetimeUI._jQueryInterface
$.fn[NAME].Constructor = DatetimeUI
2020-12-24 23:42:33 +01:00
$.fn[NAME].noConflict = function () {
2022-05-03 20:50:57 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return DatetimeUI._jQueryInterface
}
2019-06-08 17:20:51 +02:00
// 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"]'
2022-05-03 20:50:57 +02:00
).jsDatetimeUI()
})
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return DatetimeUI
})($)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
export default DatetimeUI