Skip to content

Commit

Permalink
Rewrote onBlockDropped using new getBlockPath. 😎 #26
Browse files Browse the repository at this point in the history
  • Loading branch information
prakhar1989 committed Jul 6, 2015
1 parent d8839c0 commit 1542bd7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
52 changes: 30 additions & 22 deletions js/stores/SurveyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ var SurveyStore = Reflux.createStore({
}
return `${prefix}_${Math.floor((Math.random() * 99999) + 1)}`
},
getBlockPath(blockID, survey, _blockMap) {
// function that returns a chain of IDs from the root block
// to the block with id - id
var getIDsList = function getIDsList(id, path = []) {
if (!_blockMap.has(id)) {
return path.concat([id]).reverse()
}
return getIDsList(_blockMap.get(id), path.concat([id]));
};

// function that returns the index of block id within the list of blocks
var getIndex = (id, list) => list.findIndex(b => b.get('id') === id);

// initialize path with index of root node
var [rootID, ...restIDs] = getIDsList(blockID);
var path = [getIndex(rootID, survey)];

// reduce over the rest of ids by finding id at each level
// and changing path accordingly
return restIDs.reduce((path, id) => {
var path = path.concat(['subblocks']);
var index = getIndex(id, survey.getIn(path));
return path.concat([index]);
}, path);
},
/**
* Runs when the blockDropped action is called by the view.
* Adds a new block to the end of the survey object.
Expand All @@ -128,28 +153,11 @@ var SurveyStore = Reflux.createStore({
this.updateSurveyData(newSurvey, true);
SurveyActions.showAlert("New block added.", AlertTypes.SUCCESS);
} else {
// block is dropped on another block
let isSubblock = _blockMap.has(targetID);
if (isSubblock) { // only handles 2 levels currently
let parentBlockIndex = this.getBlockIndex(_blockMap.get(targetID));
let blockIndex = this.getBlockIndex(targetID, survey.get(parentBlockIndex));
let newSurvey = survey.updateIn(
[parentBlockIndex, 'subblocks', blockIndex, 'subblocks'],
list => list.splice(0, 0, newBlock)
);

// update and cache
this.updateSurveyData(newSurvey, true);
} else {
let blockIndex = this.getBlockIndex(targetID);
let newSurvey = survey.updateIn([blockIndex, 'subblocks'], list =>
list.splice(0, 0, newBlock)
);

// update and cache
this.updateSurveyData(newSurvey, true);
}

let blockPath = this.getBlockPath(targetID, survey, _blockMap);
let newSurvey = survey.updateIn([...blockPath, 'subblocks'],
list => list.splice(0, 0, newBlock)
);
this.updateSurveyData(newSurvey, true);

// update block map with new subblock
_blockMap = _blockMap.set(newBlock.get('id'), targetID);
Expand Down
4 changes: 2 additions & 2 deletions tests/testSurvey.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ describe("getBlockPath", function() {
var blockPath = getBlockPath('b_41444', this.survey, this._blockMap);

// add new subblock
var newSurvey = this.survey.updateIn(blockPath.concat(['subblocks']),
list => [block, ...list]
var newSurvey = this.survey.updateIn([...blockPath, 'subblocks'],
list => list.splice(0, 0, block)
);

// update the blockMap
Expand Down

0 comments on commit 1542bd7

Please sign in to comment.