added validation for add(). An error is thrown if an invalid node is added

This commit is contained in:
Eric Rowell
2013-07-22 22:47:13 -07:00
parent 1fc57bed4c
commit 3eea92d72e
6 changed files with 126 additions and 2 deletions

View File

@@ -58,7 +58,10 @@
* @param {Node} child
*/
add: function(child) {
var go = Kinetic.Global, children = this.children;
var go = Kinetic.Global,
children = this.children;
this._validateAdd(child);
child.index = children.length;
child.parent = this;
children.push(child);

View File

@@ -4,7 +4,13 @@
this.nodeType = 'Group';
// call super constructor
Kinetic.Container.call(this, config);
},
_validateAdd: function(child) {
var type = child.getType();
if (type !== 'Group' && type !== 'Shape') {
Kinetic.Util.error('You may only add groups and shapes to groups.');
}
},
});
Kinetic.Util.extend(Kinetic.Group, Kinetic.Container);
})();

View File

@@ -10,6 +10,12 @@
// call super constructor
Kinetic.Container.call(this, config);
},
_validateAdd: function(child) {
var type = child.getType();
if (type !== 'Group' && type !== 'Shape') {
Kinetic.Util.error('You may only add groups and shapes to a layer.');
}
},
/**
* get visible intersection object that contains shape and pixel data. This is the preferred
* method for determining if a point intersects a shape or not

View File

@@ -47,6 +47,11 @@
this._bindContentEvents();
Kinetic.Global.stages.push(this);
},
_validateAdd: function(child) {
if (child.getType() !== 'Layer') {
Kinetic.Util.error('You may only add layers to the stage.');
}
},
/**
* set container dom element which contains the stage wrapper div element
* @method

View File

@@ -253,6 +253,7 @@
EMPTY_STRING = '',
ZERO = '0',
KINETIC_WARNING = 'Kinetic warning: ',
KINETIC_ERROR = 'Kinetic error: ',
RGB_PAREN = 'rgb(',
COLORS = {
aqua: [0,255,255],
@@ -658,6 +659,9 @@
_capitalize: function(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
error: function(str) {
throw new Error(KINETIC_ERROR + str);
},
warn: function(str) {
/*
* IE9 on Windows7 64bit will throw a JS error

View File

@@ -29,6 +29,105 @@ Test.Modules.CONTAINER = {
group.add(circle);
layer.draw();
},
'adder validation': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var group = new Kinetic.Group();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'myCircle'
});
stage.add(layer);
layer.add(group);
group.add(circle);
layer.draw();
// disassemble the tree
circle.remove();
group.remove();
layer.remove();
// ===================================
var errorThrown = false;
try {
stage.add(stage);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding stage to stage');
stage.remove();
// ===================================
var errorThrown = false;
try {
stage.add(group);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding group to stage');
group.remove();
// ===================================
var errorThrown = false;
try {
stage.add(circle);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding shape to stage');
circle.remove();
// ===================================
var errorThrown = false;
try {
layer.add(stage);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding stage to layer');
stage.remove();
// ===================================
var errorThrown = false;
try {
layer.add(layer);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding layer to layer');
layer.remove();
// ===================================
var errorThrown = false;
try {
group.add(stage);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding stage to group');
stage.remove();
// ===================================
var errorThrown = false;
try {
group.add(layer);
} catch(err) {
errorThrown = true;
}
test(errorThrown, 'error should have been thrown when adding layer to group');
layer.remove();
},
'add layer then group then shape': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
@@ -47,6 +146,7 @@ Test.Modules.CONTAINER = {
name: 'myCircle'
});
// these should all pass because they are valid
stage.add(layer);
layer.add(group);
group.add(circle);