simple js search
[pharext/replicator.pharext.org] / public / html.php
index e93122bc561b64062414b62ef8c7b056191915dc..659d19a06cd2d9b34aa636b60dc8cf0bf93a9761 100644 (file)
@@ -1,9 +1,14 @@
 <?php
 const INCLUDED = __FILE__;
+const NCURRENT = 2;
+
+$css = "concise/css/concise.min.css";
+$fnt = "//fonts.googleapis.com/css?family=Droid+Sans";
 
 require_once "index.php";
 
 ob_start($res);
+$res->addHeader("Link", "<" . dirname((new http\Env\Url)->path) . $css . ">; rel=preload; as=style");
 
 ?>
 <!doctype html>
@@ -11,8 +16,8 @@ ob_start($res);
        <head>
                <meta charset="utf-8">
                <title>Replicator</title>
-               <link rel="stylesheet" href="concise/css/concise.min.css">
-               <link href="//fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet" type="text/css">
+               <link rel="stylesheet" href="<?=$css?>">
+               <link rel="stylesheet" href="<?=$fnt?>">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <style>
                        body {
@@ -84,6 +89,23 @@ ob_start($res);
                                        width: auto;
                                }
                        }
+                       .old-version, #new-toggle {
+                               display: none;
+                       }
+                       .package-description {
+                               white-space: pre-line;
+                       }
+                       form * {
+                               display: inline-block;
+                               margin-right: 1em;
+                       }
+                       form label input{
+                               margin-left: 1em;
+                               vertical-align: middle;
+                       }
+                       form input[type=reset] {
+                               padding: 0;
+                       }
                </style>
        </head>
        <body>
@@ -98,10 +120,15 @@ ob_start($res);
                </div>
                <div class="container">
 
-                       <?php if (!empty($package)) : $versions = package_versions($package); ?>
+                       <?php if (!empty($package)) : $versions = package_versions($package); $info = package_info($package); ?>
 
                        <h2><?= htmlspecialchars($package) ?></h2>
-                       <table class="table table-full">
+                       <?php if ($info) : ?>
+                               <h3><?= htmlspecialchars($info["title"]) ?><br>
+                                       <small>License: <?= htmlspecialchars($info["license"]) ?></small></h3>
+                               <p class="package-description"><?= htmlspecialchars($info["description"]) ?></p>
+                       <?php endif; ?>
+                       <table class="table table-full versions">
                                <thead>
                                        <tr>
                                                <th class="text-left" colspan="2">Package</th>
@@ -112,9 +139,9 @@ ob_start($res);
                                </thead>
                                <tbody>
 
-                                       <?php foreach (array_reverse($versions) as $version => $phars) : ?>
+                                       <?php $i = 0; foreach (array_reverse($versions) as $version => $phars) : ++$i; ?>
                                                <?php foreach (array_map("array_values", $phars) as $ext => list($phar, $date, $size, $pharext)) : ?>
-                                       <tr>
+                                       <tr <?php if ($i > NCURRENT) : ?>class="old-version"<?php endif; ?> <?php if ($i === NCURRENT) : ?>id="old"<?php endif; ?>>
                                                <?php if (empty($ext)) : ?>
                                                <td class="text-left" rowspan="<?= count($phars) ?>">
                                                        <?= htmlspecialchars($package) ?>
@@ -144,19 +171,28 @@ ob_start($res);
 
                                </tbody>
                        </table>
+                               <?php if ($i >= 3) : ?>
+                                       <p class="small">
+                                               <a id="old-toggle" href="#old" onclick="toggleOldVersions(this)">Show
+                                                       <?=count($versions)-NCURRENT?> older version(s) &raquo;</a>
+                                               <a id="new-toggle" href="#" onclick="toggleOldVersions(this)">Show
+                                                       less versions &laquo;</a>
+                                       </p>
+                               <?php endif; ?>
                        <?php else:     ?>
 
                        <h2>Available Packages</h2>
-                       <ul class="list-inline">
+                       <form name="search"></form>
+                       <ul class="list-inline package-list">
                        <?php foreach (array_map("htmlspecialchars", $packages) as $index => $pkg) : ?>
                                <?php $next = strtolower($pkg{0}); ?>
                                <?php if (isset($prev) && $next != $prev) : ?>
 
                        </ul>
