Implemented distributed moving of draggables via Ajax.

This commit is contained in:
Paul van Tilburg 2010-09-11 00:13:10 +02:00
parent d78ec2e6c8
commit 3b43695538
2 changed files with 42 additions and 2 deletions

View File

@ -2,6 +2,7 @@ require "active_record"
require "camping"
require "coderay"
require "fileutils"
require "json"
require "markaby"
require "mime/types"
require "pathname"
@ -53,7 +54,16 @@ module Plemp::Controllers
end
end
class StaticXX
class Current
def get
out = Hash.new
Draggable.all.each { |d| out[d.file] = [d.left, d.top] }
$stderr.puts("Current status requested: #{out.to_json}")
out.to_json
end
end
class StaticXX
def get(type, path)
mime_type = MIME::Types.type_for(path).first
@headers['Content-Type'] = mime_type.nil? ? "text/plain" : mime_type.to_s
@ -107,7 +117,7 @@ module Plemp::Views
:type => "text/javascript"
end
script :type => "text/javascript" do
"BaseUrl = \"#{URL()}\";"
"BaseUrl = \"#{URL()}\";\nsetup_pu()";
end
end
body do

View File

@ -28,3 +28,33 @@ function setup_draggable(id) {
Draggables.addObserver(new DragRegObserver($(id)));
$(id).appear({duration: 2.0});
}
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' });
}
}
}
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!") }
});
}
}