plugin_edit_ctrl.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. tabIndex: number;
  13. tabs: any;
  14. navModel: any;
  15. hasDashboards: any;
  16. preUpdateHook: () => any;
  17. postUpdateHook: () => any;
  18. /** @ngInject */
  19. constructor(
  20. private $scope,
  21. private $rootScope,
  22. private backendSrv,
  23. private $sce,
  24. $routeParams,
  25. navModelSrv,
  26. ) {
  27. this.navModel = navModelSrv.getPluginsNav();
  28. this.model = {};
  29. this.pluginId = $routeParams.pluginId;
  30. this.tabIndex = 0;
  31. this.tabs = ['Readme'];
  32. this.preUpdateHook = () => Promise.resolve();
  33. this.postUpdateHook = () => Promise.resolve();
  34. }
  35. init() {
  36. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(result => {
  37. this.model = result;
  38. this.pluginIcon = this.getPluginIcon(this.model.type);
  39. this.model.dependencies.plugins.forEach(plug => {
  40. plug.icon = this.getPluginIcon(plug.type);
  41. });
  42. this.includes = _.map(result.includes, plug => {
  43. plug.icon = this.getPluginIcon(plug.type);
  44. return plug;
  45. });
  46. if (this.model.type === 'app') {
  47. this.hasDashboards = _.find(result.includes, {type: 'dashboard'});
  48. if (this.hasDashboards) {
  49. this.tabs.unshift('Dashboards');
  50. }
  51. this.tabs.unshift('Config');
  52. this.tabIndex = 0;
  53. }
  54. return this.initReadme();
  55. });
  56. }
  57. initReadme() {
  58. return this.backendSrv.get(`/api/plugins/${this.pluginId}/markdown/readme`).then(res => {
  59. var md = new Remarkable();
  60. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  61. });
  62. }
  63. getPluginIcon(type) {
  64. switch (type) {
  65. case 'datasource': return 'icon-gf icon-gf-datasources';
  66. case 'panel': return 'icon-gf icon-gf-panel';
  67. case 'app': return 'icon-gf icon-gf-apps';
  68. case 'page': return 'icon-gf icon-gf-endpoint-tiny';
  69. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  70. default: return 'icon-gf icon-gf-apps';
  71. }
  72. }
  73. update() {
  74. this.preUpdateHook().then(() => {
  75. var updateCmd = _.extend({
  76. enabled: this.model.enabled,
  77. pinned: this.model.pinned,
  78. jsonData: this.model.jsonData,
  79. secureJsonData: this.model.secureJsonData,
  80. }, {});
  81. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  82. })
  83. .then(this.postUpdateHook)
  84. .then((res) => {
  85. window.location.href = window.location.href;
  86. });
  87. }
  88. importDashboards() {
  89. return Promise.resolve();
  90. }
  91. setPreUpdateHook(callback: () => any) {
  92. this.preUpdateHook = callback;
  93. }
  94. setPostUpdateHook(callback: () => any) {
  95. this.postUpdateHook = callback;
  96. }
  97. updateAvailable() {
  98. var modalScope = this.$scope.$new(true);
  99. modalScope.plugin = this.model;
  100. this.$rootScope.appEvent('show-modal', {
  101. src: 'public/app/features/plugins/partials/update_instructions.html',
  102. scope: modalScope
  103. });
  104. }
  105. enable() {
  106. this.model.enabled = true;
  107. this.model.pinned = true;
  108. this.update();
  109. }
  110. disable() {
  111. this.model.enabled = false;
  112. this.model.pinned = false;
  113. this.update();
  114. }
  115. }
  116. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);