plugin_edit_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.tabIndex = 1;
  44. this.tabs.push('Config');
  45. this.hasDashboards = _.findWhere(result.includes, {type: 'dashboard'});
  46. if (this.hasDashboards) {
  47. this.tabs.push('Dashboards');
  48. }
  49. }
  50. return this.initReadme();
  51. });
  52. }
  53. initReadme() {
  54. return this.backendSrv.get(`/api/plugins/${this.pluginId}/readme`).then(res => {
  55. return System.import('remarkable').then(Remarkable => {
  56. var md = new Remarkable();
  57. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  58. });
  59. });
  60. }
  61. getPluginIcon(type) {
  62. switch (type) {
  63. case 'datasource': return 'icon-gf icon-gf-datasources';
  64. case 'panel': return 'icon-gf icon-gf-panel';
  65. case 'app': return 'icon-gf icon-gf-apps';
  66. case 'page': return 'icon-gf icon-gf-endpoint-tiny';
  67. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  68. }
  69. }
  70. update() {
  71. this.preUpdateHook().then(() => {
  72. var updateCmd = _.extend({
  73. enabled: this.model.enabled,
  74. pinned: this.model.pinned,
  75. jsonData: this.model.jsonData,
  76. secureJsonData: this.model.secureJsonData,
  77. }, {});
  78. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  79. })
  80. .then(this.postUpdateHook)
  81. .then((res) => {
  82. window.location.href = window.location.href;
  83. });
  84. }
  85. importDashboards() {
  86. // move to dashboards tab
  87. this.tabIndex = 2;
  88. return new Promise((resolve) => {
  89. if (!this.$scope.$$phase) {
  90. this.$scope.$digest();
  91. }
  92. // let angular load dashboards tab
  93. setTimeout(() => {
  94. resolve();
  95. }, 1000);
  96. }).then(() => {
  97. return new Promise((resolve, reject) => {
  98. // send event to import list component
  99. appEvents.emit('dashboard-list-import-all', {
  100. resolve: resolve,
  101. reject: reject
  102. });
  103. });
  104. });
  105. }
  106. setPreUpdateHook(callback: () => any) {
  107. this.preUpdateHook = callback;
  108. }
  109. setPostUpdateHook(callback: () => any) {
  110. this.postUpdateHook = callback;
  111. }
  112. updateAvailable() {
  113. var modalScope = this.$scope.$new(true);
  114. modalScope.plugin = this.model;
  115. this.$rootScope.appEvent('show-modal', {
  116. src: 'public/app/features/plugins/partials/update_instructions.html',
  117. scope: modalScope
  118. });
  119. }
  120. enable() {
  121. this.model.enabled = true;
  122. this.model.pinned = true;
  123. this.update();
  124. }
  125. disable() {
  126. this.model.enabled = false;
  127. this.model.pinned = false;
  128. this.update();
  129. }
  130. }
  131. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);