localstorage

This commit is contained in:
Galen Wolfe-Pauly 2015-08-27 18:53:10 -07:00
parent 0fecaa1d77
commit 3c39233ef9
7 changed files with 114 additions and 28 deletions

View File

@ -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?

View File

@ -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)
))

View File

@ -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 = $ "<div class='item placeholder'><div class='sort'>x</div></div>"
WorkStore.addChangeListener @_onChangeStore
WorkActions.listenList @props.list
WorkActions.getLocal 'filters'
WorkActions.getLocal 'sorts'
@alias()
componentDidUpdate: (_props,_state)->

View File

@ -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 {

View File

@ -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 = $("<div class='item placeholder'><div class='sort'>x</div></div>");
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;
},

View File

@ -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

View File

@ -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