Skip to content

Commit

Permalink
fix(flow): correctly set index
Browse files Browse the repository at this point in the history
  • Loading branch information
solkimicreb committed Nov 23, 2016
1 parent 2a46879 commit f47da94
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
10 changes: 6 additions & 4 deletions middlewares/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,20 @@ function $clearContent () {
this[secret.firstNodes] = []
}

function $moveContent (fromIndex, toIndex) {
function $moveContent (fromIndex, toIndex, extraContext) {
if (typeof fromIndex !== 'number' || typeof toIndex !== 'number') {
throw new Error('first and second argument must be numbers')
}
if (extraContext !== undefined && typeof extraContext !== 'object') {
throw new Error('third argument must be an object or undefined')
}
const firstNodes = this[secret.firstNodes]
const fromNode = firstNodes[fromIndex]
const untilNode = firstNodes[fromIndex + 1]
const toNode = firstNodes[toIndex]

let node = fromNode
let next
// do not do this if it is a single node (no loop needed)!
while (node && node !== untilNode) {
next = node.nextSibling
this.insertBefore(node, toNode)
Expand All @@ -123,8 +125,8 @@ function $moveContent (fromIndex, toIndex) {
firstNodes.splice(fromIndex, 1)
firstNodes.splice(toIndex, 0, fromNode)

if (fromNode && fromNode.$contextState) {
fromNode.$contextState.$index = toIndex
if (extraContext && fromNode && fromNode.$contextState) {
Object.assign(fromNode.$contextState, extraContext)
}
}

Expand Down
13 changes: 8 additions & 5 deletions middlewares/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function repeatAttribute (array) {
this[secret.hasRepeat] = true
}
const trackBy = this.getAttribute('track-by')
const repeatValue = this.getAttribute('repeat-value')
const repeatValue = this.getAttribute('repeat-value') || '$value'
const repeatIndex = this.getAttribute('repeat-index') || '$index'

array = array || []
const prevArray = this[secret.prevArray] = this[secret.prevArray] || []
Expand All @@ -54,31 +55,33 @@ function repeatAttribute (array) {
let prevItem = prevArray[++i]

if (prevItem === undefined) {
this.$insertContent(i, {$index: i, [repeatValue]: item})
this.$insertContent(i, {[repeatIndex]: i, [repeatValue]: item})
prevArray[i] = item
continue
}
if (item === prevItem) {
this.$mutateContext(i, {[repeatIndex]: i})
continue
}
if (trackBy === '$index') {
if (trackBy === repeatIndex) {
this.$mutateContext(i, {[repeatValue]: item})
prevArray[i] = item
continue
}
if (trackBy && isTrackBySame(item, prevItem, trackBy)) {
this.$mutateContext(i, {[repeatIndex]: i})
continue
}
for (let j = i + 1; j < prevArray.length; j++) {
prevItem = prevArray[j]
if (item === prevItem || (trackBy && isTrackBySame(item, prevItem, trackBy))) {
this.$moveContent(j, i)
this.$moveContent(j, i, {[repeatIndex]: i})
prevArray.splice(i, 0, prevItem)
prevArray.splice(j, 1)
continue iteration
}
}
this.$insertContent(i, {$index: i, [repeatValue]: item})
this.$insertContent(i, {[repeatIndex]: i, [repeatValue]: item})
prevArray.splice(i, 0, item)
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"data binding"
],
"dependencies": {
"@risingstack/nx-compile": "^4.0.0",
"@risingstack/nx-observe": "^2.0.0",
"@risingstack/nx-compile": "^4.1.0",
"@risingstack/nx-observe": "^2.0.1",
"keycode": "^2.1.4"
},
"devDependencies": {
Expand Down

0 comments on commit f47da94

Please sign in to comment.