From c2766ad976787daa70a4b4c330b1f9bb0843da1a Mon Sep 17 00:00:00 2001 From: Cameron Dawson Date: Tue, 16 Dec 2014 15:22:31 -0800 Subject: [PATCH] Bug 1111697 - remove $.deparam use on MainCtrl and jobfilters --- ui/index.html | 1 - ui/js/controllers/main.js | 45 +++++++++++++++--------------------- ui/js/services/jobfilters.js | 36 ++++++++++------------------- ui/vendor/jquery.deparam.js | 31 ------------------------- 4 files changed, 30 insertions(+), 83 deletions(-) delete mode 100644 ui/vendor/jquery.deparam.js diff --git a/ui/index.html b/ui/index.html index 932e8436396..a7c1a2c61d0 100755 --- a/ui/index.html +++ b/ui/index.html @@ -38,7 +38,6 @@ - diff --git a/ui/js/controllers/main.js b/ui/js/controllers/main.js index 024af24e112..fe1e29b33b3 100644 --- a/ui/js/controllers/main.js +++ b/ui/js/controllers/main.js @@ -98,7 +98,7 @@ treeherder.controller('MainCtrl', [ // Ctrl+Enter saves pinboard classification and related bugs } else if (!ev.metaKey && !ev.altKey && !ev.shiftKey && ev.ctrlKey) { - if ((ev.keyCode == 13) && $scope.selectedJob) { + if ((ev.keyCode === 13) && $scope.selectedJob) { $rootScope.$emit(thEvents.saveClassification); } } @@ -193,30 +193,18 @@ treeherder.controller('MainCtrl', [ }; - /** - * Extract the params from the querystring of this url that should - * trigger a reload of the page, because it requires new data from - * the repo - * @param urlStr a full Url as a string - * @returns Object containing only the params in ``reloadFields`` - */ - var getTriggerParams = function(urlStr) { - var tokens = urlStr.split("?"); - var triggerParams = {}; - - if (tokens.length > 1) { - triggerParams = _.pick( - $.deparam(tokens[1]), - ThResultSetModel.reloadOnChangeParameters - ); - } - - return triggerParams; + var getNewReloadTriggerParams = function() { + return _.pick( + $location.search(), + ThResultSetModel.reloadOnChangeParameters + ); }; + $scope.cachedReloadTriggerParams = getNewReloadTriggerParams(); + // reload the page if certain params were changed in the URL. For // others, such as filtering, just re-filter without reload. - $rootScope.$on('$locationChangeSuccess', function(ev, newUrl, oldUrl) { + $rootScope.$on('$locationChangeSuccess', function() { // used to test for display of watched-repo-navbar $rootScope.locationPath = $location.path().replace('/', ''); @@ -224,15 +212,18 @@ treeherder.controller('MainCtrl', [ // used to avoid bad urls when the app redirects internally $rootScope.urlBasePath = $location.absUrl().split('?')[0]; - $log.debug("check for reload", "newUrl=", newUrl, "oldUrl=", oldUrl); - - var oldParams = getTriggerParams(oldUrl); - var newParams = getTriggerParams(newUrl); + var newReloadTriggerParams = getNewReloadTriggerParams(); // if we are just setting the repo to the default because none was // set initially, then don't reload the page. - var defaulting = newParams.repo === thDefaultRepo && !oldParams.repo; - if (!_.isEqual(oldParams, newParams) && !defaulting) { + var defaulting = newReloadTriggerParams.repo === thDefaultRepo && + !$scope.cachedReloadTriggerParams.repo; + + if (!defaulting && $scope.cachedReloadTriggerParams && + !_.isEqual(newReloadTriggerParams, $scope.cachedReloadTriggerParams)) { + $window.location.reload(); + } else { + $scope.cachedReloadTriggerParams = newReloadTriggerParams; } }); diff --git a/ui/js/services/jobfilters.js b/ui/js/services/jobfilters.js index 323cd61620c..9bcbaa5fe4a 100644 --- a/ui/js/services/jobfilters.js +++ b/ui/js/services/jobfilters.js @@ -113,45 +113,32 @@ treeherder.factory('thJobFilters', [ var cachedResultStatusFilters = {}; var cachedClassifiedStateFilters = {}; var cachedFieldFilters = {}; + var cachedFilterParams; /** * Checks for a filter change and, if detected, updates the cached filter * values from the query string. Then publishes the global event * to re-render jobs. */ - $rootScope.$on('$locationChangeSuccess', function(ev, newUrl, oldUrl) { + $rootScope.$on('$locationChangeSuccess', function() { - $log.debug("check for refilter", "newUrl=", newUrl, "oldUrl=", oldUrl); + var newFilterParams = getNewFilterParams(); - var oldFilters = _getFilterParams(oldUrl); - var newFilters = _getFilterParams(newUrl); - if (!_.isEqual(oldFilters, newFilters)) { + if (!_.isEqual(cachedFilterParams, newFilterParams)) { + cachedFilterParams = newFilterParams; _refreshFilterCaches(); $rootScope.$emit(thEvents.globalFilterChanged); } }); - /** - * Take a url string and extract all the query string params that - * begin with the filter prefix. - * - * @param urlStr a full Url as a string - * @returns Object containing only the params with the PREFIX - */ - var _getFilterParams = function(urlStr) { - var tokens = urlStr.split("?"); + var getNewFilterParams = function() { var filterParams = {}; - - if (tokens.length > 1) { - var qsParams = $.deparam(tokens[1]); - _.each(qsParams, function (value, field) { - if (_startsWith(field, PREFIX)) { - filterParams[field] = value; - } - }); - } - + _.each($location.search(), function (value, field) { + if (_startsWith(field, PREFIX)) { + filterParams[field] = value; + } + }); return filterParams; }; @@ -557,6 +544,7 @@ treeherder.factory('thJobFilters', [ }; // initialize caches on initial load + cachedFilterParams = getNewFilterParams(); _refreshFilterCaches(); /********************************* diff --git a/ui/vendor/jquery.deparam.js b/ui/vendor/jquery.deparam.js deleted file mode 100644 index 619b3592c70..00000000000 --- a/ui/vendor/jquery.deparam.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * jQuery.deparam - The oposite of jQuery param. Creates an object of query string parameters. - * - * Credits for the idea and Regex: - * http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/ - * - * :camd - added support for multiple values of the same param -*/ -(function($){ - $.deparam = $.deparam || function(uri){ - if(uri === undefined){ - uri = window.location.search; - } - var queryString = {}; - uri.replace( - new RegExp( - "([^?=&]+)(=([^&#]*))?", "g"), - function($0, $1, $2, $3) { - if (queryString.hasOwnProperty($1)) { - if (!$.isArray(queryString[$1])) { - queryString[$1] = [queryString[$1]] - } - queryString[$1].push($3); - } else { - queryString[$1] = $3; - } - } - ); - return queryString; - }; -})(jQuery); \ No newline at end of file