From fc44dba2effe0632baee38db438c01019f998228 Mon Sep 17 00:00:00 2001 From: Todd Wolfson Date: Wed, 7 Oct 2015 11:43:28 -0500 Subject: [PATCH] Added "Select last used data source" to query view --- rd_ui/app/scripts/controllers/query_view.js | 28 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/rd_ui/app/scripts/controllers/query_view.js b/rd_ui/app/scripts/controllers/query_view.js index 4548b0d74..361c77e46 100644 --- a/rd_ui/app/scripts/controllers/query_view.js +++ b/rd_ui/app/scripts/controllers/query_view.js @@ -19,14 +19,35 @@ $scope.queryResult = $scope.query.getQueryResult(maxAge, parameters); } + var getDataSourceId = function() { + // Try to get the query's data source id + var dataSourceId = $scope.query.data_source_id; + + // If there is no source yet, then parse what we have in localStorage + // e.g. `null` -> `NaN`, malformed data -> `NaN`, "1" -> 1 + if (dataSourceId === undefined) { + dataSourceId = parseInt(localStorage.lastSelectedDataSourceId, 10); + } + + // If we had an invalid value in localStorage (e.g. nothing, deleted source), then use the first data source + var isValidDataSourceId = !isNaN(dataSourceId) && _.some($scope.dataSources, function(ds) { + return ds.id == dataSourceId; + }); + if (!isValidDataSourceId) { + dataSourceId = $scope.dataSources[0].id; + } + + // Return our data source id + return dataSourceId; + } + $scope.dataSource = {}; $scope.query = $route.current.locals.query; var updateSchema = function() { $scope.hasSchema = false; $scope.editorSize = "col-md-12"; - var dataSourceId = $scope.query.data_source_id || $scope.dataSources[0].id; - DataSource.getSchema({id: dataSourceId}, function(data) { + DataSource.getSchema({id: getDataSourceId()}, function(data) { if (data && data.length > 0) { $scope.schema = data; _.each(data, function(table) { @@ -53,7 +74,7 @@ updateSchema(); if ($scope.query.isNew()) { - $scope.query.data_source_id = $scope.query.data_source_id || dataSources[0].id; + $scope.query.data_source_id = getDataSourceId(); $scope.dataSource = _.find(dataSources, function(ds) { return ds.id == $scope.query.data_source_id; }); } }); @@ -146,6 +167,7 @@ $scope.updateDataSource = function() { Events.record(currentUser, 'update_data_source', 'query', $scope.query.id); + localStorage.lastSelectedDataSourceId = $scope.query.data_source_id; $scope.query.latest_query_data = null; $scope.query.latest_query_data_id = null;