grafanaSimplePanel.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. define([
  2. 'angular',
  3. ],
  4. function (angular) {
  5. 'use strict';
  6. angular
  7. .module('grafana.directives')
  8. .directive('grafanaSimplePanel', function($compile) {
  9. var panelLoading = '<span ng-show="panelMeta.loading == true">' +
  10. '<span style="font-size:72px;font-weight:200">'+
  11. '<i class="icon-spinner icon-spin"></i> loading ...' +
  12. '</span>'+
  13. '</span>';
  14. return {
  15. restrict: 'E',
  16. link: function($scope, elem, attr) {
  17. // once we have the template, scan it for controllers and
  18. // load the module.js if we have any
  19. // compile the module and uncloack. We're done
  20. function loadModule($module) {
  21. $module.appendTo(elem);
  22. /* jshint indent:false */
  23. $compile(elem.contents())($scope);
  24. elem.removeClass("ng-cloak");
  25. }
  26. function loadController(name) {
  27. elem.addClass("ng-cloak");
  28. // load the panels module file, then render it in the dom.
  29. var nameAsPath = name.replace(".", "/");
  30. $scope.require([
  31. 'jquery',
  32. 'text!panels/'+nameAsPath+'/module.html'
  33. ], function ($, moduleTemplate) {
  34. var $module = $(moduleTemplate);
  35. // top level controllers
  36. var $controllers = $module.filter('ngcontroller, [ng-controller], .ng-controller');
  37. // add child controllers
  38. $controllers = $controllers.add($module.find('ngcontroller, [ng-controller], .ng-controller'));
  39. if ($controllers.length) {
  40. $controllers.first().prepend(panelLoading);
  41. $scope.require([
  42. 'panels/'+nameAsPath+'/module'
  43. ], function() {
  44. loadModule($module);
  45. });
  46. } else {
  47. loadModule($module);
  48. }
  49. });
  50. }
  51. $scope.$watch(attr.type, function (name) {
  52. loadController(name);
  53. });
  54. }
  55. };
  56. });
  57. });