misc.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ngchange = attrs.change ? (' ng-change="' + attrs.change + '"') : '';
  62. var tip = attrs.tip ? (' <tip>' + attrs.tip + '</tip>') : '';
  63. var label = '<label for="' + scope.$id + attrs.model + '" class="checkbox-label">' +
  64. text + tip + '</label>';
  65. var template = '<input class="cr1" id="' + scope.$id + attrs.model + '" type="checkbox" ' +
  66. ' ng-model="' + attrs.model + '"' + ngchange +
  67. ' ng-checked="' + attrs.model + '"></input>' +
  68. ' <label for="' + scope.$id + attrs.model + '" class="cr1"></label>';
  69. template = label + template;
  70. elem.replaceWith($compile(angular.element(template))(scope));
  71. }
  72. };
  73. });
  74. angular
  75. .module('grafana.directives')
  76. .directive('gfDropdown', function ($parse, $compile, $timeout) {
  77. function buildTemplate(items, placement) {
  78. var upclass = placement === 'top' ? 'dropup' : '';
  79. var ul = [
  80. '<ul class="dropdown-menu ' + upclass + '" role="menu" aria-labelledby="drop1">',
  81. '</ul>'
  82. ];
  83. angular.forEach(items, function (item, index) {
  84. if (item.divider) {
  85. return ul.splice(index + 1, 0, '<li class="divider"></li>');
  86. }
  87. var li = '<li' + (item.submenu && item.submenu.length ? ' class="dropdown-submenu"' : '') + '>' +
  88. '<a tabindex="-1" ng-href="' + (item.href || '') + '"' + (item.click ? ' ng-click="' + item.click + '"' : '') +
  89. (item.target ? ' target="' + item.target + '"' : '') + (item.method ? ' data-method="' + item.method + '"' : '') +
  90. (item.configModal ? ' dash-editor-link="' + item.configModal + '"' : "") +
  91. '>' + (item.text || '') + '</a>';
  92. if (item.submenu && item.submenu.length) {
  93. li += buildTemplate(item.submenu).join('\n');
  94. }
  95. li += '</li>';
  96. ul.splice(index + 1, 0, li);
  97. });
  98. return ul;
  99. }
  100. return {
  101. restrict: 'EA',
  102. scope: true,
  103. link: function postLink(scope, iElement, iAttrs) {
  104. var getter = $parse(iAttrs.gfDropdown), items = getter(scope);
  105. $timeout(function () {
  106. var placement = iElement.data('placement');
  107. var dropdown = angular.element(buildTemplate(items, placement).join(''));
  108. dropdown.insertAfter(iElement);
  109. $compile(iElement.next('ul.dropdown-menu'))(scope);
  110. });
  111. iElement.addClass('dropdown-toggle').attr('data-toggle', 'dropdown');
  112. }
  113. };
  114. });
  115. });