Implement handling of empty and editable in #getItems(), and simplify code a bit.

This commit is contained in:
Jürg Lehni 2014-10-18 14:32:21 +02:00
parent e7fd751765
commit 616e848376
3 changed files with 66 additions and 71 deletions

View file

@ -1823,18 +1823,22 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
}
return true;
}
if (arguments.length === 1) {
if (typeof name === 'object') {
// `name` is the match object, not a string
for (var key in name) {
if (name.hasOwnProperty(key) && !this.matches(key, name[key]))
return false;
}
} else {
var value = this[name];
// Support legacy Item#type property to match hyphenated
// class-names.
if (value === undefined && name === 'type')
value = Base.hyphenate(this._class);
var value = /^(empty|editable)$/.test(name)
// Handle boolean test functions separately, by calling them
// to get the value.
? this['is' + Base.capitalize(name)]()
// Support legacy Item#type property to match hyphenated
// class-names.
: name === 'type'
? Base.hyphenate(this._class)
: this[name];
if (/^(constructor|class)$/.test(name)) {
if (!(this instanceof compare))
return false;
@ -1844,9 +1848,6 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
} else if (typeof compare === 'function') {
if (!compare(value))
return false;
} else if (typeof value === 'function') {
if (!value.call(this, compare))
return false;
} else if (Base.isPlainObject(compare)) {
if (!matchObject(compare, value))
return false;
@ -1872,7 +1873,7 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* @return {Item[]}
*/
getItems: function(match) {
return Item._getItems(this._children, match, true);
return Item._getItems(this._children, match);
},
/**
@ -1889,31 +1890,27 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
* @return {Item}
*/
getItem: function(match) {
return Item._getItems(this._children, match, false);
return Item._getItems(this._children, match, true)[0] || null;
},
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 && [];
_getItems: function _getItems(children, match, firstOnly) {
var items = [];
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.apply(items, res);
} else if (res) {
return res;
items.push(child);
if (firstOnly)
return items;
}
var res = _getItems(child._children, match, firstOnly);
items.push.apply(items, res);
if (firstOnly && items.length > 0)
return items;
}
return list ? items : null;
return items;
}
}
}, /** @lends Item# */{

View file

@ -554,7 +554,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
* @return {Item[]}
*/
getItems: function(match) {
return Item._getItems(this.layers, match, true);
return Item._getItems(this.layers, match);
},
/**
@ -763,7 +763,7 @@ var Project = PaperScopeItem.extend(/** @lends Project# */{
* @return {Item}
*/
getItem: function(match) {
return Item._getItems(this.layers, match, false);
return Item._getItems(this.layers, match, true)[0] || null;
},
/**

View file

@ -15,18 +15,16 @@ module('Getting and Matching Items');
test('Item#getItems()', function() {
var group = new Group([new Path({ selected: true }), new Raster()]);
equals(function() {
var items = group.getItems({
return group.getItems({
type: 'path'
});
return items.length == 1;
}, true);
}).length;
}, 1);
equals(function() {
var items = group.getItems({
return group.getItems({
selected: true
});
return items.length == 1;
}, true);
}).length;
}, 1);
});
test('Item#matches()', function() {
@ -47,48 +45,48 @@ test('Item#matches()', function() {
test('Project#getItems()', function() {
var layer = new Layer();
var matches = paper.project.getItems({
type: 'layer'
});
equals(function() {
var matches = paper.project.getItems({
type: 'layer'
});
return matches.length == 1 && matches[0] == layer;
}, true);
var matches = paper.project.getItems({
class: Item
});
equals(function() {
var matches = paper.project.getItems({
class: Item
});
return matches.length == 1 && matches[0] == layer;
}, true);
var path = new Path();
var matches = paper.project.getItems({
type: 'path'
});
equals(function() {
var matches = paper.project.getItems({
type: 'path'
});
return matches.length == 1 && matches[0] == path;
}, true);
var matches = paper.project.getItems({
constructor: Path
});
equals(function() {
var matches = paper.project.getItems({
constructor: Path
});
return matches.length == 1 && matches[0] === path;
}, true);
var group = new Group();
var matches = paper.project.getItems({
type: 'group'
});
equals(function() {
var matches = paper.project.getItems({
type: 'group'
});
return matches.length == 1 && matches[0] === group
}, true);
var raster = new Raster();
var matches = paper.project.getItems({
type: 'raster'
});
equals(function() {
var matches = paper.project.getItems({
type: 'raster'
});
return matches.length == 1 && matches[0] === raster
}, true);
@ -98,15 +96,15 @@ test('Project#getItems()', function() {
}).length;
}, 0);
raster.selected = true;
equals(function() {
raster.selected = true;
return paper.project.getItems({
selected: true
}).length;
}, 2);
raster.selected = true;
equals(function() {
raster.selected = true;
return paper.project.getItems({
selected: true,
type: 'raster'
@ -120,12 +118,12 @@ test('Project#getItems() with compare function', function() {
opacity: 0.5
});
var items = paper.project.getItems({
opacity: function(value) {
return value < 1
}
});
equals(function() {
var items = paper.project.getItems({
opacity: function(value) {
return value < 1
}
});
return items.length == 1 && items[0] == path;
}, true);
});
@ -136,11 +134,11 @@ test('Project#getItems() with specific property value', function() {
opacity: 0.5
});
var items = paper.project.getItems({
opacity: 1,
type: 'path'
});
equals(function() {
var items = paper.project.getItems({
opacity: 1,
type: 'path'
});
return items.length == 1 && items[0] == path;
}, true);
});
@ -154,11 +152,11 @@ test('Project#getItems() with color', function() {
fillColor: 'black'
});
var items = paper.project.getItems({
fillColor: 'red',
type: 'path'
});
equals(function() {
var items = paper.project.getItems({
fillColor: 'red',
type: 'path'
});
return items.length == 1 && items[0] == path;
}, true);
});
@ -176,10 +174,10 @@ test('Project#getItems() with regex function', function() {
name: 'starting'
});
var items = paper.project.getItems({
name: /^start/g
});
equals(function() {
var items = paper.project.getItems({
name: /^start/g
});
return items.length == 1 && items[0] == path;
}, true);