Move getObjects to fabric.Collection. Add support for getObjects(type). Version 1.3.10.

This commit is contained in:
kangax 2013-11-09 14:50:30 +01:00
parent 444416c80e
commit ffe6091dd7
10 changed files with 80 additions and 91 deletions

View file

@ -1,6 +1,6 @@
/*! Fabric.js Copyright 2008-2013, Printio (Juriy Zaytsev, Maxim Chernyak) */
var fabric = fabric || { version: "1.3.9" };
var fabric = fabric || { version: "1.3.10" };
if (typeof exports !== 'undefined') {
exports.fabric = fabric;
}

55
dist/all.js vendored
View file

@ -1,7 +1,7 @@
/* build: `node build.js modules=ALL exclude=gestures minifier=uglifyjs` */
/*! Fabric.js Copyright 2008-2013, Printio (Juriy Zaytsev, Maxim Chernyak) */
var fabric = fabric || { version: "1.3.9" };
var fabric = fabric || { version: "1.3.10" };
if (typeof exports !== 'undefined') {
exports.fabric = fabric;
}
@ -1979,6 +1979,20 @@ fabric.Collection = {
return this;
},
/**
* Returns an array of children objects of this instance
* @param {String} [type] When specified, only objects of this type are returned
* @return {Array}
*/
getObjects: function(type) {
if (typeof type === 'undefined') {
return this._objects;
}
return this._objects.filter(function(o) {
return o.type === type;
});
},
/**
* Returns object at specified index
* @param {Number} index
@ -7285,18 +7299,17 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
* @param {fabric.Object} obj Object that was removed
*/
_onObjectRemoved: function(obj) {
// removing active object should fire "selection:cleared" events
if (this.getActiveObject() === obj) {
this.fire('before:selection:cleared', { target: obj });
this._discardActiveObject();
this.fire('selection:cleared');
}
this.fire('object:removed', { target: obj });
obj.fire('removed');
},
/**
* Returns an array of objects this instance has
* @return {Array}
*/
getObjects: function () {
return this._objects;
},
/**
* Clears specified context of canvas element
* @param {CanvasRenderingContext2D} ctx Context to clear
@ -7780,22 +7793,6 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
},
/* _TO_SVG_END_ */
/**
* Removes an object from canvas and returns it
* @param {fabric.Object} object Object to remove
* @return {fabric.Object} removed object
*/
remove: function (object) {
// removing active object should fire "selection:cleared" events
if (this.getActiveObject() === object) {
this.fire('before:selection:cleared', { target: object });
this.discardActiveObject();
this.fire('selection:cleared');
}
return fabric.Collection.remove.call(this, object);
},
/**
* Moves an object to the bottom of the stack of drawn objects
* @param {fabric.Object} object Object to send to back
@ -16470,14 +16467,6 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
return '#<fabric.Group: (' + this.complexity() + ')>';
},
/**
* Returns an array of all objects in this group
* @return {Array} group objects
*/
getObjects: function() {
return this._objects;
},
/**
* Adds an object to a group; Then recalculates group's dimension, position.
* @param {Object} object

14
dist/all.min.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/all.min.js.gz vendored

Binary file not shown.

55
dist/all.require.js vendored
View file

@ -1,7 +1,7 @@
/* build: `node build.js modules=ALL exclude=gestures minifier=uglifyjs` */
/*! Fabric.js Copyright 2008-2013, Printio (Juriy Zaytsev, Maxim Chernyak) */
var fabric = fabric || { version: "1.3.9" };
var fabric = fabric || { version: "1.3.10" };
if (typeof exports !== 'undefined') {
exports.fabric = fabric;
}
@ -1979,6 +1979,20 @@ fabric.Collection = {
return this;
},
/**
* Returns an array of children objects of this instance
* @param {String} [type] When specified, only objects of this type are returned
* @return {Array}
*/
getObjects: function(type) {
if (typeof type === 'undefined') {
return this._objects;
}
return this._objects.filter(function(o) {
return o.type === type;
});
},
/**
* Returns object at specified index
* @param {Number} index
@ -7285,18 +7299,17 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
* @param {fabric.Object} obj Object that was removed
*/
_onObjectRemoved: function(obj) {
// removing active object should fire "selection:cleared" events
if (this.getActiveObject() === obj) {
this.fire('before:selection:cleared', { target: obj });
this._discardActiveObject();
this.fire('selection:cleared');
}
this.fire('object:removed', { target: obj });
obj.fire('removed');
},
/**
* Returns an array of objects this instance has
* @return {Array}
*/
getObjects: function () {
return this._objects;
},
/**
* Clears specified context of canvas element
* @param {CanvasRenderingContext2D} ctx Context to clear
@ -7780,22 +7793,6 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
},
/* _TO_SVG_END_ */
/**
* Removes an object from canvas and returns it
* @param {fabric.Object} object Object to remove
* @return {fabric.Object} removed object
*/
remove: function (object) {
// removing active object should fire "selection:cleared" events
if (this.getActiveObject() === object) {
this.fire('before:selection:cleared', { target: object });
this.discardActiveObject();
this.fire('selection:cleared');
}
return fabric.Collection.remove.call(this, object);
},
/**
* Moves an object to the bottom of the stack of drawn objects
* @param {fabric.Object} object Object to send to back
@ -16470,14 +16467,6 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
return '#<fabric.Group: (' + this.complexity() + ')>';
},
/**
* Returns an array of all objects in this group
* @return {Array} group objects
*/
getObjects: function() {
return this._objects;
},
/**
* Adds an object to a group; Then recalculates group's dimension, position.
* @param {Object} object

View file

@ -1,7 +1,7 @@
{
"name": "fabric",
"description": "Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.",
"version": "1.3.9",
"version": "1.3.10",
"author": "Juriy Zaytsev <kangax@gmail.com>",
"keywords": ["canvas", "graphic", "graphics", "SVG", "node-canvas", "parser", "HTML5", "object model"],
"repository": "git://github.com/kangax/fabric.js",

View file

@ -79,6 +79,20 @@ fabric.Collection = {
return this;
},
/**
* Returns an array of children objects of this instance
* @param {String} [type] When specified, only objects of this type are returned
* @return {Array}
*/
getObjects: function(type) {
if (typeof type === 'undefined') {
return this._objects;
}
return this._objects.filter(function(o) {
return o.type === type;
});
},
/**
* Returns object at specified index
* @param {Number} index

View file

@ -104,14 +104,6 @@
return '#<fabric.Group: (' + this.complexity() + ')>';
},
/**
* Returns an array of all objects in this group
* @return {Array} group objects
*/
getObjects: function() {
return this._objects;
},
/**
* Adds an object to a group; Then recalculates group's dimension, position.
* @param {Object} object

View file

@ -571,14 +571,6 @@
obj.fire('removed');
},
/**
* Returns an array of objects this instance has
* @return {Array}
*/
getObjects: function () {
return this._objects;
},
/**
* Clears specified context of canvas element
* @param {CanvasRenderingContext2D} ctx Context to clear

View file

@ -162,6 +162,19 @@
equal(canvas.getObjects().length, 0, 'should have a 0 length when empty');
});
test('getObjects with type', function() {
var rect = new fabric.Rect({ width: 10, height: 20 });
var circle = new fabric.Circle({ radius: 30 });
canvas.add(rect, circle);
equal(canvas.getObjects().length, 2, 'should have length=2 initially');
deepEqual(canvas.getObjects('rect'), [rect], 'should return rect only');
deepEqual(canvas.getObjects('circle'), [circle], 'should return circle only');
});
test('getElement', function() {
ok(typeof canvas.getElement == 'function', 'should respond to `getElement` method');
equal(canvas.getElement(), lowerCanvasEl, 'should return a proper element');