Split the draggable div generating code from Views#draggables ; added the Draggable controller.
This commit is contained in:
parent
3bb1185267
commit
0dfe4b52f2
1 changed files with 72 additions and 49 deletions
121
plemp.rb
121
plemp.rb
|
@ -106,6 +106,27 @@ module Plemp::Controllers
|
|||
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
|
||||
#
|
||||
# Controller used through AJAX request by the main page for committing
|
||||
|
@ -251,58 +272,60 @@ module Plemp::Views
|
|||
end
|
||||
end
|
||||
|
||||
# The canvas with draggables. Creates a draggable HTML element including
|
||||
# the actually contents of the associated file per Draggable object in
|
||||
# the database.
|
||||
# The canvas with draggables repsenting the objects in the database.
|
||||
def draggables
|
||||
div.draggables! do
|
||||
@draggables.each do |d|
|
||||
file = UPLOAD_DIR + d.file
|
||||
unless file.exist?
|
||||
# The associated file of this Draggable object is gone, remove
|
||||
# the object from the database too.
|
||||
d.destroy
|
||||
next
|
||||
end
|
||||
# Determine the MIME type.
|
||||
file_type = `file --brief --mime-type #{file}`.chomp
|
||||
mime_type = MIME::Types[file_type].first
|
||||
if mime_type.nil?
|
||||
mime_type = MIME::Type.new(file_type)
|
||||
end
|
||||
# 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"
|
||||
case mime_type.media_type
|
||||
when "image"
|
||||
img.draggable :id => d.file, :style => default_style,
|
||||
:src => R(StaticXX, "upload", file.basename),
|
||||
:alt => file.basename
|
||||
when "video"
|
||||
# HTML5 is not supported by Markaby!
|
||||
self << \
|
||||
" <video class=\"draggable\" id=\"#{d.file}\" " +
|
||||
"style=\"#{default_style}\" " +
|
||||
"src=\"#{R(StaticXX, "upload", d.file)}\" " +
|
||||
"controls=\"true\">" + "</video>\n"
|
||||
when "audio"
|
||||
# HTML5 is not supported by Markaby!
|
||||
self <<
|
||||
" <audio class=\"draggable\" id=\"#{d.file}\" " +
|
||||
"style=\"#{default_style};height=80px;\" " +
|
||||
"src=\"#{R(StaticXX, "upload", d.file)}\" " +
|
||||
"controls=\"true\">" + "</audio>\n"
|
||||
when "text"
|
||||
div.draggable :id => d.file, :style => default_style do
|
||||
CodeRay.scan_file(file).div(:tab_width => 2,
|
||||
:line_numbers => :inline)
|
||||
end
|
||||
else
|
||||
span.draggable :id => d.file, :style => default_style do
|
||||
em "#{d.file}: Unsupported file type!"
|
||||
end
|
||||
end # case
|
||||
end
|
||||
@draggables.each { |d| @drag = d; draggable }
|
||||
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?
|
||||
# The associated file of this Draggable object is gone, remove
|
||||
# the object from the database too.
|
||||
@drag.destroy
|
||||
return ""
|
||||
end
|
||||
# Determine the MIME type.
|
||||
file_type = `file --brief --mime-type #{file}`.chomp
|
||||
mime_type = MIME::Types[file_type].first
|
||||
if mime_type.nil?
|
||||
mime_type = MIME::Type.new(file_type)
|
||||
end
|
||||
# Create an HTML element based on the type of the Draggable object.
|
||||
default_style = "left:#{@drag.left}px;top:#{@drag.top}px;z-index:0;display:none"
|
||||
case mime_type.media_type
|
||||
when "image"
|
||||
img.draggable :id => @drag.file, :style => default_style,
|
||||
:src => R(StaticXX, "upload", file.basename),
|
||||
:alt => file.basename
|
||||
when "video"
|
||||
# HTML5 is not supported by Markaby!
|
||||
self << \
|
||||
" <video class=\"draggable\" id=\"#{@drag.file}\" " +
|
||||
"style=\"#{default_style}\" " +
|
||||
"src=\"#{R(StaticXX, "upload", @drag.file)}\" " +
|
||||
"controls=\"true\">" + "</video>\n"
|
||||
when "audio"
|
||||
# HTML5 is not supported by Markaby!
|
||||
self <<
|
||||
" <audio class=\"draggable\" id=\"#{@drag.file}\" " +
|
||||
"style=\"#{default_style};height=80px;\" " +
|
||||
"src=\"#{R(StaticXX, "upload", @drag.file)}\" " +
|
||||
"controls=\"true\">" + "</audio>\n"
|
||||
when "text"
|
||||
div.draggable :id => @drag.file, :style => default_style do
|
||||
CodeRay.scan_file(file).div(:tab_width => 2,
|
||||
:line_numbers => :inline)
|
||||
end
|
||||
else
|
||||
span.draggable :id => @drag.file, :style => default_style do
|
||||
em "#{@drag.file}: Unsupported file type!"
|
||||
end
|
||||
end # case
|
||||
end # def draggable
|
||||
|
||||
end # module Plemp::Views
|
||||
|
|
Reference in a new issue