Skip to content

Commit

Permalink
Issue 3832 Add toDatalessObject to Group class since it may contains …
Browse files Browse the repository at this point in the history
…pathGroups. (#3863)

* fix toDatalessObject

* added test
  • Loading branch information
asturur authored Apr 22, 2017
1 parent ba19885 commit d95612e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@
});
},

/**
* Returns object representation of an instance, in dataless mode.
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {Object} object representation of an instance
*/
toDatalessObject: function(propertiesToInclude) {
var objsToObject = this.getObjects().map(function(obj) {
var originalDefaults = obj.includeDefaultValues;
obj.includeDefaultValues = obj.group.includeDefaultValues;
var _obj = obj.toDatalessObject(propertiesToInclude);
obj.includeDefaultValues = originalDefaults;
return _obj;
});
return extend(this.callSuper('toDatalessObject', propertiesToInclude), {
objects: objsToObject
});
},

/**
* Renders instance on a given context
* @param {CanvasRenderingContext2D} ctx context to render instance on
Expand Down
6 changes: 2 additions & 4 deletions src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,11 +1296,9 @@
/**
* Renders controls and borders for the object
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Boolean} [noTransform] When true, context is not transformed
*/
_renderControls: function(ctx, noTransform) {
if (!this.active || noTransform
|| (this.group && this.group !== this.canvas.getActiveGroup())) {
_renderControls: function(ctx) {
if (!this.active || (this.group && this.group !== this.canvas.getActiveGroup())) {
return;
}

Expand Down
10 changes: 10 additions & 0 deletions test/unit/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,16 @@
equal(isTransparent(ctx, 11, 11, 0), false, '11,11 is opaque');
equal(isTransparent(ctx, 12, 12, 0), true, '12,12 is transparent');
});

test('group toDatalessObject', function() {
var rect1 = new fabric.Rect({ top: 1, left: 1, width: 2, height: 2, strokeWidth: 0, fill: 'red', opacity: 1, objectCaching: false}),
rect2 = new fabric.Rect({ top: 5, left: 5, width: 2, height: 2, strokeWidth: 0, fill: 'red', opacity: 1, objectCaching: false}),
pathGroup = new fabric.PathGroup([rect1, rect2], { sourcePath: 'sourcePath'}),
group = new fabric.Group([pathGroup]),
dataless = group.toDatalessObject();

equal(dataless.objects[0].paths, 'sourcePath', 'the paths have been changed with the sourcePath');
});
// asyncTest('cloning group with image', function() {
// var rect = new fabric.Rect({ top: 100, left: 100, width: 30, height: 10 }),
// img = new fabric.Image(_createImageElement()),
Expand Down

0 comments on commit d95612e

Please sign in to comment.