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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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