BigW Consortium Gitlab

url_utility.js.coffee 1.51 KB
Newer Older
1 2 3 4 5
((w) ->

  w.gl ?= {}
  w.gl.utils ?= {}

Arinde Eniola committed
6 7 8
  # Returns an array containing the value(s) of the
  # of the key passed as an argument
  w.gl.utils.getParameterValues = (sParam) ->
9 10 11
    sPageURL = decodeURIComponent(window.location.search.substring(1))
    sURLVariables = sPageURL.split('&')
    sParameterName = undefined
Arinde Eniola committed
12
    values = []
13 14 15 16
    i = 0
    while i < sURLVariables.length
      sParameterName = sURLVariables[i].split('=')
      if sParameterName[0] is sParam
Arinde Eniola committed
17
        values.push(sParameterName[1])
18
      i++
Arinde Eniola committed
19
    values
20 21 22 23 24 25 26 27 28

  # #
  #  @param {Object} params - url keys and value to merge
  #  @param {String} url
  # #
  w.gl.utils.mergeUrlParams = (params, url) ->
    newUrl = decodeURIComponent(url)
    for paramName, paramValue of params
      pattern = new RegExp "\\b(#{paramName}=).*?(&|$)"
Phil Hughes committed
29
      if not paramValue?
30
        newUrl = newUrl.replace pattern, ''
Phil Hughes committed
31
      else if url.search(pattern) isnt -1
32 33 34
        newUrl = newUrl.replace pattern, "$1#{paramValue}$2"
      else
        newUrl = "#{newUrl}#{(if newUrl.indexOf('?') > 0 then '&' else '?')}#{paramName}=#{paramValue}"
35 36 37 38 39 40 41

    # Remove a trailing ampersand
    lastChar = newUrl[newUrl.length - 1]

    if lastChar is '&'
        newUrl = newUrl.slice 0, -1

42 43
    newUrl

44 45 46 47 48 49 50 51
  # removes parameter query string from url. returns the modified url
  w.gl.utils.removeParamQueryString = (url, param) ->
    url = decodeURIComponent(url)
    urlVariables = url.split('&')
    (
      variables for variables in urlVariables when variables.indexOf(param) is -1
    ).join('&')

52
) window