plugin_edit_ctrl.ts 4.9 KB

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