Add edit_sort feature.

This commit is contained in:
邱博亞 2024-07-18 20:59:34 +08:00
parent a704b6a1fd
commit a2b5614298
12 changed files with 225 additions and 4 deletions

View File

@ -10,6 +10,31 @@ class Admin::EventNewsController < OrbitAdminController
super
@app_title = "event_news_mod"
end
def update_sort
ids = params[:ids]
ids.each_with_index do |id,i|
EventNews.where(id: id).update(sort_number: i)
end
EventNewsCache.all.delete
edit_sort
render 'update_sort',layout: false
end
def update_sort_setting
setting = @event_news_setting
setting.update_attributes(settings_params)
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] = setting.enable_manually_sort
end
redirect_to edit_sort_admin_event_news_index_path
end
def edit_sort
@setting = @event_news_setting
@event_news = EventNews.where(is_top: true).order_by({is_top: -1,sort_number: 1,postdate: -1, _id: -1})
@table_fields = ['event_news.table.sort_number','event_news.table.title','event_news.event_date','event_news.start_date']
end
def index
EventNews.remove_expired_status

View File

@ -1,5 +1,19 @@
class EventNewsModsController < ApplicationController
include EventNewsHelper
def initialize
super
@app_title = 'event_news_mod'
@manually_sort = manually_sort
EventNews.instance_variable_set('@manually_sort',@manually_sort)
#self.request = OrbitHelper.request
end
def manually_sort
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] rescue EventNewsSetting.first.enable_manually_sort
else
EventNewsSetting.first.enable_manually_sort rescue false
end
end
def index
EventNews.remove_expired_status
sorted,total_pages = get_sorted_event_news

View File

@ -88,16 +88,32 @@ class EventNews
before_destroy :destroy_email
scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)}
scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc, event_date: :desc,postdate: :desc,id: :desc)}
scope :can_display_and_sorted_according_today, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil}).order(is_top: :desc, event_date: :asc,postdate: :desc,id: :desc).where(:event_date.gte => Date.today.to_time)}
scope :can_display_and_sorted, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).valid_time_range.order(event_date: :desc)}
scope :can_display_and_sorted_according_today, ->{where(:is_hidden.ne=>true,:is_preview.ne => true).order(event_date: :asc).valid_time_range.where(:event_date.gte => Date.today.to_time)}
scope :valid_time_range, ->{and_any_of([{"postdate"=>{"$lte"=> Time.now}, "deadline"=>{"$gte"=> Time.now}}, {"postdate"=>{"$lte"=> Time.now}, "deadline"=>nil}]).order((@manually_sort ? {is_top: :desc,sort_number: :asc,postdate: :desc,id: :desc} : {is_top: :desc,postdate: :desc,id: :desc}))}
scope :is_approved, ->{where(:approved => true)}
scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true)}
scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)}
before_create :set_expire
before_save :check_limit
before_save do
if @is_hidden_changed.nil? || @is_hidden_changed != true
@is_hidden_changed = self.is_hidden_changed?
end
if self.is_top_changed? && !self.is_top
self.sort_number = nil
end
end
index({postdate: 1}, { unique: false, background: true })
index({is_top: -1, postdate: -1, event_date: -1, _id: -1}, { unique: false, background: true })
index({is_top: -1, event_date: 1, postdate: -1, _id: -1}, { unique: false, background: true })
index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, postdate: -1, event_date: -1, _id: -1, deadline: -1}, { unique: false, background: true })
index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, event_date: 1, postdate: -1, event_date: -1, _id: -1, deadline: -1}, { unique: false, background: true })
if EventNewsSetting.pluck(:enable_manually_sort).first == true
index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, sort_number: 1, postdate: -1, event_date: -1, _id: -1, deadline: -1}, { unique: false, background: true })
index({approved: -1, is_hidden: 1, is_preview: 1, is_top: -1, sort_number: 1, event_date: 1, postdate: -1, _id: -1, deadline: -1}, { unique: false, background: true })
end
field :sort_number, type: Integer
def get_org_model
if self.is_preview
org_model = nil

