From 6044857feace6876e056695cf4620bf340420958 Mon Sep 17 00:00:00 2001 From: Fu Matthew Date: Fri, 7 Sep 2012 17:55:59 +0800 Subject: [PATCH] complete preview for both new and announcement --- app/assets/javascripts/inc/modal-preview.js | 66 ++++++++++----- app/helpers/admin/web_component_helper.rb | 23 ++++++ app/models/preview.rb | 81 ++++++++++++++++++- app/models/preview_file.rb | 19 +++++ .../shared/preview}/_modal_preview.html.erb | 2 +- app/views/shared/preview/after_create.js.erb | 1 + app/views/shared/preview/preview.html.erb | 12 +++ .../views/shared/preview}/preview.js.erb | 5 +- config/locales/en.yml | 2 + config/locales/zh_tw.yml | 22 ++++- lib/orbit_core_lib.rb | 16 ++-- .../back_end/bulletins_controller.rb | 32 ++++++-- .../front_end/bulletins_controller.rb | 3 +- .../announcement/app/models/bulletin.rb | 13 +++ .../back_end/bulletins/_form.html.erb | 8 +- .../back_end/bulletins/new.html.erb | 2 +- .../back_end/news_bulletins_controller.rb | 21 +++-- .../news/app/models/news_bulletin.rb | 14 ++++ .../back_end/news_bulletins/_form.html.erb | 10 +-- .../news/back_end/news_bulletins/new.html.erb | 2 +- .../front_end/news_bulletins/show.html.erb | 4 +- .../news_bulletins/home_banner.html.erb | 2 +- .../back_end/web_links_controller.rb | 6 +- .../widget/web_links_controller.rb | 2 +- 24 files changed, 301 insertions(+), 67 deletions(-) create mode 100644 app/helpers/admin/web_component_helper.rb create mode 100644 app/models/preview_file.rb rename {vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins => app/views/shared/preview}/_modal_preview.html.erb (97%) create mode 100644 app/views/shared/preview/after_create.js.erb create mode 100644 app/views/shared/preview/preview.html.erb rename {vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins => app/views/shared/preview}/preview.js.erb (61%) 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 @@

<%= t("modal.preview") %>

