edit_ctrl.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. export class PluginEditCtrl {
  5. model: any;
  6. pluginId: any;
  7. includedPanels: any;
  8. includedDatasources: any;
  9. tabIndex: number;
  10. preUpdateHook: () => any;
  11. postUpdateHook: () => any;
  12. /** @ngInject */
  13. constructor(private backendSrv: any, private $routeParams: any) {
  14. this.model = {};
  15. this.pluginId = $routeParams.pluginId;
  16. this.tabIndex = 0;
  17. this.backendSrv.get(`/api/org/plugins/${this.pluginId}/settings`).then(result => {
  18. this.model = result;
  19. this.includedPanels = _.where(result.includes, {type: 'panel'});
  20. this.includedDatasources = _.where(result.includes, {type: 'datasource'});
  21. });
  22. }
  23. update() {
  24. var chain = Promise.resolve();
  25. var self = this;
  26. // if set, handle the preUpdateHook. If this returns a promise,
  27. // the next step of execution will block until the promise resolves.
  28. // if the promise is rejected, this update will be aborted.
  29. if (this.preUpdateHook != null) {
  30. chain = chain.then(function() {
  31. return Promise.resolve(self.preUpdateHook());
  32. });
  33. }
  34. // Perform the core update procedure
  35. chain = chain.then(function() {
  36. var updateCmd = _.extend({
  37. pluginId: self.model.pluginId,
  38. orgId: self.model.orgId,
  39. enabled: self.model.enabled,
  40. pinned: self.model.pinned,
  41. jsonData: self.model.jsonData,
  42. secureJsonData: self.model.secureJsonData,
  43. }, {});
  44. return self.backendSrv.post(`/api/org/plugins/${self.pluginId}/settings`, updateCmd);
  45. });
  46. // if set, performt he postUpdate hook. If a promise is returned it will block
  47. // the final step of the update procedure (reloading the page) until the promise
  48. // resolves. If the promise is rejected the page will not be reloaded.
  49. if (this.postUpdateHook != null) {
  50. chain = chain.then(function() {
  51. return Promise.resolve(this.postUpdateHook());
  52. });
  53. }
  54. // all stesp in the update procedure are complete, so reload the page to make changes
  55. // take effect.
  56. chain.then(function() {
  57. window.location.href = window.location.href;
  58. });
  59. }
  60. setPreUpdateHook(callback: () => any) {
  61. this.preUpdateHook = callback;
  62. }
  63. setPOstUpdateHook(callback: () => any) {
  64. this.postUpdateHook = callback;
  65. }
  66. toggleEnabled() {
  67. this.update();
  68. }
  69. togglePinned() {
  70. this.update();
  71. }
  72. }
  73. angular.module('grafana.controllers').controller('PluginEditCtrl', PluginEditCtrl);