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) {
|
|
|
|
console.log(data);
|
|
|
|
this.draggable({ stack: ".draggable",
|
|
|
|
containment: "window",
|
|
|
|
stop: update_drag_info })
|
|
|
|
.hover(function() { $(this).find(".comments").fadeIn(); },
|
|
|
|
function() { $(this).find(".comments").fadeOut(); });
|
|
|
|
this.find(".delete").click({ id: this.id,
|
|
|
|
element: this },
|
|
|
|
delete_draggable);
|
|
|
|
this.find(".title").editable('draggables/' + this.id,
|
|
|
|
{ tooltip: "Click to edit…",
|
|
|
|
name: 'title',
|
|
|
|
submit: 'OK',
|
|
|
|
cancel: 'Discard',
|
|
|
|
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-01-16 10:22:22 +01:00
|
|
|
$.each(data, function(drag_id, drag_info) {
|
|
|
|
$.get("draggables/" + drag_id, function(data) {
|
2012-01-11 23:31:07 +01:00
|
|
|
$("#draggables").append(data);
|
2012-01-15 17:15:10 +01:00
|
|
|
// Assume we have appended one draggable here:
|
2012-01-16 10:22:22 +01:00
|
|
|
$(".draggable").last().plempable(drag_info);
|
|
|
|
});
|
|
|
|
});
|
2012-01-11 23:31:07 +01:00
|
|
|
}, "json");
|
2012-01-11 17:04:20 +01:00
|
|
|
});
|
|
|
|
|
2012-01-12 13:22:52 +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-01-16 10:22:22 +01:00
|
|
|
// Delete a draggable on the server; remove it with a visual effect.
|
2012-01-15 17:15:10 +01:00
|
|
|
function delete_draggable(event) {
|
|
|
|
drag_id = event.data.id
|
|
|
|
$.post("draggables/" + drag_id, {"_method": "delete"}, function(data) {
|
2012-01-15 23:51:01 +01:00
|
|
|
if (data) {
|
|
|
|
event.data.element.hide('fade', 'slow', function() {
|
|
|
|
event.data.element.remove () });
|
|
|
|
}
|
2012-01-15 17:15:10 +01:00
|
|
|
}, "json");
|
|
|
|
}
|
2012-01-11 23:31:07 +01:00
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|