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

217 lines
4.7 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-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
import FormBasics from './_ui.form.basics'
2019-11-27 06:29:20 +01:00
2019-07-10 21:00:20 +02:00
const FormToggleUI = (($) => {
// Constants
2022-05-03 20:50:57 +02:00
const NAME = 'jsFormToggleUI'
const DATA_KEY = NAME
const W = window
const $Html = $('html, body')
const FieldUI = 'jsFormFieldUI'
2019-07-10 21:00:20 +02:00
class FormToggleUI {
2022-05-03 20:50:57 +02:00
constructor ($el) {
const ui = this
ui.$el = $el
2019-11-07 17:35:19 +01:00
if (!ui.getCondition()) {
2022-05-03 20:50:57 +02:00
console.warn(`${NAME}: no condition found`)
return
2019-10-20 01:40:40 +02:00
}
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
ui.toggle()
2019-07-10 21:00:20 +02:00
2020-07-24 20:13:25 +02:00
ui.$el.on(`change shown.${FieldUI} hidden.${FieldUI}`, (e) => {
2022-05-03 20:50:57 +02:00
ui.toggle()
})
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
ui.$el.addClass(`${NAME}-active`)
ui.$el.data(DATA_KEY, ui)
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
return ui
2019-07-10 21:00:20 +02:00
}
2022-05-03 20:50:57 +02:00
getDataEl () {
const ui = this
const $el = ui.$el
2019-07-10 21:00:20 +02:00
2020-07-24 20:13:25 +02:00
return $el.is('[type="radio"],[type="checkbox"]') &&
2022-05-03 20:50:57 +02:00
$el.parents('.optionset,.checkboxset').length
? $el.parents('.optionset,.checkboxset')
: $el
2019-11-07 17:35:19 +01:00
}
2022-05-03 20:50:57 +02:00
getCondition () {
const ui = this
2019-11-07 17:35:19 +01:00
2022-05-03 20:50:57 +02:00
return ui.getDataEl().data('value-toggle')
2019-11-07 17:35:19 +01:00
}
2022-05-03 20:50:57 +02:00
getCurrentVal () {
const ui = this
const $el = ui.$el
2019-11-07 17:35:19 +01:00
2022-05-03 20:50:57 +02:00
if ($el.attr('type') === 'checkbox') {
if ($el.parents('.checkboxset').length && $el.is(':checked')) {
return $el.val()
2019-11-27 06:29:20 +01:00
}
2022-05-03 20:50:57 +02:00
return !!$el.is(':checked')
2019-11-27 06:29:20 +01:00
}
2022-05-03 20:50:57 +02:00
if ($el.attr('type') === 'radio') {
return $Html.find(`[name="${$el.attr('name')}"]:checked`).val()
2019-11-27 06:29:20 +01:00
}
2022-05-03 20:50:57 +02:00
return $el.val()
2019-11-07 17:35:19 +01:00
}
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
getTrueTarget () {
const ui = this
const $dataEl = ui.getDataEl()
2019-11-07 17:35:19 +01:00
2019-11-27 06:29:20 +01:00
// compatibility params
2022-05-03 20:50:57 +02:00
const target = $dataEl.data('value-toggle-yes')
2019-11-07 17:35:19 +01:00
if (!target || !target.length) {
2022-05-03 20:50:57 +02:00
return ui.getElement($dataEl.data('target'))
2019-11-07 17:35:19 +01:00
}
2022-05-03 20:50:57 +02:00
return ui.getElement(target)
2019-11-07 17:35:19 +01:00
}
2022-05-03 20:50:57 +02:00
getFalseTarget () {
const ui = this
const $dataEl = ui.getDataEl()
2019-11-27 06:29:20 +01:00
// compatibility params
2022-05-03 20:50:57 +02:00
const target = $dataEl.data('value-toggle-no')
2019-11-27 06:29:20 +01:00
if (!target || !target.length) {
2022-05-03 20:50:57 +02:00
return ui.getElement($dataEl.data('value-toggle-false'))
2019-11-27 06:29:20 +01:00
}
2019-11-07 17:35:19 +01:00
2022-05-03 20:50:57 +02:00
return ui.getElement(target)
2019-11-07 17:35:19 +01:00
}
2022-05-03 20:50:57 +02:00
getElement (target) {
return target && target.length && $(target).length ? $(target) : false
2019-11-07 17:35:19 +01:00
}
2022-05-03 20:50:57 +02:00
toggle () {
const ui = this
const $el = ui.$el
2019-11-07 17:35:19 +01:00
2022-05-03 20:50:57 +02:00
const val = ui.getCurrentVal()
const $dataEl = ui.getDataEl()
2019-07-10 21:00:20 +02:00
2019-11-27 06:29:20 +01:00
// conditional toggler
2022-05-03 20:50:57 +02:00
const condition = ui.getCondition()
2019-10-20 01:40:40 +02:00
if (!condition) {
2022-05-03 20:50:57 +02:00
return
2019-10-20 01:40:40 +02:00
}
// yes/no toggler
2020-07-24 20:13:25 +02:00
const yesNoVal =
2022-05-03 20:50:57 +02:00
!!((condition === true && val && val !== '' && val !== '0') ||
condition === val)
2019-10-20 01:40:40 +02:00
2022-05-03 20:50:57 +02:00
const $yesTarget = ui.getTrueTarget()
const $noTarget = ui.getFalseTarget()
const elUI = $el.data(FieldUI)
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
if ((elUI && !elUI.shown) || typeof val === 'undefined') {
ui.toggleElement($yesTarget, false)
ui.toggleElement($noTarget, false)
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
return
2019-07-10 21:00:20 +02:00
}
if (yesNoVal) {
2022-05-03 20:50:57 +02:00
ui.toggleElement($yesTarget, true)
ui.toggleElement($noTarget, false)
2019-07-10 21:00:20 +02:00
} else {
2022-05-03 20:50:57 +02:00
ui.toggleElement($yesTarget, false)
ui.toggleElement($noTarget, true)
2019-07-10 21:00:20 +02:00
}
}
2022-05-03 20:50:57 +02:00
toggleElement ($el, show) {
2019-10-20 01:40:40 +02:00
if (!$el.length) {
2022-05-03 20:50:57 +02:00
return
2019-10-20 01:40:40 +02:00
}
2022-05-03 20:50:57 +02:00
const ui = this
const action = show ? 'show' : 'hide'
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
$el.filter('div.collapse').each((i, el) => {
const $el = $(el)
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
$el.collapse(action)
})
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
$el.trigger(`${action}.${NAME}`)
2019-07-10 21:00:20 +02:00
}
2022-05-03 20:50:57 +02:00
dispose () {
const ui = this
const $el = ui.$el
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
$el.removeClass(`${NAME}-active`)
$.removeData(this._el, DATA_KEY)
this._el = null
2019-07-10 21:00:20 +02:00
}
2022-05-03 20:50:57 +02:00
static _jQueryInterface () {
2019-07-10 21:00:20 +02:00
return this.each((i, el) => {
// attach functionality to el
2022-05-03 20:50:57 +02:00
const $el = $(el)
let data = $el.data(DATA_KEY)
2019-07-10 21:00:20 +02:00
if (!data) {
2022-05-03 20:50:57 +02:00
data = new FormToggleUI($el)
$el.data(DATA_KEY, data)
2019-07-10 21:00:20 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-07-10 21:00:20 +02:00
}
2022-05-03 20:50:57 +02:00
static validate () {
2019-07-10 21:00:20 +02:00
return $(Events.FORM_FIELDS).each((i, el) => {
2022-05-03 20:50:57 +02:00
const $el = $(el)
const name = $el.attr('name')
2019-07-10 21:00:20 +02:00
2020-07-24 20:13:25 +02:00
if ($(`[name="${name}"]`).length > 1) {
console.warn(
2021-08-18 20:51:15 +02:00
`${NAME}: Module malfunction duplicate "${name}" elements found`
2022-05-03 20:50:57 +02:00
)
2019-07-10 21:00:20 +02:00
}
2022-05-03 20:50:57 +02:00
})
2019-07-10 21:00:20 +02:00
}
}
// jQuery interface
2022-05-03 20:50:57 +02:00
$.fn[NAME] = FormToggleUI._jQueryInterface
$.fn[NAME].Constructor = FormToggleUI
2020-07-24 20:13:25 +02:00
$.fn[NAME].noConflict = function () {
2022-05-03 20:50:57 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return FormToggleUI._jQueryInterface
}
2019-07-10 21:00:20 +02:00
// auto-apply
2020-07-24 20:13:25 +02:00
$(W).on(`${Events.LODEDANDREADY}`, () => {
2022-05-03 20:50:57 +02:00
// FormToggleUI.validate();
$(Events.FORM_FIELDS).filter('[data-value-toggle]').jsFormToggleUI()
2019-11-27 06:29:20 +01:00
2022-05-03 20:50:57 +02:00
$('[data-value-toggle]')
2019-10-20 01:40:40 +02:00
.not(Events.FORM_FIELDS)
.find(Events.FORM_FIELDS)
2022-05-03 20:50:57 +02:00
.jsFormToggleUI()
})
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
return FormToggleUI
})($)
2019-07-10 21:00:20 +02:00
2022-05-03 20:50:57 +02:00
export default FormToggleUI