Subversion Repositories oidplus

Rev

Rev 1289 | Rev 1349 | Go to most recent revision | View as "text/javascript" | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * OIDplus 2.0
  3.  * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
  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.  
  18. /*jshint esversion: 6 */
  19.  
  20. // $('#html').jstree();
  21.  
  22. var bs5Utils = undefined;
  23.  
  24. var current_node = "";
  25. var popstate_running = false;
  26. // DEFAULT_LANGUAGE will be set by oidplus.min.js.php
  27. // language_messages will be set by oidplus.min.js.php
  28. // language_tblprefix will be set by oidplus.min.js.php
  29. // csrf_token will be set by oidplus.min.js.php
  30. // samesite_policy will bet set by oidplus.min.js.php
  31.  
  32. var pageChangeCallbacks = [];
  33. var pageChangeRequestCallbacks = [];
  34.  
  35. var pageLoadedCallbacks= {
  36.         "anyPageLoad":         [], // this is processed inside both AJAX successful reload and document.ready (at the very end)
  37.         "ajaxPageLoad":        [], // inside AJAX successful reload only
  38.         "documentReadyBefore": [], // inside document.ready, in the very beginning of the function
  39.         "documentReadyAfter":  []  // inside document.ready, in the very end of the function
  40. };
  41.  
  42. var oidplus_menu_width = 450; // In pixels. You can change this at runtime because of the glayoutWorkaroundB() workaround
  43.  
  44. function executeAllCallbacks(functionsArray) {
  45.         functionsArray.forEach(
  46.                 function(fel) {
  47.                         if (typeof fel == 'function') fel();
  48.                 }
  49.         );
  50. }
  51.  
  52. function getOidPlusSystemTitle() {
  53.         return getMeta('OIDplus-SystemTitle'); // do not translate
  54. }
  55.  
  56. function combine_systemtitle_and_pagetitle(systemtitle, pagetitle) {
  57.         // Please also change the function in index.php
  58.         if (systemtitle == pagetitle) {
  59.                 return systemtitle;
  60.         } else {
  61.                 return pagetitle + ' - ' + systemtitle;
  62.         }
  63. }
  64.  
  65. function getSystemUrl(relative) {
  66.         relative = (typeof relative === 'undefined') ? false : relative; // do not translate
  67.         var url = new URL(window.location.href);
  68.         var res = relative ? url.pathname : url.href.substr(0, url.href.length-url.search.length);
  69.         if (res.endsWith("index.php")) res = res.substring(0, res.lastIndexOf('/')) + "/";
  70.         return res;
  71. }
  72.  
  73. function getTreeLoadURL() {
  74.         var url = new URL(window.location.href);
  75.         var goto = url.searchParams.get("goto");
  76.         return (goto != null) ? "ajax.php?csrf_token="+encodeURIComponent(csrf_token)+"&action=tree_load&anticache="+Date.now()+"&goto="+encodeURIComponent(goto)
  77.                               : "ajax.php?csrf_token="+encodeURIComponent(csrf_token)+"&action=tree_load&anticache="+Date.now();
  78. }
  79.  
  80. function reloadContent() {
  81.         // window.location.href = "?goto="+encodeURIComponent(current_node);
  82.         if (openOidInPanel(current_node, false)) {
  83.                 if(!$('#oidtree').jstree(true).get_node(current_node)) {
  84.                         // Avoid that a language change at "oidplus:srvreg_status" won't redirect the user to "oidplus:srv_registration" because of the reselection during refresh
  85.                         $('#oidtree').jstree("deselect_all");
  86.                 }
  87.  
  88.                 $('#oidtree').jstree("refresh");
  89.         }
  90. }
  91.  
  92. function x_rec(x_data, i) {
  93.         $('#oidtree').jstree('open_node', x_data[i], function(e, data) {
  94.                 if (i+1 < x_data.length) {
  95.                         x_rec(x_data, i+1);
  96.                 } else {
  97.                         popstate_running = true; // don't call openOidInPanel again
  98.                         try {
  99.                                 $('#oidtree').jstree('select_node', x_data[i]);
  100.                         } catch (err) {
  101.                                 popstate_running = false;
  102.                         } finally {
  103.                                 popstate_running = false;
  104.                         }
  105.                 }
  106.         });
  107. }
  108.  
  109. function performCloseQueryCB() {
  110.         for (var i=0; i<pageChangeRequestCallbacks.length; i++) {
  111.                 if (!pageChangeRequestCallbacks[i][0](pageChangeRequestCallbacks[i][1])) return false;
  112.         }
  113.         pageChangeRequestCallbacks = [];
  114.         return true; // may close
  115. }
  116.  
  117. function performCloseCB() {
  118.         for (var i=0; i<pageChangeCallbacks.length; i++) {
  119.                 pageChangeCallbacks[i][0](pageChangeCallbacks[i][1]);
  120.         }
  121.         pageChangeCallbacks = [];
  122. }
  123.  
  124. function openOidInPanel(id, reselect/*=false*/, anchor/*=''*/, force/*=false*/) {
  125.         reselect = (typeof reselect === 'undefined') ? false : reselect; // do not translate
  126.         anchor = (typeof anchor === 'undefined') ? '' : anchor; // do not translate
  127.         force = (typeof force === 'undefined') ? false : force; // do not translate
  128.  
  129.         var mayClose = performCloseQueryCB();
  130.         if (!force && !mayClose) return false;
  131.  
  132.         performCloseCB();
  133.  
  134.         $.xhrPool.abortAll();
  135.  
  136.         if (reselect) {
  137.                 $('#oidtree').jstree('deselect_all');
  138.  
  139.                 popstate_running = true; // don't call openOidInPanel during tree selection
  140.                 try {
  141.                         // If the node is already loaded in the tree, select it
  142.                         if (!$('#oidtree').jstree('select_node', id)) {
  143.                                 // If the node is not loaded, then we try to search it.
  144.                                 // If it can be found, then open all parent nodes and select the node
  145.                                 $.ajax({
  146.                                         url:"ajax.php",
  147.                                         method:"POST",
  148.                                         beforeSend: function(jqXHR, settings) {
  149.                                                 //$.xhrPool.abortAll();
  150.                                                 $.xhrPool.add(jqXHR);
  151.                                         },
  152.                                         complete: function(jqXHR, text) {
  153.                                                 $.xhrPool.remove(jqXHR);
  154.                                         },
  155.                                         data:{
  156.                                                 csrf_token:csrf_token,
  157.                                                 action:"tree_search",
  158.                                                 search:id,
  159.                                                 anticache:Date.now()
  160.                                         },
  161.                                         error:function(jqXHR, textStatus, errorThrown) {
  162.                                                 if (errorThrown == "abort") return;
  163.                                                 console.error("Tree search failed");
  164.                                                 console.error(_L("Error: %1",errorThrown));
  165.                                         },
  166.                                         success:function(data) {
  167.                                                 if ("error" in data) {
  168.                                                         console.error("Tree search failed");
  169.                                                         console.error(data);
  170.                                                 } else if ((data instanceof Array) && (data.length > 0)) {
  171.                                                         x_rec(data, 0);
  172.                                                 } else {
  173.                                                         console.error("Tree search failed");
  174.                                                         console.error(data);
  175.                                                 }
  176.                                         }
  177.                                 });
  178.                         }
  179.                 } catch (err) {
  180.                         popstate_running = false;
  181.                 } finally {
  182.                         popstate_running = false;
  183.                 }
  184.         }
  185.  
  186.         // This loads the actual content
  187.  
  188.         // document.title = ""; // <-- we may not do this, otherwise Firefox won't
  189.         //                            show titles in the browser history (right-click
  190.         //                            on back-button), although document.title() is
  191.         //                            set inside the AJAX-callback [Firefox bug?!]
  192.  
  193.         $('#real_title').html("&nbsp;");
  194.         $('#real_content').html(_L("Loading..."));
  195.         $('#static_link').attr("href", oidplus_webpath_absolute_canonical+"?goto="+encodeURIComponent(id));
  196.         $("#gotoedit").val(id);
  197.  
  198.         // Normal opening of a description
  199.         $.ajax({
  200.                 url:"ajax.php",
  201.                 method:"GET",
  202.                 beforeSend: function(jqXHR, settings) {
  203.                         //$.xhrPool.abortAll();
  204.                         $.xhrPool.add(jqXHR);
  205.                 },
  206.                 complete: function(jqXHR, text) {
  207.                         $.xhrPool.remove(jqXHR);
  208.                 },
  209.                 data:{
  210.                         csrf_token:csrf_token,
  211.                         action:"get_description",
  212.                         id:id,
  213.                         anticache:Date.now()
  214.                 },
  215.                 error:function(jqXHR, textStatus, errorThrown) {
  216.                         if (errorThrown == "abort") return;
  217.                         alertError(_L("Failed to load content: %1",errorThrown));
  218.                         console.error(_L("Error: %1",errorThrown));
  219.                 },
  220.                 success:function(data) {
  221.                         // TODO: Use oidplus_ajax_success(), since this checks the existance of "error" in data, and checks if status>=0
  222.                         if ("error" in data) {
  223.                                 alertError(_L("Failed to load content: %1",data.error));
  224.                                 console.error(data.error);
  225.                         } else if (data.status >= 0) {
  226.                                 data.id = id;
  227.  
  228.                                 var state = {
  229.                                         "node_id":id,
  230.                                         "titleHTML":(data.icon ? '<img src="'+data.icon+'" width="48" height="48" alt="'+data.title.htmlentities()+'"> ' : '') + data.title.htmlentities(),
  231.                                         "textHTML":data.text,
  232.                                         "staticlinkHREF":oidplus_webpath_absolute_canonical+"?goto="+encodeURIComponent(id),
  233.                                 };
  234.                                 if (current_node != id) {
  235.                                         window.history.pushState(state, data.title, "?goto="+encodeURIComponent(id));
  236.                                 } else {
  237.                                         window.history.replaceState(state, data.title, "?goto="+encodeURIComponent(id));
  238.                                 }
  239.  
  240.                                 document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), data.title);
  241.  
  242.                                 if (data.icon) {
  243.                                         $('#real_title').html('<img src="'+data.icon+'" width="48" height="48" alt="'+data.title.htmlentities()+'"> ' + data.title.htmlentities());
  244.                                 } else {
  245.                                         $('#real_title').html(data.title.htmlentities());
  246.                                 }
  247.                                 $('#real_content').html(data.text);
  248.                                 document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), data.title);
  249.                                 current_node = id;
  250.  
  251.                                 executeAllCallbacks(pageLoadedCallbacks.anyPageLoad);
  252.                                 executeAllCallbacks(pageLoadedCallbacks.ajaxPageLoad);
  253.  
  254.                                 if (anchor != '') {
  255.                                         jumpToAnchor(anchor);
  256.                                 }
  257.                         } else {
  258.                                 alertError(_L("Failed to load content: %1",data.status));
  259.                                 console.error(data);
  260.                         }
  261.                 }
  262.         });
  263.  
  264.         return true;
  265. }
  266.  
  267. // This function opens the "parentID" node, and then selects the "childID" node (which should be beneath the parent node)
  268. function openAndSelectNode(childID, parentID) {
  269.         if ($('#oidtree').jstree(true).get_node(parentID)) {
  270.                 $('#oidtree').jstree('open_node', parentID, function(e, data) { // open parent node
  271.                         if ($('#oidtree').jstree(true).get_node(childID)) { // is the child there?
  272.                                 $('#oidtree').jstree('deselect_all').jstree('select_node', childID); // select it
  273.                         } else {
  274.                                 // This can happen if the content page contains brand new items which are not in the treeview yet
  275.                                 $("#gotoedit").val(childID);
  276.                                 window.location.href = "?goto="+encodeURIComponent(childID);
  277.                         }
  278.                 }, true);
  279.         } else {
  280.                 // This should usually not happen
  281.                 $("#gotoedit").val(childID);
  282.                 window.location.href = "?goto="+encodeURIComponent(childID);
  283.         }
  284. }
  285.  
  286. $(window).on("popstate", function(e) {
  287.         if (!performCloseQueryCB()) {
  288.                 // TODO: does not work!!! The "back/forward" action will be cancelled, but the browser still thinks it was successful,
  289.                 // so if you do it again, you will then jump 2 pages back, etc!
  290.                 // This does also not help:
  291.                 //window.history.pushState(e.originalEvent.state, e.originalEvent.title, e.originalEvent.url);
  292.                 //window.history.forward();
  293.                 return;
  294.         }
  295.  
  296.         popstate_running = true;
  297.         try {
  298.                 var data = e.originalEvent.state;
  299.  
  300.                 current_node = data.node_id;
  301.                 $("#gotoedit").val(current_node);
  302.                 $('#oidtree').jstree('deselect_all').jstree('select_node', data.node_id);
  303.                 $('#real_title').html(data.titleHTML);
  304.                 $('#real_content').html(data.textHTML);
  305.                 $('#static_link').attr("href", data.staticlinkHREF);
  306.                 document.title = combine_systemtitle_and_pagetitle(getOidPlusSystemTitle(), data.titleHTML.html_entity_decode());
  307.         } catch (err) {
  308.                 popstate_running = false;
  309.         } finally {
  310.                 popstate_running = false;
  311.         }
  312. });
  313.  
  314. $(document).ready(function () {
  315.  
  316.         executeAllCallbacks(pageLoadedCallbacks.documentReadyBefore);
  317.  
  318.         /*
  319.         window.onbeforeunload = function(e) {
  320.                 // TODO: This won't be called because TinyMCE overrides it??
  321.                 // TODO: when the user accepted the query in performCloseQueryCB(), then the message will be shown again by the browser!
  322.                 if (!performCloseQueryCB()) {
  323.                         // Cancel the event
  324.                         e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
  325.                         // Chrome requires returnValue to be set
  326.                         e.returnValue = '';
  327.                 } else {
  328.                         // the absence of a returnValue property on the event will guarantee the browser unload happens
  329.                         delete e['returnValue'];
  330.                 }
  331.         };
  332.         */
  333.  
  334.         if (typeof oidplus_menu_width_uservalue !== 'undefined') {
  335.                 oidplus_menu_width = oidplus_menu_width_uservalue;
  336.         }
  337.  
  338.         // --- JsTree
  339.  
  340.         if ($('#oidtree').length > 0) $('#oidtree')
  341.         .jstree({
  342.                 plugins: ['massload','search','conditionalselect'],
  343.                 'core' : {
  344.                         'data' : {
  345.                                 "url" : getTreeLoadURL(),
  346.                                 "data" : function (node) {
  347.                                         return { "id" : node.id };
  348.                                 }
  349.                         },
  350.                         "multiple": false
  351.                 },
  352.                 'conditionalselect' : function (node) {
  353.                         if (node.original.conditionalselect !== undefined) {
  354.                                 return eval(node.original.conditionalselect);
  355.                         } else {
  356.                                 return performCloseQueryCB();
  357.                         }
  358.                 },
  359.         })
  360.         .on('ready.jstree', function (e, data) {
  361.                 var url = new URL(window.location.href);
  362.                 var goto = url.searchParams.get("goto");
  363.                 if (goto == null) goto = "oidplus:system"; // the page was not called with ?goto=...
  364.                 if ($('#gotoedit').length > 0) $("#gotoedit").val(goto);
  365.  
  366.                 // 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)
  367.                 // But then we need to set the history state manually
  368.                 current_node = goto;
  369.                 window.history.replaceState({
  370.                         "node_id":goto,
  371.                         "titleHTML":$('#real_title').html(),
  372.                         "textHTML":$('#real_content').html(),
  373.                         "staticlinkHREF":oidplus_webpath_absolute_canonical+"?goto="+encodeURIComponent(goto),
  374.                 }, $('#real_title').html(), "?goto="+encodeURIComponent(goto));
  375.  
  376.                 if (goto != null) data.instance.select_node([goto]);
  377.  
  378.                 setTimeout(glayoutWorkaroundAC, 100);
  379.                 setTimeout(glayoutWorkaroundB, 100);
  380.         })
  381.         .on('select_node.jstree', function (node, selected, event) {
  382.                 mobileNavClose();
  383.  
  384.                 var id = selected.node.id;
  385.                 if ((!popstate_running) && (current_node != id)) {
  386.                         // 4th argument: we force the reload (because in the
  387.                         // conditional select above, we already asked if
  388.                         // tinyMCE needs to be saved)
  389.                         openOidInPanel(id, false, '', true);
  390.                 }
  391.         });
  392.  
  393.         // --- Layout
  394.  
  395.         if ($('#system_title_menu').length > 0) $("#system_title_menu")[0].style.display = "block";
  396.  
  397.         var tmpObjectTree = _L("OBJECT TREE").replace(/(.{1})/g,"$1<br>");
  398.         tmpObjectTree = tmpObjectTree.substring(0, tmpObjectTree.length-"<br>".length);
  399.  
  400.         if ($('#oidtree').length > 0) $('#oidtree').addClass('ui-layout-west');
  401.         if ($('#content_window').length > 0) $('#content_window').addClass('ui-layout-center');
  402.         if ($('#system_title_bar').length > 0) $('#system_title_bar').addClass('ui-layout-north');
  403.         if ($('#frames').length > 0) glayout = $('#frames').layout({
  404.                 north__size:                  40,
  405.                 north__slidable:              false,
  406.                 north__closable:              false,
  407.                 north__resizable:             false,
  408.                 west__size:                   oidplus_menu_width,
  409.                 west__spacing_closed:         20,
  410.                 west__togglerLength_closed:   230,
  411.                 west__togglerAlign_closed:    "center",
  412.                 west__togglerContent_closed:  tmpObjectTree,
  413.                 west__togglerTip_closed:      _L("Open & Pin Menu"),
  414.                 west__sliderTip:              _L("Slide Open Menu"),
  415.                 west__slideTrigger_open:      "mouseover",
  416.                 center__maskContents:         true, // IMPORTANT - enable iframe masking
  417.                 onresize_start:               function() { if (typeof handle_glayout_onresize_start == 'function') handle_glayout_onresize_start(); }
  418.         });
  419.  
  420.         if ($('#gotobox').length == 0) $("#languageBox").css('right', '20px'); // Language Box to the right if there is no goto-box
  421.  
  422.         if ($('#gotobox').length > 0) $("#gotobox").addClass("mobilehidden");
  423.         if ($('#languageBox').length > 0) $("#languageBox").addClass("mobilehidden");
  424.         if ($('#gotobox').length > 0) $("#gotobox")[0].style.display = "block";
  425.         if ($('#gotoedit').length > 0) $('#gotoedit').keypress(function(event) {
  426.                 var keycode = (event.keyCode ? event.keyCode : event.which);
  427.                 if (keycode == '13') {
  428.                         gotoButtonClicked();
  429.                 }
  430.         });
  431.  
  432.         if (typeof Bs5Utils !== "undefined") {
  433.                 Bs5Utils.defaults.toasts.position = 'top-center';
  434.                 Bs5Utils.defaults.toasts.stacking = true;
  435.                 bs5Utils = new Bs5Utils();
  436.         }
  437.  
  438.         executeAllCallbacks(pageLoadedCallbacks.anyPageLoad);
  439.         executeAllCallbacks(pageLoadedCallbacks.documentReadyAfter);
  440. });
  441.  
  442. // can be overridden if necessary
  443. var handle_glayout_onresize_start = undefined;
  444.  
  445. function glayoutWorkaroundAC() {
  446.         // "Bug A": Sometimes, the design is completely destroyed after reloading the page. It does not help when glayout.resizeAll()
  447.         //          is called at the beginning (e.g. during the ready function), and it does not help if we wait 500ms.
  448.         //          So we do it all the time. It has probably something to do with slow loading times, since the error
  449.         //          does only appear when the page is "blank" for a short while while it is loading.
  450.         glayout.resizeAll();
  451.  
  452.         // "Bug C": With Firefox (And sometimes with Chrome), there is a gap between the content-window (including scroll bars)
  453.         //          and the right corner of the screen. Removing the explicit width solves this problem.
  454.         $("#content_window")[0].style.removeProperty("width");
  455.  
  456.         setTimeout(glayoutWorkaroundAC, 100);
  457. }
  458.  
  459. function glayoutWorkaroundB() {
  460.         // "Bug B": Sometimes, after reload, weird space between oidtree and content window, because oidtree has size of 438px
  461.         $("#oidtree")[0].style.width = oidplus_menu_width + "px";
  462. }
  463.  
  464. function mobileNavClose() {
  465.         if ($("#system_title_menu").is(":hidden")) {
  466.                 return;
  467.         }
  468.  
  469.         $("#oidtree").slideUp("medium").promise().done(function() {
  470.                 $("#oidtree").addClass("ui-layout-west");
  471.                 $("#oidtree").show();
  472. //              $("#gotobox").hide();
  473. //              $("#languageBox").hide();
  474.                 $("#gotobox").addClass("mobilehidden");
  475.                 $("#languageBox").addClass("mobilehidden");
  476.         });
  477.         $("#system_title_menu").removeClass("active");
  478. }
  479.  
  480. function mobileNavOpen() {
  481.         $("#oidtree").hide();
  482.         $("#oidtree").removeClass("ui-layout-west");
  483.         $("#oidtree").slideDown("medium");
  484. //      $("#gotobox").show();
  485. //      $("#languageBox").show();
  486.         $("#gotobox").removeClass("mobilehidden");
  487.         $("#languageBox").removeClass("mobilehidden");
  488.         $("#system_title_menu").addClass("active");
  489. }
  490.  
  491. function mobileNavButtonClick(sender) {
  492.         if ($("#oidtree").hasClass("ui-layout-west")) {
  493.                 mobileNavOpen();
  494.         } else {
  495.                 mobileNavClose();
  496.         }
  497. }
  498.  
  499. function mobileNavButtonHover(sender) {
  500.         sender.classList.toggle("hover");
  501. }
  502.  
  503. function gotoButtonClicked() {
  504.         openOidInPanel($("#gotoedit").val(), true);
  505. }
  506.  
  507. function setLanguage(lngid) {
  508.         setCookie('LANGUAGE', lngid, 0/*Until browser closes*/, oidplus_webpath_relative);
  509.  
  510.         if (current_node == "") return false; // Happens for Setup. Open URL instead.
  511.  
  512.         $(".lng_flag").each(function(){
  513.                 $(this).addClass("picture_ghost");
  514.         });
  515.         $("#lng_flag_"+$.escapeSelector(lngid)).removeClass("picture_ghost");
  516.  
  517.         if (isInternetExplorer()) {
  518.                 // Internet Explorer has problems with sending new cookies to new AJAX requests, so we reload the page completely
  519.                 window.location.reload();
  520.         } else {
  521.                 // TODO: Small detail: The "Go" button also needs to be re-translated
  522.                 reloadContent();
  523.                 mobileNavClose();
  524.         }
  525.         return true; // we have handled it. Do not follow href=""
  526. }
  527.  
  528. function show_waiting_anim() {
  529.         $("#loading").show();
  530. }
  531.  
  532. function hide_waiting_anim() {
  533.         $("#loading").hide();
  534. }
  535.  
  536. /* Mini-framework to abort all AJAX requests if a new request is made */
  537.  
  538. $.xhrPool = [];
  539. $.xhrPool.add = function(jqXHR) {
  540.         $.xhrPool.push(jqXHR);
  541. }
  542. $.xhrPool.remove = function(jqXHR) {
  543.         var index = $.xhrPool.indexOf(jqXHR);
  544.         if (index > -1) {
  545.                 $.xhrPool.splice(index, 1);
  546.         }
  547. };
  548. $.xhrPool.abortAll = function() {
  549.         var calls = Array.from($.xhrPool);
  550.         $.each(calls, function(key, value) {
  551.                 value.abort();
  552.         });
  553. }
  554.  
  555. /* Individual alert types */
  556. /* TODO: alert() blocks program flow, but alertSuccess() not! Will the mass change have negative effects (e.g. a redirect without the user seeing the toast)? */
  557.  
  558. function alertSuccess(txt) {
  559.         if (typeof bs5Utils !== "undefined") {
  560.                 bs5Utils.Snack.show('success', _L(txt), delay = 5000, dismissible = true);
  561.         } else {
  562.                 alert(txt);
  563.         }
  564. }
  565.  
  566. function alertWarning(txt) {
  567.         // TODO: as toast?
  568.         alert(txt);
  569. }
  570.  
  571. function alertError(txt) {
  572.         // TODO: as toast?
  573.         alert(txt);
  574. }
  575.  
  576. /* AJAX success/error-handling */
  577.  
  578. function oidplus_ajax_error(jqXHR, textStatus, errorThrown) {
  579.         if (errorThrown == "abort") return;
  580.         console.error(errorThrown, jqXHR);
  581.         alertError(_L("Error: %1", errorThrown));
  582. }
  583.  
  584. function oidplus_ajax_success(data, cb) {
  585.         if (typeof data === "object" && "error" in data) {
  586.                 alertError(_L("Error: %1", data.error));
  587.         } else if (typeof data === "object" && data.status >= 0) {
  588.                 cb(data);
  589.         } else {
  590.                 alertError(_L("Error: %1", data));
  591.         }
  592. }
  593.