removed Global namespace. global stuff is now just global, inside the Kinetic namespace. There was no reason to have a secondary level for global vars

This commit is contained in:
Eric Rowell
2013-09-08 22:02:04 -07:00
parent 400fae734d
commit c40fc7cf38
11 changed files with 97 additions and 111 deletions

View File

@@ -58,8 +58,7 @@
* @param {Node} child * @param {Node} child
*/ */
add: function(child) { add: function(child) {
var go = Kinetic.Global, var children = this.children;
children = this.children;
this._validateAdd(child); this._validateAdd(child);
child.index = children.length; child.index = children.length;
@@ -137,8 +136,7 @@
return Kinetic.Collection.toCollection(retArr); return Kinetic.Collection.toCollection(retArr);
}, },
_getNodeById: function(key) { _getNodeById: function(key) {
var go = Kinetic.Global, var node = Kinetic.ids[key];
node = go.ids[key];
if(node !== undefined && this.isAncestorOf(node)) { if(node !== undefined && this.isAncestorOf(node)) {
return node; return node;
@@ -146,7 +144,7 @@
return null; return null;
}, },
_getNodesByName: function(key) { _getNodesByName: function(key) {
var go = Kinetic.Global, arr = go.names[key] || []; var arr = Kinetic.names[key] || [];
return this._getDescendants(arr); return this._getDescendants(arr);
}, },
_get: function(selector) { _get: function(selector) {

View File

@@ -28,7 +28,6 @@
}, },
_endDragBefore: function(evt) { _endDragBefore: function(evt) {
var dd = Kinetic.DD, var dd = Kinetic.DD,
go = Kinetic.Global,
node = dd.node, node = dd.node,
nodeType, layer; nodeType, layer;
@@ -41,7 +40,7 @@
// operation actually started. // operation actually started.
if(dd.isDragging) { if(dd.isDragging) {
dd.isDragging = false; dd.isDragging = false;
go.listenClickTap = false; Kinetic.listenClickTap = false;
if (evt) { if (evt) {
evt.dragEndNode = node; evt.dragEndNode = node;

View File

@@ -31,9 +31,23 @@
var Kinetic = {}; var Kinetic = {};
(function() { (function() {
Kinetic = { Kinetic = {
// public
version: '@@version', version: '@@version',
// private
stages: [],
idCounter: 0,
ids: {},
names: {},
shapes: {},
listenClickTap: false,
inDblClickWindow: false,
// configurations
enableTrace: false, enableTrace: false,
traceArrMax: 100, traceArrMax: 100,
dblClickWindow: 400,
/** /**
* @namespace Filters * @namespace Filters
* @memberof Kinetic * @memberof Kinetic
@@ -148,31 +162,12 @@ var Kinetic = {};
*/ */
Group: function(config) { Group: function(config) {
this.___init(config); this.___init(config);
} },
};
/**
* @namespace Global
* @memberof Kinetic
*/
Kinetic.Global = {
stages: [],
idCounter: 0,
ids: {},
names: {},
//shapes hash. rgb keys and shape values
shapes: {},
// event flags
listenClickTap: false,
inDblClickWindow: false,
dblClickWindow: 400,
/** /**
* returns whether or not drag and drop is currently active * returns whether or not drag and drop is currently active
* @method * @method
* @memberof Kinetic.Global * @memberof Kinetic
*/ */
isDragging: function() { isDragging: function() {
var dd = Kinetic.DD; var dd = Kinetic.DD;
@@ -191,7 +186,7 @@ var Kinetic = {};
* returns whether or not a drag and drop operation is ready, but may * returns whether or not a drag and drop operation is ready, but may
* not necessarily have started * not necessarily have started
* @method * @method
* @memberof Kinetic.Global * @memberof Kinetic
*/ */
isDragReady: function() { isDragReady: function() {
var dd = Kinetic.DD; var dd = Kinetic.DD;

View File

@@ -34,7 +34,7 @@
// this indicates that a hit pixel may have been found // this indicates that a hit pixel may have been found
if(p[3] === 255) { if(p[3] === 255) {
colorKey = Kinetic.Util._rgbToHex(p[0], p[1], p[2]); colorKey = Kinetic.Util._rgbToHex(p[0], p[1], p[2]);
shape = Kinetic.Global.shapes[HASH + colorKey]; shape = Kinetic.shapes[HASH + colorKey];
return { return {
shape: shape, shape: shape,
pixel: p pixel: p

View File

@@ -57,7 +57,7 @@
Kinetic.Util.addMethods(Kinetic.Node, { Kinetic.Util.addMethods(Kinetic.Node, {
_init: function(config) { _init: function(config) {
var that = this; var that = this;
this._id = Kinetic.Global.idCounter++; this._id = Kinetic.idCounter++;
this.eventListeners = {}; this.eventListeners = {};
this.attrs = {}; this.attrs = {};
this.cache = {}; this.cache = {};
@@ -256,11 +256,9 @@
* node.destroy(); * node.destroy();
*/ */
destroy: function() { destroy: function() {
var go = Kinetic.Global;
// remove from ids and names hashes // remove from ids and names hashes
go._removeId(this.getId()); Kinetic._removeId(this.getId());
go._removeName(this.getName(), this._id); Kinetic._removeName(this.getName(), this._id);
this.remove(); this.remove();
}, },
@@ -1150,11 +1148,10 @@
* @param {String} id * @param {String} id
*/ */
setId: function(id) { setId: function(id) {
var oldId = this.getId(), var oldId = this.getId();
go = Kinetic.Global;
go._removeId(oldId); Kinetic._removeId(oldId);
go._addId(this, id); Kinetic._addId(this, id);
this._setAttr(ID, id); this._setAttr(ID, id);
return this; return this;
}, },
@@ -1165,11 +1162,10 @@
* @param {String} name * @param {String} name
*/ */
setName: function(name) { setName: function(name) {
var oldName = this.getName(), var oldName = this.getName();
go = Kinetic.Global;
go._removeName(oldName, this._id); Kinetic._removeName(oldName, this._id);
go._addName(this, name); Kinetic._addName(this, name);
this._setAttr(NAME, name); this._setAttr(NAME, name);
return this; return this;
}, },
@@ -1233,7 +1229,7 @@
return this; return this;
}, },
shouldDrawHit: function() { shouldDrawHit: function() {
return this.isListening() && this.isVisible() && !Kinetic.Global.isDragging(); return this.isListening() && this.isVisible() && !Kinetic.isDragging();
}, },
isDraggable: function() { isDraggable: function() {
return false; return false;

View File

@@ -27,7 +27,7 @@
this._strokeFuncHit = _strokeFuncHit; this._strokeFuncHit = _strokeFuncHit;
// set colorKey // set colorKey
var shapes = Kinetic.Global.shapes; var shapes = Kinetic.shapes;
var key; var key;
while(true) { while(true) {
@@ -205,7 +205,7 @@
// extends Node.prototype.destroy // extends Node.prototype.destroy
destroy: function() { destroy: function() {
Kinetic.Node.prototype.destroy.call(this); Kinetic.Node.prototype.destroy.call(this);
delete Kinetic.Global.shapes[this.colorKey]; delete Kinetic.shapes[this.colorKey];
return this; return this;
}, },
drawScene: function(canvas) { drawScene: function(canvas) {

View File

@@ -41,10 +41,10 @@
// call super constructor // call super constructor
Kinetic.Container.call(this, config); Kinetic.Container.call(this, config);
this.nodeType = STAGE; this.nodeType = STAGE;
this._id = Kinetic.Global.idCounter++; this._id = Kinetic.idCounter++;
this._buildDOM(); this._buildDOM();
this._bindContentEvents(); this._bindContentEvents();
Kinetic.Global.stages.push(this); Kinetic.stages.push(this);
}, },
_validateAdd: function(child) { _validateAdd: function(child) {
if (child.getType() !== 'Layer') { if (child.getType() !== 'Layer') {
@@ -347,10 +347,9 @@
}, },
_mouseout: function(evt) { _mouseout: function(evt) {
this._setPointerPosition(evt); this._setPointerPosition(evt);
var go = Kinetic.Global, var targetShape = this.targetShape;
targetShape = this.targetShape;
if(targetShape && !go.isDragging()) { if(targetShape && !Kinetic.isDragging()) {
targetShape._fireAndBubble(MOUSEOUT, evt); targetShape._fireAndBubble(MOUSEOUT, evt);
targetShape._fireAndBubble(MOUSELEAVE, evt); targetShape._fireAndBubble(MOUSELEAVE, evt);
this.targetShape = null; this.targetShape = null;
@@ -361,15 +360,14 @@
}, },
_mousemove: function(evt) { _mousemove: function(evt) {
this._setPointerPosition(evt); this._setPointerPosition(evt);
var go = Kinetic.Global, var dd = Kinetic.DD,
dd = Kinetic.DD,
obj = this.getIntersection(this.getPointerPosition()), obj = this.getIntersection(this.getPointerPosition()),
shape; shape;
if(obj) { if(obj) {
shape = obj.shape; shape = obj.shape;
if(shape) { if(shape) {
if(!go.isDragging() && obj.pixel[3] === 255 && (!this.targetShape || this.targetShape._id !== shape._id)) { if(!Kinetic.isDragging() && obj.pixel[3] === 255 && (!this.targetShape || this.targetShape._id !== shape._id)) {
if(this.targetShape) { if(this.targetShape) {
this.targetShape._fireAndBubble(MOUSEOUT, evt, shape); this.targetShape._fireAndBubble(MOUSEOUT, evt, shape);
this.targetShape._fireAndBubble(MOUSELEAVE, evt, shape); this.targetShape._fireAndBubble(MOUSELEAVE, evt, shape);
@@ -389,7 +387,7 @@
*/ */
else { else {
this._fire(MOUSEMOVE, evt); this._fire(MOUSEMOVE, evt);
if(this.targetShape && !go.isDragging()) { if(this.targetShape && !Kinetic.isDragging()) {
this.targetShape._fireAndBubble(MOUSEOUT, evt); this.targetShape._fireAndBubble(MOUSEOUT, evt);
this.targetShape._fireAndBubble(MOUSELEAVE, evt); this.targetShape._fireAndBubble(MOUSELEAVE, evt);
this.targetShape = null; this.targetShape = null;
@@ -408,11 +406,10 @@
}, },
_mousedown: function(evt) { _mousedown: function(evt) {
this._setPointerPosition(evt); this._setPointerPosition(evt);
var go = Kinetic.Global, var obj = this.getIntersection(this.getPointerPosition()),
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this; shape = obj && obj.shape ? obj.shape : this;
go.listenClickTap = true; Kinetic.listenClickTap = true;
this.clickStartShape = shape; this.clickStartShape = shape;
shape._fireAndBubble(MOUSEDOWN, evt); shape._fireAndBubble(MOUSEDOWN, evt);
@@ -425,30 +422,29 @@
_mouseup: function(evt) { _mouseup: function(evt) {
this._setPointerPosition(evt); this._setPointerPosition(evt);
var that = this, var that = this,
go = Kinetic.Global,
obj = this.getIntersection(this.getPointerPosition()), obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this; shape = obj && obj.shape ? obj.shape : this;
shape._fireAndBubble(MOUSEUP, evt); shape._fireAndBubble(MOUSEUP, evt);
// detect if click or double click occurred // detect if click or double click occurred
if(go.listenClickTap && shape._id === this.clickStartShape._id) { if(Kinetic.listenClickTap && shape._id === this.clickStartShape._id) {
shape._fireAndBubble(CLICK, evt); shape._fireAndBubble(CLICK, evt);
if(go.inDblClickWindow) { if(Kinetic.inDblClickWindow) {
shape._fireAndBubble(DBL_CLICK, evt); shape._fireAndBubble(DBL_CLICK, evt);
go.inDblClickWindow = false; Kinetic.inDblClickWindow = false;
} }
else { else {
go.inDblClickWindow = true; Kinetic.inDblClickWindow = true;
} }
setTimeout(function() { setTimeout(function() {
go.inDblClickWindow = false; Kinetic.inDblClickWindow = false;
}, go.dblClickWindow); }, Kinetic.dblClickWindow);
} }
go.listenClickTap = false; Kinetic.listenClickTap = false;
// always call preventDefault for desktop events because some browsers // always call preventDefault for desktop events because some browsers
// try to drag and drop the canvas element // try to drag and drop the canvas element
@@ -458,11 +454,10 @@
}, },
_touchstart: function(evt) { _touchstart: function(evt) {
this._setPointerPosition(evt); this._setPointerPosition(evt);
var go = Kinetic.Global, var obj = this.getIntersection(this.getPointerPosition()),
obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this; shape = obj && obj.shape ? obj.shape : this;
go.listenClickTap = true; Kinetic.listenClickTap = true;
this.tapStartShape = shape; this.tapStartShape = shape;
shape._fireAndBubble(TOUCHSTART, evt); shape._fireAndBubble(TOUCHSTART, evt);
@@ -474,30 +469,29 @@
_touchend: function(evt) { _touchend: function(evt) {
this._setPointerPosition(evt); this._setPointerPosition(evt);
var that = this, var that = this,
go = Kinetic.Global,
obj = this.getIntersection(this.getPointerPosition()), obj = this.getIntersection(this.getPointerPosition()),
shape = obj && obj.shape ? obj.shape : this; shape = obj && obj.shape ? obj.shape : this;
shape._fireAndBubble(TOUCHEND, evt); shape._fireAndBubble(TOUCHEND, evt);
// detect if tap or double tap occurred // detect if tap or double tap occurred
if(go.listenClickTap && shape._id === this.tapStartShape._id) { if(Kinetic.listenClickTap && shape._id === this.tapStartShape._id) {
shape._fireAndBubble(TAP, evt); shape._fireAndBubble(TAP, evt);
if(go.inDblClickWindow) { if(Kinetic.inDblClickWindow) {
shape._fireAndBubble(DBL_TAP, evt); shape._fireAndBubble(DBL_TAP, evt);
go.inDblClickWindow = false; Kinetic.inDblClickWindow = false;
} }
else { else {
go.inDblClickWindow = true; Kinetic.inDblClickWindow = true;
} }
setTimeout(function() { setTimeout(function() {
go.inDblClickWindow = false; Kinetic.inDblClickWindow = false;
}, go.dblClickWindow); }, Kinetic.dblClickWindow);
} }
go.listenClickTap = false; Kinetic.listenClickTap = false;
// only call preventDefault if the shape is listening for events // only call preventDefault if the shape is listening for events
if (shape.isListening() && evt.preventDefault) { if (shape.isListening() && evt.preventDefault) {

View File

@@ -4,9 +4,16 @@
<title>KineticJS Mocha Tests</title> <title>KineticJS Mocha Tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" /> <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
<style> <style>
#mocha {
margin-top: 20px;
}
#mocha .test { #mocha .test {
overflow: auto; overflow: auto;
} }
h1 {
font-family: Calibri;
margin-left: 10px;
}
h2.kinetic-title { h2.kinetic-title {
font-family: Calibri; font-family: Calibri;
font-size: 16px; font-size: 16px;
@@ -17,6 +24,7 @@
</style> </style>
</head> </head>
<body> <body>
<h1>KineticJS Test</h1>
<div id="mocha"></div> <div id="mocha"></div>
<div id="kinetic-container"></div> <div id="kinetic-container"></div>

View File

@@ -2293,8 +2293,6 @@ suite('Node', function() {
id: 'myCircle' id: 'myCircle'
}); });
var go = Kinetic.Global;
layer.add(circle); layer.add(circle);
var node = stage.get('#myCircle')[0]; var node = stage.get('#myCircle')[0];
@@ -2511,30 +2509,29 @@ suite('Node', function() {
var circleColorKey = circle.colorKey; var circleColorKey = circle.colorKey;
var rectColorKey = rect.colorKey; var rectColorKey = rect.colorKey;
var go = Kinetic.Global;
layer.add(circle); layer.add(circle);
layer.add(rect); layer.add(rect);
stage.add(layer); stage.add(layer);
assert.equal(go.ids.myCircle2._id, circle._id); assert.equal(Kinetic.ids.myCircle2._id, circle._id);
assert.equal(go.names.myRect2[0]._id, rect._id); assert.equal(Kinetic.names.myRect2[0]._id, rect._id);
assert.equal(Kinetic.Global.shapes[circleColorKey]._id, circle._id); assert.equal(Kinetic.shapes[circleColorKey]._id, circle._id);
assert.equal(Kinetic.Global.shapes[rectColorKey]._id, rect._id); assert.equal(Kinetic.shapes[rectColorKey]._id, rect._id);
circle.destroy(); circle.destroy();
assert.equal(go.ids.myCircle2, undefined); assert.equal(Kinetic.ids.myCircle2, undefined);
assert.equal(go.names.myRect2[0]._id, rect._id); assert.equal(Kinetic.names.myRect2[0]._id, rect._id);
assert.equal(Kinetic.Global.shapes[circleColorKey], undefined); assert.equal(Kinetic.shapes[circleColorKey], undefined);
assert.equal(Kinetic.Global.shapes[rectColorKey]._id, rect._id); assert.equal(Kinetic.shapes[rectColorKey]._id, rect._id);
rect.destroy(); rect.destroy();
assert.equal(go.ids.myCircle2, undefined); assert.equal(Kinetic.ids.myCircle2, undefined);
assert.equal(go.names.myRect2, undefined); assert.equal(Kinetic.names.myRect2, undefined);
assert.equal(Kinetic.Global.shapes[circleColorKey], undefined); assert.equal(Kinetic.shapes[circleColorKey], undefined);
assert.equal(Kinetic.Global.shapes[rectColorKey], undefined); assert.equal(Kinetic.shapes[rectColorKey], undefined);
}); });
// ====================================================== // ======================================================

View File

@@ -430,18 +430,17 @@ Test.Modules.CONTAINER = {
layer.add(rect); layer.add(rect);
stage.add(layer); stage.add(layer);
var go = Kinetic.Global;
test(go.ids['myCircle3'].getId() === 'myCircle3', 'circle id not in ids hash'); test(Kinetic.ids['myCircle3'].getId() === 'myCircle3', 'circle id not in ids hash');
test(go.names['myRect3'][0].getName() === 'myRect3', 'rect name not in names hash'); test(Kinetic.names['myRect3'][0].getName() === 'myRect3', 'rect name not in names hash');
circle.setId('newCircleId'); circle.setId('newCircleId');
test(go.ids['newCircleId'] !== undefined, 'circle not in ids hash'); test(Kinetic.ids['newCircleId'] !== undefined, 'circle not in ids hash');
test(go.ids['myCircle3'] === undefined, 'old circle id key is still in ids hash'); test(Kinetic.ids['myCircle3'] === undefined, 'old circle id key is still in ids hash');
rect.setName('newRectName'); rect.setName('newRectName');
test(go.names['newRectName'][0] !== undefined, 'new rect name not in names hash'); test(Kinetic.names['newRectName'][0] !== undefined, 'new rect name not in names hash');
test(go.names['myRect3'] === undefined, 'old rect name is still in names hash'); test(Kinetic.names['myRect3'] === undefined, 'old rect name is still in names hash');
}, },
'add layer': function(containerId) { 'add layer': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({
@@ -530,16 +529,16 @@ Test.Modules.CONTAINER = {
test(layer.children.length === 1, 'layer should have 1 children'); test(layer.children.length === 1, 'layer should have 1 children');
test(group.children.length === 2, 'group should have 2 children'); test(group.children.length === 2, 'group should have 2 children');
test(Kinetic.Global.names.circleName.length > 0, 'circleName should be in names hash'); test(Kinetic.names.circleName.length > 0, 'circleName should be in names hash');
test(Kinetic.Global.ids.circleId.getId() === 'circleId', 'layerId should be in ids hash'); test(Kinetic.ids.circleId.getId() === 'circleId', 'layerId should be in ids hash');
layer.destroyChildren(); layer.destroyChildren();
layer.draw(); layer.draw();
test(layer.children.length === 0, 'layer should have 0 children'); test(layer.children.length === 0, 'layer should have 0 children');
test(group.children.length === 0, 'group should have 0 children'); test(group.children.length === 0, 'group should have 0 children');
test(Kinetic.Global.names.circleName === undefined, 'circleName should not be in names hash'); test(Kinetic.names.circleName === undefined, 'circleName should not be in names hash');
test(Kinetic.Global.ids.circleId === undefined, 'layerId should not be in ids hash'); test(Kinetic.ids.circleId === undefined, 'layerId should not be in ids hash');
}, },
'add group': function(containerId) { 'add group': function(containerId) {
var stage = new Kinetic.Stage({ var stage = new Kinetic.Stage({

View File

@@ -304,17 +304,17 @@ Test.Modules.STAGE = {
layer.add(circle); layer.add(circle);
stage.add(layer); stage.add(layer);
test(Kinetic.Global.ids.stageFalconId._id === stage._id, 'stage id should be in global ids map'); test(Kinetic.ids.stageFalconId._id === stage._id, 'stage id should be in global ids map');
test(Kinetic.Global.names.stageFalconName[0]._id === stage._id, 'stage name should be in global names map'); test(Kinetic.names.stageFalconName[0]._id === stage._id, 'stage name should be in global names map');
test(Kinetic.Global.ids.circleFalconId._id === circle._id, 'circle id should be in global ids map'); test(Kinetic.ids.circleFalconId._id === circle._id, 'circle id should be in global ids map');
test(Kinetic.Global.names.circleFalconName[0]._id === circle._id, 'circle name should be in global names map'); test(Kinetic.names.circleFalconName[0]._id === circle._id, 'circle name should be in global names map');
stage.destroy(); stage.destroy();
test(Kinetic.Global.ids.stageFalconId === undefined, 'stage should no longer be in ids map'); test(Kinetic.ids.stageFalconId === undefined, 'stage should no longer be in ids map');
test(Kinetic.Global.names.stageFalconName === undefined, 'stage should no longer be in names map'); test(Kinetic.names.stageFalconName === undefined, 'stage should no longer be in names map');
test(Kinetic.Global.ids.circleFalconId === undefined, 'circle should no longer be in ids map'); test(Kinetic.ids.circleFalconId === undefined, 'circle should no longer be in ids map');
test(Kinetic.Global.names.circleFalconName === undefined, 'circle should no longer be in names map'); test(Kinetic.names.circleFalconName === undefined, 'circle should no longer be in names map');
}, },