From 52cbd6655068f4ddcfd5f2bd69057bcebedd3cbf Mon Sep 17 00:00:00 2001 From: Alison Date: Tue, 8 Aug 2017 15:59:27 -0500 Subject: [PATCH 1/2] commit point --- .../app/pages/queries/add-to-dashboard.html | 23 ++++ client/app/pages/queries/add-to-dashboard.js | 104 ++++++++++++++++++ client/app/pages/queries/index.js | 2 + client/app/pages/queries/query.html | 3 + client/app/pages/queries/view.js | 11 ++ client/app/services/dashboard.js | 2 + redash/handlers/api.py | 4 +- redash/handlers/dashboards.py | 24 ++++ redash/models.py | 23 ++++ 9 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 client/app/pages/queries/add-to-dashboard.html create mode 100644 client/app/pages/queries/add-to-dashboard.js diff --git a/client/app/pages/queries/add-to-dashboard.html b/client/app/pages/queries/add-to-dashboard.html new file mode 100644 index 0000000000..e519fc06b0 --- /dev/null +++ b/client/app/pages/queries/add-to-dashboard.html @@ -0,0 +1,23 @@ + + diff --git a/client/app/pages/queries/add-to-dashboard.js b/client/app/pages/queries/add-to-dashboard.js new file mode 100644 index 0000000000..59b18ac7a0 --- /dev/null +++ b/client/app/pages/queries/add-to-dashboard.js @@ -0,0 +1,104 @@ +import template from './add-to-dashboard.html'; + +const AddToDashboardForm = { + controller($sce, Dashboard, currentUser, toastr, Query, Widget) { + 'ngInject'; + + this.query = this.resolve.query; + this.saveAddToDashbosard = this.resolve.saveAddToDashboard; + this.saveInProgress = false; + + this.trustAsHtml = html => $sce.trustAsHtml(html); + + this.onDashboardSelected = (dash) => { + // console.log(dash); + // add widget to dashboard + this.saveInProgress = true; + + // this.dashboard = this.resolve.dashboard; + // this.saveInProgress = false; + this.widgetSize = 1; + this.selectedVis = null; + this.query = {}; + this.selected_query = this.query.id; + + this.type = 'visualization'; + this.isVisualization = () => this.type === 'visualization'; + + // TODO handle multiple visualization queries + + // console.log(this.resolve.query); + Query.get({ id: this.resolve.query.id }, (query) => { + if (query) { + this.selected_query = query; + if (query.visualizations.length) { + this.selectedVis = query.visualizations[0]; + } + } + }); + + const widget = new Widget({ + visualization_id: this.selectedVis && this.selectedVis.id, + dashboard_id: dash.id, + options: {}, + width: this.widgetSize, + type: this.type, + }); + console.log(dash); + console.log(widget); + widget.$save().then((response) => { + console.log(response); + this.selectedDashboard = Dashboard.get({ slug: dash.slug }); + console.log(this.selectedDashboard); + console.log(this.selectedDashboard.version); + console.log(this.selectedDashboard.layout); + console.log(this.selectedDashboard.widgets); + // update dashboard layout + this.selectedDashboard.layout = response.layout; + this.selectedDashboard.version = response.version; + const newWidget = new Widget(response.widget); + if (response.new_row) { + this.selectedDashboard.widgets.push([newWidget]); + } else { + this.selectedDashboard.widgets[this.selectedDashboard.widgets.length - 1].push(newWidget); + } + this.close(); + }).catch(() => { + toastr.error('Widget can not be added'); + }).finally(() => { + this.saveInProgress = false; + }); + + // TODO go to dashboard, preserving GET parameters + this.dashboardPath = `/dashboards/${dash.slug}`; + // $location.path(this.dashboardPath); + }; + + this.selectedDashboard = null; + + this.searchDashboards = (term, limitToUsersDashboards) => { + if (!term || term.length < 3) { + return; + } + + Dashboard.search({ + q: term, + user_id: currentUser.id, + limit_to_users_dashboards: limitToUsersDashboards, + include_drafts: true, + }, (results) => { + this.dashboards = results; + }); + }; + }, + bindings: { + resolve: '<', + close: '&', + dismiss: '&', + }, + template, +}; + +export default function (ngModule) { + ngModule.component('addToDashboardDialog', AddToDashboardForm); +} diff --git a/client/app/pages/queries/index.js b/client/app/pages/queries/index.js index cc0daab198..248bd2b908 100644 --- a/client/app/pages/queries/index.js +++ b/client/app/pages/queries/index.js @@ -11,6 +11,7 @@ import registerQuerySearchResultsPage from './queries-search-results-page'; import registerVisualizationEmbed from './visualization-embed'; import registerCompareQueryDialog from './compare-query-dialog'; import registerGetDataSourceVersion from './get-data-source-version'; +import registerAddToDashboard from './add-to-dashboard'; export default function (ngModule) { registerQueryResultsLink(ngModule); @@ -23,6 +24,7 @@ export default function (ngModule) { registerCompareQueryDialog(ngModule); registerApiKeyDialog(ngModule); registerGetDataSourceVersion(ngModule); + registerAddToDashboard(ngModule); return Object.assign({}, registerQuerySearchResultsPage(ngModule), registerSourceView(ngModule), diff --git a/client/app/pages/queries/query.html b/client/app/pages/queries/query.html index d42447341c..d6a925e18b 100644 --- a/client/app/pages/queries/query.html +++ b/client/app/pages/queries/query.html @@ -157,6 +157,9 @@

uib-dropdown-toggle aria-expanded="false"> Download Dataset +