enhanced setAttrs() logic to allow custom node properties and functions

This commit is contained in:
Eric Rowell 2012-04-27 19:42:04 -07:00
parent 508bfb7a9b
commit 70df77f9fa
5 changed files with 144 additions and 134 deletions

35
dist/kinetic-core.js vendored
View File

@ -140,6 +140,12 @@ Kinetic.GlobalObject = {
else { else {
this.frame.lastTime = 0; this.frame.lastTime = 0;
} }
},
_isElement: function(obj) {
return !!(obj && obj.nodeType == 1);
},
_isFunction: function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
} }
}; };
@ -272,10 +278,23 @@ Kinetic.Node.prototype = {
* @param {Object} config * @param {Object} config
*/ */
setAttrs: function(config) { setAttrs: function(config) {
var go = Kinetic.GlobalObject;
// set properties from config // set properties from config
if(config) { if(config) {
for(var key in config) { for(var key in config) {
var val = config[key]; var val = config[key];
/*
* add functions, DOM elements, and images
* directly to the node
*/
if(go._isFunction(val) || go._isElement(val)) {
this[key] = val;
}
/*
* add all other object types to attrs object
*/
else {
// handle special keys // handle special keys
switch (key) { switch (key) {
/* /*
@ -324,21 +343,13 @@ Kinetic.Node.prototype = {
this.attrs[key].height = val.height; this.attrs[key].height = val.height;
} }
break; break;
/*
* config properties that we don't want in attrs
*/
case 'drawFunc':
break;
case 'image':
break;
case 'container':
break;
default: default:
this.attrs[key] = config[key]; this.attrs[key] = config[key];
break; break;
} }
} }
} }
}
}, },
/** /**
* determine if shape is visible or not * determine if shape is visible or not
@ -2167,9 +2178,6 @@ Kinetic.Shape = function(config) {
this.attrs.lineJoin = undefined; this.attrs.lineJoin = undefined;
this.attrs.detectionType = 'path'; this.attrs.detectionType = 'path';
// special
this.drawFunc = config.drawFunc;
this.data = []; this.data = [];
this.nodeType = 'Shape'; this.nodeType = 'Shape';
@ -2563,9 +2571,6 @@ Kinetic.Image = function(config) {
height: undefined height: undefined
}; };
// special
this.image = config.image;
this.shapeType = "Image"; this.shapeType = "Image";
config.drawFunc = function() { config.drawFunc = function() {
if(this.image !== undefined) { if(this.image !== undefined) {

View File

@ -112,6 +112,12 @@ Kinetic.GlobalObject = {
else { else {
this.frame.lastTime = 0; this.frame.lastTime = 0;
} }
},
_isElement: function(obj) {
return !!(obj && obj.nodeType == 1);
},
_isFunction: function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
} }
}; };

View File

@ -120,10 +120,23 @@ Kinetic.Node.prototype = {
* @param {Object} config * @param {Object} config
*/ */
setAttrs: function(config) { setAttrs: function(config) {
var go = Kinetic.GlobalObject;
// set properties from config // set properties from config
if(config) { if(config) {
for(var key in config) { for(var key in config) {
var val = config[key]; var val = config[key];
/*
* add functions, DOM elements, and images
* directly to the node
*/
if(go._isFunction(val) || go._isElement(val)) {
this[key] = val;
}
/*
* add all other object types to attrs object
*/
else {
// handle special keys // handle special keys
switch (key) { switch (key) {
/* /*
@ -172,21 +185,13 @@ Kinetic.Node.prototype = {
this.attrs[key].height = val.height; this.attrs[key].height = val.height;
} }
break; break;
/*
* config properties that we don't want in attrs
*/
case 'drawFunc':
break;
case 'image':
break;
case 'container':
break;
default: default:
this.attrs[key] = config[key]; this.attrs[key] = config[key];
break; break;
} }
} }
} }
}
}, },
/** /**
* determine if shape is visible or not * determine if shape is visible or not

View File

@ -26,9 +26,6 @@ Kinetic.Shape = function(config) {
this.attrs.lineJoin = undefined; this.attrs.lineJoin = undefined;
this.attrs.detectionType = 'path'; this.attrs.detectionType = 'path';
// special
this.drawFunc = config.drawFunc;
this.data = []; this.data = [];
this.nodeType = 'Shape'; this.nodeType = 'Shape';

View File

@ -19,9 +19,6 @@ Kinetic.Image = function(config) {
height: undefined height: undefined
}; };
// special
this.image = config.image;
this.shapeType = "Image"; this.shapeType = "Image";
config.drawFunc = function() { config.drawFunc = function() {
if(this.image !== undefined) { if(this.image !== undefined) {