luci-theme-bootstrap: fix menu url generation (#625)
[project/luci.git] / themes / luci-theme-bootstrap / luasrc / view / themes / bootstrap / header.htm
1 <%#
2  Copyright 2008 Steven Barth <steven@midlink.org>
3  Copyright 2008-2016 Jo-Philipp Wich <jow@openwrt.org>
4  Copyright 2012 David Menting <david@nut-bolt.nl>
5  Licensed to the public under the Apache License 2.0.
6 -%>
7
8 <%
9         local sys  = require "luci.sys"
10         local util = require "luci.util"
11         local http = require "luci.http"
12         local disp = require "luci.dispatcher"
13
14         local boardinfo = util.ubus("system", "board")
15
16         local request  = disp.context.path
17         local request2 = disp.context.request
18
19         local category = request[1]
20         local cattree  = category and disp.node(category)
21
22         local leaf = request2[#request2]
23
24         local tree = disp.node()
25         local node = disp.context.dispatched
26
27         local categories = disp.node_childs(tree)
28
29         local c = tree
30         local i, r
31
32         -- tag all nodes leading to this page
33         for i, r in ipairs(request) do
34                 if c.nodes and c.nodes[r] then
35                         c = c.nodes[r]
36                         c._menu_selected = true
37                 end
38         end
39
40         -- send as HTML5
41         http.prepare_content("text/html")
42
43         local function nodeurl(prefix, name, query)
44                 local u = url(prefix, name)
45                 if query then
46                         u = u .. http.build_querystring(query)
47                 end
48                 return pcdata(u)
49         end
50
51         local function render_tabmenu(prefix, node, level)
52                 if not level then
53                         level = 1
54                 end
55
56                 local childs = disp.node_childs(node)
57                 if #childs > 0 then
58                         if level > 2 then
59                                 write('<ul class="tabs">')
60                         end
61
62                         local selected_node
63                         local selected_name
64                         local i, v
65
66                         for i, v in ipairs(childs) do
67                                 local nnode = node.nodes[v]
68                                 if nnode._menu_selected then
69                                         selected_node = nnode
70                                         selected_name = v
71                                 end
72
73                                 if level > 2 then
74                                         write('<li class="tabmenu-item-%s %s"><a href="%s">%s</a></li>' %{
75                                                 v, (nnode._menu_selected or (node.leaf and v == leaf)) and 'active' or '',
76                                                 nodeurl(prefix, v, nnode.query),
77                                                 striptags(translate(nnode.title))
78                                         })
79                                 end
80                         end
81
82                         if level > 2 then
83                                 write('</ul>')
84                         end
85
86                         if selected_node then
87                                 render_tabmenu(prefix .. "/" .. selected_name, selected_node, level + 1)
88                         end
89                 end
90         end
91
92         local function render_submenu(prefix, node)
93                 local childs = disp.node_childs(node)
94                 if #childs > 0 then
95                         write('<ul class="dropdown-menu">')
96
97                         for i, r in ipairs(childs) do
98                                 local nnode = node.nodes[r]
99                                 write('<li><a href="%s">%s</a></li>' %{
100                                         nodeurl(prefix, r, nnode.query),
101                                         pcdata(striptags(translate(nnode.title)))
102                                 })
103                         end
104
105                         write('</ul>')
106                 end
107         end
108
109         local function render_topmenu()
110                 local childs = disp.node_childs(cattree)
111                 if #childs > 0 then
112                         write('<ul class="nav">')
113
114                         for i, r in ipairs(childs) do
115                                 local nnode = cattree.nodes[r]
116                                 local grandchildren = disp.node_childs(nnode)
117
118                                 if #grandchildren > 0 then
119                                         write('<li class="dropdown"><a class="menu" href="#">%s</a>' % pcdata(striptags(translate(nnode.title))))
120                                         render_submenu(category .. "/" .. r, nnode)
121                                         write('</li>')
122                                 else
123                                         write('<li><a href="%s">%s</a></li>' %{
124                                                 nodeurl(category, r, nnode.query),
125                                                 pcdata(striptags(translate(nnode.title)))
126                                         })
127                                 end
128                         end
129
130                         write('</ul>')
131                 end
132         end
133
134         local function render_changes()
135                 -- calculate the number of unsaved changes
136                 if tree.nodes[category] and tree.nodes[category].ucidata then
137                         local ucichanges = 0
138
139                         for i, j in pairs(require("luci.model.uci").cursor():changes()) do
140                                 for k, l in pairs(j) do
141                                         for m, n in pairs(l) do
142                                                 ucichanges = ucichanges + 1;
143                                         end
144                                 end
145                         end
146
147                         if ucichanges > 0 then
148                                 write('<a class="label notice" href="%s?redir=%s">%s: %d</a>' %{
149                                         url(category, 'uci/changes'),
150                                         http.urlencode(http.formvalue('redir') or REQUEST_URI),
151                                         translate('Unsaved Changes'),
152                                         ucichanges
153                                 })
154                         end
155                 end
156         end
157 -%>
158 <!DOCTYPE html>
159 <html lang="<%=luci.i18n.context.lang%>">
160         <head>
161                 <meta charset="utf-8">
162                 <title><%=striptags( (boardinfo.hostname or "?") .. ( (node and node.title) and ' - ' .. translate(node.title) or '')) %> - LuCI</title>
163                 <!--[if lt IE 9]><script src="<%=media%>/html5.js"></script><![endif]-->
164                 <meta name="viewport" content="initial-scale=1.0">
165                 <link rel="stylesheet" href="<%=media%>/cascade.css">
166                 <link rel="stylesheet" media="only screen and (max-device-width: 854px)" href="<%=media%>/mobile.css" type="text/css" />
167                 <link rel="shortcut icon" href="<%=media%>/favicon.ico">
168                 <% if node and node.css then %>
169                         <link rel="stylesheet" href="<%=resource%>/<%=node.css%>">
170                 <% end -%>
171                 <% if css then %>
172                         <style title="text/css"><%= css %></style>
173                 <% end -%>
174                 <script src="<%=resource%>/xhr.js"></script>
175                 <script src="<%=resource%>/jql.min.js"></script>
176         </head>
177
178         <body class="lang_<%=luci.i18n.context.lang%> <%- if node then %><%= striptags( node.title ) %><%- end %>">
179                 <header>
180                         <div class="fill">
181                                 <div class="container">
182                                         <a class="brand" href="#"><%=boardinfo.hostname or "?"%></a>
183                                         <% render_topmenu() %>
184                                         <div class="pull-right">
185                                                 <% render_changes() %>
186                                                 <span id="xhr_poll_status" style="display:none" onclick="XHR.running() ? XHR.halt() : XHR.run()">
187                                                         <span class="label success" id="xhr_poll_status_on"><%:Auto Refresh%> <%:on%></span>
188                                                         <span class="label" id="xhr_poll_status_off" style="display:none"><%:Auto Refresh%> <%:off%></span>
189                                                 </span>
190                                         </div>
191                                 </div>
192                         </div>
193                 </header>
194
195                 <%- if luci.sys.process.info("uid") == 0 and luci.sys.user.getuser("root") and not luci.sys.user.getpasswd("root") then -%>
196                         <div class="container">
197                                 <div class="alert-message warning">
198                                         <h4><%:No password set!%></h4>
199                                         <%:There is no password set on this router. Please configure a root password to protect the web interface and enable SSH.%><br>
200                                         <a href="<%=pcdata(luci.dispatcher.build_url("admin/system/admin"))%>"><%:Go to password configuration...%></a>
201                                 </div>
202                         </div>
203                 <%- end -%>
204
205                 <div id="maincontent" class="container">
206                         <% if category then render_tabmenu(category, cattree) end %>