plugin_edit_ctrl.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. }
  71. }
  72. update() {
  73. this.preUpdateHook().then(() => {
  74. var updateCmd = _.extend({
  75. enabled: this.model.enabled,
  76. pinned: this.model.pinned,
  77. jsonData: this.model.jsonData,
  78. secureJsonData: this.model.secureJsonData,
  79. }, {});
  80. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  81. })
  82. .then(this.postUpdateHook)
  83. .then((res) => {
  84. window.location.href = window.location.href;
  85. });
  86. }
  87. importDashboards() {
  88. return Promise.resolve();
  89. }
  90. setPreUpdateHook(callback: () => any) {
  91. this.preUpdateHook = callback;
  92. }
  93. setPostUpdateHook(callback: () => any) {
  94. this.postUpdateHook = callback;
  95. }
  96. updateAvailable() {
  97. var modalScope = this.$scope.$new(true);
  98. modalScope.plugin = this.model;
  99. this.$rootScope.appEvent('show-modal', {
  100. src: 'public/app/features/plugins/partials/update_instructions.html',
  101. scope: modalScope
  102. });
  103. }
  104. enable() {
  105. this.model.enabled = true;
  106. this.model.pinned = true;
  107. this.update();
  108. }
  109. disable() {
  110. this.model.enabled = false;
  111. this.model.pinned = false;
  112. this.update();
  113. }
  114. }
  115. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);