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

155 lines
3.6 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";
import Events from "../_events";
2019-06-08 17:20:51 +02:00
const FormStorage = (($) => {
// Constants
2021-08-18 20:51:15 +02:00
const NAME = "jsFormStorage";
2019-06-08 17:20:51 +02:00
const DATA_KEY = NAME;
const STORAGE = window.localStorage;
class FormStorage {
constructor(element) {
2020-09-09 17:40:58 +02:00
console.log(`${NAME}: init`);
2019-06-08 17:20:51 +02:00
const ui = this;
const $element = $(element);
2021-08-18 20:51:15 +02:00
const $elements = $element.find("input, textarea, select");
2019-06-08 17:20:51 +02:00
2020-12-24 23:42:33 +01:00
const setRangeValues = function (el) {
2019-06-08 17:20:51 +02:00
const $el = $(el);
2021-08-18 20:51:15 +02:00
$el.siblings(".value").text($el.val());
2019-06-08 17:20:51 +02:00
};
ui._element = element;
$element.data(DATA_KEY, this);
$element.addClass(`${NAME}-active`);
// restore form data from localStorage
$elements.each((i, el) => {
const $el = $(el);
2021-08-18 20:51:15 +02:00
const id = $el.attr("id");
const type = $el.attr("type");
2019-06-08 17:20:51 +02:00
const val = STORAGE.getItem(NAME + id);
2021-08-18 20:51:15 +02:00
if (type === "file") {
2019-06-08 17:20:51 +02:00
return true;
}
if (id && val && type) {
2021-08-18 20:51:15 +02:00
if (type && (type === "checkbox" || type === "radio")) {
$el.prop("checked", val);
2019-06-08 17:20:51 +02:00
} else {
$el.val(val);
}
}
$el.trigger(Events.RESTORE_FIELD);
});
// range fields
$('input[type="range"]').each((i, el) => {
setRangeValues(el);
});
$element.trigger(Events.RESTORE_FIELD);
2021-08-18 20:51:15 +02:00
$('input[type="range"]').on("change", (e) => {
2019-06-08 17:20:51 +02:00
setRangeValues(e.currentTarget);
});
// store form data into localStorage
2021-08-18 20:51:15 +02:00
$elements.on("change", (e) => {
2019-06-08 17:20:51 +02:00
const $el = $(e.currentTarget);
2021-08-18 20:51:15 +02:00
const id = $el.attr("id");
const type = $el.attr("type");
2019-06-08 17:20:51 +02:00
// skip some elements
2021-08-18 20:51:15 +02:00
if ($el.hasClass("no-storage")) {
2019-06-08 17:20:51 +02:00
return true;
}
let val = $el.val();
2021-08-18 20:51:15 +02:00
if (type && (type === "checkbox" || type === "radio")) {
val = !!$el.is(":checked");
2019-06-08 17:20:51 +02:00
}
2021-08-18 20:51:15 +02:00
if (id && type && type !== "password") {
2019-06-08 17:20:51 +02:00
STORAGE.setItem(NAME + id, val);
}
});
2021-08-18 20:51:15 +02:00
$element.on("submit", () => {
2019-06-08 17:20:51 +02:00
$element.data(DATA_KEY).clear();
});
2020-12-24 23:42:33 +01:00
$element
2021-08-18 20:51:15 +02:00
.find(".btn-toolbar,.form-actions")
2020-12-24 23:42:33 +01:00
.children('button,[type="submit"],[type="clear"]')
2021-08-18 20:51:15 +02:00
.on("click", () => {
2020-12-24 23:42:33 +01:00
$element.data(DATA_KEY).clear();
});
2019-06-08 17:20:51 +02:00
$element.addClass(`${NAME}-active`);
$element.trigger(Events.FORM_INIT_STORAGE);
}
// Public methods
dispose() {
const $element = $(this._element);
$element.removeClass(`${NAME}-active`);
$.removeData(this._element, DATA_KEY);
this._element = null;
}
clear() {
STORAGE.clear();
}
static _jQueryInterface() {
2021-08-18 20:51:15 +02:00
if (typeof window.localStorage !== "undefined") {
2020-12-24 23:42:33 +01:00
return this.each(function () {
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 FormStorage(this);
$element.data(DATA_KEY, data);
}
});
}
}
}
// jQuery interface
$.fn[NAME] = FormStorage._jQueryInterface;
$.fn[NAME].Constructor = FormStorage;
2020-12-24 23:42:33 +01:00
$.fn[NAME].noConflict = function () {
2019-06-08 17:20:51 +02:00
$.fn[NAME] = JQUERY_NO_CONFLICT;
return FormStorage._jQueryInterface;
};
// auto-apply
$(window).on(`${Events.AJAX} ${Events.LOADED}`, () => {
2021-08-18 20:51:15 +02:00
$("form").each((i, el) => {
2019-06-08 17:20:51 +02:00
const $el = $(el);
// skip some forms
2021-08-18 20:51:15 +02:00
if ($el.hasClass("no-storage")) {
2019-06-08 17:20:51 +02:00
return true;
}
$el.jsFormStorage();
});
});
return FormStorage;
})($);
export default FormStorage;