now leveraging the setAttrs method for all setters in Node class. utilizing draggableChange attr event to trigger drag and drop inits

This commit is contained in:
Eric Rowell
2012-06-09 15:31:25 -07:00
parent 9697af530c
commit bba1d0ad21
5 changed files with 248 additions and 167 deletions

141
dist/kinetic-core.js vendored
View File

@@ -366,7 +366,7 @@ window.requestAnimFrame = (function(callback) {
Kinetic.Node = function(config) {
this.defaultNodeAttrs = {
visible: true,
listening: true,
listen: true,
name: undefined,
alpha: 1,
x: 0,
@@ -387,6 +387,17 @@ Kinetic.Node = function(config) {
this.setDefaultAttrs(this.defaultNodeAttrs);
this.eventListeners = {};
// bind events
this.on('draggableChange.kinetic_reserved', function() {
if(this.attrs.draggable) {
this._listenDrag();
}
else {
this._dragCleanup();
}
});
this.setAttrs(config);
};
/*
@@ -531,23 +542,11 @@ Kinetic.Node.prototype = {
else {
// handle special keys
switch (key) {
/*
* config properties that require a method
*/
case 'draggable':
that.draggable(c[key]);
break;
case 'listening':
that.listen(c[key]);
break;
case 'rotationDeg':
that._setAttr(obj, 'rotation', c[key] * Math.PI / 180);
// override key for change event
key = 'rotation';
break;
/*
* config objects
*/
case 'centerOffset':
var pos = go._getXY(val);
that._setAttr(obj[key], 'x', pos.x);
@@ -606,13 +605,17 @@ Kinetic.Node.prototype = {
* show node
*/
show: function() {
this.attrs.visible = true;
this.setAttrs({
visible: true
});
},
/**
* hide node
*/
hide: function() {
this.attrs.visible = false;
this.setAttrs({
visible: false
});
},
/**
* get zIndex
@@ -694,14 +697,18 @@ Kinetic.Node.prototype = {
* @param {Number} x
*/
setX: function(x) {
this.attrs.x = x;
this.setAttrs({
x: x
});
},
/**
* set node y position
* @param {Number} y
*/
setY: function(y) {
this.attrs.y = y;
this.setAttrs({
y: y
});
},
/**
* get node x position
@@ -720,7 +727,9 @@ Kinetic.Node.prototype = {
* @param {String} type can be "path" or "pixel"
*/
setDetectionType: function(type) {
this.attrs.detectionType = type;
this.setAttrs({
detectionType: type
});
},
/**
* get detection type
@@ -790,26 +799,41 @@ Kinetic.Node.prototype = {
},
/**
* move node by an amount
* @param {Number} x
* @param {Number} y
*/
move: function(x, y) {
this.attrs.x += x;
this.attrs.y += y;
move: function() {
var pos = Kinetic.GlobalObject._getXY(arguments);
var x = this.getX();
var y = this.getY();
if(pos.x !== undefined) {
x += pos.x;
}
if(pos.y !== undefined) {
y += pos.y;
}
this.setAttrs({
x: x,
y: y
});
},
/**
* set node rotation in radians
* @param {Number} theta
*/
setRotation: function(theta) {
this.attrs.rotation = theta;
this.setAttrs({
rotation: theta
});
},
/**
* set node rotation in degrees
* @param {Number} deg
*/
setRotationDeg: function(deg) {
this.attrs.rotation = (deg * Math.PI / 180);
this.setRotation(deg * Math.PI / 180);
},
/**
* get rotation in radians
@@ -828,21 +852,33 @@ Kinetic.Node.prototype = {
* @param {Number} theta
*/
rotate: function(theta) {
this.attrs.rotation += theta;
this.setAttrs({
rotation: this.getRotation() + theta
});
},
/**
* rotate node by an amount in degrees
* @param {Number} deg
*/
rotateDeg: function(deg) {
this.attrs.rotation += (deg * Math.PI / 180);
this.setAttrs({
rotation: this.getRotation() + (deg * Math.PI / 180)
});
},
/**
* listen or don't listen to events
* @param {Boolean} listening
* @param {Boolean} listen
*/
listen: function(listening) {
this.attrs.listening = listening;
listen: function(listen) {
this.setAttrs({
listen: listen
});
},
/**
* is listening or not
*/
isListening: function() {
return this.attrs.listen;
},
/**
* move node to top
@@ -899,7 +935,9 @@ Kinetic.Node.prototype = {
* @param {Object} alpha
*/
setAlpha: function(alpha) {
this.attrs.alpha = alpha;
this.setAttrs({
alpha: alpha
});
},
/**
* get alpha. Alpha values range from 0 to 1.
@@ -924,18 +962,12 @@ Kinetic.Node.prototype = {
},
/**
* enable or disable drag and drop
* @param {Boolean} isDraggable
* @param {Boolean} draggable
*/
draggable: function(isDraggable) {
if(this.attrs.draggable !== isDraggable) {
if(isDraggable) {
this._listenDrag();
}
else {
this._dragCleanup();
}
this.attrs.draggable = isDraggable;
}
draggable: function(draggable) {
this.setAttrs({
draggable: draggable
});
},
/**
* determine if node is currently in drag and drop mode
@@ -1101,7 +1133,9 @@ Kinetic.Node.prototype = {
* @param {String} constraint
*/
setDragConstraint: function(constraint) {
this.attrs.dragConstraint = constraint;
this.setAttrs({
dragConstraint: constraint
});
},
/**
* get drag constraint
@@ -1118,7 +1152,9 @@ Kinetic.Node.prototype = {
* @config {Number} [bottom] bottom bounds position
*/
setDragBounds: function(bounds) {
this.attrs.dragBounds = bounds;
this.setAttrs({
dragBounds: bounds
});
},
/**
* get drag bounds
@@ -1172,9 +1208,7 @@ Kinetic.Node.prototype = {
return m;
},
_fireChangeEvent: function(attr) {
if(this.getStage() !== undefined) {
this._handleEvent(attr + 'Change', {});
}
this._handleEvent(attr + 'Change', {});
},
_setAttr: function(obj, attr, val) {
if(val !== undefined) {
@@ -1218,8 +1252,8 @@ Kinetic.Node.prototype = {
}
var stage = this.getStage();
var mouseoverNode = stage.mouseoverShape;
var mouseoutNode = stage.mouseoutShape;
var mouseoverNode = stage ? stage.mouseoverShape : null;
var mouseoutNode = stage ? stage.mouseoutShape : null;
var el = this.eventListeners;
var okayToRun = true;
@@ -1514,7 +1548,7 @@ Kinetic.Stage = function(config) {
this._setStageDefaultProperties();
this._id = Kinetic.GlobalObject.idCounter++;
this._buildDOM();
this._listen();
this._bindEvents();
this._prepareDrag();
var go = Kinetic.GlobalObject;
@@ -2018,7 +2052,7 @@ else if(!isDragging && this.touchMove) {
// propapgate backwards through children
for(var i = children.length - 1; i >= 0; i--) {
var child = children[i];
if(child.attrs.listening) {
if(child.attrs.listen) {
if(child.nodeType === 'Shape') {
var exit = this._detectEvent(child, evt);
if(exit) {
@@ -2062,7 +2096,7 @@ else if(!isDragging && this.touchMove) {
var shapeDetected = false;
for(var n = this.children.length - 1; n >= 0; n--) {
var layer = this.children[n];
if(layer.isVisible() && n >= 0 && layer.attrs.listening) {
if(layer.isVisible() && n >= 0 && layer.attrs.listen) {
if(this._traverseChildren(layer, evt)) {
shapeDetected = true;
break;
@@ -2083,7 +2117,7 @@ else if(!isDragging && this.touchMove) {
* begin listening for events by adding event handlers
* to the container
*/
_listen: function() {
_bindEvents: function() {
var go = Kinetic.GlobalObject;
var that = this;
@@ -2384,7 +2418,6 @@ else if(!isDragging && this.touchMove) {
this.content.appendChild(this.pathLayer.canvas);
this.setSize(this.attrs.width, this.attrs.height);
this._resizeDOM();
},
_addId: function(node) {
if(node.attrs.id !== undefined) {

File diff suppressed because one or more lines are too long