plugin_srv.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. define([
  2. 'angular',
  3. 'lodash',
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.services');
  8. module.service('pluginSrv', function($rootScope, $timeout, $q, backendSrv) {
  9. var self = this;
  10. this.init = function() {
  11. console.log("pluginSrv init");
  12. this.plugins = {};
  13. };
  14. this.get = function(type) {
  15. return $q(function(resolve) {
  16. if (type in self.plugins) {
  17. return resolve(self.plugins[type]);
  18. }
  19. backendSrv.get('/api/plugins').then(function(results) {
  20. _.forEach(results, function(p) {
  21. self.plugins[p.type] = p;
  22. });
  23. return resolve(self.plugins[type]);
  24. });
  25. });
  26. };
  27. this.getAll = function() {
  28. return $q(function(resolve) {
  29. if (!_.isEmpty(self.plugins)) {
  30. return resolve(self.plugins);
  31. }
  32. backendSrv.get('api/plugins').then(function(results) {
  33. _.forEach(results, function(p) {
  34. self.plugins[p.type] = p;
  35. });
  36. return resolve(self.plugins);
  37. });
  38. });
  39. };
  40. this.update = function(plugin) {
  41. return $q(function(resolve, reject) {
  42. backendSrv.post('/api/plugins', plugin).then(function(resp) {
  43. self.plugins[plugin.type] = plugin;
  44. resolve(resp);
  45. }, function(resp) {
  46. reject(resp);
  47. });
  48. });
  49. };
  50. this.init();
  51. });
  52. });