misc.js 4.3 KB

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