Module: Zena::Use::Urls::ZafuMethods

Includes:
RubyLess
Defined in:
lib/zena/use/urls.rb

Overview

ViewMethods

Instance Method Summary collapse

Instance Method Details

#insert_dom_id(signature) ⇒ Object

Add the dom_id inside a RubyLess built method (used with make_href and ajax).



363
364
365
366
# File 'lib/zena/use/urls.rb', line 363

def insert_dom_id(signature)
  return nil if signature.size != 1
  {:method => @insert_dom_id, :class => String}
end

Create a link tag.

Parameters (hash)

  • :update - DOM_ID: produce an Ajax call that will update this part of the page (optional)

  • :default_text - default text to use for the link if there are no ‘text’, ‘eval’ or ‘attr’ params

  • :action - link action (edit, show, etc)



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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
494
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
# File 'lib/zena/use/urls.rb', line 407

def make_link(options = {})
  remote_target = (options[:update] || @params.delete(:update))
  options[:action] ||= @params.delete(:action)
  confirm = @params.delete(:confirm)

  @markup.tag ||= 'a'

  if @markup.tag == 'a'
    markup = @markup
  else
    markup = Zafu::Markup.new('a')
  end

  steal_and_eval_html_params_for(markup, @params)

  href = make_href(remote_target, options)
  
  # This is to make sure live_id is set *inside* the <a> tag.
  if @live_param
    text = add_live_id(text_for_link, markup)
    @live_param = nil
  else
    text = text_for_link(options[:default_text])
  end

  http_method = http_method_from_action(options[:action])
    
  if http_method == 'delete' && method != 'unlink'
    confirm ||= '#{t("Destroy")} "#{h title}" ?'
  end

  if confirm
    confirm = ::RubyLess.translate_string(self, confirm)

    if confirm.literal
      markup.set_param(:"data-confirm", confirm.literal)
    else
      markup.set_dyn_param(:"data-confirm", "<%= fquote(#{confirm}) %>")
    end
  end

  if remote_target
    # ajax link (link_to_remote)

    # Add href to non-ajax method.
    markup.set_dyn_param(:href, "<%= #{make_href(nil, options.merge(:update => false))} %>")

    if true
      # Use onclick with Ajax.
      # FIXME: Use Zena.do so that we can use ajax stamp. This means that we write a variant of "make_href" which returns json for query parameters.
      if confirm
        markup.set_dyn_param(:onclick, "if(confirm(this.getAttribute(\"data-confirm\"))) {new Ajax.Request(\"<%= #{href} %>\", {asynchronous:true, evalScripts:true, method:\"#{http_method}\"});} return false;")
      else
        markup.set_dyn_param(:onclick, "new Ajax.Request(\"<%= #{href} %>\", {asynchronous:true, evalScripts:true, method:\"#{http_method}\"}); return false;")
      end
    else
      #### FIXME: We need the 'update' parameter to trigger a js response for delete but we ignore
      ####        the content.
      
      
      if remote_target.kind_of?(String)
        # YUCK. We should have a way to have dom_ids that do not need
        # us to look for remote_target !
        remote_target = find_target(remote_target)
      end
      # Experimental new model for javascript actions.
      # Works for 'swap' but needs more adaptations for 'edit' or other links
    
      hash_params = []
      (options[:query_params] || @params).each do |key, value|
        next if [:update, :href, :eval, :text, :attr, :t, :host].include?(key)
        case key
        when :anchor
          # Get anchor and force string interpolation
          value = "%Q{#{get_anchor_name(value)}}"
        when :publish
          if value == 'true'
            key = 'node[v_status]'
            value = Zena::Status::Pub
          else
            next
          end
        when :encode_params, :format, :mode, :insert, :states
          # Force string interpolation
          value = "%Q{#{value}}"
        else
          if value.blank?
            value = "''"
          end
        end
        hash_params << "#{key.inspect} => #{value}"
      end

      if host = param(:host)
        hash_params << ":host => %Q{#{host}}"
      end
    
      if !hash_params.blank?
        query = RubyLess.translate(self, "{#{hash_params.join(', ')}}.to_json")
      else
        query = ''
      end
    
      dom_id, dom_prefix = get_dom_id(remote_target)
      markup.set_dyn_param(:onclick, %Q{return Zena.#{http_method}("<%= #{dom_id} %>",<%= #{query} %>)})
    end
  else
    markup.set_dyn_param(:href, "<%= #{href} %>")

    if http_method != 'get' || confirm
      markup.set_dyn_param(:onclick, "return Zena.m(this,#{http_method.inspect})")
    end
  end

  # We wrap without callbacks (before_wrap, after_wrap) so that the link
  # is used as raw text in these callbacks.
  markup.wrap(text)
end

#r_anchorObject

Insert a named anchor



394
395
396
397
# File 'lib/zena/use/urls.rb', line 394

def r_anchor
  @params[:anchor] ||= 'true'
  r_link
end

creates a link. Options are: :href (node, parent, project, root) :tattr (translated attribute used as text link) :attr (attribute used as text link) <r:link href=‘node’><r:trans attr=‘lang’/></r:link> <r:link href=‘node’ tattr=‘lang’/> <r:link update=‘dom_id’/> <r:link page=‘next’/> <r:link page=‘previous’/> <r:link page=‘list’/>



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/zena/use/urls.rb', line 376

def r_link
  # If we have a contextual timezone set, pass it to @params
  if tz_name = @params[:tz]
    tz_result, tz_var = set_tz_var(tz_name)
    return tz_result unless tz_var
    @params[:tz] = 'tz'
  elsif tz_var = get_context_var('set_var', 'tz')
    @params[:tz] = 'tz'
  end

  if @params[:page] && @params[:page] != '[page_page]' # lets users use 'page' as pagination key
    pagination_links
  else
    make_link
  end
end