2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Container
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container constructor. Containers are used to contain nodes or other containers
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
Kinetic.Container = function() {
|
|
|
|
this.children = [];
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Container methods
|
|
|
|
*/
|
|
|
|
Kinetic.Container.prototype = {
|
|
|
|
/**
|
|
|
|
* get children
|
|
|
|
*/
|
|
|
|
getChildren: function() {
|
|
|
|
return this.children;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* remove all children
|
|
|
|
*/
|
|
|
|
removeChildren: function() {
|
|
|
|
while(this.children.length > 0) {
|
|
|
|
this.remove(this.children[0]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* remove child from container
|
|
|
|
* @param {Node} child
|
|
|
|
*/
|
|
|
|
_remove: function(child) {
|
2012-04-08 11:32:24 +08:00
|
|
|
if(this.children[child.index]._id == child._id) {
|
2012-04-09 08:37:49 +08:00
|
|
|
var stage = this.getStage();
|
|
|
|
stage._removeId(child);
|
|
|
|
stage._removeName(child);
|
|
|
|
|
|
|
|
var go = Kinetic.GlobalObject;
|
|
|
|
for(var n = 0; n < go.tempNodes.length; n++) {
|
|
|
|
var node = go.tempNodes[n];
|
|
|
|
if(node._id === child._id) {
|
|
|
|
go.tempNodes.splice(n, 1);
|
|
|
|
n = go.tempNodes.length;
|
|
|
|
}
|
|
|
|
}
|
2012-04-02 01:29:16 +08:00
|
|
|
|
|
|
|
this.children.splice(child.index, 1);
|
|
|
|
this._setChildrenIndices();
|
|
|
|
child = undefined;
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* draw children
|
|
|
|
*/
|
|
|
|
_drawChildren: function() {
|
|
|
|
var children = this.children;
|
|
|
|
for(var n = 0; n < children.length; n++) {
|
|
|
|
var child = children[n];
|
2012-04-05 13:57:36 +08:00
|
|
|
if(child.nodeType === 'Shape') {
|
2012-03-07 13:45:48 +08:00
|
|
|
child._draw(child.getLayer());
|
2012-03-18 01:28:25 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-03-07 13:45:48 +08:00
|
|
|
child._draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* add node to container
|
|
|
|
* @param {Node} child
|
|
|
|
*/
|
|
|
|
_add: function(child) {
|
2012-04-08 11:32:24 +08:00
|
|
|
child._id = Kinetic.GlobalObject.idCounter++;
|
2012-03-07 13:45:48 +08:00
|
|
|
child.index = this.children.length;
|
|
|
|
child.parent = this;
|
|
|
|
|
|
|
|
this.children.push(child);
|
2012-04-08 11:32:24 +08:00
|
|
|
|
2012-04-09 02:01:31 +08:00
|
|
|
var stage = child.getStage();
|
2012-04-09 08:37:49 +08:00
|
|
|
if(stage === undefined) {
|
|
|
|
var go = Kinetic.GlobalObject;
|
|
|
|
go.tempNodes.push(child);
|
2012-04-09 02:01:31 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
stage._addId(child);
|
|
|
|
stage._addName(child);
|
2012-04-09 08:37:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pull in other nodes that are now linked
|
|
|
|
* to a stage
|
|
|
|
*/
|
|
|
|
var go = Kinetic.GlobalObject;
|
|
|
|
go._pullNodes(stage);
|
2012-04-09 02:01:31 +08:00
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set children indices
|
|
|
|
*/
|
|
|
|
_setChildrenIndices: function() {
|
|
|
|
/*
|
|
|
|
* if reordering Layers, remove all canvas elements
|
|
|
|
* from the container except the buffer and backstage canvases
|
|
|
|
* and then readd all the layers
|
|
|
|
*/
|
2012-04-05 13:57:36 +08:00
|
|
|
if(this.nodeType === 'Stage') {
|
2012-03-18 01:28:25 +08:00
|
|
|
var canvases = this.content.children;
|
2012-03-07 13:45:48 +08:00
|
|
|
var bufferCanvas = canvases[0];
|
|
|
|
var backstageCanvas = canvases[1];
|
|
|
|
|
2012-03-18 01:28:25 +08:00
|
|
|
this.content.innerHTML = '';
|
|
|
|
this.content.appendChild(bufferCanvas);
|
|
|
|
this.content.appendChild(backstageCanvas);
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for(var n = 0; n < this.children.length; n++) {
|
|
|
|
this.children[n].index = n;
|
|
|
|
|
2012-04-05 13:57:36 +08:00
|
|
|
if(this.nodeType === 'Stage') {
|
2012-03-18 01:28:25 +08:00
|
|
|
this.content.appendChild(this.children[n].canvas);
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-18 01:28:25 +08:00
|
|
|
};
|