plugin_edit_ctrl.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 backendSrv,
  20. private $routeParams,
  21. private $sce,
  22. private $http) {
  23. this.model = {};
  24. this.pluginId = $routeParams.pluginId;
  25. this.tabIndex = 0;
  26. this.tabs = ['Overview'];
  27. this.preUpdateHook = () => Promise.resolve();
  28. this.postUpdateHook = () => Promise.resolve();
  29. }
  30. init() {
  31. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(result => {
  32. this.model = result;
  33. this.pluginIcon = this.getPluginIcon(this.model.type);
  34. this.model.dependencies.plugins.forEach(plug => {
  35. plug.icon = this.getPluginIcon(plug.type);
  36. });
  37. this.includes = _.map(result.includes, plug => {
  38. plug.icon = this.getPluginIcon(plug.type);
  39. return plug;
  40. });
  41. if (this.model.type === 'app') {
  42. this.tabs.push('Config');
  43. this.hasDashboards = _.findWhere(result.includes, {type: 'dashboard'});
  44. if (this.hasDashboards) {
  45. this.tabs.push('Dashboards');
  46. }
  47. }
  48. return this.initReadme();
  49. });
  50. }
  51. initReadme() {
  52. return this.backendSrv.get(`/api/plugins/${this.pluginId}/readme`).then(res => {
  53. return System.import('remarkable').then(Remarkable => {
  54. var md = new Remarkable();
  55. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  56. });
  57. });
  58. }
  59. getPluginIcon(type) {
  60. switch (type) {
  61. case 'datasource': return 'icon-gf icon-gf-datasources';
  62. case 'panel': return 'icon-gf icon-gf-panel';
  63. case 'app': return 'icon-gf icon-gf-apps';
  64. case 'page': return 'icon-gf icon-gf-endpoint-tiny';
  65. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  66. }
  67. }
  68. update() {
  69. this.preUpdateHook().then(() => {
  70. var updateCmd = _.extend({
  71. enabled: this.model.enabled,
  72. pinned: this.model.pinned,
  73. jsonData: this.model.jsonData,
  74. secureJsonData: this.model.secureJsonData,
  75. }, {});
  76. return this.backendSrv.post(`/api/plugins/${this.pluginId}/settings`, updateCmd);
  77. })
  78. .then(this.postUpdateHook)
  79. .then((res) => {
  80. window.location.href = window.location.href;
  81. });
  82. }
  83. importDashboards() {
  84. // move to dashboards tab
  85. this.tabIndex = 2;
  86. return new Promise((resolve) => {
  87. if (!this.$scope.$$phase) {
  88. this.$scope.$digest();
  89. }
  90. // let angular load dashboards tab
  91. setTimeout(() => {
  92. resolve();
  93. }, 1000);
  94. }).then(() => {
  95. return new Promise((resolve, reject) => {
  96. // send event to import list component
  97. appEvents.emit('dashboard-list-import-all', {
  98. resolve: resolve,
  99. reject: reject
  100. });
  101. });
  102. });
  103. }
  104. setPreUpdateHook(callback: () => any) {
  105. this.preUpdateHook = callback;
  106. }
  107. setPostUpdateHook(callback: () => any) {
  108. this.postUpdateHook = callback;
  109. }
  110. enable() {
  111. this.model.enabled = true;
  112. this.model.pinned = true;
  113. this.update();
  114. }
  115. disable() {
  116. this.model.enabled = false;
  117. this.model.pinned = false;
  118. this.update();
  119. }
  120. }
  121. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);