moved ids and names hashes to the stage level

This commit is contained in:
Eric Rowell 2012-04-08 11:01:31 -07:00
parent 8c70333472
commit 3c17e59eb0
7 changed files with 127 additions and 75 deletions

98
dist/kinetic-core.js vendored
View File

@ -3,7 +3,7 @@
* http://www.kineticjs.com/ * http://www.kineticjs.com/
* Copyright 2012, Eric Rowell * Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses. * Licensed under the MIT or GPL Version 2 licenses.
* Date: Apr 07 2012 * Date: Apr 08 2012
* *
* Copyright (C) 2011 - 2012 by Eric Rowell * Copyright (C) 2011 - 2012 by Eric Rowell
* *
@ -41,8 +41,7 @@ var Kinetic = {};
Kinetic.GlobalObject = { Kinetic.GlobalObject = {
stages: [], stages: [],
idCounter: 0, idCounter: 0,
ids: {}, tempNodes: [],
names: {},
animations: [], animations: [],
animIdCounter: 0, animIdCounter: 0,
frame: { frame: {
@ -78,12 +77,25 @@ Kinetic.GlobalObject = {
} }
} }
}, },
_pullNodes: function(stage) {
var go = Kinetic.GlobalObject;
var tempNodes = go.tempNodes;
for(var n = 0; n < tempNodes.length; n++) {
var node = tempNodes[n];
if(node.getStage() !== undefined && node.getStage()._id === stage._id) {
stage._addId(node);
stage._addName(node);
go.tempNodes.splice(n, 1);
n -= 1;
}
}
},
_runFrames: function() { _runFrames: function() {
var nodes = {}; var nodes = {};
for(var n = 0; n < this.animations.length; n++) { for(var n = 0; n < this.animations.length; n++) {
var anim = this.animations[n]; var anim = this.animations[n];
if(anim.node && anim.node.id !== undefined) { if(anim.node && anim.node._id !== undefined) {
nodes[anim.node.id] = anim.node; nodes[anim.node._id] = anim.node;
} }
anim.func(this.frame); anim.func(this.frame);
} }
@ -125,28 +137,6 @@ Kinetic.GlobalObject = {
else { else {
this.frame.lastTime = 0; this.frame.lastTime = 0;
} }
},
addId: function(node) {
var go = Kinetic.GlobalObject;
if(node.attrs.id !== undefined) {
go.ids[node.attrs.id] = node;
}
},
removeId: function(node) {
},
addName: function(node) {
var go = Kinetic.GlobalObject;
var name = node.attrs.name;
if(name !== undefined) {
if(go.names[name] === undefined) {
go.names[name] = [];
}
go.names[name].push(node);
}
},
removeName: function(node) {
} }
}; };
@ -621,7 +611,12 @@ Kinetic.Node.prototype = {
return this; return this;
} }
else { else {
return this.getParent().getStage(); if(this.getParent() === undefined) {
return undefined;
}
else {
return this.getParent().getStage();
}
} }
}, },
/** /**
@ -909,9 +904,15 @@ Kinetic.Container.prototype = {
this.children.push(child); this.children.push(child);
var go = Kinetic.GlobalObject; var stage = child.getStage();
go.addId(child); if (stage === undefined) {
go.addName(child); var go = Kinetic.GlobalObject;
go.tempNodes.push(child);
}
else {
stage._addId(child);
stage._addName(child);
}
}, },
/** /**
* set children indices * set children indices
@ -963,6 +964,8 @@ Kinetic.Stage = function(config) {
this.attrs.width = 400; this.attrs.width = 400;
this.attrs.height = 200; this.attrs.height = 200;
this.nodeType = 'Stage'; this.nodeType = 'Stage';
this.ids = {};
this.names = {};
/* /*
* if container is a string, assume it's an id for * if container is a string, assume it's an id for
@ -1006,8 +1009,8 @@ Kinetic.Stage = function(config) {
var go = Kinetic.GlobalObject; var go = Kinetic.GlobalObject;
go.stages.push(this); go.stages.push(this);
go.addId(this); this._addId(this);
go.addName(this); this._addName(this);
}; };
/* /*
* Stage methods * Stage methods
@ -1054,13 +1057,12 @@ Kinetic.Stage.prototype = {
* @param {String} selector * @param {String} selector
*/ */
get: function(selector) { get: function(selector) {
var go = Kinetic.GlobalObject;
var hash; var hash;
if(selector.charAt(0) === '#') { if(selector.charAt(0) === '#') {
hash = go.ids; hash = this.ids;
} }
else if(selector.charAt(0) === '.') { else if(selector.charAt(0) === '.') {
hash = go.names; hash = this.names;
} }
else { else {
return false; return false;
@ -1262,6 +1264,10 @@ Kinetic.Stage.prototype = {
layer.canvas.height = this.attrs.height; layer.canvas.height = this.attrs.height;
this._add(layer); this._add(layer);
// populate stage node ids and names
var go = Kinetic.GlobalObject;
go._pullNodes(this);
// draw layer and append canvas to container // draw layer and append canvas to container
layer.draw(); layer.draw();
this.content.appendChild(layer.canvas); this.content.appendChild(layer.canvas);
@ -1799,6 +1805,26 @@ Kinetic.Stage.prototype = {
this.pathLayer.canvas.height = this.attrs.height; this.pathLayer.canvas.height = this.attrs.height;
this.pathLayer.canvas.className = 'kineticjs-path-layer'; this.pathLayer.canvas.className = 'kineticjs-path-layer';
this.content.appendChild(this.pathLayer.canvas); this.content.appendChild(this.pathLayer.canvas);
},
_addId: function(node) {
if(node.attrs.id !== undefined) {
this.ids[node.attrs.id] = node;
}
},
_removeId: function(node) {
},
_addName: function(node) {
var name = node.attrs.name;
if(name !== undefined) {
if(this.names[name] === undefined) {
this.names[name] = [];
}
this.names[name].push(node);
}
},
_removeName: function(node) {
} }
}; };
// Extend Container and Node // Extend Container and Node

File diff suppressed because one or more lines are too long

View File

@ -65,9 +65,15 @@ Kinetic.Container.prototype = {
this.children.push(child); this.children.push(child);
var go = Kinetic.GlobalObject; var stage = child.getStage();
go.addId(child); if (stage === undefined) {
go.addName(child); var go = Kinetic.GlobalObject;
go.tempNodes.push(child);
}
else {
stage._addId(child);
stage._addName(child);
}
}, },
/** /**
* set children indices * set children indices

View File

@ -13,8 +13,7 @@ var Kinetic = {};
Kinetic.GlobalObject = { Kinetic.GlobalObject = {
stages: [], stages: [],
idCounter: 0, idCounter: 0,
ids: {}, tempNodes: [],
names: {},
animations: [], animations: [],
animIdCounter: 0, animIdCounter: 0,
frame: { frame: {
@ -50,12 +49,25 @@ Kinetic.GlobalObject = {
} }
} }
}, },
_pullNodes: function(stage) {
var go = Kinetic.GlobalObject;
var tempNodes = go.tempNodes;
for(var n = 0; n < tempNodes.length; n++) {
var node = tempNodes[n];
if(node.getStage() !== undefined && node.getStage()._id === stage._id) {
stage._addId(node);
stage._addName(node);
go.tempNodes.splice(n, 1);
n -= 1;
}
}
},
_runFrames: function() { _runFrames: function() {
var nodes = {}; var nodes = {};
for(var n = 0; n < this.animations.length; n++) { for(var n = 0; n < this.animations.length; n++) {
var anim = this.animations[n]; var anim = this.animations[n];
if(anim.node && anim.node.id !== undefined) { if(anim.node && anim.node._id !== undefined) {
nodes[anim.node.id] = anim.node; nodes[anim.node._id] = anim.node;
} }
anim.func(this.frame); anim.func(this.frame);
} }
@ -97,28 +109,6 @@ Kinetic.GlobalObject = {
else { else {
this.frame.lastTime = 0; this.frame.lastTime = 0;
} }
},
addId: function(node) {
var go = Kinetic.GlobalObject;
if(node.attrs.id !== undefined) {
go.ids[node.attrs.id] = node;
}
},
removeId: function(node) {
},
addName: function(node) {
var go = Kinetic.GlobalObject;
var name = node.attrs.name;
if(name !== undefined) {
if(go.names[name] === undefined) {
go.names[name] = [];
}
go.names[name].push(node);
}
},
removeName: function(node) {
} }
}; };

View File

@ -462,7 +462,12 @@ Kinetic.Node.prototype = {
return this; return this;
} }
else { else {
return this.getParent().getStage(); if(this.getParent() === undefined) {
return undefined;
}
else {
return this.getParent().getStage();
}
} }
}, },
/** /**

View File

@ -19,6 +19,8 @@ Kinetic.Stage = function(config) {
this.attrs.width = 400; this.attrs.width = 400;
this.attrs.height = 200; this.attrs.height = 200;
this.nodeType = 'Stage'; this.nodeType = 'Stage';
this.ids = {};
this.names = {};
/* /*
* if container is a string, assume it's an id for * if container is a string, assume it's an id for
@ -62,8 +64,8 @@ Kinetic.Stage = function(config) {
var go = Kinetic.GlobalObject; var go = Kinetic.GlobalObject;
go.stages.push(this); go.stages.push(this);
go.addId(this); this._addId(this);
go.addName(this); this._addName(this);
}; };
/* /*
* Stage methods * Stage methods
@ -110,13 +112,12 @@ Kinetic.Stage.prototype = {
* @param {String} selector * @param {String} selector
*/ */
get: function(selector) { get: function(selector) {
var go = Kinetic.GlobalObject;
var hash; var hash;
if(selector.charAt(0) === '#') { if(selector.charAt(0) === '#') {
hash = go.ids; hash = this.ids;
} }
else if(selector.charAt(0) === '.') { else if(selector.charAt(0) === '.') {
hash = go.names; hash = this.names;
} }
else { else {
return false; return false;
@ -318,6 +319,10 @@ Kinetic.Stage.prototype = {
layer.canvas.height = this.attrs.height; layer.canvas.height = this.attrs.height;
this._add(layer); this._add(layer);
// populate stage node ids and names
var go = Kinetic.GlobalObject;
go._pullNodes(this);
// draw layer and append canvas to container // draw layer and append canvas to container
layer.draw(); layer.draw();
this.content.appendChild(layer.canvas); this.content.appendChild(layer.canvas);
@ -855,6 +860,26 @@ Kinetic.Stage.prototype = {
this.pathLayer.canvas.height = this.attrs.height; this.pathLayer.canvas.height = this.attrs.height;
this.pathLayer.canvas.className = 'kineticjs-path-layer'; this.pathLayer.canvas.className = 'kineticjs-path-layer';
this.content.appendChild(this.pathLayer.canvas); this.content.appendChild(this.pathLayer.canvas);
},
_addId: function(node) {
if(node.attrs.id !== undefined) {
this.ids[node.attrs.id] = node;
}
},
_removeId: function(node) {
},
_addName: function(node) {
var name = node.attrs.name;
if(name !== undefined) {
if(this.names[name] === undefined) {
this.names[name] = [];
}
this.names[name].push(node);
}
},
_removeName: function(node) {
} }
}; };
// Extend Container and Node // Extend Container and Node

View File

@ -321,7 +321,7 @@ Test.prototype.tests = {
layer.add(circle); layer.add(circle);
layer.add(rect); layer.add(rect);
stage.add(layer); stage.add(layer);
var node = stage.get('#myCircle'); var node = stage.get('#myCircle');
var nodes = stage.get('.myRect'); var nodes = stage.get('.myRect');