edit_ctrl.ts 2.9 KB

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