diff --git a/src/item/Item.js b/src/item/Item.js index 55443fe0..b8414b16 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -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} * diff --git a/src/project/Project.js b/src/project/Project.js index 308911a3..7f15ca7a 100644 --- a/src/project/Project.js +++ b/src/project/Project.js @@ -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} *