Changeset 1147

Show
Ignore:
Timestamp:
2008-09-15 13:51:17 (4 months ago)
Author:
gaspard
Message:

Improved [drop]: the drop element can now alter query parameters. This can be used to preload forms and ask for validation, with comments.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/nodes_controller.rb

    r1127 r1147  
    7777        # swap (a way to preview content by drag & drop) 
    7878        @node = other 
    79       elsif params[:change] == 'dropped' 
     79      elsif params[:change] == 'receiver' 
     80        attributes[:copy] = other 
     81        @node.update_attributes_with_transformation(attributes) 
     82        if !@node.errors.empty? 
     83          @errors = @node.errors 
     84        end 
     85      else 
    8086        attributes[:copy] = @node 
    8187        other.update_attributes_with_transformation(attributes) 
     
    8389          @errors = other.errors 
    8490        end 
    85       else 
    86         attributes[:copy] = other 
    87         @node.update_attributes_with_transformation(attributes) 
    88         if !@node.errors.empty? 
    89           @errors = @node.errors 
    90         end 
    91       end 
     91      end   
     92    elsif p = params[:params] 
     93      params.merge!(other.replace_attributes_in_values(p)) 
    9294    end 
    9395     
  • trunk/app/models/node.rb

    r1131 r1147  
    132132                     :section_zip, :skin, :ref_lang, :fullpath, :rootpath, :position, :publish_from, :max_status, :rgroup_id,  
    133133                     :wgroup_id, :pgroup_id, :basepath, :custom_base, :klass, :zip, :score, :comments_count, :l_status, :l_comment, 
    134                      :custom_a, :custom_b, :title, :text 
    135   safe_attribute     :m_text, :m_title, :m_author    
     134                     :custom_a, :custom_b, :title, :text, 
     135                     :m_text, :m_title, :m_author    
    136136  zafu_context       :author => "Contact", :parent => "Node",  
    137137                     :project => "Project", :section => "Section",  
     
    673673        end 
    674674      end 
    675  
     675       
    676676      res 
    677677    end 
     
    731731  end 
    732732   
    733   # Return true if the attribute can be read (is zafu_readable) 
     733  # Return true if the attribute can be read. This is not the same as zafu_readable? as some 
     734  # attributes can be read but should not be shown ('id' or 'file' for example). 
    734735  def safe_attribute?(att) 
    735736    # FIXME: SECURITY: is there any risk here ? (k can be anything) 
  • trunk/lib/has_relations.rb

    r1128 r1147  
    224224              # invalid relation 
    225225              if mode == '=' 
    226                 errors.add(role, "invalid relation") unless args[0].empty
     226                errors.add(role, "invalid relation") unless args[0].blank
    227227                return args[0] 
    228228              else 
  • trunk/lib/parser/lib/rules/zena.rb

    r1138 r1147  
    278278      elsif @params[:attr] 
    279279        attribute_method = node_attribute(@params[:attr], :else=>@params[:else], :default=>@params[:default]) 
     280      elsif p = @params[:param] 
     281        return "<%= params[#{p.to_sym.inspect}] %>" 
    280282      elsif @params[:date] 
    281283        # date can be any attribute v_created_at or updated_at etc. 
     
    482484 
    483485            context_bak = @context.dup # avoid side effects when rendering the same block 
    484             template    = expand_block(self, :block=>self, :list=>false, :saved_template=>true, :ignore =>['form']) 
     486            ignore_list = @method == 'block' ? ['form'] : [] # do not show the form in the normal template of a block 
     487            template    = expand_block(self, :block=>self, :list=>false, :saved_template=>true, :ignore => ignore_list) 
    485488            @context    = context_bak 
    486489            @result     = '' 
     
    523526      end 
    524527      return parser_error("cannot use 's' as key (used by start_node)") if @params[:key] == 's' 
    525       out "<%= form_remote_tag(:url => zafu_node_path(#{node_id}), :method => :get, :html => {:id => \"#{dom_id}_f\"}) %><div class='hidden'><input type='hidden' name='t_url' value='#{block.template_url}'/><input type='hidden' name='dom_id' value='#{block.erb_dom_id}'/>#{start_node_input}</div><div class='wrapper'>" 
     528      out "<%= form_remote_tag(:url => zafu_node_path(#{node_id}), :method => :get, :html => {:id => \"#{dom_id}_f\"}) %><div class='hidden'><input type='hidden' name='t_url' value='#{block.template_url}'/><input type='hidden' name='dom_id' value='#{block.erb_dom_id}'/>#{start_node_s_param(:input)}</div><div class='wrapper'>" 
    526529      if @blocks == [] 
    527530        out "<input type='text' name='#{@params[:key] || 'f'}' value='<%= params[#{(@params[:key] || 'f').to_sym.inspect}] %>'/>" 
     
    12371240          next if [:hover, :change, :done].include?(k) 
    12381241          value, static = parse_attributes_in_value(v, :erb => false, :skip_node_attributes => true) 
    1239           url_params << "node[#{k}]=#{CGI.escape(value)}" 
     1242          key = change == 'params' ? "params[#{k}]" : "node[#{k}]" 
     1243          url_params << "#{key}=#{CGI.escape(value)}" 
    12401244        end 
    12411245        return parser_error("missing parameters to set values") if url_params == [] 
    12421246      end 
    12431247     
    1244       url_params << "change=dropped" if change == 'dropped
     1248      url_params << "change=#{change}" if change == 'receiver
    12451249      url_params << "t_url=#{CGI.escape(template_url)}" 
    12461250      url_params << "dom_id=#{erb_dom_id}" 
     1251      url_params << start_node_s_param(:erb) 
    12471252      url_params << "done=#{CGI.escape(@params[:done])}" if @params[:done] 
    12481253     
     
    21742179    def node(klass = self.node_class) 
    21752180      if klass == self.node_class 
    2176         @context[:saved_template] ? "@#{base_class.to_s.underscore}" : (@context[:node] || '@node') 
     2181        (@context[:saved_template] && @context[:main_node]) ? "@#{base_class.to_s.underscore}" : (@context[:node] || '@node') 
    21772182      elsif klass == Node 
    21782183        @context[:previous_node] || '@node' 
     
    23702375    # DOM id for the current context 
    23712376    def dom_id(suffix='') 
    2372       return "\#{dom_id(#{node})}" if @context && @context[:saved_template] 
     2377      return "\#{dom_id(#{node})}" if @context && (@context[:saved_template] && @context[:main_node]) 
    23732378      if @context && scope_node = @context[:scope_node] 
    23742379        res = "#{dom_prefix}_\#{#{scope_node}.zip}" 
     
    23952400     
    23962401    def erb_dom_id(suffix='') 
    2397       return "<%= dom_id(#{node}) %>" if @context && @context[:saved_template] 
     2402      return "<%= dom_id(#{node}) %>" if @context && (@context[:saved_template] && @context[:main_node]) 
    23982403      if @context && scope_node = @context[:scope_node] 
    23992404        res = "#{dom_prefix}_<%= #{scope_node}.zip %>" 
     
    24442449    # use our own scope 
    24452450    def clear_dom_scope 
    2446       @context.delete(:dom_prefix)     # should not propagate 
    24472451      @context.delete(:make_form)      # should not propagate 
    2448       @context.delete(:saved_template) # should not propagate 
     2452      @context.delete(:main_node)      # should not propagate 
    24492453    end 
    24502454     
     
    24522456    def new_dom_scope 
    24532457      clear_dom_scope 
     2458      @context.delete(:saved_template) # should not propagate on fresh template 
     2459      @context.delete(:dom_prefix)     # should not propagate on fresh template 
     2460      @context[:main_node]  = true     # the current context will be rendered with a fresh '@node' 
    24542461      @context[:dom_prefix] = self.dom_prefix 
    24552462    end 
     
    28472854      url_params << "link_id=\#{#{node}.link_id}" if @context[:need_link_id] && node_kind_of?(Node) 
    28482855      url_params << "node[v_status]=#{Zena::Status[:pub]}" if @params[:publish] 
    2849       url_params << start_node_input(false
     2856      url_params << start_node_s_param(:string
    28502857       
    28512858      res = '' 
     
    30833090      return '' if attribute == 'parent_id' # set with 'r_form' 
    30843091       
    3085       if @context[:in_add] 
    3086         value = '' 
    3087       else 
    3088         value = attribute ? "<%= #{node_attribute(attribute)} %>" : "" 
     3092      if @blocks == [] || @blocks == [''] 
     3093        if @context[:in_add] 
     3094          value = '' 
     3095        else 
     3096          value = attribute ? "<%= #{node_attribute(attribute)} %>" : "" 
     3097        end 
     3098      else 
     3099        value = expand_with 
    30893100      end 
    30903101      html_id = @context[:dom_prefix] ? " id='#{erb_dom_id}_#{attribute}'" : '' 
     
    31183129    end 
    31193130     
    3120     def start_node_input(erb = true
    3121       if erb 
     3131    def start_node_s_param(type = :input
     3132      if type == :input 
    31223133        "<input type='hidden' name='s' value='<%= params[:s] || @node[:zip] %>'/>" 
     3134      elsif type == :erb 
     3135        "s=<%= params[:s] || @node[:zip] %>" 
    31233136      else 
    31243137        "s=\#{params[:s] || @node[:zip]}" 
  • trunk/test/fixtures/files/Node-test.zafu

    r1098 r1147  
    101101<r:images in='site' do='each' draggable='true' do='img' mode='tiny'/> 
    102102 
     103<h3>drop set relation</h3> 
     104 
    103105<ul do='tags in site'> 
    104106  <li style='border:1px solid #400; min_width:100px; min_height:100px;' do='each' do='drop' set='set_tag'> 
     
    108110</ul> 
    109111 
     112<h3>drop set attributes</h3> 
     113 
    110114<ul do='projects in site'> 
    111115  <li style='border:1px solid #400; min_width:100px; min_height:100px;' do='each'><r:drop d_foo='I am an icon for [v_title]' icon_for_id='[id]'><b do='title' actions='all'/><r:icon where='1'><r:unlink do='img' mode='pv'/> <r:show attr='d_foo'/> </r:icon></r:drop> 
    112116</li> 
    113117</ul> 
     118 
     119<h3>drop reload with param</h3> 
     120 
     121<div style='border:1px solid #400; min_width:100px; min_height:100px;' do='drop' change='params' d='[id]' do='if' test='param:d'> 
     122  <r:node where='id eq param:d' in='site'> 
     123    Change icon to <r:img mode='tiny'/> ? 
     124    <r:main store='icon' do='form'> 
     125      <r:input type='hidden' name='icon_id' set_value='[icon.id]'/> 
     126      <r:input type='hidden' name='m_title' set_value='icon changed to [icon.name]'/> 
     127      <r:textarea name='m_text'/> 
     128      <input type='submit'/> 
     129    </r:main> 
     130  </r:node> 
     131  <r:else> 
     132    <r:icon do='img' mode='pv'/> 
     133    please drop a node to change the icon 
     134    <ul do='comments'> 
     135    <li do='each'><b do='[title]'/> <b do='[author_name]'/> 
     136    <div do='{text}'/> 
     137    </li> 
     138    </ul></r:else> 
     139</div> 
    114140 
    115141<hr/> 
  • trunk/test/helpers/zena_parser/ajax.yml

    r1137 r1147  
    182182      </li> 
    183183    </ul> 
    184   'each/in/en/each/dom/id/list1.erb': "/list2_..@node.zip._..var1.zip./" 
     184  'each/in/en/each/dom/id/list1.erb': "/list1_..@node.zip._..var1.zip./" 
    185185 
    186186start_id: 
     
    188188  res: "/<input name='s' type='hidden' value='22'/>/" 
    189189 
     190drop_param: 
     191  src: "<r:drop change='params' d='[id]'><r:show param='d'/></r:drop>" 
     192  tem: "/<div class='drop' id='list1'><%= params\[:d\] %>.*drop\?params\[d\]=%5Bid%5D/" 
     193 
    190194swap_in_each: 
    191195  src: "<r:pages><li do='each'><span do='swap' attr='d_status' states='todo,done,alert'/></li></r:pages>" 
  • trunk/test/helpers/zena_parser/basic.yml

    r1138 r1147  
    8181  src: "<b do='[d_assigned]' edit_preview='true'/>" 
    8282  res: "<b id='d_assigned22'>gaspard</b>" 
     83 
     84show_param: 
     85  context: 
     86    d: 'hello ladies' 
     87  src: "<i do='show' param='d'/>" 
     88  tem: "<i><%= params[:d] %></i>" 
     89  res: "<i>hello ladies</i>" 
    8390   
    8491title_in_version_context: 
     
    909916  res: "<input name='node[c_first_name]' type='text' value='Solenopsis'/>" 
    910917 
     918textarea: 
     919  context: 
     920    node: 'ant' 
     921  src: "<r:textarea name='c_first_name'/>" 
     922  tem: "<textarea name='node[c_first_name]'><%= @node.c_zafu_read(\"first_name\") %></textarea>" 
     923  res: "<textarea name='node[c_first_name]'>Solenopsis</textarea>" 
     924 
     925textarea_with_blocks: 
     926  context: 
     927    node: 'ant' 
     928  src: "<r:textarea name='c_first_name'>Sir <r:show attr='c_first_name'/></textarea>" 
     929  tem: "<textarea name='node[c_first_name]'>Sir <%= @node.c_zafu_read(\"first_name\") %></textarea></textarea>" 
     930  res: "<textarea name='node[c_first_name]'>Sir Solenopsis</textarea></textarea>" 
     931   
    911932input_new: 
    912933  context: 
  • trunk/test/helpers/zena_parser_test.rb

    r1061 r1147  
    3434    $_test_site = context.delete('site') || 'zena' 
    3535    @request.host = sites_host($_test_site) 
    36     params[:user_id] = users_id(context['visitor'].to_sym) 
    37     params[:node_id] = nodes_id(context['node'].to_sym) 
    38     params[:prefix]  = context['lang'] 
    39     params[:date]    = context['ref_date'] ? context['ref_date'].to_s : nil 
     36    params[:user_id] = users_id(context.delete('visitor').to_sym) 
     37    params[:node_id] = nodes_id(context.delete('node').to_sym) 
     38    params[:prefix]  = context.delete('lang') 
     39    params[:date]    = context['ref_date'] ? context.delete('ref_date').to_s : nil 
    4040    params[:url] = "/#{test.to_s.gsub('_', '/')}" 
     41    params.merge!(context) # merge the rest of the context as query parameters 
    4142    TestController.templates = @@test_strings[file] 
    4243    if src