Implemented distributed moving of draggables via Ajax.
This commit is contained in:
parent
d78ec2e6c8
commit
3b43695538
14
plemp.rb
14
plemp.rb
|
@ -2,6 +2,7 @@ require "active_record"
|
||||||
require "camping"
|
require "camping"
|
||||||
require "coderay"
|
require "coderay"
|
||||||
require "fileutils"
|
require "fileutils"
|
||||||
|
require "json"
|
||||||
require "markaby"
|
require "markaby"
|
||||||
require "mime/types"
|
require "mime/types"
|
||||||
require "pathname"
|
require "pathname"
|
||||||
|
@ -53,7 +54,16 @@ module Plemp::Controllers
|
||||||
end
|
end
|
||||||
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)
|
def get(type, path)
|
||||||
mime_type = MIME::Types.type_for(path).first
|
mime_type = MIME::Types.type_for(path).first
|
||||||
@headers['Content-Type'] = mime_type.nil? ? "text/plain" : mime_type.to_s
|
@headers['Content-Type'] = mime_type.nil? ? "text/plain" : mime_type.to_s
|
||||||
|
@ -107,7 +117,7 @@ module Plemp::Views
|
||||||
:type => "text/javascript"
|
:type => "text/javascript"
|
||||||
end
|
end
|
||||||
script :type => "text/javascript" do
|
script :type => "text/javascript" do
|
||||||
"BaseUrl = \"#{URL()}\";"
|
"BaseUrl = \"#{URL()}\";\nsetup_pu()";
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
body do
|
body do
|
||||||
|
|
|
@ -28,3 +28,33 @@ function setup_draggable(id) {
|
||||||
Draggables.addObserver(new DragRegObserver($(id)));
|
Draggables.addObserver(new DragRegObserver($(id)));
|
||||||
$(id).appear({duration: 2.0});
|
$(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!") }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in New Issue