1class GoLocal {
2
3    /** @type {HTMLDialogElement} */
4    #dialog;
5
6    /** @type {URL} */
7    #targetURL;
8
9    /**
10     *
11     * @param {Event} event
12     */
13    constructor(event) {
14        // decide if a dialog should be shown
15        if (localStorage.getItem('golocal')) {
16            // continue without dialog
17            return;
18        }
19
20        // still here? continue with dialog
21        this.#targetURL = event.target.href;
22        event.preventDefault();
23
24        // create HTML and attach to document
25        this.#dialog = this.createDialog();
26        const parent = document.body.querySelector('.dokuwiki') || document.body;
27        parent.appendChild(this.#dialog);
28
29
30        this.#dialog.showModal();
31    }
32
33    /**
34     * Create the dialog HTML and attach event listeners
35     *
36     * @returns {HTMLDialogElement}
37     */
38    createDialog() {
39        const dialog = document.createElement('dialog');
40        dialog.className = 'golocal-dialog';
41
42        const main = document.createElement('main');
43        dialog.appendChild(main);
44
45        const intro = document.createElement('div');
46        intro.innerHTML = LANG.plugins.golocal.dialog_intro;
47        main.appendChild(intro);
48
49        const url = new URL(window.location.href);
50        url.searchParams.set('do', 'golocal');
51        const install = document.createElement('a');
52        install.href = url.toString();
53        install.className = 'install';
54        install.textContent = LANG.plugins.golocal.dialog_install;
55        main.appendChild(install);
56
57        const footer = document.createElement('footer');
58        dialog.appendChild(footer);
59
60        const rememberLabel = document.createElement('label');
61
62        const rememberCheckbox = document.createElement('input');
63        rememberCheckbox.type = 'checkbox';
64        rememberLabel.appendChild(rememberCheckbox);
65
66        const rememberSpan = document.createElement('span');
67        rememberSpan.textContent = LANG.plugins.golocal.dialog_remember;
68        rememberLabel.appendChild(rememberSpan);
69
70        footer.appendChild(rememberLabel);
71
72        const continueButton = document.createElement('button');
73        continueButton.textContent = LANG.plugins.golocal.dialog_continue;
74        continueButton.addEventListener('click', (ev) => this.onContinue(ev, rememberCheckbox));
75        continueButton.autofocus = true;
76        footer.appendChild(continueButton);
77
78        dialog.addEventListener('close', this.onClose.bind(this));
79        return dialog;
80    }
81
82    /**
83     * Continue to the target URL
84     * @param {Event} ev The click event
85     * @param {HTMLInputElement} rememberCheckbox The checkbox to remember the choice (if available)
86     */
87    onContinue(ev, rememberCheckbox = null) {
88        if (rememberCheckbox && rememberCheckbox.checked) {
89            localStorage.setItem('golocal', 'true');
90        }
91
92        this.#dialog.close();
93        window.location.href = this.#targetURL;
94    }
95
96
97    onClose(e) {
98        this.#dialog.remove();
99    }
100}
101
102class GoLocalArch {
103
104    constructor() {
105        const ul = document.querySelector('.golocal-download');
106        if (!ul) {
107            return;
108        }
109
110        const os = this.getOs();
111
112        ul.querySelectorAll(`div.li.os-${os}`).forEach(li => {
113            li.classList.add('active');
114        });
115    }
116
117    /**
118     * Get the OS of the user
119     *
120     * @link https://tecadmin.net/javascript-detect-os
121     */
122    getOs() {
123        const userAgent = window.navigator.userAgent;
124        const platform = window.navigator.platform;
125        const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K', 'darwin'];
126        const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
127        const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
128        let os = null;
129
130        if (macosPlatforms.indexOf(platform) !== -1) {
131            os = 'macos';
132        } else if (iosPlatforms.indexOf(platform) !== -1) {
133            os = 'ios';
134        } else if (windowsPlatforms.indexOf(platform) !== -1) {
135            os = 'windows';
136        } else if (/Android/.test(userAgent)) {
137            os = 'android';
138        } else if (/Linux/.test(platform)) {
139            os = 'linux';
140        }
141
142        return os;
143    }
144}
145
146jQuery(function () {
147    jQuery('a.windows').each(function () {
148        // fix up all standard windows share links
149        let $this = jQuery(this);
150        $this.attr('href', $this.attr('href').replace('file:///', 'golocal://'));
151    }).click(function (e) {
152        new GoLocal(e);
153    });
154
155    new GoLocalArch();
156});
157