xref: /help/help3xsl/help2.js (revision 3d0fcf7a)
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// Used to set Application in caseinline=APP
10function setApplSpan(SpanID) {
11    var module = getParameterByName("DbPAR");
12    if (module === null) {
13        module = "WRITER";
14    }
15    var y = document.getElementById(SpanID).getElementsByTagName("SPAN");
16    var n = y.length;
17    var foundSystem = false;
18    for (i = 0; i < n; i++) {
19        if (y[i].getAttribute("id") === null){
20            continue;
21        }
22        else if( y[i].getAttribute("id").startsWith(module)){
23            y[i].removeAttribute("hidden");
24            foundSystem=true;
25        }
26    }
27    for (i = 0; i < n; i++) {
28        if (y[i].getAttribute("id") === null){
29            continue;
30        }
31        else if( y[i].getAttribute("id").startsWith("default")){
32            if(!foundSystem){
33                y[i].removeAttribute("hidden");
34            }
35        }
36    }
37}
38// Used to set system in case, caseinline=SYSTEM
39function setSystemSpan(spanID) {
40    var system = getParameterByName("System");
41    // if no System in URL, get browser system
42    if (system === null) {
43        system = getSystem();
44    }
45    var y = document.getElementById(spanID).getElementsByTagName("SPAN");
46    var n = y.length;
47    var foundSystem = false;
48    for (i = 0; i < n; i++) {
49        if (y[i].getAttribute("id") === null){
50            continue;
51        }
52        else if( y[i].getAttribute("id").startsWith(system)){
53            y[i].removeAttribute("hidden");
54            foundSystem=true;
55        }
56    }
57    for (i = 0; i < n; i++) {
58        if (y[i].getAttribute("id") === null){
59            continue;
60        }
61        else if( y[i].getAttribute("id").startsWith("default")){
62            if(!foundSystem){
63                y[i].removeAttribute("hidden");
64            }
65        }
66    }
67}
68// Find spans that need the switch treatment and give it to them
69var spans = document.querySelectorAll("[class^=switch]");
70var n = spans.length;
71for (z = 0; z < n; z++) {
72    var id = spans[z].getAttribute("id");
73    if (id === null) {
74        continue;
75    }
76    else if (id.startsWith("swlnsys")) {
77        setSystemSpan(id);
78    } else {
79        setApplSpan(id);
80    }
81}
82/* add &DbPAR= and &System= to the links in DisplayArea div */
83/* skip for object files */
84function fixURL(module, system) {
85    var itemlink = document.getElementById("DisplayArea").getElementsByTagName("a");
86    var pSystem = (system === null) ? getSystem() : system;
87    var pAppl = (module === null) ? "WRITER" : module;
88    var n = itemlink.length;
89    for (var i = 0; i < n; i++) {
90        if (itemlink[i].getAttribute("class") != "objectfiles"){
91        setURLParam(itemlink[i], pSystem, pAppl);
92        }
93    }
94}
95//Set the params inside URL
96function setURLParam(itemlink, pSystem, pAppl) {
97    var href = itemlink.getAttribute("href");
98    if (href !== null) {
99        // skip external links
100        if (!href.startsWith("http")) {
101            // handle bookmark.
102            if (href.lastIndexOf('#') != -1) {
103                var postf = href.substring(href.lastIndexOf('#'), href.length);
104                var pref = href.substring(0, href.lastIndexOf('#'));
105                itemlink.setAttribute("href", pref + "?" + '&DbPAR=' + pAppl + '&System=' + pSystem + postf);
106            } else {
107                itemlink.setAttribute("href", href + "?" + '&DbPAR=' + pAppl + '&System=' + pSystem);
108            }
109        }
110    }
111}
112
113function getSystem() {
114    var system = "Unknown OS";
115    if (navigator.appVersion.indexOf("Win") != -1) system = "WIN";
116    if (navigator.appVersion.indexOf("Mac") != -1) system = "MAC";
117    if (navigator.appVersion.indexOf("X11") != -1) system = "UNIX";
118    if (navigator.appVersion.indexOf("Linux") != -1) system = "UNIX";
119    return system;
120}
121
122function getParameterByName(name, url) {
123    if (!url) {
124        url = window.location.href;
125    }
126    name = name.replace(/[\[\]]/g, "\\$&");
127    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
128    var results = regex.exec(url);
129    if (!results) {
130        return null;
131    }
132    if (!results[2]) {
133        return '';
134    }
135    return decodeURIComponent(results[2].replace(/\+/g, " "));
136}
137
138function existingLang(lang) {
139    if (lang === undefined) {
140        return 'en-US';
141    }
142
143    if (languagesSet.has(lang)) {
144        return lang;
145    }
146
147    lang = lang.replace(/[-_].*/, '');
148    if (languagesSet.has(lang)) {
149        return lang;
150    }
151
152    return 'en-US';
153}
154
155function setupModules(target, lang) {
156    var modulesNav = document.getElementById('modules-nav');
157    if (!modulesNav.classList.contains('loaded')) {
158        var html =
159            '<a href="' + target + lang + '/text/swriter/main0000.html?DbPAR=WRITER"><div class="writer-icon"></div>Writer</a>' +
160            '<a href="' + target + lang + '/text/scalc/main0000.html?DbPAR=CALC"><div class="calc-icon"></div>Calc</a>' +
161            '<a href="' + target + lang + '/text/simpress/main0000.html?DbPAR=IMPRESS"><div class="impress-icon"></div>Impress</a>' +
162            '<a href="' + target + lang + '/text/sdraw/main0000.html?DbPAR=DRAW"><div class="draw-icon"></div>Draw</a>' +
163            '<a href="' + target + lang + '/text/shared/explorer/database/main.html?DbPAR=BASE"><div class="base-icon"></div>Base</a>' +
164            '<a href="' + target + lang + '/text/smath/main0000.html?DbPAR=MATH"><div class="math-icon"></div>Math</a>' +
165            '<a href="' + target + lang + '/text/schart/main0000.html?DbPAR=CHART"><div class="chart-icon"></div>Chart</a>' +
166            '<a href="' + target + lang + '/text/sbasic/shared/main0601.html?DbPAR=BASIC"><div class="basic-icon"></div>Basic</a>';
167        modulesNav.innerHTML = html;
168        modulesNav.classList.add('loaded');
169    }
170}
171
172function setupLanguages(target, page) {
173    var langNav = document.getElementById('langs-nav');
174    if (!langNav.classList.contains('loaded')) {
175        var html = '';
176        languagesSet.forEach(function(lang) {
177            html += '<a href="' + target + lang + page + '">' + ((lang in languageNames)? languageNames[lang]: lang) + '</a>';
178        });
179        langNav.innerHTML = html;
180        langNav.classList.add('loaded');
181    }
182}
183
184// Test, if we are online
185if (document.body.getElementsByTagName('meta')) {
186    var _paq = _paq || [];
187    /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
188    _paq.push(['disableCookies']);
189    _paq.push(['trackPageView']);
190    _paq.push(['enableLinkTracking']);
191    (function() {
192    var u="//piwik.documentfoundation.org/";
193    _paq.push(['setTrackerUrl', u+'piwik.php']);
194    _paq.push(['setSiteId', '68']);
195    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
196    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
197    })();
198    var system = getParameterByName("System");
199} else {
200    var system = getSystem();
201}
202
203var module = getParameterByName("DbPAR");
204var helpID = getParameterByName("HID");
205fixURL(module,system);
206var dbg = getParameterByName("Debug");
207if (dbg == null) { dbg=0; }
208document.getElementById("DEBUG").style.display = (dbg == 0) ? "none":"block";
209document.getElementById("bm_module").innerHTML ="Module is: "+module;
210document.getElementById("bm_system").innerHTML ="System is: "+system;
211document.getElementById("bm_HID").innerHTML ="HID is: "+helpID;
212
213// Mobile devices need the modules and langs on page load
214if (Math.max(document.documentElement.clientWidth, window.innerWidth || 0) < 960) {
215    var e = new Event('change');
216    var modules = document.getElementById('modules');
217    var langs = document.getElementById('langs');
218    modules.dispatchEvent(e);
219    langs.dispatchEvent(e);
220}
221/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
222