UNPKG

este-library-oldschool

Version:

Library for github.com/steida/este.git

252 lines (235 loc) 6.85 kB
// Generated by github.com/steida/coffee2closure 900.1.18 /** @fileoverview Forms persister. Persist form fields state into localStorage or session. @see /demos/ui/formspersister.html */ var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.superClass_ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; goog.provide('este.ui.FormsPersister'); goog.provide('este.ui.FormsPersister.create'); goog.require('este.ui.Component'); goog.require('goog.dom.forms'); goog.require('este.dom'); goog.require('goog.events.FocusHandler'); goog.require('goog.events.InputHandler'); goog.require('este.storage.create'); /** @param {boolean=} session @constructor @extends {este.ui.Component} */ este.ui.FormsPersister = function(session) { if (session == null) { session = false; } este.ui.FormsPersister.superClass_.constructor.call(this); this.storage = este.storage.createCollectable('e-ui-formspersister', session); } extend(este.ui.FormsPersister, superClass); /** @param {Element} element @param {boolean=} session @return {este.ui.FormsPersister} */ este.ui.FormsPersister.create = function(element, session) { var persist; persist = new este.ui.FormsPersister(session); persist.decorate(element); return persist; }; /** Minimal client storage ftw. @type {number} */ este.ui.FormsPersister.prototype.expirationTime = 1000 * 60 * 60 * 24 * 30; /** @type {goog.storage.CollectableStorage} @protected */ este.ui.FormsPersister.prototype.storage = null; /** @type {goog.events.FocusHandler} @protected */ este.ui.FormsPersister.prototype.focusHandler = null; /** @override */ este.ui.FormsPersister.prototype.decorateInternal = function(element) { var data, path; este.ui.FormsPersister.superClass_.decorateInternal.call(this, element); path = this.getElementDomPath(); data = this.storage.get(path.join()); if (!data) { return; } this.retrieve(/** @type {Object} */(data)); }; /** @param {Object} data @protected */ este.ui.FormsPersister.prototype.retrieve = function(data) { var el, field, fields, fieldsMap, form, formPath, i, j, k, len, len1, len2, name, name1, numberPath, ref, ref1, ref2, ref3, stringPath, value; for (formPath in data) { fields = data[formPath]; stringPath = formPath.split(','); numberPath = goog.array.map(stringPath, function(item) { return Number(item); }); form = este.dom.getElementByDomPathIndex(numberPath); if (!form || !form.elements) { continue; } fieldsMap = {}; ref = form.elements; for (i = 0, len = ref.length; i < len; i++) { el = ref[i]; if (fieldsMap[name1 = el.name] == null) { fieldsMap[name1] = []; } fieldsMap[el.name].push(el); } for (name in fields) { value = fields[name]; field = (ref1 = fieldsMap[name]) != null ? ref1[0] : void 0; if (!field) { continue; } switch (field.type) { case 'radio': ref2 = fieldsMap[name]; for (j = 0, len1 = ref2.length; j < len1; j++) { el = ref2[j]; goog.dom.forms.setValue(el, el.value === value); } break; case 'checkbox': ref3 = fieldsMap[name]; for (k = 0, len2 = ref3.length; k < len2; k++) { el = ref3[k]; goog.dom.forms.setValue(el, goog.array.contains(value, el.value)); } break; default: goog.dom.forms.setValue(field, value); } } } }; /** @override */ este.ui.FormsPersister.prototype.enterDocument = function() { este.ui.FormsPersister.superClass_.enterDocument.call(this); this.focusHandler = new goog.events.FocusHandler(this.getElement()); this.on(this.focusHandler, 'focusin', this.onFocusin); this.on(this.getElement(), 'change', this.onChange); }; /** @override */ este.ui.FormsPersister.prototype.exitDocument = function() { this.focusHandler.dispose(); este.ui.FormsPersister.superClass_.exitDocument.call(this); }; /** @param {goog.events.BrowserEvent} e @protected */ este.ui.FormsPersister.prototype.onFocusin = function(e) { var ref, target; target = /** @type {Element} */(e.target); if (!((ref = target.tagName) === 'INPUT' || ref === 'TEXTAREA')) { return; } return this.registerInputHander(target); }; /** @param {Element} field @protected */ este.ui.FormsPersister.prototype.registerInputHander = function(field) { var handler; handler = new goog.events.InputHandler(field); this.on(handler, 'input', this.onFieldInput); return this.getHandler().listenOnce(field, 'blur', function(e) { return handler.dispose(); }); }; /** @param {goog.events.BrowserEvent} e @protected */ este.ui.FormsPersister.prototype.onFieldInput = function(e) { return this.storeField(/** @type {Element} */(e.target)); }; /** @param {goog.events.BrowserEvent} e @protected */ este.ui.FormsPersister.prototype.onChange = function(e) { return this.storeField(/** @type {Element} */(e.target)); }; /** @param {Element} field @protected */ este.ui.FormsPersister.prototype.storeField = function(field) { var formDomPath, name, value; formDomPath = este.dom.getDomPathIndexes(field.form); name = field.name; value = this.getFieldValue(field); return this.store(formDomPath, name, value); }; /** @param {Array.<number>} formDomPath @param {string} name @param {string|Array.<string>} value */ este.ui.FormsPersister.prototype.store = function(formDomPath, name, value) { var key, path, storage; path = this.getElementDomPath(); key = path.join(); storage = this.storage.get(key); if (storage == null) { storage = {}; } if (storage[formDomPath] == null) { storage[formDomPath] = {}; } storage[formDomPath][name] = value; return this.storage.set(key, storage, goog.now() + this.expirationTime); }; /** @return {Array.<number>} @protected */ este.ui.FormsPersister.prototype.getElementDomPath = function() { return este.dom.getDomPathIndexes(this.getElement()); }; /** @param {Element} field @return {string|Array.<string>} @protected */ este.ui.FormsPersister.prototype.getFieldValue = function(field) { var el, i, len, ref, value, values; if (field.type === 'checkbox') { values = []; ref = field.form.elements; for (i = 0, len = ref.length; i < len; i++) { el = ref[i]; if (!(el.name === field.name)) { continue; } value = goog.dom.forms.getValue(el); if (value != null) { values.push(value); } } return values; } return goog.dom.forms.getValue(field); };