Orbit/lib/parsers/parser_common.rb

350 lines
14 KiB
Ruby
Raw Permalink Normal View History

2011-12-23 10:34:21 +00:00
module ParserCommon
2013-05-24 10:42:01 +00:00
include ActionView::Helpers::TagHelper
# include ActionView::Helpers::UrlHelper
2012-05-07 05:33:29 +00:00
2012-05-08 20:15:02 +00:00
def menu_level(page, current_page, current, menu, edit = false)
2012-01-24 03:38:53 +00:00
res = ''
2012-05-11 08:16:09 +00:00
if page.visible_children.size > 0
res << "<ul class='"
res << menu.values["class_#{current}"] rescue nil
res << "'>"
i = nil
i = 1 if menu.values["li_incremental_#{current}"]
2012-12-28 03:37:37 +00:00
children = current == 1 ? page.visible_children : page.visible_children
2012-08-11 10:19:30 +00:00
children.each do |child|
2012-05-08 20:15:02 +00:00
res << menu_li(child, current_page, current, menu, i, edit)
i += 1 if i
end
2012-08-11 10:19:30 +00:00
if menu.values['home'] && current == 1
res << menu_li(page, current_page, current, menu, i, edit)
end
res << "</ul>"
2011-12-23 10:34:21 +00:00
end
res
2011-12-23 10:34:21 +00:00
end
2012-05-08 20:15:02 +00:00
def menu_li(page, current_page, current, menu, i, edit)
res = "<li class='"
res << menu.values["li_class_#{current}"] rescue nil
2011-12-23 10:34:21 +00:00
res << "_#{i}" if i
2012-05-11 08:16:09 +00:00
res << " active" if (current_page.id.eql?(page.id) || current_page.descendant_of?(page))
res << "'>"
root = ""
2012-07-30 10:34:54 +00:00
res << "<a href='#{edit ? root + admin_page_path(page.id) : (page.class.to_s.eql?('Page') ? root + page.path : page.url)}'><span>#{page.title}</span></a>"
2012-05-18 07:20:45 +00:00
if page.visible_children.size > 0 && current < menu.levels
res << "<span class='dot'></span>"
2012-05-08 20:15:02 +00:00
res << menu_level(page, current_page, current + 1, menu, edit)
2012-05-11 08:16:09 +00:00
end unless (page.root? rescue nil)
2011-12-23 10:34:21 +00:00
res << "</li>"
end
2012-03-27 09:57:50 +00:00
# ad_banners
def parse_ad_banners_edit(body = nil, page = nil, edit=nil)
2012-03-27 09:57:50 +00:00
body.css('ad_banner').each do |banner|
res = ''
ad_banner = AdBanner.find(banner["id"]) rescue nil
2012-04-02 02:54:48 +00:00
if ad_banner
2012-03-27 09:57:50 +00:00
res << "<script type='text/javascript'>
2012-08-13 07:10:53 +00:00
jQuery(function( $ ){
$.preload( '#slideshow-#{ad_banner.title.dehumanize} img', {
onFinish:finish,
threshold: 2 //'2' is the default, how many at a time, to load.
});
function finish(){
$('#slideshow-#{ad_banner.title.dehumanize}').cycle({
delay: -1000,
fx: '#{ad_banner.ad_fx.nil? ? 'fade': ad_banner.ad_fx}',
timeoutFn: getTimeout,
pager: '.banner_nav-#{ad_banner.title.dehumanize}',
2012-08-13 07:10:53 +00:00
pagerAnchorBuilder: function(idx, slide) {
return \"<li><a href='#'></a></li>\";
}
});
};
2012-05-08 09:21:26 +00:00
});
2012-03-27 09:57:50 +00:00
</script>"
res << "<div style='position:relative'>"
res << "<ul id='banner_nav' class='clear banner_nav-#{ad_banner.title.dehumanize}'></ul>"
res << "<div id='slideshow-#{ad_banner.title.dehumanize}' class='slideshow'>"
2012-04-01 15:39:49 +00:00
printable_ad_images = []
ad_banner.ad_images.each do |ad_image|
if ad_image.display?
ad_image.weight.times do
printable_ad_images << ad_image
end
2012-03-27 09:57:50 +00:00
end
2012-04-01 15:39:49 +00:00
end
printable_ad_images.shuffle!
2012-08-15 08:34:00 +00:00
hide = printable_ad_images.size > 1
2012-04-01 15:39:49 +00:00
printable_ad_images.each do |ad_image| #TODO Need Reflact
res << "<img src='#{ad_image.file}' "
2012-07-25 22:02:22 +00:00
res << "alt='#{ad_image.title || ' '}' "
2012-04-25 10:03:30 +00:00
res << "time_to_next='#{ad_banner.transition_msec}' "
2012-04-01 15:39:49 +00:00
res << "link_open='#{ad_image.link_open}' "
# res << "link_url='#{(ad_image.direct_to_after_click?? ad_image.out_link : ad_banner.context) || ' '}' "
2012-08-13 03:03:11 +00:00
res << "link_url='#{(ad_image.out_link)}' "
2012-08-15 08:34:00 +00:00
res << "style='#{'display:none;' if hide} #{'cursor:pointer;' if !ad_image.out_link.blank?}'"
2012-04-01 15:39:49 +00:00
res << "/>"
end
res << "</div>"
2012-03-27 09:57:50 +00:00
res << "</div>"
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
banner.swap(fragment)
end
end
# page_images
def parse_images_edit(body, page, edit=nil)
2012-03-27 09:57:50 +00:00
body.css('.page_image').each do |page_image|
# image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
# image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
image = page.design.images.detect{|image| image.name.eql?(File.basename(page_image['src'])) } unless image
2012-03-27 09:57:50 +00:00
if image
res = "<img src=#{image.file.url} "
page_image.attributes.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
res << '>'
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
page_image.swap(fragment)
end
end
# page_menu
def parse_menu_edit(body, page, edit=nil)
2012-03-27 09:57:50 +00:00
page_menu = body.css('.page_menu').first
home = get_homepage
menu = page.design.layout.menu
fragment = Nokogiri::HTML::DocumentFragment.new(body, menu_level(home, page, 1, menu, edit))
page_menu.swap(fragment)
2012-03-27 09:57:50 +00:00
end
2012-04-23 18:30:40 +00:00
# sub_menus
def parse_sub_menus_edit(body = nil, page = nil, edit=nil)
2012-04-23 18:30:40 +00:00
body.css('sub_menu').each do |sub_menu|
2012-05-08 20:31:04 +00:00
menu_page = Page.find(sub_menu['id']) rescue nil
2012-04-23 18:30:40 +00:00
res = ''
2012-05-11 08:16:09 +00:00
if menu_page && menu_page.visible_children.size > 0
2012-05-08 20:31:04 +00:00
res << "<div class='category_list'>"
2012-07-25 22:02:22 +00:00
res << "<h3 class='h3'>#{menu_page.title}</h3>"
2012-05-08 20:31:04 +00:00
res << "<ul class='list'>"
2012-05-11 08:16:09 +00:00
menu_page.visible_children.each do |child|
2012-05-08 20:31:04 +00:00
res << "<li class='#{page.id.eql?(child.id) ? 'active' : nil}'>"
root = ""
2012-07-30 10:34:54 +00:00
res << "<a href='#{edit ? root + admin_page_path(child.id) : (child.class.to_s.eql?('Page') ? root + child.path : child.url)}'>#{child.title}</a>"
2012-05-08 20:31:04 +00:00
res << "</li>"
end
res << "</ul>"
res << "</h3>"
res << "</div>"
2012-04-23 18:30:40 +00:00
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
sub_menu.swap(fragment)
end
end
2013-01-31 11:20:55 +00:00
# breadcrumb
def parse_breadcrumbs_edit(body = nil, page = nil, edit=nil)
body.css('breadcrumb').each do |breadcrumb|
ancestors = Page.find(page.id).ancestors_and_self rescue nil
2013-02-07 09:26:40 +00:00
ancestors = nil if ancestors.size == 1
2013-01-31 11:20:55 +00:00
res = ''
if ancestors
res << "<ul class='breadcrumb'>"
ancestors.each_with_index do |node, i|
last = i == ancestors.size-1
res << "<li class=#{'active' if last}>"
if last
res << node.title
else
res << "<a herf='/#{node.path}'>#{node.title}</a>"
res << "<span class='divider'>/</span>"
end
2013-01-31 11:20:55 +00:00
res << "</li>"
end
res << "</ul>"
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
breadcrumb.swap(fragment)
end
end
2013-05-24 10:42:01 +00:00
# tag_cloud
def parse_tag_clouds_edit(body = nil, page = nil, edit=nil)
body.css('tag_cloud').each do |tag_cloud|
module_app = ModuleApp.find(tag_cloud["id"]) rescue nil
res = ''
if module_app
res << "<div class='tag_cloud'>"
res << "<h3 class='h3'>#{t(:tag_cloud)}</h3>"
res << "<div class='cloud'>"
module_app.sorted_tags_for_cloud.each do |tag|
# res << link_to(tag[0].name, ('/' + {:tag_id => tag[0].id, :category_id => params[:category_id]}.to_param), :class => "#{tag[1]} #{tag[0].id.to_s.eql?(params[:tag_id]) ? 'active' : nil} ")
res << "<span class='#{tag[1]} #{tag[0].id.to_s.eql?(params[:tag_id]) ? 'active' : nil}'>"
res << tag[0].name
res << "</span>"
end
res << "</div>"
res << "</div>"
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, res)
tag_cloud.swap(fragment)
end
end
2012-05-08 09:27:02 +00:00
# page_footer
def parse_footer_edit(body, page)
2012-05-07 05:33:29 +00:00
page_footer = body.css('.page_footer').first
2012-05-08 09:27:02 +00:00
if page_footer
res = "<div id='#{page_footer['id']}', class='#{page_footer['class']}'>"
2012-07-25 22:02:22 +00:00
res << @site.footer rescue nil
2012-05-08 09:27:02 +00:00
res << "</div>"
fragment = Nokogiri::HTML::DocumentFragment.new(body ,res)
page_footer.swap(fragment) rescue nil
else
''
end
end
# page_sub_menu
def parse_sub_menu_edit(body, page)
2012-05-08 09:27:02 +00:00
page_sub_menu = body.css('.page_sub_menu').first
if page_sub_menu
res = "<div id='#{page_sub_menu['id']}', class='#{page_sub_menu['class']}'>"
2012-07-25 22:02:22 +00:00
res << @site.sub_menu rescue nil
2012-05-08 09:27:02 +00:00
res << "</div>"
fragment = Nokogiri::HTML::DocumentFragment.new(body ,res)
page_sub_menu.swap(fragment) rescue nil
else
''
end
2012-05-07 05:33:29 +00:00
end
2013-01-31 11:20:55 +00:00
def parse_breadcrumb_edit(body, page)
page_breadcrumb = body.css('.page_breadcrumb').first
if page_breadcrumb
res = "<div id='#{page_breadcrumb['id']}', class='#{page_breadcrumb['class']}'>"
res << @site.breadcrumb rescue nil
res << "</div>"
fragment = Nokogiri::HTML::DocumentFragment.new(body ,res)
page_breadcrumb.swap(fragment) rescue nil
else
''
end
end
2013-02-01 09:07:16 +00:00
2012-07-27 08:53:12 +00:00
# sitemap
def parse_sitemaps_edit(body = nil, page = nil, edit=nil)
2012-07-27 08:53:12 +00:00
sitemap = body.css('sitemap').first
2013-02-06 04:28:10 +00:00
url = front_show_sitemap_path
options = "?inner=true"
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div class='dymanic_load' path='#{url + options}'></div>")
sitemap.swap(fragment)
2012-07-27 08:53:12 +00:00
end
# terms_of_use
def parse_terms_of_uses_edit(body = nil, page = nil, edit=nil)
terms_of_use = body.css('terms_of_use').first
url = front_show_terms_of_use_path
options = "?inner=true"
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div class='dymanic_load' path='#{url + options}'></div>")
terms_of_use.swap(fragment)
end
2012-05-13 11:32:05 +00:00
# page_contents
def parse_contents_edit(body, page, edit=nil)
2012-05-13 11:32:05 +00:00
public_r_tags = []
2013-01-21 08:56:37 +00:00
url = ''
2012-05-13 11:32:05 +00:00
body.css('.page_content').each do |content|
ret = ''
2012-05-13 16:33:52 +00:00
category = params[:category_id].blank? ? page[:category] : params[:category_id]
tag = params[:tag_id].blank? ? page[:tag] : params[:tag_id]
2012-05-13 11:32:05 +00:00
if (content["main"] == "true" && !page.module_app.nil?)
2013-01-21 08:56:37 +00:00
if page.app_frontend_url == 'default_widget'
url = "/panel/orbit_app/widget/#{page.frontend_style}?inner=true"
else
url = "/panel/#{page.module_app.key}/front_end/#{page.app_frontend_url}"
end
ret << "<div id='appfrontend' class='dymanic_load' path='#{url}"
ret << "/#{params[:id]}" if params[:id] && !params[:id].eql?(page.id.to_s)
2013-03-05 10:34:41 +00:00
categories_str=category.collect{|t| "category_id[]=#{t}"}.join('&')
tags_str=tag.collect{|t| "tag_id[]=#{t}"}.join('&')
categories_str = "&#{categories_str}" unless categories_str.blank?
tags_str = "&#{tags_str}" unless tags_str.blank?
2013-04-03 11:03:16 +00:00
ret << "?inner=true&page_id=#{page.id}#{categories_str}#{tags_str}&preview=#{params[:preview]}&page_main=#{params[:page_main]}&search_query=#{params[:search_query]}&clicked_field_name=#{params[:clicked_field_name]}&name=#{params[:name]}&item_type=page"
2013-01-21 08:56:37 +00:00
ret << "'></div>"
2012-05-13 11:32:05 +00:00
else
part = page.page_parts.detect{ |p| p.name.to_s == content['name'].to_s } rescue nil
2012-07-25 22:02:22 +00:00
part_title = part.title rescue nil
2012-05-13 11:32:05 +00:00
if edit
ret << "<div id='#{content['name']}' part_id='#{part.id}' class='editable'>" if part
ret << "<div class='edit_link'>"
2012-05-13 11:32:05 +00:00
ret << " <a href='#{edit_admin_page_part_path(part.id)}' class='nav'>#{t(:edit)}</a>" if part
2012-05-13 12:13:26 +00:00
ret << '</div>'
2012-05-13 11:32:05 +00:00
end
case part.kind
when 'text'
2012-07-25 22:02:22 +00:00
ret << part.content rescue ''
2012-05-13 11:32:05 +00:00
when 'module_widget'
2012-12-27 08:20:40 +00:00
url = case part.widget_path
when 'default_widget'
"/panel/orbit_app/widget/#{part.widget_style}?inner=true"
else
# raise ModuleAppError,"PagePart can't find ModuleApp" if part.module_app.nil?
"/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true" rescue ''
2012-12-27 08:20:40 +00:00
end
2013-03-05 10:34:41 +00:00
categories_str=(!part[:category].blank? ? part[:category] : category).collect{|t| "category_id[]=#{t}"}.join('&')
tags_str=(!part[:tag].blank? ? part[:tag] : tag).collect{|t| "tag_id[]=#{t}"}.join('&')
categories_str = "&#{categories_str}" unless categories_str.blank?
tags_str = "&#{tags_str}" unless tags_str.blank?
2013-04-03 11:03:16 +00:00
options = "&part_id=#{part.id}#{categories_str}#{tags_str}&page=#{params[:page]}&search_query=#{params[:search_query]}&clicked_field_name=#{params[:clicked_field_name]}&part_title=#{Rack::Utils.escape(part_title).gsub("+", "%20") rescue nil}&item_type=page_part"
2012-08-11 10:19:30 +00:00
ret << "<div class='dymanic_load widget' path='#{url + options}'></div>"
2012-05-13 11:32:05 +00:00
when 'public_r_tag'
ret << "<r:#{part.public_r_tag} id='#{part.public_r_tag_object_id}'/>"
public_r_tags << part.public_r_tag
else
''
end if part
end
scope = "<#{content.name}"
content.attributes.each_pair do |key, value|
scope << " #{key}='#{value}'"
end
scope << ">#{ret}</#{content.name}>"
fragment = Nokogiri::HTML::DocumentFragment.new(body, scope)
content.swap(fragment)
end
public_r_tags.uniq
end
# page_counter
2012-12-28 02:36:18 +00:00
def parse_counter_edit(body = nil)
body.css('.page_counter').each do |counter|
res = ''
case counter['option']
when 'all'
res << display_visitors.to_s
when 'today'
res << display_visitors_today.to_s
when 'this_week'
res << display_visitors_this_week.to_s
when 'this_month'
res << display_visitors_this_month.to_s
when 'this_year'
res << display_visitors_this_year.to_s
end
fragment = Nokogiri::HTML::DocumentFragment.new(body, "<div id='#{counter['id']}' class='#{counter['class']}'>#{res}</div>")
counter.swap(fragment)
end
end
2011-12-23 10:34:21 +00:00
end