added order for faq

This commit is contained in:
Harry Bomrah 2016-05-10 01:12:43 +08:00
parent 0a7713dce6
commit 3ee5ecd592
9 changed files with 242 additions and 8 deletions

View File

@ -1,4 +1,79 @@
/* /*reset*/
Place all the styles related to the matching controller here. h1, h2, h3, h4, h5, p{
They will automatically be included in application.css. margin: 0;
*/ padding: 0;
}
br{
display: block;
margin: 0 0 5px;
}
ol, ul{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
body > img {
display: none;
}
img{
border: none;
padding: 0;
margin: 0;
}
a {
outline: none;
text-decoration: none !important;
}
table, tr, td{
padding: 0;
border-spacing: 0;
}
/*reset end *//* CSS Document */
#web_resource_order_table{
border-collapse: unset !important;
position: relative;
width: 593px;
margin: 40px auto;
}
#web_resource_order_table th{
background-color: #454545;
color: #fff;
}
.position-text{
position: relative;
width: 85px;
}
#web_resource_order_table .editable-input{
position: relative;
width: 25px;
margin-bottom: 0;
padding: 0px 0px 0px 5px;
}
.position-text-div{
height: 22px;
}
.order-edit-notification{
background-color: #ffffd5;
z-index: 10;
display: none;
height: 25px;
left: 40%;
position: fixed;
text-align: center;
margin-top: 5px;
top: 85px;
width: 400px;
font-size: 13px;
}

View File

@ -60,6 +60,31 @@ class Admin::FaqsController < OrbitAdminController
redirect_to admin_faqs_path redirect_to admin_faqs_path
end end
def order
empty_position_qas = Qa.where(:order_position => nil)
if empty_position_qas.count > 0
max_position = Qa.max(:order_position)
max_position = 0 if max_position.nil?
empty_position_qas.each_with_index do |epl,i|
epl.order_position = i + max_position
epl.save
end
end
@qas = Qa.all.asc(:order_position)
end
def updateorder
ids_with_order = params[:order]
ids_with_order.each_with_index do |id,index|
qa = Qa.find(id) rescue nil
if !qa.nil?
qa.order_position = index
qa.save
end
end
render :json => {"success" => true}.to_json
end
private private
def setup_vars def setup_vars
@module_app = ModuleApp.where(:key => "faq").first @module_app = ModuleApp.where(:key => "faq").first

View File

