Skip to content

Commit 4fb6a5d

Browse files
committed
Implement #getItems(match) on Project and Item as a simple and efficient query interface.
1 parent 49a8ea2 commit 4fb6a5d

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

src/item/Item.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,56 @@ var Item = Base.extend(Callback, /** @lends Item# */{
12341234
return this._parent ? this._parent.isInserted() : false;
12351235
},
12361236

1237+
// DOCS: Item#getItems
1238+
getItems: function(match) {
1239+
var children = this.children,
1240+
items = [];
1241+
for (var i = 0, l = children && children.length; i < l; i++) {
1242+
var child = children[i];
1243+
if (child.matches(match))
1244+
items.push(child);
1245+
items.push.apply(items, child.getItems(match));
1246+
}
1247+
return items;
1248+
},
1249+
1250+
// DOCS: Item#matches
1251+
matches: function(match) {
1252+
// matchObject() is used to match against objects in a nested manner.
1253+
// This is useful for matching against Item#data.
1254+
function matchObject(obj1, obj2) {
1255+
for (var i in obj1) {
1256+
if (obj1.hasOwnProperty(i)) {
1257+
var val1 = obj1[i],
1258+
val2 = obj2[i];
1259+
if (Base.isPlainObject(val1) && Base.isPlainObject(val2)) {
1260+
if (!matchObject(val1, val2))
1261+
return false;
1262+
} else if (!Base.equals(val1, val2)) {
1263+
return false;
1264+
}
1265+
}
1266+
}
1267+
return true;
1268+
}
1269+
for (var key in match) {
1270+
if (match.hasOwnProperty(key)) {
1271+
var value = this[key],
1272+
compare = match[key];
1273+
if (compare instanceof RegExp) {
1274+
if (!compare.test(value))
1275+
return false;
1276+
} else if (Base.isPlainObject(compare)) {
1277+
if (!matchObject(compare, value))
1278+
return false;
1279+
} else if (!Base.equals(value, compare)) {
1280+
return false;
1281+
}
1282+
}
1283+
}
1284+
return true;
1285+
},
1286+
12371287
equals: function(item) {
12381288
// Note: We do not compare name and selected state.
12391289
return item === this || item && this._class === item._class

src/project/Project.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
154154
return this._index;
155155
},
156156

157+
// DOCS: Project#getItems
158+
getItems: function(match) {
159+
var layers = this.layers,
160+
items = [];
161+
for (var i = 0, l = layers.length; i < l; i++)
162+
items.push.apply(items, layers[i].getItems(match));
163+
return items;
164+
},
165+
157166
/**
158167
* The selected items contained within the project.
159168
*
@@ -202,16 +211,18 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
202211
* Selects all items in the project.
203212
*/
204213
selectAll: function() {
205-
for (var i = 0, l = this.layers.length; i < l; i++)
206-
this.layers[i].setFullySelected(true);
214+
var layers = this.layers;
215+
for (var i = 0, l = layers.length; i < l; i++)
216+
layers[i].setFullySelected(true);
207217
},
208218

209219
/**
210220
* Deselects all selected items in the project.
211221
*/
212222
deselectAll: function() {
213-
for (var i in this._selectedItems)
214-
this._selectedItems[i].setFullySelected(false);
223+
var selectedItems = this._selectedItems;
224+
for (var i in selectedItems)
225+
selectedItems[i].setFullySelected(false);
215226
},
216227

217228
/**

0 commit comments

Comments
 (0)