root/trunk/app/helpers/hyperde/node.rb @ 47

Revision 47, 4.2 KB (checked in by dema, 7 years ago)

Enhanced the attribute helpers to behave better on error conditions.

Line 
1module ApplicationHelper
2
3  def attr?(attr_name, &block)
4    value = attr(attr_name)
5    buf = String.new
6    buf << block.call(value) if block && value && value.size > 0
7    buf
8  end
9   
10  def attr(attr_name, template = nil, &block)
11    value = ""
12    begin
13        value = @node.send(attr_name)
14        if value.nil?
15          ""
16        else
17          attr_value(value, attr_name, template, &block)     
18      end
19    rescue => ex
20        value = ex.to_s
21    end
22    value
23  end
24
25  def attr_value(value, attr_name, template = nil, &block)
26    HtmlAnchor.view = self   
27    buf = String.new
28    case value
29    when Index
30      if block
31        buf << block.call(value)
32      else
33        buf << index(value) if value.entries.size > 0
34      end
35    when AnchorValue
36      buf << anchor(value, template, &block)
37    else
38      case @node.nav_attributes[attr_name].data_type
39      when "Image"
40        buf << "<img src=\"#{value.to_s}\"/>"
41      when "Url", "Uri"
42        buf << "<a href=\"#{value.to_s}\" target=\"_blank\">#{value.to_s}</a>"
43      when "Email"
44        buf << "<a href=\"mailto:#{value.to_s}\" target=\"_blank\">#{value.to_s}</a>"
45      else
46        buf << value.to_s
47      end
48    end
49    buf
50  end
51     
52  def attributes(attr_list = nil)
53    buf = String.new
54   
55    if attr_list.nil? || attr_list.include?("id")
56      buf << "<div class='attribute'><span class='attribute_label'>Id:&nbsp;</span>"
57      buf << "<span class='attribute_value'>#{@node.id}</span></div>"
58    end
59       
60    attrs = @node.nav_attributes.values.select do |attr|
61      attr_list ? attr_list.include?(attr.name) : true
62    end
63    attrs.sort! { |a,b| (a.order.to_s + a.name) <=> (b.order.to_s + b.name) } 
64    attrs.each do |attr_def|
65      attr?(attr_def.name) do |value|
66        buf << "<div class='attribute'>" 
67        buf << "<span class='attribute_label'>" 
68        buf << NavAttribute.human_attribute_name(attr_def.name) << ":&nbsp;</span>"
69        buf << "<span class='attribute_value'>" << value << "</span></div>"
70      end
71    end
72    buf
73  end
74
75  def anchor(anc, template = nil, &block)
76    native = "<span class='node_anchor_link'>%s</span>"
77    create_anchor = lambda { |anc| NodeHtmlAnchor.new(anc) }
78    template_for_anchor(template, block, create_anchor, anc, native)
79  end
80
81  def op(op_name, options = {}, template = nil, &block)
82    native = "%s"
83    create_anchor = lambda { |op_def| NodeOperationHtmlAnchor.new(op_def[:name], op_def[:options]) }
84    template_for_anchor(template, block, create_anchor, { :name => op_name, :options => options}, native)   
85  end
86
87  class NodeOperationHtmlAnchor < HtmlAnchor
88    attr_reader :function, :onclick
89   
90    def initialize(op_name, options = {})
91      options[:url] ||= { :action => "context", :op => op_name, 
92                          :view => options[:view], 
93                          :params => params_for_anchor(view.params[:p]) }
94      options[:with] ||= "Form.serialize(document.forms[0])"
95      options[:complete] ||= "window.location.reload()" unless options[:update]
96      options[:loading] ||= "$('__loading').innerHTML = '#{options[:label_loading] || 'Loading...'}';$('__loading').style.display='block'"
97      options[:loaded] ||= "$('__loading').style.display = 'none'"
98      options[:method] ||= "'post'"
99      @label = options[:label]
100      @function = view.remote_function(options)
101      @ahref = view.link_to_function(@label, @function)
102      @onclick = "#{@function}; return false;"
103      @url = "javascript:#{@onclick}"
104    end
105  end
106   
107  class NodeHtmlAnchor < HtmlAnchor
108    attr_reader :anchor
109
110    def initialize(anc)
111      @anchor = anc
112      @label = anc.label_value
113      @url = view.url_for(:action => action, :id => anc.target_id, 
114                     :params => params_for_anchor(anc.params_values)) if anc.target_id
115      @ahref = view.link_to(@label, :action => action, :id => anc.target_id, 
116                     :params => params_for_anchor(anc.params_values)) if anc.target_id
117    end
118   
119    def action
120      case @anchor
121      when ContextAnchorValue
122        "context"
123      when IndexAnchorValue
124        "show_index"
125      else
126        nil
127      end
128    end
129  end     
130   
131end
Note: See TracBrowser for help on using the browser.