Check for callback presence in fabric.Group.fromObject.

This commit is contained in:
kangax 2012-06-21 09:59:48 +02:00
parent 126c2aa507
commit dd43c23e86
4 changed files with 145 additions and 143 deletions

143
dist/all.js vendored
View file

@ -10713,32 +10713,32 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
//= require "object.class"
(function(global){
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
min = fabric.util.array.min,
max = fabric.util.array.max,
invoke = fabric.util.array.invoke,
removeFromArray = fabric.util.removeFromArray;
if (fabric.Group) {
return;
}
/**
/**
* @class Group
* @extends fabric.Object
*/
fabric.Group = fabric.util.createClass(fabric.Object, /** @scope fabric.Group.prototype */ {
/**
* @property
* @type String
*/
type: 'group',
/**
* Constructor
* @method initialized
@ -10749,24 +10749,24 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
initialize: function(objects, options) {
this.objects = objects || [];
this.originalState = { };
this.callSuper('initialize');
this._calcBounds();
this._updateObjectsCoords();
if (options) {
extend(this, options);
}
this._setOpacityIfSame();
// group is active by default
this.setCoords(true);
this.saveCoords();
//this.activateAllObjects();
},
/**
* @private
* @method _updateObjectsCoords
@ -10774,25 +10774,25 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
_updateObjectsCoords: function() {
var groupDeltaX = this.left,
groupDeltaY = this.top;
this.forEachObject(function(object) {
var objectLeft = object.get('left'),
objectTop = object.get('top');
object.set('originalLeft', objectLeft);
object.set('originalTop', objectTop);
object.set('left', objectLeft - groupDeltaX);
object.set('top', objectTop - groupDeltaY);
object.setCoords();
// do not display corners of objects enclosed in a group
object.hideCorners = true;
}, this);
},
/**
* Returns string represenation of a group
* @method toString
@ -10801,7 +10801,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
toString: function() {
return '#<fabric.Group: (' + this.complexity() + ')>';
},
/**
* Returns an array of all objects in this group
* @method getObjects
@ -10810,7 +10810,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
getObjects: function() {
return this.objects;
},
/**
* Adds an object to a group; Then recalculates group's dimension, position.
* @method addWithUpdate
@ -10825,7 +10825,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
this._updateObjectsCoords();
return this;
},
/**
* Removes an object from a group; Then recalculates group's dimension, position.
* @method removeWithUpdate
@ -10841,7 +10841,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
this._updateObjectsCoords();
return this;
},
/**
* Adds an object to a group
* @method add
@ -10853,7 +10853,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
this.objects.push(object);
return this;
},
/**
* Removes an object from a group
* @method remove
@ -10863,8 +10863,9 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
*/
remove: function(object) {
removeFromArray(this.objects, object);
return this;
},
/**
* Returns a size of a group (i.e: length of an array containing its objects)
* @return {Number} Group size
@ -10872,7 +10873,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
size: function() {
return this.getObjects().length;
},
/**
* Sets property to a given value
* @method set
@ -10900,7 +10901,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
}
return this;
},
/**
* Returns true if a group contains an object
* @method contains
@ -10910,7 +10911,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
contains: function(object) {
return this.objects.indexOf(object) > -1;
},
/**
* Returns object representation of an instance
* @method toObject
@ -10921,7 +10922,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
objects: invoke(this.objects, 'clone')
});
},
/**
* Renders instance on a given context
* @method render
@ -10930,9 +10931,9 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
render: function(ctx, noTransform) {
ctx.save();
this.transform(ctx);
var groupScaleFactor = Math.max(this.scaleX, this.scaleY);
for (var i = 0, len = this.objects.length, object; object = this.objects[i]; i++) {
var originalScaleFactor = object.borderScaleFactor;
object.borderScaleFactor = groupScaleFactor;
@ -10946,7 +10947,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
ctx.restore();
this.setCoords();
},
/**
* Returns object from the group at the specified index
* @method item
@ -10956,7 +10957,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
item: function(index) {
return this.getObjects()[index];
},
/**
* Returns complexity of an instance
* @method complexity
@ -10968,7 +10969,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
return total;
}, 0);
},
/**
* Retores original state of each of group objects (original state is that which was before group was created).
* @private
@ -10980,7 +10981,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
this.objects.forEach(this._restoreObjectState, this);
return this;
},
/**
* Restores original state of a specified object in group
* @private
@ -10989,7 +10990,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
* @return {fabric.Group} thisArg
*/
_restoreObjectState: function(object) {
var groupLeft = this.get('left'),
groupTop = this.get('top'),
groupAngle = this.getAngle() * (Math.PI / 180),
@ -10997,23 +10998,23 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
objectTop = object.get('originalTop'),
rotatedTop = Math.cos(groupAngle) * object.get('top') + Math.sin(groupAngle) * object.get('left'),
rotatedLeft = -Math.sin(groupAngle) * object.get('top') + Math.cos(groupAngle) * object.get('left');
object.setAngle(object.getAngle() + this.getAngle());
object.set('left', groupLeft + rotatedLeft * this.get('scaleX'));
object.set('top', groupTop + rotatedTop * this.get('scaleY'));
object.set('scaleX', object.get('scaleX') * this.get('scaleX'));
object.set('scaleY', object.get('scaleY') * this.get('scaleY'));
object.setCoords();
object.hideCorners = false;
object.setActive(false);
object.setCoords();
return this;
},
/**
* Destroys a group (restoring state of its objects)
* @method destroy
@ -11023,7 +11024,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
destroy: function() {
return this._restoreObjectsState();
},
/**
* Saves coordinates of this instance (to be used together with `hasMoved`)
* @saveCoords
@ -11035,7 +11036,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
this._originalTop = this.get('top');
return this;
},
/**
* Checks whether this group was moved (since `saveCoords` was called last)
* @method hasMoved
@ -11045,7 +11046,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
return this._originalLeft !== this.get('left') ||
this._originalTop !== this.get('top');
},
/**
* Sets coordinates of all group objects
* @method setObjectsCoords
@ -11058,7 +11059,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
});
return this;
},
/**
* Activates (makes active) all group objects
* @method activateAllObjects
@ -11071,15 +11072,15 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
});
return this;
},
/**
* Executes given function for each object in this group
* @method forEachObject
* @param {Function} callback
* Callback invoked with current object as first argument,
* @param {Function} callback
* Callback invoked with current object as first argument,
* index - as second and an array of all objects - as third.
* Iteration happens in reverse order (for performance reasons).
* Callback is invoked in a context of Global Object (e.g. `window`)
* Callback is invoked in a context of Global Object (e.g. `window`)
* when no `context` argument is given
*
* @param {Object} context Context (aka thisObject)
@ -11088,7 +11089,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
* @chainable
*/
forEachObject: fabric.StaticCanvas.prototype.forEachObject,
/**
* @private
* @method _setOpacityIfSame
@ -11096,24 +11097,24 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
_setOpacityIfSame: function() {
var objects = this.getObjects(),
firstValue = objects[0] ? objects[0].get('opacity') : 1;
var isSameOpacity = objects.every(function(o) {
return o.get('opacity') === firstValue;
});
if (isSameOpacity) {
this.opacity = firstValue;
}
},
/**
* @private
* @method _calcBounds
*/
_calcBounds: function() {
var aX = [],
aY = [],
minX, minY, maxX, maxY, o, width, height,
var aX = [],
aY = [],
minX, minY, maxX, maxY, o, width, height,
i = 0,
len = this.objects.length;
@ -11125,22 +11126,22 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
aY.push(o.oCoords[prop].y);
}
};
minX = min(aX);
maxX = max(aX);
minY = min(aY);
maxY = max(aY);
width = (maxX - minX) || 0;
height = (maxY - minY) || 0;
this.width = width;
this.height = height;
this.left = (minX + width / 2) || 0;
this.top = (minY + height / 2) || 0;
},
/**
* Checks if point is contained within the group
* @method containsPoint
@ -11148,18 +11149,18 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
* @return {Boolean} true if point is contained within group
*/
containsPoint: function(point) {
var halfWidth = this.get('width') / 2,
halfHeight = this.get('height') / 2,
centerX = this.get('left'),
centerY = this.get('top');
return centerX - halfWidth < point.x &&
return centerX - halfWidth < point.x &&
centerX + halfWidth > point.x &&
centerY - halfHeight < point.y &&
centerY + halfHeight > point.y;
},
/**
* Makes all of this group's objects grayscale (i.e. calling `toGrayscale` on them)
* @method toGrayscale
@ -11171,7 +11172,7 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
}
}
});
/**
* Returns fabric.Group instance from an object representation
* @static
@ -11183,12 +11184,12 @@ fabric.util.object.extend(fabric.StaticCanvas.prototype, {
fabric.Group.fromObject = function(object, callback) {
fabric.util.enlivenObjects(object.objects, function(enlivenedObjects) {
delete object.objects;
callback(new fabric.Group(enlivenedObjects, object));
callback && callback(new fabric.Group(enlivenedObjects, object));
});
};
fabric.Group.async = true;
})(typeof exports != 'undefined' ? exports : this);
//= require "object.class"

2
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.

View file

@ -1,32 +1,32 @@
//= require "object.class"
(function(global){
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
min = fabric.util.array.min,
max = fabric.util.array.max,
invoke = fabric.util.array.invoke,
removeFromArray = fabric.util.removeFromArray;
if (fabric.Group) {
return;
}
/**
/**
* @class Group
* @extends fabric.Object
*/
fabric.Group = fabric.util.createClass(fabric.Object, /** @scope fabric.Group.prototype */ {
/**
* @property
* @type String
*/
type: 'group',
/**
* Constructor
* @method initialized
@ -37,24 +37,24 @@
initialize: function(objects, options) {
this.objects = objects || [];
this.originalState = { };
this.callSuper('initialize');
this._calcBounds();
this._updateObjectsCoords();
if (options) {
extend(this, options);
}
this._setOpacityIfSame();
// group is active by default
this.setCoords(true);
this.saveCoords();
//this.activateAllObjects();
},
/**
* @private
* @method _updateObjectsCoords
@ -62,25 +62,25 @@
_updateObjectsCoords: function() {
var groupDeltaX = this.left,
groupDeltaY = this.top;
this.forEachObject(function(object) {
var objectLeft = object.get('left'),
objectTop = object.get('top');
object.set('originalLeft', objectLeft);
object.set('originalTop', objectTop);
object.set('left', objectLeft - groupDeltaX);
object.set('top', objectTop - groupDeltaY);
object.setCoords();
// do not display corners of objects enclosed in a group
object.hideCorners = true;
}, this);
},
/**
* Returns string represenation of a group
* @method toString
@ -89,7 +89,7 @@
toString: function() {
return '#<fabric.Group: (' + this.complexity() + ')>';
},
/**
* Returns an array of all objects in this group
* @method getObjects
@ -98,7 +98,7 @@
getObjects: function() {
return this.objects;
},
/**
* Adds an object to a group; Then recalculates group's dimension, position.
* @method addWithUpdate
@ -113,7 +113,7 @@
this._updateObjectsCoords();
return this;
},
/**
* Removes an object from a group; Then recalculates group's dimension, position.
* @method removeWithUpdate
@ -129,7 +129,7 @@
this._updateObjectsCoords();
return this;
},
/**
* Adds an object to a group
* @method add
@ -141,7 +141,7 @@
this.objects.push(object);
return this;
},
/**
* Removes an object from a group
* @method remove
@ -151,8 +151,9 @@
*/
remove: function(object) {
removeFromArray(this.objects, object);
return this;
},
/**
* Returns a size of a group (i.e: length of an array containing its objects)
* @return {Number} Group size
@ -160,7 +161,7 @@
size: function() {
return this.getObjects().length;
},
/**
* Sets property to a given value
* @method set
@ -188,7 +189,7 @@
}
return this;
},
/**
* Returns true if a group contains an object
* @method contains
@ -198,7 +199,7 @@
contains: function(object) {
return this.objects.indexOf(object) > -1;
},
/**
* Returns object representation of an instance
* @method toObject
@ -209,7 +210,7 @@
objects: invoke(this.objects, 'clone')
});
},
/**
* Renders instance on a given context
* @method render
@ -218,9 +219,9 @@
render: function(ctx, noTransform) {
ctx.save();
this.transform(ctx);
var groupScaleFactor = Math.max(this.scaleX, this.scaleY);
for (var i = 0, len = this.objects.length, object; object = this.objects[i]; i++) {
var originalScaleFactor = object.borderScaleFactor;
object.borderScaleFactor = groupScaleFactor;
@ -234,7 +235,7 @@
ctx.restore();
this.setCoords();
},
/**
* Returns object from the group at the specified index
* @method item
@ -244,7 +245,7 @@
item: function(index) {
return this.getObjects()[index];
},
/**
* Returns complexity of an instance
* @method complexity
@ -256,7 +257,7 @@
return total;
}, 0);
},
/**
* Retores original state of each of group objects (original state is that which was before group was created).
* @private
@ -268,7 +269,7 @@
this.objects.forEach(this._restoreObjectState, this);
return this;
},
/**
* Restores original state of a specified object in group
* @private
@ -277,7 +278,7 @@
* @return {fabric.Group} thisArg
*/
_restoreObjectState: function(object) {
var groupLeft = this.get('left'),
groupTop = this.get('top'),
groupAngle = this.getAngle() * (Math.PI / 180),
@ -285,23 +286,23 @@
objectTop = object.get('originalTop'),
rotatedTop = Math.cos(groupAngle) * object.get('top') + Math.sin(groupAngle) * object.get('left'),
rotatedLeft = -Math.sin(groupAngle) * object.get('top') + Math.cos(groupAngle) * object.get('left');
object.setAngle(object.getAngle() + this.getAngle());
object.set('left', groupLeft + rotatedLeft * this.get('scaleX'));
object.set('top', groupTop + rotatedTop * this.get('scaleY'));
object.set('scaleX', object.get('scaleX') * this.get('scaleX'));
object.set('scaleY', object.get('scaleY') * this.get('scaleY'));
object.setCoords();
object.hideCorners = false;
object.setActive(false);
object.setCoords();
return this;
},
/**
* Destroys a group (restoring state of its objects)
* @method destroy
@ -311,7 +312,7 @@
destroy: function() {
return this._restoreObjectsState();
},
/**
* Saves coordinates of this instance (to be used together with `hasMoved`)
* @saveCoords
@ -323,7 +324,7 @@
this._originalTop = this.get('top');
return this;
},
/**
* Checks whether this group was moved (since `saveCoords` was called last)
* @method hasMoved
@ -333,7 +334,7 @@
return this._originalLeft !== this.get('left') ||
this._originalTop !== this.get('top');
},
/**
* Sets coordinates of all group objects
* @method setObjectsCoords
@ -346,7 +347,7 @@
});
return this;
},
/**
* Activates (makes active) all group objects
* @method activateAllObjects
@ -359,15 +360,15 @@
});
return this;
},
/**
* Executes given function for each object in this group
* @method forEachObject
* @param {Function} callback
* Callback invoked with current object as first argument,
* @param {Function} callback
* Callback invoked with current object as first argument,
* index - as second and an array of all objects - as third.
* Iteration happens in reverse order (for performance reasons).
* Callback is invoked in a context of Global Object (e.g. `window`)
* Callback is invoked in a context of Global Object (e.g. `window`)
* when no `context` argument is given
*
* @param {Object} context Context (aka thisObject)
@ -376,7 +377,7 @@
* @chainable
*/
forEachObject: fabric.StaticCanvas.prototype.forEachObject,
/**
* @private
* @method _setOpacityIfSame
@ -384,24 +385,24 @@
_setOpacityIfSame: function() {
var objects = this.getObjects(),
firstValue = objects[0] ? objects[0].get('opacity') : 1;
var isSameOpacity = objects.every(function(o) {
return o.get('opacity') === firstValue;
});
if (isSameOpacity) {
this.opacity = firstValue;
}
},
/**
* @private
* @method _calcBounds
*/
_calcBounds: function() {
var aX = [],
aY = [],
minX, minY, maxX, maxY, o, width, height,
var aX = [],
aY = [],
minX, minY, maxX, maxY, o, width, height,
i = 0,
len = this.objects.length;
@ -413,22 +414,22 @@
aY.push(o.oCoords[prop].y);
}
};
minX = min(aX);
maxX = max(aX);
minY = min(aY);
maxY = max(aY);
width = (maxX - minX) || 0;
height = (maxY - minY) || 0;
this.width = width;
this.height = height;
this.left = (minX + width / 2) || 0;
this.top = (minY + height / 2) || 0;
},
/**
* Checks if point is contained within the group
* @method containsPoint
@ -436,18 +437,18 @@
* @return {Boolean} true if point is contained within group
*/
containsPoint: function(point) {
var halfWidth = this.get('width') / 2,
halfHeight = this.get('height') / 2,
centerX = this.get('left'),
centerY = this.get('top');
return centerX - halfWidth < point.x &&
return centerX - halfWidth < point.x &&
centerX + halfWidth > point.x &&
centerY - halfHeight < point.y &&
centerY + halfHeight > point.y;
},
/**
* Makes all of this group's objects grayscale (i.e. calling `toGrayscale` on them)
* @method toGrayscale
@ -459,7 +460,7 @@
}
}
});
/**
* Returns fabric.Group instance from an object representation
* @static
@ -471,10 +472,10 @@
fabric.Group.fromObject = function(object, callback) {
fabric.util.enlivenObjects(object.objects, function(enlivenedObjects) {
delete object.objects;
callback(new fabric.Group(enlivenedObjects, object));
callback && callback(new fabric.Group(enlivenedObjects, object));
});
};
fabric.Group.async = true;
})(typeof exports != 'undefined' ? exports : this);