Skip to content

Commit

Permalink
In-place editing of tables works (basic tablekit functionality). Need…
Browse files Browse the repository at this point in the history
… to implement add/remove/move rows/columns. Refs [#228].
  • Loading branch information
gaspard committed Oct 23, 2008
1 parent a60e834 commit a6b9efc
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 23 deletions.
14 changes: 13 additions & 1 deletion app/controllers/application.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions app/controllers/nodes_controller.rb
Expand Up @@ -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]
Expand Down
15 changes: 3 additions & 12 deletions app/helpers/application_helper.rb
Expand Up @@ -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
"<span class='unknownLink'>could not build table from text</span>"
end
Expand Down
8 changes: 4 additions & 4 deletions app/views/nodes/_table.rhtml
@@ -1,9 +1,9 @@
<table>
<th>
<table id='<%= node.zip %>_<%= attribute %>' class='editable'>
<tr>
<% table[1][0].each do |heading| -%>
<td><%= heading %></td>
<th><%= heading %></th>
<% end -%>
</th>
</tr>
<% table[1][1..-1].each do |row| -%>
<tr>
<% row.each do |td| -%>
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/lib/rules/zazen.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/lib/rules/zena.rb
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions public/javascripts/tablekit.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a6b9efc

Please sign in to comment.