Subversion Repositories oidplus

Rev

Rev 704 | Rev 797 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 daniel-mar 1
/*
2
 * OIDplus 2.0
781 daniel-mar 3
 * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
4 daniel-mar 4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
9 daniel-mar 18
/*jshint esversion: 6 */
19
 
219 daniel-mar 20
// $('#html').jstree();
21
 
22
var current_node = "";
23
var popstate_running = false;
362 daniel-mar 24
// DEFAULT_LANGUAGE will be set by oidplus.min.js.php
360 daniel-mar 25
// language_messages will be set by oidplus.min.js.php
26
// language_tblprefix will be set by oidplus.min.js.php
424 daniel-mar 27
// csrf_token will be set by oidplus.min.js.php
561 daniel-mar 28
// samesite_policy will bet set by oidplus.min.js.php
219 daniel-mar 29
 
399 daniel-mar 30
var pageChangeCallbacks = [];
31
var pageChangeRequestCallbacks = [];
32
 
356 daniel-mar 33
function isInternetExplorer() {
641 daniel-mar 34
        // see also includes/functions.inc.php
356 daniel-mar 35
        var ua = window.navigator.userAgent;
36
        return ((ua.indexOf("MSIE ") > 0) || (ua.indexOf("Trident/") > 0));
37
}
38
 
2 daniel-mar 39
String.prototype.explode = function (separator, limit) {
40
        // https://stackoverflow.com/questions/4514323/javascript-equivalent-to-php-explode
41
        const array = this.split(separator);
42
        if (limit !== undefined && array.length >= limit) {
43
                array.push(array.splice(limit - 1).join(separator));
44
        }
45
        return array;
46
};
47
 
48
String.prototype.htmlentities = function () {
399 daniel-mar 49
        return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');//"
9 daniel-mar 50
};
2 daniel-mar 51
 
52
String.prototype.html_entity_decode = function () {
53
        return $('<textarea />').html(this).text();
9 daniel-mar 54
};
2 daniel-mar 55
 
781 daniel-mar 56
if (!String.prototype.replaceAll) {
57
        /**
58
         * String.prototype.replaceAll() polyfill
59
         * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
60
         * @author Chris Ferdinandi
61
         * @license MIT
62
         */
63
        String.prototype.replaceAll = function(str, newStr){
64
                // If a regex pattern
65
                if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
66
                        return this.replace(str, newStr);
67
                }
68
                // If a string
69
                return this.replace(new RegExp(str, 'g'), newStr);
70
        };
71
}
72
 
73
if (!String.prototype.startsWith) {
74
        // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#polyfill
75
        Object.defineProperty(String.prototype, 'startsWith', {
76
                value: function(search, rawPos) {
77
                        var pos = rawPos > 0 ? rawPos|0 : 0;
78
                        return this.substring(pos, pos + search.length) === search;
79
                }
80
        });
81
}
82
 
112 daniel-mar 83
function getMeta(metaName) {
561 daniel-mar 84
        const metas = $('meta[name='+metaName+']');
85
        return (metas.length == 0) ? '' : metas[0].content;
112 daniel-mar 86
}
87
 
88
function getOidPlusSystemTitle() {
360 daniel-mar 89
        return getMeta('OIDplus-SystemTitle'); // do not translate
112 daniel-mar 90
}
91
 
5 daniel-mar 92
function combine_systemtitle_and_pagetitle(systemtitle, pagetitle) {
309 daniel-mar 93
        // Please also change the function in index.php
5 daniel-mar 94
        if (systemtitle == pagetitle) {
95
                return systemtitle;
96
        } else {
309 daniel-mar 97
                return pagetitle + ' - ' + systemtitle;
5 daniel-mar 98
        }
99
}
100
 
385 daniel-mar 101
function getSystemUrl(relative) {
102
        var url = new URL(window.location.href);
103
        if (relative) {
104
                return url.pathname;
105
        } else {
106
                return url.href.substr(0, url.href.length-url.search.length);
107
        }
108
}
109
 
