sessions,ajaxSocketLogin, authorData=(schema,socket)

This commit is contained in:
Curtis SerVaas
2014-08-20 10:30:19 -07:00
parent 3f25adb1e6
commit 56aab83d53
12 changed files with 428 additions and 348 deletions
+109 -121
View File
@@ -27,11 +27,6 @@ console.log("\n\n");
module.exports = {
// createUser: function(username, email, password, callback) {
// var user = {username: username, email: email
// , password: encryptPassword(password)};
// db.insertOne('users', user, callback);
// },
createUser: function(username, password, callback){
User.addUser(username, password, callback);
},
@@ -44,137 +39,130 @@ module.exports = {
io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket){
socket.on("COMMIT", function(){
makeCommit();
socket.emit("commitReceived");
socket.AUTHOR = {}; //anonymous.
socket.on("logIn", function(data){
socket.AUTHOR = data;
attachLogInListeners(socket);
});
socket.on('nodeRequest', function(data){
var nodes = Node.findAndSocketSend(socket); //finds, then sends through socket.
});
socket.on("revHistoryRequest", function(subRootId){
getAndSendRevHistory(subRootId, socket);
});
//socket.emit('news', {hello: "world"});
socket.on('nodeRequest', function(data){
var nodes = Node.findAndSendSocket(socket); //finds, then sends through socket.
//var nodes = {'keep': 'calm'};
//socket.emit('nodeData', nodes); (emit is in findNodes);
})
socket.on("edit", function(data){
var id = data[0];
var newText = data[1];
console.log("\n\n\n\n edit received:" + newText);
Node.updateText(id, newText);
socket.broadcast.emit("edit", [id, newText]);
});
//This works for all ids except negative ids.
socket.on("editing", function(id){
socket.broadcast.emit("editing", id);
});
socket.on("blurred", function(data){
// console.log('\nBLURRED\n')
socket.broadcast.emit("blurred", data);
var id = data[0];
var text = data[1];
Node.updateText(id, text);
});
//I'm pretty sure i don't use this anywhere.
socket.on('nodeInsert', function(data){
Node.addNode("testText", [], [], function(){});
});
socket.on("transclude", function(data){
var parId = data[0];
var transcludeId = data[2];
var newIndex = data[1];
var now = Date.now();
Node.updateParent(parId, transcludeId ,newIndex, now );
})
socket.on("newNode", function(data){
//data[0] = [parID, newindex] . data[1] =
var modelJson = data[1]; //(includes the negative ID to find later);
var parId = data[0][0];
var newIndex = data[0][1];
var now = Date.now();
var callback = function(err, instance, now){
socket.emit("updateReceived", [modelJson._id ,instance, data[0]]);
socket.broadcast.emit("newNode", [ data[0] ,instance]);
//io.sockets.emit("newNode", [data[0], instance])
//I'm going to have to update the parent Model on this as well...
//(and therefore, broadcast the array...)
Node.updateParent(parId, instance._id ,newIndex, now );
}
Node.addNode(modelJson.text, modelJson.children, modelJson.parents, callback);
});
socket.on("removeNode", function(data){
// console.log("removeNode!!!");
var thisId = data[0];
var thisIndex = data[1];
var parId = data[2];
socket.broadcast.emit("removeNode", data);
Node.removeNode(thisId, thisIndex, parId);
});
socket.on("movedNode", function(data){
// var ids = [thisModel.get("_id"), oldParModel.get("_id"), newParModel.get("_id")];
// var arrays = [thisModel.get("parents"), oldParModel.get("children"), newParModel.get("children")];
// var data = [ids, arrays];
Node.moveNode(data[0], data[1]);
socket.broadcast.emit("movedNode", [data[0], data[2]]);
});
socket.on("getTimeHash", function(){
var timeHash = getTimeHash();
socket.emit("timeHash", timeHash);
})
});
// io.configure(function (){
// io.set('authorization', function (handshakeData, callback) {
// if (handshakeData.headers.cookie) {
// handshakeData.cookie = cookie.parse(decodeURIComponent(handshakeData.headers.cookie));
// handshakeData.sessionID = handshakeData.cookie['connect.sid'];
// sessionStore.get(handshakeData.sessionID, function (err, session) {
// if (err || !session) {
// return callback(null, false);
// } else {
// handshakeData.session = session;
// console.log('session data', session);
// return callback(null, true);
// }
// });
// }
// else {
// return callback(null, false);
// }
// });
// });
},
},
setUpDB: function(){
Node.setUpDB();
//getTimeHash();
}
}
function attachLogInListeners(socket){
socket.on("COMMIT", function(){
makeCommit();
socket.emit("commitReceived");
});
//Used when a node is split...
socket.on("edit", function(data){
var id = data[0];
var newText = data[1];
console.log("\n\n\n\n edit received:" + newText);
Node.updateText(id, newText);
socket.broadcast.emit("edit", [id, newText]);
});
//This works for all ids except negative ids.
socket.on("editing", function(data){ //data=[id, Author.name]
socket.broadcast.emit("editing", data);
});
socket.on("blurred", function(data){
socket.broadcast.emit("blurred", data);
var id = data[0];
var text = data[1];
var author = data[2];
Node.updateText(id, text, author._id);
});
// ADD AUTHOR INFORMATION
// socket.on("transclude", function(data){
// var parId = data[0];
// var transcludeId = data[2];
// var newIndex = data[1];
// var now = Date.now();
// Node.updateParent(parId, transcludeId ,newIndex, now );
// });
socket.on("newNode", function(data){
//data[0] = [parID, newindex] . data[1] = modelJSON
var modelJson = data[1]; //(includes the negative ID to find later);
var parId = data[0][0];
var newIndex = data[0][1];
var Author = data[2];
var now = Date.now();
var callback = function(err, instance, now){
socket.emit("updateReceived", [modelJson._id ,instance, data[0]]);
socket.broadcast.emit("newNode", [ [parId,newIndex] , instance ]);
//io.sockets.emit("newNode", [data[0], instance])
//I'm going to have to update the parent Model on this as well...
//(and therefore, broadcast the array...)
Node.updateParent(parId, instance._id ,newIndex, now );
}
Node.addNode(modelJson.text, modelJson.children, modelJson.parents, modelJson.author._id ,callback);
});
socket.on("removeNode", function(data){
// console.log("removeNode!!!");
var thisId = data[0];
var thisIndex = data[1];
var parId = data[2];
var author = data[3]; data[3] = null;
socket.broadcast.emit("removeNode", data);
Node.removeNode(thisId, thisIndex, parId, author._id);
});
socket.on("movedNode", function(data){
// var ids = [thisModel.get("_id"), oldParModel.get("_id"), newParModel.get("_id")];
// var arrays = [thisModel.get("parents"), oldParModel.get("children"), newParModel.get("children")];
// var indices = [dragIndex, dropIndex];
// var data = [ids, arrays, indices, CurrentUser];
Node.moveNode(data[0], data[1],data[3]._id);
socket.broadcast.emit("movedNode", [data[0], data[2]]);
});
socket.on("getTimeHash", function(){
var timeHash = getTimeHash();
socket.emit("timeHash", timeHash);
}) ;
}
+2 -1
View File
@@ -30,7 +30,8 @@ function updateGlobals(rootSnaps){
//This should only be called once per ID.
function FetchSelfAndChildrenBack(rootId, callback){
MySnap.find({cur_id: rootId}).sort({timestamp: 1}).exec(function(err, rootSnaps){
MySnap.find({cur_id: rootId}).populate('authorId','_id google.name' ).sort({timestamp: 1}).
exec(function(err, rootSnaps){
if(err){console.log("ERROR HERE")}
updateGlobals(rootSnaps);
+30 -7
View File
@@ -1,6 +1,9 @@
module.exports = function(app, passport) {
app.get('/', function(req, res) { res.render('index'); } );
app.get('/', function(req, res) {
var user = {bleh: "blah"}
res.render('index', {data: user});
});
// route for logging out
app.get('/logout', function(req, res) {
@@ -12,13 +15,33 @@ module.exports = function(app, passport) {
app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] }));
//scope?? //(callBack = None).
// the callback after google has authenticated the user
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect : '/',
failureRedirect : '/'
}));
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect : '/',
failureRedirect : '/'
}));
// // the callback after google has authenticated the user
// app.get('/auth/google/callback', function(req, res, next){
// passport.authenticate('google', function(err, user, info) {
// if(err){return next(err);}
// if(!user){return res.render('index.ejs', { data: {failed: "true"} } ) }
// req.logIn(user, function(err){
// if(err) {return next(err); }
// return res.redirect("/");
// // return res.render('index.ejs', { data: user } );
// });
// })(req, res, next); //have to actually call the passport method.
// });
app.post("/ajaxlogin", function(req, res){
console.log("AJAXLOGIN");
console.log("req.isAuthenticated() = " + req.isAuthenticated());
res.send(req.user);
})
};
// route middleware to make sure a user is logged in
+26 -17
View File
@@ -5,7 +5,8 @@ var NodeSchema = new mongoose.Schema({
children: {type: Array},
parents: {type: Array},
markdown: {type: Boolean},
timestamp: {type: Number}
timestamp: {type: Number},
author: {type: String, ref: 'User'} //authorId is populated with limited AuthorObject
});
var MyNode = mongoose.model('nodes', NodeSchema);
@@ -19,7 +20,7 @@ var addSnap = snapModule.addSnap;
// Exports
module.exports.MyNode = MyNode;
module.exports.findAndSendSocket = findAndSendSocket
module.exports.findAndSocketSend = findAndSocketSend
module.exports.setUpDB = setUpDB;
module.exports.moveNode = moveNode;
@@ -38,10 +39,11 @@ module.exports.addNode = addNode;
// return rootID;
// }
function findAndSendSocket(socket){
function findAndSocketSend(socket){
var nodes = {'keeping': 'calm'}
MyNode.find(function(err, nodes){
MyNode.find().populate('authorId','_id google.name' ).exec(function(err, nodes){
if(!err){
// require("underscore").each(nodes, function(node){console.log(node)});
socket.emit('nodeData', nodes)
return {'hell': 'yes'}
}else{
@@ -57,12 +59,10 @@ function findAndSendSocket(socket){
function setUpDB(){
MyNode.remove({}, function(err) { console.log('collection removed') });
MySnap.remove({}, function(err) { console.log('collection removed') });
// addNode("0root", [], ["123456"]);
// addNode("Welcolme!", [], [rootNode._id]);
addNode("0root", [], ["123456"], function(err, rootNode){
addNode("Welcolme!", [], [rootNode._id], function(err, firstBullet){
var curtisId = "53e4079cd7dbc73d16c87c53";
addNode("0root", [], ["123456"], curtisId ,function(err, rootNode){
addNode("Welcolme!", [], [rootNode._id], curtisId ,function(err, firstBullet){
rootNode.children = [firstBullet._id]
var now = rootNode.timestamp = firstBullet.timestamp
addSnap(rootNode, now);
@@ -79,20 +79,24 @@ function setUpDB(){
function moveNode(ids, arrays){
var thisId = ids[0]; //draggedId
function moveNode(ids, arrays, authorId){
var now = Date.now();
var thisId = ids[0]; //draggedId
MyNode.findById(thisId, null, function(err, node){
node.author = authorId; //
node.timestamp = now;
node.parents = arrays[0];
node.save();
});
var oldParId = ids[1];
MyNode.findById(oldParId, null, function(err, node){
node.timestamp = now;
node.children = arrays[1];
node.save();
});
var newParId = ids[2];
MyNode.findById(newParId, null, function(err, node){
node.timestamp = now;
@@ -101,8 +105,9 @@ function moveNode(ids, arrays){
});
}
function removeNode(thisId, thisIndex, parId){
function removeNode(thisId, thisIndex, parId, authorId){
var now = Date.now();
MyNode.findById(parId, null, function(err, parNode){
if(err || parNode == null){
return;
@@ -114,14 +119,16 @@ function removeNode(thisId, thisIndex, parId){
parNode.save();
});
MyNode.findById(thisId, null, function(err, delNode){
if(err || delNode == null){
return;
}
delNode.author = authorId;
delNode.timestamp = now;
if(delNode.parents.length ==1 ){//(this if statement is technically redundant)
// delNode.remove();
delNode.parnets = []; //(this is how deletion is represented).
delNode.parents = []; //(this is how deletion is represented).
delNode.save();
}
else{ //this is the condition that we'll have to take care of if there are dups.
@@ -131,18 +138,19 @@ function removeNode(thisId, thisIndex, parId){
})
}
function updateText(id, newText){
function updateText(id, newText, authorId){
MyNode.findById(id, null, function(err, node){
if(err || node == null){
return;
}
node.timestamp = Date.now();
node.text = newText;
node.authorId = authorId;
node.save();
});
}
function updateParent(parId, newId ,newIndex, now){
function updateParent(parId, newId ,newIndex,now){
MyNode.findById(parId, null, function(err, parNode){
if(err || parNode == null){
return;
@@ -153,13 +161,14 @@ function updateParent(parId, newId ,newIndex, now){
})
}
//add Node to the DB.
function addNode(text, children, parents, callback){
function addNode(text, children, parents, authorId, callback){
var instance = new MyNode();
instance.text = text;
instance.children = children;
instance.parents = parents;
instance.markdown = 0;
instance.timestamp = Date.now();
instance.authorId = authorId;
instance.save(function (err) {
if (err) {
+3 -1
View File
@@ -7,7 +7,8 @@ var SnapSchema = new mongoose.Schema({
markdown: {type: Boolean},
timestamp: {type: Number},
cur_id: {type: String}
cur_id: {type: String},
authorId: {type: String, ref: 'User'}
});
var MySnap = mongoose.model('snaps', SnapSchema);
@@ -29,6 +30,7 @@ function addSnap(node, now, remove, callback){
instance.markdown = node.markdown;
instance.cur_id = node._id;
instance.timestamp = now;
instance.authorId = node.authorId;
if(remove){
+19 -8
View File
@@ -1,6 +1,5 @@
$(function(){
//alert("jquery works");
var myRouter;
$("#COMMIT").click(function(){
@@ -13,11 +12,21 @@ $(function(){
$(e.target).attr("data-id", rootId);
});
$("body").on("click", ".snapLI", function(e){
$(e.target).addClass("selectedSnap");
});
$(".toggleSidebar").click(function(){
$("#mainPanel").toggleClass("floatRight centering");
$("#metaSidebar").toggleClass("hideBar");
});
$("#markdownButton").click(function(){
vo.thisView.createMarkDownBullet();
});
@@ -37,6 +46,9 @@ $(function(){
$("body").on("mouseover", "a.expandCollapse", function(event){
// console.log("bleh");
// debugger;
@@ -65,18 +77,17 @@ $(function(){
var that = this;
voInitializer(that, event);
var id = $(event.target).closest("li").attr("data-id");
console.log("ABOUT TO EMIT 'EDITING' ");
console.log(id);
socket.emit("editing", id);
socket.emit("editing", [id, CurrentUser._id]);
});
$("body").on("blur", "textarea", function(event){
var thisLI = $(event.target).closest("li");
var id = thisLI.attr("data-id");
var text = thisLI.children().children("textarea").val();
//console.log(id);
//alert("blurred ID" + id + text);
socket.emit("blurred", [id, text]);
$("textarea").textareaAutoExpand();
$("textarea").textareaAutoExpand();
socket.emit("blurred", [id, text, CurrentUser]);
});
$("body").on("click", ".transclude", function(event){
//alert(vo.thisId);
+2 -1
View File
@@ -8,6 +8,7 @@ addNode = function(botStr, topStr){
, text: botStr
, parents: [vo.parentId]
, children: []
, author: CurrentUser
};
@@ -27,7 +28,7 @@ addNode = function(botStr, topStr){
vo.parentModel.get("children").insert(vo.thisIndex + 1, randomId);
socket.emit("newNode", [[vo.parentId, vo.thisIndex+1], modelJSON ]);
socket.emit("newNode", [ [vo.parentId, vo.thisIndex+1] , modelJSON ]);
var parentViews = vo.parentModel.get("views");
+3 -4
View File
@@ -26,10 +26,9 @@ var moveNode = function(thisModel, dragIndex, oldParModel, newParModel, dropInde
var ids = [thisModel.get("_id"), oldParModel.get("_id"), newParModel.get("_id")];
var arrays = [thisModel.get("parents"), oldParModel.get("children"), newParModel.get("children")];
var indices = [dragIndex, dropIndex];
var data = [ids, arrays, indices];
console.log("about to emit data!!");
console.log(data);
if(chr){
var data = [ids, arrays, indices, CurrentUser];
if(chr){
socket.emit("movedNode", data);
}
+5 -3
View File
@@ -9,8 +9,8 @@
return parId != vo.parentId;
});
vo.thisModel.set("parents", newParents);
if(!broadcast){
socket.emit("removeNode", [vo.thisId, vo.thisIndex, vo.parentId]);
if(!broadcast){ //broadCast=true means we received this update from somebody else.
socket.emit("removeNode", [vo.thisId, vo.thisIndex, vo.parentId, CurrentUser]);
}
}
@@ -22,7 +22,8 @@ var upDateParentModelViews = function(vo, broadcast){
vo.parentModel.set("children", children);
if(!broadcast){
if(!broadcast){ //broadCast=true means we received this update from somebody else.
//UI STUFF
console.log("what");
if( !vo.thisLI.is(":first-child") ){
@@ -36,6 +37,7 @@ var upDateParentModelViews = function(vo, broadcast){
}
}
_.each(vo.parentModel.get("views"), function(parentView){
parentView.removeNode(vo.thisIndex);
});
+223 -180
View File
@@ -5,191 +5,56 @@
socketEvents: _.extend({}, Backbone.Events),
routes: {
'': 'index',
':id': "index"
'': 'initialize',
':id': "viewRoot"
},
initialize: function(){
//alert('initializing router');
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("<a href='logout'>Logout</a> ");
socket.emit("logIn", data); //logs the user into the socket.
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");
}
});
},
index: function(otherID){
var that = this;
//alert('index');
//this.viewRoot();
if(socket){ //Most of the time.
that.viewRoot(otherID);
return;
}
// index: function(otherID){
// var that = this;
// otherID = otherID ||
//(initialization)
socket = io.connect();
socket.emit("nodeRequest");
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);
});
socket.on("commitReceived", function(){
alert("commitReceived");
});
socket.on("revHistory", function(data){
alert("revHistory!!");
console.log("revHistory!!");
snapHash = data[0];
timeHash = data[1];
console.log("snapHash");
console.log(snapHash);
console.log("timeHash");
console.log(timeHash);
var list = "";
_.each(Object.keys(timeHash), function(timestamp){
list += "<li><a class='timestamp'>"+timestamp+"</a></li>";
})
$("#revTimestamps").html(list);
})
$("#revTimestamps").on("click", "a.timestamp", function(event){
// debugger;
var a = event.target;
var timestamp = parseInt($(a).html());
var subRootId = $(".getRevHistory").attr("data-id");
var snapCollection = renderRevControl(subRootId, timestamp);
console.log("snapCollection")
console.log(snapCollection);
that.viewRoot(subRootId, snapCollection);
});
socket.on('edit', function(data){
var id = data[0];
var newText = data[1];
var curModel = nodesCollection.findWhere({_id: id})
curModel.set("text", newText);
_.each(curModel.get("views"), function(view){
view.updateText(newText);
});
});
socket.on("updateReceived", function(data){
//data[0] is _id. data[1] is instance.
var instance = data[1];
var old_id = data[0];
var parId = data[2][0];
var newIndex = data[2][1];
var parentModel = nodesCollection.findWhere({_id: parId});
var new_id = instance._id
var oldModel = nodesCollection.findWhere({ _id: old_id });
oldModel.set("_id", new_id );
_.each(oldModel.get("views"), function(view){
view.updateId(new_id);
});
parentModel.get("children")[newIndex] = instance._id;
})
socket.on("newNode", function(data){
//need the instance + the index.
var modelJson = data[1]; //(includes the negative ID to find later);
var parId = data[0][0];
var newIndex = data[0][1];
var newNode = new NodeModel(modelJson);
nodesCollection.add(newNode);
var parentModel = nodesCollection.findWhere({_id: parId});
var parentViews = parentModel.get("views");
_.each(parentViews, function(parentView){
parentView.addNode(newNode, newIndex).lock();
//look at socket.emit("newNode" to see why -1);
});
parentModel.get("children").insert(newIndex, modelJson._id);
});
socket.on("editing", function(id){
console.log("EDITING RECEIVED + ID");
console.log(id);
if(id < 0){return;}
var tempViews = nodesCollection.findWhere({_id: id}).get("views");
_.each(tempViews, function(tempView){
tempView.lock();
//tempView.updateText("editing...");
});
});
socket.on("blurred", function(data){
// console.log('WHAT THE FUCK!!');
// console.log(data);
var id = data[0];
var text = data[1];
//console.log()
//alert("blurred\n" + id + text);
var tempModel = nodesCollection.findWhere({_id: id});
tempModel.set("text", text);
var tempViews = tempModel.get("views");
_.each(tempViews, function(tempView){
tempView.unlock();
tempView.updateText(text);
});
});
socket.on("removeNode", function(data){
var vo = {};
vo.thisId = data[0];
vo.thisIndex = data[1];
vo.parentId = data[2]; //not parId
vo.thisModel = nodesCollection.findWhere({_id: vo.thisId});
vo.parentModel = nodesCollection.findWhere({_id: vo.parentId});
//alert("broadcastRemoveNode")
removeNode(vo, true); //true means broadcast.
});
socket.on("movedNode", function(data){
var ids = data[0];
var indices = data[1];
var thisModel = nodesCollection.findWhere({_id: ids[0]});
var oldParModel = nodesCollection.findWhere({_id: ids[1]});
var newParModel = nodesCollection.findWhere({_id: ids[2]});
// debugger;
moveNode(thisModel, indices[0], oldParModel, newParModel, indices[1]);
});
socket.on("revControl", function(data){
//(store all of this...).
var timeHash = data[0];
var snapHash = data[1];
var timeStamps = timeHash.keys;
//To be continued...
});
},
// },
viewRoot: function(id, snapCollection){
@@ -224,10 +89,188 @@
$("textarea").textareaAutoExpand();
}
},
setUpSocket: function(){
var that = this;
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);
});
socket.on("commitReceived", function(){
alert("commitReceived");
});
socket.on("revHistory", function(data){
alert("revHistory!!");
console.log("revHistory!!");
snapHash = data[0];
timeHash = data[1];
console.log("snapHash");
console.log(snapHash);
console.log("timeHash");
console.log(timeHash);
var list = "";
_.each(Object.keys(timeHash), function(timestamp){
list += "<li><a class='timestamp'>"+timestamp+"</a></li>";
})
$("#revTimestamps").html(list);
})
$("#revTimestamps").on("click", "a.timestamp", function(event){
// debugger;
var a = event.target;
var timestamp = parseInt($(a).html());
var subRootId = $(".getRevHistory").attr("data-id");
var snapCollection = renderRevControl(subRootId, timestamp);
console.log("snapCollection")
console.log(snapCollection);
that.viewRoot(subRootId, snapCollection);
});
socket.on('edit', function(data){
var id = data[0];
var newText = data[1];
var curModel = nodesCollection.findWhere({_id: id})
curModel.set("text", newText);
_.each(curModel.get("views"), function(view){
view.updateText(newText);
});
});
socket.on("updateReceived", function(data){
//data[0] is _id. data[1] is instance.
var instance = data[1];
var old_id = data[0];
var parId = data[2][0];
var newIndex = data[2][1];
var parentModel = nodesCollection.findWhere({_id: parId});
var new_id = instance._id
var oldModel = nodesCollection.findWhere({ _id: old_id });
oldModel.set("_id", new_id );
_.each(oldModel.get("views"), function(view){
view.updateId(new_id);
});
parentModel.get("children")[newIndex] = instance._id;
})
socket.on("newNode", function(data){
//need the instance + the index.
var modelJson = data[1]; //(includes the negative ID to find later);
var parId = data[0][0];
var newIndex = data[0][1];
var newNode = new NodeModel(modelJson);
nodesCollection.add(newNode);
var parentModel = nodesCollection.findWhere({_id: parId});
var parentViews = parentModel.get("views");
_.each(parentViews, function(parentView){
parentView.addNode(newNode, newIndex).lock();
//look at socket.emit("newNode" to see why -1);
});
parentModel.get("children").insert(newIndex, modelJson._id);
});
socket.on("editing", function(data){
var id = data[0];
var authorName = data[1];
console.log("EDITING RECEIVED + ID");
console.log(id);
if(id < 0){return;}
var tempViews = nodesCollection.findWhere({_id: id}).get("views");
_.each(tempViews, function(tempView){
tempView.lock();
});
});
socket.on("blurred", function(data){
var id = data[0];
var text = data[1];
var author = data[2];
var tempModel = nodesCollection.findWhere({_id: id});
tempModel.set("text", text);
tempModel.set("author", author);
var tempViews = tempModel.get("views");
_.each(tempViews, function(tempView){
tempView.unlock();
tempView.updateText(text);
});
});
socket.on("removeNode", function(data){
var tempVo = {}; //PROTECTING STATE!!
// tempVo.author = data[3]; //(actually, unnecessary...)
tempVo.thisId = data[0];
tempVo.thisIndex = data[1];
tempVo.parentId = data[2];
tempVo.thisModel = nodesCollection.findWhere({_id: tempVo.thisId});
tempVo.parentModel = nodesCollection.findWhere({_id: tempVo.parentId});
removeNode(tempVo, true); //true means came from broadcast.
});
socket.on("movedNode", function(data){
var ids = data[0];
var indices = data[1];
var author = data[2];
var thisModel = nodesCollection.findWhere({_id: ids[0]});
var oldParModel = nodesCollection.findWhere({_id: ids[1]});
var newParModel = nodesCollection.findWhere({_id: ids[2]});
thisModel.set("author", author);
moveNode(thisModel, indices[0], oldParModel, newParModel, indices[1]);
});
socket.on("revControl", function(data){
//(store all of this...).
var timeHash = data[0];
var snapHash = data[1];
var timeStamps = timeHash.keys;
//To be continued...
});
socket.emit("nodeRequest");
}
});
function setUpSocket(){
}
+2 -2
View File
@@ -167,9 +167,9 @@ var showView = Backbone.View.extend({
var that = this;
this.$el.children().children("textarea").val(that.model.get("text"));
},
lock: function(){ //for concurrent editing...
lock: function(name){ //for concurrent editing...
this.$el.children().children("textarea").attr("readonly", "readonly");
this.$el.children().children("textarea").val("editing....")
this.$el.children().children("textarea").val(name + " is editing.");
},
unlock: function(){
this.$el.children().children("textarea").removeAttr("readonly");
+4 -3
View File
@@ -59,7 +59,8 @@
<ul class='navbar-right'>
<li><a class='toggleSidebar'>Toggle sideBar</a></li>
<li><a href='/auth/google'>Sign In With Google</a></li>
<li><a id="googleButton" href='/auth/google'>Sign In With Google</a></li>
<!-- <li><a id="profileButton"></a></li> -->
<li><a id='COMMIT'>Make Commit!</a></li>
<!-- <li><a><img src='img/undo.png'></a></li>
<li><a><img src='img/redo.png'></a></li> -->
@@ -95,12 +96,12 @@
</div>
<script src="js/tempFolder/bootstrap.js"></script>
<script src="js/tempFolder/startup.js"></script>
<!-- <script src="js/tempFolder/startup.js"></script> -->
</body>
<script>
$(function(){
$('textarea').textareaAutoExpand();
});
});
</script>
</html>