2012-01-11 17:04:20 +01:00
|
|
|
$(document).ready(function() {
|
2012-01-16 10:22:22 +01:00
|
|
|
// Handlers for showing/hiding the upload dialog via clicks & keys.
|
2012-01-11 17:04:20 +01:00
|
|
|
$("#add").click(show_add_dialog);
|
|
|
|
$("#add_form #cancel").click(hide_add_dialog);
|
2012-01-12 13:22:52 +01:00
|
|
|
if ($.browser.mozilla) {
|
|
|
|
$(document).keypress(key_handler);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(document).keyup(key_handler);
|
|
|
|
}
|
2012-01-11 23:31:07 +01:00
|
|
|
|
2012-01-16 10:22:22 +01:00
|
|
|
// The plempable jQuery plugin containing most of the Plemp! UI
|
|
|
|
// functionality.
|
|
|
|
$.fn.plempable = function(data) {
|
|
|
|
this.draggable({ stack: ".draggable",
|
|
|
|
containment: "window",
|
2012-01-17 14:27:37 +01:00
|
|
|
cancel: "audio, video",
|
|
|
|
distance: 10,
|
2012-01-16 10:22:22 +01:00
|
|
|
stop: update_drag_info })
|
2012-01-17 14:27:52 +01:00
|
|
|
.click(raise_draggable)
|
2012-01-16 10:22:22 +01:00
|
|
|
.hover(function() { $(this).find(".comments").fadeIn(); },
|
|
|
|
function() { $(this).find(".comments").fadeOut(); });
|
2012-01-16 20:27:09 +01:00
|
|
|
this.find(".delete").click({ id: this.attr("id"),
|
2012-01-16 10:22:22 +01:00
|
|
|
element: this },
|
|
|
|
delete_draggable);
|
2012-01-16 20:27:09 +01:00
|
|
|
this.find(".title").editable('draggables/' + this.attr("id"),
|
2012-01-16 10:22:22 +01:00
|
|
|
{ tooltip: "Click to edit…",
|
|
|
|
name: 'title',
|
|
|
|
submit: 'OK',
|
2012-01-17 14:27:37 +01:00
|
|
|
cancel: 'Cancel',
|
2012-01-16 10:22:22 +01:00
|
|
|
style: 'inherit' });
|
|
|
|
// FIXME: actually get the comments!
|
|
|
|
this.find(".comments").text("Comments (0)").hide();
|
|
|
|
// Highlight contained code.
|
|
|
|
this.find("pre code").each(function(idx, elem) {
|
|
|
|
hljs.highlightBlock(elem, ' ');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-01-12 13:22:52 +01:00
|
|
|
// Populate the canvas with the draggables.
|
2012-01-12 11:38:40 +01:00
|
|
|
$.get("draggables", function(data) {
|
2012-02-09 10:28:09 +01:00
|
|
|
var timestamp = data.timestamp;
|
|
|
|
|
|
|
|
// Retrieve each draggable from the list and add it to the canvas.
|
|
|
|
$.each(data.list, function(drag_id, drag_info) {
|
|
|
|
add_draggable_to_canvas(drag_id, drag_info);
|
2012-01-16 10:22:22 +01:00
|
|
|
});
|
2012-02-09 10:31:03 +01:00
|
|
|
|
|
|
|
// Start a (long-poll) loop for update events from the server.
|
|
|
|
poll_server(timestamp);
|
2012-01-11 23:31:07 +01:00
|
|
|
}, "json");
|
2012-01-11 17:04:20 +01:00
|
|
|
});
|
|
|
|
|
2012-02-09 10:28:09 +01:00
|
|
|
// Callback functions
|
2012-01-16 10:22:22 +01:00
|
|
|
|
|
|
|
// Show the add dialog with a visual effect.
|
2012-01-11 17:04:20 +01:00
|
|
|
function show_add_dialog() {
|
2012-01-11 23:26:40 +01:00
|
|
|
$('#add_dialog').fadeIn('slow');
|
2012-01-15 17:15:10 +01:00
|
|
|
}
|
2012-01-11 17:04:20 +01:00
|
|
|
|
2012-01-16 10:22:22 +01:00
|
|
|
// Hide the add dialog with a visual effect and reset the form when finished.
|
2012-01-11 17:04:20 +01:00
|
|
|
function hide_add_dialog() {
|
2012-01-11 23:26:40 +01:00
|
|
|
$("#add_dialog").fadeOut('fast', function() {
|
2012-01-11 17:04:20 +01:00
|
|
|
$("#add_form")[0].reset();
|
|
|
|
});
|
2012-01-15 17:15:10 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 10:28:09 +01:00
|
|
|
// Add a draggable element to the canvas.
|
|
|
|
function add_draggable_to_canvas(drag_id, drag_info) {
|
|
|
|
$.get("draggables/" + drag_id, function(data) {
|
|
|
|
$("#draggables").append(data);
|
|
|
|
// Assume we have appended a signle draggable here:
|
|
|
|
$(".draggable").last().plempable(drag_info);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a draggable element from the canvas.
|
|
|
|
function delete_draggable_from_canvas(drag_id) {
|
|
|
|
$("#draggables #" + drag_id).hide('fade', 'slow', function() {
|
|
|
|
element.remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a draggable on the server.
|
2012-01-15 17:15:10 +01:00
|
|
|
function delete_draggable(event) {
|
2012-01-16 23:45:15 +01:00
|
|
|
drag_id = event.data.id;
|
2012-01-15 17:15:10 +01:00
|
|
|
$.post("draggables/" + drag_id, {"_method": "delete"}, function(data) {
|
2012-01-15 23:51:01 +01:00
|
|
|
if (data) {
|
2012-02-09 10:28:09 +01:00
|
|
|
delete_draggable_from_canvas(drag_id);
|
2012-01-15 23:51:01 +01:00
|
|
|
}
|
2012-01-15 17:15:10 +01:00
|
|
|
}, "json");
|
|
|
|
}
|
2012-01-11 23:31:07 +01:00
|
|
|
|
2012-01-17 14:27:52 +01:00
|
|
|
// Raise the draggable to the foreground.
|
|
|
|
function raise_draggable(event) {
|
|
|
|
var max = 0;
|
|
|
|
$(".draggable").each(function() {
|
|
|
|
var cur = parseFloat($(this).css("z-index"));
|
|
|
|
max = cur > max ? cur : max;
|
|
|
|
});
|
|
|
|
$(this).css("z-index", max + 1);
|
|
|
|
}
|
|
|
|
|
2012-01-16 10:22:22 +01:00
|
|
|
// Update the position of a draggable on the server.
|
2012-01-11 23:31:07 +01:00
|
|
|
function update_drag_info(event, ui) {
|
2012-01-13 10:43:47 +01:00
|
|
|
$.post("draggables/" + ui.helper.context.id, ui.position, "json");
|
2012-01-11 23:31:07 +01:00
|
|
|
}
|
2012-01-12 13:22:52 +01:00
|
|
|
|
2012-02-09 10:31:03 +01:00
|
|
|
// Poll for server events via AJAX.
|
|
|
|
function poll_server(timestamp) {
|
|
|
|
$.ajax({ url: "events?timestamp=" + timestamp,
|
|
|
|
success: handle_server_event,
|
|
|
|
error: function() { poll_server(timestamp) }, // FIXME; handle properly!
|
|
|
|
dataType: "json", timeout: 60000 });
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle server events {
|
|
|
|
function handle_server_event(event) {
|
|
|
|
switch(event.type) {
|
|
|
|
case "reposition":
|
|
|
|
$("#draggables #" + event.data.id).animate({ top: event.data.top,
|
|
|
|
left: event.data.left });
|
|
|
|
break;
|
|
|
|
case "title update":
|
|
|
|
$("#draggables #" + event.data.id + " h2 .title").text(event.data.title);
|
|
|
|
break;
|
|
|
|
case "add":
|
|
|
|
add_draggable_to_canvas(event.data.id, event.data.info);
|
|
|
|
break;
|
|
|
|
case "delete":
|
|
|
|
delete_draggable_from_canvas(event.data.id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Unsupported event type!");
|
|
|
|
console.log(event);
|
|
|
|
}
|
|
|
|
poll_server(event.timestamp);
|
|
|
|
}
|
|
|
|
|
2012-01-16 10:22:22 +01:00
|
|
|
// Handle the Escape and Plus keys for hiding/showing the add dialog.
|
2012-01-12 13:22:52 +01:00
|
|
|
function key_handler(event) {
|
|
|
|
switch (event.keyCode) {
|
|
|
|
case 27: /* Escape */
|
|
|
|
hide_add_dialog();
|
|
|
|
break;
|
|
|
|
case 187: /* Plus (numpad) */
|
|
|
|
show_add_dialog();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|