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

169 lines
3.4 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'
import Events from '../_events'
2019-06-08 17:20:51 +02:00
const FormFieldUI = (($) => {
// Constants
2022-05-03 20:50:57 +02:00
const NAME = 'jsFormFieldUI'
const DATA_KEY = NAME
const $Html = $('html, body')
2019-06-08 17:20:51 +02:00
class FormFieldUI {
2022-05-03 20:50:57 +02:00
constructor (el) {
const ui = this
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
ui.$el = $(el)
ui.shown = true
2019-11-27 06:29:20 +01:00
2022-05-03 20:50:57 +02:00
ui.$el.data(DATA_KEY, ui)
// ui.$actions = ui.$el.parents('form').children('.btn-toolbar,.form-actions');
2019-06-08 17:20:51 +02:00
ui.vals = {
2020-12-24 23:42:33 +01:00
val: ui.$el.val(),
2022-05-03 20:50:57 +02:00
checked: ui.$el.is(':checked'),
}
2019-06-08 17:20:51 +02:00
2019-07-10 20:59:57 +02:00
// bootstrap collapse integration
2022-05-03 20:50:57 +02:00
ui.$el.parents('.optionset').not('.field').removeClass('collapse')
2020-12-24 23:42:33 +01:00
ui.$collapse = ui.$el
2022-05-03 20:50:57 +02:00
.parents('.field.collapse')
.not('.composite')
.first()
2019-07-10 20:59:57 +02:00
if (ui.$collapse.length) {
2022-05-03 20:50:57 +02:00
ui.$el.removeClass('collapse')
2019-07-10 20:59:57 +02:00
2022-05-03 20:50:57 +02:00
ui.$collapse.on('show.bs.collapse', (e) => {
ui.show()
})
2019-07-10 20:59:57 +02:00
2022-05-03 20:50:57 +02:00
ui.$collapse.on('hidden.bs.collapse', (e) => {
ui.hide()
})
2019-07-10 20:59:57 +02:00
}
2022-05-03 20:50:57 +02:00
ui.$el.addClass(`${NAME}-active`)
2019-07-10 20:59:57 +02:00
2022-05-03 20:50:57 +02:00
return ui
2019-06-08 17:20:51 +02:00
}
// Public methods
2022-05-03 20:50:57 +02:00
dispose () {
const ui = this
const $el = ui.$el
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
$el.removeClass(`${NAME}-active`)
$.removeData($el[0], DATA_KEY)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
show () {
const ui = this
const $el = ui.$el
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
ui.restore()
ui.shown = true
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
/* if (ui.$collapse.length) {
2019-07-10 20:59:57 +02:00
ui.$collapse.collapse('show');
}
2019-06-08 17:20:51 +02:00
if ($el.hasClass('collapse')) {
$el.collapse('show');
2022-05-03 20:50:57 +02:00
} */
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
$el.trigger(`shown.${NAME}`)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
hide () {
const ui = this
const $el = ui.$el
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
ui.wipe()
ui.shown = false
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
/* if (ui.$collapse.length) {
2019-07-10 20:59:57 +02:00
ui.$collapse.collapse('hide');
}
2019-06-08 17:20:51 +02:00
if ($el.hasClass('collapse')) {
$el.collapse('hide');
}
2022-05-03 20:50:57 +02:00
$el.trigger('change'); */
$el.trigger(`hidden.${NAME}`)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
wipe () {
const ui = this
const $el = ui.$el
2019-06-08 17:20:51 +02:00
ui.vals = {
2022-05-03 20:50:57 +02:00
name: $el.attr('name'),
2020-12-24 23:42:33 +01:00
val: $el.val(),
2022-05-03 20:50:57 +02:00
checked: $el.is(':checked'),
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
$el.val('')
$el.prop('checked', false)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
restore () {
const ui = this
const $el = ui.$el
const checked = ui.vals.checked
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
$el.val(ui.vals.val)
$el.prop('checked', checked)
2019-06-08 17:20:51 +02:00
}
2022-05-03 20:50:57 +02:00
addMessage (msg, type = null, scrollTo = true) {
const ui = this
const $field = ui.$el.closest('.field')
2022-05-03 20:50:57 +02:00
$field.addClass('has-message')
if (msg) {
2022-05-03 20:50:57 +02:00
$field.append(`<div class="message alert ${type}">${msg}</div>`)
}
if (scrollTo) {
2022-05-03 20:50:57 +02:00
const pos = $field.offset().top
$field.focus()
$Html.scrollTop(pos - 100)
}
}
2022-05-03 20:50:57 +02:00
removeMessages () {
const ui = this
const $field = ui.$el.closest('.field')
2022-05-03 20:50:57 +02:00
$field.removeClass('has-message')
$field.find('.message').remove()
}
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 el
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 FormFieldUI(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] = FormFieldUI._jQueryInterface
$.fn[NAME].Constructor = FormFieldUI
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 FormFieldUI._jQueryInterface
}
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
return FormFieldUI
})($)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
export default FormFieldUI