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=‘…’/>



339
340
341
# File 'lib/zena/use/display.rb', line 339

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

Instance Method Details

#extract_label(res, attribute) ⇒ Object



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

def extract_label(res, attribute)
  return res if @params[:type] == 'hidden'
  attribute ||= @params[:param]
  if (label = @params[:tlabel] || 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 ?



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/zena/use/display.rb', line 537

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.



522
523
524
525
526
# File 'lib/zena/use/display.rb', line 522

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



558
559
560
561
562
563
564
565
# File 'lib/zena/use/display.rb', line 558

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



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
598
599
600
601
602
603
604
605
# File 'lib/zena/use/display.rb', line 568

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



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

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



626
627
628
629
630
631
632
633
634
635
636
# File 'lib/zena/use/display.rb', line 626

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



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

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



510
511
512
513
514
515
516
517
518
# File 'lib/zena/use/display.rb', line 510

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



529
530
531
532
533
# File 'lib/zena/use/display.rb', line 529

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



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/zena/use/display.rb', line 358

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 !



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/zena/use/display.rb', line 608

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'/>


345
346
347
348
349
350
351
352
353
354
355
# File 'lib/zena/use/display.rb', line 345

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



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/zena/use/display.rb', line 639

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