Permalink
Browse files

[#11543703] Deletion sends an array of canvas objects from the front-…

…end.
  • Loading branch information...
1 parent c62412b commit 62a510d45238862fdb13ffa0a649cbf9f2766f61 @adrianbravo adrianbravo committed Mar 25, 2011
Showing with 47 additions and 57 deletions.
  1. +1 −2 app.js
  2. +34 −37 lib/message_processor.js
  3. +11 −13 public/javascripts/main.js
  4. +0 −4 public/javascripts/message_processor.js
  5. +1 −1 test/lib/message_processor.js
View
3 app.js
@@ -74,8 +74,7 @@ io.on('connection', function(client){
client.on('disconnect', function(){
client.user.unsubscribe_all();
- // TODO restrict this to a project
- //client.broadcast({ announcement: client.user.ip + ' disconnected' });
+ //client.broadcast({ message: client.user.ip + ' disconnected' });
});
});
View
@@ -1,13 +1,13 @@
var Project = require('./project').Project
- , Page = require('./page').Page
- , CanvasObject = require('./canvas_object').CanvasObject;
+, Page = require('./page').Page
+, CanvasObject = require('./canvas_object').CanvasObject;
var MessageProcessor = {
- // Routes messages to their appropriate processors.
- // So a message like { user_update: {} } should call
- // user_update() with the data keyed by user_update.
- process: function (client, message) {
+ // Routes messages to their appropriate processors.
+ // So a message like { user_update: {} } should call
+ // user_update() with the data keyed by user_update.
+ process: function(client, message) {
for (var action in message) {
if (this[action]) {
this[action](client, message[action]);
@@ -17,21 +17,21 @@ var MessageProcessor = {
}
},
- project_find: function(client, data) {
- Project.find_by_id(data.id, function(project) {
- if (project.error || !project.validate_hash(data.hash)) { // Project not found or hash not valid
- client.send({ error: project.error });
- return;
- }
+ project_find: function(client, data) {
+ Project.find_by_id(data.id, function(project) {
+ if (project.error || !project.validate_hash(data.hash)) { // Project not found or hash not valid
+ client.send({ error: project.error });
+ return;
+ }
if (typeof project.password != 'undefined') {
client.send({ project_prompt_password: true });
} else {
client.user.assign_project(project._id);
client.send({ project_load: project });
}
- });
- },
+ });
+ },
project_authorize: function(client, data) {
// authorize that the password matches the client's project
@@ -54,12 +54,12 @@ var MessageProcessor = {
client.user.assign_project(project._id, true);
client.send({ project_load: project, project_create: true });
});
- },
+ },
project_update: function(client, data) {
if (data.password && !client.user.created_project) delete data.password;
- Project.find_by_id(client.user.project_id, function(project) {
+ Project.find_by_id(client.user.project_id, function(project) {
project.update(data, function(project) {
if (project.error) {
client.send({ error: project.error });
@@ -84,15 +84,15 @@ var MessageProcessor = {
client.user.name = data.name;
client.send({
- announcement: 'You successfully changed your display name to ' + data.name + '.',
+ message: 'You successfully changed your display name to ' + data.name + '.',
user_update: { name: data.name }
});
- client.user.broadcast_to_project({ announcement: client.user.handle() + ' changed their display name to ' + data.name + '.' });
+ client.user.broadcast_to_project({ message: client.user.handle() + ' changed their display name to ' + data.name + '.' });
},
page_create: function(client, data) {
- Project.find_by_id(client.user.project_id, function(project) {
+ Project.find_by_id(client.user.project_id, function(project) {
Page.create(project, function(page) {
if (page.error) {
client.send({ error: page.error });
@@ -115,8 +115,7 @@ var MessageProcessor = {
return;
}
client.user.broadcast_to_project({ page_update: { page: page.json() } }, true);
- var msg = { message: client.user.handle() + ' renamed a project page.'};
- client.user.broadcast_to_project(msg, true);
+ client.user.broadcast_to_project({ message: client.user.handle() + ' renamed a project page.'}, true);
});
});
},
@@ -128,8 +127,7 @@ var MessageProcessor = {
if (page.error) {
client.send({ error: page.error });
} else {
- var msg = { message: client.user.handle() + ' deleted a project page.'};
- client.user.broadcast_to_project(msg, true);
+ client.user.broadcast_to_project({ message: client.user.handle() + ' deleted a project page.'}, true);
client.user.broadcast_to_project({ page_delete: { page: page.json() } }, true);
}
});
@@ -172,30 +170,29 @@ var MessageProcessor = {
if (data.page == undefined) { client.send({ error: 'Data needs a pages attribute.'}); return; }
Page.find_by_id_and_project_id(data.page.id, client.user.project_id, function(page) {
if (page.error) return;
- CanvasObject.find(data.canvas_object.id, page, function(canvas_object) {
- if (canvas_object.error) return;
- canvas_object.delete(function(deleted_canvas_object) {
- if (canvas_object.error) {
- client.send({ error: canvas_object.error });
- } else {
- var msg = { message: client.user.handle() + ' deleted element: ' + canvas_object.name() + '.'};
- client.user.broadcast_to_project(msg, true);
- client.user.broadcast_to_project({ canvas_object_delete: { canvas_object: canvas_object.json() } }, true);
- }
+ for (var i in data.canvas_objects) {
+ CanvasObject.find(data.canvas_objects[i].id, page, function(canvas_object) {
+ if (canvas_object.error) return;
+ canvas_object.delete(function(deleted_canvas_object) {
+ if (!canvas_object.error) {
+ client.user.broadcast_to_project({
+ message: client.user.handle() + ' deleted element: ' + canvas_object.name() + '.',
+ canvas_object_delete: { canvas_object: canvas_object.json() }
+ }, true);
+ }
+ });
});
- });
+ }
});
},
message: function(client, data) {
- var msg = { message: client.user.handle() + data };
- client.user.broadcast_to_project(msg, true);
+ client.user.broadcast_to_project({ message: client.user.handle() + data }, true);
},
};
// Our string escaper, which just trims and strips tags.
-// TODO test
String.prototype.esc = function() {
return this.trim().replace(/<[^>]*>?/g, '');
};
View
@@ -1,28 +1,26 @@
$(window).load(function() {
- // Key bindings used so that users may delete mockup objects (with the delete key).
+ // Key bindings
+ // Referenced http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
$(document).keydown(function(e) {
last_key_pressed = e.keyCode || "";
if ($(document.activeElement).is('input, textarea') || e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return true;
- //Key bindings from: http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
+
switch (e.keyCode) {
case 8: // backspace key
case 46: // delete key
+ var canvas_objects = [];
$('#canvas .ui-selected').each(function() {
- env.socket.send({
- canvas_object_delete: {
- canvas_object: { id: $(this).attr('canvas_object_id') },
- page: { id: env.project.current_page }
- }
- });
+ canvas_objects.push({ id: $(this).attr('canvas_object_id') });
});
+ env.socket.send({ canvas_object_delete: { canvas_objects : canvas_objects, page : { id: env.project.current_page } } });
break;
- //look at last_key_pressed
- case 67://c implement copy mockup object
- break;
- case 86://v implement paste mockup object
- break;
+ // look at last_key_pressed
+ case 67:// c implement copy mockup object, may need to add ctrl as well
+ break;
+ case 86:// v implement paste mockup object
+ break;
default:
return;
}
@@ -110,10 +110,6 @@ MessageProcessor = {
connected: function() {
},
- announcement: function(data) {
- new Growl(data);
- },
-
message: function(data) {
new Growl(data);
},
@@ -366,7 +366,7 @@ exports.process = testCase({
Page.create(project, function(page) {
CanvasObject.create(page, { canvas_object: {} }, function(canvas_object) {
// delete canvas object
- MessageProcessor.process(that.client, { canvas_object_delete: { page: { id: 1 }, canvas_object: { id: 0 } } });
+ MessageProcessor.process(that.client, { canvas_object_delete: { page: { id: 1 }, canvas_objects: [{ id: 0 }] } });
setTimeout(function() {
test.equal(that.client.sent.error, undefined);

0 comments on commit 62a510d

Please sign in to comment.