Module: Zafu::Process::Ajax
- Defined in:
- lib/zafu/process/ajax.rb
Instance Method Summary collapse
-
#dom_name(context = @context) ⇒ Object
Return object id (or name).
-
#expand_with_finder(finder) ⇒ Object
This method process a list and handles building the necessary templates for ajax ‘add’.
- #form_url(dom_prefix = node.dom_prefix) ⇒ Object
- #get_unique_name(key, own_id = false) ⇒ Object
-
#need_dom_id? ⇒ Boolean
Return true if we need to insert the dom id for this element.
-
#public_descendants ⇒ Object
Block visibility of descendance with ‘do_list’.
- #r_add ⇒ Object
- #r_add_btn ⇒ Object
-
#r_block ⇒ Object
Store a context as a sub-template that can be used in ajax calls.
- #r_cancel ⇒ Object
- #r_each ⇒ Object
- #r_edit ⇒ Object
- #save_state ⇒ Object
-
#template_url(dom_prefix = node.dom_prefix) ⇒ Object
Unique template_url, ending with dom_id.
-
#unique_name(context = @context) ⇒ Object
Return a different name on each call.
Instance Method Details
#dom_name(context = @context) ⇒ Object
Return object id (or name). Sets name when needed. Context is passed when the parent needs to set dom_name on a child before @context is passed down.
369 370 371 |
# File 'lib/zafu/process/ajax.rb', line 369 def dom_name(context = @context) @dom_name ||= unique_name(context) end |
#expand_with_finder(finder) ⇒ Object
This method process a list and handles building the necessary templates for ajax ‘add’.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/zafu/process/ajax.rb', line 10 def (finder) @context.delete(:make_form) # Do not propagate. return super unless finder[:class].kind_of?(Array) # reset scope @context[:saved_template] = nil # Get the block responsible for rendering each elements in the list each_block = descendant('each') add_block = descendant('add') form_block = descendant('form') || each_block edit_block = descendant('edit') # Should 'edit' and 'add' auto-publish ? publish_after_save = (form_block && form_block.params[:publish]) || (edit_block && edit_block.params[:publish]) # class name for create form if class_name = (add_block && add_block.params[:klass]) || (form_block && form_block.params[:klass]) unless klass = get_class(class_name) return parser_error("Invalid class '#{class_name}'") end else klass = finder[:class].first end if need_ajax?(each_block) #node.dom_prefix = dom_name # We need to build the templates for ajax rendering. # HACK to avoid changing dom_prefix in drag&drop # The bug with wrong prefix between drop inline and ajax result comes from # here (see selenium test drop1 and drop2). node.dom_prefix = dom_name # 1. Render inline # assign [] to var out "<% if (#{var} = #{finder[:method]}) || (#{node}.#{finder[:class].first <= Comment ? "can_comment?" : "can_write?"} && #{var}=[]) %>" # The list is not empty or we have enough rights to add new elements. # New node context. open_node_context(finder, :node => self.node.move_to(var, finder[:class])) do # Pagination count and other contextual variables exist here. tmplt_node = self.node.move_to(var, finder[:class]) tmplt_node.dom_prefix = node.dom_prefix # INLINE ========== out wrap( ( :in_if => false, # 'r_add' needs the form when rendering. Send with :form. :form => form_block, :publish_after_save => publish_after_save, # Do not render the form block directly: let [add] do this. :ignore => ['form'], :klass => klass ) ) # Render 'else' clauses else_clauses = ( :in_if => true, :only => ['elsif', 'else'], :markup => @markup ) # 2. Save 'each' template store_block(each_block, :node => tmplt_node) # 3. Save 'form' template cont = { :saved_template => form_url(tmplt_node.dom_prefix), :klass => klass, :make_form => each_block == form_block, # Used to get parameters like 'publish', 'done', 'after' :add => add_block, :publish_after_save => publish_after_save, :new_keys => {:parent_id => '@node.parent_zip'} } store_block(form_block, cont) end out "<% end %>" else super end # out wrap(expand_with(:node => node.move_to(var, finder[:class]), :in_if => true)) #query = opts[:query] # # #if need_ajax? # new_dom_scope # # ajax, build template. We could merge the following code with 'r_block'. # # # FORM ============ # if each_block != form_block # form = expand_block(form_block, :klass => klass, :add=>add_block, :publish_after_save => publish_after_save, :saved_template => true) # else # form = expand_block(form_block, :klass => klass, :add=>add_block, :make_form=>true, :publish_after_save => publish_after_save, :saved_template => true) # end # out helper.save_erb_to_url(form, form_url) #else # # no form, render, edit and add are not ajax # if descendant('add') || descendant('add_document') # out "<% if (#{list_var} = #{list_finder}) || (#{node}.#{node.will_be?(Comment) ? "can_comment?" : "can_write?"} && #{list_var}=[]) %>" # elsif list_finder != 'nil' # out "<% if #{list_var} = #{list_finder} %>" # else # out "<% if nil %>" # end # # # out render_html_tag(expand_with(:list=>list_var, :in_if => false)) # out expand_with(:in_if=>true, :only=>['elsif', 'else'], :html_tag => @html_tag, :html_tag_params => @html_tag_params) # out "<% end %>" #end end |
#form_url(dom_prefix = node.dom_prefix) ⇒ Object
362 363 364 |
# File 'lib/zafu/process/ajax.rb', line 362 def form_url(dom_prefix = node.dom_prefix) template_url(dom_prefix) + '_form' end |
#get_unique_name(key, own_id = false) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/zafu/process/ajax.rb', line 379 def get_unique_name(key, own_id = false) @next_name_index ||= {} if @next_name_index[key] @next_name_index[key] += 1 key + @next_name_index[key].to_s elsif own_id @next_name_index[key] = 0 key else @next_name_index[key] = 1 key + '1' end end |
#need_dom_id? ⇒ Boolean
Return true if we need to insert the dom id for this element.
353 354 355 |
# File 'lib/zafu/process/ajax.rb', line 353 def need_dom_id? @context[:form] || child['unlink'] || (single_child_method && single_child_method == child['drop']) end |
#public_descendants ⇒ Object
Block visibility of descendance with ‘do_list’.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/zafu/process/ajax.rb', line 334 def public_descendants all = super if ['context', 'each', 'block'].include?(method) # do not propagate 'form',etc up all.reject do |k,v| # FIXME: Zena leakage ['form','unlink', 'count'].include?(k) end elsif ['if', 'case'].include?(method) || (method =~ /^[A-Z]/) # conditional all.reject do |k,v| ['else', 'elsif', 'when'].include?(k) end else all end end |
#r_add ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/zafu/process/ajax.rb', line 209 def r_add return parser_error("Should not be called from within 'each'") if parent.method == 'each' return parser_error("Should not be called outside list context") unless node.list_context? return '' if @context[:make_form] if node.will_be?(Comment) out "<% if #{node.up(Node)}.can_comment? %>" else out "<% if #{node.up(Node)}.can_write? %>" end unless descendant('add_btn') # Add a descendant between self and blocks. ==> add( add_btn(blocks) ) blocks = @blocks.dup @blocks = [] add_btn = make(:void, :method => 'add_btn', :params => @params.dup, :text => '') add_btn.blocks = blocks @all_descendants = nil end if @context[:form] # ajax add @markup.set_id("#{node.dom_prefix}_add") @markup.append_param(:class, 'btn_add') if @params[:focus] focus = "$(\"#{node.dom_prefix}_#{@params[:focus]}\").focus();" else focus = "$(\"#{node.dom_prefix}_form_t\").focusFirstElement();" end # Expand 'add' block out wrap("#{(:onclick=>"[\"#{node.dom_prefix}_add\", \"#{node.dom_prefix}_form\"].each(Element.toggle);#{focus}return false;")}") klass = @context[:klass] || node.single_class # New object to render form. new_node = node.move_to("#{var}_new", klass, :new_keys => {}) if new_node.will_be?(Node) # FIXME: BUG if we set <r:form klass='Post'/> the user cannot select class with menu... if @context[:saved_template] parent_id = "#{node}.parent_zip" else parent_id = "#{node(Node)}.zip" end new_node.opts[:new_keys]['parent_id'] = parent_id # FIXME: inspect '@context[:form]' to see if it contains v_klass ? out "<% if #{new_node} = secure(Node) { Node.new_node('class' => '#{new_node.klass}', 'parent_id' => #{parent_id}) } %>" # if node.will_be?(Node) # # Nested contexts: # # 1. @node # # 2. var1 = @node.children # # 3. var1_new = Node.new # if node.opts[:new_record] || @context[:saved_template] # if @context[:saved_template] || !@context[:in_add] # # TODO: we should not add parent_id on every saved_template. Why is this needed ? # parent_id = "#{node}.parent_zip" # else # # We are in var2_new # parent_id = "#{node.up(Node)}.zip" # end # # hidden_fields['node[parent_id]'] = "<%= #{parent_id} %>" # end # els else out "<% if #{new_node} = #{new_node.class_name}.new %>" end form_block = @context[:form] # Expand (inline) 'form' block out (form_block, # Needed in form to be able to return the result :template_url => template_url(node.dom_prefix), # Used to avoid wrong dom_id in hidden form. Should not be # necessary but it's hard to fix when node changes a lot (drop in add). :dom_prefix => node.dom_prefix, # Used to add needed hidden fields in form :in_add => true, # Used to get parameters like 'publish' or 'klass' :add => self, # Transform 'each' block into a form :make_form => form_block.method == 'each', # Node context = new node :node => new_node ) out "<% end %>" else # no ajax @markup.append_param(:class, 'btn_add') if @markup.tag out wrap() end out "<% end %>" end |
#r_add_btn ⇒ Object
310 311 312 313 314 |
# File 'lib/zafu/process/ajax.rb', line 310 def r_add_btn default = node.will_be?(Comment) ? _("btn_add_comment") : _("btn_add") out "<a href='#' onclick='#{@context[:onclick]}'>#{text_for_link(default)}</a>" end |
#r_block ⇒ Object
Store a context as a sub-template that can be used in ajax calls
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/zafu/process/ajax.rb', line 135 def r_block if parent.method == 'each' && @method == parent.single_child_method # Block stored in 'each', do nothing # What happens when this is used as remote target ? return end # Since we are using ajax, we will need this object to have an ID set. node.dom_prefix = dom_name @markup.done = false if @context[:block] == self # Storing template (called from within store_block) # Set id with the template's node context (<%= @node.zip %>). @markup.set_id(node.dom_id(:list => false)) else # 1. store template # will wrap with @markup store_block(self, :ajax_action => 'show') if edit_block = descendant('edit') form_block = descendant('form') || self publish_after_save = form_block.params[:publish] || (edit_block && edit_block.params[:publish]) # 2. store form cont = { :saved_template => form_url(node.dom_prefix), :make_form => self == form_block, :publish_after_save => publish_after_save, :ajax_action => 'edit', } store_block(form_block, cont) end # 3. render # Set id with the current node context (<%= var1.zip %>). @markup.set_id(node.dom_id(:list => false)) out end end |
#r_cancel ⇒ Object
205 206 207 |
# File 'lib/zafu/process/ajax.rb', line 205 def r_cancel r_edit end |
#r_each ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/zafu/process/ajax.rb', line 316 def r_each if @context[:saved_template] # render to start a saved template node.saved_dom_id = "\#{ndom_id(#{node})}" node.dom_prefix ||= dom_name node.propagate_dom_scope! @markup.set_id(node.dom_id) = @markup.set_param(:style, [:style]) if [:style] out wrap() else super end end |
#r_edit ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/zafu/process/ajax.rb', line 182 def r_edit # ajax if cancel = @context[:form_cancel] # cancel button out cancel else # edit button # TODO: show 'reply' instead of 'edit' in comments if visitor != author block = ancestor(%w{each block}) # These parameters are detected by r_block and set in form. # removed so they do not polute link @params.delete(:publish) @params.delete(:cancel) @params.delete(:tcancel) link = wrap(make_link(:default_text => _('edit'), :update => block, :action => 'edit')) out "<% if #{node}.can_write? %>#{link}<% end %>" end end |
#save_state ⇒ Object
4 5 6 |
# File 'lib/zafu/process/ajax.rb', line 4 def save_state super.merge(:@markup => @markup.dup) end |
#template_url(dom_prefix = node.dom_prefix) ⇒ Object
Unique template_url, ending with dom_id
358 359 360 |
# File 'lib/zafu/process/ajax.rb', line 358 def template_url(dom_prefix = node.dom_prefix) "#{root.[:root]}/#{dom_prefix}" end |
#unique_name(context = @context) ⇒ Object
Return a different name on each call
374 375 376 377 |
# File 'lib/zafu/process/ajax.rb', line 374 def unique_name(context = @context) base = @name || context[:name] || 'list' root.get_unique_name(base, base == @name).gsub(/[^\d\w\/]/,'_') end |