Merge branch 'master' into 'master'

Master

add the feature that let user can modify the show order of categories for index and widget by themselves

See merge request !5
This commit is contained in:
wmcheng 2019-09-21 01:20:11 +08:00
commit d32de01d1e
8 changed files with 164 additions and 14 deletions

View File

@ -7,7 +7,24 @@ class Admin::ArchiveFilesController < OrbitAdminController
redirect_to '/' + I18n.locale.to_s + module_pages[0] + '?title=' + params['title'].to_s redirect_to '/' + I18n.locale.to_s + module_pages[0] + '?title=' + params['title'].to_s
end end
end end
def save_categories_order
keys = params['data_keys']
values = params['data_values']
keys.each do |key|
key.slice! 'sort_number-'
end
keys.each_with_index do |key,index|
archivecategory = ArchiveCategory.where(category_id: key)
if !(values[index].empty?)
archivecategory.first.update_attributes(sort_number: values[index].to_i)
else
archivecategory.first.update_attributes(sort_number: nil)
end
end
render :json => {}.to_json
end
def index def index
if ArchiveSortOrder.count == 0 if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save ArchiveSortOrder.new('sort_order' => false).save
end end
@ -48,7 +65,21 @@ class Admin::ArchiveFilesController < OrbitAdminController
render_401 render_401
end end
end end
def categories_order
categories = @module_app.categories
categories.each do |category|
archive_cat = ArchiveCategory.all.select{ |value| value.category_id.to_s == category.id.to_s}
if archive_cat.length == 0
ArchiveCategory.create(category_id: category.id.to_s)
end
end
cats_with_nil = ArchiveCategory.all.in(sort_number: nil)
cats_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
@cats = cats_no_nil.concat(cats_with_nil).collect do |cat|
[categories.where(:_id => cat.category_id).first,cat.sort_number.to_s]
end
render :partial => "categories_order" if request.xhr?
end
def create def create
@archive_file = ArchiveFile.new(archive_vars) @archive_file = ArchiveFile.new(archive_vars)
@archive_file.create_user_id = current_user.id @archive_file.create_user_id = current_user.id

View File

@ -1,5 +1,7 @@
class ArchivesController < ApplicationController class ArchivesController < ApplicationController
def index def index
#avoid the categories to be not in the ArchiveCategory
check_cat_sort_data()
if ArchiveSortOrder.count == 0 if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save ArchiveSortOrder.new('sort_order' => false).save
end end
@ -7,8 +9,9 @@ class ArchivesController < ApplicationController
if !params['title'].nil? if !params['title'].nil?
files_by_cateogry = ArchiveFile.where(is_hidden: false).group_by(&:category) files_by_cateogry = ArchiveFile.where(is_hidden: false).group_by(&:category)
categories = files_by_cateogry.keys categories = files_by_cateogry.keys
categories_sort = get_sorted_cat_with_filter(categories,'orm')
cats_last = [] cats_last = []
categories.each do |category| categories_sort.each do |category|
url_to_edit = "" url_to_edit = ""
flag = false flag = false
archives = [] archives = []
@ -66,7 +69,8 @@ class ArchivesController < ApplicationController
files_by_cateogry = ArchiveFile.where(is_hidden: false).filter_by_categories.filter_by_tags.group_by(&:category) files_by_cateogry = ArchiveFile.where(is_hidden: false).filter_by_categories.filter_by_tags.group_by(&:category)
each_data_count = [] each_data_count = []
categories = files_by_cateogry.keys categories = files_by_cateogry.keys
cats = categories.collect do |category| categories_sort = get_sorted_cat_with_filter(categories,'orm')
cats = categories_sort.collect do |category|
url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : "" url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : ""
archives = files_by_cateogry[category].collect do |archive| archives = files_by_cateogry[category].collect do |archive|
statuses = archive.statuses_with_classname.collect do |status| statuses = archive.statuses_with_classname.collect do |status|
@ -247,24 +251,29 @@ F cats = files_by_cateogry.keys.collect do |cat|
end end
def widget def widget
check_cat_sort_data()
if ArchiveSortOrder.count == 0 if ArchiveSortOrder.count == 0
ArchiveSortOrder.new('sort_order' => false).save ArchiveSortOrder.new('sort_order' => false).save
end end
page_data_count = OrbitHelper.widget_data_count page_data_count = OrbitHelper.widget_data_count
categories = OrbitHelper.widget_categories categories = OrbitHelper.widget_categories #data are categories' ids or 'all'
@categories = [] @categories = []
if categories.first == "all" if categories.first == "all"
module_app = OrbitHelper.widget_module_app categories = OrbitHelper.widget_module_app.categories
@categories = module_app.categories.collect do |cat| categories_sort = get_sorted_cat_with_filter(categories,'orm')
@categories = categories_sort.collect do |cat|
{ {
"title" => cat.title, "title" => cat.title,
"id" => cat.id.to_s "id" => cat.id.to_s
} }
end end
else else
categories.each do |cat| categories_sort = get_sorted_cat_with_filter(categories,'id')
c = Category.find(cat) @categories = categories_sort.collect do |cat|
@categories << {"title" => c.title, "id" => c.id.to_s} {
"title" => cat.title,
"id" => cat.id.to_s
}
end end
end end
cats = @categories.collect do |cat| cats = @categories.collect do |cat|
@ -312,7 +321,35 @@ F cats = files_by_cateogry.keys.collect do |cat|
} }
end end
private private
def self.get_total_pages def get_sorted_cat_with_filter(categories,cat_type)
@@total_pages all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil)
end all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: 'desc')
all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat|
cat.category_id.to_s
end
if cat_type=='id'
categories_temp = ModuleApp.where(:key => "archive").first.categories
categories = categories_temp.select{|cat| categories.include? cat.id.to_s}
end
categories_sort = []
all_categories_id.each do |cat_id|
category_selected = categories.select{|category| cat_id == category.id.to_s}
if category_selected.length!=0
categories_sort << category_selected[0]
end
end
categories_sort
end
def self.get_total_pages
@@total_pages
end
def check_cat_sort_data
categories_temp = ModuleApp.where(:key => "archive").first.categories
categories_temp.each do |category|
archive_cat = ArchiveCategory.all.select{ |value| value.category_id.to_s == category.id.to_s}
if archive_cat.length == 0
ArchiveCategory.create(category_id: category.id.to_s)
end
end
end
end end

