Commit db8836ca authored by Arinde Eniola's avatar Arinde Eniola

attach the utitlity function to the global scope with some changes

parent 261c8e76
...@@ -33,12 +33,12 @@ ...@@ -33,12 +33,12 @@
paramKeys = ['author_id', 'label_name', 'milestone_title', 'assignee_id', 'issue_search'] paramKeys = ['author_id', 'label_name', 'milestone_title', 'assignee_id', 'issue_search']
for paramKey in paramKeys for paramKey in paramKeys
newParams[paramKey] = getUrlParameter(paramKey) or '' newParams[paramKey] = gl.utils.getUrlParameter(paramKey) or ''
if stateFilters.length if stateFilters.length
stateFilters.find('a').each -> stateFilters.find('a').each ->
initialUrl = $(this).attr 'href' initialUrl = $(this).attr 'href'
$(this).attr 'href', mergeUrlParams(newParams, initialUrl) $(this).attr 'href', gl.utils.mergeUrlParams(newParams, initialUrl)
# Make sure we trigger ajax request only after user stop typing # Make sure we trigger ajax request only after user stop typing
initSearch: -> initSearch: ->
......
function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
}
/**
* @param {Object} params - url keys and value to merge
* @param {String} url
*/
function mergeUrlParams(params, url){
var newUrl = decodeURIComponent(url);
Object.keys(params).forEach(function(paramName) {
var pattern = new RegExp('\\b('+paramName+'=).*?(&|$)')
if (url.search(pattern) >= 0){
newUrl = newUrl.replace(pattern,'$1' + params[paramName] + '$2');
} else {
newUrl = newUrl + (newUrl.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + params[paramName]
}
});
return newUrl;
}
\ No newline at end of file
((w) ->
w.gl ?= {}
w.gl.utils ?= {}
w.gl.utils.getUrlParameter = (sParam) ->
sPageURL = decodeURIComponent(window.location.search.substring(1))
sURLVariables = sPageURL.split('&')
sParameterName = undefined
i = 0
while i < sURLVariables.length
sParameterName = sURLVariables[i].split('=')
if sParameterName[0] is sParam
return if sParameterName[1] is undefined then true else sParameterName[1]
i++
# #
# @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}=).*?(&|$)"
if url.search(pattern) >= 0
newUrl = newUrl.replace pattern, "$1#{paramValue}$2"
else
newUrl = "#{newUrl}#{(if newUrl.indexOf('?') > 0 then '&' else '?')}#{paramName}=#{paramValue}"
newUrl
) window
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment