misc.js 4.8 KB

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