clean up indexToInsertByPosition
This commit is contained in:
parent
b7ebee985b
commit
1cd53768ab
1 changed files with 24 additions and 27 deletions
|
@ -46,39 +46,36 @@ function indexOfItemById (items, id) {
|
|||
}
|
||||
|
||||
// Returns the index of where to insert the item according to |position|.
|
||||
function indexToInsertByPosition (items, position) {
|
||||
var insertIndex
|
||||
if (!position) {
|
||||
return items.length
|
||||
}
|
||||
const [query, id] = position.split('=')
|
||||
insertIndex = indexOfItemById(items, id)
|
||||
if (insertIndex === -1 && query !== 'endof') {
|
||||
function indexToInsertByPosition (items, pos) {
|
||||
if (!pos) return items.length
|
||||
|
||||
const [query, id] = pos.split('=')
|
||||
let idx = indexOfItemById(items, id)
|
||||
|
||||
if (idx === -1 && query !== 'endof') {
|
||||
console.warn("Item with id '" + id + "' is not found")
|
||||
return items.length
|
||||
}
|
||||
switch (query) {
|
||||
case 'after':
|
||||
insertIndex++
|
||||
break
|
||||
case 'endof':
|
||||
|
||||
// If the |id| doesn't exist, then create a new group with the |id|.
|
||||
if (insertIndex === -1) {
|
||||
items.push({
|
||||
id: id,
|
||||
type: 'separator'
|
||||
})
|
||||
insertIndex = items.length - 1
|
||||
}
|
||||
if (query === 'after') {
|
||||
idx += 1
|
||||
} else if (query === 'endof') {
|
||||
// create new group with id if none exists
|
||||
if (idx === -1) {
|
||||
items.push({
|
||||
id,
|
||||
type: 'separator'
|
||||
})
|
||||
idx = items.length - 1
|
||||
}
|
||||
idx += 1
|
||||
|
||||
// Find the end of the group.
|
||||
insertIndex++
|
||||
while (insertIndex < items.length && items[insertIndex].type !== 'separator') {
|
||||
insertIndex++
|
||||
}
|
||||
// search for end of group
|
||||
while (idx < items.length && items[idx].type !== 'separator') {
|
||||
idx += 1
|
||||
}
|
||||
}
|
||||
return insertIndex
|
||||
return idx
|
||||
}
|
||||
|
||||
const Menu = bindings.Menu
|
||||
|
|
Loading…
Add table
Reference in a new issue