ask/app/views/admin/asks/_index.html.erb

264 lines
9.5 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.batch_select_asks = [];
$(".delete_asks_btn, .batch_modify_status_btn").addClass('hide');
});
}
$(document).on("click", ".batch_select", function(){
var that = $(this);
var id = that.data("id");
if(window.batch_select_asks == undefined){
window.batch_select_asks = [];
}
if(that.prop("checked")){
window.batch_select_asks.push(id);
}else{
window.batch_select_asks.delete(id);
}
if(window.batch_select_asks.length == 0){
$(".delete_asks_btn, .batch_modify_status_btn").addClass('hide');
}else{
$(".delete_asks_btn, .batch_modify_status_btn").removeClass('hide');
}
})
function batch_delete_asks(){
if(!window.batch_select_asks || window.batch_select_asks.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.batch_select_asks.forEach(function(val){
data.append('ids[]', val);
});
$.ajax({
url: "<%=delete_admin_asks_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!" );
}
}
})
}
function batch_edit_status_asks(){
if($('#dialog-batch-modify-status').length == 0){
var li_statuses = "";
<% AskTicketStatus.all.each do |s| %>
li_statuses += "<li style=\"list-style: none;\"><label><input type=\"radio\" name=\"status_key\" value=\"<%=s.key%>\"><%=s.title%></label></li>"
<% end%>
$("#main-wrap").before("<div id='dialog-batch-modify-status' title='"+"<%= t('ask.batch_modify_status') %>'>"+
"<div style='clear:both;'></div><div><ul>"+ li_statuses +"</ul></div>"+
"</div>");
}else{
$('[name="status_key"]').prop('checked',false);
}
$( "#dialog-batch-modify-status" ).dialog({
resizable: true,
minHeight: 100,
maxHeight: 400,
modal: true,
buttons: {
"<%= t(:confirm) %>": function(){
var status_key = $('[name="status_key"]:checked').val();
if(status_key){
if(window.confirm("<%=t(:sure?)%>")){
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.batch_select_asks.forEach(function(val){
data.append('ids[]', val);
});
data.append("status_key", status_key);
$.ajax({
url: "<%=batch_modify_status_admin_asks_path%>",
method: 'POST',
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( "Edit status failed!" );
}
}
})
}
}
$( this ).dialog( "close" );
},
"<%= t(:cancel) %>": function() {
$( this ).dialog( "close" );
$( this ).remove();
}
}
});
}
$(document).on("click", ".delete_asks_btn", function(){
if(window.confirm("<%=t(:sure?)%>")){
if(window.confirm("<%=t(:sure?)%>")){
batch_delete_asks();
}
}
})
$(document).on("click", ".batch_modify_status_btn", function(){
batch_edit_status_asks();
})
</script>
<div>
<form>
<h4><%= t(:category) %></h4>
<select name="category" id="submit_when_change">
<% @cats_relations.each do |id, title| %>
<option value="<%=id%>" <%= @category_id == id ? 'selected' : '' %>><%= title %></option>
<% end %>
</select>
</form>
</div>
<table class="table main-list">
<thead>
<tr class="sort-header">
<th style="width: 1em;"></th>
<% @table_fields.each do |f| %>
<%= ask_thead(f, @ask_setting.field_name(f)) %>
<% end %>
<% @table_ext_fields.each do |f, v| %>
<%= ask_thead(f, v['field'][I18n.locale.to_s]) %>
<% end %>
</tr>
</thead>
<tbody>
<%
situation_class_relation = {
"is_waiting"=>"label-important",
"is_processed"=>"label-warning",
"is_referral"=>"",
"is_published"=>"label-success"
}
situation_trans = AskTicketStatus.all.map{|a| [a.key, a.title]}.to_h
last_serial_number = @ask_setting.last_serial_number
display_length = [last_serial_number.to_s.length + 1, 4].max
edit_box_display_index = @table_fields.count > 3 ? 2 : @table_fields.count
%>
<% @askquestions.each do |b| %>
<tr>
<% is_editable = can_edit_or_delete?(b)
ask_show_url = Page.where(:module=>'ask').first.url rescue nil
edit_button_can_show = true
%>
<td><% if is_editable %><input type="checkbox" class="batch_select" data-id="<%= b.id %>"><% end %></td>
<% @table_fields.each_with_index do |f,i| %>
<td>
<% case f %>
<% when 'serial_number' %>
<%= b.get_serial_number(last_serial_number, display_length) %>
<% when 'situation' %>
<span class='label <%=situation_class_relation[b.situation]%>'><%= situation_trans[b.situation] %></span>
<% when 'ask_category_id' %>
<%= b.category.title rescue 'category not set' %>
<% when 'sex' %>
<%= ['male','female'].include?(sex.to_s) ? t("mongoid.attributes.ask_question.#{sex}") : '' %>
<% when 'mail' %>
<%= b.mail %>
<% when 'title' %>
<% if b.situation == 'is_published' %>
<%= link_to b.title, (ask_show_url ? (ask_show_url+"?item=#{b.id}") : "javascript:alert('#{t('ask.no_index_page')}')") %>
<% else %>
<%= b.title %>
<% end %>
<% when 'name' %>
<%= b.name %>
<% when 'phone' %>
<%= b.phone %>
<% when 'appointment' %>
<%= b.appointment.strftime("%Y-%m-%d %H:%M") rescue nil %>
<% when 'created_at' %>
<%= b.created_at.strftime("%Y-%m-%d %H:%M") rescue nil %>
<% when 'ip' %>
<%= b.ip %>
<% when 'reviewer' %>
<%= b.reviewer.name rescue "" %>
<% when 'review_time' %>
<%= b.review_time.strftime("%Y-%m-%d %H:%M") rescue nil %>
<% end %>
<% if edit_box_display_index == i %>
<%= render partial: 'edit_box_for_index', :locals => {is_editable: is_editable, b: b} %>
<% end %>
</td>
<% end %>
<% @table_ext_fields.each do |f, v| %>
<td>
<%= show_on_front(f,v,b,true) %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<%= content_tag :div, class: "bottomnav clearfix" do %>
<%= content_tag :div, class: "pagination pagination-centered" do
paginate(@askquestions)
end %>
<%= content_tag :div, link_to(I18n.t('ask.display_fields_setting'), backend_table_setting_admin_asks_path + (@category_id.present? ? "?category_id=#{@category_id}" : ''), :class=>'btn btn-primary' ) + link_to(t("ask.delete_selected"), "javascript:void(0)", :class=>"btn btn-danger delete_asks_btn hide", :style=>'margin-left: 0.5em;') + link_to(t("ask.batch_modify_status"), "javascript:void(0)", :class=>"btn btn-success batch_modify_status_btn hide", :style=>'margin-left: 0.5em;'), class: 'pull-right' %>
<% end %>
<script>
$(document).ready(function(){
$("#submit_when_change").on("change",function(){
$(this)[0].form.submit();
})
})
</script>