xref: /help/help3xsl/help.js (revision a678c053)
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10// Pagination and fuzzy search
11var url = document.URL;
12var moduleRegex = new RegExp('text\\/(\\w+)\\/');
13var regexArray = moduleRegex.exec(url);
14var currentModule = null;
15// get the module name from the URL and remove the first character,
16// but first deal with snowflake Base
17if(url.indexOf('explorer/database/') !== -1) {
18    currentModule = 'BASE';
19} else {
20    currentModule = regexArray[1].toUpperCase().substring(1);
21}
22var results = null;
23var fullLinkified = '';
24var modules = ['CALC', 'WRITER', 'IMPRESS', 'DRAW', 'BASE', 'MATH', 'CHART', 'BASIC', 'SHARED'];
25var indexkids = function() { document.getElementsByClassName("index")[0].children; };
26// if user is not on a shared category page, limit the index to the current module + shared
27if(currentModule !== 'HARED') {
28    bookmarks = bookmarks.filter(function(obj) {
29            return obj['app'] === currentModule || obj['app'] === 'SHARED';
30        });
31}
32bookmarks.forEach(function(obj) {
33            fullLinkified += '<a href="' + obj['url'] + '" class="' + obj['app'] + '">' + obj['text'] + '</a>';
34        });
35function fullList() {
36    document.getElementsByClassName("index")[0].innerHTML = fullLinkified;
37    addIds();
38    Paginator(document.getElementsByClassName("index")[0]);
39}
40// add id to the first items of each category in the index. CSS ::before rule adds the heading text
41function addIds() {
42    for (var i = 0, len = indexkids.length; i < len; i++) {
43        indexkids[i].removeAttribute("id");
44    }
45    modules.forEach(function(module) {
46        var moduleHeader = document.getElementsByClassName(module)[0];
47        if (typeof moduleHeader !== 'undefined') {
48            // let's wrap the header in a span, so the ::before element will not become a link
49            moduleHeader.outerHTML = '<span id="' + module + '" class="' + module + '">' + moduleHeader.outerHTML + '</span>';
50        }
51    });
52}
53// render the unfiltered index list on page load
54fullList();
55// filter the index list based on search field input
56var search = document.getElementById('search-bar');
57var filter = function() {
58    var target = search.value.trim();
59    if (target.length < 1) {
60        fullList();
61        return;
62    }
63    results = fuzzysort.go(target, bookmarks, {threshold: -15000, key:'text'});
64    var filtered = '';
65    results.forEach(function(result) {
66        filtered += '<a href="' + result.obj['url'] + '" class="' + result.obj['app'] + '">' + fuzzysort.highlight(result) + '</a>';
67    });
68    document.getElementsByClassName("index")[0].innerHTML = filtered;
69    addIds();
70    Paginator(document.getElementsByClassName("index")[0]);
71};
72function debounce(fn, wait) {
73    var timeout;
74    return function() {
75        clearTimeout(timeout);
76        timeout = setTimeout(function() {
77            fn.apply(this, arguments);
78        }, (wait || 150));
79    };
80}
81search.addEventListener('keyup', debounce(filter, 100));
82// copy useful content to clipboard on mouse click
83var copyable = document.getElementsByClassName("input");
84for (var i = 0, len = copyable.length; i < len; i++) {
85    (function() {
86        var item = copyable[i];
87
88        function changeColor(item, color, colorToChangeBackTo) {
89            item.style.backgroundColor = color;
90            setTimeout(function() {
91                item.style.backgroundColor = colorToChangeBackTo;
92            }, 150);
93        }
94        item.onclick = function() {
95            document.execCommand("copy");
96            changeColor(item, "#18A303", "transparent");
97        };
98        item.addEventListener("copy", function(event) {
99            event.preventDefault();
100            if (event.clipboardData) {
101                event.clipboardData.setData("text/plain", item.textContent);
102            }
103        });
104    }());
105}
106// auto-expand contents per subitem
107var pathname = window.location.pathname;
108var pathRegex = /text\/.*\.html$/;
109var linkIndex = 0;
110var contentMatch = pathname.match(pathRegex);
111function linksMatch(content) {
112    var linkMatch = new RegExp(content);
113    var links = document.getElementById("Contents").getElementsByTagName("a");
114    for (var i = 0, len = links.length; i < len; i++) {
115        if (links[i].href.match(linkMatch)) {
116            return i;
117        }
118    }
119}
120linkIndex = linksMatch(contentMatch);
121if (typeof linkIndex !== "undefined") {
122    var current = document.getElementById("Contents").getElementsByTagName("a")[linkIndex];
123    var cItem = current.parentElement;
124    var parents = [];
125    while (cItem.parentElement && !cItem.parentElement.matches("#Contents") && parents.indexOf(cItem.parentElement) == -1) {
126        parents.push(cItem = cItem.parentElement);
127    }
128    var liParents = [].filter.call(parents, function(item) {
129        return item.matches("li");
130    });
131    for (var i = 0, len = liParents.length; i < len; i++) {
132        var input = liParents[i].querySelectorAll(':scope > input');
133        document.getElementById(input[0].id).checked = true;
134    }
135    current.classList.add('contents-current');
136}
137/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
138