Module: Zena::Use::Display::ZafuMethods

Includes:
RubyLess, Common
Defined in:
lib/zena/use/display.rb

Overview

ViewMethods

Constant Summary collapse

SHOW_KEYS =
[:tz, :tformat, :format, :lang, :zero]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#icon_finder

Class Method Details

.included(base) ⇒ Object

As a last resort, if the method does not compile try to use <r:show eval=‘…’/>



429
430
431
# File 'lib/zena/use/display.rb', line 429

def self.included(base)
  base.process_unknown :show_eval
end

Instance Method Details

#extract_label(res, attribute) ⇒ Object



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
544
545
# File 'lib/zena/use/display.rb', line 518

def extract_label(res, attribute)
  return res if @params[:type] == 'hidden'
  attribute ||= @params[:param]
  if (label = param(:label) || param(:tlabel)) && attribute
    case label
    when 'true'
      "<label>#{attribute}</label> <span>#{res}</span>"
    when 't'
      "<label>#{trans(attribute)}</label> <span>#{res}</span>"
    when '','false'
      # no label
      res
    else
      if @params[:tlabel]
        code = ::RubyLess.translate(self, "t(%Q{#{label}})")
      else
        code = ::RubyLess.translate_string(self, label)
      end
      if code.literal
        "<label>#{code.literal}</label> <span>#{res}</span>"
      else
        "<label><%= #{code} %></label> <span>#{res}</span>"
      end
    end
  else
    res
  end
end

#r_add_documentObject

Show html to add open a popup window to add a document. TODO: inline ajax for upload ?



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/zena/use/display.rb', line 627

def r_add_document
  return parser_error("only works with nodes (not with #{node.klass})") unless node.will_be?(Node)
  @markup.append_param(:class, 'btn_add')
  node = self.node.list_context? ? self.node.up : self.node

  params = {}
  @params.each do |k,v|
    next if [:attr, :eval, :text, :t].include?(k)
    v = RubyLess.translate_string(self, v)
    if v.literal
      params[k] = CGI.escape(v.literal)
    else
      params[k] = "<%= CGI.escape(#{v}) %>"
    end
  end
  
  res = node_action_link('add_doc', "<%= #{node}.zip %>", :text => text_for_link(''), :params => params)
  "<% if #{node}.can_write? %>#{wrap(res)}<% end %>"
end

#r_content_for_layoutObject

Used by zafu templates that act as layouts (adminLayout for example) to insert the content if present or render.



612
613
614
615
616
# File 'lib/zena/use/display.rb', line 612

def r_content_for_layout
  "<% if content_for_layout = yield %><%= content_for_layout %><% else %>" +
  expand_with +
  "<% end %>"
end

#r_iconObject

Find icon through a relation named ‘icon’ or use first image child



648
649
650
651
652
653
654
655
# File 'lib/zena/use/display.rb', line 648

def r_icon
  if node.will_be?(Node)
    finder = build_finder(:first, icon_finder)
    expand_with_finder(finder)
  else
    rubyless_eval
  end
end

#r_imgObject

Display an image



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/zena/use/display.rb', line 658

def r_img
  if node.will_be?(VirtualClass)
    return "<img src='<%= #{node}.prop['icon'] %>'/>"
  elsif !node.will_be?(Node)
    return
  end

  if src = @params[:src]
    finder = ::RubyLess.translate(self, @params[:src]) #build_finder(:first, @params[:src])
    return parser_error("invalid class (#{finder.klass})") unless finder.klass <= Node

    img = finder
  else
    img = node
  end

  mode = @params[:mode] || 'std'
  # FIXME: replace this call by something that integrates better with html_tag_params and such.
  res = "img_tag(#{img}, :mode=>#{mode.inspect}"
  [:class, :alt_src, :id, :border, :style].each do |k|
    res  += ", :#{k}=>#{@params[k].inspect}" if @params[k]
  end
  if host = param(:host)
    res += ", :host => #{::RubyLess.translate_string(self, host)}"
  end
  res += ")"
  if finder = @params[:link]
    finder = ::RubyLess.translate(self, finder)

    return parser_error("Invalid class (#{finder.klass})") unless finder.klass <= Node

    opts_str = @context["exp_host"] ? ", :host => #{@context["exp_host"]}" : ""

    "<a href='<%= zen_path(#{finder}#{opts_str}) %>'><%= #{res} %></a>"
  else
    "<%= #{res} %>"
  end
end

#r_javascriptsObject

Insert javascript asset tags



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/zena/use/display.rb', line 553

def r_javascripts
  if @params[:list] == 'all' || @params[:list].nil?
    list = %w{ prototype effects dragdrop window zena grid upload-progress }
  else
    list = @params[:list].split(',').map{|e| e.strip}
  end

  out helper.javascript_include_tag(*list)
  return
# Experimental: move all scripts at end of file
  if list.include?('prototype')
    list -= ['prototype']
    out helper.javascript_include_tag('prototype')
  end
  return if list.empty?

  list = list.map do |e|
    "Script.load('#{helper.javascript_path(e)}');"
  end
  code = %Q{  var Script = {
    _loadedScripts: [],
    load: function(script){
      if (this._loadedScripts.include(script)){
return false;
      }
      var code = new Ajax.Request(script, {
asynchronous: false, method: "GET",
evalJS: false, evalJSON: false
      }).transport.responseText;
      if (Prototype.Browser.IE) {
window.execScript(code);
      } else if (Prototype.Browser.WebKit){
$$("head").first().insert(Object.extend(
  new Element("script", {type: "text/javascript"}), {text: code}
));
      } else {
window.eval(code);
      }
      this._loadedScripts.push(script);
    }
  };
  #{list.join("\n  ")}
  }
  out "<% js_data.unshift #{code.inspect} -%>"
