From 3c39233ef9be32ab73fe21ec7c38b7182964b618 Mon Sep 17 00:00:00 2001 From: Galen Wolfe-Pauly Date: Thu, 27 Aug 2015 18:53:10 -0700 Subject: [PATCH] localstorage --- pub/work/src/js/actions/WorkActions.coffee | 21 ++++- .../src/js/components/FilterComponent.coffee | 9 +- .../src/js/components/ListComponent.coffee | 7 +- .../src/js/components/SortComponent.coffee | 1 + pub/work/src/js/main.js | 87 +++++++++++++++---- .../src/js/persistence/Persistence.coffee | 7 ++ pub/work/src/js/stores/WorkStore.coffee | 10 ++- 7 files changed, 114 insertions(+), 28 deletions(-) diff --git a/pub/work/src/js/actions/WorkActions.coffee b/pub/work/src/js/actions/WorkActions.coffee index 01a6b9fde4..c6116355c9 100644 --- a/pub/work/src/js/actions/WorkActions.coffee +++ b/pub/work/src/js/actions/WorkActions.coffee @@ -48,12 +48,29 @@ module.exports = version += 1 Persistence.put old:{id,version,dif:add:comment:val} - setFilter: (key,val) -> Dispatcher.handleViewAction {type:'setFilter', key,val} - setSort: (key,val) -> Dispatcher.handleViewAction {type:'setSort',key,val} + setFilter: (key,val,filters) -> + Dispatcher.handleViewAction {type:'setFilter', key,val} + filters[key] = val + Persistence.setLocal 'filters',filters + + setSort: (key,val,sorts) -> + Dispatcher.handleViewAction {type:'setSort',key,val} + sorts[key] = val + Persistence.setLocal 'sorts',sorts + moveItem: (list,to,from) -> Persistence.put {sort:list} Dispatcher.handleViewAction {type:'moveItems',list,to,from} + getLocal: (key)-> + Persistence.getLocal key,(e,r) -> + new Error(e) if e + return if r is null + obj = + type:"load#{key[0].toUpperCase()+key.slice(1)}" + obj[key] = r + Dispatcher.handleServerAction obj + listenList: (type)-> Persistence.subscribe type, (err,d)-> if d? diff --git a/pub/work/src/js/components/FilterComponent.coffee b/pub/work/src/js/components/FilterComponent.coffee index 1259b6c235..82350752d6 100644 --- a/pub/work/src/js/components/FilterComponent.coffee +++ b/pub/work/src/js/components/FilterComponent.coffee @@ -16,8 +16,8 @@ module.exports = recl if e.keyCode is 13 e.stopPropagation() e.preventDefault() - onKeyUp: (e) -> @change(e) - onBlur: (e) -> @change(e) + onKeyUp: (e) -> @change e + onBlur: (e) -> @change e change: (e) -> $t = $(e.target).closest('.filter') txt = $t.find('.input').text().trim() @@ -38,6 +38,9 @@ module.exports = recl render: -> (div {className:'filters'}, @fields.map ({filter,key,title})=> + txt = @props.filters[key] + txt = txt?.replace(/\~/g,"") if key is 'creator' + (div {key, 'data-key':key, className:"#{filter} filter ib"}, (label {}, title) switch filter @@ -53,5 +56,5 @@ module.exports = recl @onKeyDown @onKeyUp @onBlur - },@props.filters[filter]) + },txt) )) diff --git a/pub/work/src/js/components/ListComponent.coffee b/pub/work/src/js/components/ListComponent.coffee index afe369619d..27a9302d5b 100644 --- a/pub/work/src/js/components/ListComponent.coffee +++ b/pub/work/src/js/components/ListComponent.coffee @@ -17,7 +17,6 @@ module.exports = recl noNew:WorkStore.noNew() canSort:WorkStore.canSort() fulllist:WorkStore.getFullList() - listening:WorkStore.getListening() sorts:WorkStore.getSorts() filters:WorkStore.getFilters() expand:false @@ -107,14 +106,16 @@ module.exports = recl _changeListening: -> - _changeFilter: (key,val) -> WorkActions.setFilter key,val + _changeFilter: (key,val) -> WorkActions.setFilter key,val,@state.filters - _changeSort: (key,val) -> WorkActions.setSort key,val + _changeSort: (key,val) -> WorkActions.setSort key,val,@state.sorts componentDidMount: -> @placeholder = $ "
x
" WorkStore.addChangeListener @_onChangeStore WorkActions.listenList @props.list + WorkActions.getLocal 'filters' + WorkActions.getLocal 'sorts' @alias() componentDidUpdate: (_props,_state)-> diff --git a/pub/work/src/js/components/SortComponent.coffee b/pub/work/src/js/components/SortComponent.coffee index bb30c29a06..535200fdfc 100644 --- a/pub/work/src/js/components/SortComponent.coffee +++ b/pub/work/src/js/components/SortComponent.coffee @@ -11,6 +11,7 @@ module.exports = recl else if sor is 1 then sor = -1 else if sor is -1 then sor = 0 @props.onChange key,sor + render: -> (div {className:'sorts'}, _.map @props.sorts,(state,key) => (button { diff --git a/pub/work/src/js/main.js b/pub/work/src/js/main.js index bc2ffc83ab..b2a55036b9 100644 --- a/pub/work/src/js/main.js +++ b/pub/work/src/js/main.js @@ -43,14 +43,14 @@ module.exports = { }); }, setItem: function(arg, key, val) { - var id, obj, set, version; + var id, obj1, set, version; id = arg.id, version = arg.version; version += 1; key = key.split('_').join('-'); set = ( - obj = {}, - obj["" + key] = val, - obj + obj1 = {}, + obj1["" + key] = val, + obj1 ); Persistence.put({ old: { @@ -69,7 +69,7 @@ module.exports = { }); }, ownItem: function(arg, act) { - var id, obj, version; + var id, obj1, version; id = arg.id, version = arg.version; version += 1; return Persistence.put({ @@ -78,9 +78,9 @@ module.exports = { version: version, dif: { doer: ( - obj = {}, - obj["" + act] = null, - obj + obj1 = {}, + obj1["" + act] = null, + obj1 ) } } @@ -141,19 +141,23 @@ module.exports = { } }); }, - setFilter: function(key, val) { - return Dispatcher.handleViewAction({ + setFilter: function(key, val, filters) { + Dispatcher.handleViewAction({ type: 'setFilter', key: key, val: val }); + filters[key] = val; + return Persistence.setLocal('filters', filters); }, - setSort: function(key, val) { - return Dispatcher.handleViewAction({ + setSort: function(key, val, sorts) { + Dispatcher.handleViewAction({ type: 'setSort', key: key, val: val }); + sorts[key] = val; + return Persistence.setLocal('sorts', sorts); }, moveItem: function(list, to, from) { Persistence.put({ @@ -166,6 +170,22 @@ module.exports = { from: from }); }, + getLocal: function(key) { + return Persistence.getLocal(key, function(e, r) { + var obj; + if (e) { + new Error(e); + } + if (r === null) { + return; + } + obj = { + type: "load" + (key[0].toUpperCase() + key.slice(1)) + }; + obj[key] = r; + return Dispatcher.handleServerAction(obj); + }); + }, listenList: function(type) { return Persistence.subscribe(type, function(err, d) { var ref, sort, tasks; @@ -431,8 +451,12 @@ module.exports = recl({ className: 'filters' }, this.fields.map((function(_this) { return function(arg) { - var filter, key, title; + var filter, key, title, txt; filter = arg.filter, key = arg.key, title = arg.title; + txt = _this.props.filters[key]; + if (key === 'creator') { + txt = txt != null ? txt.replace(/\~/g, "") : void 0; + } return div({ key: key, 'data-key': key, @@ -451,7 +475,7 @@ module.exports = recl({ onKeyDown: this.onKeyDown, onKeyUp: this.onKeyUp, onBlur: this.onBlur - }, this.props.filters[filter]); + }, txt); } }).call(_this)); }; @@ -741,7 +765,6 @@ module.exports = recl({ noNew: WorkStore.noNew(), canSort: WorkStore.canSort(), fulllist: WorkStore.getFullList(), - listening: WorkStore.getListening(), sorts: WorkStore.getSorts(), filters: WorkStore.getFilters(), expand: false, @@ -888,15 +911,17 @@ module.exports = recl({ }, _changeListening: function() {}, _changeFilter: function(key, val) { - return WorkActions.setFilter(key, val); + return WorkActions.setFilter(key, val, this.state.filters); }, _changeSort: function(key, val) { - return WorkActions.setSort(key, val); + return WorkActions.setSort(key, val, this.state.sorts); }, componentDidMount: function() { this.placeholder = $("
x
"); WorkStore.addChangeListener(this._onChangeStore); WorkActions.listenList(this.props.list); + WorkActions.getLocal('filters'); + WorkActions.getLocal('sorts'); return this.alias(); }, componentDidUpdate: function(_props, _state) { @@ -1476,6 +1501,19 @@ module.exports = { if (cache != null) { return cb(null, cache); } + }, + setLocal: function(key, val) { + return window.localStorage.setItem(key, JSON.stringify(val)); + }, + getLocal: function(key, cb) { + var e, out; + try { + out = JSON.parse(window.localStorage.getItem(key)); + return cb(null, out); + } catch (_error) { + e = _error; + return cb(e); + } } }; @@ -1643,8 +1681,12 @@ WorkStore = assign({}, EventEmitter.prototype, { } return _tasks[item.id] = this.itemFromData(item, index); }, - getListening: function() { - return _listening; + loadFilters: function(arg) { + var filters; + filters = arg.filters; + console.log('filters'); + console.log(filters); + return _filters = filters; }, getFilters: function() { return _filters; @@ -1654,6 +1696,13 @@ WorkStore = assign({}, EventEmitter.prototype, { key = arg.key, val = arg.val; return _filters[key] = val; }, + loadSorts: function(arg) { + var sorts; + sorts = arg.sorts; + console.log('load sorts'); + console.log(sorts); + return _sorts = sorts; + }, getSorts: function() { return _sorts; }, diff --git a/pub/work/src/js/persistence/Persistence.coffee b/pub/work/src/js/persistence/Persistence.coffee index de1db17ded..b07cb48ee7 100644 --- a/pub/work/src/js/persistence/Persistence.coffee +++ b/pub/work/src/js/persistence/Persistence.coffee @@ -12,3 +12,10 @@ module.exports = subscribe: (key,cb) -> listeners[key] = cb (cb null,cache) if cache? + setLocal: (key,val) -> window.localStorage.setItem key,JSON.stringify val + getLocal: (key,cb) -> + try + out = JSON.parse window.localStorage.getItem key + cb null,out + catch e + cb e diff --git a/pub/work/src/js/stores/WorkStore.coffee b/pub/work/src/js/stores/WorkStore.coffee index 6823886703..df5c450f8b 100644 --- a/pub/work/src/js/stores/WorkStore.coffee +++ b/pub/work/src/js/stores/WorkStore.coffee @@ -90,10 +90,18 @@ WorkStore = assign {},EventEmitter.prototype,{ throw new Error "Collision: already have #{item.id}" _tasks[item.id] = @itemFromData item, index - getListening: -> _listening + loadFilters: ({filters}) -> + console.log 'filters' + console.log filters + _filters = filters getFilters: -> _filters setFilter: ({key,val}) -> _filters[key] = val + + loadSorts: ({sorts}) -> + console.log 'load sorts' + console.log sorts + _sorts = sorts getSorts: -> _sorts setSort: ({key,val}) -> for k,v of _sorts