2 daniel-mar 110
function getTreeLoadURL() {
111
        var url = new URL(window.location.href);
112
        var goto = url.searchParams.get("goto");
641 daniel-mar 113
        return (goto != null) ? "ajax.php?csrf_token="+encodeURIComponent(csrf_token)+"&action=tree_load&anticache="+Date.now()+"&goto="+encodeURIComponent(goto)
114
                              : "ajax.php?csrf_token="+encodeURIComponent(csrf_token)+"&action=tree_load&anticache="+Date.now();
2 daniel-mar 115
}
116
 
117
function reloadContent() {
154 daniel-mar 118
        // window.location.href = "?goto="+encodeURIComponent(current_node);
399 daniel-mar 119
        if (openOidInPanel(current_node, false)) {
450 daniel-mar 120
                if(!$('#oidtree').jstree(true).get_node(current_node)) {
121
                        // Avoid that a language change at "oidplus:srvreg_status" won't redirect the user to "oidplus:srv_registration" because of the reselection during refresh
122
                        $('#oidtree').jstree("deselect_all");
123
                }
124
 
399 daniel-mar 125
                $('#oidtree').jstree("refresh");
126
        }
2 daniel-mar 127
}
128
 
107 daniel-mar 129
function x_rec(x_data, i) {
130
        $('#oidtree').jstree('open_node', x_data[i], function(e, data) {
131
                if (i+1 < x_data.length) {
132
                        x_rec(x_data, i+1);
133
                } else {
114 daniel-mar 134
                        popstate_running = true; // don't call openOidInPanel again
135
                        try {
136
                                $('#oidtree').jstree('select_node', x_data[i]);
137
                        } catch (err) {
138
                                popstate_running = false;
139
                        } finally {
140
                                popstate_running = false;
141
                        }
107 daniel-mar 142
                }
143
        });
144
}
2 daniel-mar 145
 
399 daniel-mar 146
function performCloseQueryCB() {
147
        for (var i=0; i<pageChangeRequestCallbacks.length; i++) {
148
                if (!pageChangeRequestCallbacks[i][0](pageChangeRequestCallbacks[i][1])) return false;
149
        }
150
        pageChangeRequestCallbacks = [];
151
        return true; // may close
152
}
153
 
154
function performCloseCB() {
155
        for (var i=0; i<pageChangeCallbacks.length; i++) {
156
                pageChangeCallbacks[i][0](pageChangeCallbacks[i][1]);
157
        }
158
        pageChangeCallbacks = [];
159
}
160
 
