Added support for other file types than images.

* Use mime/types library for MIME type management.
 * Use CodeRay for syntax highlighting code/text files.
 * Added code to observe the drags; fixed committing position changes to the app.
 * Commenced work on the Z-index problem.
This commit is contained in:
Paul van Tilburg 2010-09-10 17:46:24 +02:00
parent 56216b3bea
commit 37138619c0
3 changed files with 64 additions and 35 deletions

View File

@ -1,6 +1,8 @@
require "camping" require "camping"
require "coderay"
require "fileutils" require "fileutils"
require "markaby" require "markaby"
require "mime/types"
require "pathname" require "pathname"
Camping.goes :Plemp Camping.goes :Plemp
@ -14,7 +16,6 @@ unless defined? BASE_DIR
Positions = Hash.new Positions = Hash.new
end end
module Plemp::Controllers module Plemp::Controllers
class Index class Index
@ -24,28 +25,24 @@ module Plemp::Controllers
end end
class StaticXX class StaticXX
MIME_TYPES = {'.css' => 'text/css',
'.js' => 'text/javascript',
'.jpeg' => 'image/jpeg',
'.jpg' => 'image/jpeg',
'.png' => 'image/png'}
def get(type, path) def get(type, path)
@headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain" mime_type = MIME::Types.type_for(path).first
@headers['Content-Type'] = mime_type.nil? ? "text/plain" : mime_type.to_s
unless path.include? ".." or not ["public", "upload"].include? type unless path.include? ".." or not ["public", "upload"].include? type
@headers['X-Sendfile'] = (BASE_DIR + type + path).to_s @headers['X-Sendfile'] = (BASE_DIR + type + path).to_s
else else
@status = "403" @status = "403"
"403 - Invalid path" "Error 403: Invalid path: #{path}"
end end
end end
end end
class SaveposXXX class SaveposXXX
def post(id, top, left) def post(id, top, left)
$stderr.puts("Got: id: #{id} -> top: #{top}, left: #{left}") [top, left].each { |pos| pos.gsub!(/px$/, '') }
$stderr.puts("Got: id: #{id} -> top: #{top}, left: #{left}") if $DEBUG
Positions[id] = [top, left] Positions[id] = [top, left]
$stderr.puts Positions.inspect $stderr.puts Positions.inspect if $DEBUG
"" ""
end end
end end
@ -83,6 +80,7 @@ module Plemp::Views
end end
def main def main
h1 "Plemp!"
form :action => R(Upload), :method => "post", form :action => R(Upload), :method => "post",
:enctype => "multipart/form-data" do :enctype => "multipart/form-data" do
p do p do
@ -90,24 +88,42 @@ module Plemp::Views
input :type => "submit", :value => "Upload!" input :type => "submit", :value => "Upload!"
end end
end end
UPLOAD_DIR.entries.select { |f| (UPLOAD_DIR + f).file? }.each do |f| div.draggables! do
next if f.to_s =~ /^\./ UPLOAD_DIR.entries.select { |f| (UPLOAD_DIR + f).file? }.each do |f|
file = UPLOAD_DIR + f next if f.to_s =~ /^\./
id = file.basename.to_s file = UPLOAD_DIR + f
if Positions.has_key? id id = file.basename.to_s
top, left = Positions[id] if Positions.has_key? id
else top, left = Positions[id]
top, left = ["#{rand(300)}", "#{rand(700)}"] else
Positions[id] = [top, left] top, left = ["#{rand(300)}", " #{80 + rand(700)}"]
end Positions[id] = [top, left]
img.draggable :id => id, end
:src => R(StaticXX, "upload", file.basename), file_type = `file --brief --mime-type #{file}`.chomp
:alt => file.basename mime_type = MIME::Types[file_type].first
script :type => "text/javascript" do if mime_type.nil?
"new Draggable('#{id}', { scroll: window });\n" + mime_type = MIME::Type.new(file_type)
"new Effect.Move('#{id}', { x: #{left}, y: #{top}, mode: 'absolute' });" end
default_style = "left:#{left}px;top:#{top}px;z-index:0;display:none"
case mime_type.media_type
when "image"
img.draggable :id => id, :style => default_style,
:src => R(StaticXX, "upload", file.basename),
:alt => file.basename
when "text"
div.draggable(:id => id, :style => default_style) { CodeRay.scan_file(file).div }
else
span.draggable(:id => id, :style => default_style) { em "#{id}: Unsupported file type!" }
end
script :type => "text/javascript" do
<<-JS
new Draggable('#{id}', { scroll: window });
Draggables.addObserver(new DragRegObserver($('#{id}')));
$('#{id}').appear({duration: 2.0});
JS
end
end end
end end
span(:onClick => "alert(maxZIndex()); return false;" ) { "test" }
end end
end end

View File

@ -1,7 +1,18 @@
var drgs = new Draggables; var DragRegObserver = Class.create();
DragRegObserver.prototype = {
function dragreg(div, event) { initialize: function(element) {
id = div.element.id; this.element = $(element);
new Ajax.Request('http://localhost:3301/savepos/' + id + },
'/' + this.style.top + '/' + this.style.left); onStart: function() {},
onEnd: function (eventName, draggable, event) {
if (Draggables.activeDraggable.element == this.element) {
elem = draggable.element;
new Ajax.Request('http://localhost:3301/savepos/' + elem.id +
'/' + elem.style.top + '/' + elem.style.left);
}
}
}
function maxZIndex() {
return $$('.draggable').map(function(d) { return d.getStyle("z-index") }).max();
} }

View File

@ -3,7 +3,9 @@
.draggable { .draggable {
position: absolute; position: absolute;
padding: 15px; padding: 15px;
width: 300px; width: 400px;
max-height: 300px;
overflow: auto;
top: 0px; top: 0px;
left: 0px; left: 0px;
border: thin solid black; border: thin solid black;