From 24ae7ed5e3f6ba54da87c067b791e808bcd1dc74 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Fri, 13 May 2011 09:08:42 +0800 Subject: [PATCH] Links edition --- app/controllers/admin/items_controller.rb | 6 ++- app/controllers/admin/links_controller.rb | 43 ++++++++++++++----- app/controllers/admin/pages_controller.rb | 11 ++++- app/controllers/application_controller.rb | 6 ++- app/helpers/application_helper.rb | 28 ++++++++---- app/views/admin/items/_page.html.erb | 11 ----- .../admin/items/_site_map_left_bar.html.erb | 1 + app/views/admin/items/index.html.erb | 43 +++---------------- app/views/admin/items/index.js.erb | 6 +-- app/views/admin/links/_edit.html.erb | 12 ++++++ app/views/admin/links/_new.html.erb | 13 ++++++ app/views/admin/links/_show.html.erb | 24 +++++++++++ app/views/admin/links/create.js.erb | 2 + app/views/admin/links/delete.js.erb | 1 + app/views/admin/links/edit.html.erb | 13 ++---- app/views/admin/links/edit.js.erb | 1 + app/views/admin/links/new.html.erb | 14 ++---- app/views/admin/links/new.js.erb | 1 + app/views/admin/links/show.html.erb | 5 +++ app/views/admin/links/show.js.erb | 1 + app/views/admin/links/update.js.erb | 1 + app/views/admin/pages/_show.html.erb | 1 + app/views/admin/pages/show.html.erb | 1 + app/views/admin/pages/show.js.erb | 1 + app/views/layouts/_page.html.erb | 1 - app/views/layouts/_site_map_left_bar.html.erb | 1 - app/views/layouts/content.html.erb | 5 ++- config/locales/en.yml | 2 + config/locales/zh_tw.yml | 2 + config/routes.rb | 6 ++- public/javascripts/application.js | 19 ++++++++ 31 files changed, 184 insertions(+), 98 deletions(-) delete mode 100644 app/views/admin/items/_page.html.erb create mode 100644 app/views/admin/items/_site_map_left_bar.html.erb create mode 100644 app/views/admin/links/_edit.html.erb create mode 100644 app/views/admin/links/_new.html.erb create mode 100644 app/views/admin/links/_show.html.erb create mode 100644 app/views/admin/links/create.js.erb create mode 100644 app/views/admin/links/delete.js.erb create mode 100644 app/views/admin/links/edit.js.erb create mode 100644 app/views/admin/links/new.js.erb create mode 100644 app/views/admin/links/show.html.erb create mode 100644 app/views/admin/links/show.js.erb create mode 100644 app/views/admin/links/update.js.erb create mode 100644 app/views/admin/pages/_show.html.erb create mode 100644 app/views/admin/pages/show.html.erb create mode 100644 app/views/admin/pages/show.js.erb delete mode 100644 app/views/layouts/_page.html.erb delete mode 100644 app/views/layouts/_site_map_left_bar.html.erb diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index eb780d73b..29d145dbb 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -6,12 +6,14 @@ class Admin::ItemsController < ApplicationController before_filter :find_parent_item before_filter :find_snippets, :only => :index before_filter :is_admin? + before_filter :set_current_item def index if params[:item_id] - @page = Item.find(params[:item_id]) + @item = Item.find(params[:item_id]) else - @page = Item.first(:conditions => {:parent_id => nil}) + @item = Item.first(:conditions => {:parent_id => nil}) + @page = @item end end diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb index 88ece1bb9..b1ba5db62 100644 --- a/app/controllers/admin/links_controller.rb +++ b/app/controllers/admin/links_controller.rb @@ -1,18 +1,19 @@ class Admin::LinksController < ApplicationController - layout "admin" + layout "content" + before_filter :authenticate_user! before_filter :find_parent_item before_filter :is_admin? def show - #TODO + @link ||= Link.find(params[:id]) end def new @link = Link.new @link.is_published = true - @link.parent_id = @parent_item.id rescue nil + @link.parent_id = Page.find(params[:parent_id]).id rescue nil end def edit @@ -24,10 +25,17 @@ class Admin::LinksController < ApplicationController @link = Link.new(params[:link]) if @link.save - flash[:notice] = t('admin.create_success_link') - redirect_to admin_items_url( :parent_id => @link.parent_id ) + flash.now[:notice] = t('admin.create_success_link') + respond_to do |format| + format.html { + redirect_to admin_link_url(@link) + } + format.js {} + end else - render :action => "new" + flash.now[:error] = t('admin.create_error_link') + @i18n_variable = @link.i18n_variable + render :action => "new" end end @@ -35,9 +43,16 @@ class Admin::LinksController < ApplicationController @link = Link.find(params[:id]) if @link.update_attributes(params[:link]) - flash[:notice] = t('admin.update_success_link') - redirect_to admin_items_url( :parent_id => @link.parent_id ) + flash.now[:notice] = t('admin.update_success_link') + respond_to do |format| + format.html { + redirect_to admin_link_url(@link) + } + format.js {} + end else + flash.now[:error] = t('admin.update_error_link') + @i18n_variable = @link.i18n_variable render :action => "edit" end end @@ -46,8 +61,16 @@ class Admin::LinksController < ApplicationController @link = Link.find(params[:id]) @link.destroy @link.destroy_i18n_variable - - redirect_to admin_items_url( :parent_id => @link.parent_id ) + end + + def delete + destroy + respond_to do |format| + format.html { + redirect_to admin_items_url + } + format.js {} + end end end diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 1373ea4a7..9be2d1280 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -1,12 +1,21 @@ class Admin::PagesController < ApplicationController layout "content" + before_filter :authenticate_user! before_filter :find_parent_item before_filter :is_admin? + before_filter :set_current_item def show - #TODO + @page = Page.find(params[:id]) + respond_to do |format| + format.html { + @item = @page + render 'admin/items/index' + } + format.js {} + end end def new diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ee8568b58..d24c75058 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base include Parser helper :all - before_filter :set_locale, :set_site + before_filter :set_locale, :set_site # Find the parent for the given item def find_parent_item @@ -51,5 +51,9 @@ class ApplicationController < ActionController::Base @site_in_use_locales = @site.in_use_locales @site_valid_locales = @site.valid_locales end + + def set_current_item + session[:current_page] = params[:id] || @page.id || @link.id || @item.id rescue nil + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f6c77ceb1..e17b54fee 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -49,25 +49,35 @@ module ApplicationHelper end end - def render_node_and_children(node, current_name = 'home') + def render_node_and_children(node) ret = '' if node + case node._type + when 'Page' + dest = admin_page_path(:id => node.id) + when 'Link' + dest = admin_link_path(:id => node.id) + end ret << "
  • " - ret << (link_to node.name, admin_items_path(:item_id => node.id), :remote => true, :class => (current_name.eql?(node.name) ? 'sidebar_no_link' : 'sidebar_link')) - ret << render_children(node) if node.children.size > 0 + ret << (link_to node.name, dest) + ret << ' | ' << (link_to 'new_link', new_admin_link_path(:parent_id => node.id)) if node._type.eql?('Page') + ret << ' | ' << (link_to t(:delete), delete_admin_link_path(node, :authenticity_token => form_authenticity_token), :confirm => t('sure?'), :class => 'delete') if node._type.eql?('Link') + ret << render_children(node) ret << "
  • " end ret.html_safe end def render_children(parent) - ret = '' - ret << "" - ret end end diff --git a/app/views/admin/items/_page.html.erb b/app/views/admin/items/_page.html.erb deleted file mode 100644 index 49e1bea31..000000000 --- a/app/views/admin/items/_page.html.erb +++ /dev/null @@ -1,11 +0,0 @@ - - <%= item.class %> - <%= link_to item.name, admin_items_path(:parent_id => item.id) %> - <%= item.i18n_variable[I18n.locale] rescue nil %> - <%= item.position %> - <%= item.is_published.to_s %> - - <%= link_to t(:show), item.url %> | - <%= link_to t(:edit), edit_admin_page_path(item) %> | - <%= link_to t(:delete), admin_page_path(item), :confirm => t('sure?'), :method => :delete %> - diff --git a/app/views/admin/items/_site_map_left_bar.html.erb b/app/views/admin/items/_site_map_left_bar.html.erb new file mode 100644 index 000000000..e82b4988f --- /dev/null +++ b/app/views/admin/items/_site_map_left_bar.html.erb @@ -0,0 +1 @@ +<%= render_node_and_children(Item.first(:conditions => {:parent_id => nil})) %> \ No newline at end of file diff --git a/app/views/admin/items/index.html.erb b/app/views/admin/items/index.html.erb index fc47481c1..44c814eda 100644 --- a/app/views/admin/items/index.html.erb +++ b/app/views/admin/items/index.html.erb @@ -1,55 +1,24 @@ <% content_for :sidebar do %> - + <% end -%> <% content_for :page_specific_javascript do %> - --> <% end -%> <% content_for :page_specific_css do %> - + --> <% end %> - -<%= render 'layouts/page' %> - - - - <% end if @items %> - -<% end %> - -<%= render :partial => "snippets", :locals => { :snippets => @snippets } %> --> +<%= render "admin/#{@item._type.downcase.pluralize}/show" %> \ No newline at end of file diff --git a/app/views/admin/items/index.js.erb b/app/views/admin/items/index.js.erb index 2338f9476..4b80c217b 100644 --- a/app/views/admin/items/index.js.erb +++ b/app/views/admin/items/index.js.erb @@ -1,9 +1,9 @@ $('#main').empty(); -switch ("<%= escape_javascript(@page._type)%>") { +switch ("<%= escape_javascript(@item._type)%>") { case 'Link': - $('#main').append("TODO: page to edit the link"); + $('#main').append("<%= escape_javascript(render(:partial => 'admin/links/show')) %>"); break; case 'Page': - $('#main').append("<%= escape_javascript(render(:partial => 'layouts/page')) %>"); + $('#main').append("<%= escape_javascript(render(:partial => 'page')) %>"); break; }; \ No newline at end of file diff --git a/app/views/admin/links/_edit.html.erb b/app/views/admin/links/_edit.html.erb new file mode 100644 index 000000000..dc40edf39 --- /dev/null +++ b/app/views/admin/links/_edit.html.erb @@ -0,0 +1,12 @@ +

    <%= t('admin.editing_link') %>

    + +<%= flash_messages %> + +<%= form_for @link, :url => admin_link_path(@link), :html => { :class => 'form' } do |f| %> + + <%= render :partial => "admin/links/form", :locals => { :f => f } %> + +

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

    +<% end %> diff --git a/app/views/admin/links/_new.html.erb b/app/views/admin/links/_new.html.erb new file mode 100644 index 000000000..23cbd5a2f --- /dev/null +++ b/app/views/admin/links/_new.html.erb @@ -0,0 +1,13 @@ +

    <%= t('admin.new_link') %>

    + +<%= flash_messages %> + +<%= form_for @link, :url => admin_links_path, :html => { :class => 'form' } do |f| %> + + <%= render :partial => "admin/links/form", :locals => { :f => f } %> + +

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

    + +<% end %> diff --git a/app/views/admin/links/_show.html.erb b/app/views/admin/links/_show.html.erb new file mode 100644 index 000000000..4b7aa2766 --- /dev/null +++ b/app/views/admin/links/_show.html.erb @@ -0,0 +1,24 @@ +<%= flash_messages %> +

    + <%= t('admin.name') %> + <%= @link.name %> +

    + +<% @site_valid_locales.each do |locale| %> +

    + <%= "#{t('admin.title')} #{locale}" %> + <%= @link.i18n_variable[locale] if @link.i18n_variable %> +

    +<% end %> + +

    + <%= t('admin.is_published') %> + <%= @link.is_published.to_s %> +

    + +

    + <%= t('admin.url') %> + <%= @link.url %> +

    + +<%= link_to t(:edit), edit_admin_link_path(@link) %> \ No newline at end of file diff --git a/app/views/admin/links/create.js.erb b/app/views/admin/links/create.js.erb new file mode 100644 index 000000000..9190c0052 --- /dev/null +++ b/app/views/admin/links/create.js.erb @@ -0,0 +1,2 @@ +$('#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 diff --git a/app/views/admin/links/delete.js.erb b/app/views/admin/links/delete.js.erb new file mode 100644 index 000000000..d8771a3cc --- /dev/null +++ b/app/views/admin/links/delete.js.erb @@ -0,0 +1 @@ +$('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>"); \ No newline at end of file diff --git a/app/views/admin/links/edit.html.erb b/app/views/admin/links/edit.html.erb index 25a65c934..7729178c2 100644 --- a/app/views/admin/links/edit.html.erb +++ b/app/views/admin/links/edit.html.erb @@ -1,10 +1,5 @@ -

    <%= t('admin.editing_link') %>

    +<% content_for :sidebar do %> + +<% end -%> -<%= form_for @link, :url => admin_link_path(@link) do |f| %> - - <%= render :partial => "form", :locals => { :f => f } %> - -

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

    -<% end %> +<%= render 'edit' %> diff --git a/app/views/admin/links/edit.js.erb b/app/views/admin/links/edit.js.erb new file mode 100644 index 000000000..b1292081a --- /dev/null +++ b/app/views/admin/links/edit.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'admin/links/edit')) %>"); \ No newline at end of file diff --git a/app/views/admin/links/new.html.erb b/app/views/admin/links/new.html.erb index 0151abc0e..7d7142b51 100644 --- a/app/views/admin/links/new.html.erb +++ b/app/views/admin/links/new.html.erb @@ -1,11 +1,5 @@ -

    <%= t('admin.new_link') %>

    +<% content_for :sidebar do %> + +<% end -%> -<%= form_for @link, :url => admin_links_path do |f| %> - - <%= render :partial => "form", :locals => { :f => f } %> - -

    - <%= f.submit t(:create) %> <%= link_back %> -

    - -<% end %> +<%= render 'new' %> diff --git a/app/views/admin/links/new.js.erb b/app/views/admin/links/new.js.erb new file mode 100644 index 000000000..fb85026e2 --- /dev/null +++ b/app/views/admin/links/new.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'admin/links/new')) %>"); \ No newline at end of file diff --git a/app/views/admin/links/show.html.erb b/app/views/admin/links/show.html.erb new file mode 100644 index 000000000..bf733a474 --- /dev/null +++ b/app/views/admin/links/show.html.erb @@ -0,0 +1,5 @@ +<% content_for :sidebar do %> + +<% end -%> + +<%= render 'show' %> diff --git a/app/views/admin/links/show.js.erb b/app/views/admin/links/show.js.erb new file mode 100644 index 000000000..f9411af54 --- /dev/null +++ b/app/views/admin/links/show.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'show')) %>"); \ No newline at end of file diff --git a/app/views/admin/links/update.js.erb b/app/views/admin/links/update.js.erb new file mode 100644 index 000000000..0225c28bb --- /dev/null +++ b/app/views/admin/links/update.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'admin/links/show')) %>"); \ No newline at end of file diff --git a/app/views/admin/pages/_show.html.erb b/app/views/admin/pages/_show.html.erb new file mode 100644 index 000000000..22a5f373c --- /dev/null +++ b/app/views/admin/pages/_show.html.erb @@ -0,0 +1 @@ +<%= parse_page(@page).html_safe %> \ No newline at end of file diff --git a/app/views/admin/pages/show.html.erb b/app/views/admin/pages/show.html.erb new file mode 100644 index 000000000..3e1160c7a --- /dev/null +++ b/app/views/admin/pages/show.html.erb @@ -0,0 +1 @@ +<%= render 'show' %> \ No newline at end of file diff --git a/app/views/admin/pages/show.js.erb b/app/views/admin/pages/show.js.erb new file mode 100644 index 000000000..f9411af54 --- /dev/null +++ b/app/views/admin/pages/show.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'show')) %>"); \ No newline at end of file diff --git a/app/views/layouts/_page.html.erb b/app/views/layouts/_page.html.erb deleted file mode 100644 index 221e472b1..000000000 --- a/app/views/layouts/_page.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= parse_page(@page).html_safe if @page._type.eql?('Page') %> \ No newline at end of file diff --git a/app/views/layouts/_site_map_left_bar.html.erb b/app/views/layouts/_site_map_left_bar.html.erb deleted file mode 100644 index c9291de1f..000000000 --- a/app/views/layouts/_site_map_left_bar.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= render_node_and_children(@page) %> \ No newline at end of file diff --git a/app/views/layouts/content.html.erb b/app/views/layouts/content.html.erb index b4acc1e59..7269b7d64 100644 --- a/app/views/layouts/content.html.erb +++ b/app/views/layouts/content.html.erb @@ -2,12 +2,13 @@ - <%= @title %> + <%= @title || 'R4 rescue title' %> <%= yield :page_specific_link %> <%= stylesheet_link_tag "easy", "main", "devise", "content", :media => "screen, projection" %> <%= stylesheet_link_tag "easyprint", :media => "print" %> - <%= javascript_include_tag "jquery", "jquery-ui", "rails", "easy", "application", :cache => 'all' %> + <%= javascript_include_tag "jquery", "jquery-ui", "rails", "easy", :cache => 'all' %> + <%= javascript_include_tag "application", :cache => 'all' %> <%= yield :page_specific_javascript %>