Subversion Repositories oidplus

Rev

Rev 985 | 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. /* Misc functions */
  21.  
  22. function htmlDecodeWithLineBreaks(html) {
  23.         // https://stackoverflow.com/questions/4502673/jquery-text-function-loses-line-breaks-in-ie
  24.         var breakToken = '_______break_______';
  25.         var lineBreakedHtml = html.replace(/<br\s?\/?>/gi, breakToken).replace(/<p\.*?>(.*?)<\/p>/gi, breakToken + '$1' + breakToken);
  26.         return $('<div>').html(lineBreakedHtml).text().replace(new RegExp(breakToken, 'g'), '\n');
  27. }
  28.  
  29. function copyToClipboard(elem) {
  30.         // Source: https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery
  31.         // Modified (see below)
  32.  
  33.         // create hidden text element, if it doesn't already exist
  34.         var targetId = "_hiddenCopyText_";
  35.         var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
  36.         var origSelectionStart, origSelectionEnd;
  37.         if (isInput) {
  38.                 // can just use the original source element for the selection and copy
  39.                 target = elem;
  40.                 origSelectionStart = elem.selectionStart;
  41.                 origSelectionEnd = elem.selectionEnd;
  42.         } else {
  43.                 // must use a temporary form element for the selection and copy
  44.                 target = document.getElementById(targetId);
  45.                 if (!target) {
  46.                         var target = document.createElement("textarea");
  47.                         target.style.position = "absolute";
  48.                         target.style.left = "-9999px";
  49.                         target.style.top = "0";
  50.                         target.id = targetId;
  51.                         document.body.appendChild(target);
  52.                 }
  53.  
  54.                 // Changed by Daniel Marschall, 3 Oct 2022
  55.                 // "textContent" will swallow the line breaks of a <div>xx<br>xx</div>
  56.                 // htmlDecodeWithLineBreaks convert <br> to linebreaks but strips the other
  57.                 // HTML tags.
  58.                 // target.textContent = elem.textContent;
  59.                 target.textContent = htmlDecodeWithLineBreaks(elem.innerHTML);
  60.         }
  61.         // select the content
  62.         var currentFocus = document.activeElement;
  63.         target.focus();
  64.         target.setSelectionRange(0, target.value.length);
  65.  
  66.         // copy the selection
  67.         var succeed;
  68.         try {
  69.                 succeed = document.execCommand("copy");
  70.         } catch(e) {
  71.                 succeed = false;
  72.         }
  73.         // restore original focus
  74.         if (currentFocus && typeof currentFocus.focus === "function") {
  75.                 currentFocus.focus();
  76.         }
  77.  
  78.         if (isInput) {
  79.                 // restore prior selection
  80.                 elem.setSelectionRange(origSelectionStart, origSelectionEnd);
  81.         } else {
  82.                 // clear temporary content
  83.                 target.textContent = "";
  84.         }
  85.         return succeed;
  86. }
  87.  
  88. function isInternetExplorer() {
  89.         // see also includes/functions.inc.php
  90.         var ua = window.navigator.userAgent;
  91.         return ((ua.indexOf("MSIE ") > 0) || (ua.indexOf("Trident/") > 0));
  92. }
  93.  
  94. function getMeta(metaName) {
  95.         const metas = $('meta[name='+metaName+']');
  96.         return (metas.length == 0) ? '' : metas[0].content;
  97. }
  98.  
  99. String.prototype.explode = function (separator, limit) {
  100.         // https://stackoverflow.com/questions/4514323/javascript-equivalent-to-php-explode
  101.         const array = this.split(separator);
  102.         if (limit !== undefined && array.length >= limit) {
  103.                 array.push(array.splice(limit - 1).join(separator));
  104.         }
  105.         return array;
  106. };
  107.  
  108. String.prototype.htmlentities = function () {
  109.         return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');//"
  110. };
  111.  
  112. String.prototype.html_entity_decode = function () {
  113.         return $('<textarea />').html(this).text();
  114. };
  115.  
  116. if (!String.prototype.replaceAll) {
  117.         /**
  118.          * String.prototype.replaceAll() polyfill
  119.          * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
  120.          * @author Chris Ferdinandi
  121.          * @license MIT
  122.          */
  123.         String.prototype.replaceAll = function(str, newStr){
  124.                 // If a regex pattern
  125.                 if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
  126.                         return this.replace(str, newStr);
  127.                 }
  128.                 // If a string
  129.                 return this.replace(new RegExp(str, 'g'), newStr);
  130.         };
  131. }
  132.  
  133. if (!String.prototype.startsWith) {
  134.         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#polyfill
  135.         Object.defineProperty(String.prototype, 'startsWith', {
  136.                 value: function(search, rawPos) {
  137.                         var pos = rawPos > 0 ? rawPos|0 : 0;
  138.                         return this.substring(pos, pos + search.length) === search;
  139.                 }
  140.         });
  141. }
  142.  
  143.  
  144.  
  145.  
  146. function jumpToAnchor(anchor) {
  147.         window.location.href = "#" + anchor;
  148. }
  149.  
  150. function getCookie(cname) {
  151.         // Source: https://www.w3schools.com/js/js_cookies.asp
  152.         var name = cname + "=";
  153.         var decodedCookie = decodeURIComponent(document.cookie);
  154.         var ca = decodedCookie.split(';');
  155.         for(var i = 0; i <ca.length; i++) {
  156.                 var c = ca[i];
  157.                 while (c.charAt(0) == ' ') {
  158.                         c = c.substring(1);
  159.                 }
  160.                 if (c.indexOf(name) == 0) {
  161.                         return c.substring(name.length, c.length);
  162.                 }
  163.         }
  164.         return undefined;
  165. }
  166.  
  167. function setCookie(cname, cvalue, exdays, path) {
  168.         var d = new Date();
  169.         d.setTime(d.getTime() + (exdays*24*60*60*1000));
  170.         var expires = exdays == 0 ? "" : "; expires="+d.toUTCString();
  171.         document.cookie = cname + "=" + cvalue + expires + ";path=" + path + ";SameSite=" + samesite_policy;
  172. }
  173.  
  174.  
  175.  
  176. function isNull(val, def) {
  177.         // For compatibility with Internet Explorer, use isNull(a,b) instead of a??b
  178.         if (val == null) {
  179.                 // since null==undefined, this also works with undefined
  180.                 return def;
  181.         } else {
  182.                 return val;
  183.         }
  184. }
  185.  
  186. function btoa(bin) {
  187.         var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  188.         var table = tableStr.split("");
  189.         for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
  190.                 var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
  191.                 if ((a | b | c) > 255) throw new Error(_L('String contains an invalid character'));
  192.                 base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
  193.                                        (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
  194.                                        (isNaN(b + c) ? "=" : table[c & 63]);
  195.         }
  196.         return base64.join("");
  197. };
  198.  
  199. function hexToBase64(str) {
  200.         return btoa(String.fromCharCode.apply(null,
  201.                     str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
  202. }
  203.  
  204. function _b64EncodeUnicode(str) {
  205.         if (str == "") {
  206.                 return "''";
  207.         } else {
  208.                 return "base64_decode('"+b64EncodeUnicode(str)+"')";
  209.         }
  210. }
  211.  
  212. function b64EncodeUnicode(str) {
  213.         // first we use encodeURIComponent to get percent-encoded UTF-8,
  214.         // then we convert the percent encodings into raw bytes which
  215.         // can be fed into btoa.
  216.         return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
  217.         function toSolidBytes(match, p1) {
  218.                 return String.fromCharCode('0x' + p1);
  219.         }));
  220. }
  221.  
  222. function generateRandomString(length) {
  223.         var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  224.         retVal = "";
  225.         for (var i = 0, n = charset.length; i < length; ++i) {
  226.                 retVal += charset.charAt(Math.floor(Math.random() * n));
  227.         }
  228.         return retVal;
  229. }
  230.  
  231. function RemoveLastDirectoryPartOf(the_url) {
  232.         var the_arr = the_url.split('/');
  233.         if (the_arr.pop() == '') the_arr.pop();
  234.         return( the_arr.join('/') );
  235. }
  236.  
  237.