edit_ctrl.ts 3.3 KB

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