plugin_edit_ctrl.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import appEvents from 'app/core/app_events';
  5. import {WizardFlow} from './wizard';
  6. export class PluginEditCtrl {
  7. model: any;
  8. pluginIcon: string;
  9. pluginId: any;
  10. includes: any;
  11. readmeHtml: any;
  12. includedDatasources: any;
  13. tabIndex: number;
  14. tabs: any;
  15. hasDashboards: any;
  16. preUpdateHook: () => any;
  17. postUpdateHook: () => any;
  18. /** @ngInject */
  19. constructor(private $scope,
  20. private backendSrv,
  21. private $routeParams,
  22. private $sce,
  23. private $http) {
  24. this.model = {};
  25. this.pluginId = $routeParams.pluginId;
  26. this.tabIndex = 0;
  27. this.tabs = ['Overview'];
  28. this.preUpdateHook = () => Promise.resolve();
  29. this.postUpdateHook = () => Promise.resolve();
  30. }
  31. init() {
  32. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(result => {
  33. this.model = result;
  34. this.pluginIcon = this.getPluginIcon(this.model.type);
  35. this.model.dependencies.plugins.forEach(plug => {
  36. plug.icon = this.getPluginIcon(plug.type);
  37. });
  38. this.includes = _.map(result.includes, plug => {
  39. plug.icon = this.getPluginIcon(plug.type);
  40. return plug;
  41. });
  42. if (this.model.type === 'app') {
  43. this.tabs.push('Config');
  44. this.hasDashboards = _.findWhere(result.includes, {type: 'dashboard'});
  45. if (this.hasDashboards) {
  46. this.tabs.push('Dashboards');
  47. }
  48. }
  49. return this.initReadme();
  50. });
  51. }
  52. initReadme() {
  53. return this.backendSrv.get(`/api/plugins/${this.pluginId}/readme`).then(res => {
  54. return System.import('remarkable').then(Remarkable => {
  55. var md = new Remarkable();
  56. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  57. });
  58. });
  59. }
  60. getPluginIcon(type) {
  61. switch (type) {
  62. case 'datasource': return 'icon-gf icon-gf-datasources';
  63. case 'panel': return 'icon-gf icon-gf-panel';
  64. case 'app': return 'icon-gf icon-gf-apps';
  65. case 'page': return 'icon-gf icon-gf-share';
  66. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  67. }
  68. }
  69. update() {
  70. var wizard = new WizardFlow("Application Setup");
  71. wizard.addStep("Validating form", () => {
  72. return new Promise((resolve) => {
  73. setTimeout(resolve, 2000);
  74. });
  75. });
  76. wizard.addStep("Saving application config", () => {
  77. return new Promise((resolve) => {
  78. setTimeout(resolve, 2000);
  79. });
  80. });
  81. wizard.addStep("Validing key", () => {
  82. return new Promise((resolve) => {
  83. setTimeout(resolve, 2000);
  84. });
  85. });
  86. wizard.addStep("Adding Raintank metric data source", () => {
  87. return new Promise((resolve) => {
  88. setTimeout(resolve, 2000);
  89. });
  90. });
  91. wizard.addStep("Adding Raintank event data source", () => {
  92. return new Promise((resolve) => {
  93. setTimeout(resolve, 2000);
  94. });
  95. });
  96. wizard.addStep("Importing worldPing dashboards", () => {
  97. return new Promise((resolve) => {
  98. setTimeout(resolve, 2000);
  99. });
  100. });
  101. wizard.start();
  102. // this.preUpdateHook().then(() => {
  103. // var updateCmd = _.extend({
  104. // enabled: this.model.enabled,
  105. // pinned: this.model.pinned,
  106. // jsonData: this.model.jsonData,
  107. // secureJsonData: this.model.secureJsonData,
  108. // }, {});
  109. // return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  110. // })
  111. // .then(this.postUpdateHook)
  112. // .then((res) => {
  113. // window.location.href = window.location.href;
  114. // });
  115. }
  116. importDashboards() {
  117. // move to dashboards tab
  118. this.tabIndex = 2;
  119. return new Promise((resolve) => {
  120. if (!this.$scope.$$phase) {
  121. this.$scope.$digest();
  122. }
  123. // let angular load dashboards tab
  124. setTimeout(() => {
  125. resolve();
  126. }, 1000);
  127. }).then(() => {
  128. return new Promise((resolve, reject) => {
  129. // send event to import list component
  130. appEvents.emit('dashboard-list-import-all', {
  131. resolve: resolve,
  132. reject: reject
  133. });
  134. });
  135. });
  136. }
  137. setPreUpdateHook(callback: () => any) {
  138. this.preUpdateHook = callback;
  139. }
  140. setPostUpdateHook(callback: () => any) {
  141. this.postUpdateHook = callback;
  142. }
  143. enable() {
  144. this.model.enabled = true;
  145. this.model.pinned = true;
  146. this.update();
  147. }
  148. disable() {
  149. this.model.enabled = false;
  150. this.model.pinned = false;
  151. this.update();
  152. }
  153. }
  154. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);