Permalink
Browse files

Support nested locations.

  • Loading branch information...
trekawek committed Jan 25, 2019
1 parent fbfaf8d commit 76bd8bcfd42bcfa8e2b3b8d8a40c0be4e7d36be6
@@ -1,37 +1,57 @@
<?php
function array_starts_with($needle, $haystack) {
$needle = array_values($needle);
$haystack = array_values($haystack);
foreach ($needle as $i => $e) {
if ($haystack[$i] != $e) {
return false;
}
}
return true;
}
function find_device($uri) {
if (count($uri) == 0) {
return array(null, $uri);
}
$found_device = null;
$found_device_len = 0;
foreach (CONFIG['devices'] as $d) {
$name = array_map('trim', explode('/', $d['name']));
$name_len = count($name);
if (array_starts_with($name, $uri) && $found_device_len < $name_len) {
$found_device = $d;
$found_device_len = $name_len;
}
}
return array($found_device, array_slice($uri, $found_device_len));
}
function parse_uri() {
list($uri) = explode('?', $_SERVER['REQUEST_URI']);
$uri = explode('/', $uri);
$uri = array_values(array_filter($uri));
$device = null;
if (count($uri) > 0) {
foreach (CONFIG['devices'] as $d) {
if ($uri[0] == $d['name']) {
$device = $d;
array_shift($uri);
break;
}
}
}
list($device, $uri) = find_device($uri);
if ($device === null && count(CONFIG['devices']) == 1) {
$device = CONFIG['devices'][0];
}
if (count($uri) > 0) {
$current_action = implode('/', $uri);
} else {
$current_action = 'sensors';
}
if ($device == null) {
header('Location: '
.l(CONFIG['devices'][0], $current_action)
.($_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : ''));
die();
}
if (count($uri) > 0) {
$current_action = implode('/', $uri);
} else {
$current_action = 'sensors';
}
return array($device, $current_action);
}
@@ -28,6 +28,7 @@ function gtag(){dataLayer.push(arguments);}
<?php else: ?>
<link rel="stylesheet" href="/public/css/themes/<?php echo $current_theme ?>.min.css" >
<?php endif ?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.smartmenus/1.1.0/addons/bootstrap-4/jquery.smartmenus.bootstrap-4.min.css" integrity="sha256-IbVTniyadRTitKPpYX/0NvZ1dyrr0e1sD4+MR9q4CWM=" crossorigin="anonymous" />
<link rel="stylesheet" href="/public/css/style.css?v=1.3" >

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
@@ -53,37 +54,30 @@ function gtag(){dataLayer.push(arguments);}
<a class="nav-link <?php echo ($action == $current_action) ? 'active' : ''; ?>" href="<?php echo l($device, $action); ?>"><?php echo $name; ?></a>
</li>
<?php endforeach ?>
<?php require('partials/navbar/locations.php') ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php echo __('Theme') ?>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li>
<?php foreach(THEMES as $name => $desc): ?>
<a class="dropdown-item <?php echo ($name == $current_theme) ? 'active' : ''; ?>" href="<?php echo l($device, $current_action, array('theme' => $name)); ?>"><?php echo $desc ?></a>
<?php endforeach ?>
</div>
</li>
</ul>
</li>
<?php if (count(CONFIG['devices']) > 1): ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php echo __('Locations') ?>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<?php foreach(CONFIG['devices'] as $d): ?>
<a class="dropdown-item <?php echo ($d == $device) ? 'active' : ''; ?>" href="<?php echo l($d, $current_action); ?>"><?php echo $d['description']; ?></a>
<?php endforeach ?>
</div>
</li>
<?php endif ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-globe" aria-hidden="true"></i>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<?php foreach($supported_languages as $lang => $desc): ?>
<li>
<a class="dropdown-item <?php echo ($lang == $current_lang) ? 'active' : ''; ?>" href="<?php echo l($device, $current_action, array('lang' => $lang)); ?>"><img src="/public/img/flags/<?php echo $lang ?>.png"/> <?php echo $desc ?></a>
</li>
<?php endforeach ?>
</div>
</ul>
</li>
</ul>
</div>
@@ -0,0 +1,24 @@
<?php
function render_children($node, $device, $current_action) {
?>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<?php foreach ($node['children'] as $d => $n): ?>
<li class="dropdown">
<?php if (isset($n['name'])): ?>
<a class="dropdown-item <?php echo ($n['name'] == $device['name']) ? 'active' : '' ?>" href="<?php echo l(array('name' => $n['name']), $current_action) ?>">
<?php echo $d ?>
</a>
<?php else: ?>
<a class="dropdown-item" href="#">
<?php echo $d ?>
</a>
<?php endif ?>
<?php
if (count($n['children']) > 0) {
render_children($n, $device, $current_action);
}
?>
</li>
<?php endforeach ?>
</ul>
<?php } ?>
@@ -0,0 +1,22 @@
<?php if (count(CONFIG['devices']) > 1) {
require_once('partials/navbar/children.php');
$tree = array('children' => array());
foreach (CONFIG['devices'] as $d) {
$desc = array_map('trim', explode('/', $d['description']));
$node = &$tree;
foreach ($desc as $s) {
if (!isset($node['children'][$s])) {
$node['children'][$s] = array('children' => array());
}
$node = &$node['children'][$s];
}
$node['name'] = $d['name'];
}
?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<?php echo __('Locations') ?>
</a>
<?php render_children($tree, $device, $current_action) ?>
</li>
<?php } ?>
@@ -1,13 +1,14 @@
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.js" integrity="sha256-fNXJFIlca05BIO2Y5zh1xrShK3ME+/lYZ0j+ChxX2DA=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha256-63ld7aiYP6UxBifJWEzz87ldJyVnETUABZAYs5Qcsmc=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha256-WHwIASWxNdKakx7TceUP/BqWQYMcEIfeLNdFMoFfRWA=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha256-63ld7aiYP6UxBifJWEzz87ldJyVnETUABZAYs5Qcsmc=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js" integrity="sha256-eZNgBgutLI47rKzpfUji/dD9t6LRs2gI3YqXKdoDOmo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js" integrity="sha256-VBLiveTKyUZMEzJd6z2mhfxIqz3ZATCuVMawPZGzIfA=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/locale/pl.js" integrity="sha256-prSi7A1V9ZxDIIGoYopovpFgGHQodz6JVZu5Aad7o8s=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js" integrity="sha256-oSgtFCCmHWRPQ/JmR4OoZ3Xke1Pw4v50uh6pLcu+fIc=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js" integrity="sha256-Olnajf3o9kfkFGloISwP1TslJiWUDd7IYmfC+GdCKd4=" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.smartmenus/1.1.0/jquery.smartmenus.min.js" integrity="sha256-L+Zo9Q4bGfdY06BqwMYLDoacazH6GrQxkLavPdT0a44=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.smartmenus/1.1.0/addons/bootstrap-4/jquery.smartmenus.bootstrap-4.js" integrity="sha256-ggICBcXcHyst/t5vKIzkNSSwP1uGQnwIh/nm4M3n4fo=" crossorigin="anonymous"></script>
<script src="/public/js/main.js?v=1.11"></script>
</body>
</html>

0 comments on commit 76bd8bc

Please sign in to comment.