-                       <ul class="list-inline">
+                       <ul class="list-inline package-list">
                                <?php endif; ?>
 
-                               <li><a href="?<?= $pkg ?>"><?=  $pkg ?></a></li>
+                               <li id="<?= strtolower($pkg) ?>"><a href="?<?= $pkg ?>"><?=  $pkg ?></a></li>
                                <?php $prev = $next; ?>
                        <?php endforeach; ?>
 
@@ -217,6 +253,111 @@ gpg --verify <?= htmlspecialchars(basename($phar)).".asc" ?> \
                                </a>
                        </footer>
                </div>
+               <script type="text/javascript">
+               function searchPackages(search, regex) {
+                       console.log("searchPackages", search, regex);
+                       document.querySelectorAll("ul.package-list li").forEach(function(li) {
+                               if (regex) {
+                                       if (li.id.match(search.toLowerCase())) {
+                                               li.style.removeProperty("display");
+                                       } else {
+                                               li.style.display = "none";
+                                       }
+                               } else {
+                                       if (li.id.startsWith(search.toLowerCase())) {
+                                               li.style.removeProperty("display");
+                                       } else {
+                                               li.style.display = "none";
+                                       }
+                               }
+                       });
+               }
+               
+               document.body.onload = function() {
+                       var form = document.querySelector("form[name=search]");
+                       var input = document.createElement("input");
+                       var reset = document.createElement("input");
+                       var prefix_label = document.createElement("label");
+                       var prefix = document.createElement("input");
+                       var regex_label = document.createElement("label");
+                       var regex = document.createElement("input");
+
+                       form.onreset = function() {
+                               searchPackages("", false);
+                       };
+                       
+                       input.id = "s";
+                       input.autocomplete = "off";
+                       input.name = "s";
+                       input.type = "search";
+                       input.placeholder = "Search...";
+                       input.oninput = function() {
+                               searchPackages(input.value, regex.checked);
+                       };
+                       input.style.paddingRight = "4ch";
+                       form.appendChild(input);
+
+                       reset.id = "r";
+                       reset.name = "r";
+                       reset.type = "reset";
+                       reset.value = "☒";
+                       reset.title = "Reset";
+                       reset.style.marginLeft = "-4ch";
+                       reset.style.marginRight = "4ch";
+                       reset.style.border = "none";
+                       reset.style.background = "transparent";
+                       form.appendChild(reset);
+
+                       prefix.id = "prefix";
+                       prefix.name = "by";
+                       prefix.value = "prefix";
+                       prefix.type = "radio";
+                       prefix.defaultChecked = true;
+                       prefix.checked = true;
+                       prefix.onchange = function() {
+                               searchPackages(input.value, regex.checked);
+                       };
+                       //form.appendChild(prefix);
+                       prefix_label.innerText = "by Prefix";
+                       prefix_label.appendChild(prefix);
+                       form.appendChild(prefix_label);
+
+                       regex.id = "regex";
+                       regex.name = "by";
+                       regex.value = "regex";
+                       regex.type = "radio";
+                       regex.checked = false;
+                       regex.onchange = function() {
+                               searchPackages(input.value, regex.checked);
+                       };
+                       //form.appendChild(regex);
+                       regex_label.innerText = "by RegExp";
+                       regex_label.appendChild(regex);
+                       form.appendChild(regex_label);
+
+                       input.focus();
+               };
+
+               function toggleOldVersions(a) {
+                       var nodes, row_style;
+
+                       if (a.hash.substring(1) === "old") {
+                               row_style = "table-row";
+                               document.getElementById("old-toggle").style.display = "none";
+                               document.getElementById("new-toggle").style.display = "inline";
+                       } else {
+                               row_style = "none";
+                               document.getElementById("old-toggle").style.display = "inline";
+                               document.getElementById("new-toggle").style.display = "none";
+                       }
+
+                       nodes = document.querySelectorAll("table.versions>tbody>tr.old-version");
+
+                       for (var i = 0; i < nodes.length; ++i) {
+                               nodes.item(i).style.display = row_style;
+                       }
+               }
+               </script>
        </body>
 </html>
 <?php