1 /**
  2  * 
  3  * Creates a new mind map.
  4  * 
  5  * @constructor
  6  * @param {mindmaps.Node} [root]
  7  */
  8 mindmaps.MindMap = function(root) {
  9 	/**
 10 	 * nodes is only used for quick lookup of a node by id. Each node must be
 11 	 * registered in this map via createNode() or addNode(node).
 12 	 */
 13 	this.nodes = new mindmaps.NodeMap();
 14 
 15 	if (root) {
 16 		this.root = root;
 17 	} else {
 18 		this.root = new mindmaps.Node();
 19 		this.root.text.font.size = 20;
 20 		this.root.text.font.weight = "bold";
 21 		this.root.text.caption = "Central Idea";
 22 	}
 23 
 24 	this.addNode(this.root);
 25 };
 26 
 27 /**
 28  * Creates a new mind map object from JSON String.
 29  * 
 30  * @static
 31  * @param {String} json
 32  * @returns {mindmaps.MindMap}
 33  */
 34 mindmaps.MindMap.fromJSON = function(json) {
 35 	return mindmaps.MindMap.fromObject(JSON.parse(json));
 36 };
 37 
 38 /**
 39  * Creates a new mind map object from generic object.
 40  * 
 41  * @static
 42  * @param {Object} obj
 43  * @returns {mindmaps.MindMap}
 44  */
 45 mindmaps.MindMap.fromObject = function(obj) {
 46 	var root = mindmaps.Node.fromObject(obj.root);
 47 	var mm = new mindmaps.MindMap(root);
 48 
 49 	// register all nodes in the map
 50 	root.forEachDescendant(function(descendant) {
 51 		mm.addNode(descendant);
 52 	});
 53 
 54 	return mm;
 55 };
 56 
 57 /**
 58  * Called by JSON.stringify().
 59  * 
 60  * @private
 61  * 
 62  */
 63 mindmaps.MindMap.prototype.toJSON = function() {
 64 	var obj = {
 65 		root : this.root
 66 	};
 67 	return obj;
 68 };
 69 
 70 /**
 71  * Creates a JSON representation of the mind map.
 72  * 
 73  * @returns {String}
 74  */
 75 mindmaps.MindMap.prototype.serialize = function() {
 76 	return JSON.stringify(this);
 77 };
 78 
 79 /**
 80  * Create a node and add to nodes map.
 81  * 
 82  * @returns {mindmaps.Node}
 83  */
 84 mindmaps.MindMap.prototype.createNode = function() {
 85 	var node = new mindmaps.Node();
 86 	this.addNode(node);
 87 	return node;
 88 };
 89 
 90 /**
 91  * Adds an existing node and all its children to the nodes map.
 92  * 
 93  * @param {mindmaps.Node} node
 94  */
 95 mindmaps.MindMap.prototype.addNode = function(node) {
 96 	this.nodes.add(node);
 97 
 98 	// add all children
 99 	var self = this;
100 	node.forEachDescendant(function(descendant) {
101 		self.nodes.add(descendant);
102 	});
103 };
104 
105 /**
106  * Removes a node from the mind map. Severs tie to the parent.
107  * 
108  * @param {mindmaps.Node} node
109  */
110 mindmaps.MindMap.prototype.removeNode = function(node) {
111 	// detach node from parent
112 	var parent = node.parent;
113 	parent.removeChild(node);
114 
115 	// clear nodes table: remove node and its children
116 	var self = this;
117 	node.forEachDescendant(function(descendant) {
118 		self.nodes.remove(descendant);
119 	});
120 
121 	this.nodes.remove(node);
122 };
123 
124 /**
125  * Get the root of the mind map.
126  * 
127  * @returns {mindmaps.Node}
128  */
129 mindmaps.MindMap.prototype.getRoot = function() {
130 	return this.root;
131 };