From 70d52cb9241d9c12367ab3568a6d019101341578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sat, 10 Apr 2021 17:12:40 +0800 Subject: [PATCH] add youtube --- .../cycle2/jquery.cycle2.video.min.js | 2 + app/controllers/ad_banners_controller.rb | 54 ++++++++++++ app/controllers/admin/ad_images_controller.rb | 3 + app/models/ad_image.rb | 27 +++++- app/views/admin/ad_banners/_index.html.erb | 43 +++++++--- .../admin/ad_banners/_index_table.html.erb | 2 +- app/views/admin/ad_banners/show.html.erb | 11 ++- app/views/admin/ad_images/_form.html.erb | 84 +++++++++++++------ app/views/admin/ad_images/edit.html.erb | 2 +- app/views/admin/ad_images/new.html.erb | 2 +- config/locales/en.yml | 6 ++ config/locales/zh_tw.yml | 6 ++ 12 files changed, 200 insertions(+), 42 deletions(-) create mode 100644 app/assets/javascripts/cycle2/jquery.cycle2.video.min.js diff --git a/app/assets/javascripts/cycle2/jquery.cycle2.video.min.js b/app/assets/javascripts/cycle2/jquery.cycle2.video.min.js new file mode 100644 index 0000000..c47c840 --- /dev/null +++ b/app/assets/javascripts/cycle2/jquery.cycle2.video.min.js @@ -0,0 +1,2 @@ +/* Plugin for Cycle2; Copyright (c) 2012 M. Alsup; v20141007 */ +!function(a){"use strict";function b(){try{this.playVideo()}catch(a){}}function c(){try{this.pauseVideo()}catch(a){}}var d='
';a.extend(a.fn.cycle.defaults,{youtubeAllowFullScreen:!0,youtubeAutostart:!1,youtubeAutostop:!0}),a(document).on("cycle-bootstrap",function(e,f){f.youtube&&(f.hideNonActive=!1,f.container.find(f.slides).each(function(b){if(void 0!==a(this).attr("href")){var c,e=a(this),g=e.attr("href"),h=f.youtubeAllowFullScreen?"true":"false";g+=(/\?/.test(g)?"&":"?")+"enablejsapi=1",f.youtubeAutostart&&f.startingSlide===b&&(g+="&autoplay=1"),c=f.API.tmpl(d,{url:g,allowFullScreen:h}),e.replaceWith(c)}}),f.slides=f.slides.replace(/(\b>?a\b)/,"div.cycle-youtube"),f.youtubeAutostart&&f.container.on("cycle-initialized cycle-after",function(c,d){var e="cycle-initialized"==c.type?d.currSlide:d.nextSlide;a(d.slides[e]).find("object,embed").each(b)}),f.youtubeAutostop&&f.container.on("cycle-before",function(b,d){a(d.slides[d.currSlide]).find("object,embed").each(c)}))})}(jQuery); \ No newline at end of file diff --git a/app/controllers/ad_banners_controller.rb b/app/controllers/ad_banners_controller.rb index 5c032e6..1e34b6e 100644 --- a/app/controllers/ad_banners_controller.rb +++ b/app/controllers/ad_banners_controller.rb @@ -1,6 +1,17 @@ class AdBannersController < ApplicationController def widget adbanner = Banner.find(OrbitHelper.widget_custom_value) + widget = OrbitHelper.get_current_widget + if widget.widget_type == "ad_banner_widget2_video" + return video_widget(adbanner) + else + return image_widget(adbanner) + end + end + + private + + def image_widget(adbanner) images = [] adbanner.ad_images.can_display.asc(:sort_number,:created_at).each.with_index do |b,i| if b.language_enabled.include?(I18n.locale.to_s) @@ -47,4 +58,47 @@ class AdBannersController < ApplicationController "images" => images } end + + def video_widget(adbanner) + images = [] + adbanner.ad_images.can_display.asc(:sort_number).each_with_index do |ad_b,i| + if ad_b.language_enabled.include?(I18n.locale.to_s) + image_link = OrbitHelper.is_mobile_view ? ad_b.file.mobile.url : ad_b.file.url + alt_text = (ad_b.title.nil? || ad_b.title == "" ? "ad-banner image" : ad_b.title) + caption = i == 0 ? '
' : "" + klass = i == 0 ? "active" : "" + if ad_b.exchange_item == "1" + image_html = "
#{ad_b.title}
" + else ad_b.exchange_item == "2" + youtube_url = format_url(ad_b.youtube,i) + image_html = "
#{ad_b.title}
#{ad_b.title}
" + end + images << { + "html" => image_html + } + end + end + { + "extras" => { + "ad_fx" => adbanner.ad_fx, + "speed" => adbanner.speed, + "title" => adbanner.title, + "timeout" => adbanner.timeout, + "more" => "More" + }, + "images" => images + } + end + + def format_url(url,index) + uri = URI.parse(url) + ps = uri.query.to_s.split("&") + url_params = {} + ps.each do |p| + x = p.split("=") + url_params[x.first] = x.last + end + # url = "http://www.youtube.com/v/#{url_params["v"]}/?version=3&playerapiid=ytplayer#{index}" + url = "https://www.youtube.com/embed/#{url_params["v"]}" + end end diff --git a/app/controllers/admin/ad_images_controller.rb b/app/controllers/admin/ad_images_controller.rb index b590eb4..70aad8d 100644 --- a/app/controllers/admin/ad_images_controller.rb +++ b/app/controllers/admin/ad_images_controller.rb @@ -11,6 +11,8 @@ class Admin::AdImagesController < Admin::AdBannersController def edit @ad_image = AdImage.find(params[:id]) + @item = [[t('image'),"1"],[t('video'),"2"]] + @item_choose = @ad_image.exchange_item if can_edit_or_delete?(@ad_image.banner) @ad_banner = @ad_image.banner else @@ -27,6 +29,7 @@ class Admin::AdImagesController < Admin::AdBannersController def new @ad_image = AdImage.new @ad_banner = Banner.find(params[:banner_id]) + @item = [[t('image'),"1"],[t('video'),"2"]] if can_edit_or_delete?(@ad_banner) @tags = @module_app.tags || [] @ad_image.postdate = Date.today diff --git a/app/models/ad_image.rb b/app/models/ad_image.rb index a57465d..b0ba05f 100644 --- a/app/models/ad_image.rb +++ b/app/models/ad_image.rb @@ -13,15 +13,16 @@ class AdImage field :link_open, type: String field :postdate , :type => DateTime, :default => Time.now field :deadline , :type => DateTime + field :youtube , :type => String field :sort_number, :type => Integer field :language_enabled, :type => Array, :default => ["en","zh_tw"] - + field :exchange_item, :default => "1" LINK_OPEN_TYPES = ["local", "new_window"] belongs_to :banner before_validation :add_http - validates :file, presence: true + # validates :file, presence: true validates :out_link, format: { with: URI::regexp(%w(http https)) }, allow_blank: true # validates :title, presence: true @@ -33,6 +34,28 @@ class AdImage self.deadline <% if !banner.ad_images.can_display.blank? %> <% banner.ad_images.can_display.desc(:created_at).each do |image| %> -
+ <% if image.exchange_item == '1' %> +
+ <% elsif image.exchange_item == '2' %> +
+ <% end %> <% end %> <% else %>
@@ -250,6 +254,8 @@
<%= t(:images) %> + + <%= t(:video) %>
@@ -285,16 +291,31 @@ <% banner.ad_images.desc(:created_at).each_with_index do |image,idx| %>
-
" style="background: url('<%= image.file.thumb.url %>'); background-size: cover; background-position: center;"> - <% if image.expired? %> -
<%= t("ad_banner.expired") %>
- <% end %> - <% if (image.postdate > Time.now rescue false) %> -
<%= t("start_date")+': '+(format_value image.postdate) %>
- <% end %> - <%= t(:edit) %> - <%= t(:delete_) %> -
+ <% if image.exchange_item == '1' %> +
" style="background: url('<%= image.file.thumb.url %>'); background-size: cover; background-position: center;"> + <% if image.expired? %> +
<%= t("ad_banner.expired") %>
+ <% end %> + <% if (image.postdate > Time.now rescue false) %> +
<%= t("start_date")+': '+(format_value image.postdate) %>
+ <% end %> + <%= t('image') %> + <%= t(:edit) %> + <%= t(:delete_) %> +
+ <% else %> +
" style="background: url('http://img.youtube.com/vi/<%= image.youtube.to_s[-11,11] %>/hqdefault.jpg'); background-size: cover; background-position: center;"> + <% if image.expired? %> +
<%= t("ad_banner.expired") %>
+ <% end %> + <% if (image.postdate > Time.now rescue false) %> +
<%= t("start_date")+': '+(format_value image.postdate) %>
+ <% end %> + <%= t('video') %> + <%= t(:edit) %> + <%= t(:delete_) %> +
+ <% end %>
diff --git a/app/views/admin/ad_banners/_index_table.html.erb b/app/views/admin/ad_banners/_index_table.html.erb index d18eb15..7604c6a 100644 --- a/app/views/admin/ad_banners/_index_table.html.erb +++ b/app/views/admin/ad_banners/_index_table.html.erb @@ -28,7 +28,7 @@ > <% if !banner.ad_images.can_display.blank? %> <% banner.ad_images.can_display.desc(:created_at).each do |image| %> -
+
'); background-size: cover; background-position: center;">
<% end %> <% else %>
diff --git a/app/views/admin/ad_banners/show.html.erb b/app/views/admin/ad_banners/show.html.erb index db5e76b..d94d27e 100644 --- a/app/views/admin/ad_banners/show.html.erb +++ b/app/views/admin/ad_banners/show.html.erb @@ -1,3 +1,6 @@ +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/jquery-ui-sortable.min" %> +<% end %> <% content_for :page_specific_css do %>