summaryrefslogtreecommitdiff
path: root/help3xsl
diff options
context:
space:
mode:
authorIlmari Lauhakangas <ilmari.lauhakangas@libreoffice.org>2020-03-04 15:47:20 +0200
committerOlivier Hallot <olivier.hallot@libreoffice.org>2020-03-05 13:12:37 +0100
commit7fdef2928278c4bd5488547f833f503187ea8431 (patch)
tree909b29f11e8872e361262d3cc059d7623c9de901 /help3xsl
parent6e4e81d1d12dd131a8ceb95255544b8f51f302d8 (diff)
Reorganise and refactor stuff in help.js for readability
Change-Id: Ife1b412350be5ac046736312ff9216273eff281b Reviewed-on: https://gerrit.libreoffice.org/c/help/+/89973 Tested-by: Jenkins Reviewed-by: Olivier Hallot <olivier.hallot@libreoffice.org>
Diffstat (limited to 'help3xsl')
-rw-r--r--help3xsl/help.js112
1 files changed, 57 insertions, 55 deletions
diff --git a/help3xsl/help.js b/help3xsl/help.js
index 48767222e3..48fdca8bb6 100644
--- a/help3xsl/help.js
+++ b/help3xsl/help.js
@@ -11,90 +11,92 @@
var url = window.location.pathname;
var moduleRegex = new RegExp('text\\/(\\w+)\\/');
var regexArray = moduleRegex.exec(url);
-var currentModule = null;
-// get the module name from the URL and remove the first character,
-// but first deal with snowflake Base
-if(url.indexOf('explorer/database/') !== -1) {
- currentModule = 'BASE';
-} else {
- if (null === regexArray){// comes from search or elsewhere, no defined module in URL
- currentModule = 'HARED'
- }else{
- currentModule = regexArray[1].toUpperCase().substring(1);
- }
-}
-var results = null;
-var fullLinkified = '';
var modules = ['CALC', 'WRITER', 'IMPRESS', 'DRAW', 'BASE', 'MATH', 'CHART', 'BASIC', 'SHARED'];
-var index = document.getElementsByClassName("index")[0];
-var indexkids = index.children;
-// if user is not on a shared category page, limit the index to the current module + shared
-if(currentModule !== 'HARED') {
- bookmarks = bookmarks.filter(function(obj) {
- return obj['app'] === currentModule || obj['app'] === 'SHARED';
- });
+var indexEl = document.getElementsByClassName("index")[0];
+var fullLinks = fullLinkify(indexEl, bookmarks, modules, currentModule());
+var search = document.getElementById('search-bar');
+search.addEventListener('keyup', debounce(filter, 100, indexEl));
+// Preserve search input value during the session
+search.value = sessionStorage.getItem('searchsave');
+if (search.value !== undefined) {
+ filter(indexEl);
}
-bookmarks.forEach(function(obj) {
- fullLinkified += '<a href="' + obj['url'] + '" class="' + obj['app'] + '">' + obj['text'] + '</a>';
+window.addEventListener('unload', function(event) {
+ sessionStorage.setItem('searchsave', search.value);
+});
+// render the unfiltered index list on page load
+fillIndex(indexEl, fullLinks, modules);
+
+function currentModule() {
+ var module = '';
+ // get the module name from the URL and remove the first character,
+ // but first deal with snowflake Base
+ if(url.indexOf('explorer/database/') !== -1) {
+ module = 'BASE';
+ } else {
+ if (null === regexArray){// comes from search or elsewhere, no defined module in URL
+ module = 'HARED'
+ } else {
+ module = regexArray[1].toUpperCase().substring(1);
+ }
+ }
+ return module;
+};
+function fullLinkify(indexEl, bookmarks, modules, currentModule) {
+ var fullLinkified = '';
+ // if user is not on a shared category page, limit the index to the current module + shared
+ if(currentModule !== 'HARED') {
+ bookmarks = bookmarks.filter(function(obj) {
+ return obj['app'] === currentModule || obj['app'] === 'SHARED';
});
-function fullList() {
- index.innerHTML = fullLinkified;
- addIds();
- Paginator(index);
+ }
+ bookmarks.forEach(function(obj) {
+ fullLinkified += '<a href="' + obj['url'] + '" class="' + obj['app'] + '">' + obj['text'] + '</a>';
+ });
+ return fullLinkified;
}
-// add id to the first items of each category in the index. CSS ::before rule adds the heading text
-function addIds() {
- for (var i = 0, len = indexkids.length; i < len; i++) {
- indexkids[i].removeAttribute("id");
+function fillIndex(indexEl, content, modules) {
+ indexEl.innerHTML = content;
+ var indexKids = indexEl.children;
+ for (var i = 0, len = indexKids.length; i < len; i++) {
+ indexKids[i].removeAttribute("id");
}
modules.forEach(function(module) {
- var moduleHeader = index.getElementsByClassName(module)[0];
+ var moduleHeader = indexEl.getElementsByClassName(module)[0];
if (typeof moduleHeader !== 'undefined') {
// let's wrap the header in a span, so the ::before element will not become a link
moduleHeader.outerHTML = '<span id="' + module + '" class="' + module + '">' + moduleHeader.outerHTML + '</span>';
}
});
+ Paginator(indexEl);
}
-// render the unfiltered index list on page load
-fullList();
// filter the index list based on search field input
-var search = document.getElementById('search-bar');
-var filter = function() {
+function filter(indexList) {
+ var results = null;
var target = search.value.trim();
+ var filtered = '';
if (target.length < 1) {
- fullList();
+ fillIndex(indexEl, fullLinks, modules);
return;
}
results = fuzzysort.go(target, bookmarks, {threshold: -15000, key:'text'});
- var filtered = '';
+
results.forEach(function(result) {
filtered += '<a href="' + result.obj['url'] + '" class="' + result.obj['app'] + '">' + fuzzysort.highlight(result) + '</a>';
});
- index.innerHTML = filtered;
- addIds();
- Paginator(index);
+ fillIndex(indexList, filtered, modules);
+
};
-function debounce(fn, wait) {
+// delay the rendering of the filtered results while user is typing
+function debounce(fn, wait, indexList) {
var timeout;
return function() {
clearTimeout(timeout);
timeout = setTimeout(function() {
- fn.apply(this, arguments);
+ fn.call(this, indexList);
}, (wait || 150));
};
}
-search.addEventListener('keyup', debounce(filter, 100));
-
-// Preserve search input value during the session
-search.value = sessionStorage.getItem('searchsave');
-
-if (search.value !== undefined) {
- filter();
-}
-
-window.addEventListener('unload', function(event) {
- sessionStorage.setItem('searchsave', search.value);
-});
// copy pycode and bascode to clipboard on mouse click
// Show border when copy is done