From a6b9efca95e08ace90d6b69fd11302637f1c77d2 Mon Sep 17 00:00:00 2001 From: Gaspard Bucher Date: Thu, 23 Oct 2008 14:57:48 +0200 Subject: [PATCH] In-place editing of tables works (basic tablekit functionality). Need to implement add/remove/move rows/columns. Refs [#228]. --- app/controllers/application.rb | 14 +++++++++++++- app/controllers/nodes_controller.rb | 29 +++++++++++++++++++++++++++++ app/helpers/application_helper.rb | 15 +++------------ app/views/nodes/_table.rhtml | 8 ++++---- config/routes.rb | 3 ++- lib/parser/lib/rules/zazen.rb | 2 +- lib/parser/lib/rules/zena.rb | 2 +- public/javascripts/tablekit.js | 9 ++++++--- 8 files changed, 59 insertions(+), 23 deletions(-) diff --git a/app/controllers/application.rb b/app/controllers/application.rb index f3ceb282..c52f448c 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base init_gettext 'zena' helper_method :prefix, :zen_path, :zen_url, :data_path, :node_url, :notes, :error_messages_for, :render_errors, :processing_error - helper_method :get_template_text, :template_url_for_asset, :save_erb_to_url, :lang, :visitor, :fullpath_from_template_url, :eval_parameters_from_template_url, :format_date + helper_method :get_template_text, :template_url_for_asset, :save_erb_to_url, :lang, :visitor, :fullpath_from_template_url, :eval_parameters_from_template_url, :format_date, :get_table_from_json before_filter :set_lang before_filter :authorize before_filter :check_lang @@ -870,6 +870,18 @@ def path_params end res end + + # Build a tabular content from a node's attribute + def get_table_from_json(node, attribute) + text = Node.zafu_attribute(node, attribute) + if text.blank? + table = [{"type"=>"table"},[["title"],["value"]]] + else + table = JSON.parse(text) + end + raise JSON::ParserError unless table.kind_of?(Array) && table.size == 2 && table[0].kind_of?(Hash) && table[0]['type'] == 'table' && table[1].kind_of?(Array) + table + end end load_patches_from_plugins \ No newline at end of file diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index 67492464..3d808baa 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb @@ -71,6 +71,35 @@ def zafu end end + # Ajax table editor + def table_edit + # get table + table = get_table_from_json(@node, params[:attr]) + # get row/cell + table_data = table[1] + + if row = table_data[params[:row].to_i] + if cell = row[params[:cell].to_i] + if cell != params[:value] + row[params[:cell].to_i] = params[:value] + @node.update_attributes(params[:attr] => table.to_json) + end + else + puts "NEW COLUMN: #{table.inspect}" + # new column + end + else + puts "NEW ROW: #{table.inspect}" + # new row + end + + respond_to do |format| + format.html { render :inline => @node.errors.empty? ? params[:value] : error_messages_for(@node) } + end + rescue JSON::ParserError + render :inline => _('could not save value (bad attribute)') + end + # This method is called when an element is dropped on a node. def drop set = params[:set] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c34ecb21..ce1e49df 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -563,19 +563,10 @@ def make_table(opts) prefix = suffix = "" end - unless table - # get attribute content - attribute = "d_#{attribute}" unless ['v_', 'd_'].include?(attribute[0..1]) - text = Node.zafu_attribute(node, attribute) - if text.blank? - table = [{"type"=>"table"},[["title"],["value"]]] - else - table = JSON.parse(text) - end - end - raise JSON::ParserError unless table.kind_of?(Array) && table.size == 2 && table[0].kind_of?(Hash) && table[0]['type'] == 'table' && table[1].kind_of?(Array) + attribute = "d_#{attribute}" unless ['v_', 'd_'].include?(attribute[0..1]) + table ||= get_table_from_json(node, attribute) - prefix + render_to_string( :partial=>'nodes/table', :locals=>{:table=>table}) + suffix + prefix + render_to_string( :partial=>'nodes/table', :locals=>{:table=>table, :node=>node, :attribute=>attribute}) + suffix rescue JSON::ParserError "could not build table from text" end diff --git a/app/views/nodes/_table.rhtml b/app/views/nodes/_table.rhtml index 53955e26..4b3e72f5 100644 --- a/app/views/nodes/_table.rhtml +++ b/app/views/nodes/_table.rhtml @@ -1,9 +1,9 @@ - -
+ + <% table[1][0].each do |heading| -%> - + <% end -%> - + <% table[1][1..-1].each do |row| -%> <% row.each do |td| -%> diff --git a/config/routes.rb b/config/routes.rb index 9eb4aa9c..bdee2169 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,7 +19,8 @@ :collection => { :asearch => :get, :search => :get }, :member => { :import => :post, :export => :get, :save_text => :put, :order => :any, :clear_order => :any, - :zafu => :get, :drop => :put, :attribute => :get } do |nodes| + :zafu => :get, :drop => :put, :attribute => :get, + :table_edit => :post } do |nodes| nodes.resources :versions, :name_prefix => nil, :member => { :edit => :get, diff --git a/lib/parser/lib/rules/zazen.rb b/lib/parser/lib/rules/zazen.rb index 6fdd058f..d5443d8e 100644 --- a/lib/parser/lib/rules/zazen.rb +++ b/lib/parser/lib/rules/zazen.rb @@ -330,7 +330,7 @@ def scan_pipe node = @helper.find_node_by_pseudo(id, @context[:node]) id = node.pseudo_id(@context[:node], @translate_ids) if node end - store "|#{style}#{id}.#{attribute}#{title}|" + store "|#{style}#{id == '' ? '' : "#{id}."}#{attribute}#{title}|" else node = id == '' ? @context[:node] : @helper.find_node_by_pseudo(id, @context[:node]) store @helper.make_table(:style=>style, :node=>node, :attribute=>attribute, :title=>title) diff --git a/lib/parser/lib/rules/zena.rb b/lib/parser/lib/rules/zena.rb index 0490150b..1f87430f 100644 --- a/lib/parser/lib/rules/zena.rb +++ b/lib/parser/lib/rules/zena.rb @@ -1652,7 +1652,7 @@ def r_date def r_javascripts if @params[:list] == 'all' || @params[:list].nil? - list = %w{ prototype effects zena } + list = %w{ prototype effects tablekit zena } else list = @params[:list].split(',').map{|e| e.strip} end diff --git a/public/javascripts/tablekit.js b/public/javascripts/tablekit.js index 1eeb397f..a3157b38 100755 --- a/public/javascripts/tablekit.js +++ b/public/javascripts/tablekit.js @@ -776,7 +776,7 @@ TableKit.Editable = { }, getCellEditor : function(cell, table, head) { var head = head ? head : $(TableKit.getHeaderCells(table, cell)[TableKit.getCellIndex(cell)]); - var ftype = TableKit.Editable.types['text-input']; + var ftype = TableKit.Editable.types['multi-line-input']; if(head.id && TableKit.Editable.types[head.id]) { ftype = TableKit.Editable.types[head.id]; } else { @@ -873,8 +873,11 @@ TableKit.Editable.CellEditor.prototype = { var head = $(TableKit.getHeaderCells(null, cell)[TableKit.getCellIndex(cell)]); var row = cell.up('tr'); var table = cell.up('table'); - var s = '&row=' + (TableKit.getRowIndex(row)+1) + '&cell=' + (TableKit.getCellIndex(cell)+1) + '&id=' + row.id + '&field=' + head.id + '&' + Form.serialize(form); - this.ajax = new Ajax.Updater(cell, op.ajaxURI || TableKit.option('editAjaxURI', table.id)[0], Object.extend(op.ajaxOptions || TableKit.option('editAjaxOptions', table.id)[0], { + var dom_id = table.id; + var attr = dom_id.replace(/^\d+_/,''); + var node_id = dom_id.match(/^\d+/); + var s = '&row=' + TableKit.getRowIndex(row) + '&cell=' + TableKit.getCellIndex(cell) + '&attr=' + attr + '&' + Form.serialize(form); + this.ajax = new Ajax.Updater(cell, '/nodes/' + node_id + '/table_edit', Object.extend(op.ajaxOptions || TableKit.option('editAjaxOptions', table.id)[0], { postBody : s, onComplete : function() { var data = TableKit.getCellData(cell);
<%= heading %><%= heading %>