diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index 0133d81..e4af582 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -41,13 +41,15 @@ class ArchivesController < ApplicationController } end end - if archive.url.present? - files << { - "file-name" => archive.title, - "file-type" => "link", - "file-url" => archive.url - } - end + if archive.urls.present? + archive.urls.each_with_index do |url,i| + files << { + "file-name" => archive.title, + "file-type" => archive.get_url_text(i), + "file-url" => url + } + end + end archives << { "archive-title" => archive.title, "description" => archive.description, @@ -127,13 +129,15 @@ class ArchivesController < ApplicationController "file-url" => "/xhr/archive/download?file=#{file.id}" } end - if archive.url.present? - files << { - "file-name" => archive.title, - "file-type" => "link", - "file-url" => archive.url - } - end + if archive.urls.present? + archive.urls.each_with_index do |url,i| + files << { + "file-name" => archive.title, + "file-type" => archive.get_url_text(i), + "file-url" => url + } + end + end end { "archive-title" => archive.title, @@ -184,12 +188,14 @@ class ArchivesController < ApplicationController } end end - if archive.url.present? - files << { - "file-name" => archive.title, - "file-type" => "link", - "file-url" => archive.url - } + if archive.urls.present? + archive.urls.each_with_index do |url,i| + files << { + "file-name" => archive.title, + "file-type" => archive.get_url_text(i), + "file-url" => url + } + end end { "archive-title" => archive.title, diff --git a/app/models/archive_file.rb b/app/models/archive_file.rb index eb19fc5..f3417c3 100644 --- a/app/models/archive_file.rb +++ b/app/models/archive_file.rb @@ -24,7 +24,9 @@ class ArchiveFile field :title, as: :slug_title, localize: true field :description, localize: true - field :url, localize: true + field :urls, localize: true, type: Array + field :url, localize: true #Old field + field :url_texts, localize: true, type: Array, default: [] field :create_user_id field :update_user_id field :postdate , :type => DateTime, :default => Time.now @@ -48,18 +50,41 @@ class ArchiveFile before_save :add_http - protected - - def add_http - temp_url = {} - Site.first.in_use_locales.each do |locale| - locale = locale.to_s - temp_url[locale] = self.url_translations[locale] - unless /https?:\/\/[\S]+/.match(self.url_translations[locale]) || self.url_translations[locale].blank? - temp_url[locale] = 'http://' + self.url_translations[locale] + after_initialize do + unless self.new_record? + if self.urls.nil? && self.url.present? + self.urls_translations = self.url_translations.map{|k,v| [k,[v]]}.to_h + self.save end end - self.url_translations = temp_url + end + + def get_url_text(idx=0,org=false) + url_text = self.url_texts[idx] rescue nil + if org + url_text + else + url_text.present? ? url_text : "link" + end + end + protected + def add_http + in_use_locales = Site.first.in_use_locales + temp_urls = {} + in_use_locales.each do |locale| + locale = locale.to_s + temp_urls[locale] = [] + self.urls_translations[locale].each do |tmp_url| + tmp = tmp_url + if tmp.present? + unless /^(http|https|ftp):\/\/[\S]+/.match(tmp_url) || url.include?("mailto:") + tmp = 'http://' + tmp_url + end + temp_urls[locale] << tmp + end + end rescue nil + end + self.urls_translations = temp_urls end # def to_indexed_json @@ -115,5 +140,4 @@ class ArchiveFile end end end - end diff --git a/app/views/admin/archive_files/_form.html.erb b/app/views/admin/archive_files/_form.html.erb index 99e7fcd..ecb7cf2 100644 --- a/app/views/admin/archive_files/_form.html.erb +++ b/app/views/admin/archive_files/_form.html.erb @@ -161,17 +161,21 @@ <% end %> - +
- +
- <%= f.fields_for :url_translations do |f| %> - <%= f.text_field locale, class: "input-block-level", placeholder: t(:url), value: (@archive_file.url_translations[locale] rescue nil) %> - <% end %> + + <%= t(:add) %>
- <% end %> @@ -409,8 +413,14 @@ } }); $('.main-forms .add-on').tooltip(); - $(document).on('click', '#add_file', add_file_field); - $(document).on('click', '.delete_file', function(){ + $(document).on('click', '#add_file', add_file_field); + $(document).on('click', '.add_link', function(){ + var form_link_url = "<%= escape_javascript(render(:partial => "form_link", :locals=>{:f=>f,:locale=>"new_locale",:url=>nil,:url_text=>nil} )) %>" + var old_locale = new RegExp("new_locale", "g"), + new_locale = $(this).data("locale"); + $(this).siblings(".link_append_target").append(form_link_url.replace(old_locale,new_locale)); + }) + $(document).on('click', '.delete_file, .delete_link', function(){ $(this).parents('.input-prepend').remove(); }); $(document).on('click',"[type='file']",function(){ @@ -422,6 +432,12 @@ $(this).parents('.start-line').hide(); } }); + $(document).on('click', '.remove_existing_link', function(){ + if(confirm("<%= I18n.t(:sure?)%>")){ + $(this).parents('.input-prepend').remove(); + } + }); + }); <% end %> diff --git a/app/views/admin/archive_files/_form_link.html.erb b/app/views/admin/archive_files/_form_link.html.erb new file mode 100644 index 0000000..486b521 --- /dev/null +++ b/app/views/admin/archive_files/_form_link.html.erb @@ -0,0 +1,15 @@ +
+ + <%= text_field_tag "#{f.object_name}[urls_translations][#{locale}][]", url , class: "input-large", placeholder: t(:url) %> + "> + <%= text_field_tag "#{f.object_name}[url_texts_translations][#{locale}][]", url_text , class: "input-large", placeholder: t("archive.url_text_hint") %> + <% if locale == "new_locale" %> + + + + <% else %> + + + + <% end %> +
diff --git a/config/locales/en.yml b/config/locales/en.yml index f3a89d3..9b0ddaa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,6 +2,8 @@ en: restful_actions: categories_order: Categories Order archive: + url_text: "URL Text" + url_text_hint: "URL Text, default: link" updated_at: Updated At download_file: Download file drag_file_to_here: Drag file to here diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index c409ac9..7284567 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -2,6 +2,8 @@ zh_tw: restful_actions: categories_order: 類別排序 archive: + url_text: "連結文字" + url_text_hint: "連結文字,預設為link" updated_at: 上傳日期 download_file: 檔案下載 drag_file_to_here: 拖移檔案到此