161
function openOidInPanel(id, reselect/*=false*/, anchor/*=''*/, force/*=false*/) {
699 daniel-mar 162
        reselect = (typeof reselect === 'undefined') ? false : reselect; // do not translate
163
        anchor = (typeof anchor === 'undefined') ? '' : anchor; // do not translate
164
        force = (typeof force === 'undefined') ? false : force; // do not translate
150 daniel-mar 165
 
399 daniel-mar 166
        var mayClose = performCloseQueryCB();
167
        if (!force && !mayClose) return false;
168
 
169
        performCloseCB();
405 daniel-mar 170
 
704 daniel-mar 171
        $.xhrPool.abortAll();
172
 
107 daniel-mar 173
        if (reselect) {
2 daniel-mar 174
                $('#oidtree').jstree('deselect_all');
107 daniel-mar 175
 
114 daniel-mar 176
                popstate_running = true; // don't call openOidInPanel during tree selection
177
                try {
178
                        // If the node is already loaded in the tree, select it
179
                        if (!$('#oidtree').jstree('select_node', id)) {
180
                                // If the node is not loaded, then we try to search it.
181
                                // If it can be found, then open all parent nodes and select the node
182
                                $.ajax({
183
                                        url:"ajax.php",
184
                                        method:"POST",
544 daniel-mar 185
                                        beforeSend: function(jqXHR, settings) {
704 daniel-mar 186
                                                //$.xhrPool.abortAll();
544 daniel-mar 187
                                                $.xhrPool.add(jqXHR);
188
                                        },
189
                                        complete: function(jqXHR, text) {
190
                                                $.xhrPool.remove(jqXHR);
191
                                        },
114 daniel-mar 192
                                        data:{
424 daniel-mar 193
                                                csrf_token:csrf_token,
114 daniel-mar 194
                                                action:"tree_search",
641 daniel-mar 195
                                                search:id,
196
                                                anticache:Date.now()
114 daniel-mar 197
                                        },
198
                                        error:function(jqXHR, textStatus, errorThrown) {
544 daniel-mar 199
                                                if (errorThrown == "abort") return;
360 daniel-mar 200
                                                console.error(_L("Error: %1",errorThrown));
114 daniel-mar 201
                                        },
202
                                        success:function(data) {
203
                                                if ("error" in data) {
204
                                                        console.error(data);
205
                                                } else if ((data instanceof Array) && (data.length > 0)) {
206
                                                        x_rec(data, 0);
207
                                                } else {
208
                                                        console.error(data);
209
                                                }
210
                                        }
211
                                });
107 daniel-mar 212
                        }
114 daniel-mar 213
                } catch (err) {
214
                        popstate_running = false;
215
                } finally {
216
                        popstate_running = false;
217
                }
2 daniel-mar 218
        }
219
 
114 daniel-mar 220
        // This loads the actual content
221
 
405 daniel-mar 222
        // document.title = ""; // <-- we may not do this, otherwise Firefox won't
223
        //                            show titles in the browser history (right-click
224
        //                            on back-button), although document.title() is
499 daniel-mar 225
        //                            set inside the AJAX-callback [Firefox bug?!]
405 daniel-mar 226
 
2 daniel-mar 227
        $('#real_title').html("&nbsp;");
355 daniel-mar 228
        $('#real_content').html(_L("Loading..."));
108 daniel-mar 229
        $('#static_link').attr("href", "index.php?goto="+encodeURIComponent(id));
183 daniel-mar 230
        $("#gotoedit").val(id);
2 daniel-mar 231
 
232
        // Normal opening of a description
560 daniel-mar 233
        $.ajax({
234
                url:"ajax.php",
235
                method:"GET",
236
                beforeSend: function(jqXHR, settings) {
704 daniel-mar 237
                        //$.xhrPool.abortAll();
560 daniel-mar 238
                        $.xhrPool.add(jqXHR);
239
                },
240
                complete: function(jqXHR, text) {
241
                        $.xhrPool.remove(jqXHR);
242
                },
243
                data:{
244
                        csrf_token:csrf_token,
245
                        action:"get_description",
641 daniel-mar 246
                        id:id,
247
                        anticache:Date.now()
560 daniel-mar 248
                },
249
                error:function(jqXHR, textStatus, errorThrown) {
250
                        if (errorThrown == "abort") return;
251
                        alert(_L("Failed to load content: %1",errorThrown));
252
                        console.error(_L("Error: %1",errorThrown));
253
                },
254
                success:function(data) {
107 daniel-mar 255
                        if ("error" in data) {
360 daniel-mar 256
                                alert(_L("Failed to load content: %1",data.error));
107 daniel-mar 257
                                console.error(data.error);
560 daniel-mar 258
                        } else if (data.status >= 0) {
259
                                data.id = id;
107 daniel-mar 260
 
560 daniel-mar 261
                                var state = {
262
                                        "node_id":id,
263
                                        "titleHTML":(data.icon ? '<img src="'+data.icon+'" width="48" height="48" alt="'+data.title.htmlentities()+'"> ' : '') + data.title.htmlentities(),
264
                                        "textHTML":data.text,
265
                                        "staticlinkHREF":"index.php?goto="+encodeURIComponent(id),
266
                                };
267
                                if (current_node != id) {
268
                                        window.history.pushState(state, data.title, "?goto="+encodeURIComponent(id));
269
                                } else {
270
                                        window.history.replaceState(state, data.title, "?goto="+encodeURIComponent(id));
271
                                }
2 daniel-mar 272
 
560 daniel-mar 273
                                document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), data.title);
2 daniel-mar 274
 
560 daniel-mar 275
                                if (data.icon) {
276
                                        $('#real_title').html('<img src="'+data.icon+'" width="48" height="48" alt="'+data.title.htmlentities()+'"> ' + data.title.htmlentities());
277
                                } else {
278
                                        $('#real_title').html(data.title.htmlentities());
279
                                }
280
                                $('#real_content').html(data.text);
281
                                document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), data.title);
282
                                current_node = id;
405 daniel-mar 283
 
560 daniel-mar 284
                                if (anchor != '') {
285
                                        jumpToAnchor(anchor);
286
                                }
32 daniel-mar 287
                        } else {
560 daniel-mar 288
                                alert(_L("Failed to load content: %1",data.status));
289
                                console.error(data);
32 daniel-mar 290
                        }
