Split the draggable div generating code from Views#draggables ; added the Draggable controller.

This commit is contained in:
Paul van Tilburg 2010-09-14 17:03:03 +02:00
parent 3bb1185267
commit 0dfe4b52f2
1 changed files with 72 additions and 49 deletions

View File

@ -106,6 +106,27 @@ module Plemp::Controllers
end end
end end
# = The draggable controller
#
# Controller that provides direct access to the HTML generating code
# for draggable objects.
#
# path:: /draggable/<id>
# view:: views#Draggable
class DraggableX
# Retrieves the draggable with the given _id_ from the database and
# returns the HTML.
def get(id)
@drag = Draggable.find_by_file(id)
if @drag.nil?
@status = "404"
return "Error 404: Unknown draggable ID: #{id}"
end
# Call draggable from the views, but do not wrap the layout.
mab(false) { draggable }
end
end
# = The position save controller # = The position save controller
# #
# Controller used through AJAX request by the main page for committing # Controller used through AJAX request by the main page for committing
@ -251,18 +272,22 @@ module Plemp::Views
end end
end end
# The canvas with draggables. Creates a draggable HTML element including # The canvas with draggables repsenting the objects in the database.
# the actually contents of the associated file per Draggable object in
# the database.
def draggables def draggables
div.draggables! do div.draggables! do
@draggables.each do |d| @draggables.each { |d| @drag = d; draggable }
file = UPLOAD_DIR + d.file end
end # def draggables
# Creates a draggable HTML (block) element including the actually
# contents of the associated file for Draggbale object _d_.
def draggable
file = UPLOAD_DIR + @drag.file
unless file.exist? unless file.exist?
# The associated file of this Draggable object is gone, remove # The associated file of this Draggable object is gone, remove
# the object from the database too. # the object from the database too.
d.destroy @drag.destroy
next return ""
end end
# Determine the MIME type. # Determine the MIME type.
file_type = `file --brief --mime-type #{file}`.chomp file_type = `file --brief --mime-type #{file}`.chomp
@ -271,38 +296,36 @@ module Plemp::Views
mime_type = MIME::Type.new(file_type) mime_type = MIME::Type.new(file_type)
end end
# Create an HTML element based on the type of the Draggable object. # Create an HTML element based on the type of the Draggable object.
default_style = "left:#{d.left}px;top:#{d.top}px;z-index:0;display:none" default_style = "left:#{@drag.left}px;top:#{@drag.top}px;z-index:0;display:none"
case mime_type.media_type case mime_type.media_type
when "image" when "image"
img.draggable :id => d.file, :style => default_style, img.draggable :id => @drag.file, :style => default_style,
:src => R(StaticXX, "upload", file.basename), :src => R(StaticXX, "upload", file.basename),
:alt => file.basename :alt => file.basename
when "video" when "video"
# HTML5 is not supported by Markaby! # HTML5 is not supported by Markaby!
self << \ self << \
" <video class=\"draggable\" id=\"#{d.file}\" " + " <video class=\"draggable\" id=\"#{@drag.file}\" " +
"style=\"#{default_style}\" " + "style=\"#{default_style}\" " +
"src=\"#{R(StaticXX, "upload", d.file)}\" " + "src=\"#{R(StaticXX, "upload", @drag.file)}\" " +
"controls=\"true\">" + "</video>\n" "controls=\"true\">" + "</video>\n"
when "audio" when "audio"
# HTML5 is not supported by Markaby! # HTML5 is not supported by Markaby!
self << self <<
" <audio class=\"draggable\" id=\"#{d.file}\" " + " <audio class=\"draggable\" id=\"#{@drag.file}\" " +
"style=\"#{default_style};height=80px;\" " + "style=\"#{default_style};height=80px;\" " +
"src=\"#{R(StaticXX, "upload", d.file)}\" " + "src=\"#{R(StaticXX, "upload", @drag.file)}\" " +
"controls=\"true\">" + "</audio>\n" "controls=\"true\">" + "</audio>\n"
when "text" when "text"
div.draggable :id => d.file, :style => default_style do div.draggable :id => @drag.file, :style => default_style do
CodeRay.scan_file(file).div(:tab_width => 2, CodeRay.scan_file(file).div(:tab_width => 2,
:line_numbers => :inline) :line_numbers => :inline)
end end
else else
span.draggable :id => d.file, :style => default_style do span.draggable :id => @drag.file, :style => default_style do
em "#{d.file}: Unsupported file type!" em "#{@drag.file}: Unsupported file type!"
end end
end # case end # case
end end # def draggable
end
end # def draggables
end # module Plemp::Views end # module Plemp::Views