Last active
May 9, 2018 02:27
-
-
Save indiscripts/3a01b818717488847c07ff6083a4a829 to your computer and use it in GitHub Desktop.
This file contains 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
Group.prototype.addItems = function(/*PageItem[]*/newItems) | |
//---------------------------- | |
// Emulates Group.groups.add(items) | |
// [ Also supports Group.addItems(existing_item) ] | |
// <newItems> == Array of 2+ PageItem | |
// -> create a <newItems> subgroup in <this> | |
// <newItems> == single PageItem | |
// -> add the PageItem in <this> | |
// Returns the added subgroup or item | |
// (the whole group hierarchy is preserved) | |
// Note - this method fails if <this> is | |
// anchored/embedded in a story | |
{ | |
var nodes=[], gs=[], g=this, node; | |
var makeNode = function(id,g) | |
{ | |
var elems = g.pageItems.everyItem().getElements(); | |
if (id === null) // placeholder for newItems | |
return {index: elems.length, items:elems.concat(0)}; | |
for(var i=elems.length-1 ; i>=0 ; i--) | |
// need to find the actual index in the group | |
if (elems[i].id == id) break; | |
return {index: i, items:elems}; | |
}; | |
var id=null; | |
for( ; g.constructor == Group ; id=g.id, g=g.parent ) | |
{ | |
gs.push(g.getElements()[0]); | |
nodes.push(makeNode(id,g)); | |
} | |
var add = (function() | |
{ | |
var host = g; | |
return function(items) | |
{ | |
return host.groups.add(items); | |
}; | |
})(); | |
var r = ( 'array' == typeof newItems ) ? | |
add(newItems) : | |
newItems; | |
while( g=gs.pop() ) g.ungroup(); | |
for( g=r ; node=nodes.shift() ; ) | |
{ | |
node.items[node.index] = g; | |
g = add(node.items); | |
} | |
return r; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment