Last active
May 28, 2016 12:32
-
-
Save stegrams/72d79945696c5cdcd364cd6246a231b1 to your computer and use it in GitHub Desktop.
Lane action in LaneStore for dragging a note over a note or an empy lane instead of attachToLane. Source: http://survivejs.com/webpack_react/implementing_dnd/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
queryIds(searchIds){ | |
let ids = [...searchIds]; | |
return this.lanes.reduce((result, lane, laneIdx) => { | |
// no more ids | |
if(!ids.length) return result; | |
const notes = lane.notes; | |
const idsIndexOfLane = ids.indexOf(lane.id); | |
// current lane id is included in searchIds | |
if(idsIndexOfLane !==-1){ | |
const noteIdx = 0; | |
const isLane = true; | |
result[lane.id] = {notes, noteIdx, lane, laneIdx, isLane}; | |
ids.splice(idsIndexOfLane, 1); | |
return result; | |
} | |
// check current lane note ids for search ids | |
ids = ids.filter(id => { | |
const noteIdx = notes.indexOf(id); | |
if(noteIdx === -1) return true; | |
result[id] = {notes, noteIdx, lane, laneIdx}; | |
return false; | |
}); | |
return result; | |
}, {}); | |
} | |
move({sourceId, targetId}){ | |
const {[sourceId]: source, [targetId]: target} = | |
this.queryIds([sourceId, targetId]); | |
if(target.isLane && target.notes.length ) return; | |
const lanes = [...this.lanes]; | |
const slane = lanes[source.laneIdx] = | |
{...source.lane, notes: [...source.notes]}; | |
let tlane = slane; | |
if(source.lane.id !== target.lane.id){ | |
tlane = lanes[target.laneIdx] = | |
{...target.lane, notes: target.notes.slice()}; | |
} | |
slane.notes.splice(source.noteIdx, 1); | |
tlane.notes.splice(target.noteIdx, 0, sourceId); | |
this.setState({lanes}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment