liquid_feedback_frontend
view env/ui/tabs.lua @ 219:217653875fbb
Fix registration process for units
| author | bsw | 
|---|---|
| date | Sun Mar 13 16:59:16 2011 +0100 (2011-03-13) | 
| parents | 7d0f4721d2f3 | 
| children | fecd4c13054a | 
 line source
     1 if config.user_tab_mode == "accordeon" or config.user_tab_mode == "accordeon_first_expanded" or config.user_tab_mode == "accordeon_all_expanded" then
     3   function ui.tabs(tabs)
     4     local params = param.get_all_cgi()
     5     local current_tabs_string = params["tab"]
     6     local current_tabs = {}
     7     if current_tabs_string then
     8       for current_tab in current_tabs_string:gmatch("([^%|]+)") do
     9         current_tabs[current_tab] = current_tab
    10       end
    11     end
    13     local unique_string = param.get("tab_id") or multirand.string(16, '0123456789abcdef')
    15     function render_tab(tab, first)
    16       local params = param.get_all_cgi()
    17       local active = false
    18       for current_tab in pairs(current_tabs) do
    19         if tab.name == current_tab then
    20           active = true
    21         end
    22       end
    23       if config.user_tab_mode == "accordeon_first_expanded" then
    24         if first and current_tabs_string == nil then
    25           active = true
    26         end
    27       end
    29       if active and not first then
    30 	    app.html_title.prefix = tab.label
    31       end
    33       local link_tabs = {}
    34       if config.user_tab_mode == "accordeon" 
    35         or config.user_tab_mode == "accordeon_first_expanded"
    36         or config.user_tab_mode == "accordeon_all_expanded" and current_tabs_string
    37       then
    38         if not current_tabs_string and not first then
    39           link_tabs[tabs[1].name] = true
    40         end
    41         for current_tab in pairs(current_tabs) do
    42           if current_tab ~= tab.name then
    43             link_tabs[current_tab] = true
    44           end
    45         end
    46       elseif config.user_tab_mode == "accordeon_all_expanded" and not current_tabs_string then
    47         for i, current_tab in ipairs(tabs) do
    48           if current_tab.name ~= tab.name then
    49             link_tabs[current_tab.name] = true
    50           end
    51         end
    52       end
    53       if not active then
    54         link_tabs[tab.name] = true
    55       end
    57       params["tab"] = tab.name
    58       local onclick
    59       if not tab.content then
    60         onclick =
    61           'if (ui_tabs_active["' .. unique_string .. '"]["' .. tab.name .. '"]) {' ..
    62             'el=document.getElementById("tab' .. unique_string .. '_content_' .. tab.name .. '");' ..
    63             'el.innerHTML="";' ..
    64             'el.style.display="none";' ..
    65             'ui_tabs_active["' .. unique_string .. '"]["' .. tab.name .. '"]=false' ..
    66           '} else {' ..
    67             'ui_tabs_active["' .. unique_string .. '"]["' .. tab.name .. '"]=true;' ..
    68             'document.getElementById("tab' .. unique_string .. '_content_' .. tab.name .. '").style.display="block"; ' ..
    69             ui._partial_load_js{
    70               params = { tab = tab.name }
    71             } ..
    72           '};' ..
    73           'return(false);'
    74       end
    75       ui.link{
    76         attr = {
    77           name = "tab_" .. tab.name,
    78           class = (
    79             tab.name == current_tab and "ui_tabs_accordeon_head selected" .. (tab.class and (" " .. tab.class) or "") or
    80             not current_tab and i == 1 and "ui_tabs_accordeon_head selected" .. (tab.class and (" " .. tab.class) or "")  or
    81             "ui_tabs_accordeon_head" .. (tab.class and (" " .. tab.class) or "") 
    82           ),
    83           id = "tab" .. unique_string .. "_head_" .. tab.name,
    84           onclick = onclick,
    85         },
    86         module  = request.get_module(),
    87         view    = request.get_view(),
    88         id      = param.get_id_cgi(),
    89         params  = params,
    90         anchor  = "tab" .. unique_string .. "_" .. tab.name,
    91         content = function()
    92           if tab.icon then
    93             if not tab.icon.attr then
    94               tab.icon.attr = {}
    95             end
    96             tab.icon.attr.id = "tab" .. unique_string .. "_icon_" .. tab.name
    97             tab.icon.attr.width = 16
    98             tab.icon.attr.height = 16
    99             ui.image(tab.icon)
   100           end
   101           slot.put(tab.label)
   102         end
   103       }
   104       local expanded = active or not request.get_json_request_slots() and config.user_tab_mode == "accordeon_all_expanded" and not current_tabs_string
   105       ui.container{
   106         attr = {
   107           class = "ui_tabs_accordeon_content" .. (tab.class and (" " .. tab.class) or ""),
   108           style = not expanded and "display: none;" or nil,
   109           id = "tab" .. unique_string .. "_content_" .. tab.name
   110         },
   111         content = function()
   112           if expanded then
   113             ui.script{ script = 'ui_tabs_active["' .. unique_string .. '"]["' .. tab.name .. '"] = true;' }
   114             execute.view{
   115               module = tab.module,
   116               view = tab.view,
   117               id = tab.id,
   118               params = tab.params
   119             }
   120           else
   121             slot.put(" ")
   122           end
   123         end
   124       }
   125     end
   127     if not request.get_json_request_slots() or not current_tabs_string then
   128       ui.script{ script = "ui_tabs_active['" .. unique_string .. "'] = {};" }
   129       ui.container{
   130         attr = { class = "ui_tabs" },
   131         content = function()
   132           for i, tab in ipairs(tabs) do
   133             local static_params = tabs.static_params or {}
   134             static_params.tab = tab.name
   135             static_params.tab_id = unique_string
   136             ui.partial{
   137               module           = tabs.module,
   138               view             = tabs.view,
   139               id               = tabs.id,
   140               params           = static_params,
   141               param_names      = { "page" },
   142               hourglass_target = "tab" .. unique_string .. "_icon_" .. tab.name,
   143               target           = "tab" .. unique_string .. "_content_" .. tab.name,
   144               content = function()
   145                 render_tab(tab, i == 1)
   146               end
   147             }
   148           end
   149         end
   150       }
   151     else
   152       local dyntab
   153       for i, tab in ipairs(tabs) do
   154         if tab.name == current_tabs_string then
   155           dyntab = tab
   156         end
   157       end
   158       if dyntab then
   159         local static_params = tabs.static_params or {}
   160         static_params.tab = dyntab.name
   161         static_params.tab_id = unique_string
   162         dyntab.params.tab_id = unique_string
   163         ui.partial{
   164           module           = tabs.module,
   165           view             = tabs.view,
   166           id               = tabs.id,
   167           params           = static_params,
   168           param_names      = { "page" },
   169           hourglass_target = "tab" .. unique_string .. "_icon_" .. dyntab.name,
   170           target           = "tab" .. unique_string .. "_content_" .. dyntab.name,
   171           content = function()
   172             execute.view{
   173               module = dyntab.module,
   174               view   = dyntab.view,
   175               id     = dyntab.id,
   176               params = dyntab.params,
   177             }
   178           end
   179         }
   180       end
   181     end
   182   end
   184 else -- 'classic tab'
   186   function ui.tabs(tabs)
   187     ui.container{
   188       attr = { class = "ui_tabs" },
   189       content = function()
   190         local params = param.get_all_cgi()
   191         local current_tab = params["tab"]
   192         ui.container{
   193           attr = { class = "ui_tabs_links" },
   194           content = function()
   195             for i, tab in ipairs(tabs) do
   196               params["tab"] = i > 1 and tab.name or nil
   197               ui.link{
   198                 attr = { 
   199                   class = (
   200                     tab.name == current_tab and "selected" .. (tab.class and (" " .. tab.class) or "") or
   201                     not current_tab and i == 1 and "selected" .. (tab.class and (" " .. tab.class) or "") or
   202                     "" .. (tab.class and (" " .. tab.class) or "")
   203                   )
   204                 },
   205                 module  = request.get_module(),
   206                 view    = request.get_view(),
   207                 id      = param.get_id_cgi(),
   208                 content = tab.label,
   209                 params  = params
   210               }
   211               slot.put(" ")
   212             end
   213           end
   214         }
   215         for i, tab in ipairs(tabs) do
   216           if tab.name == current_tab and i > 1 then
   217 						app.html_title.prefix = tab.label
   218 	        end
   219           if tab.name == current_tab or not current_tab and i == 1 then
   220             ui.container{
   221               attr = { class = "ui_tabs_content" },
   222               content = function()
   223                 if tab.content then
   224                   tab.content()
   225                 else
   226                   execute.view{
   227                     module = tab.module,
   228                     view   = tab.view,
   229                     id     = tab.id,
   230                     params = tab.params,
   231                   }
   232                 end
   233               end
   234             }
   235           end
   236         end
   237       end
   238     }
   239   end
   241 end