View File

@ -0,0 +1,5 @@
class ArchiveCategory
include Mongoid::Document
field :category_id, type: String
field :sort_number, type: Integer
end

View File

@ -0,0 +1,60 @@
<% all_locales = I18n.available_locales %>
<% count = all_locales.length %>
<script type='text/javascript'>
$('.category_sort_number_form').ready(function(){
$('form[name="category_sort_number_form"]').submit(function(){
var eles = $('form[name="category_sort_number_form"]').serialize().split('&')
var keys=[],values=[]
for (var i=0;i<eles.length;i++){
var temp = eles[i].split('=')
keys.push(temp[0])
values.push(temp[1])
}
if (navigator.onLine) {
$.ajax({
url : "/admin/archive_files/categories_order",
dataType : "json",
type : "post",
data:{data_keys: keys, data_values: values},
success:function(data){
location.reload();
},
error:function(){
alert('Your server has some problem, please try again later!')
}
})
} else {
alert('Please connect the network and try again later!')
}
return false;
})
})
</script>
<div class='category_sort_number_form'>
<form method="post" action="/admin/archive_files/categories_order?" name="category_sort_number_form" >
<table>
<thead style='line-height: 3;'>
<tr style="text-align: center;">
<td><%= t('archive.category') %></td>
<td><%= t('archive.sort_number') %></td>
</tr>
</thead>
<tbody style='line-height: 4;'>
<% @cats.each do |cat| %>
<tr>
<td style='padding-right: 10px;'>
<% all_locales.each_with_index do |locale,index| %>
<%= "#{cat[0][:title][locale]}" %>
<%= index!=(count-1) ? "/" : ":" %>
<% end %>
</td>
<td>
<input style='margin: 0;' type="number" name="sort_number-<%= cat[0].id %>" value="<%= cat[1].to_s %>">
</td>
</tr>
<% end %>
</tbody>
</table>
<input type="submit" value="<%= t('archive.save') %>">
</form>
</div>

View File

@ -1,12 +1,17 @@
en: en:
restful_actions:
categories_order: Categories Order
archive: archive:
category: Category
sort_number: Sort Order
save: Save
no_page: You have to create at least one page for archive module no_page: You have to create at least one page for archive module
choose_order: Order with ascending choose_order: Order with ascending
"yes": "Yes" "yes": "Yes"
"no": "No" "no": "No"
archive: Archive archive: Archive
all: All all: All
categories_order: Categories Order
new: New new: New
downloaded_times: Downloaded Times downloaded_times: Downloaded Times
show_lang: Language show_lang: Language

View File

@ -1,6 +1,10 @@
zh_tw: zh_tw:
restful_actions:
categories_order: 類別排序
archive: archive:
category: 類別
sort_number: 排序號碼
save: 儲存
no_page: 請至少為檔案室模組新增一頁內容頁 no_page: 請至少為檔案室模組新增一頁內容頁
choose_order: 升序排列 choose_order: 升序排列
"yes": "yes":
@ -8,6 +12,7 @@ zh_tw:
show_lang: 呈現語系 show_lang: 呈現語系
download: 檔案下載 download: 檔案下載
archive: 檔案室 archive: 檔案室
categories_order: 類別排序
Title: 標題 Title: 標題
downloaded_times: 下載次數 downloaded_times: 下載次數
Files: 檔案 Files: 檔案

View File

@ -4,6 +4,8 @@ locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_l
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do namespace :admin do
get 'archive_files/categories_order' => 'archive_files#categories_order'
post 'archive_files/categories_order' => 'archive_files#save_categories_order'
resources :archive_files resources :archive_files
end end
get "/xhr/archive/download" => "archives#download_file" get "/xhr/archive/download" => "archives#download_file"

View File

@ -43,6 +43,11 @@ module Archive
:active_for_action=>{'admin/archive_files'=>'tags'}, :active_for_action=>{'admin/archive_files'=>'tags'},
:active_for_tag => 'Archive', :active_for_tag => 'Archive',
:available_for => 'managers' :available_for => 'managers'
context_link 'archive.categories_order',
:link_path=>"admin_archive_files_categories_order_path" ,
:priority=>5,
:active_for_action=>{'admin/archive_files'=>'categories_order'},
:available_for => 'managers'
end end
end end
end end