| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- ///<reference path="../../../headers/common.d.ts" />
- import kbn from 'app/core/utils/kbn';
- import coreModule from 'app/core/core_module';
- import appEvents from 'app/core/app_events';
- import config from 'app/core/config';
- import _ from 'lodash';
- export class DashImportCtrl {
- step: number;
- jsonText: string;
- parseError: string;
- nameExists: boolean;
- dash: any;
- dismiss: any;
- inputs: any[];
- inputsValid: boolean;
- /** @ngInject */
- constructor(private backendSrv, private $location, private $scope) {
- this.step = 1;
- this.nameExists = false;
- }
- onUpload(dash) {
- this.dash = dash;
- this.dash.id = null;
- this.step = 2;
- this.inputs = [];
- if (this.dash.__inputs) {
- for (let input of this.dash.__inputs) {
- var inputModel = {
- name: input.name,
- type: input.type,
- options: []
- };
- if (input.type === 'datasource') {
- this.setDatasourceOptions(input, inputModel);
- }
- this.inputs.push(inputModel);
- }
- }
- this.inputsValid = this.inputs.length === 0;
- this.titleChanged();
- }
- setDatasourceOptions(input, inputModel) {
- var sources = _.filter(config.datasources, val => {
- return val.type === input.pluginId;
- });
- if (sources.length === 0) {
- inputModel.error = "No data sources of type " + input.pluginName + " found";
- } else {
- inputModel.info = "Select a " + input.pluginName + " data source";
- }
- inputModel.options = sources.map(val => {
- return {text: val.name, value: val.name};
- });
- }
- inputOptionChanged() {
- this.inputsValid = true;
- for (let input of this.inputs) {
- if (!input.value) {
- this.inputsValid = false;
- }
- }
- }
- titleChanged() {
- this.backendSrv.search({query: this.dash.title}).then(res => {
- this.nameExists = false;
- for (let hit of res) {
- if (this.dash.title === hit.title) {
- this.nameExists = true;
- break;
- }
- }
- });
- }
- saveDashboard() {
- return this.backendSrv.saveDashboard(this.dash, {overwrite: true}).then(res => {
- this.$location.url('dashboard/db/' + res.slug);
- this.dismiss();
- });
- }
- loadJsonText() {
- try {
- this.parseError = '';
- var dash = JSON.parse(this.jsonText);
- this.onUpload(dash);
- } catch (err) {
- console.log(err);
- this.parseError = err.message;
- return;
- }
- }
- }
- export function dashImportDirective() {
- return {
- restrict: 'E',
- templateUrl: 'public/app/features/dashboard/import/import.html',
- controller: DashImportCtrl,
- bindToController: true,
- controllerAs: 'ctrl',
- };
- }
- coreModule.directive('dashImport', dashImportDirective);
|