edit_ctrl.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/org/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. pluginId: self.model.pluginId,
  65. orgId: self.model.orgId,
  66. enabled: self.model.enabled,
  67. pinned: self.model.pinned,
  68. jsonData: self.model.jsonData,
  69. secureJsonData: self.model.secureJsonData,
  70. }, {});
  71. return self.backendSrv.post(`/api/org/plugins/${self.pluginId}/settings`, updateCmd);
  72. });
  73. // if set, performt he postUpdate hook. If a promise is returned it will block
  74. // the final step of the update procedure (reloading the page) until the promise
  75. // resolves. If the promise is rejected the page will not be reloaded.
  76. if (this.postUpdateHook != null) {
  77. chain = chain.then(function() {
  78. return Promise.resolve(this.postUpdateHook());
  79. });
  80. }
  81. // all stesp in the update procedure are complete, so reload the page to make changes
  82. // take effect.
  83. chain.then(function() {
  84. window.location.href = window.location.href;
  85. });
  86. }
  87. setPreUpdateHook(callback: () => any) {
  88. this.preUpdateHook = callback;
  89. }
  90. setPOstUpdateHook(callback: () => any) {
  91. this.postUpdateHook = callback;
  92. }
  93. toggleEnabled() {
  94. this.update();
  95. }
  96. togglePinned() {
  97. this.update();
  98. }
  99. }
  100. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);