Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## webpages_helper.rb
- module WebpagesHelper
- def display_html_contents(contents, options = {})
- contents.map do |c|
- ContentRenderer.new(c, options[:mode], self).render_content
- end
- end
- end
- ## content_renderer.rb
- class ContentRenderer
- def initialize(content, mode, template)
- @mode = mode
- @template = template
- @content = content
- end
- def render_content
- id = get_id
- style = @content.element.style
- title = get_title
- double_click = get_double_click
- image_source = get_image_source
- content_tag get_html_tag, get_content_data, :src => image_source, :id => id, :style => style, :title => title, :ondblclick => double_click
- end
- def get_content_data
- sortable = make_sortable
- sortable_image + make_dropzone + parse_data + render_nested_contents.to_s + sortable
- end
- def render_nested_contents
- @content.children.map do |c|
- @content = c
- render_content
- end
- end
- def parse_data
- unless @content.data.blank?
- substitute @content.data, { :link => nil, :bold => :strong, :italic => :em }
- else
- if ( @mode == :display || containers.include?(@content.element.markup) || @content.photo_id )
- ""
- else
- get_container_name
- end
- end
- end
- def reg_exp
- { :link => /\[link(?:=(.*?))?\](.*?)\[\/link\]/,
- :bold => /\[b\](.*?)\[\/b\]/,
- :italic => /\[i\](.*?)\[\/i\]/ }
- end
- def substitute(content, options = {})
- options.map { |k, v| k == :link ? link_substitute(content) : content.gsub!(reg_exp[k]) { content_tag v, $1 } }
- content
- end
- def link_substitute(content)
- if @mode == :display
- content.gsub!(reg_exp[:link]) { $1.nil? ? link_to(nil, $2) : link_to($2, $1) }
- else
- content.gsub!(reg_exp[:link]) { content_tag :span, $2, :class => 'fake_link' }
- end
- end
- def get_html_tag
- @mode == :sort ? :div : @content.element.markup
- end
- def get_id
- if @mode == :display
- nil
- else
- "content_#{@content.id}"
- end
- end
- def get_title
- if @mode != :display && @content.parent
- "inside #{@content.parent.element.name}"
- end
- end
- def get_double_click(override = false)
- remote_function(:url => content_path(@content.id), :method => :get) if @mode == :build && (!containers.include?(@content.element.markup) || override)
- end
- def get_image_source
- formatted_photo_path(Photo.find(@content.photo_id), :png) unless @content.photo_id.nil?
- end
- def get_element
- if @mode == :sort
- # scriptaculous requires all sortable elements in a container to be the same type
- :div
- else
- @content.element.markup
- end
- end
- def get_container_name
- "#{@content.element.name}" + (content_tag :span, "(id: #{@content.id})", :class => "builder_id_label")
- end
- def make_sortable
- if @mode == :sort && @content.children.length > 1
- sortable_element("content_#{@content.id}", :url => sort_contents_path(:id => @content.id), :tag => 'div')
- else
- ""
- end
- end
- def sortable_image
- if @mode == :sort && @content.photo_id
- image_tag formatted_thumb_photo_path(Photo.find(@content.photo_id), :png, :resize => "150x150")
- else
- ""
- end
- end
- def make_dropzone
- if @mode == :build && containers.include?(@content.element.markup)
- the_tag = @content.element.markup == "ul" ? :li : :div
- (content_tag the_tag, "Drag elements here for #{get_container_name}", :id => "dropzone_#{@content.id}", :class => "drop_zone", :ondblclick => get_double_click(true)) +
- drop_receiving_element("dropzone_#{@content.id}", :url => contents_path, :with => "'drop_id=#{@content.id}' + '&id=#{@content.webpage_id}' + '&drag_id=' + encodeURIComponent(element.id) + '&type=' + encodeURIComponent(element.tagName)" )
- else
- ""
- end
- end
- def containers
- ["ul","div"]
- end
- def method_missing(*args, &block)
- @template.send(*args, &block)
- end
- end
Add Comment
Please, Sign In to add comment