From 261c8e765f5b13dd627fcda5ca1ae263ecfad0c8 Mon Sep 17 00:00:00 2001 From: Arinde Eniola Date: Thu, 31 Mar 2016 23:33:32 +0100 Subject: fix missing filters on status tab when user swithches to another state --- app/assets/javascripts/issues.js.coffee | 15 ++++++++++++++ app/assets/javascripts/lib/url_utility.js | 33 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 app/assets/javascripts/lib/url_utility.js (limited to 'app/assets') diff --git a/app/assets/javascripts/issues.js.coffee b/app/assets/javascripts/issues.js.coffee index b1479bfb449..8d069a209d2 100644 --- a/app/assets/javascripts/issues.js.coffee +++ b/app/assets/javascripts/issues.js.coffee @@ -26,6 +26,20 @@ $(".selected_issue").bind "change", Issues.checkChanged + # Update state filters if present in page + updateStateFilters: -> + stateFilters = $('.issues-state-filters') + newParams = {} + paramKeys = ['author_id', 'label_name', 'milestone_title', 'assignee_id', 'issue_search'] + + for paramKey in paramKeys + newParams[paramKey] = getUrlParameter(paramKey) or '' + + if stateFilters.length + stateFilters.find('a').each -> + initialUrl = $(this).attr 'href' + $(this).attr 'href', mergeUrlParams(newParams, initialUrl) + # Make sure we trigger ajax request only after user stop typing initSearch: -> @timer = null @@ -54,6 +68,7 @@ # Change url so if user reload a page - search results are saved history.replaceState {page: issuesUrl}, document.title, issuesUrl Issues.reload() + Issues.updateStateFilters() dataType: "json" checkChanged: -> diff --git a/app/assets/javascripts/lib/url_utility.js b/app/assets/javascripts/lib/url_utility.js new file mode 100644 index 00000000000..5fa3a4c69d3 --- /dev/null +++ b/app/assets/javascripts/lib/url_utility.js @@ -0,0 +1,33 @@ +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 -- cgit v1.2.1 From db8836ca84ab86dc39b4d8b3282603c724a78e67 Mon Sep 17 00:00:00 2001 From: Arinde Eniola Date: Sat, 2 Apr 2016 16:56:31 +0100 Subject: attach the utitlity function to the global scope with some changes --- app/assets/javascripts/issues.js.coffee | 4 +-- app/assets/javascripts/lib/url_utility.js | 33 ------------------------ app/assets/javascripts/lib/url_utility.js.coffee | 31 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 35 deletions(-) delete mode 100644 app/assets/javascripts/lib/url_utility.js create mode 100644 app/assets/javascripts/lib/url_utility.js.coffee (limited to 'app/assets') diff --git a/app/assets/javascripts/issues.js.coffee b/app/assets/javascripts/issues.js.coffee index 8d069a209d2..0d9f2094c2a 100644 --- a/app/assets/javascripts/issues.js.coffee +++ b/app/assets/javascripts/issues.js.coffee @@ -33,12 +33,12 @@ paramKeys = ['author_id', 'label_name', 'milestone_title', 'assignee_id', 'issue_search'] for paramKey in paramKeys - newParams[paramKey] = getUrlParameter(paramKey) or '' + newParams[paramKey] = gl.utils.getUrlParameter(paramKey) or '' if stateFilters.length stateFilters.find('a').each -> 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 initSearch: -> diff --git a/app/assets/javascripts/lib/url_utility.js b/app/assets/javascripts/lib/url_utility.js deleted file mode 100644 index 5fa3a4c69d3..00000000000 --- a/app/assets/javascripts/lib/url_utility.js +++ /dev/null @@ -1,33 +0,0 @@ -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 diff --git a/app/assets/javascripts/lib/url_utility.js.coffee b/app/assets/javascripts/lib/url_utility.js.coffee new file mode 100644 index 00000000000..abd556e0b4e --- /dev/null +++ b/app/assets/javascripts/lib/url_utility.js.coffee @@ -0,0 +1,31 @@ +((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 -- cgit v1.2.1