Change link to array.

This commit is contained in:
BoHung Chiu 2021-09-29 18:43:34 +08:00
parent 3ad002bdf1
commit 56bee2dab0
6 changed files with 105 additions and 40 deletions

View File

@ -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,

View File

@ -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

View File

@ -161,17 +161,21 @@
<% end %>
</div>
</div>
<!-- url -->
<!-- urls -->
<div class="control-group input-title">
<label class="control-label muted"><%= t(:url) %></label>
<label class="control-label muted"><%= t(:link) %></label>
<div class="controls">
<%= 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 %>
<div class="link_append_target">
<% I18n.with_locale(locale) do %>
<% f.object.urls.each_with_index do |url,i| %>
<%= render :partial => "form_link", :locals=>{:f=>f,:locale=>locale,:url=>url,:url_text=>f.object.get_url_text(i,true)} %>
<% end %>
<% end %>
</div>
<a class="trigger btn btn-small btn-primary add_link" data-locale="<%=locale%>"><i class="icons-plus"></i> <%= t(:add) %></a>
</div>
</div>
</div>
<% 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();
}
});
});
</script>
<% end %>

View File

@ -0,0 +1,15 @@
<div class="input-prepend input-append start-line" data-locale="<%=locale%>">
<span class="add-on icons-link" title="<%= t(:url) %>"></span>
<%= text_field_tag "#{f.object_name}[urls_translations][#{locale}][]", url , class: "input-large", placeholder: t(:url) %>
<span class="add-on icons-pencil" title="<%= t("archive.url_text") %>"></span>
<%= 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" %>
<span class="delete_link add-on btn" title="<%= t(:delete_) %>">
<a class="icon-trash"></a>
</span>
<% else %>
<span class="remove_existing_link add-on btn" title="<%= t(:remove) %>">
<a class="icon-remove"></a>
</span>
<% end %>
</div>

View File

@ -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

View File

@ -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: 拖移檔案到此