Răsfoiți Sursa

HUE-5494 [core] Support for changing a URL parameter

Enrico Berti 9 ani în urmă
părinte
comite
e8dc807c78

+ 18 - 0
desktop/core/src/desktop/static/desktop/js/hue.utils.js

@@ -196,6 +196,24 @@ if (!String.prototype.includes) {
     window.history.replaceState(null, null, newURL);
   }
 
+  hueUtils.changeURLParameter = function (param, value) {
+    var newSearch = '';
+    if (window.location.getParameter(param) !== '') {
+      newSearch += '?';
+      window.location.search.replace(/\?/gi, '').split('&').forEach(function (p) {
+        if (p.split('=')[0] !== param) {
+          newSearch += p;
+        }
+      });
+      newSearch += (newSearch !== '?' ? '&' : '') + param + '=' + value;
+    }
+    else {
+      newSearch = window.location.search + (window.location.search.indexOf('?') > -1 ? '&' : '?') + param + '=' + value;
+    }
+
+    hueUtils.changeURL(window.location.pathname + newSearch);
+  }
+
   /**
    * @param {string} pseudoJson
    * @constructor

+ 14 - 0
desktop/core/src/desktop/static/desktop/spec/hueUtilsSpec.js

@@ -35,5 +35,19 @@
     it("should have the String.includes polyfill", function() {
       expect('banana'.includes('anan')).toBeTruthy();
     });
+
+    it("should change completely the URL", function () {
+      hueUtils.changeURL('/banana');
+      expect(window.location.pathname).toEqual('/banana');
+      hueUtils.changeURL('/jasmine');
+    });
+
+    it("should change just a parameter in the URL", function () {
+      hueUtils.changeURL('/banana?peeled=no');
+      hueUtils.changeURLParameter('peeled', 'yes');
+      expect(window.location.search).toEqual('?peeled=yes');
+      hueUtils.changeURL('/jasmine');
+    });
+
   });
 })();