From 71de4f6e98c7e1665b7657d5e79a8dda14f2b052 Mon Sep 17 00:00:00 2001 From: Anton Dyudin Date: Thu, 27 Aug 2015 14:45:19 -0700 Subject: [PATCH] don't persist blank item sort, drag ghost, save it --- pub/work/src/js/actions/WorkActions.coffee | 6 +- .../src/js/components/FieldComponent.coffee | 16 ++-- .../src/js/components/ItemComponent.coffee | 5 +- .../src/js/components/ListComponent.coffee | 27 +++--- pub/work/src/js/main.js | 91 +++++++++++-------- pub/work/src/js/stores/WorkStore.coffee | 3 +- 6 files changed, 83 insertions(+), 65 deletions(-) diff --git a/pub/work/src/js/actions/WorkActions.coffee b/pub/work/src/js/actions/WorkActions.coffee index f963f56dd6..c96285112a 100644 --- a/pub/work/src/js/actions/WorkActions.coffee +++ b/pub/work/src/js/actions/WorkActions.coffee @@ -54,10 +54,8 @@ module.exports = setFilter: (key,val) -> Dispatcher.handleViewAction {type:'setFilter', key,val} setSort: (key,val) -> Dispatcher.handleViewAction {type:'setSort',key,val} moveItem: (list,to,from) -> - sort = _.clone list - sort.splice to, 0, sort.splice(from,1)[0] - Persistence.put {sort} - Dispatcher.handleViewAction {type:'moveItems',list:sort,to,from} + Persistence.put {sort:list} + Dispatcher.handleViewAction {type:'moveItems',list,to,from} listenList: (type)-> Persistence.subscribe type, (err,d)-> diff --git a/pub/work/src/js/components/FieldComponent.coffee b/pub/work/src/js/components/FieldComponent.coffee index 960bb57571..abfb19464e 100644 --- a/pub/work/src/js/components/FieldComponent.coffee +++ b/pub/work/src/js/components/FieldComponent.coffee @@ -35,22 +35,24 @@ module.exports = recl onKeyUp: (e) -> $t = $(e.target).closest '.field' - val = @parse @getVal() + _val = @getVal() + if @props.item.ghost and _val is "" + return + val = @parse _val unless @validate val @setState invalid:yes return @setState invalid:no unless @equal @props.defaultValue, val - @oldValue ?= [] @oldValue.push val if @to then clearTimeout @to @to = setTimeout => - {item,_key} = @props + {item,_key,index} = @props if item.version >= 0 WorkActions.setItem item, _key, val - else WorkActions.newItem item.index, + else WorkActions.newItem index, id: item.id tags: item.tags audience: item.audience @@ -62,7 +64,7 @@ module.exports = recl $(@refs.input.getDOMNode()).val() else $(@refs.input.getDOMNode()).text() - parse : (text)-> switch @props._key + parse: (text)-> switch @props._key when 'tags' then text.trim().split(" ") when 'audience' then text.trim().split(" ").map (a) -> "~#{a}" when 'date_due' @@ -71,12 +73,12 @@ module.exports = recl new Date(d).valueOf() else text - equal: (vol,val) -> switch @props._key + equal: (vol=(@parse ""),val) -> switch @props._key when 'tags', 'audience' (_.xor(vol,val).length is 0) when 'date_due' vol.valueOf() is val - else (vol ? "") is val + else vol is val validate: (val) -> switch @props._key when 'date_due' diff --git a/pub/work/src/js/components/ItemComponent.coffee b/pub/work/src/js/components/ItemComponent.coffee index eb94160d16..ec3bd583a5 100644 --- a/pub/work/src/js/components/ItemComponent.coffee +++ b/pub/work/src/js/components/ItemComponent.coffee @@ -73,8 +73,9 @@ module.exports = recl getInitialState: -> {expand:false} renderField: (_key,props,render=_.identity)-> - defaultValue = @props.item[_key] - rece Field, $.extend props, {render,_key,item:@props.item,defaultValue} + {item,index} = @props + defaultValue = item[_key] + rece Field, $.extend props, {render,_key,defaultValue,item,index} renderTopField: (key,props,format)-> _props = _.extend props,{className:"#{props.className ? key} top"} diff --git a/pub/work/src/js/components/ListComponent.coffee b/pub/work/src/js/components/ListComponent.coffee index db7e6d7512..d3dca8b89b 100644 --- a/pub/work/src/js/components/ListComponent.coffee +++ b/pub/work/src/js/components/ListComponent.coffee @@ -38,7 +38,10 @@ module.exports = recl to = Number @over.closest('.item-wrap').attr('data-index') if from= 0) + WorkActions.moveItem list, to, from @dragged.removeClass 'hidden' @placeholder.remove() @@ -69,18 +72,17 @@ module.exports = recl index++ ins = @state.selected+1 # XX consolidate @setState {selected:ins,select:true} - {tags,audience} = item - WorkActions.newItem index, {tags,audience} + unless item.ghost + {tags,audience} = item + item = {tags,audience} + WorkActions.newItem index, item # backspace - remove if at 0 when 8 if (window.getSelection().getRangeAt(0).endOffset is 0) and (e.target.innerText.length is 0) - if i.props.item.ghost - WorkActions.moveGhost null - else - if @state.selected isnt 0 - @setState {selected:@state.selected-1,select:"end"} - WorkActions.removeItem i.props.item + if @state.selected isnt 0 + @setState {selected:@state.selected-1,select:"end"} + WorkActions.removeItem i.props.item e.preventDefault() # up when 38 @@ -149,14 +151,17 @@ module.exports = recl }, _.map @state.list,(item,index) => className = "item-wrap" key = item.id - if item.ghost then className += " ghost" + draggable = @state.canSort + if item.ghost + className += " ghost" + draggable = false (div {className,key,'data-index':index}, rece(ItemComponent,{ item index + draggable @_focus @title_keyDown - draggable:@state.canSort @_dragStart @_dragEnd}) ) diff --git a/pub/work/src/js/main.js b/pub/work/src/js/main.js index 4c432ab0db..1becccc242 100644 --- a/pub/work/src/js/main.js +++ b/pub/work/src/js/main.js @@ -142,15 +142,12 @@ module.exports = { }); }, moveItem: function(list, to, from) { - var sort; - sort = _.clone(list); - sort.splice(to, 0, sort.splice(from, 1)[0]); Persistence.put({ - sort: sort + sort: list }); return Dispatcher.handleViewAction({ type: 'moveItems', - list: sort, + list: list, to: to, from: from }); @@ -221,9 +218,13 @@ module.exports = recl({ }, div(props))); }, onKeyUp: function(e) { - var $t, val; + var $t, _val, val; $t = $(e.target).closest('.field'); - val = this.parse(this.getVal()); + _val = this.getVal(); + if (this.props.item.ghost && _val === "") { + return; + } + val = this.parse(_val); if (!this.validate(val)) { this.setState({ invalid: true @@ -243,12 +244,12 @@ module.exports = recl({ } return this.to = setTimeout((function(_this) { return function() { - var _key, item, obj, ref1; - ref1 = _this.props, item = ref1.item, _key = ref1._key; + var _key, index, item, obj, ref1; + ref1 = _this.props, item = ref1.item, _key = ref1._key, index = ref1.index; if (item.version >= 0) { return WorkActions.setItem(item, _key, val); } else { - return WorkActions.newItem(item.index, ( + return WorkActions.newItem(index, ( obj = { id: item.id, tags: item.tags, @@ -289,6 +290,9 @@ module.exports = recl({ } }, equal: function(vol, val) { + if (vol == null) { + vol = this.parse(""); + } switch (this.props._key) { case 'tags': case 'audience': @@ -296,7 +300,7 @@ module.exports = recl({ case 'date_due': return vol.valueOf() === val; default: - return (vol != null ? vol : "") === val; + return vol === val; } }, validate: function(val) { @@ -554,16 +558,18 @@ module.exports = recl({ }; }, renderField: function(_key, props, render) { - var defaultValue; + var defaultValue, index, item, ref1; if (render == null) { render = _.identity; } - defaultValue = this.props.item[_key]; + ref1 = this.props, item = ref1.item, index = ref1.index; + defaultValue = item[_key]; return rece(Field, $.extend(props, { render: render, _key: _key, - item: this.props.item, - defaultValue: defaultValue + defaultValue: defaultValue, + item: item, + index: index })); }, renderTopField: function(key, props, format) { @@ -735,7 +741,7 @@ module.exports = recl({ return this.dragged = i.dragged; }, _dragEnd: function(e, i) { - var from, id, to; + var from, id, list, sort, to, version; from = Number(this.dragged.closest('.item-wrap').attr('data-index')); to = Number(this.over.closest('.item-wrap').attr('data-index')); if (from < to) { @@ -744,16 +750,20 @@ module.exports = recl({ if (this.drop === 'after') { to++; } - WorkActions.moveItem((function() { + sort = _.clone(this.state.list); + sort.splice(to, 0, sort.splice(from, 1)[0]); + list = (function() { var j, len, ref1, results; - ref1 = this.state.list; results = []; - for (j = 0, len = ref1.length; j < len; j++) { - id = ref1[j].id; - results.push(id); + for (j = 0, len = sort.length; j < len; j++) { + ref1 = sort[j], id = ref1.id, version = ref1.version; + if ((version != null ? version : -1) >= 0) { + results.push(id); + } } return results; - }).call(this), to, from); + })(); + WorkActions.moveItem(list, to, from); this.dragged.removeClass('hidden'); return this.placeholder.remove(); }, @@ -795,25 +805,24 @@ module.exports = recl({ select: true }); } - tags = item.tags, audience = item.audience; - WorkActions.newItem(index, { - tags: tags, - audience: audience - }); + if (!item.ghost) { + tags = item.tags, audience = item.audience; + item = { + tags: tags, + audience: audience + }; + } + WorkActions.newItem(index, item); break; case 8: if ((window.getSelection().getRangeAt(0).endOffset === 0) && (e.target.innerText.length === 0)) { - if (i.props.item.ghost) { - WorkActions.moveGhost(null); - } else { - if (this.state.selected !== 0) { - this.setState({ - selected: this.state.selected - 1, - select: "end" - }); - } - WorkActions.removeItem(i.props.item); + if (this.state.selected !== 0) { + this.setState({ + selected: this.state.selected - 1, + select: "end" + }); } + WorkActions.removeItem(i.props.item); e.preventDefault(); } break; @@ -894,11 +903,13 @@ module.exports = recl({ onDragOver: this._dragOver }, _.map(this.state.list, (function(_this) { return function(item, index) { - var className, key; + var className, draggable, key; className = "item-wrap"; key = item.id; + draggable = _this.state.canSort; if (item.ghost) { className += " ghost"; + draggable = false; } return div({ className: className, @@ -907,9 +918,9 @@ module.exports = recl({ }, rece(ItemComponent, { item: item, index: index, + draggable: draggable, _focus: _this._focus, title_keyDown: _this.title_keyDown, - draggable: _this.state.canSort, _dragStart: _this._dragStart, _dragEnd: _this._dragEnd })); @@ -1557,7 +1568,7 @@ WorkStore = assign({}, EventEmitter.prototype, { list.reverse(); } } - if (!(((_filters.creator != null) && _filters.owner !== urb.ship) || (_filters.done != null))) { + if (!(((_filters.creator != null) && _filters.owner !== urb.ship) || _filters.done === true)) { ghost = $.extend({ ghost: true, version: -1 diff --git a/pub/work/src/js/stores/WorkStore.coffee b/pub/work/src/js/stores/WorkStore.coffee index 08070c947f..4cdebdb29a 100644 --- a/pub/work/src/js/stores/WorkStore.coffee +++ b/pub/work/src/js/stores/WorkStore.coffee @@ -64,7 +64,8 @@ WorkStore = assign {},EventEmitter.prototype,{ break list = _.sortBy list,k,k if v is -1 then list.reverse() - unless (_filters.creator? and _filters.owner isnt urb.ship) or _filters.done? + unless (_filters.creator? and _filters.owner isnt urb.ship) or + _filters.done is true ghost = $.extend {ghost:true,version:-1}, _ghost if _filters.tags then ghost.tags = _filters.tags if _filters.audience then ghost.audience = _filters.audience