plugin_edit_ctrl.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.tabIndex = 1;
  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-share';
  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. enable() {
  112. this.model.enabled = true;
  113. this.model.pinned = true;
  114. this.update();
  115. }
  116. disable() {
  117. this.model.enabled = false;
  118. this.model.pinned = false;
  119. this.update();
  120. }
  121. }
  122. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);