url_utility.js 2.91 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-param-reassign, no-cond-assign, one-var, one-var-declaration-per-line, no-void, guard-for-in, no-restricted-syntax, prefer-template, quotes, max-len */
Fatih Acet's avatar
Fatih Acet committed
2 3 4 5 6 7 8 9 10
(function() {
  (function(w) {
    var base;
    if (w.gl == null) {
      w.gl = {};
    }
    if ((base = w.gl).utils == null) {
      base.utils = {};
    }
11 12
    // Returns an array containing the value(s) of the
    // of the key passed as an argument
Fatih Acet's avatar
Fatih Acet committed
13 14 15 16 17 18 19 20 21 22
    w.gl.utils.getParameterValues = function(sParam) {
      var i, sPageURL, sParameterName, sURLVariables, values;
      sPageURL = decodeURIComponent(window.location.search.substring(1));
      sURLVariables = sPageURL.split('&');
      sParameterName = void 0;
      values = [];
      i = 0;
      while (i < sURLVariables.length) {
        sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] === sParam) {
23
          values.push(sParameterName[1].replace(/\+/g, ' '));
Fatih Acet's avatar
Fatih Acet committed
24
        }
25
        i += 1;
Fatih Acet's avatar
Fatih Acet committed
26 27 28
      }
      return values;
    };
29 30
    // @param {Object} params - url keys and value to merge
    // @param {String} url
Fatih Acet's avatar
Fatih Acet committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    w.gl.utils.mergeUrlParams = function(params, url) {
      var lastChar, newUrl, paramName, paramValue, pattern;
      newUrl = decodeURIComponent(url);
      for (paramName in params) {
        paramValue = params[paramName];
        pattern = new RegExp("\\b(" + paramName + "=).*?(&|$)");
        if (paramValue == null) {
          newUrl = newUrl.replace(pattern, '');
        } else if (url.search(pattern) !== -1) {
          newUrl = newUrl.replace(pattern, "$1" + paramValue + "$2");
        } else {
          newUrl = "" + newUrl + (newUrl.indexOf('?') > 0 ? '&' : '?') + paramName + "=" + paramValue;
        }
      }
45
      // Remove a trailing ampersand
Fatih Acet's avatar
Fatih Acet committed
46 47 48 49 50 51
      lastChar = newUrl[newUrl.length - 1];
      if (lastChar === '&') {
        newUrl = newUrl.slice(0, -1);
      }
      return newUrl;
    };
52
    // removes parameter query string from url. returns the modified url
53
    w.gl.utils.removeParamQueryString = function(url, param) {
Fatih Acet's avatar
Fatih Acet committed
54 55 56 57 58 59
      var urlVariables, variables;
      url = decodeURIComponent(url);
      urlVariables = url.split('&');
      return ((function() {
        var j, len, results;
        results = [];
60
        for (j = 0, len = urlVariables.length; j < len; j += 1) {
Fatih Acet's avatar
Fatih Acet committed
61 62 63 64 65 66 67 68
          variables = urlVariables[j];
          if (variables.indexOf(param) === -1) {
            results.push(variables);
          }
        }
        return results;
      })()).join('&');
    };
69 70 71 72 73 74 75 76 77 78
    w.gl.utils.getLocationHash = function(url) {
      var hashIndex;
      if (typeof url === 'undefined') {
        // Note: We can't use window.location.hash here because it's
        // not consistent across browsers - Firefox will pre-decode it
        url = window.location.href;
      }
      hashIndex = url.indexOf('#');
      return hashIndex === -1 ? null : url.substring(hashIndex + 1);
    };
Fatih Acet's avatar
Fatih Acet committed
79 80
  })(window);
}).call(this);