plugin_edit_ctrl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. export class PluginEditCtrl {
  5. model: any;
  6. pluginIcon: string;
  7. pluginId: any;
  8. includes: any;
  9. readmeHtml: any;
  10. includedDatasources: any;
  11. tabIndex: number;
  12. tabs: any;
  13. hasDashboards: any;
  14. preUpdateHook: () => any;
  15. postUpdateHook: () => any;
  16. /** @ngInject */
  17. constructor(private backendSrv, private $routeParams, private $sce, private $http) {
  18. this.model = {};
  19. this.pluginId = $routeParams.pluginId;
  20. this.tabIndex = 0;
  21. this.tabs = ['Overview'];
  22. }
  23. init() {
  24. return this.backendSrv.get(`/api/plugins/${this.pluginId}/settings`).then(result => {
  25. this.model = result;
  26. this.pluginIcon = this.getPluginIcon(this.model.type);
  27. this.model.dependencies.plugins.forEach(plug => {
  28. plug.icon = this.getPluginIcon(plug.type);
  29. });
  30. this.includes = _.map(result.includes, plug => {
  31. plug.icon = this.getPluginIcon(plug.type);
  32. return plug;
  33. });
  34. if (this.model.type === 'app') {
  35. this.tabs.push('Config');
  36. this.hasDashboards = _.findWhere(result.includes, {type: 'dashboard'});
  37. if (this.hasDashboards) {
  38. this.tabs.push('Dashboards');
  39. }
  40. }
  41. return this.initReadme();
  42. });
  43. }
  44. initReadme() {
  45. return this.backendSrv.get(`/api/plugins/${this.pluginId}/readme`).then(res => {
  46. return System.import('remarkable').then(Remarkable => {
  47. var md = new Remarkable();
  48. this.readmeHtml = this.$sce.trustAsHtml(md.render(res));
  49. });
  50. });
  51. }
  52. getPluginIcon(type) {
  53. switch (type) {
  54. case 'datasource': return 'icon-gf icon-gf-datasources';
  55. case 'panel': return 'icon-gf icon-gf-panel';
  56. case 'app': return 'icon-gf icon-gf-apps';
  57. case 'page': return 'icon-gf icon-gf-share';
  58. case 'dashboard': return 'icon-gf icon-gf-dashboard';
  59. }
  60. }
  61. update() {
  62. var chain = Promise.resolve();
  63. var self = this;
  64. // if set, handle the preUpdateHook. If this returns a promise,
  65. // the next step of execution will block until the promise resolves.
  66. // if the promise is rejected, this update will be aborted.
  67. if (this.preUpdateHook != null) {
  68. chain = self.preUpdateHook();
  69. }
  70. // Perform the core update procedure
  71. chain = chain.then(function() {
  72. var updateCmd = _.extend({
  73. enabled: self.model.enabled,
  74. pinned: self.model.pinned,
  75. jsonData: self.model.jsonData,
  76. secureJsonData: self.model.secureJsonData,
  77. }, {});
  78. return self.backendSrv.post(`/api/plugins/${self.pluginId}/settings`, updateCmd);
  79. });
  80. // if set, performt he postUpdate hook. If a promise is returned it will block
  81. // the final step of the update procedure (reloading the page) until the promise
  82. // resolves. If the promise is rejected the page will not be reloaded.
  83. if (this.postUpdateHook != null) {
  84. chain = chain.then(function() {
  85. return this.postUpdateHook();
  86. });
  87. }
  88. // all stesp in the update procedure are complete, so reload the page to make changes
  89. // take effect.
  90. chain.then(function() {
  91. window.location.href = window.location.href;
  92. });
  93. }
  94. setPreUpdateHook(callback: () => any) {
  95. this.preUpdateHook = callback;
  96. }
  97. setPostUpdateHook(callback: () => any) {
  98. this.postUpdateHook = callback;
  99. }
  100. enable() {
  101. this.model.enabled = true;
  102. this.model.pinned = true;
  103. this.update();
  104. }
  105. }
  106. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);