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

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