Simplify and merge Item#getItems() code with Project#getItems()

This commit is contained in:
Jürg Lehni 2014-03-18 11:47:50 +01:00
parent 833d71f7ea
commit 62f7b0d73e
2 changed files with 40 additions and 54 deletions

View file

@ -1816,41 +1816,42 @@ var Item = Base.extend(Callback, /** @lends Item# */{
}
return true;
}
}, new function() {
function getItems(item, match, list) {
var children = item._children,
items = list && [];
for (var i = 0, l = children && children.length; i < l; i++) {
var child = children[i];
if (child.matches(match)) {
}, {
statics: {
// NOTE: We pass children instead of item as first argument so the
// method can be used for Project#layers as well in Project.
_getItems: function _getItems(children, match, list) {
var items = list && [];
for (var i = 0, l = children && children.length; i < l; i++) {
var child = children[i];
if (child.matches(match)) {
if (list) {
items.push(child);
} else {
return child;
}
}
var res = _getItems(child._children, match, list);
if (list) {
items.push(child);
} else {
return child;
items.push.apply(items, res);
} else if (res) {
return res;
}
}
var res = getItems(child, match, list);
if (list) {
items.push.apply(items, res);
} else if (res) {
return res;
}
return list ? items : null;
}
return list ? items : null;
}
},
return /** @lends Item# */{
// DOCS: Item#getItems
getItems: function(match) {
return getItems(this, match, true);
},
// DOCS: Item#getItems
getItems: function(match) {
return Item._getItems(this._children, match, true);
},
// DOCS: Item#getItem
getItem: function(match) {
return Item._getItems(this._children, match, false);
},
// DOCS: Item#getItem
getItem: function(match) {
return getItems(this, match, false);
}
};
}, /** @lends Item# */{
/**
* {@grouptitle Importing / Exporting JSON and SVG}
*

View file

@ -308,33 +308,18 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
if (res) return res;
}
return null;
}
}, new function() {
function getItems(project, match, list) {
var layers = project.layers,
items = list && [];
for (var i = 0, l = layers.length; i < l; i++) {
var res = layers[i][list ? 'getItems' : 'getItem'](match);
if (list) {
items.push.apply(items, res);
} else if (res)
return res;
}
return list ? items : null;
}
},
return /** @lends Project# */{
// DOCS: Project#getItems
getItems: function(match) {
return getItems(this, match, true);
},
// DOCS: Project#getItems
getItems: function(match) {
return Item._getItems(this.layers, match, true);
},
// DOCS: Project#getItems
getItem: function(match) {
return Item._getItems(this.layers, match, false);
},
// DOCS: Project#getItems
getItem: function(match) {
return getItems(this, match, false);
}
};
}, /** @lends Project# */{
/**
* {@grouptitle Importing / Exporting JSON and SVG}
*