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.
This commit is contained in:
Paul van Tilburg 2010-09-11 18:59:10 +02:00
parent 3be69752f2
commit f633696e7e
2 changed files with 27 additions and 8 deletions

View File

@ -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

View File

@ -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',