implemented clone method (inspired by matteo78) and added thorough unit tests

This commit is contained in:
Eric Rowell
2012-07-07 14:43:12 -07:00
parent 7f7cd24838
commit a8ab9a2533
7 changed files with 150 additions and 18 deletions

50
dist/kinetic-core.js vendored
View File

@@ -454,7 +454,6 @@ Kinetic.Node = Kinetic.Class.extend({
this.setDefaultAttrs(this.defaultNodeAttrs);
this.eventListeners = {};
this.setAttrs(config);
// bind events
@@ -1105,6 +1104,41 @@ Kinetic.Node = Kinetic.Class.extend({
return m;
},
/**
* clone node
* @param {Object} config used to override cloned
* attrs
*/
clone: function(obj) {
// instantiate new node
var classType = this.shapeType || this.nodeType;
var node = new Kinetic[classType](this.attrs);
/*
* copy over user listeners
*/
for(var key in this.eventListeners) {
var allListeners = this.eventListeners[key];
for(var n = 0; n < allListeners.length; n++) {
var listener = allListeners[n];
/*
* don't include kinetic namespaced listeners because
* these are generated by the constructors
*/
if(listener.name.indexOf('kinetic') < 0) {
// if listeners array doesn't exist, then create it
if(!node.eventListeners[key]) {
node.eventListeners[key] = [];
}
node.eventListeners[key].push(listener);
}
}
}
// apply attr overrides
node.setAttrs(obj);
return node;
},
_fireChangeEvent: function(attr) {
this._handleEvent(attr + 'Change', {});
},
@@ -1120,7 +1154,7 @@ Kinetic.Node = Kinetic.Class.extend({
this._dragCleanup();
var go = Kinetic.Global;
var that = this;
this.on('mousedown.kinetic_initdrag touchstart.kinetic_initdrag', function(evt) {
this.on('mousedown.kinetic touchstart.kinetic', function(evt) {
that._initDrag();
});
},
@@ -1141,8 +1175,8 @@ Kinetic.Node = Kinetic.Class.extend({
* remove drag and drop event listener
*/
_dragCleanup: function() {
this.off('mousedown.kinetic_initdrag');
this.off('touchstart.kinetic_initdrag');
this.off('mousedown.kinetic');
this.off('touchstart.kinetic');
},
/**
* handle node event
@@ -3439,7 +3473,7 @@ Kinetic.Ellipse = Kinetic.Shape.extend({
this._convertRadius();
var that = this;
this.on('radiusChange', function() {
this.on('radiusChange.kinetic', function() {
that._convertRadius();
});
},
@@ -3632,7 +3666,7 @@ Kinetic.Sprite = Kinetic.Shape.extend({
this._super(config);
var that = this;
this.on('animationChange', function() {
this.on('animationChange.kinetic', function() {
// reset index when animation changes
that.setIndex(0);
});
@@ -4011,7 +4045,7 @@ Kinetic.Text = Kinetic.Shape.extend({
var that = this;
for(var n = 0; n < attrs.length; n++) {
var attr = attrs[n];
this.on(attr + 'Change', that._setTextData);
this.on(attr + 'Change.kinetic', that._setTextData);
}
that._setTextData();
@@ -4470,9 +4504,7 @@ Kinetic.Path = Kinetic.Shape.extend({
}
}
this.fill();
//console.profile();
this.stroke();
//console.profileEnd();
};
// call super constructor
this._super(config);