Class: OMF::Web::Rack::TabMapper

Inherits:
Common::LObject show all
Defined in:
lib/omf-web/rack/tab_mapper.rb

Instance Method Summary collapse

Methods included from Common::Loggable

#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn

Constructor Details

#initialize(opts = {}) ⇒ TabMapper

Returns a new instance of TabMapper.



17
18
19
20
21
22
# File 'lib/omf-web/rack/tab_mapper.rb', line 17

def initialize(opts = {})
  @opts = opts
  @tab_opts = opts[:tabs] || {}
  @tabs = {}
  find_tabs()
end

Instance Method Details

#_component_name(path) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/omf-web/rack/tab_mapper.rb', line 55

def _component_name(path)
  unless comp_name = path[1]
    comp_name = ((@opts[:tabs] || [])[0] || {})[:id]
  end
  comp_name = comp_name.to_sym if comp_name
  comp_name
end

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/omf-web/rack/tab_mapper.rb', line 34

def call(env)
  #puts env.keys.inspect

  req = ::Rack::Request.new(env)
  #puts "COOKIES>>>> #{req.cookies.inspect}"
  #req.cookies['user'] = 'booo'
  
  # sessionID = req.params['sid']
  # if sessionID.nil? || sessionID.empty?
    # sessionID = "s#{(rand * 10000000).to_i}"
  # end
  # Thread.current["sessionID"] = sessionID
  
  OMF::Web::Theme.require 'page'      
  body, headers = render_page(req)
  if headers.kind_of? String
    headers = {"Content-Type" => headers}
  end
  [200, headers, [body]] # required for ruby > 1.9.2 
end

#find_tabsObject



24
25
26
27
28
29
30
31
32
# File 'lib/omf-web/rack/tab_mapper.rb', line 24

def find_tabs()
  tabs = OMF::Web::Widget.toplevel_widgets(@opts[:use_tabs])
  @enabled_tabs = {} 
  tabs.each do |t| 
    name = t[:id].to_sym
    @enabled_tabs[name] = t  
  end
  @opts[:tabs] = tabs
end

#find_top_widget(tab, req) ⇒ Object



92
93
94
95
96
97
# File 'lib/omf-web/rack/tab_mapper.rb', line 92

def find_top_widget(tab, req)
  sid = req.params['sid']
  tab_id = tab[:id]
  @tabs[tab_id] ||= OMF::Web::Widget.create_widget(tab_id) 
  #inst = OMF::Web::SessionStore[tab_id, :tab] 
end

#render_card(req) ⇒ Object



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
# File 'lib/omf-web/rack/tab_mapper.rb', line 63

def render_card(req)
  #puts ">>>> REQ: #{req.script_name}::#{req.inspect}"
  
  opts = @opts.dup
  opts[:prefix] = req.script_name
  opts[:request] = req      
  opts[:path] = req.path_info

  path = req.path_info.split('/')
  unless comp_name = _component_name(path)
    return render_no_card(opts)
  end
  opts[:component_name] = comp_name.to_sym
  # action = (path[2] || 'show').to_sym

  tab = @enabled_tabs[comp_name]
  unless tab
    warn "Request for unknown component '#{comp_name.inspect}':(#{@enabled_tabs.keys.inspect})"
    return render_unknown_card(comp_name, opts)
  end
  opts[:tab] = tab_id = tab[:id]
  
  widget = find_top_widget(tab, req)
  OMF::Web::Theme.require 'page'
  page = OMF::Web::Theme::Page.new(widget, opts)
  [page.to_html, 'text/html']
end

#render_no_card(popts) ⇒ Object



107
108
109
110
111
112
# File 'lib/omf-web/rack/tab_mapper.rb', line 107

def render_no_card(popts)
  popts[:active_id] = 'unknown'
  popts[:flash] = {:alert => %{There are no components defined for this site.}}
  popts[:tabs] = []      
  [OMF::Web::Theme::Page.new(nil, popts).to_html, 'text/html']
end

#render_page(req) ⇒ Object



114
115
116
# File 'lib/omf-web/rack/tab_mapper.rb', line 114

def render_page(req)
  render_card(req)
end

#render_unknown_card(comp_name, popts) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/omf-web/rack/tab_mapper.rb', line 99

def render_unknown_card(comp_name, popts)
  #popts[:active_id] = 'unknown'
  popts[:flash] = {:alert => %{Unknonw component '#{comp_name}'. To select any of the available 
    components, please click on one of the tabs above.}}

  [OMF::Web::Theme::Page.new(nil, popts).to_html, 'text/html']
end