Class: Ezframe::Html

Inherits:
Object show all
Defined in:
lib/ezframe/html.rb

Class Method Summary collapse

Class Method Details

.convert(ht_h = {}) ⇒ Object



6
7
8
9
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
# File 'lib/ezframe/html.rb', line 6

def convert(ht_h = {})
  ht_h = hook_for_convert(ht_h)
  return "" if ht_h.nil? || ht_h.to_s.empty?
  return ht_h.to_html if ht_h.respond_to?(:to_html)
  return ht_h.to_s if ht_h.is_a?(String) || ht_h.is_a?(Symbol) || ht_h.is_a?(Integer) || ht_h.is_a?(Time)
  return ht_h.map { |args| convert(args) }.join if ht_h.is_a?(Array)

  tag = ht_h[:tag]
  case tag
  when "textarea"
    textarea(ht_h)
  when "select"
    return select(ht_h) if ht_h[:item]
  when "icon"
    tag = "i"
  end
  tag = ht_h[:tag]
  error_box = ""
  if %w[input select textarea].include?(tag)
    error_box = "<div id=\"error-box-#{ht_h[:name]}\" class=\"error-box hide\"></div>"
  end
  opt_s, child_s = join_attribute(ht_h)
  if !child_s.strip.empty? || !%w[img input hr br meta].include?(tag)
    start_tag = [ht_h[:tag], opt_s].compact.join(" ").strip
    return "<#{start_tag}>#{child_s}</#{ht_h[:tag]}>"+error_box
  end
  tag_content = [ ht_h[:tag], opt_s ].compact.join(" ")
  return "<#{tag_content}/>"+error_box
end

.hook_for_convert(ht_h) ⇒ Object



112
113
114
# File 'lib/ezframe/html.rb', line 112

def hook_for_convert(ht_h)
  return ht_h
end

.join_attribute(attrs) ⇒ Object

attributeの連結文字列化



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ezframe/html.rb', line 37

def join_attribute(attrs)
  child_s = ""
  opt_a = attrs.map do |k, v|
    case k
    when :child
      child_s = convert(v)
      next
    when :tag, :final
      next
    when :key
      "name=\"#{v}\"" if attrs[:tag].to_sym == :input
      next
    else
      if v.is_a?(Array)
        "#{k}=\"#{v.join(" ")}\""
      elsif v.nil?
        nil
      else
        "#{k}=\"#{v}\""
      end
    end
  end
  [opt_a.compact.join(" "), child_s]
end

.select(ht_h = {}) ⇒ Object



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
# File 'lib/ezframe/html.rb', line 70

def select(ht_h = {})
  attr = ht_h.clone
  item = attr[:item]
  # puts "Html.select: #{item}"
  if item.is_a?(Hash)
    option_a = ht_h[:item].map do |k, v|
      h = { tag: "option", value: k }
      if v.is_a?(Array)
        v, selected = v
        h[:selected] = "selected" if selected
      end
      h[:child] = v
      # EzLog.info "select: hash: k=#{k}, v=#{v}, value=#{ht_h[:value]}"
      if ht_h[:value] && ht_h[:value].to_s == k.to_s
        h[:selected] = "selected"
      end
      h
    end
  elsif item.is_a?(Array)
    option_a = item.map do |v|
      h = { tag: "option", value: v[0], child: v[1] }
      if %w[selected default].include?(v[2])
        h[:selected] = "selected"
      end
      # EzLog.info "select: array: v=#{v}, value=#{ht_h[:value]}"
      if ht_h[:value] && ht_h[:value].to_s == v[0].to_s
        h[:selected] = "selected"
      end
      # puts h.inspect
      h
    end
  else
    warn "unknown item: #{ht_h.inspect}"
  end
  attr[:tag] = "select"
  attr[:child] = option_a
  attr[:name] ||= attr[:key]
  attr[:final] = true
  attr.delete(:item)
  Html.convert(attr)
end

.textarea(ht_h) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/ezframe/html.rb', line 62

def textarea(ht_h)
  value = ht_h[:value]
  if value
    ht_h[:child] = value
    ht_h.delete(:value)
  end
end