From 247b8ec4dc03c14ee7aea7dcf35ea08295e9e090 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Fri, 10 Jun 2011 10:31:50 +0800 Subject: [PATCH] HTML code can be modify (paser: update) --- app/controllers/admin/pages_controller.rb | 19 +++-- app/helpers/admin/item_helper.rb | 1 + app/models/page_part.rb | 2 +- app/views/admin/links/create.js.erb | 3 +- app/views/admin/pages/_edit_code.html.erb | 8 ++ app/views/admin/pages/_form_code.html.erb | 3 + app/views/admin/pages/create.js.erb | 3 +- app/views/admin/pages/edit.js.erb | 1 + app/views/admin/pages/show.html.erb | 2 +- app/views/admin/pages/update.js.erb | 2 + lib/parser.rb | 93 +++++++++++++++++++++++ lib/template/root.layout | 3 +- 12 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 app/views/admin/pages/_edit_code.html.erb create mode 100644 app/views/admin/pages/_form_code.html.erb create mode 100644 app/views/admin/pages/edit.js.erb create mode 100644 app/views/admin/pages/update.js.erb diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index c5ab9907..d6992c71 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -25,8 +25,10 @@ class Admin::PagesController < ApplicationController def edit @item = Page.find(params[:id]) - @item.content = parse_content(@item.content, {:locale => 'show'}) + # @item.content = parse_content(@item.content, {:locale => 'show'}) + @item.content = parse_page_code(@item) @i18n_variable = @item.i18n_variable + @code = params[:code] end def create @@ -39,7 +41,7 @@ class Admin::PagesController < ApplicationController format.html { redirect_to admin_page_url(@item) } - format.js {} + format.js {} end else flash.now[:error] = t('admin.create_error_page') @@ -50,12 +52,15 @@ class Admin::PagesController < ApplicationController def update @item = Page.find(params[:id]) - parse_content(@item.content, {:locale => 'destroy'}) - if @item.update_attributes(params[:page]) - @item.content = parse_content(@item.content, {:locale => 'create'}) - @item.save! + parse_content_update_code(@item, params[:page][:content]) + if @item.update_attributes(params[:item]) flash[:notice] = t('admin.update_success_page') - redirect_to admin_items_url( :parent_id => @item.parent_id ) + respond_to do |format| + format.html { + redirect_to admin_page_url(@item) + } + format.js {} + end else render :action => "edit" end diff --git a/app/helpers/admin/item_helper.rb b/app/helpers/admin/item_helper.rb index c0472f17..98dae928 100644 --- a/app/helpers/admin/item_helper.rb +++ b/app/helpers/admin/item_helper.rb @@ -11,6 +11,7 @@ module Admin::ItemHelper end ret << "
  • " ret << (link_to node.name, dest) + ret << ' | ' << (link_to t('admin.code'), eval("edit_admin_#{node._type.downcase}_path(node, :code => true)")) if node._type.eql?('Page') ret << ' | ' << (link_to t('admin.new_page'), new_admin_page_path(:parent_id => node.id)) if node._type.eql?('Page') ret << ' | ' << (link_to t('admin.new_link'), new_admin_link_path(:parent_id => node.id)) if node._type.eql?('Page') ret << ' | ' << (link_to t(:delete), eval("delete_admin_#{node._type.downcase}_path(node, :authenticity_token => form_authenticity_token)"), :confirm => t('sure?'), :class => 'delete') diff --git a/app/models/page_part.rb b/app/models/page_part.rb index 09f0152d..667eea48 100644 --- a/app/models/page_part.rb +++ b/app/models/page_part.rb @@ -20,7 +20,7 @@ class PagePart if self.i18n_variable_id self.i18n_variable.update_attributes(attr) else - var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.page.i18n_variable_id})) + var = I18nVariable.new(attr.merge({:key => self.name, :document_class => self.class, :parent_id => self.page.i18n_variable_id})) var.save self.i18n_variable_id = var.id end diff --git a/app/views/admin/links/create.js.erb b/app/views/admin/links/create.js.erb index 9190c005..1679d64c 100644 --- a/app/views/admin/links/create.js.erb +++ b/app/views/admin/links/create.js.erb @@ -1,2 +1,3 @@ $('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>"); -$('#main').html("<%= escape_javascript(render(:partial => 'admin/links/show')) %>"); \ No newline at end of file +$('#main').html("<%= escape_javascript(render(:partial => 'admin/links/show')) %>"); +history.pushState(null, document.title, "<%= escape_javascript(admin_link_url(@item)) %>"); \ No newline at end of file diff --git a/app/views/admin/pages/_edit_code.html.erb b/app/views/admin/pages/_edit_code.html.erb new file mode 100644 index 00000000..24069927 --- /dev/null +++ b/app/views/admin/pages/_edit_code.html.erb @@ -0,0 +1,8 @@ +<%= flash_messages %> + +<%= form_for @item, :url => admin_page_path(@item), :html => { :class => 'form' } do |f| %> + <%= render :partial => "admin/pages/form_code", :locals => { :f => f, :locale => locale } %> +

    + <%= f.submit t(:update) %> <%= link_back %> +

    +<% end %> \ No newline at end of file diff --git a/app/views/admin/pages/_form_code.html.erb b/app/views/admin/pages/_form_code.html.erb new file mode 100644 index 00000000..ad0e55ed --- /dev/null +++ b/app/views/admin/pages/_form_code.html.erb @@ -0,0 +1,3 @@ +<%= f.error_messages %> + +<%= f.text_area :content %> \ No newline at end of file diff --git a/app/views/admin/pages/create.js.erb b/app/views/admin/pages/create.js.erb index 5d59fd1d..9c4b31c7 100644 --- a/app/views/admin/pages/create.js.erb +++ b/app/views/admin/pages/create.js.erb @@ -1,2 +1,3 @@ $('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>"); -$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>"); \ No newline at end of file +$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>"); +history.pushState(null, document.title, "<%= escape_javascript(admin_page_url(@item)) %>"); \ No newline at end of file diff --git a/app/views/admin/pages/edit.js.erb b/app/views/admin/pages/edit.js.erb new file mode 100644 index 00000000..c0761ed6 --- /dev/null +++ b/app/views/admin/pages/edit.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => @code ? 'edit_code' : 'edit')) %>"); \ No newline at end of file diff --git a/app/views/admin/pages/show.html.erb b/app/views/admin/pages/show.html.erb index 3e1160c7..5371ccde 100644 --- a/app/views/admin/pages/show.html.erb +++ b/app/views/admin/pages/show.html.erb @@ -1 +1 @@ -<%= render 'show' %> \ No newline at end of file +<%= render @code ? 'show_code' : 'show' %> \ No newline at end of file diff --git a/app/views/admin/pages/update.js.erb b/app/views/admin/pages/update.js.erb new file mode 100644 index 00000000..6c6a1630 --- /dev/null +++ b/app/views/admin/pages/update.js.erb @@ -0,0 +1,2 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>"); +history.pushState(null, document.title, "<%= escape_javascript(admin_page_url(@item)) %>"); \ No newline at end of file diff --git a/lib/parser.rb b/lib/parser.rb index 51580e48..7f24c8a8 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -184,9 +184,102 @@ module Parser end end + def parse_page_code(page) + layout_content = page.layout.content + context = parser_context_code(page) + parser = Radius::Parser.new(context, :tag_prefix => 'r') + parser.parse(layout_content) + end + + def parser_context_code(page) + Radius::Context.new do |c| + c.define_tag 'layout_part' do |tag| + part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } + res = '' + res << "" + res << part.content if part + res << "" + end + end + end + + def parse_content_update_code(page, page_content) + context = parser_context_update_code(page, page_content) + parser = Radius::Parser.new(context, :tag_prefix => 'r') + parser.parse(page_content) + end + + def parser_context_update_code(page, page_content) + Radius::Context.new do |c| + c.define_tag 'layout_part' do |tag| + @part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } + tag.locals.name = tag.attr['name'].to_s + if @part + part_update = tag.expand + if part_update + if !part_update.eql?(@part.content) + @part.content = tag.expand + @part.save + tag.locals.action = 'update' + end + else + tag.locals.action = 'destroy' + end + else + tag.locals.action = 'new' + end + res = '' + res << "" + res << tag.expand + res << "" + end + c.define_tag 'multi_lang' do |tag| + case tag.locals.action + when 'new', 'update' + @h = {} + tag.expand + @part.i18n_variable = @h + when 'destroy' + else + res = '' + res << "" + res << tag.expand + res << "" + end + end + c.define_tag 'multi_lang:lang' do |tag| + if tag.locals.action + @h.merge!(tag.attr['name'] => "#{tag.expand}") + else + res = '' + res << "" + res << tag.expand + res << "" + end + end + end + end + def self.included(base) base.send :helper_method, :parse_page if base.respond_to? :helper_method base.send :helper_method, :parse_page_edit if base.respond_to? :helper_method + base.send :helper_method, :parse_page_code if base.respond_to? :helper_method end end diff --git a/lib/template/root.layout b/lib/template/root.layout index 08d16bb4..cdc61cfb 100644 --- a/lib/template/root.layout +++ b/lib/template/root.layout @@ -1,5 +1,4 @@ - +