|
| 1 | +import template from './add-to-dashboard.html'; |
| 2 | + |
| 3 | +const AddToDashboardForm = { |
| 4 | + controller($sce, Dashboard, currentUser, toastr, Query, Widget) { |
| 5 | + 'ngInject'; |
| 6 | + |
| 7 | + this.query = this.resolve.query; |
| 8 | + this.vis = this.resolve.vis; |
| 9 | + this.saveAddToDashbosard = this.resolve.saveAddToDashboard; |
| 10 | + this.saveInProgress = false; |
| 11 | + |
| 12 | + this.trustAsHtml = html => $sce.trustAsHtml(html); |
| 13 | + |
| 14 | + this.onDashboardSelected = (dash) => { |
| 15 | + // add widget to dashboard |
| 16 | + this.saveInProgress = true; |
| 17 | + this.widgetSize = 1; |
| 18 | + this.selectedVis = null; |
| 19 | + this.query = {}; |
| 20 | + this.selected_query = this.query.id; |
| 21 | + this.type = 'visualization'; |
| 22 | + this.isVisualization = () => this.type === 'visualization'; |
| 23 | + |
| 24 | + const widget = new Widget({ |
| 25 | + visualization_id: this.vis && this.vis.id, |
| 26 | + dashboard_id: dash.id, |
| 27 | + options: {}, |
| 28 | + width: this.widgetSize, |
| 29 | + type: this.type, |
| 30 | + }); |
| 31 | + |
| 32 | + // (response) |
| 33 | + widget.$save().then(() => { |
| 34 | + // (dashboard) |
| 35 | + this.selectedDashboard = Dashboard.get({ slug: dash.slug }, () => {}); |
| 36 | + this.close(); |
| 37 | + }).catch(() => { |
| 38 | + toastr.error('Widget can not be added'); |
| 39 | + }).finally(() => { |
| 40 | + this.saveInProgress = false; |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + this.selectedDashboard = null; |
| 45 | + |
| 46 | + this.searchDashboards = (term) => { // , limitToUsersDashboards |
| 47 | + if (!term || term.length < 3) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + Dashboard.search({ |
| 52 | + q: term, |
| 53 | + user_id: currentUser.id, |
| 54 | + // limit_to_users_dashboards: limitToUsersDashboards, |
| 55 | + include_drafts: true, |
| 56 | + }, (results) => { |
| 57 | + this.dashboards = results; |
| 58 | + }); |
| 59 | + }; |
| 60 | + }, |
| 61 | + bindings: { |
| 62 | + resolve: '<', |
| 63 | + close: '&', |
| 64 | + dismiss: '&', |
| 65 | + vis: '<', |
| 66 | + }, |
| 67 | + template, |
| 68 | +}; |
| 69 | + |
| 70 | +export default function (ngModule) { |
| 71 | + ngModule.component('addToDashboardDialog', AddToDashboardForm); |
| 72 | +} |
0 commit comments