Fix preview feature.

This commit is contained in:
BoHung Chiu 2023-09-24 15:15:32 +08:00
parent 7bcdde4268
commit 9bb9c5959b
4 changed files with 50 additions and 1 deletions

View File

@ -112,6 +112,12 @@ class Admin::NewsController < OrbitAdminController
def update
uid = params[:id].split('-').last
news_bulletin = NewsBulletin.where(:uid=>uid).first rescue nil
NewsBulletin.where(:copy_id=>news_bulletin.id.to_s).destroy
news_bulletin.is_edit = true
news_bulletin.save
set_approved = news_bulletin.is_preview
news_bulletin = news_bulletin.get_org_model
news_bulletin.approved = true if set_approved
news_bulletin_params[:tags] = news_bulletin_params[:tags].blank? ? [] : news_bulletin_params[:tags]
news_bulletin_params[:email_member_ids] = news_bulletin_params[:email_member_ids].blank? ? [] : news_bulletin_params[:email_member_ids]
@ -207,6 +213,7 @@ class Admin::NewsController < OrbitAdminController
news_bulletin_data.delete('news_bulletin_files_attributes')
news_bulletin_data.delete('news_bulletin_links_attributes')
news_bulletin.update_attributes(news_bulletin_data)
news_bulletin.copy_id = params['news_bulletin_id']
else
news_bulletin = NewsBulletin.new(news_bulletin_params)
end
@ -215,7 +222,18 @@ class Admin::NewsController < OrbitAdminController
news_bulletin.save
render :text=>page_for_news_bulletin(news_bulletin)
end
def get_preview_action
news_bulletin = NewsBulletin.find_by(:uid=>params['uid'])
is_not_edit = (!news_bulletin.is_edit)
org_bulletin = news_bulletin.get_org_model
NewsBulletin.where(:copy_id=>org_bulletin.id.to_s).destroy
if is_not_edit
news_bulletin.destroy
render :json=> {:success=>true,:action=>"close"}
else
render :json=> {:success=>true,:action=>"redirect",:path=>edit_admin_news_path(:id=>org_bulletin.id)}
end
end
def destroy_preview
news_bulletin = NewsBulletin.find_by(:uid=>params['uid'])
if news_bulletin.is_preview

View File

@ -9,6 +9,9 @@ class NewsBulletin
include OrbitCategory::Categorizable
include Slug
field :is_edit, type: Boolean, default: false #use to check whether the preview record changed
field :copy_id
field :title, as: :slug_title, type: String, localize: true
field :subtitle, localize: true
field :text, localize: true
@ -57,6 +60,18 @@ class NewsBulletin
scope :can_display_postdate, ->{where(:is_hidden=>false,:postdate.lt=>Time.now).order_by([:is_top, :desc])}
scope :is_approved, ->{where(:approved => true)}
def get_org_model
if self.is_preview
org_model = nil
if self.copy_id
org_model = self.class.find(self.copy_id) rescue nil
end
org_model.nil? ? self : org_model
else
self
end
end
def update_user
User.find(update_user_id) rescue nil
end

View File

@ -400,11 +400,26 @@
processData: false,
contentType: false
}).done(function(data){
if(window.location.protocol === "https:"){
data = data.replace("http:","https:");
}
window.preview_news_uid = data.split("-").slice(-1)[0].split("?")[0];
$('.modal-body iframe').attr('src',data);
$('#show_preview .modal').modal();
$('#show_preview .modal').height(function() {
return $(window).height() * 0.7;
});
$('#show_preview .modal').on('hidden.bs.modal', function () {
$.ajax({
type: "post",
url: '<%= admin_news_get_preview_action_path %>',
data: {uid: window.preview_news_uid}
}).done(function(data){
if(data["action"] == "redirect"){
window.location.href = data["path"];
}
})
});
var slug = data.split('/')[(data.split('/').length-1)];
// $('#preview-iframe').on('load', function(){

View File

@ -4,6 +4,7 @@ Rails.application.routes.draw do
scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do
post 'news/get_preview_action', to: 'news#get_preview_action'
post 'news/preview', to: 'news#preview'
get 'news/destroy_preview/:slug_title-:uid', to: 'news#destroy_preview'
get 'newsbulletins.json', to: 'newsbulletins#get_bulletins'