diff options
author | Dan Finlay <dan@danfinlay.com> | 2016-08-26 03:17:09 +0800 |
---|---|---|
committer | Dan Finlay <dan@danfinlay.com> | 2016-08-26 03:17:21 +0800 |
commit | 6eb09c1a796f636ce45c9589a47a40f37e4ed2ac (patch) | |
tree | 34a973a4f78b01bbc2af74e1673a4b7b62639f45 /ui/lib | |
parent | b2233f7e0a6d2596b3fdffe0f0f76bfa58f541f9 (diff) | |
download | tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.tar tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.tar.gz tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.tar.bz2 tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.tar.lz tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.tar.xz tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.tar.zst tangerine-wallet-browser-6eb09c1a796f636ce45c9589a47a40f37e4ed2ac.zip |
Add persistent form class
Diffstat (limited to 'ui/lib')
-rw-r--r-- | ui/lib/persistent-form.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/ui/lib/persistent-form.js b/ui/lib/persistent-form.js new file mode 100644 index 000000000..4bb19ad58 --- /dev/null +++ b/ui/lib/persistent-form.js @@ -0,0 +1,60 @@ +const inherits = require('util').inherits +const Component = require('react').Component +const defaultKey = 'persistent-form-default' +const eventName = 'keyup'//.persistent-form-change'//.persistent-form-change' + +module.exports = PersistentForm + +function PersistentForm () { + Component.call(this) +} + +inherits(PersistentForm, Component) + +PersistentForm.prototype.componentDidMount = function () { + const fields = document.querySelectorAll('[data-persistent-formid]') + const store = this.getPersistentStore() + fields.forEach((field) => { + const key = field.getAttribute('data-persistent-formid') + const val = field.value + const cached = store[key] + if (cached !== undefined) { + field.value = cached + } + + field.addEventListener(eventName, this.persistentFieldDidUpdate.bind(this)) + }) +} + +PersistentForm.prototype.getPersistentStore = function () { + let store = window.localStorage[this.persistentFormParentId || defaultKey] + if (store && store !== 'null') { + store = JSON.parse(store) + } else { + store = {} + } + return store +} + +PersistentForm.prototype.setPersistentStore = function (newStore) { + window.localStorage[this.persistentFormParentId || defaultKey] = JSON.stringify(newStore) +} + +PersistentForm.prototype.persistentFieldDidUpdate = function (event) { + const field = event.target + const store = this.getPersistentStore() + const key = field.getAttribute('data-persistent-formid') + const val = field.value + store[key] = val + console.log(val) + this.setPersistentStore(store) +} + +PersistentForm.prototype.componentWillUnmount = function () { + const fields = document.querySelectorAll('[data-persistent-formid]') + const store = this.getPersistentStore() + fields.forEach((field) => { + field.removeEventListener(eventName, this.persistentFieldDidUpdate.bind(this)) + }) + this.setPersistentStore({}) +} |