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

146 lines
3.3 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'
import SpinnerUI from './_ui.spinner'
import FormFieldUI from './_ui.form.fields'
2019-06-08 17:20:51 +02:00
2020-02-25 07:17:22 +01:00
const FormBasics = (($) => {
// Constants
2022-05-03 20:50:57 +02:00
const NAME = 'jsFormBasics'
const DATA_KEY = NAME
const $Html = $('html, body')
const W = window
const D = document
2020-02-25 07:17:22 +01:00
class FormBasics {
2022-05-03 20:50:57 +02:00
constructor (el) {
const ui = this
const $el = $(el)
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
ui._el = el
ui.dispose()
2020-06-25 11:53:52 +02:00
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: init`)
$el.data(DATA_KEY, this)
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
// $('[data-inputmask]').inputmask();
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
const $fields = $el.find(Events.FORM_FIELDS)
2020-02-25 07:17:22 +01:00
// init fields ui
$fields.each((i, el) => {
// skip some fields here
2022-05-03 20:50:57 +02:00
new FormFieldUI(el)
})
2020-02-25 07:17:22 +01:00
$fields.each((e, el) => {
2022-05-03 20:50:57 +02:00
const $el = $(el)
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
if ($el.hasClass('required') || $el.attr('aria-required')) {
$el.closest('.field').addClass('required')
2020-02-25 07:17:22 +01:00
}
2022-05-03 20:50:57 +02:00
})
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
const $radioOptions = $el.find('input[type="radio"]')
2020-02-25 07:17:22 +01:00
$radioOptions.each((e, el) => {
2022-05-03 20:50:57 +02:00
const $el = $(el)
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
if ($el.is(':checked')) {
$el.parents('.radio').addClass('checked')
2020-02-25 07:17:22 +01:00
}
2022-05-03 20:50:57 +02:00
})
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
$radioOptions.on('change', (e) => {
const $el = $(e.currentTarget)
const $parent = $el.parents('.radio')
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
$parent.siblings('.radio').each((i, el) => {
const $el = $(el)
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
if (!$el.find('input').is(':checked')) {
$el.removeClass('checked')
2020-02-25 07:17:22 +01:00
}
2022-05-03 20:50:57 +02:00
})
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
if ($el.is(':checked')) {
$parent.addClass('checked')
2020-02-25 07:17:22 +01:00
}
2022-05-03 20:50:57 +02:00
})
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
$el.on('submit', (e) => {
2020-05-13 18:05:34 +02:00
setTimeout(() => {
2022-05-03 20:50:57 +02:00
if (!$el.find('.error').length) {
SpinnerUI.show()
2020-05-13 18:05:34 +02:00
}
2022-05-03 20:50:57 +02:00
}, 100)
})
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
$('.field.password .show-password').on('click', (e) => {
console.log(`${NAME}: .field.password .show-password (click)`)
2022-05-03 20:50:57 +02:00
const $el = $(e.currentTarget)
const $field = $el.siblings('input')
const $icon = $el.find('.fas')
const attr = $field.attr('type')
2022-05-03 20:50:57 +02:00
if (attr === 'password') {
$field.attr('type', 'text')
$icon.removeClass('fa-eye').addClass('fa-eye-slash')
} else {
2022-05-03 20:50:57 +02:00
$field.attr('type', 'password')
$icon.removeClass('fa-eye-slash').addClass('fa-eye')
}
2022-05-03 20:50:57 +02:00
})
2022-05-03 20:50:57 +02:00
$el.addClass(`${NAME}-active`)
$el.trigger(Events.FORM_INIT_BASICS)
2020-02-25 07:17:22 +01:00
}
// Public methods
2022-05-03 20:50:57 +02:00
dispose () {
console.log(`${NAME}: dispose`)
const ui = this
const $el = $(ui._el)
$.removeData(ui._el, DATA_KEY)
ui._el = null
$el.removeClass(`${NAME}-active`)
2020-02-25 07:17:22 +01:00
}
2022-05-03 20:50:57 +02:00
static _jQueryInterface () {
2020-02-25 07:17:22 +01:00
return this.each(() => {
// attach functionality to el
2022-05-03 20:50:57 +02:00
const $el = $(this)
let data = $el.data(DATA_KEY)
2020-02-25 07:17:22 +01:00
if (!data) {
2022-05-03 20:50:57 +02:00
data = new FormBasics(this)
$el.data(DATA_KEY, data)
2020-02-25 07:17:22 +01:00
}
2022-05-03 20:50:57 +02:00
})
2020-02-25 07:17:22 +01:00
}
}
// jQuery interface
2022-05-03 20:50:57 +02:00
$.fn[NAME] = FormBasics._jQueryInterface
$.fn[NAME].Constructor = FormBasics
2020-06-25 11:53:52 +02:00
$.fn[NAME].noConflict = function () {
2022-05-03 20:50:57 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT
return FormBasics._jQueryInterface
}
2020-02-25 07:17:22 +01:00
const init = () => {
2022-05-03 20:50:57 +02:00
$('form').jsFormBasics()
}
2020-02-25 07:17:22 +01:00
// auto-apply
2020-09-09 17:40:58 +02:00
$(W).on(`${NAME}.init ${Events.AJAX} ${Events.LOADED}`, () => {
2022-05-03 20:50:57 +02:00
init()
})
2020-02-25 07:17:22 +01:00
2022-05-03 20:50:57 +02:00
return FormBasics
})($)
2019-06-08 17:20:51 +02:00
2022-05-03 20:50:57 +02:00
export default FormBasics