plugin_edit_ctrl.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import Remarkable from 'remarkable';
  5. export class PluginEditCtrl {
  6. model: any;
  7. pluginIcon: string;
  8. pluginId: any;
  9. includes: any;
  10. readmeHtml: any;
  11. includedDatasources: any;
  12. tab: string;
  13. navModel: any;
  14. hasDashboards: any;
  15. preUpdateHook: () => any;
  16. postUpdateHook: () => any;
  17. /** @ngInject */
  18. constructor(
  19. private $scope,
  20. private $rootScope,
  21. private backendSrv,
  22. private $sce,
  23. private $routeParams,
  24. navModelSrv
  25. ) {
  26. this.pluginId = $routeParams.pluginId;
  27. this.preUpdateHook = () => Promise.resolve();
  28. this.postUpdateHook = () => Promise.resolve();
  29. this.init();
  30. }
  31. setNavModel(model) {
  32. let defaultTab = 'readme';
  33. this.navModel = {
  34. main: {
  35. img: model.info.logos.large,
  36. subTitle: model.info.author.name,
  37. url: '',
  38. text: '',
  39. breadcrumbs: [
  40. { title: 'Plugins', url: '/plugins' },
  41. { title: model.name },
  42. ],
  43. children: [
  44. {
  45. icon: 'fa fa-fw fa-file-text-o',
  46. id: 'readme',
  47. text: 'Readme',
  48. url: `plugins/${this.model.id}/edit?tab=readme`,
  49. },
  50. ],
  51. },
  52. };
  53. if (model.type === 'app') {
  54. this.navModel.main.children.push({
  55. icon: 'gicon gicon-cog',
  56. id: 'config',
  57. text: 'Config',
  58. url: `plugins/${this.model.id}/edit?tab=config`,
  59. });
  60. let hasDashboards = _.find(model.includes, { type: 'dashboard' });
  61. if (hasDashboards) {
  62. this.navModel.main.children.push({
  63. icon: 'gicon gicon-dashboard',
  64. id: 'dashboards',
  65. text: 'Dashboards',
  66. url: `plugins/${this.model.id}/edit?tab=dashboards`,
  67. });
  68. }
  69. defaultTab = 'config';
  70. }
  71. this.tab = this.$routeParams.tab || defaultTab;
  72. for (let tab of this.navModel.main.children) {
  73. if (tab.id === this.tab) {
  74. tab.active = true;
  75. }
  76. }
  77. }
  78. init() {
  79. return this.backendSrv
  80. .get(`/api/plugins/${this.pluginId}/settings`)
  81. .then(result => {
  82. this.model = result;
  83. this.pluginIcon = this.getPluginIcon(this.model.type);
  84. this.model.dependencies.plugins.forEach(plug => {
  85. plug.icon = this.getPluginIcon(plug.type);
  86. });
  87. this.includes = _.map(result.includes, plug => {
  88. plug.icon = this.getPluginIcon(plug.type);
  89. return plug;
  90. });
  91. this.setNavModel(this.model);
  92. return this.initReadme();
  93. });
  94. }
  95. initReadme() {
  96. return this.backendSrv
  97. .get(`/api/plugins/${this.pluginId}/markdown/readme`)
  98. .then(res => {
  99. var md = new Remarkable();
  100. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  101. });
  102. }
  103. getPluginIcon(type) {
  104. switch (type) {
  105. case 'datasource':
  106. return 'icon-gf icon-gf-datasources';
  107. case 'panel':
  108. return 'icon-gf icon-gf-panel';
  109. case 'app':
  110. return 'icon-gf icon-gf-apps';
  111. case 'page':
  112. return 'icon-gf icon-gf-endpoint-tiny';
  113. case 'dashboard':
  114. return 'icon-gf icon-gf-dashboard';
  115. default:
  116. return 'icon-gf icon-gf-apps';
  117. }
  118. }
  119. update() {
  120. this.preUpdateHook()
  121. .then(() => {
  122. var updateCmd = _.extend(
  123. {
  124. enabled: this.model.enabled,
  125. pinned: this.model.pinned,
  126. jsonData: this.model.jsonData,
  127. secureJsonData: this.model.secureJsonData,
  128. },
  129. {}
  130. );
  131. return this.backendSrv.post(
  132. `/api/plugins/${this.pluginId}/settings`,
  133. updateCmd
  134. );
  135. })
  136. .then(this.postUpdateHook)
  137. .then(res => {
  138. window.location.href = window.location.href;
  139. });
  140. }
  141. importDashboards() {
  142. return Promise.resolve();
  143. }
  144. setPreUpdateHook(callback: () => any) {
  145. this.preUpdateHook = callback;
  146. }
  147. setPostUpdateHook(callback: () => any) {
  148. this.postUpdateHook = callback;
  149. }
  150. updateAvailable() {
  151. var modalScope = this.$scope.$new(true);
  152. modalScope.plugin = this.model;
  153. this.$rootScope.appEvent('show-modal', {
  154. src: 'public/app/features/plugins/partials/update_instructions.html',
  155. scope: modalScope,
  156. });
  157. }
  158. enable() {
  159. this.model.enabled = true;
  160. this.model.pinned = true;
  161. this.update();
  162. }
  163. disable() {
  164. this.model.enabled = false;
  165. this.model.pinned = false;
  166. this.update();
  167. }
  168. }
  169. angular
  170. .module('grafana.controllers')
  171. .controller('PluginEditCtrl', PluginEditCtrl);