created Collection class to handle iterating over arrays returned from get()

This commit is contained in:
ericdrowell
2012-09-23 10:53:23 -07:00
parent 8d6103d1e0
commit 9f5d0fe351
7 changed files with 90 additions and 77 deletions

View File

@@ -4,7 +4,7 @@ class Build < Thor
# This is the list of files to concatenate. The first file will appear at the top of the final file. All files are relative to the lib directory.
FILES = [
"license.js", "src/Global.js", "src/Transition.js", "src/filters/Grayscale.js",
"src/util/Type.js", "src/util/Canvas.js", "src/util/Tween.js", "src/util/Transform.js",
"src/util/Type.js", "src/util/Canvas.js", "src/util/Tween.js", "src/util/Transform.js", "src/util/Collection.js",
"src/Animation.js", "src/Node.js", "src/Container.js", "src/Stage.js", "src/Layer.js", "src/Group.js", "src/Shape.js",
"src/shapes/Rect.js", "src/shapes/Circle.js", "src/shapes/Ellipse.js", "src/shapes/Image.js", "src/shapes/Polygon.js", "src/shapes/Text.js", "src/shapes/Line.js", "src/shapes/Sprite.js", "src/shapes/Star.js", "src/shapes/RegularPolygon.js", "src/shapes/Path.js", "src/shapes/TextPath.js"
]

74
dist/kinetic-core.js vendored
View File

@@ -3,7 +3,7 @@
* http://www.kineticjs.com/
* Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses.
* Date: Sep 22 2012
* Date: Sep 23 2012
*
* Copyright (C) 2011 - 2012 by Eric Rowell
*
@@ -1029,6 +1029,45 @@ Kinetic.Transform.prototype = {
}
};
/**
* Collection constructor. Collection extends
* Array. This class is used in conjunction with get()
* @constructor
*/
Kinetic.Collection = function() {
var args = [].slice.call( arguments ),
length = args.length, i = 0;
this.length = length;
for (; i < length; i++ ) {
this[ i ] = args[ i ];
}
return this;
}
Kinetic.Collection.prototype = new Array();
/**
* apply a method to all nodes in the array
* @name apply
* @methodOf Kinetic.Collection.prototype
* @param {String} method
* @param val
*/
Kinetic.Collection.prototype.apply = function(method, val) {
for (var n=0; n<this.length; n++) {
this[n][method](val);
}
};
/**
* iterate through node array
* @name each
* @methodOf Kinetic.Collection.prototype
* @param {Function} func
*/
Kinetic.Collection.prototype.each = function(func) {
for (var n=0; n<this.length; n++) {
func(this[n]);
}
};
///////////////////////////////////////////////////////////////////////
// Animation
///////////////////////////////////////////////////////////////////////
@@ -2262,37 +2301,6 @@ Kinetic.Node._addGetter = function(constructor, attr) {
Kinetic.Node.addGettersSetters(Kinetic.Node, ['x', 'y', 'scale', 'rotation', 'opacity', 'name', 'id', 'offset', 'draggable', 'dragConstraint', 'dragBounds', 'listening']);
Kinetic.Node.addSetters(Kinetic.Node, ['rotationDeg']);
/**
* Node array constructor. Node.Array extends
* Array. This class is used to run node methods on
* an array of nodes returned from get()
* @constructor
*/
Kinetic.Node.Array = function() {
var args = [].slice.call( arguments ),
length = args.length, i = 0;
this.length = length;
for (; i < length; i++ ) {
this[ i ] = args[ i ];
}
return this;
}
Kinetic.Node.Array.prototype = new Array();
// node methods
for(var key in Kinetic.Node.prototype) {
if(!(key in Kinetic.Node.Array.prototype)) {
(function(k) {
Kinetic.Node.Array.prototype[k] = function() {
for (var n=0; n< this.length; n++) {
Kinetic.Node.prototype[k].apply(this[n], arguments);
}
}
})(key);
}
}
/**
* set node x position
* @name setX
@@ -2609,7 +2617,7 @@ Kinetic.Container.prototype = {
return false;
}
var retArr = new Kinetic.Node.Array();
var retArr = new Kinetic.Collection();
for(var n = 0; n < arr.length; n++) {
var node = arr[n];
if(this.isAncestorOf(node)) {

File diff suppressed because one or more lines are too long

View File

@@ -157,7 +157,7 @@ Kinetic.Container.prototype = {
return false;
}
var retArr = new Kinetic.Node.Array();
var retArr = new Kinetic.Collection();
for(var n = 0; n < arr.length; n++) {
var node = arr[n];
if(this.isAncestorOf(node)) {

View File

@@ -1106,37 +1106,6 @@ Kinetic.Node._addGetter = function(constructor, attr) {
Kinetic.Node.addGettersSetters(Kinetic.Node, ['x', 'y', 'scale', 'rotation', 'opacity', 'name', 'id', 'offset', 'draggable', 'dragConstraint', 'dragBounds', 'listening']);
Kinetic.Node.addSetters(Kinetic.Node, ['rotationDeg']);
/**
* Node array constructor. Node.Array extends
* Array. This class is used to run node methods on
* an array of nodes returned from get()
* @constructor
*/
Kinetic.Node.Array = function() {
var args = [].slice.call( arguments ),
length = args.length, i = 0;
this.length = length;
for (; i < length; i++ ) {
this[ i ] = args[ i ];
}
return this;
}
Kinetic.Node.Array.prototype = new Array();
// node methods
for(var key in Kinetic.Node.prototype) {
if(!(key in Kinetic.Node.Array.prototype)) {
(function(k) {
Kinetic.Node.Array.prototype[k] = function() {
for (var n=0; n< this.length; n++) {
Kinetic.Node.prototype[k].apply(this[n], arguments);
}
}
})(key);
}
}
/**
* set node x position
* @name setX

39
src/util/Collection.js Normal file
View File

@@ -0,0 +1,39 @@
/**
* Collection constructor. Collection extends
* Array. This class is used in conjunction with get()
* @constructor
*/
Kinetic.Collection = function() {
var args = [].slice.call( arguments ),
length = args.length, i = 0;
this.length = length;
for (; i < length; i++ ) {
this[ i ] = args[ i ];
}
return this;
}
Kinetic.Collection.prototype = new Array();
/**
* apply a method to all nodes in the array
* @name apply
* @methodOf Kinetic.Collection.prototype
* @param {String} method
* @param val
*/
Kinetic.Collection.prototype.apply = function(method, val) {
for (var n=0; n<this.length; n++) {
this[n][method](val);
}
};
/**
* iterate through node array
* @name each
* @methodOf Kinetic.Collection.prototype
* @param {Function} func
*/
Kinetic.Collection.prototype.each = function(func) {
for (var n=0; n<this.length; n++) {
func(this[n]);
}
};

View File

@@ -654,16 +654,13 @@ Test.prototype.tests = {
test(shapes.length === 2, 'shapes array should have 2 elements');
shapes.setX(200);
shapes.apply('setX', 200);
layer.draw();
for (var n=0; n<shapes.length; n++) {
test(shapes[n].getX() === 200, 'shape x should be 200');
}
shapes.setDraggable(true);
shapes.each(function(shape) {
test(shape.getX() === 200, 'shape x should be 200');
});
},
'STAGE - test ids and names hashes': function(containerId) {
var stage = new Kinetic.Stage({