View File

@ -11,11 +11,32 @@ class EventNewsSetting
field :including_day_of_the_week, type: Boolean, default: true
field :including_time, type: Boolean, default: true
field :hour_clock_24, type: Boolean, default: true
field :enable_manually_sort, type: Boolean, default: false
has_many :event_news_status_settings, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :event_news_status_settings, :allow_destroy => true
after_save do
before_save do
can_update_shared_hash = (defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash)
if self.including_day_of_the_week_changed? || self.including_time_changed? || self.hour_clock_24_changed?
EventNewsCache.all.destroy
@need_update_event_news = true
end
if self.enable_manually_sort_changed? && self.enable_manually_sort
EventNews.index({approved: -1,is_hidden: 1,is_preview: 1, is_top: -1,sort_number: 1,postdate: -1,_id: -1,deadline: -1}, { unique: false, background: true })
EventNews.create_indexes
if can_update_shared_hash
OrbitHelper::SharedMutex.synchronize do
OrbitHelper::SharedHash["event_news_mod"][:enable_manually_sort] = self.enable_manually_sort
end
end
@need_update_event_news = true
end
end
after_save do
if @need_update_event_news
@need_update_event_news = false
Thread.new do
EventNewsCache.all.delete
end
end
end
def self.check_limit_for_user(user_id, b_id = nil)

View File

@ -0,0 +1,38 @@
<div id="data-table" class="ut-table">
<table class="table main-list">
<thead>
<tr class="sort-header">
<% @table_fields.each do |field| %>
<%
field_text = field.to_s.include?('.') ? t(field.to_s) : field.to_s
sort = field.to_s.split('.')[-1]
active = params[:sort].eql? sort
order = active ? (["asc", "desc"]-[params[:order]]).first : "asc"
arrow = (order.eql? "desc") ? "<b class='icons-arrow-up-3'></b>" : "<b class='icons-arrow-down-4'></b>"
klass = sort.eql?(:title) ? "span5" : "span2"
th_data = "<a href='?sort=#{sort}&order=#{order}'>#{field_text} #{active ? arrow : ""}</a>"
%>
<th class='<%= klass %> <%= active ? "active" : "" %>'><%= th_data.html_safe %></th>
<% end %>
</tr>
</thead>
<tbody id="sortable">
<% @event_news.each do |ev| %>
<tr data-id="<%= ev.id %>">
<td>
<%= number_field_tag nil,ev.sort_number,class: 'sort_number',step: 1 %>
</td>
<td>
<%= ev.title.html_safe %>
</td>
<td>
<%= format_value ev.event_date %>
</td>
<td>
<%= format_value ev.postdate %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>

View File

