Browse Source

add buildURL func

Hamidreza Ghavami 1 year ago
parent
commit
1c9fc9422e
1 changed files with 18 additions and 0 deletions
  1. 18 0
      web/assets/js/util/common.js

+ 18 - 0
web/assets/js/util/common.js

@@ -135,3 +135,21 @@ function doAllItemsExist(array1, array2) {
     }
     return true;
 }
+
+function buildURL({ host, port, isTLS, base, path }) {
+    if (!host || host.length === 0) host = window.location.hostname;
+    if (!port || port.length === 0) port = window.location.port;
+
+    if (isTLS === undefined) isTLS = window.location.protocol === "https:";
+
+    const protocol = isTLS ? "https:" : "http:";
+
+    port = String(port);
+    if (port === "" || (isTLS && port === "443") || (!isTLS && port === "80")) {
+        port = "";
+    } else {
+        port = `:${port}`;
+    }
+
+    return `${protocol}//${host}${port}${base}${path}`;
+}