grafanaPanel.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'lodash',
  5. ],
  6. function (angular, $) {
  7. 'use strict';
  8. angular
  9. .module('grafana.directives')
  10. .directive('grafanaPanel', function($compile) {
  11. var container = '<div class="panel-container"></div>';
  12. var content = '<div class="panel-content"></div>';
  13. var panelHeader =
  14. '<div class="panel-header">'+
  15. '<div class="row-fluid panel-extra">' +
  16. '<div class="panel-extra-container">' +
  17. '<span class="alert-error panel-error small pointer"' +
  18. 'config-modal="app/partials/inspector.html" ng-show="panel.error" data-placement="right" bs-tooltip="panel.error">' +
  19. '<i class="icon-exclamation-sign"></i><span class="panel-error-arrow"></span>' +
  20. '</span>' +
  21. '<span class="panel-loading" ng-show="panelMeta.loading == true">' +
  22. '<i class="icon-spinner icon-spin icon-large"></i>' +
  23. '</span>' +
  24. '<span class="dropdown">' +
  25. '<span class="panel-text panel-title pointer" gf-dropdown="panelMeta.menu" tabindex="1" ' +
  26. 'data-drag=true data-jqyoui-options="kbnJqUiDraggableOptions"'+
  27. ' jqyoui-draggable="'+
  28. '{'+
  29. 'animate:false,'+
  30. 'mutate:false,'+
  31. 'index:{{$index}},'+
  32. 'onStart:\'panelMoveStart\','+
  33. 'onStop:\'panelMoveStop\''+
  34. '}" ng-model="row.panels" ' +
  35. '>' +
  36. '{{panel.title || "No title"}}' +
  37. '</span>' +
  38. '</span>'+
  39. '</div>'+
  40. '</div>\n'+
  41. '</div>';
  42. return {
  43. restrict: 'E',
  44. link: function($scope, elem, attr) {
  45. // once we have the template, scan it for controllers and
  46. // load the module.js if we have any
  47. var newScope = $scope.$new();
  48. $scope.kbnJqUiDraggableOptions = {
  49. revert: 'invalid',
  50. helper: function() {
  51. return $('<div style="width:200px;height:100px;background: rgba(100,100,100,0.50);"/>');
  52. },
  53. placeholder: 'keep'
  54. };
  55. // compile the module and uncloack. We're done
  56. function loadModule($module) {
  57. $module.appendTo(elem);
  58. elem.wrap(container);
  59. /* jshint indent:false */
  60. $compile(elem.contents())(newScope);
  61. elem.removeClass("ng-cloak");
  62. }
  63. newScope.$on('$destroy',function() {
  64. elem.unbind();
  65. elem.remove();
  66. });
  67. $scope.$watch(attr.type, function (name) {
  68. elem.addClass("ng-cloak");
  69. // load the panels module file, then render it in the dom.
  70. var nameAsPath = name.replace(".", "/");
  71. $scope.require([
  72. 'jquery',
  73. 'text!panels/'+nameAsPath+'/module.html'
  74. ], function ($, moduleTemplate) {
  75. var $module = $(moduleTemplate);
  76. // top level controllers
  77. var $controllers = $module.filter('ngcontroller, [ng-controller], .ng-controller');
  78. // add child controllers
  79. $controllers = $controllers.add($module.find('ngcontroller, [ng-controller], .ng-controller'));
  80. if ($controllers.length) {
  81. $controllers.first().prepend(panelHeader);
  82. $controllers.first().find('.panel-header').nextAll().wrapAll(content);
  83. $scope.require(['panels/' + nameAsPath + '/module'], function() {
  84. loadModule($module);
  85. });
  86. } else {
  87. loadModule($module);
  88. }
  89. });
  90. });
  91. }
  92. };
  93. });
  94. });