misc.js 4.8 KB

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