560 daniel-mar 291
                }
2 daniel-mar 292
        });
399 daniel-mar 293
 
294
        return true;
2 daniel-mar 295
}
296
 
107 daniel-mar 297
// This function opens the "parentID" node, and then selects the "childID" node (which should be beneath the parent node)
2 daniel-mar 298
function openAndSelectNode(childID, parentID) {
299
        if ($('#oidtree').jstree(true).get_node(parentID)) {
107 daniel-mar 300
                $('#oidtree').jstree('open_node', parentID, function(e, data) { // open parent node
2 daniel-mar 301
                        if ($('#oidtree').jstree(true).get_node(childID)) { // is the child there?
107 daniel-mar 302
                                $('#oidtree').jstree('deselect_all').jstree('select_node', childID); // select it
2 daniel-mar 303
                        } else {
304
                                // This can happen if the content page contains brand new items which are not in the treeview yet
183 daniel-mar 305
                                $("#gotoedit").val(childID);
154 daniel-mar 306
                                window.location.href = "?goto="+encodeURIComponent(childID);
2 daniel-mar 307
                        }
308
                }, true);
309
        } else {
310
                // This should usually not happen
183 daniel-mar 311
                $("#gotoedit").val(childID);
154 daniel-mar 312
                window.location.href = "?goto="+encodeURIComponent(childID);
2 daniel-mar 313
        }
314
}
315
 
316
$(window).on("popstate", function(e) {
399 daniel-mar 317
        if (!performCloseQueryCB()) {
318
                // TODO: does not work!!! The "back/forward" action will be cancelled, but the browser still thinks it was successful,
319
                // so if you do it again, you will then jump 2 pages back, etc!
320
                // This does also not help:
321
                //window.history.pushState(e.originalEvent.state, e.originalEvent.title, e.originalEvent.url);
322
                //window.history.forward();
323
                return;
324
        }
325
 
2 daniel-mar 326
        popstate_running = true;
327
        try {
328
                var data = e.originalEvent.state;
329
 
330
                current_node = data.node_id;
327 daniel-mar 331
                $("#gotoedit").val(current_node);
107 daniel-mar 332
                $('#oidtree').jstree('deselect_all').jstree('select_node', data.node_id);
2 daniel-mar 333
                $('#real_title').html(data.titleHTML);
334
                $('#real_content').html(data.textHTML);
335
                $('#static_link').attr("href", data.staticlinkHREF);
112 daniel-mar 336
                document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), data.titleHTML.html_entity_decode());
2 daniel-mar 337
        } catch (err) {
338
                popstate_running = false;
339
        } finally {
340
                popstate_running = false;
341
        }
342
});
343
 
