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

137
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,70 +278,75 @@ 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];
// handle special keys
switch (key) { /*
/* * add functions, DOM elements, and images
* config properties that require a method to * directly to the node
* be set */
*/ if(go._isFunction(val) || go._isElement(val)) {
case 'draggable': this[key] = val;
this.draggable(config[key]); }
break; /*
case 'listening': * add all other object types to attrs object
this.listen(config[key]); */
break; else {
case 'rotationDeg': // handle special keys
this.attrs.rotation = config[key] * Math.PI / 180; switch (key) {
break; /*
/* * config properties that require a method to
* config objects * be set
*/ */
case 'centerOffset': case 'draggable':
if(val.x !== undefined) { this.draggable(config[key]);
this.attrs[key].x = val.x; break;
} case 'listening':
if(val.y !== undefined) { this.listen(config[key]);
this.attrs[key].y = val.y; break;
} case 'rotationDeg':
break; this.attrs.rotation = config[key] * Math.PI / 180;
case 'scale': break;
if(val.x !== undefined) { /*
this.attrs[key].x = val.x; * config objects
} */
if(val.y !== undefined) { case 'centerOffset':
this.attrs[key].y = val.y; if(val.x !== undefined) {
} this.attrs[key].x = val.x;
break; }
case 'crop': if(val.y !== undefined) {
if(val.x !== undefined) { this.attrs[key].y = val.y;
this.attrs[key].x = val.x; }
} break;
if(val.y !== undefined) { case 'scale':
this.attrs[key].y = val.y; if(val.x !== undefined) {
} this.attrs[key].x = val.x;
if(val.width !== undefined) { }
this.attrs[key].width = val.width; if(val.y !== undefined) {
} this.attrs[key].y = val.y;
if(val.height !== undefined) { }
this.attrs[key].height = val.height; break;
} case 'crop':
break; if(val.x !== undefined) {
/* this.attrs[key].x = val.x;
* config properties that we don't want in attrs }
*/ if(val.y !== undefined) {
case 'drawFunc': this.attrs[key].y = val.y;
break; }
case 'image': if(val.width !== undefined) {
break; this.attrs[key].width = val.width;
case 'container': }
break; if(val.height !== undefined) {
default: this.attrs[key].height = val.height;
this.attrs[key] = config[key]; }
break; break;
default:
this.attrs[key] = config[key];
break;
}
} }
} }
} }
@ -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,70 +120,75 @@ 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];
// handle special keys
switch (key) { /*
/* * add functions, DOM elements, and images
* config properties that require a method to * directly to the node
* be set */
*/ if(go._isFunction(val) || go._isElement(val)) {
case 'draggable': this[key] = val;
this.draggable(config[key]); }
break; /*
case 'listening': * add all other object types to attrs object
this.listen(config[key]); */
break; else {
case 'rotationDeg': // handle special keys
this.attrs.rotation = config[key] * Math.PI / 180; switch (key) {
break; /*
/* * config properties that require a method to
* config objects * be set
*/ */
case 'centerOffset': case 'draggable':
if(val.x !== undefined) { this.draggable(config[key]);
this.attrs[key].x = val.x; break;
} case 'listening':
if(val.y !== undefined) { this.listen(config[key]);
this.attrs[key].y = val.y; break;
} case 'rotationDeg':
break; this.attrs.rotation = config[key] * Math.PI / 180;
case 'scale': break;
if(val.x !== undefined) { /*
this.attrs[key].x = val.x; * config objects
} */
if(val.y !== undefined) { case 'centerOffset':
this.attrs[key].y = val.y; if(val.x !== undefined) {
} this.attrs[key].x = val.x;
break; }
case 'crop': if(val.y !== undefined) {
if(val.x !== undefined) { this.attrs[key].y = val.y;
this.attrs[key].x = val.x; }
} break;
if(val.y !== undefined) { case 'scale':
this.attrs[key].y = val.y; if(val.x !== undefined) {
} this.attrs[key].x = val.x;
if(val.width !== undefined) { }
this.attrs[key].width = val.width; if(val.y !== undefined) {
} this.attrs[key].y = val.y;
if(val.height !== undefined) { }
this.attrs[key].height = val.height; break;
} case 'crop':
break; if(val.x !== undefined) {
/* this.attrs[key].x = val.x;
* config properties that we don't want in attrs }
*/ if(val.y !== undefined) {
case 'drawFunc': this.attrs[key].y = val.y;
break; }
case 'image': if(val.width !== undefined) {
break; this.attrs[key].width = val.width;
case 'container': }
break; if(val.height !== undefined) {
default: this.attrs[key].height = val.height;
this.attrs[key] = config[key]; }
break; break;
default:
this.attrs[key] = config[key];
break;
}
} }
} }
} }

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) {