From 0dfe4b52f2cb6ea93c4d0291d75bbdc52618372a Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Tue, 14 Sep 2010 17:03:03 +0200 Subject: [PATCH] Split the draggable div generating code from Views#draggables ; added the Draggable controller. --- plemp.rb | 121 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 49 deletions(-) diff --git a/plemp.rb b/plemp.rb index 9613ffc..50aca2a 100644 --- a/plemp.rb +++ b/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/ + # 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 << \ - " \n" - when "audio" - # HTML5 is not supported by Markaby! - self << - " \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 << \ + " \n" + when "audio" + # HTML5 is not supported by Markaby! + self << + " \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