2010-09-11 18:59:10 +02:00
|
|
|
// Main function to initialise plemp
|
|
|
|
function init_plemp(base_url) {
|
|
|
|
setup_pu();
|
|
|
|
document.onkeyup=key_handler;
|
|
|
|
}
|
|
|
|
|
2010-09-11 13:24:46 +02:00
|
|
|
// Creates a Draggable for each div with the given ID.
|
2010-09-12 12:38:15 +02:00
|
|
|
function setup_draggables() {
|
|
|
|
$$('.draggable').each(function(d) { setup_draggable(d.id); });
|
|
|
|
}
|
|
|
|
|
2010-09-11 13:24:46 +02:00
|
|
|
function setup_draggable(id) {
|
|
|
|
drag = new Draggable(id, { scroll: window });
|
|
|
|
old_endeffect = drag.options.endeffect;
|
|
|
|
drag.options.endeffect = function(element) {
|
|
|
|
old_endeffect(element);
|
|
|
|
element.style.zIndex = new_ZIndex();
|
|
|
|
}
|
2010-09-12 00:32:40 +02:00
|
|
|
drag_obs = new DragRegObserver($(id));
|
|
|
|
Draggables.addObserver(drag_obs);
|
|
|
|
$(id).dirty = false;
|
|
|
|
$(id).appear();
|
2010-09-11 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Observer for draggables that commits the drag changes to the server.
|
2010-09-10 17:46:24 +02:00
|
|
|
var DragRegObserver = Class.create();
|
|
|
|
DragRegObserver.prototype = {
|
|
|
|
initialize: function(element) {
|
|
|
|
this.element = $(element);
|
|
|
|
},
|
2010-09-12 00:32:40 +02:00
|
|
|
onStart: function(eventName, draggable, event) {
|
|
|
|
draggable.element.dirty = true;
|
|
|
|
},
|
2010-09-10 17:46:24 +02:00
|
|
|
onEnd: function (eventName, draggable, event) {
|
|
|
|
if (Draggables.activeDraggable.element == this.element) {
|
|
|
|
elem = draggable.element;
|
2010-09-11 18:59:10 +02:00
|
|
|
new Ajax.Request(document.baseURI + 'savepos/' + elem.id +
|
2010-09-12 00:32:40 +02:00
|
|
|
'/' + elem.style.top + '/' + elem.style.left, {
|
|
|
|
onSuccess: function(response) { elem.dirty = false; }
|
|
|
|
});
|
2010-09-10 17:46:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-19 00:51:58 +02:00
|
|
|
|
2010-09-11 18:59:10 +02:00
|
|
|
// Global function to handle key presses.
|
|
|
|
function key_handler(e) {
|
|
|
|
var key_code = e.keyCode;
|
|
|
|
|
|
|
|
switch(key_code) {
|
2010-09-12 00:32:40 +02:00
|
|
|
case 27: /* Escape */
|
2010-09-11 18:59:10 +02:00
|
|
|
hide_add_dialog();
|
|
|
|
break;
|
2010-09-12 12:36:20 +02:00
|
|
|
case 187: /* Plus */
|
|
|
|
show_add_dialog();
|
|
|
|
break;
|
2010-09-11 18:59:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 13:24:46 +02:00
|
|
|
//// Helper functions
|
|
|
|
|
2010-09-11 18:59:10 +02:00
|
|
|
// Shows the upload/add dialog.
|
2010-09-11 13:24:46 +02:00
|
|
|
function show_add_dialog() {
|
2010-09-12 12:36:20 +02:00
|
|
|
if (!$('add_dialog').visible()) {
|
|
|
|
$('text').clear();
|
|
|
|
$('add_dialog').appear({duration: 0.25});
|
|
|
|
}
|
2010-09-10 18:34:16 +02:00
|
|
|
}
|
|
|
|
|
2010-09-11 18:59:10 +02:00
|
|
|
// Hides and clears the upload/add dialog.
|
2010-09-11 13:24:46 +02:00
|
|
|
function hide_add_dialog() {
|
2010-09-12 12:36:20 +02:00
|
|
|
if ($('add_dialog').visible()) {
|
|
|
|
$('add_dialog').fade({duration: 0.25});
|
|
|
|
$('add_form').reset();
|
|
|
|
}
|
2010-09-11 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a Z-index higher with respect to the Z-index of all existing
|
|
|
|
// draggables.
|
|
|
|
function new_ZIndex() {
|
|
|
|
cur_indices = $$('.draggable').map(function(d) { return d.getStyle("z-index") })
|
|
|
|
max_index = cur_indices.max();
|
|
|
|
return parseInt(max_index) + 1;
|
2010-06-19 00:51:58 +02:00
|
|
|
}
|
2010-09-11 00:13:10 +02:00
|
|
|
|
2010-09-12 00:32:40 +02:00
|
|
|
// Moves a draggable with the given ID to a position unless:
|
|
|
|
// it is currently being dragged, or it has been dragged but not this is
|
|
|
|
// not registered at the server side yet, or it is already on the right spot.
|
2010-09-11 00:13:10 +02:00
|
|
|
function move_draggable_if_needed(id, left, top) {
|
2010-09-12 00:32:40 +02:00
|
|
|
var actDrag = Draggables.activeDraggable;
|
|
|
|
if ($(id).dirty || (actDrag && actDrag.element == $(id))) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-11 00:13:10 +02:00
|
|
|
if ($(id).style.left != left || $(id).style.top != top) {
|
2010-09-12 00:32:40 +02:00
|
|
|
new Effect.Move(id, { x: left, y: top, mode: 'absolute' });
|
2010-09-11 00:13:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-11 13:24:46 +02:00
|
|
|
// Sets up an Ajax periodical updater to retrieve the current global
|
|
|
|
// state from the server. Moves the draggables accordingly.
|
2010-09-11 00:13:10 +02:00
|
|
|
var pu = null;
|
|
|
|
function setup_pu() {
|
|
|
|
if (!pu) {
|
2010-09-11 18:59:10 +02:00
|
|
|
pu = new Ajax.PeriodicalUpdater('', document.baseURI + "current/", {
|
2010-09-11 00:13:10 +02:00
|
|
|
method: 'get',
|
|
|
|
frequency: 5,
|
|
|
|
evalJSON: 'force',
|
|
|
|
onSuccess: function(t) {
|
|
|
|
var json = t.responseJSON;
|
|
|
|
if (json) {
|
|
|
|
$H(json).each(function(pair) {
|
|
|
|
move_draggable_if_needed(pair.key, pair.value[0],
|
|
|
|
pair.value[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFailure: function() { alert("Something went wrong!") }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|