From 32e72176cac86d378f754b80df196a6b32713de8 Mon Sep 17 00:00:00 2001 From: Eric Rowell Date: Sat, 3 Nov 2012 22:20:46 -0700 Subject: [PATCH] isListening() method now takes into account ancestor listening --- src/Node.js | 41 +++++++++++++++++++++++------------- tests/js/unitTests.js | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/src/Node.js b/src/Node.js index 8b33be41..5e851e56 100644 --- a/src/Node.js +++ b/src/Node.js @@ -229,18 +229,31 @@ Kinetic.Node.prototype = { } }, /** - * determine if shape is visible or not. Shape is visible only + * determine if node is visible or not. Node is visible only * if it's visible and all of its ancestors are visible. If an ancestor - * is invisible, this means that the shape is also invisible - * @name isVisible + * is invisible, this means that the node is also invisible + * @name getVisible * @methodOf Kinetic.Node.prototype */ - isVisible: function() { - if(this.attrs.visible && this.getParent() && !this.getParent().isVisible()) { + getVisible: function() { + if(this.attrs.visible && this.getParent() && !this.getParent().getVisible()) { return false; } return this.attrs.visible; }, + /** + * determine if node is listening or not. Node is listening only + * if it's listening and all of its ancestors are listening. If an ancestor + * is not listening, this means that the node is also not listening + * @name getVisible + * @methodOf Kinetic.Node.prototype + */ + getListening: function() { + if(this.attrs.listening && this.getParent() && !this.getParent().getListening()) { + return false; + } + return this.attrs.listening; + }, /** * show node * @name show @@ -1015,17 +1028,23 @@ Kinetic.Node._createNode = function(obj, container) { return no; }; // add getters setters -Kinetic.Node.addGettersSetters(Kinetic.Node, ['x', 'y', 'rotation', 'opacity', 'name', 'id', 'listening', 'visible']); +Kinetic.Node.addGettersSetters(Kinetic.Node, ['x', 'y', 'rotation', 'opacity', 'name', 'id']); Kinetic.Node.addGetters(Kinetic.Node, ['scale', 'offset']); -Kinetic.Node.addSetters(Kinetic.Node, ['width', 'height']); +Kinetic.Node.addSetters(Kinetic.Node, ['width', 'height', 'listening', 'visible']); // mappings /** - * determine if listening to events or not. Alias of getListening() + * Alias of getListening() * @name isListening * @methodOf Kinetic.Node.prototype */ Kinetic.Node.prototype.isListening = Kinetic.Node.prototype.getListening; +/** + * Alias of getVisible() + * @name isVisible + * @methodOf Kinetic.Node.prototype + */ +Kinetic.Node.prototype.isVisible = Kinetic.Node.prototype.getVisible; // collection mappings (function() { @@ -1153,10 +1172,4 @@ Kinetic.Node.prototype.isListening = Kinetic.Node.prototype.getListening; * determine if listening to events or not * @name getListening * @methodOf Kinetic.Node.prototype - */ - -/** - * determine if visible or not - * @name getVisible - * @methodOf Kinetic.Node.prototype */ \ No newline at end of file diff --git a/tests/js/unitTests.js b/tests/js/unitTests.js index cd19a35c..2425b907 100644 --- a/tests/js/unitTests.js +++ b/tests/js/unitTests.js @@ -4112,6 +4112,44 @@ Test.prototype.tests = { layer.add(circle); stage.add(layer); }, + '*NODE - test isListening': function(containerId) { + var stage = new Kinetic.Stage({ + container: containerId, + width: 578, + height: 200 + }); + var layer = new Kinetic.Layer(); + var rect = new Kinetic.Rect({ + x: 100, + y: 100, + rotationDeg: 20, + width: 100, + height: 50, + fill: 'green', + stroke: 'black', + strokeWidth: 4 + }); + + layer.add(rect); + stage.add(layer); + + test(rect.isListening(), 'rect should be listening'); + + rect.setListening(false); + test(!rect.isListening(), 'rect should not be listening'); + + rect.setListening(true); + test(rect.isListening(), 'rect should be listening'); + + layer.setListening(false); + test(!rect.isListening(), 'rect should not be listening because layer is not listening'); + + layer.setListening(true); + test(rect.isListening(), 'rect should be listening'); + + stage.setListening(false); + test(!rect.isListening(), 'rect should not be listening because stage is not listening'); + }, 'NODE - test simulate and fire event': function(containerId) { var stage = new Kinetic.Stage({ container: containerId, @@ -4144,6 +4182,11 @@ Test.prototype.tests = { console.log(rightClick); */ }); + + var foo; + circle.on('customEvent', function(evt) { + foo = evt.foo; + }); layer.on('click', function() { clicks.push('layer'); @@ -4157,6 +4200,12 @@ Test.prototype.tests = { circle.fire('click'); test(clicks.toString() == 'circle,layer,circle', 'problem with fire'); + + // test custom event + circle.fire('customEvent', {foo:'bar'}); + + test(foo === 'bar', 'problem with customEvent param passing'); + }, 'EVENTS - add remove event': function(containerId) { var stage = new Kinetic.Stage({