// Main function to initialise plemp function init_plemp(base_url) { setup_pu(); document.onkeyup=key_handler; } // 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(document.baseURI + 'savepos/' + elem.id + '/' + elem.style.top + '/' + elem.style.left); } } } // Global function to handle key presses. function key_handler(e) { var key_code = e.keyCode; switch(key_code) { case 27: hide_add_dialog(); break; } } //// Helper functions // Shows the upload/add dialog. function show_add_dialog() { $('text').clear(); $('add_dialog').appear({duration: 0.25}); } // Hides and clears the upload/add dialog. function hide_add_dialog() { $('add_dialog').fade({duration: 0.25}); $('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('', document.baseURI + "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!") } }); } }