From f633696e7ef8ef225a452dd435e0c84b35550496 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Sat, 11 Sep 2010 18:59:10 +0200 Subject: [PATCH] Some Javascript refactoring. * Moved all initialisation stuff to init_plemp(). * No longer set and user BaseUrl but use document.baseURI instead. * Added the function key_handler() for handling key presses. * Added functionality to dismiss the add/upload dialog when pressing Escape. --- plemp.rb | 2 +- public/dragreg.js | 33 ++++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/plemp.rb b/plemp.rb index 6ea328f..1deac51 100644 --- a/plemp.rb +++ b/plemp.rb @@ -126,7 +126,7 @@ module Plemp::Views :type => "text/javascript" end script :type => "text/javascript" do - "BaseUrl = \"#{URL()}\";\nsetup_pu()"; + "init_plemp();" end end body do diff --git a/public/dragreg.js b/public/dragreg.js index f9edecc..83fc88b 100644 --- a/public/dragreg.js +++ b/public/dragreg.js @@ -1,3 +1,9 @@ +// 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 }); @@ -20,21 +26,34 @@ DragRegObserver.prototype = { onEnd: function (eventName, draggable, event) { if (Draggables.activeDraggable.element == this.element) { elem = draggable.element; - new Ajax.Request(BaseUrl + 'savepos/' + elem.id + + new Ajax.Request(document.baseURI + 'savepos/' + elem.id + '/' + elem.style.top + '/' + elem.style.left); } } } -//// Helper functions +// Global function to handle key presses. +function key_handler(e) { + var key_code = e.keyCode; -function show_add_dialog() { - $('text').clear(); - $('add_dialog').appear({duration: 0.5}); + 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.5}); + $('add_dialog').fade({duration: 0.25}); $('add_form').reset(); } @@ -61,7 +80,7 @@ function move_draggable_if_needed(id, left, top) { var pu = null; function setup_pu() { if (!pu) { - pu = new Ajax.PeriodicalUpdater('', BaseUrl + "current/", { + pu = new Ajax.PeriodicalUpdater('', document.baseURI + "current/", { method: 'get', frequency: 5, evalJSON: 'force',