From 42bd0027b38bb39e08d9317b69fc3c8e170935c1 Mon Sep 17 00:00:00 2001
From: Jelle van der Waa
Date: Tue, 22 Jun 2021 22:27:17 +0200
Subject: [PATCH] Add archweb typeahead implementation
Use a pure vanilla JavaScript typeahead implementation to finally
deprecate the old jQuery version and typeahead library.
---
web/html/home.php | 37 ++----
web/html/index.php | 2 +-
web/html/js/bootstrap-typeahead.min.js | 1 -
web/html/js/typeahead.js | 151 +++++++++++++++++++++++++
web/html/pkgmerge.php | 24 ++--
web/template/pkgreq_form.php | 43 ++++---
6 files changed, 188 insertions(+), 70 deletions(-)
delete mode 100644 web/html/js/bootstrap-typeahead.min.js
create mode 100644 web/html/js/typeahead.js
diff --git a/web/html/home.php b/web/html/home.php
index be37e970..5ea79ee9 100644
--- a/web/html/home.php
+++ b/web/html/home.php
@@ -185,7 +185,7 @@ if (isset($_COOKIE["AURSID"])) {
@@ -202,34 +202,13 @@ if (isset($_COOKIE["AURSID"])) {
-
-
-
+
"+t+""})},render:function(t){var n=this;return t=e(t).map(function(t,r){return t=e(n.options.item).attr("data-value",r),t.find("a").html(n.highlighter(r)),t[0]}),this.$menu.html(t),this},next:function(t){var n=this.$menu.find(".active").removeClass("active"),r=n.next();r.length||(r=e(this.$menu.find("li")[0])),r.addClass("active")},prev:function(e){var t=this.$menu.find(".active").removeClass("active"),n=t.prev();n.length||(n=this.$menu.find("li").last()),n.addClass("active")},listen:function(){this.$element.on("blur",e.proxy(this.blur,this)).on("keypress",e.proxy(this.keypress,this)).on("keyup",e.proxy(this.keyup,this)),(e.browser.chrome||e.browser.webkit||e.browser.msie)&&this.$element.on("keydown",e.proxy(this.keydown,this)),this.$menu.on("click",e.proxy(this.click,this)).on("mouseenter","li",e.proxy(this.mouseenter,this))},move:function(e){if(!this.shown)return;switch(e.keyCode){case 9:case 13:case 27:e.preventDefault();break;case 38:e.preventDefault(),this.prev();break;case 40:e.preventDefault(),this.next()}e.stopPropagation()},keydown:function(t){this.suppressKeyPressRepeat=!~e.inArray(t.keyCode,[40,38,9,13,27]),this.move(t)},keypress:function(e){if(this.suppressKeyPressRepeat)return;this.move(e)},keyup:function(e){switch(e.keyCode){case 40:case 38:break;case 9:case 13:if(!this.shown)return;this.select();break;case 27:if(!this.shown)return;this.hide();break;default:this.lookup()}e.stopPropagation(),e.preventDefault()},blur:function(e){var t=this;setTimeout(function(){t.hide()},150)},click:function(e){e.stopPropagation(),e.preventDefault(),this.select()},mouseenter:function(t){this.$menu.find(".active").removeClass("active"),e(t.currentTarget).addClass("active")}},e.fn.typeahead=function(n){return this.each(function(){var r=e(this),i=r.data("typeahead"),s=typeof n=="object"&&n;i||r.data("typeahead",i=new t(this,s)),typeof n=="string"&&i[n]()})},e.fn.typeahead.defaults={source:[],items:8,menu:'',item:'',minLength:1},e.fn.typeahead.Constructor=t,e(function(){e("body").on("focus.typeahead.data-api",'[data-provide="typeahead"]',function(t){var n=e(this);if(n.data("typeahead"))return;t.preventDefault(),n.typeahead(n.data())})})}(window.jQuery)
\ No newline at end of file
diff --git a/web/html/js/typeahead.js b/web/html/js/typeahead.js
new file mode 100644
index 00000000..1b7252d7
--- /dev/null
+++ b/web/html/js/typeahead.js
@@ -0,0 +1,151 @@
+"use strict";
+
+const typeahead = (function() {
+ var input;
+ var form;
+ var suggest_type;
+ var list;
+ var submit = true;
+
+ function resetResults() {
+ if (!list) return;
+ list.style.display = "none";
+ list.innerHTML = "";
+ }
+
+ function getCompleteList() {
+ if (!list) {
+ list = document.createElement("UL");
+ list.setAttribute("class", "pkgsearch-typeahead");
+ form.appendChild(list);
+ setListLocation();
+ }
+ return list;
+ }
+
+ function onListClick(e) {
+ let target = e.target;
+ while (!target.getAttribute('data-value')) {
+ target = target.parentNode;
+ }
+ input.value = target.getAttribute('data-value');
+ if (submit) {
+ form.submit();
+ }
+ }
+
+ function setListLocation() {
+ if (!list) return;
+ const rects = input.getClientRects()[0];
+ list.style.top = (rects.top + rects.height) + "px";
+ list.style.left = rects.left + "px";
+ }
+
+ function loadData(letter, data) {
+ const pkgs = data.slice(0, 10); // Show maximum of 10 results
+
+ resetResults();
+
+ if (pkgs.length === 0) {
+ return;
+ }
+
+ const ul = getCompleteList();
+ ul.style.display = "block";
+ const fragment = document.createDocumentFragment();
+
+ for (let i = 0; i < pkgs.length; i++) {
+ const item = document.createElement("li");
+ const text = pkgs[i].replace(letter, '' + letter + '');
+ item.innerHTML = '' + text + '';
+ item.setAttribute('data-value', pkgs[i]);
+ fragment.appendChild(item);
+ }
+
+ ul.appendChild(fragment);
+ ul.addEventListener('click', onListClick);
+ }
+
+ function fetchData(letter) {
+ const url = '/rpc?type=' + suggest_type + '&arg=' + letter;
+ fetch(url).then(function(response) {
+ return response.json();
+ }).then(function(data) {
+ loadData(letter, data);
+ });
+ }
+
+ function onInputClick() {
+ if (input.value === "") {
+ resetResults();
+ return;
+ }
+ fetchData(input.value);
+ }
+
+ function onKeyDown(e) {
+ if (!list) return;
+
+ const elem = document.querySelector(".pkgsearch-typeahead li.active");
+ switch(e.keyCode) {
+ case 13: // enter
+ if (!submit) {
+ return;
+ }
+ if (elem) {
+ input.value = elem.getAttribute('data-value');
+ form.submit();
+ } else {
+ form.submit();
+ }
+ e.preventDefault();
+ break;
+ case 38: // up
+ if (elem && elem.previousElementSibling) {
+ elem.className = "";
+ elem.previousElementSibling.className = "active";
+ }
+ e.preventDefault();
+ break;
+ case 40: // down
+ if (elem && elem.nextElementSibling) {
+ elem.className = "";
+ elem.nextElementSibling.className = "active";
+ } else if (!elem && list.childElementCount !== 0) {
+ list.children[0].className = "active";
+ }
+ e.preventDefault();
+ break;
+ }
+ }
+
+ // debounce https://davidwalsh.name/javascript-debounce-function
+ function debounce(func, wait, immediate) {
+ var timeout;
+ return function() {
+ var context = this, args = arguments;
+ var later = function() {
+ timeout = null;
+ if (!immediate) func.apply(context, args);
+ };
+ var callNow = immediate && !timeout;
+ clearTimeout(timeout);
+ timeout = setTimeout(later, wait);
+ if (callNow) func.apply(context, args);
+ };
+ }
+
+ return {
+ init: function(type, inputfield, formfield, submitdata = true) {
+ suggest_type = type;
+ input = inputfield;
+ form = formfield;
+ submit = submitdata;
+
+ input.addEventListener("input", onInputClick);
+ input.addEventListener("keydown", onKeyDown);
+ window.addEventListener('resize', debounce(setListLocation, 150));
+ document.addEventListener("click", resetResults);
+ }
+ }
+}());
diff --git a/web/html/pkgmerge.php b/web/html/pkgmerge.php
index d583c239..d96562a7 100644
--- a/web/html/pkgmerge.php
+++ b/web/html/pkgmerge.php
@@ -25,7 +25,7 @@ if (has_credential(CRED_PKGBASE_DELETE)): ?>
= __('Enter the package name you wish to merge the package into. '); ?>
= __('Select the checkbox to confirm action.') ?>
-