announcement-test/app/views/admin/announcements/_index.html.erb

193 lines
6.8 KiB
Plaintext

<script>
if(document.querySelectorAll("#orbit-bar").length==0) location.reload();
if( Array.prototype.delete == undefined ){
Array.prototype.delete = function(v){
var idx = this.indexOf(v);
if(idx != -1){
return this.splice(idx, 1)[0];
}else{
return null;
}
}
}
function reload_tbody(url){
console.log("done");
if(url == undefined){
url = window.location.href;
}
$.get(url).done(function(data){
$('#index_table tbody').replaceWith($(data).find('tbody'));
if(window.scroll_top){
$(window).scrollTop(window.scroll_top);
}
window.anncs_delete_ids = [];
$(".delete_anncs_btn").addClass('hide');
});
}
$(document).on("click", ".select_delete", function(){
var that = $(this);
var id = that.data("id");
if(window.anncs_delete_ids == undefined){
window.anncs_delete_ids = [];
}
if(that.prop("checked")){
window.anncs_delete_ids.push(id);
}else{
window.anncs_delete_ids.delete(id);
}
if(window.anncs_delete_ids.length == 0){
$(".delete_anncs_btn").addClass('hide');
}else{
$(".delete_anncs_btn").removeClass('hide');
}
})
function delete_anncs(){
if(!window.anncs_delete_ids || window.anncs_delete_ids.length == 0){
return null;
}
var data = new FormData();
var csrf_token = document
.querySelector("meta[name='csrf-token']")
.getAttribute("content");
var csrf_param = document
.querySelector("meta[name='csrf-param']")
.getAttribute("content");
if (csrf_token && csrf_param) {
data.append(csrf_param, csrf_token);
}
window.anncs_delete_ids.forEach(function(val){
data.append('ids[]', val);
});
$.ajax({
url: "<%=admin_announcements_delete_path%>",
method: 'DELETE',
data: data,
headers: {
// 'Content-Type': 'multipart/form-data',
// 使用 multipart/form-data 在此不需要設定 Content-Type。
'X-Requested-With': 'XMLHttpRequest',
'Authorization': `Bearer ${ csrf_token }`,
},
contentType: false, //required
processData: false, // required
statusCode: {
204: reload_tbody,
403: function() {
alert( "Delete failed!" );
}
}
})
}
$(document).on("click", ".delete_anncs_btn", function(){
if(window.confirm("<%=t(:sure?)%>")){
if(window.confirm("<%=t(:sure?)%>")){
delete_anncs();
}
}
})
</script>
<table class="table main-list">
<thead>
<tr class="sort-header">
<th style="width: 1em;"></th>
<% @table_fields.each do |f| %>
<%= thead(f) %>
<% end %>
</tr>
</thead>
<tbody>
<% @bulletins.each do |b| %>
<tr>
<% is_editable = can_edit_or_delete?(b) %>
<td><% if is_editable %><input type="checkbox" class="select_delete" data-id="<%= b.uid %>"><% end %></td>
<td>
<%= b.status_for_table %>
</td>
<% if @enable_annc_dept %>
<td>
<%= b.annc_dept ? @annc_depts[b.annc_dept] : '' rescue "" %>
</td>
<% end %>
<td>
<%= b.category.title rescue "" %>
<% if (b.category.disable rescue false) %>
<span class='label'><%= t(:disabled) %></span>
<% end %>
</td>
<td>
<% if b.expired? || (b.category.disable rescue false)%>
<%= b.title.to_s.html_safe %>
<% else %>
<a href="<%= page_for_bulletin(b) %>" target="_blank"><%= b.title.to_s.html_safe %></a>
<% end %>
<% if b.expired? %>
<span class='label'><%= t(:expired) %></span>
<% end %>
<% if b.reapproval %>
<span class='label'><%= t("announcement.reapproval") + " " + t(:pending) %></span>
<% end %>
<% if b.rejected %>
<span class='label'><%= t(:rejected) %> : <%= b.rejection_reason rescue "" %></span>
<% end %>
<% if !b.approved? && !b.rejected %>
<span class='label'><%= t(:pending) %></span>
<% end %>
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="#" class="detail-row" onclick="$('#<%= "#{b.id.to_s}-detail" %>').slideToggle(300); return false;"><%= t(:detail) %></a></li>
<% if is_editable %>
<li><a href="/admin/announcements/<%=b.id.to_s%>/edit"><%= t(:edit) %></a></li>
<li><a href="#" class="delete text-error" rel="/admin/announcements/<%=b.id.to_s%>"><%= t(:delete_) %></a></li>
<% end %>
<% if ((!b.approved && !b.rejected && !b.reapproval) || (b.rejected && b.reapproval)) && user_can_approve?(b) %>
<li><a href="<%= page_for_bulletin(b) %>" class="approval_button" data-id="<%= b.id.to_s %>" ><%= t("announcement.approval_waiting") %></a></li>
<% end %>
</ul>
</div>
</td>
<td><%= format_value b.postdate %></td>
<td class="<%= b.expired? ? "expired" : "" %>"><%= format_value b.deadline %></td>
<td><%= link_to b.bulletin_comments.count.to_s,"/#{I18n.locale}/admin/announcements/#{b.id}/comment" %></td>
<td><%= b.update_user.user_name rescue ""%><br><%= format_value b.updated_at %></td>
</tr>
<tr class="footable-row-detail">
<td class="footable-cell-detail" colspan="6">
<div id="<%= "#{b.id.to_s}-detail" %>" class="footable-row-detail-inner" style="display: none;">
<div>
<strong><%= t(:view_count) %></strong> :
<span class="label label-info"><%= b.view_count %></span>
</div>
<div>
<strong><%= t(:tags) %></strong> :
<% b.tags.each do |tag| %>
<span class="label label-warning"><%= tag.name %></span>
<% end %>
</div>
<div>
<strong><%= t("announcement.email_to") %></strong> :
<% b.email_members.each do |member| %>
<span class="label"><%= member.name %></span>
<% end %>
<% unless b.other_mailaddress.nil? %>
<% b.other_mailaddress.split(',').each do |mailaddress| %>
<span class="label"><%= mailaddress %></span>
<% end %>
<% end %>
</div>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<%=
content_tag :div, class: "bottomnav clearfix" do
content_tag(:div, paginate(@bulletins), class: "pagination pagination-centered") +
content_tag(:div, link_to(t("announcement.delete_selected"), "javascript:void(0)", :class=>"btn btn-danger"), class: "pull-right hide delete_anncs_btn") +
content_tag(:div, link_to(t(:new_),new_admin_announcement_path, :class=>"btn btn-primary"), class: "pull-right")
end
%>