- <%= link_to t('preview'),panel_announcement_back_end_bulletin_preview_path, :class=>"btn" %> - <%= f.submit t('submit'), :class=>'btn btn-primary' %> + <%= button_tag t("preview"), :name=>"commit",:class=>'btn post_preview two_btn',:type=>:button,:url=>preview_panel_announcement_back_end_bulletins_path %> + <%= f.submit t('submit'), :class=>'btn btn-primary two_btn' %> <%= link_to t('cancel'), get_go_back, :class=>"btn" %>
diff --git a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/new.html.erb b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/new.html.erb index ded7fba0..8fc3d044 100644 --- a/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/new.html.erb +++ b/vendor/built_in_modules/announcement/app/views/panel/announcement/back_end/bulletins/new.html.erb @@ -1,5 +1,5 @@
-<%= form_for @bulletin, :url => panel_announcement_back_end_bulletins_path do |f| %> +<%= form_for @bulletin, :url => panel_announcement_back_end_bulletins_path,:html=>{ :class=>"nccu_ajax_form"} do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> <% end %>
diff --git a/vendor/built_in_modules/news/app/controllers/panel/news/back_end/news_bulletins_controller.rb b/vendor/built_in_modules/news/app/controllers/panel/news/back_end/news_bulletins_controller.rb index 2d365ec7..2e8038e3 100644 --- a/vendor/built_in_modules/news/app/controllers/panel/news/back_end/news_bulletins_controller.rb +++ b/vendor/built_in_modules/news/app/controllers/panel/news/back_end/news_bulletins_controller.rb @@ -11,7 +11,11 @@ class Panel::News::BackEnd::NewsBulletinsController < OrbitBackendController end def preview - @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") + #@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") + news_bulletin = NewsBulletin.new params + @preview_obj = news_bulletin.to_preview + @preview_obj.save + render '/shared/preview/preview.html.erb', :layout=>false end def index @@ -88,9 +92,12 @@ class Panel::News::BackEnd::NewsBulletinsController < OrbitBackendController # GET /news_bulletins/new # GET /news_bulletins/new.xml def new - - @news_bulletin = NewsBulletin.new - + if(session[:in_validate_object].blank?) + @news_bulletin = NewsBulletin.new + else + @news_bulletin = session[:in_validate_object] + session[:in_validate_object] = {} + end @link_url = panel_news_back_end_news_bulletins_path # @news_bulletin.news_bulletin_files.build @@ -120,7 +127,6 @@ class Panel::News::BackEnd::NewsBulletinsController < OrbitBackendController # POST /news_bulletins # POST /news_bulletins.xml def create - binding.pry get_tags if params[:news_bulletin_link] @@ -161,7 +167,8 @@ class Panel::News::BackEnd::NewsBulletinsController < OrbitBackendController expire_page(:controller=>'panel/news/widget/news_bulletins' , :action=>'home_banner') format.html { redirect_to(panel_news_back_end_news_bulletins_url, :notice => t('news_bulletin.create_news_bulletin_success')) } format.xml { render :xml => @news_bulletin, :status => :created, :location => @news_bulletin } - format.js + format.js + #format.json {render :json => {"success"=>"true","redirect_url"=>panel_news_back_end_news_bulletins_url,"notice" => t('news_bulletin.create_news_bulletin_success')}} else format.html { render :action => "new" } format.xml { render :xml => @news_bulletin.errors, :status => :unprocessable_entity } @@ -379,7 +386,7 @@ class Panel::News::BackEnd::NewsBulletinsController < OrbitBackendController def get_tags module_app = ModuleApp.first(:conditions => {:key => 'news'}) - @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/news/app/models/news_bulletin.rb b/vendor/built_in_modules/news/app/models/news_bulletin.rb index 4c31d939..d7e0f3bb 100644 --- a/vendor/built_in_modules/news/app/models/news_bulletin.rb +++ b/vendor/built_in_modules/news/app/models/news_bulletin.rb @@ -237,6 +237,18 @@ class NewsBulletin def sorted_tags tags.order_by(I18n.locale, :asc) end + + def to_preview + preview_object = Preview.new(:expired_at=>DateTime.now+30.minutes,:preview_at_link=>"panel_news_front_end_news_bulletin_path",:object_class_type=>"NewsBulletin") + preview_object.preview_files.build(:file=>self.news_bulletin["image"],:field_name_for_rebuild=>'image') + + + self.news_bulletin["news_bulletin_files_attributes"].each_with_index do |atr,idx| + preview_object.preview_files.build(:file=>self.news_bulletin["news_bulletin_files_attributes"][idx.to_s],:field_name_for_rebuild=>'news_bulletin_files',:file_in_array=>true) + end unless self.news_bulletin["news_bulletin_files_attributes"].nil? + preview_object.object = self.news_bulletin.except("news_bulletin_files_attributes","image") + preview_object + end protected @@ -260,4 +272,6 @@ class NewsBulletin self.tag_ids.delete('') end + + end diff --git a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb index 29d75d61..c53ce056 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/_form.html.erb @@ -4,7 +4,6 @@ <% end -%> <%= f.error_messages %> -
@@ -15,7 +14,6 @@
- <% if @news_bulletin.image %> <%= image_tag @news_bulletin.image rescue ''%> <% else %> @@ -132,7 +130,7 @@
- + <%= alert_block_tag(t("activerecord.errors.template.body"),session[:in_validate_object].errors.messages) unless session[:in_validate_object].blank? %> <%= f.label :unit_list_for_anc,t('news.news_bulletin.unit_name')%> <%= f.select :unit_list_for_anc_id,@unit_list_for_anc.collect{|t| [ t.title, t.id ]}, {:prompt => t("news.news_bulletin.ut_prompt")}, :class => "input-medium" %> @@ -279,10 +277,10 @@
- +
- <%= link_to t('preview'),preview_panel_news_back_end_news_bulletins_path, :class=>"btn post_preview",:method=>:post , :remote => true%> - <%= f.submit t('submit'), :class=>'btn btn-primary' %> + <%= button_tag t("preview"), :name=>"commit", :class=>'btn post_preview two_btn',:type=>:button, :url=>preview_panel_news_back_end_news_bulletins_path %> + <%= f.submit t('submit'), :class=>'btn btn-primary two_btn' %> <%= link_to t('cancel'), get_go_back, :class=>"btn" %>
diff --git a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/new.html.erb b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/new.html.erb index 233ad8bf..f95df671 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/new.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/back_end/news_bulletins/new.html.erb @@ -1,5 +1,5 @@
-<%= form_for @news_bulletin, :url => panel_news_back_end_news_bulletins_path do |f| %> +<%= form_for @news_bulletin, :url => panel_news_back_end_news_bulletins_path,:html=>{ :class=>"nccu_ajax_form"} do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> <% end %>
diff --git a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb index eac3fec8..ac5e284b 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/front_end/news_bulletins/show.html.erb @@ -2,8 +2,8 @@

