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

153 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-06-08 17:20:51 +02:00
import $ from 'jquery';
2019-08-12 11:23:24 +02:00
//import 'bootstrap-select/dist/js/bootstrap-select';
//$.fn.selectpicker.Constructor.BootstrapVersion = '4';
2020-02-07 20:49:38 +01:00
import select2 from 'select2/dist/js/select2.js';
2020-02-24 16:16:06 +01:00
//import Inputmask from 'inputmask';
2019-07-10 20:59:57 +02:00
2020-02-07 20:49:38 +01:00
//import select2 from 'jquery.inputmask/dist/jquery.inputmask.bundle';
2019-06-08 17:20:51 +02:00
2020-02-07 20:49:38 +01:00
import Events from '../_events';
2019-06-08 17:20:51 +02:00
import SpinnerUI from './_ui.spinner';
import FormFieldUI from './_ui.form.fields';
2020-02-25 07:17:22 +01:00
const FormBasics = (($) => {
// Constants
const NAME = 'jsFormBasics';
const DATA_KEY = NAME;
const $Html = $('html, body');
const W = window;
const D = document;
class FormBasics {
constructor(el) {
const ui = this;
const $el = $(el);
ui._el = el;
2020-06-25 11:53:52 +02:00
ui.dispose();
console.log(`${NAME}: init`);
2020-02-25 07:17:22 +01:00
$el.data(DATA_KEY, this);
//$('[data-inputmask]').inputmask();
const $fields = $el.find(Events.FORM_FIELDS);
// init fields ui
$fields.each((i, el) => {
// skip some fields here
new FormFieldUI(el);
});
2020-06-25 11:53:52 +02:00
const $selectFields = $el
.find('select:not([readonly])')
.not('.no-select2');
2020-02-25 07:17:22 +01:00
$selectFields.each((i, el) => {
$(el).select2();
});
$fields.each((e, el) => {
const $el = $(el);
if ($el.hasClass('required') || $el.attr('aria-required')) {
$el.closest('.field').addClass('required');
}
});
2020-07-20 09:15:57 +02:00
const $radioOptions = $el.find('input[type="radio"]');
2020-02-25 07:17:22 +01:00
$radioOptions.each((e, el) => {
const $el = $(el);
if ($el.is(':checked')) {
$el.parents('.radio').addClass('checked');
}
});
$radioOptions.on('change', (e) => {
const $el = $(e.currentTarget);
const $parent = $el.parents('.radio');
$parent.siblings('.radio').each((i, el) => {
const $el = $(el);
if (!$el.find('input').is(':checked')) {
$el.removeClass('checked');
}
});
if ($el.is(':checked')) {
$parent.addClass('checked');
}
});
$el.on('submit', (e) => {
2020-05-13 18:05:34 +02:00
setTimeout(() => {
if (!$el.find('.error').length) {
SpinnerUI.show();
}
}, 100);
2020-02-25 07:17:22 +01:00
});
$el.addClass(`${NAME}-active`);
$el.trigger(Events.FORM_INIT_BASICS);
}
// Public methods
dispose() {
2020-06-25 11:53:52 +02:00
console.log(`${NAME}: dispose`);
const ui = this;
const $el = $(ui._el);
2020-02-25 07:17:22 +01:00
2020-06-25 11:53:52 +02:00
const $selectFields = $el
.find('select:not([readonly])')
.not('.no-select2');
2020-02-25 07:17:22 +01:00
$selectFields.each((i, el) => {
2020-06-25 11:53:52 +02:00
const $el = $(el);
if ($el.hasClass('select2-hidden-accessible')) {
$el.select2('destroy');
}
2020-02-25 07:17:22 +01:00
});
2020-06-25 11:53:52 +02:00
$.removeData(ui._el, DATA_KEY);
ui._el = null;
2020-02-25 07:17:22 +01:00
$el.removeClass(`${NAME}-active`);
}
static _jQueryInterface() {
return this.each(() => {
// attach functionality to el
const $el = $(this);
let data = $el.data(DATA_KEY);
if (!data) {
data = new FormBasics(this);
$el.data(DATA_KEY, data);
}
});
}
}
// jQuery interface
$.fn[NAME] = FormBasics._jQueryInterface;
$.fn[NAME].Constructor = FormBasics;
2020-06-25 11:53:52 +02:00
$.fn[NAME].noConflict = function () {
2020-02-25 07:17:22 +01:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return FormBasics._jQueryInterface;
};
const init = () => {
$('form').jsFormBasics();
};
// auto-apply
$(W).on(`${Events.AJAX} ${Events.LOADED}`, () => {
init();
});
return FormBasics;
2019-06-08 17:20:51 +02:00
})($);
export default FormBasics;