end

#r_prop_gridObject



716
717
718
719
720
721
722
723
724
725
726
# File 'lib/zena/use/display.rb', line 716

def r_prop_grid
  return parser_error("not in a list context") unless node.list_context?
  return parser_error("not a Node list") unless node.single_class <= Node
  klass = "#{node.single_class.name}"
  add_block %Q{<table class='prop_grid'>
    <tr do='#{klass}' do='roles'><th class='role' colspan='\#{columns.size}' do='each' do='name'/></tr>
    <tr do='#{klass}' do='roles' do='each' do='columns'><th do='each' do='name'/></tr>
    <tr do='each'><r:#{klass} do='roles' do='each' do='columns'><td do='each' do='@node.send(name)'/></r:#{klass}></tr>
  </table>}
  expand_with
end

#r_show(code = nil) ⇒ Object

Display an attribute or RubyLess code



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
# File 'lib/zena/use/display.rb', line 481

def r_show(code = nil)
  if code.nil? && node.list_context?
    @context[:node] = node.move_to("#{node}.first", node.klass.first, :query => node.opts[:query])
    return r_show
  end

  if method = code || get_attribute_or_eval(false)
    # ok
  elsif node.will_be?(String) || node.will_be?(Time)
    method = RubyLess.translate(self, 'this')
  else
    return nil
  end

  klass = method.klass

  if klass.kind_of?(Array)
    res = show_string(method)
  elsif klass <= String
    res = show_string(method)
  elsif klass <= Number
    res = show_number(method)
  elsif klass <= Time
    res = show_time(method)
  else
    res = show_string(method)
  end

  res = extract_label(res, @params[:attr])

  if param(:blank) == 'hide'
    "<% if !#{method}.blank? %>#{@markup.wrap(res)}<% end %>"
  else
    res
  end
end

#r_stylesheetsObject

Insert stylesheet asset tags



600
601
602
603
604
605
606
607
608
# File 'lib/zena/use/display.rb', line 600

def r_stylesheets
  if @params[:list] == 'all' || @params[:list].nil?
    list = %w{ reset window zena code grid upload-progress }
  else
    list = @params[:list].split(',').map{|e| e.strip}
  end
  list << {:media => @params[:media]} if @params[:media]
  helper.stylesheet_link_tag(*list)
end

#r_title_for_layoutObject

Display the page’s default title



619
620
621
622
623
# File 'lib/zena/use/display.rb', line 619

def r_title_for_layout
  "<% if @title_for_layout %><%= @title_for_layout %><% elsif @node && [email protected]_record? %><%= @node.title %><% elsif @node.parent %><%= @node.parent.short_path %><% else %>" +
  expand_with +
  "<% end %>"
end

#r_zazen(signature = nil) ⇒ Object

Parse text with zazen helper



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
# File 'lib/zena/use/display.rb', line 448

def r_zazen(signature = nil)
  if signature.nil? || @markup.tag
    # Do not create markup when using RubyLess
    @markup.prepend_param(:class, 'zazen')
    @markup.tag ||= 'div'
  end

  if signature
    # TODO: Get 'notextile' param from rubyless.
    if node = node(Node)
      {
        :class  => String,
        :method => 'zazen',
        :accept_nil => true,
        :html_safe  => true,
        :append_hash => {:node => ::RubyLess::TypedString.new(node.to_s, :class => node.klass)}
      }
    else
      nil
    end
  else
    node = node(Node) || '@node'
    return nil unless attribute = get_attribute_or_eval

    hash_arguments = extract_from_params(:code, :notextile, :host, :line_numbers, :theme, :target) || []

    hash_arguments.insert(0, ":node => #{node}")

    "<%= zazen(#{attribute}, #{hash_arguments.join(', ')}) %>"
  end
end

#r_zenaObject

Shows a ‘made with Zena’ link or logo. ;-) Thanks for using this !



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/zena/use/display.rb', line 698

def r_zena
  if  = @params[:logo]
    # FIXME
    case 
    when 'tiny'
    else
    end
  else
    text = case @params[:type]
    when 'garden'
      _("a Zen garden")
    else
      _("made with Zena")
    end
    "<a class='zena' href='http://zenadmin.org' title='Zena <%= Zena::VERSION %>'>#{text}</a>"
  end
end

#show_evalObject

Transform <p do=‘created_at’ format=‘%d’/> into

<p do='show' eval='created_at' format='%d'/>


435
436
437
438
439
440
441
442
443
444
445
# File 'lib/zena/use/display.rb', line 435

def show_eval
  if !@params.empty? && !(@method =~ /. ./) && (@params.keys - SHOW_KEYS == [])
    # try to use r_show without using params as arguments
    code = RubyLess.translate(self, @method)
    r_show(code)
  else
    nil
  end
rescue RubyLess::Error
  nil
end

#show_string(method) ⇒ Object

Also used by rubyless_expand



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/zena/use/display.rb', line 729

def show_string(method)
  if method.kind_of?(RubyLess::TypedString)
    if lit = method.literal
      if method.opts[:html_safe]
        lit
      elsif param(:h) == 'false'
        erb_escape(lit)
      else
        ::ERB::Util.html_escape(lit)
      end
    elsif param(:h) == 'false' || method.opts[:html_safe]
      "<%= #{method} %>"
    else
      "<%=h #{method} %>"
    end
  elsif param(:h) == 'false'
    "<%= #{method} %>"
  else
    "<%=h #{method} %>"
  end
end