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

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

Fixed a bug with the attribute helper introduced in the previous changeset.

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