From 3b43695538c54a00e94d9c83572ebba497fea906 Mon Sep 17 00:00:00 2001
From: Paul van Tilburg
Date: Sat, 11 Sep 2010 00:13:10 +0200
Subject: [PATCH] Implemented distributed moving of draggables via Ajax.
---
plemp.rb | 14 ++++++++++++--
public/dragreg.js | 30 ++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 2 deletions(-)
diff --git a/plemp.rb b/plemp.rb
index 63f8401..2b81647 100644
--- a/plemp.rb
+++ b/plemp.rb
@@ -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
diff --git a/public/dragreg.js b/public/dragreg.js
index d237ae7..4c97b01 100644
--- a/public/dragreg.js
+++ b/public/dragreg.js
@@ -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!") }
+ });
+ }
+}