misc.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. define([
  2. 'angular',
  3. 'kbn'
  4. ],
  5. function (angular, kbn) {
  6. 'use strict';
  7. angular
  8. .module('grafana.directives')
  9. .directive('tip', function($compile) {
  10. return {
  11. restrict: 'E',
  12. link: function(scope, elem, attrs) {
  13. var _t = '<i class="grafana-tip fa fa-'+(attrs.icon||'question-circle')+'" bs-tooltip="\''+
  14. kbn.addslashes(elem.text())+'\'"></i>';
  15. elem.replaceWith($compile(angular.element(_t))(scope));
  16. }
  17. };
  18. });
  19. angular
  20. .module('grafana.directives')
  21. .directive('watchChange', function() {
  22. return {
  23. scope: { onchange: '&watchChange' },
  24. link: function(scope, element) {
  25. element.on('input', function() {
  26. scope.$apply(function () {
  27. scope.onchange({ inputValue: element.val() });
  28. });
  29. });
  30. }
  31. };
  32. });
  33. angular
  34. .module('grafana.directives')
  35. .directive('editorOptBool', function($compile) {
  36. return {
  37. restrict: 'E',
  38. link: function(scope, elem, attrs) {
  39. var ngchange = attrs.change ? (' ng-change="' + attrs.change + '"') : '';
  40. var tip = attrs.tip ? (' <tip>' + attrs.tip + '</tip>') : '';
  41. var showIf = attrs.showIf ? (' ng-show="' + attrs.showIf + '" ') : '';
  42. var template = '<div class="editor-option text-center"' + showIf + '>' +
  43. ' <label for="' + attrs.model + '" class="small">' +
  44. attrs.text + tip + '</label>' +
  45. '<input class="cr1" id="' + attrs.model + '" type="checkbox" ' +
  46. ' ng-model="' + attrs.model + '"' + ngchange +
  47. ' ng-checked="' + attrs.model + '"></input>' +
  48. ' <label for="' + attrs.model + '" class="cr1"></label>';
  49. elem.replaceWith($compile(angular.element(template))(scope));
  50. }
  51. };
  52. });
  53. angular
  54. .module('grafana.directives')
  55. .directive('editorCheckbox', function($compile, $interpolate) {
  56. return {
  57. restrict: 'E',
  58. link: function(scope, elem, attrs) {
  59. var text = $interpolate(attrs.text)(scope);
  60. var ngchange = attrs.change ? (' ng-change="' + attrs.change + '"') : '';
  61. var tip = attrs.tip ? (' <tip>' + attrs.tip + '</tip>') : '';
  62. var label = '<label for="' + scope.$id + attrs.model + '" class="checkbox-label">' +
  63. text + tip + '</label>';
  64. var template = '<input class="cr1" id="' + scope.$id + attrs.model + '" type="checkbox" ' +
  65. ' ng-model="' + attrs.model + '"' + ngchange +
  66. ' ng-checked="' + attrs.model + '"></input>' +
  67. ' <label for="' + scope.$id + attrs.model + '" class="cr1"></label>';
  68. template = label + template;
  69. elem.replaceWith($compile(angular.element(template))(scope));
  70. }
  71. };
  72. });
  73. angular
  74. .module('grafana.directives')
  75. .directive('gfDropdown', function ($parse, $compile, $timeout) {
  76. function buildTemplate(items, placement) {
  77. var upclass = placement === 'top' ? 'dropup' : '';
  78. var ul = [
  79. '<ul class="dropdown-menu ' + upclass + '" role="menu" aria-labelledby="drop1">',
  80. '</ul>'
  81. ];
  82. angular.forEach(items, function (item, index) {
  83. if (item.divider) {
  84. return ul.splice(index + 1, 0, '<li class="divider"></li>');
  85. }
  86. var li = '<li' + (item.submenu && item.submenu.length ? ' class="dropdown-submenu"' : '') + '>' +
  87. '<a tabindex="-1" ng-href="' + (item.href || '') + '"' + (item.click ? ' ng-click="' + item.click + '"' : '') +
  88. (item.target ? ' target="' + item.target + '"' : '') + (item.method ? ' data-method="' + item.method + '"' : '') +
  89. (item.configModal ? ' dash-editor-link="' + item.configModal + '"' : "") +
  90. '>' + (item.text || '') + '</a>';
  91. if (item.submenu && item.submenu.length) {
  92. li += buildTemplate(item.submenu).join('\n');
  93. }
  94. li += '</li>';
  95. ul.splice(index + 1, 0, li);
  96. });
  97. return ul;
  98. }
  99. return {
  100. restrict: 'EA',
  101. scope: true,
  102. link: function postLink(scope, iElement, iAttrs) {
  103. var getter = $parse(iAttrs.gfDropdown), items = getter(scope);
  104. $timeout(function () {
  105. var placement = iElement.data('placement');
  106. var dropdown = angular.element(buildTemplate(items, placement).join(''));
  107. dropdown.insertAfter(iElement);
  108. $compile(iElement.next('ul.dropdown-menu'))(scope);
  109. });
  110. iElement.addClass('dropdown-toggle').attr('data-toggle', 'dropdown');
  111. }
  112. };
  113. });
  114. });