Module: Zena::Use::Ajax::ZafuMethods

Defined in:
lib/zena/use/ajax.rb

Overview

ViewMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



262
263
264
265
266
# File 'lib/zena/use/ajax.rb', line 262

def self.included(base)
  # TODO: move process_toggle in 'before_wrap' callback so that 'node' is properly set.
  base.before_process :process_drag, :process_toggle
  base.before_wrap    :wrap_with_drag
end

Instance Method Details

#process_dragObject



268
269
270
# File 'lib/zena/use/ajax.rb', line 268

def process_drag
  @drag_param = @params.delete(:draggable)
end

#process_toggleObject



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/zena/use/ajax.rb', line 455

def process_toggle
  return unless role = @params.delete(:toggle)

  unless finder = @params.delete(:for)
    out parser_error("missing 'for' parameter")
    return
  end

  finder = RubyLess.translate(self, finder)
  unless finder.klass <= Node
    out parser_error("Invalid class 'for' parameter: #{finder.klass}")
    return
  end

  node = pre_filter_node

  if dom_id = @markup.params[:id]
    # we do not mess with it
  else
    node.dom_prefix = dom_name
    markup.set_id(node.dom_id)
    dom_id = node.dom_id(:erb => false)
  end

  markup.tag ||= 'div'

  markup.append_param(:class, 'toggle')
  
  opts = []
  if arity = @params.delete(:arity)
    opts << ":arity => #{RubyLess.translate_string(self, arity)}"
  end
  
  if js = @params.delete(:js)
    opts << ":js => #{RubyLess.translate_string(self, js)}"
  end
  var = root.get_unique_name('tog')
  markup.pre_wrap[:toggle] = "<% add_toggle_id(\"#{dom_id}\", #{var.inspect}, #{RubyLess.translate_string(self, role)},{#{opts.join(', ')}}) { #{finder} } %>"
end

#r_ajax?Boolean

Returns:

  • (Boolean)


564
565
566
# File 'lib/zena/use/ajax.rb', line 564

def r_ajax?
  r_if(RubyLess.translate(self, 'ajax?'))
end

#r_ajsObject

Only execute javascript on ajax.



558
559
560
561
562
# File 'lib/zena/use/ajax.rb', line 558

def r_ajs
  out "<% if params[:s] %>"
  r_js
  out "<% end %>"
end

#r_dropObject

Create a drop block.



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/zena/use/ajax.rb', line 383

def r_drop
  if parent.method == 'each' && @method == parent.single_child_method
    # We reuse the 'each' block.
    target = parent
  else
    # Avoid altering parent node
    @context[:node] = node.dup

    node.dom_prefix = dom_name
    target = self
  end

  node = target.node
  markup = target.markup

  markup.tag ||= 'div'

  dom_id, dom_prefix = get_dom_id(target)

  markup.append_param(:class, 'drop') unless markup.params[:class] =~ /drop/

  if hover  = @params.delete(:hover)
    query_params = ", :hover => %{#{hover}}"
  else
    query_params = ""
  end

  if role = @params.delete(:set) || @params.delete(:add)
    @params["node[#{role}_id]"] = "'\#{id}'"
  end

  url_params = {}
  @params.each do |k,v|
    case k
    when :change, :done
      # Force string interpolation
      url_params[k] = "%{#{v}}"
    else
      url_params[k] = v
    end
  end

  query_params << ", :url => #{make_href(target, :action => 'drop', :query_params => url_params)}"
  markup.pre_wrap[:drop] = "<% add_drop_id(#{dom_id}#{query_params}) %>"
  r_block
end

#r_filterObject

Display an input field to filter a remote block.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/zena/use/ajax.rb', line 335

