plugin_edit_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. export class PluginEditCtrl {
  6. model: any;
  7. pluginIcon: string;
  8. pluginId: any;
  9. includes: any;
  10. readmeHtml: any;
  11. includedDatasources: any;
  12. tabIndex: number;
  13. tabs: any;
  14. hasDashboards: any;
  15. preUpdateHook: () => any;
  16. postUpdateHook: () => any;
  17. /** @ngInject */
  18. constructor(private $scope,
  19. private $rootScope,
  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-endpoint-tiny';
  66. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  67. }
  68. }
  69. update() {
  70. this.preUpdateHook().then(() => {
  71. var updateCmd = _.extend({
  72. enabled: this.model.enabled,
  73. pinned: this.model.pinned,
  74. jsonData: this.model.jsonData,
  75. secureJsonData: this.model.secureJsonData,
  76. }, {});
  77. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  78. })
  79. .then(this.postUpdateHook)
  80. .then((res) => {
  81. window.location.href = window.location.href;
  82. });
  83. }
  84. importDashboards() {
  85. // move to dashboards tab
  86. this.tabIndex = 2;
  87. return new Promise((resolve) => {
  88. if (!this.$scope.$$phase) {
  89. this.$scope.$digest();
  90. }
  91. // let angular load dashboards tab
  92. setTimeout(() => {
  93. resolve();
  94. }, 1000);
  95. }).then(() => {
  96. return new Promise((resolve, reject) => {
  97. // send event to import list component
  98. appEvents.emit('dashboard-list-import-all', {
  99. resolve: resolve,
  100. reject: reject
  101. });
  102. });
  103. });
  104. }
  105. setPreUpdateHook(callback: () => any) {
  106. this.preUpdateHook = callback;
  107. }
  108. setPostUpdateHook(callback: () => any) {
  109. this.postUpdateHook = callback;
  110. }
  111. updateAvailable() {
  112. var modalScope = this.$scope.$new(true);
  113. modalScope.plugin = this.model;
  114. this.$rootScope.appEvent('show-modal', {
  115. src: 'public/app/features/plugins/partials/update_instructions.html',
  116. scope: modalScope
  117. });
  118. }
  119. enable() {
  120. this.model.enabled = true;
  121. this.model.pinned = true;
  122. this.update();
  123. }
  124. disable() {
  125. this.model.enabled = false;
  126. this.model.pinned = false;
  127. this.update();
  128. }
  129. }
  130. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);