344
$(document).ready(function () {
399 daniel-mar 345
        /*
346
        window.onbeforeunload = function(e) {
347
                // TODO: This won't be called because TinyMCE overrides it??
348
                // TODO: when the user accepted the query in performCloseQueryCB(), then the message will be shown again by the browser!
349
                if (!performCloseQueryCB()) {
350
                        // Cancel the event
351
                        e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
352
                        // Chrome requires returnValue to be set
353
                        e.returnValue = '';
354
                } else {
355
                        // the absence of a returnValue property on the event will guarantee the browser unload happens
356
                        delete e['returnValue'];
357
                }
358
        };
359
        */
214 daniel-mar 360
 
2 daniel-mar 361
        // --- JsTree
362
 
363
        $('#oidtree')
364
        .jstree({
365
                plugins: ['massload','search','conditionalselect'],
366
                'core' : {
367
                        'data' : {
368
                                "url" : getTreeLoadURL(),
369
                                "data" : function (node) {
370
                                        return { "id" : node.id };
371
                                }
372
                        },
373
                        "multiple": false
374
                },
375
                'conditionalselect' : function (node) {
68 daniel-mar 376
                        if (node.original.conditionalselect !== undefined) {
377
                                return eval(node.original.conditionalselect);
2 daniel-mar 378
                        } else {
399 daniel-mar 379
                                return performCloseQueryCB();
2 daniel-mar 380
                        }
381
                },
382
        })
383
        .on('ready.jstree', function (e, data) {
384
                var url = new URL(window.location.href);
385
                var goto = url.searchParams.get("goto");
386
                if (goto == null) goto = "oidplus:system"; // the page was not called with ?goto=...
183 daniel-mar 387
                $("#gotoedit").val(goto);
2 daniel-mar 388
 
107 daniel-mar 389
                // By setting current_node, select_node() will not cause ajax.php?action=get_description to load (since we already loaded the first static content via PHP, for search engines mainly)
2 daniel-mar 390
                // But then we need to set the history state manually
391
                current_node = goto;
100 daniel-mar 392
                window.history.replaceState({
107 daniel-mar 393
                        "node_id":goto,
100 daniel-mar 394
                        "titleHTML":$('#real_title').html(),
395
                        "textHTML":$('#real_content').html(),
108 daniel-mar 396
                        "staticlinkHREF":"index.php?goto="+encodeURIComponent(goto),
113 daniel-mar 397
                }, $('#real_title').html(), "?goto="+encodeURIComponent(goto));
2 daniel-mar 398
 
399
                if (goto != null) data.instance.select_node([goto]);
128 daniel-mar 400
 
643 daniel-mar 401
                setTimeout(glayoutWorkaroundAC, 100);
137 daniel-mar 402
                setTimeout(glayoutWorkaroundB, 100);
2 daniel-mar 403
        })
404
        .on('select_node.jstree', function (node, selected, event) {
120 daniel-mar 405
                mobileNavClose();
95 daniel-mar 406
 
9 daniel-mar 407
                var id = selected.node.id;
114 daniel-mar 408
                if ((!popstate_running) && (current_node != id)) {
399 daniel-mar 409
                        // 4th argument: we force the reload (because in the
410
                        // conditional select above, we already asked if
411
                        // tinyMCE needs to be saved)
412
                        openOidInPanel(id, false, '', true);
2 daniel-mar 413
                }
414
        });
415
 
416
        // --- Layout
417
 
561 daniel-mar 418
        $("#system_title_menu")[0].style.display = "block";
120 daniel-mar 419
 
355 daniel-mar 420
        var tmpObjectTree = _L("OBJECT TREE").replace(/(.{1})/g,"$1<br>");
421
        tmpObjectTree = tmpObjectTree.substring(0, tmpObjectTree.length-"<br>".length);
422
 
120 daniel-mar 423
        $('#oidtree').addClass('ui-layout-west');
424
        $('#content_window').addClass('ui-layout-center');
425
        $('#system_title_bar').addClass('ui-layout-north');
128 daniel-mar 426
        glayout = $('#frames').layout({
120 daniel-mar 427
                north__size:                  40,
428
                north__slidable:              false,
429
                north__closable:              false,
430
                north__resizable:             false,
431
                west__size:                   450,
432
                west__spacing_closed:         20,
433
                west__togglerLength_closed:   230,
695 daniel-mar 434
                west__togglerAlign_closed:    "center", // TODO: does not work! The text "OBJECT TREE" is still at the top and nearly cut off by the title bar!!!
355 daniel-mar 435
                west__togglerContent_closed:  tmpObjectTree,
436
                west__togglerTip_closed:      _L("Open & Pin Menu"),
437
                west__sliderTip:              _L("Slide Open Menu"),
120 daniel-mar 438
                west__slideTrigger_open:      "mouseover",
439
                center__maskContents:         true // IMPORTANT - enable iframe masking
440
        });
183 daniel-mar 441
 
185 daniel-mar 442
        $("#gotobox").addClass("mobilehidden");
355 daniel-mar 443
        $("#languageBox").addClass("mobilehidden");
561 daniel-mar 444
        $("#gotobox")[0].style.display = "block";
183 daniel-mar 445
        $('#gotoedit').keypress(function(event) {
446
                var keycode = (event.keyCode ? event.keyCode : event.which);
447
                if (keycode == '13') {
448
                        gotoButtonClicked();
449
                }
450
        });
376 daniel-mar 451
});
2 daniel-mar 452
 
