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,10 +1816,12 @@ var Item = Base.extend(Callback, /** @lends Item# */{
} }
return true; return true;
} }
}, new function() { }, {
function getItems(item, match, list) { statics: {
var children = item._children, // NOTE: We pass children instead of item as first argument so the
items = list && []; // 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++) { for (var i = 0, l = children && children.length; i < l; i++) {
var child = children[i]; var child = children[i];
if (child.matches(match)) { if (child.matches(match)) {
@ -1829,7 +1831,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
return child; return child;
} }
} }
var res = getItems(child, match, list); var res = _getItems(child._children, match, list);
if (list) { if (list) {
items.push.apply(items, res); items.push.apply(items, res);
} else if (res) { } else if (res) {
@ -1838,19 +1840,18 @@ var Item = Base.extend(Callback, /** @lends Item# */{
} }
return list ? items : null; return list ? items : null;
} }
},
return /** @lends Item# */{
// DOCS: Item#getItems // DOCS: Item#getItems
getItems: function(match) { getItems: function(match) {
return getItems(this, match, true); return Item._getItems(this._children, match, true);
}, },
// DOCS: Item#getItem // DOCS: Item#getItem
getItem: function(match) { getItem: function(match) {
return getItems(this, match, false); return Item._getItems(this._children, match, false);
} },
};
}, /** @lends Item# */{
/** /**
* {@grouptitle Importing / Exporting JSON and SVG} * {@grouptitle Importing / Exporting JSON and SVG}
* *

View file

@ -308,33 +308,18 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
if (res) return res; if (res) return res;
} }
return null; 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 // DOCS: Project#getItems
getItems: function(match) { getItems: function(match) {
return getItems(this, match, true); return Item._getItems(this.layers, match, true);
}, },
// DOCS: Project#getItems // DOCS: Project#getItems
getItem: function(match) { getItem: function(match) {
return getItems(this, match, false); return Item._getItems(this.layers, match, false);
} },
};
}, /** @lends Project# */{
/** /**
* {@grouptitle Importing / Exporting JSON and SVG} * {@grouptitle Importing / Exporting JSON and SVG}
* *