mirror of
https://github.com/konvajs/konva.git
synced 2025-12-04 19:08:24 +08:00
continued work on serialization/de-serialization
This commit is contained in:
41
src/Stage.js
41
src/Stage.js
@@ -191,20 +191,22 @@ Kinetic.Stage.prototype = {
|
||||
|
||||
function addNode(node) {
|
||||
var obj = {};
|
||||
obj.attrs = {};
|
||||
|
||||
// copy attrs
|
||||
for(var key in node) {
|
||||
if(node.hasOwnProperty(key) && go.arrayHas(node.jsonProps, key)) {
|
||||
obj[key] = node[key];
|
||||
obj.attrs[key.replace('_', '')] = node[key];
|
||||
}
|
||||
}
|
||||
|
||||
if(node.nodeType !== 'Shape') {
|
||||
obj._children = [];
|
||||
obj.children = [];
|
||||
|
||||
var children = node.getChildren();
|
||||
for(var n = 0; n < children.length; n++) {
|
||||
var child = children[n];
|
||||
obj._children.push(addNode(child));
|
||||
obj.children.push(addNode(child));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,41 +217,48 @@ Kinetic.Stage.prototype = {
|
||||
/**
|
||||
* load stage with JSON string
|
||||
*/
|
||||
load: function(json) {
|
||||
load: function(json, drawFuncs) {
|
||||
function loadNode(node, obj) {
|
||||
// copy properties over
|
||||
for(var key in obj) {
|
||||
node[key] = obj[key];
|
||||
// if custom shape then set draw function
|
||||
if(obj.nodeType === 'Shape' && obj.shapeType === undefined) {
|
||||
node.drawFunc = drawFuncs[obj.attrs.drawFuncName];
|
||||
}
|
||||
|
||||
var children = obj._children;
|
||||
var children = obj.children;
|
||||
if(children !== undefined) {
|
||||
for(var n = 0; n < children.length; n++) {
|
||||
var child = children[n];
|
||||
var type;
|
||||
|
||||
// determine type
|
||||
if(child.nodeType === 'Shape') {
|
||||
if(child.attrs.nodeType === 'Shape') {
|
||||
// add custom shape
|
||||
if(child.shapeType === undefined) {
|
||||
if(child.attrs.shapeType === undefined) {
|
||||
type = 'Shape';
|
||||
}
|
||||
// add standard shape
|
||||
else {
|
||||
type = child.shapeType;
|
||||
type = child.attrs.shapeType;
|
||||
}
|
||||
}
|
||||
else {
|
||||
type = child.nodeType;
|
||||
type = child.attrs.nodeType;
|
||||
}
|
||||
|
||||
var no = new Kinetic[type]({});
|
||||
var no = new Kinetic[type](child.attrs);
|
||||
node.add(no);
|
||||
loadNode(no, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
loadNode(this, JSON.parse(json));
|
||||
var obj = JSON.parse(json);
|
||||
|
||||
// copy over stage properties
|
||||
for(var key in obj.attrs) {
|
||||
this[key] = obj.attrs[key];
|
||||
}
|
||||
|
||||
loadNode(this, obj);
|
||||
this.draw();
|
||||
},
|
||||
/**
|
||||
@@ -485,7 +494,7 @@ Kinetic.Stage.prototype = {
|
||||
// propapgate backwards through children
|
||||
for(var i = children.length - 1; i >= 0; i--) {
|
||||
var child = children[i];
|
||||
if(child.listening) {
|
||||
if(child._listening) {
|
||||
if(child.nodeType === 'Shape') {
|
||||
var exit = this._detectEvent(child, evt);
|
||||
if(exit) {
|
||||
@@ -526,7 +535,7 @@ Kinetic.Stage.prototype = {
|
||||
var shapeDetected = false;
|
||||
for(var n = this.children.length - 1; n >= 0; n--) {
|
||||
var layer = this.children[n];
|
||||
if(layer.visible && n >= 0 && layer.listening) {
|
||||
if(layer.visible && n >= 0 && layer._listening) {
|
||||
if(this._traverseChildren(layer, evt)) {
|
||||
n = -1;
|
||||
shapeDetected = true;
|
||||
|
||||
Reference in New Issue
Block a user