* Stringify custom attributes-objects

* Stringify custom attributes-objects
This commit is contained in:
ada99
2016-07-09 16:54:28 +03:00
committed by Anton Lavrenov
parent 886d0c970e
commit 95edb44782
6 changed files with 123 additions and 18 deletions

View File

@@ -2336,6 +2336,42 @@ suite('Node', function() {
assert.equal(circle.toJSON(), expectedJson);
});
// ======================================================
test('serialize shape with custom attributes', function() {
var stage = addStage();
var layer = new Konva.Layer();
var group = new Konva.Group();
var circle = new Konva.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myCircle',
draggable: true
});
stage.add(layer);
layer.add(group);
group.add(circle);
layer.draw();
circle.setAttr('customAttr', 3);
circle.setAttr('customAttrObj', {
x:1,
y:5,
size: {
width: 10,
height: 20
}
});
var expectedJson = '{"attrs":{"x":289,"y":100,"radius":70,"fill":"green","stroke":"black","strokeWidth":4,"name":"myCircle","draggable":true,"customAttr":3,"customAttrObj":{"x":1,"y":5,"size":{"width":10,"height":20}}},"className":"Circle"}';
assert.equal(circle.toJSON(), expectedJson);
});
// ======================================================
test('load stage using json', function() {
var container = addContainer();
@@ -2405,7 +2441,7 @@ suite('Node', function() {
context.closePath();
context.fillStrokeShape(this);
};
var json = '{"attrs":{"width":578,"height":200},"className":"Stage","children":[{"attrs":{},"className":"Layer","children":[{"attrs":{},"className":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"id":"myTriangle"},"className":"Shape"}]}]}]}';
var json = '{"attrs":{"width":578,"height":200},"className":"Stage","children":[{"attrs":{},"className":"Layer","children":[{"attrs":{},"className":"Group","children":[{"attrs":{"fill":"#00D2FF","stroke":"black","strokeWidth":4,"id":"myTriangle","customAttrObj":{"x":1,"y":5,"size":{"width":10,"height":20}}},"className":"Shape"}]}]}]}';
var stage = Konva.Node.create(json, container);

View File

@@ -39,4 +39,25 @@ suite('Util', function(){
a : 1
});
});
test('test _prepareToStringify', function() {
var o = {
a: 1,
b: 'string1'
};
o.c = {
d: 'string2',
e: o,
f: document.createElement('p')
};
o.g = o;
assert.deepEqual(Konva.Util._prepareToStringify(o), {
a: 1,
b: 'string1',
c: {
d: 'string2'
}
})
});
});