643 daniel-mar 453
function glayoutWorkaroundAC() {
211 daniel-mar 454
        // "Bug A": Sometimes, the design is completely destroyed after reloading the page. It does not help when glayout.resizeAll()
137 daniel-mar 455
        //          is called at the beginning (e.g. during the ready function), and it does not help if we wait 500ms.
456
        //          So we do it all the time. It has probably something to do with slow loading times, since the error
457
        //          does only appear when the page is "blank" for a short while while it is loading.
458
        glayout.resizeAll();
287 daniel-mar 459
 
460
        // "Bug C": With Firefox (And sometimes with Chrome), there is a gap between the content-window (including scroll bars)
461
        //          and the right corner of the screen. Removing the explicit width solves this problem.
561 daniel-mar 462
        $("#content_window")[0].style.removeProperty("width");
643 daniel-mar 463
 
464
        setTimeout(glayoutWorkaroundAC, 100);
137 daniel-mar 465
}
466
 
467
function glayoutWorkaroundB() {
468
        // "Bug B": Sometimes, after reload, weird space between oidtree and content window, because oidtree has size of 438px
561 daniel-mar 469
        $("#oidtree")[0].style.width = "450px";
137 daniel-mar 470
}
471
 
120 daniel-mar 472
function mobileNavClose() {
473
        if ($("#system_title_menu").is(":hidden")) {
474
                return;
475
        }
476
 
477
        $("#oidtree").slideUp("medium").promise().done(function() {
478
                $("#oidtree").addClass("ui-layout-west");
479
                $("#oidtree").show();
185 daniel-mar 480
//              $("#gotobox").hide();
355 daniel-mar 481
//              $("#languageBox").hide();
185 daniel-mar 482
                $("#gotobox").addClass("mobilehidden");
355 daniel-mar 483
                $("#languageBox").addClass("mobilehidden");
120 daniel-mar 484
        });
485
        $("#system_title_menu").removeClass("active");
95 daniel-mar 486
}
487
 
120 daniel-mar 488
function mobileNavOpen() {
489
        $("#oidtree").hide();
490
        $("#oidtree").removeClass("ui-layout-west");
491
        $("#oidtree").slideDown("medium");
185 daniel-mar 492
//      $("#gotobox").show();
355 daniel-mar 493
//      $("#languageBox").show();
185 daniel-mar 494
        $("#gotobox").removeClass("mobilehidden");
355 daniel-mar 495
        $("#languageBox").removeClass("mobilehidden");
120 daniel-mar 496
        $("#system_title_menu").addClass("active");
497
}
498
 
105 daniel-mar 499
function mobileNavButtonClick(sender) {
120 daniel-mar 500
        if ($("#oidtree").hasClass("ui-layout-west")) {
501
                mobileNavOpen();
103 daniel-mar 502
        } else {
120 daniel-mar 503
                mobileNavClose();
103 daniel-mar 504
        }
505
}
105 daniel-mar 506
 
507
function mobileNavButtonHover(sender) {
508
        sender.classList.toggle("hover");
509
}
183 daniel-mar 510
 
511
function gotoButtonClicked() {
704 daniel-mar 512
        openOidInPanel($("#gotoedit").val(), true);
183 daniel-mar 513
}
199 daniel-mar 514
 
219 daniel-mar 515
function jumpToAnchor(anchor) {
516
        window.location.href = "#" + anchor;
517
}
518
 
