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

146 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-08-18 20:51:15 +02:00
"use strict";
2020-12-24 23:42:33 +01:00
2021-08-18 20:51:15 +02:00
import $ from "jquery";
2019-06-08 17:20:51 +02:00
2021-08-18 20:51:15 +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
2021-08-18 20:51:15 +02:00
const NAME = "jsFormBasics";
2020-02-25 07:17:22 +01:00
const DATA_KEY = NAME;
2021-08-18 20:51:15 +02:00
const $Html = $("html, body");
2020-02-25 07:17:22 +01:00
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);
});
$fields.each((e, el) => {
const $el = $(el);
2021-08-18 20:51:15 +02:00
if ($el.hasClass("required") || $el.attr("aria-required")) {
$el.closest(".field").addClass("required");
2020-02-25 07:17:22 +01:00
}
});
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);
2021-08-18 20:51:15 +02:00
if ($el.is(":checked")) {
$el.parents(".radio").addClass("checked");
2020-02-25 07:17:22 +01:00
}
});
2021-08-18 20:51:15 +02:00
$radioOptions.on("change", (e) => {
2020-02-25 07:17:22 +01:00
const $el = $(e.currentTarget);
2021-08-18 20:51:15 +02:00
const $parent = $el.parents(".radio");
2020-02-25 07:17:22 +01:00
2021-08-18 20:51:15 +02:00
$parent.siblings(".radio").each((i, el) => {
2020-02-25 07:17:22 +01:00
const $el = $(el);
2021-08-18 20:51:15 +02:00
if (!$el.find("input").is(":checked")) {
$el.removeClass("checked");
2020-02-25 07:17:22 +01:00
}
});
2021-08-18 20:51:15 +02:00
if ($el.is(":checked")) {
$parent.addClass("checked");
2020-02-25 07:17:22 +01:00
}
});
2021-08-18 20:51:15 +02:00
$el.on("submit", (e) => {
2020-05-13 18:05:34 +02:00
setTimeout(() => {
2021-08-18 20:51:15 +02:00
if (!$el.find(".error").length) {
2020-05-13 18:05:34 +02:00
SpinnerUI.show();
}
}, 100);
2020-02-25 07:17:22 +01:00
});
2021-08-18 20:51:15 +02:00
$(".field.password .show-password").on("click", (e) => {
console.log(`${NAME}: .field.password .show-password (click)`);
const $el = $(e.currentTarget);
2021-08-18 20:51:15 +02:00
const $field = $el.siblings("input");
const $icon = $el.find(".fas");
const attr = $field.attr("type");
2021-08-18 20:51:15 +02:00
if (attr === "password") {
$field.attr("type", "text");
$icon.removeClass("fa-eye").addClass("fa-eye-slash");
} else {
2021-08-18 20:51:15 +02:00
$field.attr("type", "password");
$icon.removeClass("fa-eye-slash").addClass("fa-eye");
}
});
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);
$.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 = () => {
2021-08-18 20:51:15 +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}`, () => {
2020-02-25 07:17:22 +01:00
init();
});
return FormBasics;
2019-06-08 17:20:51 +02:00
})($);
export default FormBasics;