diff --git a/app/assets/javascripts/inc/modal-preview.js b/app/assets/javascripts/inc/modal-preview.js index 496b2080..5df0d385 100644 --- a/app/assets/javascripts/inc/modal-preview.js +++ b/app/assets/javascripts/inc/modal-preview.js @@ -3,25 +3,53 @@ // <%= link_to "NewPreview", realtime_preview_admin_ad_banner_path(ad_banner_tab) , :class=>'preview_trigger'%> $(document).ready(function() { - $("a.post_preview").click(function(e){ - $("#main-wrap").after(""); - e.preventDefault(); - var form = $(this).parents("form").first() - //var cont = form["content"].value; - $.ajax({ - type: 'POST', - url: $(this).attr("href")+'?preview=true', - data: form.serialize(), - dataType: "script", - success: function (msg) { - $("#"+start_modal_with_id).modal('show'); }, - error: function(){ - alert("ERROR"); - } - }); - - return false - }); + // $(".post_preview").click(function(e){ + // $("#main-wrap").after(""); + // e.preventDefault(); + // var form = $(this).parents("form").first() + // //var cont = form["content"].value; + // // $.ajax({ + // // type: 'POST', + // // url: $(this).attr("href")+'?preview=true', + // // data: form.serialize(), + // // dataType: "script", + // // success: function (msg) { + // // $("#"+start_modal_with_id).modal('show'); }, + // // error: function(){ + // // alert("ERROR"); + // // } + // // }); + // url = "/panel/news/back_end/news_bulletins/preview?preview=true" + // // alert(url) + // form.attr("action",url); + // form.submit(); + // //return false; + // }); + var clicked_what = null; + $("button.post_preview").click(function(){ + clicked_what = $(this); + $("form.nccu_ajax_form").ajaxSubmit({ + beforeSubmit: function(a,f,o){ + $("#main-wrap").after(""); + o.dataType = 'script'; + o.url = clicked_what.attr("url"); + } + }); + }) + // $("form.nccu_ajax_form").ajaxForm({ + // beforeSubmit: function(a,f,o) { + // // if(clicked_what.hasClass("post_preview")){ + // // $("#main-wrap").after(""); + // // o.dataType = 'script'; + // // o.url = clicked_what.attr("url"); + // // } + // }, + // success: function(data) { + // // if(!clicked_what.hasClass("post_preview")){ + // // window.location = data.redirect_url; + // // } + // } + // }) $("a.preview_trigger").click(function(){ $("#main-wrap").after(""); diff --git a/app/helpers/admin/web_component_helper.rb b/app/helpers/admin/web_component_helper.rb new file mode 100644 index 00000000..dda121d1 --- /dev/null +++ b/app/helpers/admin/web_component_helper.rb @@ -0,0 +1,23 @@ +module Admin::WebComponentHelper + include ActionView::Helpers::TagHelper + + def alert_block_tag(title="",context="",*args) + content_tag(:div,:class=>"alert alert-block alert-error fade in") do + a = ActiveSupport::SafeBuffer.new + a << button_tag( 'x',:class=>"close",:data=>{:dismiss=>"alert"}) if (args.first[:close] rescue false) + a << content_tag(:h4,:class=>"alert-heading") do + title + end + a << content_tag(:p) do + context + end + # TODO : 可以提供更多功能 + # a << content_tag(:p) do + # b = link_to("Take this action","",:class=>"btn btn-danger") + # b << link_to("Or do this","",:class=>"btn") + # b + # end + end + + end + end diff --git a/app/models/preview.rb b/app/models/preview.rb index fb08f675..cbbe1d17 100644 --- a/app/models/preview.rb +++ b/app/models/preview.rb @@ -2,12 +2,81 @@ class Preview include Mongoid::Document include Mongoid::Timestamps - field :object, :type => Hash + # field :object_f, :type => Hash + field :object, :type=> Hash field :preview_at_link field :expired_at , :type => DateTime field :link_args, :type => Array field :object_class_type + has_many :preview_files, :autosave => true, :dependent => :destroy + + # def object=(params) + # save_upload_temp_link(params,"news_bulletin_files_attributes") #unless params[] + # self.object_f = params + # end + + # def object + # return object_f + # end + + # def save_upload_temp_link(params,field_name = "bulletin_files_attributes") + # image = preview_files.build(:file=>params[:image]) + # params[:image] = image.id + + # params[field_name].each_with_index do |item,index| + # bfa = preview_files.build(:file=>params[field_name][index.to_s][:file]) + # params[field_name][index.to_s] = bfa.id + # end unless params[field_name].nil? + # end + + # def dig_in_hash(hash,paths_ary) + # hash.each_pair do |key,in_hash| + # if in_hash.kind_of? Array + # dig_in_array(hash,paths_ary) + # elsif in_hash.kind_of? Hash + # dig_in_hash(hash,paths_ary) + # else + # puts("\n End Node: \t #{paths_ary.join } #{in_hash.class} : #{key}##{in_hash}") + # end + # end + # end + + # def dig_in_array(array,paths_ary) + # array.each do |item| + # if item.kind_of? Array + # dig_in_array(hash,paths_ary) + # elsif item.kind_of? Hash + # dig_in_hash(hash,paths_ary) + # else + # puts("\n End Node: \t #{paths_ary.join } #{item.class} : {item}") + # end + # end + # end + + + # def dig_in_hash_old(hash,paths_ary) + # hash.each_pair do |k,in_hash| + # if (!in_hash.kind_of? Array and !in_hash.kind_of? Hash) + # #p "UploadedFile : #{in_hash.is_a? ActionDispatch::Http::UploadedFile}" + # #in_hash = "no file" if in_hash.is_a? ActionDispatch::Http::UploadedFile + # #in_hash.select{|key,hash| hash.is_a? ActionDispatch::Http::UploadedFile} + # puts("\n End Node:# \n") + # p "#{paths_ary.join } #{in_hash.class} : #{in_hash}" + # #p "UploadedFile(#{in_hash}) : #{in_hash.is_a? ActionDispatch::Http::UploadedFile}" + # else + # if (!in_hash.first.kind_of? Array and !in_hash.first.kind_of? Hash) + # paths_ary << "[#{in_hash.first}]" + # end + # puts("\n Go Down [#{in_hash.first}]\n") + # dig_in_hash(in_hash,paths_ary) + # puts("\n Go Out \n") + # end + # puts "This is last" + # paths_ary.pop + # end + # end + def get_arg_hash object.slice(*link_args).inject({}){|la,(k,v)| la[k.to_sym] = v; la} end @@ -19,7 +88,15 @@ class Preview end def get_virtual_object - eval(self.object_class_type).new object + virtual_object = eval(self.object_class_type).new object + preview_files.each do |file| + if file.file_in_array + eval("virtual_object.#{file.field_name_for_rebuild}.build :file=>file.file") + else + eval("virtual_object.#{file.field_name_for_rebuild} = file.file") + end + end + virtual_object end end diff --git a/app/models/preview_file.rb b/app/models/preview_file.rb new file mode 100644 index 00000000..d06ddc25 --- /dev/null +++ b/app/models/preview_file.rb @@ -0,0 +1,19 @@ +class PreviewFile + + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + field :title, localize: true + field :description, localize: true + field :field_name_for_rebuild + field :file_in_array , :type => Boolean,default: false + + # field :to_save, :type => Boolean + field :should_destroy, :type => Boolean + + belongs_to :preview + # embedded_in :news_bulletin + +end diff --git a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_modal_preview.html.erb b/app/views/shared/preview/_modal_preview.html.erb similarity index 97% rename from vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_modal_preview.html.erb rename to app/views/shared/preview/_modal_preview.html.erb index 5b3ec481..cad29773 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_modal_preview.html.erb +++ b/app/views/shared/preview/_modal_preview.html.erb @@ -4,7 +4,7 @@
diff --git a/app/views/shared/preview/after_create.js.erb b/app/views/shared/preview/after_create.js.erb new file mode 100644 index 00000000..b700204f --- /dev/null +++ b/app/views/shared/preview/after_create.js.erb @@ -0,0 +1 @@ +window.location = "<%= @info[:redirect_url]%>"; \ No newline at end of file diff --git a/app/views/shared/preview/preview.html.erb b/app/views/shared/preview/preview.html.erb new file mode 100644 index 00000000..935824c8 --- /dev/null +++ b/app/views/shared/preview/preview.html.erb @@ -0,0 +1,12 @@ +<% if !request.xhr? %> + +<% end %> \ No newline at end of file diff --git a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/preview.js.erb b/app/views/shared/preview/preview.js.erb similarity index 61% rename from vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/preview.js.erb rename to app/views/shared/preview/preview.js.erb index e465d4de..717dc81e 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/preview.js.erb +++ b/app/views/shared/preview/preview.js.erb @@ -1,5 +1,6 @@ -$('#show_preview').html('<%=escape_javascript(render :partial=>"modal_preview") %>'); +$('#show_preview').html('<%=escape_javascript(render :partial=>"/shared/preview/modal_preview") %>'); var start_modal_with_id = "<%= @preview_obj.id %>"; $("#"+start_modal_with_id).css("width","1050px"); $("#"+start_modal_with_id).css("height","768px"); -$("#"+start_modal_with_id).css("margin","-270px 0 0 -550px"); \ No newline at end of file +$("#"+start_modal_with_id).css("margin","-270px 0 0 -550px"); +$("#"+start_modal_with_id).modal(); \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index e2d199d4..1b064058 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,6 +14,7 @@ en: cant_empty_star: Cannot be empty (*) create_fail: Creation failed create: Create + continued: continued delete: Delete desktop: Desktop disable: Disable @@ -43,6 +44,7 @@ en: search_nccu: Search NCCU show: Show submit: Submit + preview: Preview sure?: Are you sure? update: Update update_failed: Update failed diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 08e4aebb..debefaf7 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,5 +1,22 @@ zh_tw: - + mongoid: + models: + news_bulletin: 新聞 + bulletin: 公告 + ad_banner: 廣告輪播 + web_link: 連結管理 + attributes: + news_bulletin: + title: 新聞標題 + bulletin: + title: 公告標題 + ad_banner: + title: 標題 + web_link: + title: 名稱 + url: 路徑 + + _locale: 中文 me: 我 @@ -9,6 +26,7 @@ zh_tw: browse: 選擇檔案 cancel: 取消 cant_empty_star: 不能為空 (*) + continued: 詳讀全文 create: 新增 create_fail: 創建失敗 delete: 刪除 @@ -40,6 +58,7 @@ zh_tw: search_nccu: 搜尋政大 show: 顯示 submit: 送出 + preview: 預覽 sure?: 您肯定嗎? update: 更新 update_failed: 更新失敗 @@ -489,6 +508,7 @@ zh_tw: body: "以下欄位發生問題:" errors: + at_least_one: 至少要有一個 format: "%{attribute} %{message}" messages: inclusion: "沒有包含在列表中" diff --git a/lib/orbit_core_lib.rb b/lib/orbit_core_lib.rb index f152e3f4..cf72012b 100644 --- a/lib/orbit_core_lib.rb +++ b/lib/orbit_core_lib.rb @@ -1,14 +1,14 @@ module OrbitCoreLib module Preview def self.included(base) - base.instance_eval("field :is_preview,type: Boolean,:default => false") - base.instance_eval("scope :not_preview,where(:is_preview=>false)") - base.define_singleton_method :new_preview do |*args| - temp_obj = self.new(args.first) - temp_obj.is_preview = true - temp_obj.save(:validate => false) rescue - temp_obj - end + + # base.instance_eval("field :is_preview,type: Boolean,:default => false") + # base.instance_eval("scope :not_preview,where(:is_preview=>false)") + base.class_eval (" + def to_preview + raise 'Developer,please override to_preview method' + end + ") end end diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb index 75c8e304..1b4ee879 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/back_end/bulletins_controller.rb @@ -13,10 +13,11 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController before_filter :for_app_sub_manager,:except => [:index,:show,:get_sorted_and_filtered_bulletins] def preview - debugger - a=1 - - @bulletin = Bulletin.new_preview(params[:bulletin]) + #@preview_obj = Preview.create(:object=>params[:news_bulletin],:expired_at=>DateTime.now+30.minutes,:preview_at_link=>"panel_news_front_end_news_bulletin_path",:object_class_type=>"NewsBulletin") + bulletin = Bulletin.new params + @preview_obj = bulletin.to_preview + @preview_obj.save + render '/shared/preview/preview.js.erb' end def index @@ -75,8 +76,13 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController # GET /bulletins/new # GET /bulletins/new.xml def new - @bulletin = Bulletin.new(:postdate => DateTime.now) - + if(session[:in_validate_object].blank?) + @bulletin = Bulletin.new(:postdate => DateTime.now) + else + @bulletin = session[:in_validate_object] + session[:in_validate_object] = {} + end + @link_url = panel_announcement_back_end_bulletins_path get_tags respond_to do |format| @@ -139,10 +145,20 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController format.html { redirect_to(panel_announcement_back_end_bulletins_url, :notice => t('bulletin.create_bulletin_success')) } format.xml { render :xml => @bulletin, :status => :created, :location => @bulletin } - format.js + # format.js + format.js { + @info = {"success"=>"true","redirect_url"=>panel_announcement_back_end_bulletins_url} + flash[:notice] = t('bulletin.create_bulletin_success') + render "/shared/preview/after_create.js.erb" + } else format.html { render :action => "new" } format.xml { render :xml => @bulletin.errors, :status => :unprocessable_entity } + format.js { + @info = {"success"=>"false","redirect_url"=>new_panel_announcement_back_end_bulletin_url(:bulletin => @bulletin)} + session[:in_validate_object] = @bulletin + render "/shared/preview/after_create.js.erb" + } end end @@ -344,7 +360,7 @@ class Panel::Announcement::BackEnd::BulletinsController < OrbitBackendController def get_tags module_app = ModuleApp.first(:conditions => {:key => 'announcement'}) - @tags = Tag.all(:conditions => {:module_app_id => module_app.id}).order_by(I18n.locale, :asc) + @tags = Tag.all(:conditions => {:module_app_id => module_app.id}).order_by(I18n.locale, :asc) rescue [] end end diff --git a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb index a5cad78d..69f96d91 100644 --- a/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb +++ b/vendor/built_in_modules/announcement/app/controllers/panel/announcement/front_end/bulletins_controller.rb @@ -69,7 +69,8 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController end def preview_content - @bulletin = Bulletin.find params[:id] + @bulletin = Bulletin.find params[:id] rescue nil + @bulletin = Preview.find(params[:id]).get_virtual_object if @bulletin.nil? get_categorys end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 6e6dfb62..69541bc1 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -197,6 +197,18 @@ class Bulletin tags.order_by(I18n.locale, :asc) end + + def to_preview + preview_object = Preview.new(:expired_at=>DateTime.now+30.minutes,:preview_at_link=>"panel_announcement_front_end_bulletin_path",:object_class_type=>"Bulletin") + + self.bulletin["bulletin_files_attributes"].each_with_index do |atr,idx| + preview_object.preview_files.build(:file=>self.bulletin["bulletin_files_attributes"][idx.to_s],:field_name_for_rebuild=>'bulletin_files',:file_in_array=>true) + end unless self.bulletin["bulletin_files_attributes"].nil? + preview_object.object = self.bulletin.except("bulletin_files_attributes") + preview_object + end + + protected def check_deadline @@ -223,4 +235,5 @@ class Bulletin self.tag_ids.delete('') end + end diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb index 8c32cc6b..a5099787 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/_form.html.erb @@ -1,5 +1,7 @@ <% # encoding: utf-8 %> - +<% content_for :page_specific_javascript do -%> + <%= javascript_include_tag "inc/modal-preview" %> +<% end -%> <%= f.error_messages %> @@ -238,8 +240,8 @@