<%= @news_bulletin.title %>

- <%= dislpay_view_count(@news_bulletin) %> - <%= display_date(@news_bulletin.postdate) %> + <%= dislpay_view_count(@news_bulletin) rescue ""%> + <%= display_date(@news_bulletin.postdate) rescue "" %> <% unit = @news_bulletin.unit_list_for_anc.title rescue nil %> <%= " | " if unit %> <%= link_to unit, panel_news_front_end_index_news_bulletins_by_unit_path(:name=>@news_bulletin.unit_list_for_anc_id) unless unit.blank?%> diff --git a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb index 62e2e958..c61b60ff 100644 --- a/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb +++ b/vendor/built_in_modules/news/app/views/panel/news/widget/news_bulletins/home_banner.html.erb @@ -21,7 +21,7 @@
  • <%= link_to image_tag(post.image.url, :size => "290x130"), panel_news_front_end_news_bulletin_path(post, :category_id => post.news_bulletin_category_id) %>

    <%= link_to post.title, panel_news_front_end_news_bulletin_path(post, :category_id => post.news_bulletin_category_id) %>

    -

    <%= post.subtitle.html_safe rescue '' %>

    +

    <%= truncate((post.subtitle rescue '') ,:length => 200).html_safe %>

  • <% end %> diff --git a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb index 1f2263d3..22b22471 100644 --- a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb +++ b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/back_end/web_links_controller.rb @@ -3,7 +3,7 @@ class Panel::WebResource::BackEnd::WebLinksController < OrbitBackendController before_filter :authenticate_user! # before_filter :for_app_manager,:except => [:index,:show] before_filter :for_app_sub_manager,:except => [:index] - before_filter :only => [ :new,:edit,:update] do |controller| + before_filter :only => [ :new,:edit,:update,:create] do |controller| controller.get_categorys('WebLinkCategory') end @@ -74,7 +74,7 @@ class Panel::WebResource::BackEnd::WebLinksController < OrbitBackendController # POST /web_links.xml def create @web_link = WebLink.new(params[:web_link]) - + get_tags @web_link.create_user_id = current_user.id @web_link.update_user_id = current_user.id @@ -159,7 +159,7 @@ class Panel::WebResource::BackEnd::WebLinksController < OrbitBackendController def get_tags module_app = ModuleApp.first(:conditions => {:key => 'web_resource'}) - @tags = Tag.all(:conditions => {:module_app_id => module_app.id}) + @tags = Tag.all(:conditions => {:module_app_id => module_app.id}) rescue [] end end diff --git a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb index 0ba01406..deb9d0c8 100644 --- a/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb +++ b/vendor/built_in_modules/web_resource/app/controllers/panel/web_resource/widget/web_links_controller.rb @@ -52,7 +52,7 @@ class Panel::WebResource::Widget::WebLinksController < OrbitWidgetController def get_tags module_app = ModuleApp.first(:conditions => {:key => 'web_resource'}) - @tags = Tag.all(:conditions => {:module_app_id => module_app.id}) + @tags = Tag.all(:conditions => {:module_app_id => module_app.id}) rescue [] end end