noelrappin / tank-engine

A new plugin for creating Rails applications targeted for iPhones. A successor to rails_iui

noelrappin (author)
Tue Dec 09 12:05:14 -0800 2008
commit  cd20398221e5405bedae18a35592bb1a95a97d56
tree    e4d860373e3f99852ef31f0be53780c3bbe97ed7
parent  478057ca67deed3f35fdbb7b2f6e6777448b109c
tank-engine / lib / tank_engine_helper.rb
100644 169 lines (138 sloc) 5.335 kb
1
2
3
4
5
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module TankEngineHelper
  
  ## HTML Headers
  
  def viewport_tag(options = {})
    content = ""
    content += "width = device-width, user-scalable=no" if options[:device_width]
    tag(:meta, :name => "viewport", :content => content)
  end
  
  def include_tank_engine_files
    stylesheet_link_tag('tank_engine') + javascript_include_tag('tank_engine')
  end
  
  ### Navigation Bar
  
  def button_from_hash(hash)
    return "" if hash.blank?
    is_back = (hash.delete(:back) == true)
    if is_back
      hash[:html_options] ||= {}
      hash[:html_options][:id] = "backButton"
      hash[:html_options][:class] = "te_slide_right"
    end
    button_link_to(hash[:caption], hash[:url], hash[:html_options])
  end
  
  def te_navigation_bar(left_button_hash, caption, right_button_hash = {})
    left_button = button_from_hash(left_button_hash)
    header = content_tag(:h1, caption, :id => "header_text")
    right_button = button_from_hash(right_button_hash)
    content = [left_button, header, right_button].join("\n")
    content_tag(:div, content, :class => "toolbar")
  end
  
  ## Link helpers
  
  def button_link_to(name, options, html_options = {})
    html_options[:class] ||= " "
    html_options[:class] += " button"
    link_to(name, options, html_options)
  end
  
  def link_to_replace(name, options, html_options = {})
    html_options[:target] = "_replace"
    link_to(name, options, html_options)
  end
  
  def link_to_external(name, options, html_options = {})
    html_options[:target] = "_self"
    link_to(name, options, html_options)
  end
  
  def link_from_list(name, options, slide = nil, html_options = {})
    if slide
      if html_options[:class]
        html_options[:class] += " te_slide_left"
      else
        html_options[:class] = "te_slide_left"
      end
    end
    link_to(name, options, html_options)
  end
  
  def link_to_target(target, name, options, html_options = {})
    if target == :replace
      link_to_replace(name, options, html_options)
    elsif target == :self or target == :external
      link_to_external(name, options, html_options)
    else
      link_to(name, options, html_options)
    end
  end
  
# def servicelink_tel(telno)
# content_tag("a",telno,:class=>"ciuServiceLink",:target => "_self",:href => "tel:#{telno}" ,:onclick => "return(navigator.userAgent.indexOf('iPhone') != -1)")
# end
# def servicelink_email(email)
# content_tag("a",email,:class=>"ciuServiceLink",:target => "_self",:href => "mailto:#{email}" ,:onclick => "return(navigator.userAgent.indexOf('iPhone') != -1)")
# end
# def servicebutton_map(gadr,caption)
# content_tag("a",caption,:class=>"ciuServiceButton",:target => "_self",:href => "http://maps.google.com/maps?q=#{gadr}" ,:onclick => "return(navigator.userAgent.indexOf('iPhone') != -1)")
# end
 
 
  ## Lists
  
  def list_element(item, target = nil, slide = true)
    link = link_from_list(item.caption, item.url, slide)
    content_tag(:li, link)
  end
  
  def append_options(list_content, options = {})
    list_content = options[:top] + list_content if options[:top]
    list_content += list_element(options[:more], :replace) if options[:more]
    list_content += options[:bottom] if options[:bottom]
    list_content
  end
  
  def te_list(items, options = {})
    slide = !options[:no_slide]
    list_content = items.map {|i| list_element(i, nil, slide)}.join("\n")
    list_content = append_options(list_content, options)
    if options[:as_replace]
      list_content
    else
      content_tag(:ul, list_content, :selected => "true")
    end
  end
  
  def te_grouped_list(items, options = {}, &group_by_block)
    groups = items.group_by(&group_by_block).sort
    group_elements = groups.map do |group, members|
      group = content_tag(:li, group, :class => "group")
      member_elements = [group] + members.map { |m| list_element(m) }
    end
    content_tag(:ul, group_elements.flatten.join("\n"), :selected => "true")
  end
  
  ## panels and fields
  
  def fieldset(&block)
    concat(content_tag(:fieldset, capture(&block)), block.binding)
  end
  
  def row(label_text="", &block)
    label = if label_text.blank? then "" else content_tag(:label, label_text) end
    block = if block_given? then capture(&block) else "" end
    div = content_tag(:div, label + block, :class => "row")
    if block_given?
      concat(div, block.binding)
    else
      div
    end
  end
  
  def row_label(&block)
    label = content_tag(:label, capture(&block))
    div = content_tag(:div, label, :class => "row")
    concat(div, block.binding)
  end
  
  def panel(&block)
    div = content_tag(:div, capture(&block), :class => "panel")
    concat(div, block.binding)
  end
  
  def dialog(&block)
    div = content_tag(:div, capture(&block), :class => "dialog")
    concat(div, block.binding)
  end
  
  ## Orientation Change
  
  def observe_orientation_change(url_options = {})
    remote = remote_function :url => url_options,
   :with => "'position=' + String(window.orientation)"
    func = "function() { #{remote}; };"
    javascript_tag("function updateOrientation() { #{remote}; }")
  end
  
  def register_orientation_change
    javascript_tag('$(function() { $("body").bind("orientationchange", updateOrientation) });')
  end
  
end
 
ActionView::Base.send(:include, TankEngineHelper)