plugin_edit_ctrl.ts 3.2 KB

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