def r_filter
  if upd = @params[:update]
    return unless block = find_target(upd)
  else
    return parser_error("missing 'block' in same parent") unless parent && block = parent.descendant('block')
    if block.name.blank?
      block.name ||= unique_name
    end
    upd = block.name
  end

  return parser_error("cannot use 's' as key (used by start_node)") if @params[:key] == 's'

  # Move up in case we are in a list.
  if self.node.list_context?
    base_node = self.node.up(Node)
  else
    base_node = self.node
  end
  
  # Do not alter current node, create our own
  with_context(:node => base_node.dup) do
    node.dom_prefix = dom_name
    dom_id = node.dom_id(:erb => false)
    
    # TODO: add 'encode_params' and x='"foobar"' to add any value in the request
    out %Q{<%= form_remote_tag(:url => zafu_node_path(#{node}), :method => :get, :html => {:id => \"#{dom_id}_f\"}) %>
    <div class='hidden'>
      <input type='hidden' name='t_url' value='#{template_url(upd)}'/>
      <input type='hidden' name='dom_id' value='#{upd}'/>
      <input type='hidden' name='s' value='<%= start_node_zip %>'/>
    </div><div class='wrapper'>
    }
    if @blocks == []
      out "<input type='text' name='#{@params[:key] || 'f'}' value='<%= params[#{(@params[:key] || 'f').to_sym.inspect}] %>'/>"
    else
      out expand_with(:in_filter => true)
    end
    out "</div></form>"
    loading = @params[:loading]
    loading = 'Zena.loading' if loading == 'true'
    if @params[:live] || @params[:update]
      out "<% filter_form(#{node}, \"#{dom_id}_f\", #{loading.inspect}, '#{upd}') %>"
    end  
  end
end

#r_jsObject

Execute javascript after page/partial load.



546
547
548
549
550
551
552
553
554
555
# File 'lib/zena/use/ajax.rb', line 546

def r_js
  if @blocks.detect {|b| !b.kind_of?(String)}
    out "<% js_data << capture do %>"
    out expand_with
    out "<% end %>"
  else
    txt = @blocks.join('')
    out "<% js_data << #{txt.inspect} %>"
  end
end

#r_preview_nodeObject



330
331
332
# File 'lib/zena/use/ajax.rb', line 330

def r_preview_node
  expand_if("#{var} = preview_node(#{node})", node.move_to(var, node.klass))
end

#r_reset_sortObject



568
569
570
571
# File 'lib/zena/use/ajax.rb', line 568

def r_reset_sort
  text = text_for_link(trans('reset_sort'))
  out "<a href='javascript:void()' onclick='Zena.resetSort(this)'>#{text}</a>"
end

#r_toggleObject

Create a link to toggle relation on/off



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/zena/use/ajax.rb', line 431

def r_toggle
  return parser_error("missing 'set' or 'add' parameter") unless role = @params.delete(:set) || @params.delete(:add)
  return parser_error("missing 'for' parameter") unless finder = @params.delete(:for)

  finder = RubyLess.translate(self, finder)
  return parser_error("Invalid class 'for' parameter: #{finder.klass}") unless finder.klass <= Node

  node.dom_prefix = dom_name
  var = root.get_unique_name('tog')
  dom_id = node.dom_id(:erb => false)
  markup.set_id(node.dom_id)
  markup.append_param(:class, 'toggle')
  opts = {}
  if arity = @params.delete(:arity)
    opts[:arity] = arity
  end
  
  if js = @params.delete(:js)
    opts[:js] = js
  end
  
  out "<% add_toggle_id(\"#{dom_id}\", #{var.inspect}, #{RubyLess.translate_string(self, role)},#{opts.inspect}) { #{finder} } %>#{expand_with}"
end


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/zena/use/ajax.rb', line 495

def r_unlink
  return '' if @context[:make_form]
  opts = {}

  if upd = @params[:update]
    if upd == '_page'
      target = nil
    elsif target = find_target(upd)
      # ok
    else
      return
    end
  elsif target = ancestor('block')
    # ok
  else
    target = self
  end

  opts[:update] = target

  if node.will_be?(Node)
    opts[:action] = 'unlink'
  elsif node.will_be?(Link)
    # ?
    opts[:url] = "/nodes/\#{#{node}.this_zip}/links/\#{#{node}.zip}"
  end

  opts[:default_text] = _('btn_tiny_del')
  @params[:class] ||= 'unlink'

  out "<% if #{node}.can_write? && #{node}.link_id %>#{wrap(make_link(opts))}<% end %>"

 #tag_to_remote
 #"<%= tag_to_remote({:url => node_path(#{node_id}) + \"#{opts[:method] != :put ? '/zafu' : ''}?#{action.join('&')}\", :method => #{opts[:method].inspect}}) %>"
 #  out "<a class='#{@params[:class] || 'unlink'}' href='/nodes/#{erb_node_id}/links/<%= #{node}.link_id %>?#{action}' onclick=\"new Ajax.Request('/nodes/#{erb_node_id}/links/<%= #{node}.link_id %>?#{action}', {asynchronous:true, evalScripts:true, method:'delete'}); return false;\">"
 #  if [email protected]?
 #    inner = expand_with
 #  else
 #    inner = _('btn_tiny_del')
 #  end
 #  out "#{inner}</a><% else %>#{inner}<% end %>"
 #elsif node.will_be?(DataEntry)
 #  text = get_text_for_erb
 #  if text.blank?
 #    text = _('btn_tiny_del')
 #  end
 #  out "<%= link_to_remote(#{text.inspect}, {:url => \"/data_entries/\#{#{node}[:id]}?dom_id=#{dom_id}#{upd_url}\", :method => :delete}, :class=>#{(@params[:class] || 'unlink').inspect}) %>"
 #end
end

#wrap_with_drag(text) ⇒ Object

Force an id on the current tag and record the DOM_ID to make the element draggable.



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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/zena/use/ajax.rb', line 273

def wrap_with_drag(text)
  # do not render drag in make_form
  return text unless @drag_param && !@context[:make_form]
  drag = @drag_param

  if @markup.params[:id] || (@markup.done && @method == 'link') # hack to rewrap link...
    # we do not mess with the original but use our own markup
    markup = @wrap_markup = Zafu::Markup.new('span')
  else
    markup = @markup
  end

  markup.tag ||= 'div'

  if node.instance_variable_get(:@dom_prefix).blank?
    # make sure we have a scope
    node.dom_prefix = dom_name
  end

  if erb_dom_id = markup.dyn_params[:id]
    # id set, get erb id
  else
    # We do not want to use the same id as the 'each' loop but we also want to
    # avoid changing the node context
    @drag_prefix ||= root.get_unique_name('drag', true).gsub(/[^\d\w\/]/,'_')
    erb_dom_id = node.dom_id(:dom_prefix => @drag_prefix)
    markup.set_id(erb_dom_id)
  end

  dom_id = erb_dom_id[/<%=\s*(.*?)\s*%>/,1]

  markup.append_param(:class, 'drag')

  drag = 'drag_handle' if drag == 'true'

  if drag == 'all'
    js_options = ['false']
  else
    unless @blocks.detect{|b| b.kind_of?(String) ? b =~ /class=.#{drag}/ : (b.params[:class] == drag || (b.markup && b.markup.params[:class] == drag))}
      handle = "<span class='#{drag}'>&nbsp;</span>"
    end
    js_options = [drag.inspect]
  end

  if revert = @params.delete(:revert)
    js_options << (%w{true false}.include?(revert) ? revert : revert.inspect)
  end

  markup.pre_wrap[:drag] = "#{handle}<% add_drag_id(#{dom_id}, #{js_options.join(', ').inspect}) %>"

  if @markup == markup
    text
  else
    markup.wrap(text)
  end
end