Example #1
0
	_cleanup_values_array : function(array, arrow) {
		var index = array._.length - 1;
		while(true) {
			if (index === arrow._.index - 1) break;
			
			if (check.null(array._.values[index])) array._.values.pop();
			
			index -= 1;
		}
		
		return array;
	},
Example #2
0
		array._.pop = function(options) {
			var index = arrow._.index - 1;
			//optional argument: index, should be integer and in bounds on the array
			if (check.not.undefined(options) && check.not.undefined(options.index)) {
				if (check.not.integer(options.index)) {
					throw new TypeError("Optional argument 'index', should be an integer within the range 0...tail.");
				} else {
					if (options.index >= 0 && options.index < arrow._.index) {
						index = options.index
					} else {
						throw new RangeError("Optional argument 'index', should be an integer within the range 0...tail.");
					}
				}
			}
			
			if (array._.values.length === 0) {
				throw new RangeError("pop() from an empty array");
			}
			
			if (check.null(array._.values[index])) {
				throw new RangeError("pop() at empty index in the array.");
			}
			
			//cleanup the array of any extraneous null items in the values array
			array = instance._cleanup_values_array(array, arrow);
			
			//remove the item from values array and get the value for returning later
			//	using null here to signify a "gap" in the array, that the UI shows, but a real array would not show
			var ret = array._.values[index];
			array._.values[index] = null;
			
			//remove the item from the UI
			array.children[index].removeChildAt(1);
			
			return ret;
		};
Example #3
0
		array._.push = function(options) {
			if (check.undefined(options) || check.not.object(options)) {
				throw new TypeError("Requires a options object.");
			}
			
			//value should be a createjs.Text object, whose text value is a number
			if(check.not.instanceStrict(options.value, createjs.Text) || check.not.number(Number(options.value.text))) {
				throw new TypeError("Required argument: 'value', should be a createjs.Text object whose value is a number.");
			}
			
			//cleanup the array of any extraneous null items in the values array
			array = instance._cleanup_values_array(array, arrow);
			
			//error if array is full
			if (array._.values.length === array._.length) {
				throw new RangeError("Array is full, cannot push() new value.");
			}
			
			//determines whether push() will tween the object around the stage or simply place it in the array
			var tween = true;
			if (check.not.undefined(options.tween)) {
				if (check.not.boolean(options.tween)) {
					throw new TypeError("Optional argument: 'tween', should be true or false.");
				} else {
					tween = options.tween;
				}
			}
			
			var value = options.value;
			
			//get the index where we can insert (from arrow)
			var insertion_index = arrow._.index;
			
			if (tween) {
				//move() value to that index
				var move = require('lib/util/move');
				var value_move = move(value);
				
				var initial_point;
				//BUG: Equality testing does not work when comparing the stage to itself
				if (check.null(value.parent.parent)) {
					//if value is on the stage, then:
					//translate the initial point of the value, to the local coordinate space of the array
					initial_point = array.children[insertion_index].globalToLocal(value.x, value.y);
				} else {
					//else the object is inside some container, so translate the coordinates from inside that container, to the coordinates of the target container
					initial_point = value.localToLocal(value.x, value.y, array.children[insertion_index]);
					
					//if the container has a _.values decorated array, then subtract the value from the array at the source
					var possible_array = value.parent.parent;
					
					if(check.object(possible_array._) && check.array(possible_array._.values)) {
						//find the value in the source's values array
						var index = possible_array._.values.findIndex(function(element, index, array) {
							if (element === Number(value.text)) return true;
							return false;
						});
						
						//if found (return was not -1) ...
						if (index >= 0) {
							//...then remove the value at that index
							possible_array._.values.splice(index, 1);
						} else {
							//...if not, throw to alert the user of some issue
							throw new Error("Unknown error, when attempting to push() a value (" + Number(value.text) + ") onto this array, the source array containing that value did NOT have that value in its internal values array (source_array._.values)");
						}
					}
				}
				
				//before moving the value into the array container, set its position such that it will appear not to have moved after translating to the new coordinate space inside the container
				value.x = initial_point.x;
				value.y = initial_point.y;
				
				//add the value to the array as a child of the container at the insertion_index
				array.children[insertion_index].addChild(value);
				
				//just move the value to the location within the child container inside the array, since each ArrayElement has its own container, we place relative to that, which is always within the same position relative to the ArrayElement on the canvas
				value_move.to( {x:9, y:15} );
			} else {
				//else if we're not moving, then just add it to the array with manual positioning
				array.children[insertion_index].addChild(value);
				value.x = 9;
				value.y = 15;
			}
			
			//add Number(value.text) to values array
			array._.values.push(Number(value.text));
			
			//return the new length of the array, as per Array.prototype.push()
			return array._.values.length;
		};