This repository has been archived on 2020-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
plemp/public/dragreg.js

81 lines
2.4 KiB
JavaScript

// Creates a Draggable for each div with the given ID.
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();
}
Draggables.addObserver(new DragRegObserver($(id)));
$(id).appear({duration: 1.0});
}
// Observer for draggables that commits the drag changes to the server.
var DragRegObserver = Class.create();
DragRegObserver.prototype = {
initialize: function(element) {
this.element = $(element);
},
onStart: function() {},
onEnd: function (eventName, draggable, event) {
if (Draggables.activeDraggable.element == this.element) {
elem = draggable.element;
new Ajax.Request(BaseUrl + 'savepos/' + elem.id +
'/' + elem.style.top + '/' + elem.style.left);
}
}
}
//// Helper functions
function show_add_dialog() {
$('text').clear();
$('add_dialog').appear({duration: 0.5});
}
function hide_add_dialog() {
$('add_dialog').fade({duration: 0.5});
$('add_form').reset();
}
// 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;
}
// Moves a draggable with the given ID to a position if it is not already there.
function move_draggable_if_needed(id, left, top) {
if ($(id).style.left != left || $(id).style.top != top) {
var actDrag = Draggables.activeDraggable;
if (!actDrag || actDrag.element != $(id)) {
new Effect.Move(id, { x: left, y: top, mode: 'absolute' });
}
}
}
// Sets up an Ajax periodical updater to retrieve the current global
// state from the server. Moves the draggables accordingly.
var pu = null;
function setup_pu() {
if (!pu) {
pu = new Ajax.PeriodicalUpdater('', BaseUrl + "current/", {
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!") }
});
}
}