@ -0,0 +1,86 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "lib/main-forms" %>
<%= stylesheet_link_tag "lib/fileupload" %>
<%= stylesheet_link_tag "lib/main-list" %>
<% end %>
<%= form_for @setting, url: update_sort_setting_admin_event_news_index_path, html: {class: "form-horizontal main-forms"}, method: 'post' do |f| %>
<div class="input-area">
<div class="control-group">
<%= f.label :maull, t("event_news.enable_manually_sort"), :class => "control-label muted" %>
<div class="controls">
<%= f.check_box :enable_manually_sort,:id=>'enable_manually_sort' %>
</div>
</div>
</div>
<% end %>
<div style="margin-bottom: 1em;">
<button type="button" class="btn btn-primary" id="update_sort_button"><%= t('event_news.manual_update_sort') %></button>
</div>
<%= render partial: 'edit_sort' %>
<script type="text/javascript">
function parse_val(val){
return (val == '') ? 0 : parseFloat(val);
}
function update_sort(){
var ids = $.map($('#sortable>tr'),function(v){return $(v).data('id')});
$.ajax({
url: "<%= update_sort_admin_event_news_index_path %>",
type: 'POST',
dataType: 'text',
data: {ids: ids},
success: function(data){
$('#data-table').replaceWith(data);
sortable();
}
});
}
function sortable(){
$( "#sortable" ).sortable({
update: function( event, ui ) {
update_sort();
}
});
$('.sort_number').change(function(){
$(this).data('changed', true);
});
$('.sort_number').on('blur', function(){
if(!$(this).data('changed')) {
return;
}
var new_sort_number = parse_val($(this).val());
var min_number = 0;
var max_number = $('.sort_number').length;
var pool = $('.sort_number').not(this);
var same_order = pool.filter(function(){
var tmp_sort = parse_val($(this).val());
if (tmp_sort<min_number){
min_number = tmp_sort;
}
if (tmp_sort>max_number){
max_number = tmp_sort;
}
return tmp_sort==new_sort_number
});
var tmp_same_order = $();
if (same_order.length>0) {
tmp_same_order = same_order.eq(0);
tmp_same_order.parents('tr').eq(0).before($(this).parents('tr').eq(0));
} else if (new_sort_number>=max_number) {
tmp_same_order = pool.eq(-1);
tmp_same_order.parents('tr').eq(0).after($(this).parents('tr').eq(0));
} else if (new_sort_number<=min_number) {
tmp_same_order = pool.eq(0);
tmp_same_order.parents('tr').eq(0).before($(this).parents('tr').eq(0));
}
update_sort();
$(this).data('changed', false);
});
}
$(document).ready(function(){
$('#update_sort_button').click(update_sort);
$('#enable_manually_sort').change(function(){
$(this).parents('form').submit();
})
sortable();
});
</script>

View File

@ -0,0 +1 @@
<%= render partial: 'edit_sort' %>

View File

@ -4,6 +4,9 @@ en:
event_news_mod:
event_news_mod: Event News
event_news:
manually_sort: Manually Sort
enable_manually_sort: Enable Manually Sort
manual_update_sort: Manually Update Sort
category: Category
event_date_setting: "Event date setting"
event_date_use_default_setting: "Event date use default setting"
@ -78,6 +81,7 @@ en:
file: File
view_count: View Count
department: Department
sort_number: Sort Number
add_new: Add New
export_to_excel: Export to Excel
export_all_anns: Export all Event News

View File

@ -4,6 +4,9 @@ zh_tw:
event_news_mod:
event_news_mod: 活動公告
event_news:
manually_sort: 手動排序
enable_manually_sort: 開啟手動排序
manual_update_sort: 手動更新排序
category: 類別
event_date_setting: "事件日期設定"
event_date_use_default_setting: "使用預設的事件日期設定"
@ -78,6 +81,7 @@ zh_tw:
file: 檔案下載
view_count: 瀏覽人次
department: 單位
sort_number: 排序值
add_new: 新建
import: 匯入
export_to_excel: 匯出至Excel檔

View File

@ -30,6 +30,9 @@ Rails.application.routes.draw do
collection do
get 'custom_fields_title'
post 'update_custom_title'
get "edit_sort"
post "update_sort_setting", to: 'event_news#update_sort_setting'
post "update_sort", to: 'event_news#update_sort'
end
end
end

View File

@ -153,6 +153,10 @@ Gem::Specification.new do |s|
s.summary = "EventNews for Orbit"
s.description = "EventNews for Orbit"
s.license = "MIT"
s.metadata = {
"_require" => "#{File.expand_path("../app/models/event_news_setting", __FILE__)}",
"global_hash" => "{enable_manually_sort: (EventNewsSetting.first.enable_manually_sort rescue false)}"
}
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]

View File

@ -144,6 +144,11 @@ module EventNewsMod
:priority=>1,
:active_for_action=>{'admin/event_news'=>'index'},
:available_for => 'users'
context_link 'event_news.manually_sort',
:link_path=>"edit_sort_admin_event_news_index_path" ,
:priority=>2,
:active_for_action=>{'admin/event_news'=>'edit_sort'},
:available_for => 'managers'
context_link 'new_',
:link_path=>"new_admin_event_news_path" ,
:priority=>2,