@ -1,6 +1,6 @@
class FaqsController < ApplicationController class FaqsController < ApplicationController
def index def index
faqs = Qa.can_display.order_by(:created_at=>'desc').filter_by_categories faqs = Qa.can_display.order_by(:order_position => "asc").filter_by_categories
f = faqs.collect do |qa| f = faqs.collect do |qa|
statuses = qa.statuses_with_classname.collect do |status| statuses = qa.statuses_with_classname.collect do |status|
{ {

View File

@ -16,6 +16,7 @@ class Qa
field :create_user_id field :create_user_id
field :update_user_id field :update_user_id
field :uid, type: String field :uid, type: String
field :order_position, type: Integer, default: -1
has_many :qa_links, :autosave => true, :dependent => :destroy has_many :qa_links, :autosave => true, :dependent => :destroy
has_many :qa_files, :autosave => true, :dependent => :destroy has_many :qa_files, :autosave => true, :dependent => :destroy

View File

@ -0,0 +1,123 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "admin/faqs" %>
<% end %>
<div class="order-edit-notification">Please click "Save Order" button when you are done.</div>
<table width="100%" id="web_resource_order_table" class="table table-striped" class="web_soursce_table">
<thead>
<tr>
<th>Position</th>
<th>Link</th>
</thead>
<tbody>
<% @qas.each_with_index do |qa,i| %>
<tr>
<td class="position-text">
<div class="position-text-div" data-value="<%= (i + 1).to_s %>"><%= (i + 1).to_s %></div>
</td>
<td>
<div class="link-text-id" data-link-id="<%= qa.id.to_s %>"><%= qa.title %></div>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="bottomnav clearfix" style="left: 81px;">
<div class="action pull-right">
<a class="btn btn-info disabled" id="save-order-button" href="#">Save Order</a>
</div>
<div class="pagination pagination-centered"></div>
</div>
<script type="text/javascript">
var makeEditable = function(){
var input_box = $("<input type='text'/>"),
el = $(this);
input_box.addClass("editable-input");
input_box.val(el.data("value"));
input_box.attr("data-old-id",el.data("value"));
input_box.on("blur",function(){
putBackdiv($(this));
});
input_box.on("keypress",function(e){
if(e.keyCode == 13 || e.keyCode == 27){
putBackdiv($(this),e.keyCode);
}
})
el.parent().html(input_box);
input_box.focus();
}
var putBackdiv = function(el,keyCode){
current_value = parseInt((el.val() == "" ? el.data("old-id") : el.val())),
old_value = parseInt(el.data("old-id"));
if(isNaN(current_value) || keyCode == 27){
current_value = old_value;
}
if(old_value != current_value){
var new_index_value = (current_value > old_value ? current_value + 1 : current_value - 1),
div = $("<div class='position-text-div' data-value='" + current_value + "'>" + new_index_value + "</div>");
div.on("click",makeEditable);
el.parent().html(div);
$("#save-order-button").removeClass("disabled");
$(".order-edit-notification").slideDown();
sortTable(el.data("old-id"),current_value);
}else{
var div = $("<div class='position-text-div' data-value='" + current_value + "'>" + current_value + "</div>");
div.on("click",makeEditable);
el.parent().html(div);
}
}
var sortTable = function(changed_index,changed_to){
var table_element = document.getElementById("web_resource_order_table"),
data = [],
allRows = table_element.rows;
for(i = 1; i < allRows.length; i++){
var thisRow = allRows[i],
text = thisRow.cells[0].textContent.trim(),
hash = {};
hash.index = parseInt(text);
text = thisRow.cells[1].innerHTML.trim();
if(text != "&nbsp;"){
hash.link = text;
}
data.push(hash);
}
data = data.sort(function(a,b){return a.index - b.index});
renderSortedTable(data,table_element);
}
var renderSortedTable = function(data,table_element){
var allRows = table_element.rows;
for(i = 0;i < data.length; i++){
var thisRow = allRows[i + 1],
current_value = i + 1;
thisRow.cells[0].innerHTML = "<div class='position-text-div' data-value='" + current_value + "'>" + current_value + "</div>";
thisRow.cells[1].innerHTML = data[i].link;
}
$("#web_resource_order_table div.position-text-div").on("click",makeEditable);
}
$("#save-order-button").on("click",function(){
var el = $(this);
if(!el.hasClass("disabled")){
var data = [];
$("#web_resource_order_table .link-text-id").each(function(){
data.push($(this).data("link-id"));
})
$.ajax({
url : "/admin/faqs/updateorder",
data : {"order" : data},
dataType : "json",
type : "post"
}).done(function(){
el.addClass("disabled");
$(".order-edit-notification").slideUp();
})
}
return false;
})
$("#web_resource_order_table div.position-text-div").on("click",makeEditable);
</script>

View File

@ -3,6 +3,7 @@ en:
faq: faq:
all: All all: All
new: New new: New
order: Order
default_widget: default_widget:
question: Question question: Question
title: Question title: Question

View File

@ -1,6 +1,7 @@
zh_tw: zh_tw:
faq: faq:
order: Order
default_widget: default_widget:
question: 問題 question: 問題
title: 問題 title: 問題

View File

@ -3,7 +3,9 @@ Rails.application.routes.draw do
locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do namespace :admin do
resources :faqs get "/faqs/order" => "faqs#order"
post "/faqs/updateorder" => "faqs#updateorder"
resources :faqs
end end
end end
end end

View File

@ -38,10 +38,16 @@ module Faq
:active_for_category => 'Faq', :active_for_category => 'Faq',
:available_for => 'managers' :available_for => 'managers'
context_link 'faq.order',
:link_path=>"admin_faqs_order_path" ,
:priority=>4,
:active_for_action=>{'admin/faqs'=>'order'},
:available_for => 'managers'
context_link 'tags', context_link 'tags',
:link_path=>"admin_module_app_tags_path" , :link_path=>"admin_module_app_tags_path" ,
:link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'faq').id}", :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'faq').id}",
:priority=>4, :priority=>5,
:active_for_action=>{'admin/faqs'=>'tags'}, :active_for_action=>{'admin/faqs'=>'tags'},
:active_for_tag => 'Faq', :active_for_tag => 'Faq',
:available_for => 'managers' :available_for => 'managers'