Skip to content

Commit

Permalink
fix includeDefaultValues (#3497)
Browse files Browse the repository at this point in the history
* fix includeDefaultValues

* Update group.class.js

* Update group.js

* Update group.js
  • Loading branch information
asturur authored Dec 5, 2016
1 parent 1b1441a commit b775323
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
12 changes: 9 additions & 3 deletions src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
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;
max = fabric.util.array.max;

if (fabric.Group) {
return;
Expand Down Expand Up @@ -259,8 +258,15 @@
* @return {Object} object representation of an instance
*/
toObject: function(propertiesToInclude) {
var objsToObject = this.getObjects().map(function(obj) {
var originalDefaults = obj.includeDefaultValues;
obj.includeDefaultValues = false;
var _obj = obj.toObject(propertiesToInclude);
obj.includeDefaultValues = originalDefaults;
return _obj;
});
return extend(this.callSuper('toObject', propertiesToInclude), {
objects: invoke(this._objects, 'toObject', propertiesToInclude)
objects: objsToObject
});
},

Expand Down
15 changes: 10 additions & 5 deletions src/shapes/path_group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
'use strict';

var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
invoke = fabric.util.array.invoke,
parentToObject = fabric.Object.prototype.toObject;
extend = fabric.util.object.extend;

if (fabric.PathGroup) {
fabric.warn('fabric.PathGroup is already defined');
Expand Down Expand Up @@ -153,8 +151,15 @@
* @return {Object} object representation of an instance
*/
toObject: function(propertiesToInclude) {
var o = extend(parentToObject.call(this, ['sourcePath'].concat(propertiesToInclude)), {
paths: invoke(this.getObjects(), 'toObject', propertiesToInclude)
var pathsToObject = this.paths.map(function(path) {
var originalDefaults = path.includeDefaultValues;
path.includeDefaultValues = false;
var obj = path.toObject(propertiesToInclude);
path.includeDefaultValues = originalDefaults;
return obj;
});
var o = extend(this.callSuper('toObject', ['sourcePath'].concat(propertiesToInclude)), {
paths: pathsToObject
});
return o;
},
Expand Down
8 changes: 5 additions & 3 deletions src/static_canvas.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,13 @@
* @private
*/
__serializeBgOverlay: function(methodName, propertiesToInclude) {
var data = {
background: (this.backgroundColor && this.backgroundColor.toObject)
var data = { }

if (this.backgroundColor) {
data.background = this.backgroundColor.toObject
? this.backgroundColor.toObject(propertiesToInclude)
: this.backgroundColor
};
}

if (this.overlayColor) {
data.overlay = this.overlayColor.toObject
Expand Down
6 changes: 2 additions & 4 deletions test/unit/canvas.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {

var EMPTY_JSON = '{"objects":[],"background":""}';
var EMPTY_JSON = '{"objects":[]}';

// var emptyImageCanvasData = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAYAAADL1t+KAAAH7ElEQVR4nO3VMQ0AMAzAsPInvYHoMS2yEeTLHADge/M6AADYM3QACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0AAgwdAAIMHQACDB0A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ALESS_JSON = '{"objects":[{"type":"path","originX":"left","originY":"top","left":100,"top":100,"width":200,"height":200,"fill":"rgb(0,0,0)",' +
'"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' +
'"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' +
'"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"pathOffset":{"x":200,"y":200},"path":"http://example.com/"}],"background":""}';
'"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"pathOffset":{"x":200,"y":200},"path":"http://example.com/"}]}';

var RECT_JSON = '{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":10,"height":10,"fill":"rgb(0,0,0)",' +
'"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' +
Expand Down Expand Up @@ -669,7 +669,6 @@
test('toObject', function() {
ok(typeof canvas.toObject == 'function');
var expectedObject = {
background: canvas.backgroundColor,
objects: canvas.getObjects()
};
deepEqual(expectedObject, canvas.toObject());
Expand All @@ -683,7 +682,6 @@
test('toDatalessObject', function() {
ok(typeof canvas.toDatalessObject == 'function');
var expectedObject = {
background: canvas.backgroundColor,
objects: canvas.getObjects()
};
deepEqual(expectedObject, canvas.toDatalessObject());
Expand Down
9 changes: 3 additions & 6 deletions test/unit/canvas_static.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
var PATH_DATALESS_JSON = '{"objects":[{"type":"path","originX":"left","originY":"top","left":100,"top":100,"width":200,"height":200,"fill":"rgb(0,0,0)",' +
'"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' +
'"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' +
'"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"pathOffset":{"x":200,"y":200},"path":"http://example.com/"}],"background":""}';
'"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"pathOffset":{"x":200,"y":200},"path":"http://example.com/"}]}';

var RECT_JSON = '{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":10,"height":10,"fill":"rgb(0,0,0)",' +
'"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' +
Expand All @@ -38,7 +38,7 @@
var RECT_JSON_WITH_PADDING = '{"objects":[{"type":"rect","originX":"left","originY":"top","left":0,"top":0,"width":10,"height":20,"fill":"rgb(0,0,0)",' +
'"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,' +
'"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,' +
'"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"rx":0,"ry":0,"padding":123,"foo":"bar"}],"background":""}';
'"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"","fillRule":"nonzero","globalCompositeOperation":"source-over","transformMatrix":null,"skewX":0,"skewY":0,"rx":0,"ry":0,"padding":123,"foo":"bar"}]}';

function getAbsolutePath(path) {
var isAbsolute = /^https?:/.test(path);
Expand Down Expand Up @@ -761,7 +761,7 @@

test('toJSON', function() {
ok(typeof canvas.toJSON == 'function');
equal(JSON.stringify(canvas.toJSON()), '{"objects":[],"background":""}');
equal(JSON.stringify(canvas.toJSON()), '{"objects":[]}');
canvas.backgroundColor = '#ff5555';
canvas.overlayColor = 'rgba(0,0,0,0.2)';
equal(JSON.stringify(canvas.toJSON()), '{"objects":[],"background":"#ff5555","overlay":"rgba(0,0,0,0.2)"}', '`background` and `overlay` value should be reflected in json');
Expand Down Expand Up @@ -852,7 +852,6 @@
test('toObject', function() {
ok(typeof canvas.toObject == 'function');
var expectedObject = {
background: canvas.backgroundColor,
objects: canvas.getObjects()
};
deepEqual(expectedObject, canvas.toObject());
Expand Down Expand Up @@ -887,7 +886,6 @@
test('toDatalessObject', function() {
ok(typeof canvas.toDatalessObject == 'function');
var expectedObject = {
background: canvas.backgroundColor,
objects: canvas.getObjects()
};
deepEqual(expectedObject, canvas.toDatalessObject());
Expand All @@ -905,7 +903,6 @@
canvas.foobar = 123;

var expectedObject = {
background: canvas.backgroundColor,
objects: canvas.getObjects(),
freeDrawingColor: 'red',
foobar: 123
Expand Down
19 changes: 16 additions & 3 deletions test/unit/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,29 @@
var group = makeGroupWith2Objects();
group.includeDefaultValues = false;
var clone = group.toObject();

var objects = [{
type: 'rect',
left: 10,
top: -30,
width: 30,
height: 10,
strokeWidth: 0
}, {
type: 'rect',
left: -40,
top: -10,
width: 10,
height: 40,
strokeWidth: 0
}];
var expectedObject = {
'type': 'group',
'left': 50,
'top': 100,
'width': 80,
'height': 60,
'objects': clone.objects
'objects': objects
};

deepEqual(clone, expectedObject);
});

Expand Down

0 comments on commit b775323

Please sign in to comment.