server/src/main/webapp/static/js/corenkan.js
changeset 54 43c35d6ca3f0
parent 51 3247fccfbd3f
child 80 5295e118320b
--- a/server/src/main/webapp/static/js/corenkan.js	Mon Jan 14 16:00:07 2013 +0100
+++ b/server/src/main/webapp/static/js/corenkan.js	Mon Feb 11 10:34:23 2013 +0100
@@ -1,27 +1,33 @@
-//TODO add underscore as requirement
+/**
+ * TODO: add js header
+ */
+
 define([
     "dojo",
     "dojo/cookie",
+    "dojo/json",
     "coweb/main",
     "rcolor",
-    "underscore"
-], function(dojo, cookie, coweb, RColor, underscore) {
+], function(dojo, cookie, json, coweb, RColor) {
 	
     var CoRenkan = function() {
     };
 
     var proto = CoRenkan.prototype;
-    
-    var _ = underscore;
-   
+       
     proto.init = function() {
-    	console.log("ready callback");
+    	console.log("ready callback", dojo.config.corenkanConfig);
     	
-    	//this.initCollab();
-    	var that = this;
+    	this.renkan = dojo.config.corenkanConfig.renkan;
+    	this.project = dojo.config.corenkanConfig.renkan.project;
+
+    	this.initCollab(dojo.config.corenkanConfig.projectId);
     	    	
     	var sess = coweb.initSession();
-    	
+    	this.session = sess;
+
+    	var that = this;    	
+
     	sess.onStatusChange = function(status) {
     	    console.log(status);
     	    if(typeof that.onStatusChange === "function") {
@@ -29,28 +35,41 @@
     	    }
     	};
     	    	
-    	sess.prepare().then(function(data) {
+    	sess.prepare({userDefined: {project_id:dojo.config.corenkanConfig.projectId}, collab: true}).then(function(data) {
     		console.log("Prepare ok : ", data);
+        	that.setObjects();
     	});
-    	this.project = null;
                 
     };
     
-    proto.initCollab = function(id) {    	
-    	this.collab = coweb.initCollab({id : id});
+    proto.initCollab = function(id) {
+    	
+    	this.users_collab = coweb.initCollab({id: "users_" + id});
+    	
+    	this.users_collab.subscribeStateResponse(this, "onUsersStateResponse");
+    	this.users_collab.subscribeReady(this, "onLocalJoin");        
+        this.users_collab.subscribeSiteJoin(this, 'onRemoteJoin');
+        this.users_collab.subscribeSiteLeave(this, 'onRemoteLeave');
+    	this.users_collab.subscribeSync("roster", this, "onRemoteRosterChange");
+
+    	
+    	this.collab = coweb.initCollab({id : "renkan_" + id});    	    	    	
+    	
+    	this.collab.subscribeStateResponse(this, "onStateResponse");
+    	
     	this.collab.subscribeSync("project", this, "onRemoteProjectChange");
     	this.collab.subscribeSync("user", this, "onRemoteUserChange");
     	this.collab.subscribeSync("node", this, "onRemoteNodeChange");
     	this.collab.subscribeSync("edge", this, "onRemoteEdgeChange");
-    	this.collab.subscribeSync("_roster", this, "onRemoteRosterChange");
     	
-        this.collab.subscribeReady(this, 'onLocalJoin');
-        this.collab.subscribeSiteJoin(this, 'onRemoteJoin');
-        this.collab.subscribeSiteLeave(this, 'onRemoteLeave');
     };
     
     proto.onLocalJoin = function(params) {
     	console.log("Local join", params);
+    	this.current_site = params.site;
+    	if(typeof this.renkan !== "undefined" && this.renkan != null && typeof this.renkan.current_user != "undefined") {
+    		this.renkan.current_user.set("site_id", params.site);
+    	}
     };
     
     
@@ -66,9 +85,28 @@
     	if(typeof this.renkan === "undefined" || this.renkan == null || typeof this.renkan.current_user_list === "undefined" || this.renkan.current_user_list == null) {
     		return;
     	}
+    	for ( var user in this.renkan.current_user_list.filter(function(u) { return u.get("site_id") == params.site; })) {
+			this.renkan.current_user_list.remove(user);
+		}
     	
     };
     
+    proto.onUsersStateResponse = function(state) {
+    	user_list = json.parse(state);
+    	console.log("Users State response", user_list);
+    	_.each(user_list, function(user, i, l) {
+    		user['_id'] = user['id'];
+    	});    	
+    	this.renkan.current_user_list.reset(user_list, {silent: true});
+    };
+    
+    proto.onStateResponse = function(state) {
+    	obj = json.parse(state);
+    	console.log("State response", obj);
+    	obj['_id'] = obj['id'];
+    	this.project.set(obj, {silent: true});    	
+    };
+    
     function prepareValues(obj,c) {
 		values = {};
 		for(var fieldname in c.changes) {
@@ -79,8 +117,12 @@
 		return values;
     }
     
-    proto.addObjectBind = function(type, obj, c, options) {
-		console.log("add " + type,obj, c, options, this.project.toJSON());
+    proto.addObjectBind = function(type, obj, c, options, collab) {
+		console.log("add " + type,obj, c, options);
+		if(this.project == null) {
+			console.log("null project exiting");
+			return;
+		}
 		var values = obj.toJSON();
 		var new_values = {
 			id: obj.id,
@@ -93,10 +135,10 @@
 			values[k] = new_values[k];
 		}
 		console.log("add values : ", values);
-		this.collab.sendSync(type, values, "insert", options.index);
+		collab.sendSync(type, values, "insert", options.index);
     };
     
-    proto.removeObjectBind = function(type, obj, c, options) {
+    proto.removeObjectBind = function(type, obj, c, options, collab) {
 		console.log("delete " + type,obj, c, options);
 		var values = {
     	    id: obj.id,
@@ -105,10 +147,10 @@
     	    _project_id : obj.get("project").id,
     	    _user_id : (this.project.current_user!=null)?this.project.current_user.id:null
     	};
-		this.collab.sendSync(type, values, "delete", options.index);    	
+		collab.sendSync(type, values, "delete", options.index);    	
     };
     
-    proto.updateObjectBind = function(type, obj, options) {
+    proto.updateObjectBind = function(type, obj, options, collab) {
 		console.log("change " + type,obj, options);
 		if(typeof obj != "undefined" && obj.hasChanged()) {
 			var values = {
@@ -118,7 +160,7 @@
 	    	    _user_id : (this.project.current_user!=null)?this.project.current_user.id:null
 	    	};
 			_.extend(values,obj.changed);		
-	    	this.collab.sendSync(type, values);
+	    	collab.sendSync(type, values);
 		}
     };
     
@@ -144,15 +186,14 @@
 	};
 
     
-    proto.setObjects = function(renkan) {
+    proto.setObjects = function() {
     	
     	console.log(cookie("BAYEUX_BROWSER"));
+    	var renkan = this.renkan; 
     	var project = renkan.project;
     	this.setProject(project);
     	this.setRenkan(renkan);
-    	
-    	this.initCollab("renkan_" + project.id);
-    	
+    	    	
     	this.setUser(renkan);
 
     };
@@ -164,13 +205,13 @@
     	var that = this;
     	
     	renkan.current_user_list.bind("add", function(obj, c, options) {
-    		that.addObjectBind("_roster", obj, c, options);
+    		that.addObjectBind("roster", obj, c, options, that.users_collab);
     	});
-    	renkan.current_user_list.bind("remove", function(obj, c, options) {
-    		that.removeObjectBind("_roster", obj, c, options);
-    	});
+    	//renkan.current_user_list.bind("remove", function(obj, c, options) {
+    	//	that.removeObjectBind("_roster", obj, c, options, that.users_collab);
+    	//});
     	renkan.current_user_list.bind("change", function(obj, options) {
-    		that.updateObjectBind("_roster", obj, options);
+    		that.updateObjectBind("roster", obj, options, that.users_collab);
     	});
     	
     	renkan.current_user_list.bind("change", function(obj, options) {
@@ -193,9 +234,7 @@
     			}
     		}
     	});
-    	
-    	this.renkan = renkan;
-    	
+    	    	
     };
     
     proto.setUser = function(renkan) {
@@ -216,7 +255,8 @@
     		    id: user_id,
                 title: "anonymous",
                 project: project,
-                color: color.get(true, 0.3, 0.99)
+                color: color.get(true, 0.3, 0.99),
+                site_id: this.current_site
             };
     	}
     	else {
@@ -268,43 +308,41 @@
     	
     	
     	project.get("nodes").bind("add", function(obj, c, options) {    		
-    		that.addObjectBind("node", obj, c, options);
+    		that.addObjectBind("node", obj, c, options, that.collab);
     	});
     	
     	project.get("nodes").bind("remove", function(obj, c, options) {
-    		that.removeObjectBind("node", obj, c, options);
+    		that.removeObjectBind("node", obj, c, options, that.collab);
     	});
     	
     	project.get("nodes").bind("change", function(obj, options) {
-    		that.updateObjectBind("node", obj, options);
+    		that.updateObjectBind("node", obj, options, that.collab);
     	});
 
     	project.get("users").bind("add", function(obj, c, options) {    		
-    		that.addObjectBind("user", obj, c, options);
+    		that.addObjectBind("user", obj, c, options, that.collab);
     	});
     	
     	project.get("users").bind("remove", function(obj, c, options) {
-    		that.removeObjectBind("user", obj, c, options);
+    		that.removeObjectBind("user", obj, c, options, that.collab);
     	});
     	
     	project.get("users").bind("change", function(obj, options) {
-    		that.updateObjectBind("user", obj, options);
+    		that.updateObjectBind("user", obj, options, that.collab);
     	});
 
     	project.get("edges").bind("add", function(obj, c, options) {    		
-    		that.addObjectBind("edge", obj, c, options);
+    		that.addObjectBind("edge", obj, c, options, that.collab);
     	});
     	
     	project.get("edges").bind("remove", function(obj, c, options) {
-    		that.removeObjectBind("edge", obj, c, options);
+    		that.removeObjectBind("edge", obj, c, options, that.collab);
     	});
     	
     	project.get("edges").bind("change", function(obj, options) {
-    		that.updateObjectBind("edge", obj, options);
+    		that.updateObjectBind("edge", obj, options, that.collab);
     	});
-
     	
-    	this.project = project;
     };