diff --git a/public/app/core/utils/emitter.ts b/public/app/core/utils/emitter.ts index 8a38adcc625..045182c76d1 100644 --- a/public/app/core/utils/emitter.ts +++ b/public/app/core/utils/emitter.ts @@ -21,10 +21,18 @@ export class Emitter { this.subjects[fnName].next(data); } - on(name, handler) { + on(name, handler, $scope) { var fnName = createName(name); this.subjects[fnName] || (this.subjects[fnName] = new Subject()); - this.subjects[fnName].subscribe(handler); + var subscription = this.subjects[fnName].subscribe(handler); + + if ($scope) { + $scope.$on('$destroy', function() { + subscription.unsubscribe(); + }); + } + + return subscription; }; off(name, handler) { diff --git a/public/app/features/plugins/import_list/import_list.ts b/public/app/features/plugins/import_list/import_list.ts index dd0a8eef524..8eb726b81c0 100644 --- a/public/app/features/plugins/import_list/import_list.ts +++ b/public/app/features/plugins/import_list/import_list.ts @@ -3,18 +3,43 @@ import angular from 'angular'; import _ from 'lodash'; import coreModule from 'app/core/core_module'; +import appEvents from 'app/core/app_events'; export class DashImportListCtrl { dashboards: any[]; plugin: any; datasource: any; - constructor(private $http, private backendSrv, private $rootScope) { + constructor($scope, private $http, private backendSrv, private $rootScope) { this.dashboards = []; backendSrv.get(`/api/plugins/${this.plugin.id}/dashboards`).then(dashboards => { this.dashboards = dashboards; }); + + appEvents.on('dashboard-list-import-all', this.importAll.bind(this), $scope); + } + + importAll(payload) { + return this.importNext(0).then(() => { + payload.resolve("All dashboards imported"); + }).catch(err => { + payload.reject(err); + }); + } + + importNext(index) { + return this.import(this.dashboards[index], true).then(() => { + if (index+1 < this.dashboards.length) { + return new Promise(resolve => { + setTimeout(() => { + this.importNext(index+1).then(() => { + resolve(); + }); + }, 500); + }); + } + }); } import(dash, reinstall) { @@ -34,7 +59,7 @@ export class DashImportListCtrl { }); } - this.backendSrv.post(`/api/dashboards/import`, installCmd).then(res => { + return this.backendSrv.post(`/api/dashboards/import`, installCmd).then(res => { this.$rootScope.appEvent('alert-success', ['Dashboard Installed', dash.title]); _.extend(dash, res); }); diff --git a/public/app/features/plugins/plugin_edit_ctrl.ts b/public/app/features/plugins/plugin_edit_ctrl.ts index ae6f9b166aa..7f3f6bff08f 100644 --- a/public/app/features/plugins/plugin_edit_ctrl.ts +++ b/public/app/features/plugins/plugin_edit_ctrl.ts @@ -2,6 +2,7 @@ import angular from 'angular'; import _ from 'lodash'; +import appEvents from 'app/core/app_events'; export class PluginEditCtrl { model: any; @@ -17,11 +18,18 @@ export class PluginEditCtrl { postUpdateHook: () => any; /** @ngInject */ - constructor(private backendSrv, private $routeParams, private $sce, private $http) { + constructor(private $scope, + private backendSrv, + private $routeParams, + private $sce, + private $http) { this.model = {}; this.pluginId = $routeParams.pluginId; this.tabIndex = 0; this.tabs = ['Overview']; + + this.preUpdateHook = () => Promise.resolve(); + this.postUpdateHook = () => Promise.resolve(); } init() { @@ -71,43 +79,47 @@ export class PluginEditCtrl { } update() { - var chain = Promise.resolve(); - var self = this; - // if set, handle the preUpdateHook. If this returns a promise, - // the next step of execution will block until the promise resolves. - // if the promise is rejected, this update will be aborted. - if (this.preUpdateHook != null) { - chain = self.preUpdateHook(); - } - - // Perform the core update procedure - chain = chain.then(function() { + this.preUpdateHook().then(() => { var updateCmd = _.extend({ - enabled: self.model.enabled, - pinned: self.model.pinned, - jsonData: self.model.jsonData, - secureJsonData: self.model.secureJsonData, + enabled: this.model.enabled, + pinned: this.model.pinned, + jsonData: this.model.jsonData, + secureJsonData: this.model.secureJsonData, }, {}); - return self.backendSrv.post(`/api/plugins/${self.pluginId}/settings`, updateCmd); - }); - - // if set, performt he postUpdate hook. If a promise is returned it will block - // the final step of the update procedure (reloading the page) until the promise - // resolves. If the promise is rejected the page will not be reloaded. - if (this.postUpdateHook != null) { - chain = chain.then(function() { - return this.postUpdateHook(); - }); - } - - // all stesp in the update procedure are complete, so reload the page to make changes - // take effect. - chain.then(function() { + return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd); + }) + .then(this.postUpdateHook) + .then((res) => { window.location.href = window.location.href; }); } + importDashboards() { + // move to dashboards tab + this.tabIndex = 2; + + return new Promise((resolve) => { + if (!this.$scope.$$phase) { + this.$scope.$digest(); + } + + // let angular load dashboards tab + setTimeout(() => { + resolve(); + }, 1000); + + }).then(() => { + return new Promise((resolve, reject) => { + // send event to import list component + appEvents.emit('dashboard-list-import-all', { + resolve: resolve, + reject: reject + }); + }); + }); + } + setPreUpdateHook(callback: () => any) { this.preUpdateHook = callback; }