import.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import kbn from 'app/core/utils/kbn';
  3. import coreModule from 'app/core/core_module';
  4. import appEvents from 'app/core/app_events';
  5. import config from 'app/core/config';
  6. import _ from 'lodash';
  7. export class DashImportCtrl {
  8. step: number;
  9. jsonText: string;
  10. parseError: string;
  11. nameExists: boolean;
  12. dash: any;
  13. dismiss: any;
  14. inputs: any[];
  15. inputsValid: boolean;
  16. /** @ngInject */
  17. constructor(private backendSrv, private $location, private $scope) {
  18. this.step = 1;
  19. this.nameExists = false;
  20. }
  21. onUpload(dash) {
  22. this.dash = dash;
  23. this.dash.id = null;
  24. this.step = 2;
  25. this.inputs = [];
  26. if (this.dash.__inputs) {
  27. for (let input of this.dash.__inputs) {
  28. var inputModel = {
  29. name: input.name,
  30. type: input.type,
  31. options: []
  32. };
  33. if (input.type === 'datasource') {
  34. this.setDatasourceOptions(input, inputModel);
  35. }
  36. this.inputs.push(inputModel);
  37. }
  38. }
  39. this.inputsValid = this.inputs.length === 0;
  40. this.titleChanged();
  41. }
  42. setDatasourceOptions(input, inputModel) {
  43. var sources = _.filter(config.datasources, val => {
  44. return val.type === input.pluginId;
  45. });
  46. if (sources.length === 0) {
  47. inputModel.error = "No data sources of type " + input.pluginName + " found";
  48. } else {
  49. inputModel.info = "Select a " + input.pluginName + " data source";
  50. }
  51. inputModel.options = sources.map(val => {
  52. return {text: val.name, value: val.name};
  53. });
  54. }
  55. inputOptionChanged() {
  56. this.inputsValid = true;
  57. for (let input of this.inputs) {
  58. if (!input.value) {
  59. this.inputsValid = false;
  60. }
  61. }
  62. }
  63. titleChanged() {
  64. this.backendSrv.search({query: this.dash.title}).then(res => {
  65. this.nameExists = false;
  66. for (let hit of res) {
  67. if (this.dash.title === hit.title) {
  68. this.nameExists = true;
  69. break;
  70. }
  71. }
  72. });
  73. }
  74. saveDashboard() {
  75. return this.backendSrv.saveDashboard(this.dash, {overwrite: true}).then(res => {
  76. this.$location.url('dashboard/db/' + res.slug);
  77. this.dismiss();
  78. });
  79. }
  80. loadJsonText() {
  81. try {
  82. this.parseError = '';
  83. var dash = JSON.parse(this.jsonText);
  84. this.onUpload(dash);
  85. } catch (err) {
  86. console.log(err);
  87. this.parseError = err.message;
  88. return;
  89. }
  90. }
  91. }
  92. export function dashImportDirective() {
  93. return {
  94. restrict: 'E',
  95. templateUrl: 'public/app/features/dashboard/import/import.html',
  96. controller: DashImportCtrl,
  97. bindToController: true,
  98. controllerAs: 'ctrl',
  99. };
  100. }
  101. coreModule.directive('dashImport', dashImportDirective);