From 18731566d995ffcd0a6468f0f0af7172d5c5b3b6 Mon Sep 17 00:00:00 2001 From: Curtis SerVaas Date: Mon, 29 Sep 2014 13:00:44 -0400 Subject: [PATCH] Crude offline Freezing --- public/js/app.js | 4 + public/js/backbone/router.js | 182 +++++++++++++------------ public/js/libs/myLogic/addNode.js | 6 +- public/js/libs/myLogic/moveNode.js | 2 +- public/js/libs/myLogic/removeNode.js | 2 +- public/js/listeners/editorListeners.js | 4 +- public/js/listeners/revListeners.js | 4 +- 7 files changed, 108 insertions(+), 96 deletions(-) diff --git a/public/js/app.js b/public/js/app.js index 6aa1524..f449f9a 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -13,6 +13,10 @@ $(function(){ }); + + + + function createPathMenu(event){ //console.log($(event.target).attr("data-id")); var pathDiv = $(event.target).parent().children("#pathDiv") diff --git a/public/js/backbone/router.js b/public/js/backbone/router.js index 0315d72..85c7178 100644 --- a/public/js/backbone/router.js +++ b/public/js/backbone/router.js @@ -1,111 +1,119 @@ - var socket = null; - var AppRouter = Backbone.Router.extend({ +var socket = null; - socketEvents: _.extend({}, Backbone.Events), +var AppRouter = Backbone.Router.extend({ - routes: { - '': 'initialize', - ':id': "viewRoot" - }, + socketEvents: _.extend({}, Backbone.Events), - initialize: function(){ + routes: { + '': 'initialize', + ':id': "viewRoot" + }, - if(socket){ return; }//prevents double-initialization. - socket = io.connect(); - this.setUpSocket(); - + initialize: function(){ + this.socketInitialize(); + }, - $.ajax({ - url: "/ajaxlogin", - type: "POST", + socketInitialize: function(){ + if(socket){ return; }//prevents double-initialization. + socket = io.connect(); + this.setUpSocket(); + + $.ajax({ + url: "/ajaxlogin", + type: "POST", - complete: function(){ - }, - success: function(data){ - console.log("AjaxLoginDATA"); - if(typeof data == "string"){ - $("textarea").attr("disabled", true); - } //User is not logged in. - if(typeof data == "object"){//we've got the userObject. - $("#googleButton").parent().html("Logout "); - socket.emit("logIn", data); //logs the user into the socket. + complete: function(){ + }, + success: function(data){ + console.log("AjaxLoginDATA"); + if(typeof data == "string"){ + $("textarea").attr("disabled", true); + } //User is not logged in. + if(typeof data == "object"){//we've got the userObject. + $("#googleButton").parent().html("Logout "); + socket.emit("logIn", data); //logs the user into the socket. - data.google.id = null; - data.google.token = null; - data.google.email = null; - CurrentUser = data; + data.google.id = null; + data.google.token = null; + data.google.email = null; + CurrentUser = data; - console.log("CURRENT USER"); - console.log(CurrentUser); - } - }, - - - error: function(data){ - console.log("ERROR- AjaxLoginDATA"); + console.log("CURRENT USER"); + console.log(CurrentUser); } - }); + }, - }, - // index: function(otherID){ - // var that = this; - - // otherID = otherID || - - // }, - - viewRoot: function(id, snapCollection){ - - var rootModel; - var metaCollection; - var snapView = 0; - if(snapCollection){ - rootModel = snapCollection.findWhere({cur_id: id}); - metaCollection = snapCollection - snapView = 1; - } - else{ - rootModel = nodesCollection.findWhere({_id: id}); - metaCollection = nodesCollection; + error: function(data){ + console.log("ERROR- AjaxLoginDATA"); } + });//ajax + }, - var rootView = new listView({ - viewWindow: ".main1", - model: rootModel, - metaCollection: metaCollection, - snapView: snapView - }) - this.changeView(rootView); - }, + // index: function(otherID){ + // var that = this; + + // otherID = otherID || - changeView: function(view) { - if ( null != this.currentView ) { - this.currentView.undelegateEvents(); - } - - this.currentView = view; - $("textarea").textareaAutoExpand(); - + // }, - }, + viewRoot: function(id, snapCollection){ + + var rootModel; + var metaCollection; + var snapView = 0; + if(snapCollection){ + rootModel = snapCollection.findWhere({cur_id: id}); + metaCollection = snapCollection + snapView = 1; + } + else{ + rootModel = nodesCollection.findWhere({_id: id}); + metaCollection = nodesCollection; + } + + var rootView = new listView({ + viewWindow: ".main1", + model: rootModel, + metaCollection: metaCollection, + snapView: snapView + }) + this.changeView(rootView); + }, + + changeView: function(view) { + if ( null != this.currentView ) { + this.currentView.undelegateEvents(); + } + + this.currentView = view; + $("textarea").textareaAutoExpand(); + + + }, setUpSocket: function(){ var that = this; + +socket.emitWrapper = function(eventName, data){ + console.log(this); + if(this.socket.connected){ this.emit(eventName, data); } + else{ //(alert + lock) + alert("Internet connection down. Edits not synced. Refresh when back online."); + } +} + socket.on('nodeData', function(data){ -//alert("data"); - -console.log(data); -nodesCollection = new NodesCollection(data); -var id = nodesCollection.findWhere({text: "0root"}).get("_id"); -// if(otherID){ -// id = otherID -// } - - -that.viewRoot(id); + //alert("data"); + console.log(data); + nodesCollection = new NodesCollection(data); + var id = nodesCollection.findWhere({text: "0root"}).get("_id"); + // if(otherID){ + // id = otherID + // } + that.viewRoot(id); }); diff --git a/public/js/libs/myLogic/addNode.js b/public/js/libs/myLogic/addNode.js index 24522c7..d826289 100644 --- a/public/js/libs/myLogic/addNode.js +++ b/public/js/libs/myLogic/addNode.js @@ -16,7 +16,7 @@ addNode = function(botStr, topStr){ _.each(vo.thisModel.get("views"), function(view){ view.updateText(topStr); }); - socket.emit("edit", [vo.thisId, topStr]); + socket.emitWrapper("edit", [vo.thisId, topStr]); } @@ -27,7 +27,7 @@ addNode = function(botStr, topStr){ var data = [ [vo.parentId, vo.thisIndex+1] , modelJSON ]; console.log("AddNode DATA"); console.log(data); - socket.emit("newNode", data); + socket.emitWrapper("newNode", data); var parentViews = vo.parentModel.get("views"); @@ -73,7 +73,7 @@ transclude = function(){ , parents: vo.thisModel.get("parents") , children: vo.thisModel.get("children") } - socket.emit("transclude" , [vo.parentId, vo.thisIndex+1, vo.thisId]); + socket.emitWrapper("transclude" , [vo.parentId, vo.thisIndex+1, vo.thisId]); var parentViews = vo.parentModel.get("views"); var tempIndex = vo.thisIndex+1; //adding nodes alters the index. _.each(parentViews, function(parentView){ diff --git a/public/js/libs/myLogic/moveNode.js b/public/js/libs/myLogic/moveNode.js index d69b951..286642e 100644 --- a/public/js/libs/myLogic/moveNode.js +++ b/public/js/libs/myLogic/moveNode.js @@ -29,7 +29,7 @@ var moveNode = function(thisModel, dragIndex, oldParModel, newParModel, dropInde var data = [ids, arrays, indices, CurrentUser]; if(chr){ - socket.emit("movedNode", data); + socket.emitWrapper("movedNode", data); } INPUT_PROCESSED=true; diff --git a/public/js/libs/myLogic/removeNode.js b/public/js/libs/myLogic/removeNode.js index 652899e..08a603e 100644 --- a/public/js/libs/myLogic/removeNode.js +++ b/public/js/libs/myLogic/removeNode.js @@ -10,7 +10,7 @@ }); vo.thisModel.set("parents", newParents); if(!broadcast){ //broadCast=true means we received this update from somebody else. - socket.emit("removeNode", [vo.thisId, vo.thisIndex, vo.parentId, CurrentUser]); + socket.emitWrapper("removeNode", [vo.thisId, vo.thisIndex, vo.parentId, CurrentUser]); } } diff --git a/public/js/listeners/editorListeners.js b/public/js/listeners/editorListeners.js index dcf9654..9fd9742 100644 --- a/public/js/listeners/editorListeners.js +++ b/public/js/listeners/editorListeners.js @@ -45,7 +45,7 @@ $(function(){ var that = this; // alert("editing"); console.log("EDITING"); var id = $(event.target).closest("li").attr("data-id"); - socket.emit("editing", [id, CurrentUser.google.name]); + socket.emitWrapper("editing", [id, CurrentUser.google.name]); voInitializer(that, event); keydownHandler(event); //(This event is not firing for some reason..(I wonder if I put it at the top)). @@ -55,7 +55,7 @@ $(function(){ var id = thisLI.attr("data-id"); var text = thisLI.children().children("textarea").val(); $("textarea").textareaAutoExpand(); - socket.emit("blurred", [id, text, CurrentUser]); + socket.emitWrapper("blurred", [id, text, CurrentUser]); // alert("blur!!"); // editing=false; }); diff --git a/public/js/listeners/revListeners.js b/public/js/listeners/revListeners.js index 2ae6d34..c5fdc03 100644 --- a/public/js/listeners/revListeners.js +++ b/public/js/listeners/revListeners.js @@ -1,11 +1,11 @@ $(function(){ $("#COMMIT").click(function(){ - socket.emit("COMMIT"); + socket.emitWrapper("COMMIT"); }); $(".getRevHistory").click(function(e){ var rootId = $(".root").attr("data-id") - socket.emit("revHistoryRequest", rootId); + socket.emitWrapper("revHistoryRequest", rootId); $(e.target).attr("data-id", rootId); });