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; 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.
for (var i = 0, l = children && children.length; i < l; i++) { _getItems: function _getItems(children, match, list) {
var child = children[i]; var items = list && [];
if (child.matches(match)) { 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) { if (list) {
items.push(child); items.push.apply(items, res);
} else { } else if (res) {
return child; return res;
} }
} }
var res = getItems(child, match, list); return list ? items : null;
if (list) {
items.push.apply(items, res);
} else if (res) {
return res;
}
} }
return list ? items : null; },
}
return /** @lends Item# */{ // DOCS: Item#getItems
// DOCS: Item#getItems getItems: function(match) {
getItems: function(match) { return Item._getItems(this._children, match, true);
return getItems(this, 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} * {@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 Item._getItems(this.layers, match, true);
return getItems(this, 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} * {@grouptitle Importing / Exporting JSON and SVG}
* *