Guest User

Untitled

a guest
May 27th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. ## webpages_helper.rb
  2. module WebpagesHelper
  3.  
  4. def display_html_contents(contents, options = {})
  5. contents.map do |c|
  6. ContentRenderer.new(c, options[:mode], self).render_content
  7. end
  8. end
  9.  
  10. end
  11.  
  12. ## content_renderer.rb
  13. class ContentRenderer
  14. def initialize(content, mode, template)
  15. @mode = mode
  16. @template = template
  17. @content = content
  18. end
  19.  
  20. def render_content
  21. id = get_id
  22. style = @content.element.style
  23. title = get_title
  24. double_click = get_double_click
  25. image_source = get_image_source
  26.  
  27. content_tag get_html_tag, get_content_data, :src => image_source, :id => id, :style => style, :title => title, :ondblclick => double_click
  28. end
  29.  
  30. def get_content_data
  31. sortable = make_sortable
  32. sortable_image + make_dropzone + parse_data + render_nested_contents.to_s + sortable
  33. end
  34.  
  35. def render_nested_contents
  36. @content.children.map do |c|
  37. @content = c
  38. render_content
  39. end
  40. end
  41.  
  42. def parse_data
  43. unless @content.data.blank?
  44. substitute @content.data, { :link => nil, :bold => :strong, :italic => :em }
  45. else
  46. if ( @mode == :display || containers.include?(@content.element.markup) || @content.photo_id )
  47. ""
  48. else
  49. get_container_name
  50. end
  51. end
  52. end
  53.  
  54. def reg_exp
  55. { :link => /\[link(?:=(.*?))?\](.*?)\[\/link\]/,
  56. :bold => /\[b\](.*?)\[\/b\]/,
  57. :italic => /\[i\](.*?)\[\/i\]/ }
  58. end
  59.  
  60. def substitute(content, options = {})
  61. options.map { |k, v| k == :link ? link_substitute(content) : content.gsub!(reg_exp[k]) { content_tag v, $1 } }
  62. content
  63. end
  64.  
  65. def link_substitute(content)
  66. if @mode == :display
  67. content.gsub!(reg_exp[:link]) { $1.nil? ? link_to(nil, $2) : link_to($2, $1) }
  68. else
  69. content.gsub!(reg_exp[:link]) { content_tag :span, $2, :class => 'fake_link' }
  70. end
  71. end
  72.  
  73. def get_html_tag
  74. @mode == :sort ? :div : @content.element.markup
  75. end
  76.  
  77. def get_id
  78. if @mode == :display
  79. nil
  80. else
  81. "content_#{@content.id}"
  82. end
  83. end
  84.  
  85. def get_title
  86. if @mode != :display && @content.parent
  87. "inside #{@content.parent.element.name}"
  88. end
  89. end
  90.  
  91. def get_double_click(override = false)
  92. remote_function(:url => content_path(@content.id), :method => :get) if @mode == :build && (!containers.include?(@content.element.markup) || override)
  93. end
  94.  
  95. def get_image_source
  96. formatted_photo_path(Photo.find(@content.photo_id), :png) unless @content.photo_id.nil?
  97. end
  98.  
  99. def get_element
  100. if @mode == :sort
  101. # scriptaculous requires all sortable elements in a container to be the same type
  102. :div
  103. else
  104. @content.element.markup
  105. end
  106. end
  107.  
  108. def get_container_name
  109. "#{@content.element.name}" + (content_tag :span, "(id: #{@content.id})", :class => "builder_id_label")
  110. end
  111.  
  112. def make_sortable
  113. if @mode == :sort && @content.children.length > 1
  114. sortable_element("content_#{@content.id}", :url => sort_contents_path(:id => @content.id), :tag => 'div')
  115. else
  116. ""
  117. end
  118. end
  119.  
  120. def sortable_image
  121. if @mode == :sort && @content.photo_id
  122. image_tag formatted_thumb_photo_path(Photo.find(@content.photo_id), :png, :resize => "150x150")
  123. else
  124. ""
  125. end
  126. end
  127.  
  128. def make_dropzone
  129. if @mode == :build && containers.include?(@content.element.markup)
  130. the_tag = @content.element.markup == "ul" ? :li : :div
  131. (content_tag the_tag, "Drag elements here for #{get_container_name}", :id => "dropzone_#{@content.id}", :class => "drop_zone", :ondblclick => get_double_click(true)) +
  132. 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)" )
  133. else
  134. ""
  135. end
  136. end
  137.  
  138. def containers
  139. ["ul","div"]
  140. end
  141.  
  142. def method_missing(*args, &block)
  143. @template.send(*args, &block)
  144. end
  145. end
Add Comment
Please, Sign In to add comment