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

200 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-06-08 17:20:51 +02:00
import $ from 'jquery';
2019-07-10 20:59:57 +02:00
import 'bootstrap-select/dist/js/bootstrap-select';
$.fn.selectpicker.Constructor.BootstrapVersion = '4';
2019-06-08 17:20:51 +02:00
import 'jquery.inputmask/dist/jquery.inputmask.bundle';
import Events from "../_events";
import SpinnerUI from './_ui.spinner';
import FormFieldUI from './_ui.form.fields';
const FormBasics = (($) => {
// Constants
const NAME = 'jsFormBasics';
const DATA_KEY = NAME;
const $Html = $('html, body');
2019-07-10 20:59:57 +02:00
const W = window;
const D = document;
2019-06-08 17:20:51 +02:00
class FormBasics {
constructor(element) {
const ui = this;
const $element = $(element);
ui._element = element;
$element.data(DATA_KEY, this);
$('[data-inputmask]').inputmask();
const $fields = $element.find(Events.FORM_FIELDS);
// init fields ui
$fields.each((i, el) => {
2019-07-10 20:59:57 +02:00
// skip some fields here
2019-06-08 17:20:51 +02:00
new FormFieldUI(el);
});
const $selectFields = $element.find('select:not([readonly])');
const $radioOptions = $element.find('input[type="radio"]');
const separator = '::;::';
$selectFields.each((i, el) => {
const $el = $(el);
const maxOptions = $el.data('max-options') || false;
$el.selectpicker($.extend({
iconBase: 'fas',
tickIcon: 'fa-check',
virtualScroll: false,
dropupAuto: false,
size: 10,
maxOptions,
}, $el.data(), {
multipleSeparator: separator,
}));
// wrap options
if (maxOptions > 1) {
2019-07-10 20:59:57 +02:00
const wrapOptions = () => {
2019-06-08 17:20:51 +02:00
if (!$el.val().length) {
return true;
}
2019-07-10 20:59:57 +02:00
const $container = $el.parent().find('.dropdown-toggle .filter-option');
2019-06-08 17:20:51 +02:00
const val = $container.text();
const vals = val.split(separator);
let html = '';
vals.forEach((opt) => {
const $opt = $el.find('option').filter((i, e) => {
return $(e).text() === opt;
});
html += `<span class="option" data-val=${ $opt.attr('value') }>${ opt
2019-07-10 20:59:57 +02:00
} <i class="fas fa-times btn-remove"></i></span>`;
2019-06-08 17:20:51 +02:00
});
$container.html(html);
// remove value
$container.find('.option').on('click', (e) => {
e.preventDefault();
const $opt = $(e.currentTarget);
const val = $opt.data('val').toString();
//$opt.remove();
const vals = $el.selectpicker('val');
const i = vals.indexOf(val);
if (i > -1) {
vals.splice(i, 1);
$el.selectpicker('val', vals);
}
2019-07-10 20:59:57 +02:00
wrapOptions();
});
};
2019-06-08 17:20:51 +02:00
2019-07-10 20:59:57 +02:00
$el.on('rendered.bs.select changed.bs.select refreshed.bs.select loaded.bs.select change', wrapOptions);
wrapOptions();
}
});
2019-06-08 17:20:51 +02:00
2019-07-10 20:59:57 +02:00
// FIX: missing conflicting 'bootstrap/js/dist/dropdown' with bootstrap-select/dist/js/bootstrap-select
$('[data-toggle="dropdown"]').on('click', (e) => {
$(e.currentTarget).siblings('.dropdown-menu').toggleClass('show');
2019-06-08 17:20:51 +02:00
});
2019-07-10 20:59:57 +02:00
$('.dropdown-menu a').on('click', (e) => {
$(e.currentTarget).parents('.dropdown-menu').removeClass('show');
});
// /FIX
2019-06-08 17:20:51 +02:00
$fields.each((e, el) => {
const $el = $(el);
if ($el.hasClass('required') || $el.attr('aria-required')) {
$el.closest('.field').addClass('required');
}
});
$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');
2019-07-10 20:59:57 +02:00
$parent.siblings('.radio').each((i, el) => {
const $el = $(el);
if (!$el.find('input').is(':checked')) {
$el.removeClass('checked');
}
});
2019-06-08 17:20:51 +02:00
if ($el.is(':checked')) {
$parent.addClass('checked');
}
});
$element.on('submit', (e) => {
SpinnerUI.show();
});
$element.addClass(`${NAME}-active`);
$element.trigger(Events.FORM_INIT_BASICS);
}
// Public methods
dispose() {
const $element = $(this._element);
$element.removeClass(`${NAME}-active`);
$.removeData(this._element, DATA_KEY);
this._element = null;
}
static _jQueryInterface() {
2019-07-10 20:59:57 +02:00
return this.each(() => {
2019-06-08 17:20:51 +02:00
// attach functionality to element
const $element = $(this);
let data = $element.data(DATA_KEY);
if (!data) {
data = new FormBasics(this);
$element.data(DATA_KEY, data);
}
});
}
}
// jQuery interface
$.fn[NAME] = FormBasics._jQueryInterface;
$.fn[NAME].Constructor = FormBasics;
$.fn[NAME].noConflict = function() {
$.fn[NAME] = JQUERY_NO_CONFLICT;
return FormBasics._jQueryInterface;
};
2019-07-10 20:59:57 +02:00
const init = () => {
$('form').jsFormBasics();
};
2019-06-08 17:20:51 +02:00
2019-07-10 20:59:57 +02:00
// auto-apply
$(W).on(`${Events.AJAX} ${Events.LOADED}`, () => {
init();
2019-06-08 17:20:51 +02:00
});
return FormBasics;
})($);
export default FormBasics;