1 /**
  2  * Creates a new NodeMap object. Map implementation for nodes. The key is
  3  * automatically set to the node id.
  4  * 
  5  * @constructor
  6  */
  7 mindmaps.NodeMap = function() {
  8 	this.nodes = {};
  9 	this.count = 0;
 10 };
 11 
 12 /**
 13  * Return a node by its ID.
 14  * 
 15  * @param {String} nodeId
 16  * @returns {mindmaps.Node}
 17  */
 18 mindmaps.NodeMap.prototype.get = function(nodeId) {
 19 	return this.nodes[nodeId];
 20 };
 21 
 22 /**
 23  * Adds a node to the map if it hasn't been added before.
 24  * 
 25  * @param {mindmaps.Node} node
 26  * @returns {Boolean} true if added, false otherwise.
 27  */
 28 mindmaps.NodeMap.prototype.add = function(node) {
 29 	if (!this.nodes.hasOwnProperty(node.id)) {
 30 		this.nodes[node.id] = node;
 31 		this.count++;
 32 		return true;
 33 	}
 34 	return false;
 35 };
 36 
 37 /**
 38  * Removes a node from the map.
 39  * 
 40  * @param {mindmaps.Node} node
 41  * @returns {Boolean} true if removed, false otherwise.
 42  */
 43 mindmaps.NodeMap.prototype.remove = function(node) {
 44 	if (this.nodes.hasOwnProperty(node.id)) {
 45 		delete this.nodes[node.id];
 46 		this.count--;
 47 		return true;
 48 	}
 49 	return false;
 50 };
 51 
 52 /**
 53  * Returns the number of nodes in the map.
 54  * 
 55  * @returns {Number}
 56  */
 57 mindmaps.NodeMap.prototype.size = function() {
 58 	return this.count;
 59 };
 60 
 61 /**
 62  * Returns all nodes in the map.
 63  * 
 64  * @returns {Array}
 65  */
 66 mindmaps.NodeMap.prototype.values = function() {
 67 	return Object.keys(this.nodes).map(function(key) {
 68 		return this.nodes[key];
 69 	}, this);
 70 };
 71 
 72 /**
 73  * Iterator for nodes.
 74  * 
 75  * @param {Function} callback, first argument should be the node.
 76  */
 77 mindmaps.NodeMap.prototype.each = function(callback) {
 78 	for ( var id in this.nodes) {
 79 		callback(this.nodes[id]);
 80 	}
 81 };