-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jkmarx/avoid duplicate api calls #3204
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
12 changes: 0 additions & 12 deletions
12
refinery/ui/source/js/user-file-browser/ctrls/user-file-browser-ctrl.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
/** | ||
* User File Browser Files Ctrl | ||
* @namespace UserFileBrowserFilesCtrl | ||
* @desc Main controller for user files data. | ||
* @memberOf refineryApp.refineryUserFileBrowser | ||
*/ | ||
(function () { | ||
'use strict'; | ||
|
||
|
@@ -34,28 +40,27 @@ | |
userFileSortsService, | ||
gridOptionsService | ||
) { | ||
gridOptionsService.appScopeProvider = vm; | ||
var promise = $q.defer(); | ||
var vm = this; | ||
vm.nodesCount = userFileBrowserFactory.dataSetNodes.nodesCount; | ||
vm.totalNodesCount = userFileBrowserFactory.dataSetNodes.totalNodesCount; | ||
vm.downloadCsv = downloadCsv; | ||
vm.downloadCsvQuery = downloadCsvQuery; | ||
vm.refreshUserFiles = refreshUserFiles; | ||
vm.gridOptions = gridOptionsService; | ||
vm.nodesCount = userFileBrowserFactory.dataSetNodes.nodesCount; | ||
vm.sortChanged = sortChanged; | ||
vm.totalNodesCount = userFileBrowserFactory.dataSetNodes.totalNodesCount; | ||
|
||
var promise = $q.defer(); | ||
var getUserFiles = userFileBrowserFactory.getUserFiles; | ||
getUserFiles().then(function (solr) { | ||
gridOptionsService.columnDefs = userFileBrowserFactory.createColumnDefs( | ||
cullEmptyAttributes(solr.facet_field_counts) | ||
); | ||
userFileBrowserFactory.dataSetNodes.nodesCount = solr.nodes.length; | ||
userFileBrowserFactory.dataSetNodes.totalNodesCount = solr.nodes_count; | ||
|
||
gridOptionsService.data = userFileBrowserFactory.createData(solr.nodes); | ||
promise.resolve(); | ||
}, function () { | ||
$log.error('/files/ request failed'); | ||
promise.reject(); | ||
}); | ||
activate(); | ||
|
||
/* | ||
* --------------------------------------------------------- | ||
* Methods Definitions | ||
* --------------------------------------------------------- | ||
*/ | ||
function activate () { | ||
refreshUserFiles(); | ||
} | ||
// helper method to cull out attributes with no fields | ||
function cullEmptyAttributes (facetCountObj) { | ||
_.each(facetCountObj, function (counts, facetName) { | ||
|
@@ -67,40 +72,6 @@ | |
return _.keys(facetCountObj).concat('date_submitted', 'sample_name', 'name'); | ||
} | ||
|
||
vm.sortChanged = function (grid, sortColumns) { | ||
var sortUrlParam = 'sort'; | ||
var directionUrlParam = 'direction'; | ||
if (typeof sortColumns !== 'undefined' && sortColumns.length > 0) { | ||
// NOTE: With the current config, you can only sort on one column | ||
var column = sortColumns[0]; | ||
// If a hash is used with $location.search, it clears all params | ||
$location.search(sortUrlParam, column.field); | ||
$location.search(directionUrlParam, column.sort.direction); | ||
userFileSortsService.fields[0] = { | ||
name: column.field, | ||
direction: column.sort.direction | ||
}; | ||
|
||
// TODO: This is copy-and-paste | ||
getUserFiles().then(function (solr) { | ||
// TODO: Should there be something that wraps up this "then"? It is repeated. | ||
// gridOptionsService.columnDefs = userFileBrowserFactory.createColumnDefs(); | ||
gridOptionsService.data = userFileBrowserFactory.createData(solr.nodes); | ||
userFileBrowserFactory.dataSetNodes.nodesCount = solr.nodes.length; | ||
userFileBrowserFactory.dataSetNodes.totalNodesCount = solr.nodes_count; | ||
promise.resolve(); | ||
}, function () { | ||
$log.error('/files/ request failed'); | ||
promise.reject(); | ||
}); | ||
} else { | ||
$location.search(sortUrlParam, null); | ||
$location.search(directionUrlParam, null); | ||
} | ||
}; | ||
|
||
gridOptionsService.appScopeProvider = vm; | ||
|
||
/** | ||
* @name downloadCsv | ||
* @desc VM method used to interact with the `/files_download` | ||
|
@@ -125,11 +96,65 @@ | |
}); | ||
} | ||
|
||
vm.gridOptions = gridOptionsService; | ||
vm.gridOptions.onRegisterApi = function (api) { | ||
api.core.on.sortChanged(null, vm.sortChanged); | ||
}; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code kept in tack minus function wrapper. |
||
* @name refreshUserFiles | ||
* @desc Uses service to update the data in grid | ||
* @memberOf refineryUserFileBrowser.refreshUserFiles | ||
**/ | ||
function refreshUserFiles () { | ||
userFileBrowserFactory.getUserFiles().then(function (solr) { | ||
gridOptionsService.columnDefs = userFileBrowserFactory.createColumnDefs( | ||
cullEmptyAttributes(solr.facet_field_counts) | ||
); | ||
userFileBrowserFactory.dataSetNodes.nodesCount = solr.nodes.length; | ||
userFileBrowserFactory.dataSetNodes.totalNodesCount = solr.nodes_count; | ||
|
||
gridOptionsService.data = userFileBrowserFactory.createData(solr.nodes); | ||
promise.resolve(); | ||
}, function () { | ||
$log.error('/files/ request failed'); | ||
promise.reject(); | ||
}); | ||
} | ||
|
||
function sortChanged (grid, sortColumns) { | ||
var sortUrlParam = 'sort'; | ||
var directionUrlParam = 'direction'; | ||
if (typeof sortColumns !== 'undefined' && sortColumns.length > 0) { | ||
// NOTE: With the current config, you can only sort on one column | ||
var column = sortColumns[0]; | ||
// If a hash is used with $location.search, it clears all params | ||
$location.search(sortUrlParam, column.field); | ||
$location.search(directionUrlParam, column.sort.direction); | ||
userFileSortsService.fields[0] = { | ||
name: column.field, | ||
direction: column.sort.direction | ||
}; | ||
|
||
refreshUserFiles.getUserFiles().then(function (solr) { | ||
gridOptionsService.data = userFileBrowserFactory.createData(solr.nodes); | ||
userFileBrowserFactory.dataSetNodes.nodesCount = solr.nodes.length; | ||
userFileBrowserFactory.dataSetNodes.totalNodesCount = solr.nodes_count; | ||
promise.resolve(); | ||
}, function () { | ||
$log.error('/files/ request failed'); | ||
promise.reject(); | ||
}); | ||
} else { | ||
$location.search(sortUrlParam, null); | ||
$location.search(directionUrlParam, null); | ||
} | ||
} | ||
|
||
/* | ||
* --------------------------------------------------------- | ||
* Watchers | ||
* --------------------------------------------------------- | ||
*/ | ||
$scope.$watchCollection( | ||
function () { | ||
return userFileBrowserFactory.dataSetNodes; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
/** | ||
* User File Browser Filters Ctrl | ||
* @namespace UserFileBrowserFiltersCtrl | ||
* @desc Main controller for user files filter. | ||
* @memberOf refineryApp.refineryUserFileBrowser | ||
*/ | ||
(function () { | ||
'use strict'; | ||
|
||
|
@@ -9,6 +15,7 @@ | |
'$location', | ||
'$log', | ||
'$q', | ||
'$scope', | ||
'settings', | ||
'gridOptionsService', | ||
'userFileBrowserFactory', | ||
|
@@ -20,20 +27,31 @@ | |
$location, | ||
$log, | ||
$q, | ||
$scope, | ||
settings, | ||
gridOptionsService, | ||
userFileBrowserFactory, | ||
userFileFiltersService, | ||
userFileSortsService | ||
) { | ||
var direction; | ||
var promise = $q.defer(); | ||
var sort; | ||
var vm = this; | ||
vm.attributeFilters = userFileBrowserFactory.attributeFilters; | ||
vm.foldedDown = {}; | ||
// sync the attribute filter order with grid column order | ||
vm.orderColumns = settings.djangoApp.userFilesColumns; | ||
|
||
/* | ||
* --------------------------------------------------------- | ||
* Methods Definitions | ||
* --------------------------------------------------------- | ||
*/ | ||
vm.togglePanel = function (attribute) { | ||
vm.foldedDown[attribute] = ! vm.foldedDown[attribute]; | ||
}; | ||
|
||
vm.foldedDown = {}; | ||
vm.isDown = function (attribute, search) { | ||
var attributeObj = vm.attributeFilters[attribute]; | ||
return vm.foldedDown[attribute] || | ||
|
@@ -49,8 +67,7 @@ | |
var set = filterSet(attribute, value); | ||
$location.search(attribute, set); | ||
|
||
getUserFiles().then(function (solr) { | ||
// TODO: Should there be something that wraps up this "then"? It is repeated. | ||
userFileBrowserFactory.getUserFiles().then(function (solr) { | ||
vm.attributeFilters = | ||
userFileBrowserFactory.createFilters(solr.facet_field_counts); | ||
|
||
|
@@ -84,8 +101,6 @@ | |
return set; | ||
} | ||
|
||
var sort; | ||
var direction; | ||
angular.forEach($location.search(), function (values, key) { | ||
if (key === 'sort') { | ||
sort = values; | ||
|
@@ -108,15 +123,15 @@ | |
}; | ||
} | ||
|
||
var promise = $q.defer(); | ||
var getUserFiles = userFileBrowserFactory.getUserFiles; | ||
|
||
getUserFiles().then(function (solr) { | ||
vm.attributeFilters = userFileBrowserFactory.createFilters(solr.facet_field_counts); | ||
promise.resolve(); | ||
}, function () { | ||
$log.error('/files/ request failed'); | ||
promise.reject(); | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* --------------------------------------------------------- | ||
* Watchers | ||
* --------------------------------------------------------- | ||
*/ | ||
$scope.$watchCollection(function () { | ||
return userFileBrowserFactory.attributeFilters; | ||
}, function (updatedFilters) { | ||
vm.attributeFilters = updatedFilters; | ||
}); | ||
} | ||
})(); |
43 changes: 43 additions & 0 deletions
43
refinery/ui/source/js/user-file-browser/ctrls/user-file-browser-filters-ctrl.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(function () { | ||
'use strict'; | ||
|
||
describe('Controller: UserFileBrowserFiltersCtrl', function () { | ||
var ctrl; | ||
var factory; | ||
var scope; | ||
|
||
beforeEach(module('refineryApp')); | ||
beforeEach(module('refineryUserFileBrowser')); | ||
beforeEach(inject(function ( | ||
$controller, | ||
$rootScope, | ||
$window, | ||
settings, | ||
userFileBrowserFactory | ||
) { | ||
factory = userFileBrowserFactory; | ||
scope = $rootScope.$new(); | ||
settings.djangoApp.userFilesColumns = ''; | ||
|
||
ctrl = $controller('UserFileBrowserFiltersCtrl', { | ||
$scope: scope | ||
}); | ||
})); | ||
|
||
it('ctrl should exist', function () { | ||
expect(ctrl).toBeDefined(); | ||
}); | ||
|
||
it('view variables should be initialized', function () { | ||
expect(ctrl.attributeFilters).toEqual([]); | ||
expect(ctrl.foldedDown).toEqual({}); | ||
expect(ctrl.orderColumns).toEqual(''); | ||
}); | ||
|
||
it('watches factory attributeFilters and updates', function () { | ||
var mockAttributes = [{ name: 'Test Attribute' }]; | ||
angular.copy(mockAttributes, factory.attributeFilters); | ||
expect(ctrl.attributeFilters).toEqual(mockAttributes); | ||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.