From 3228a30279b1c9d6f0d48717b9ac15b6879dc624 Mon Sep 17 00:00:00 2001 From: chiu Date: Sun, 19 Jan 2020 00:31:39 +0800 Subject: [PATCH] let user can select whether to show previous layer and whether to expand the current menu --- .../site_menu_widgets_controller.rb | 150 ++++++++++++------ config/locales/en.yml | 6 + config/locales/zh_tw.yml | 6 + lib/site_menu_widget/engine.rb | 25 ++- site_menu_widget.gemspec | 13 ++ .../_site_menu_widget1.html.erb | 20 +++ .../_site_menu_widget2.html.erb | 20 +++ widgets/site_menu_widget/info.json | 20 +++ widgets/site_menu_widget/thumbs/thumb.png | Bin 0 -> 4075 bytes 9 files changed, 212 insertions(+), 48 deletions(-) create mode 100644 config/locales/en.yml create mode 100644 config/locales/zh_tw.yml create mode 100644 widgets/site_menu_widget/_site_menu_widget1.html.erb create mode 100644 widgets/site_menu_widget/_site_menu_widget2.html.erb create mode 100644 widgets/site_menu_widget/info.json create mode 100644 widgets/site_menu_widget/thumbs/thumb.png diff --git a/app/controllers/site_menu_widgets_controller.rb b/app/controllers/site_menu_widgets_controller.rb index 7d0bf3c..2f0c385 100644 --- a/app/controllers/site_menu_widgets_controller.rb +++ b/app/controllers/site_menu_widgets_controller.rb @@ -1,59 +1,119 @@ class SiteMenuWidgetsController < ApplicationController - + def create_json(pages) + items = [] + pages.each do |page| + item = {} + item["obj"] = page + if page.child_page.size > 0 + item["page_name"] = page.name + item["children"] = create_json(page.sorted_published_child_pages) + if page.page_type == "page" or page.page_type == "link" + item["url"] = (page.page_type == "link") ? "/#{locale.to_s}" + page.external_url : "/#{locale.to_s}" + page.url + item["target"] = "_self" + else + item["url"] = "/#{locale.to_s}" + page.url + item["target"] = "_blank" + end + else + item["page_name"] = page.name + item["children"] = [] + if page.page_type == "page" + item["url"] = "/#{locale.to_s}" + page.url + item["target"] = "_self" + else + item["url"] = page.external_url + item["target"] = (is_internal_url?(page.external_url) ? "_self" : "_blank") rescue "_blank" + end + end + items << item + end + items + end + def create_menu_bool_object (items) + #item_all = [] + items.each do |item1| + #item = [] + @count += 1 + if (!item1["children"].nil? && item1["children"].length!=0) + @li_index << @count + if children_is_current(item1['obj']) + @ul_index << ( @li_index.length - 1 ) + end + create_menu_bool_object(item1['children']) + #item = [children_is_current(item1['obj']),item1['obj'].id==@page_id,create_menu_bool_object(item1['children'])] + else + #item = [children_is_current(item1['obj']),item1['obj'].id==@page_id] + end + #item_all << item + end + #item_all + end + def children_is_current (page) + page.sorted_published_child_pages.select{|v| v.id==@page_id}.length!=0 rescue false + end def widget + subpart = OrbitHelper.get_current_widget params = OrbitHelper.params page = Page.where(:page_id => params[:page_id]).first + @page_id = page.id + @expand_layer = false if page.parent_page == Page.root - pages = page.sorted_published_child_pages + @pages = page.sorted_published_child_pages else - pages = page.parent_page.sorted_published_child_pages - end - - page.page_parts.collect do |pp| - pp.sub_parts.collect do |sp| - @title = sp.title if sp.module == "site_menu_widget" - end - end - - def create_json(pages) - items = [] - pages.each do |page| - item = {} - if page.child_page.size > 0 - if page.page_type == "page" or page.page_type == "link" - item["page_name"] = page.name - item["url"] = (page.page_type == "link") ? "/#{locale.to_s}" + page.external_url : "/#{locale.to_s}" + page.url - item["children"] = create_json(page.sorted_published_child_pages) - item["target"] = "_self" - else - item["page_name"] = page.name - item["url"] = "/#{locale.to_s}" + page.url - item["children"] = create_json(page.sorted_published_child_pages) - item["target"] = "_blank" - end - else - if page.page_type == "page" - item["page_name"] = page.name - item["url"] = "/#{locale.to_s}" + page.url - item["target"] = "_self" - item["children"] = [] - else - item["page_name"] = page.name - item["url"] = page.external_url - item["target"] = (is_internal_url?(page.external_url) ? "_self" : "_blank") rescue "_blank" - item["children"] = [] - end + @pages = page.parent_page.sorted_published_child_pages + if subpart.methods.include? 'select_options'.to_sym + OrbitWidget.all.select{|tmp| tmp.key.to_s=='site_menu_widget'}.each do |orbit_widget| + @show_options = orbit_widget.show_options rescue nil + end + subpart.select_options.each do |select_option| + puts ['show',@show_options] + if !(@show_options.nil?) && select_option.field_name == @show_options.keys.first.to_s + value = YAML.load(select_option.value) + if value[I18n.locale] == t('site_menu.yes') + if page.parent_page.parent_page == Page.root + @pages = Array(page.parent_page) + else + @pages = page.parent_page.parent_page.sorted_published_child_pages + end + end + elsif !(@show_options.nil?) && select_option.field_name == @show_options.keys[1].to_s + value = YAML.load(select_option.value) + if value[I18n.locale] == t('site_menu.yes') + @expand_layer = true + end + end end - items << item end - items end - - - items = create_json(pages) + @ul_index=[] + @li_index= [] + @count = -1 + title = subpart.title + items = create_json(@pages) + create_menu_bool_object(items) + if @expand_layer + temp = @ul_index.reverse.collect do |ul_index| + tp = @li_index[ul_index] + 1 + "var ul_tp = $('div[data-subpart-id=#{subpart.id}]').find('ul').eq(#{tp}) + ul_tp.removeClass('dropdown-menu') + $('div[data-subpart-id=#{subpart.id}]').find('li').eq(#{@li_index[ul_index]}).find('span').eq(0).remove() + " + end + if temp.length != 0 + temp = temp.join(' ') + expand_script = "" + else + expand_script = '' + end + else + expand_script="" + end { "pages" => items, - "extras" => {"widget_title" => @title} + "extras" => {"widget_title" => title.to_s,"expand-script"=>expand_script} } end diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000..819b5d6 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,6 @@ +en: + site_menu: + show_previous_layer: Show Previous Layer + expand_current_layer: Expand Current Layer + 'yes': 'Yes' + 'no': 'No' diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml new file mode 100644 index 0000000..2f0268d --- /dev/null +++ b/config/locales/zh_tw.yml @@ -0,0 +1,6 @@ +zh_tw: + site_menu: + show_previous_layer: 顯示上一層 + expand_current_layer: 展開所在的那層 + 'yes': 是 + 'no': 否 \ No newline at end of file diff --git a/lib/site_menu_widget/engine.rb b/lib/site_menu_widget/engine.rb index 40c975a..3c85075 100644 --- a/lib/site_menu_widget/engine.rb +++ b/lib/site_menu_widget/engine.rb @@ -1,9 +1,28 @@ module SiteMenuWidget class Engine < ::Rails::Engine - initializer "site_menu_widget" do - OrbitApp.registration "SiteMenuWidget", :type => "OrbitWidget" do - widget_label "SiteMenu" + initializer "site_menu_widget", before: :load_config_initializers do + translate_data = Dir["#{SiteMenuWidget::Engine.root}/config/locales/*.yml"] .map{|yaml_file| YAML.load(File.read(yaml_file))} + data = {} + key1 = {} + key2 = {} + value1 = {} + value2 = {} + translate_data.each do |t_data| + key1[t_data.keys[0]] = t_data.values[0]['site_menu']['show_previous_layer'] + key2[t_data.keys[0]] = t_data.values[0]['site_menu']['expand_current_layer'] + value2[t_data.keys[0]] = t_data.values[0]['site_menu']['no'] + value1[t_data.keys[0]] = t_data.values[0]['site_menu']['yes'] + end + data[key1] = [value2,value1] + data[key2] = [value2,value1] + OrbitApp.registration "SiteMenuWidget", :type => "OrbitWidget" do + widget_label "SiteMenu" widget_methods ["widget"] + begin + show_options data + rescue => e + puts ['there_was_no_show_option_method',e] + end end end end diff --git a/site_menu_widget.gemspec b/site_menu_widget.gemspec index ede0f53..36150c3 100644 --- a/site_menu_widget.gemspec +++ b/site_menu_widget.gemspec @@ -2,6 +2,19 @@ $:.push File.expand_path("../lib", __FILE__) # Maintain your gem's version: require "site_menu_widget/version" +app_path = File.expand_path(__dir__) +template_path = ENV['PWD'] + '/app/templates' +all_template = Dir.glob(template_path+'/*/') +puts 'copying module' +all_template.each do |folder| + if folder.split('/')[-1] != 'mobile' + begin + system ('cp -r '+ app_path + '/widgets/ ' + folder) + rescue + puts 'error copy' + end + end +end # Describe your gem and declare its dependencies: Gem::Specification.new do |s| diff --git a/widgets/site_menu_widget/_site_menu_widget1.html.erb b/widgets/site_menu_widget/_site_menu_widget1.html.erb new file mode 100644 index 0000000..2e865b8 --- /dev/null +++ b/widgets/site_menu_widget/_site_menu_widget1.html.erb @@ -0,0 +1,20 @@ + \ No newline at end of file diff --git a/widgets/site_menu_widget/_site_menu_widget2.html.erb b/widgets/site_menu_widget/_site_menu_widget2.html.erb new file mode 100644 index 0000000..2f24a78 --- /dev/null +++ b/widgets/site_menu_widget/_site_menu_widget2.html.erb @@ -0,0 +1,20 @@ + \ No newline at end of file diff --git a/widgets/site_menu_widget/info.json b/widgets/site_menu_widget/info.json new file mode 100644 index 0000000..8c206ef --- /dev/null +++ b/widgets/site_menu_widget/info.json @@ -0,0 +1,20 @@ +{ + "widgets" : [ + { + "filename" : "site_menu_widget1", + "name" : { + "zh_tw" : "1. 橫式 ( 模組標題, 網站第二層選單, 網站第三層選單 )", + "en" : "1. Horizontal (widget-title, second level dropdown, third level dropdown)" + }, + "thumbnail" : "thumb.png" + }, + { + "filename" : "site_menu_widget2", + "name" : { + "zh_tw" : "1. 直式 ( 模組標題, 網站第二層選單, 網站第三層選單 )", + "en" : "1. Vertical (widget-title, second level dropdown, third level dropdown)" + }, + "thumbnail" : "thumb.png" + } + ] +} \ No newline at end of file diff --git a/widgets/site_menu_widget/thumbs/thumb.png b/widgets/site_menu_widget/thumbs/thumb.png new file mode 100644 index 0000000000000000000000000000000000000000..266af5606742714c262a949cd58aedc2b84b1959 GIT binary patch literal 4075 zcmcgvYdBPE`=8Q@or=MZk}-~jIXD}qFebzhVvsZ#jA2HMW=0OFFbKQkR5@2;7{)1- z&=wM+oMYofvU8{uIS&7&UHkuj*w_2v{qU~qTI;!=weH`2-_Lz|o)v3@JtZh0Apin_ z1kKIRw!l~h+!g!w0MC|jUMDb!Q%!MHJBk;Th7Tlwj65ms1cm?>ZoCb zunI6C_z=xP0tt2@SbL8Ul825b)X)H;M@Iny{0US%gzoQ04nooOq2J}A0DAj1918i) zLM7=#|1t_^Wdkvx1QH+`FocQ+LK6wmREHte5jyHR$`Dlq5&=i5!Vy|3NCZkl3xz;J zeq2yMTcD>G$`+0Jp$nMlLw%^!02CZfqtReAH5et(8;;b`(SajW;i{@C0EbEtgG|NK zRmedyKP8|EK^}p`04k9}hHOj3yHkRx`cS~qzn0)1U}g2UU~4vB3<+^0l07N3pzZMdwOTYGka&sUi3z0mL%#bLMf?Z- z>ZS-JMhk;Ms+p<-HXx1FOpGw9+S;btnx?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