355 daniel-mar 519
function getCookie(cname) {
520
        // Source: https://www.w3schools.com/js/js_cookies.asp
521
        var name = cname + "=";
522
        var decodedCookie = decodeURIComponent(document.cookie);
523
        var ca = decodedCookie.split(';');
524
        for(var i = 0; i <ca.length; i++) {
525
                var c = ca[i];
526
                while (c.charAt(0) == ' ') {
527
                        c = c.substring(1);
528
                }
529
                if (c.indexOf(name) == 0) {
530
                        return c.substring(name.length, c.length);
531
                }
532
        }
533
        return undefined;
534
}
535
 
356 daniel-mar 536
function setCookie(cname, cvalue, exdays, path) {
537
        var d = new Date();
538
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
539
        var expires = exdays == 0 ? "" : "; expires="+d.toUTCString();
561 daniel-mar 540
        document.cookie = cname + "=" + cvalue + expires + ";path=" + path + ";SameSite=" + samesite_policy;
356 daniel-mar 541
}
542
 
355 daniel-mar 543
function setLanguage(lngid) {
356 daniel-mar 544
        setCookie('LANGUAGE', lngid, 0/*Until browser closes*/, location.pathname);
545
 
546
        $(".lng_flag").each(function(){
547
                $(this).addClass("picture_ghost");
355 daniel-mar 548
        });
675 daniel-mar 549
        $("#lng_flag_"+$.escapeSelector(lngid)).removeClass("picture_ghost");
356 daniel-mar 550
 
551
        if (isInternetExplorer()) {
552
                // Internet Explorer has problems with sending new cookies to new AJAX requests, so we reload the page completely
553
                window.location.reload();
554
        } else {
561 daniel-mar 555
                // TODO: Small detail: The "Go" button also needs to be re-translated
360 daniel-mar 556
                reloadContent();
356 daniel-mar 557
                mobileNavClose();
558
        }
355 daniel-mar 559
}
560
 
561
function getCurrentLang() {
360 daniel-mar 562
        // Note: If the argument "?lang=" is used, PHP will automatically set a Cookie, so it is OK when we only check for the cookie
699 daniel-mar 563
        var lang = getCookie('LANGUAGE'); // do not translate
564
        return (typeof lang != 'undefined') ? lang : DEFAULT_LANGUAGE; // do not translate
355 daniel-mar 565
}
566
 
360 daniel-mar 567
function _L() {
568
        var args = Array.prototype.slice.call(arguments);
506 daniel-mar 569
        var str = args.shift().trim();
360 daniel-mar 570
 
571
        var tmp = "";
699 daniel-mar 572
        if (typeof language_messages[getCurrentLang()] == 'undefined') { // do not translate
360 daniel-mar 573
                tmp = str;
574
        } else {
575
                var msg = language_messages[getCurrentLang()][str];
699 daniel-mar 576
                if (typeof msg != 'undefined') { // do not translate
360 daniel-mar 577
                        tmp = msg;
578
                } else {
579
                        tmp = str;
580
                }
581
        }
582
 
583
        tmp = tmp.replace('###', language_tblprefix);
584
 
585
        var n = 1;
586
        while (args.length > 0) {
587
                var val = args.shift();
588
                tmp = tmp.replace("%"+n, val);
589
                n++;
590
        }
591
 
370 daniel-mar 592
        tmp = tmp.replace("%%", "%");
593
 
360 daniel-mar 594
        return tmp;
355 daniel-mar 595
}
532 daniel-mar 596
 
597
function show_waiting_anim() {
598
        $("#loading").show();
599
}
600
 
601
function hide_waiting_anim() {
602
        $("#loading").hide();
603
}
544 daniel-mar 604
 
605
/* Mini-framework to abort all AJAX requests if a new request is made */
606
 
607
$.xhrPool = [];
608
$.xhrPool.add = function(jqXHR) {
609
        $.xhrPool.push(jqXHR);
610
}
611
$.xhrPool.remove = function(jqXHR) {
612
        var index = $.xhrPool.indexOf(jqXHR);
613
        if (index > -1) {
614
                $.xhrPool.splice(index, 1);
615
        }
616
};
617
$.xhrPool.abortAll = function() {
618
        var calls = Array.from($.xhrPool);
619
        $.each(calls, function(key, value) {
620
                value.abort();
621
        });
622
}