misc.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. kbn = kbn.default;
  10. coreModule.default.directive('tip', function($compile) {
  11. return {
  12. restrict: 'E',
  13. link: function(scope, elem, attrs) {
  14. var _t = '<i class="grafana-tip fa fa-'+(attrs.icon||'question-circle')+'" bs-tooltip="\''+
  15. kbn.addslashes(elem.text())+'\'"></i>';
  16. _t = _t.replace(/{/g, '\\{').replace(/}/g, '\\}');
  17. elem.replaceWith($compile(angular.element(_t))(scope));
  18. }
  19. };
  20. });
  21. coreModule.default.directive('clipboardButton', function() {
  22. return {
  23. scope: {
  24. getText: '&clipboardButton'
  25. },
  26. link: function(scope, elem) {
  27. require(['clipboard'], function(Clipboard) {
  28. scope.clipboard = new Clipboard(elem[0], {
  29. text: function() {
  30. return scope.getText();
  31. }
  32. });
  33. });
  34. scope.$on('$destroy', function() {
  35. if (scope.clipboard) {
  36. scope.clipboard.destroy();
  37. }
  38. });
  39. }
  40. };
  41. });
  42. coreModule.default.directive('compile', function($compile) {
  43. return {
  44. restrict: 'A',
  45. link: function(scope, element, attrs) {
  46. scope.$watch(function(scope) {
  47. return scope.$eval(attrs.compile);
  48. }, function(value) {
  49. element.html(value);
  50. $compile(element.contents())(scope);
  51. });
  52. }
  53. };
  54. });
  55. coreModule.default.directive('watchChange', function() {
  56. return {
  57. scope: { onchange: '&watchChange' },
  58. link: function(scope, element) {
  59. element.on('input', function() {
  60. scope.$apply(function () {
  61. scope.onchange({ inputValue: element.val() });
  62. });
  63. });
  64. }
  65. };
  66. });
  67. coreModule.default.directive('editorOptBool', function($compile) {
  68. return {
  69. restrict: 'E',
  70. link: function(scope, elem, attrs) {
  71. var ngchange = attrs.change ? (' ng-change="' + attrs.change + '"') : '';
  72. var tip = attrs.tip ? (' <tip>' + attrs.tip + '</tip>') : '';
  73. var showIf = attrs.showIf ? (' ng-show="' + attrs.showIf + '" ') : '';
  74. var template = '<div class="editor-option gf-form-checkbox text-center"' + showIf + '>' +
  75. ' <label for="' + attrs.model + '" class="small">' +
  76. attrs.text + tip + '</label>' +
  77. '<input class="cr1" id="' + attrs.model + '" type="checkbox" ' +
  78. ' ng-model="' + attrs.model + '"' + ngchange +
  79. ' ng-checked="' + attrs.model + '"></input>' +
  80. ' <label for="' + attrs.model + '" class="cr1"></label>';
  81. elem.replaceWith($compile(angular.element(template))(scope));
  82. }
  83. };
  84. });
  85. coreModule.default.directive('editorCheckbox', function($compile, $interpolate) {
  86. return {
  87. restrict: 'E',
  88. link: function(scope, elem, attrs) {
  89. var text = $interpolate(attrs.text)(scope);
  90. var model = $interpolate(attrs.model)(scope);
  91. var ngchange = attrs.change ? (' ng-change="' + attrs.change + '"') : '';
  92. var tip = attrs.tip ? (' <tip>' + attrs.tip + '</tip>') : '';
  93. var label = '<label for="' + scope.$id + model + '" class="checkbox-label">' +
  94. text + tip + '</label>';
  95. var template =
  96. '<input class="cr1" id="' + scope.$id + model + '" type="checkbox" ' +
  97. ' ng-model="' + model + '"' + ngchange +
  98. ' ng-checked="' + model + '"></input>' +
  99. ' <label for="' + scope.$id + model + '" class="cr1"></label>';
  100. template = template + label;
  101. elem.addClass('gf-form-checkbox');
  102. elem.html($compile(angular.element(template))(scope));
  103. }
  104. };
  105. });
  106. coreModule.default.directive('gfDropdown', function ($parse, $compile, $timeout) {
  107. function buildTemplate(items, placement) {
  108. var upclass = placement === 'top' ? 'dropup' : '';
  109. var ul = [
  110. '<ul class="dropdown-menu ' + upclass + '" role="menu" aria-labelledby="drop1">',
  111. '</ul>'
  112. ];
  113. angular.forEach(items, function (item, index) {
  114. if (item.divider) {
  115. return ul.splice(index + 1, 0, '<li class="divider"></li>');
  116. }
  117. var li = '<li' + (item.submenu && item.submenu.length ? ' class="dropdown-submenu"' : '') + '>' +
  118. '<a tabindex="-1" ng-href="' + (item.href || '') + '"' + (item.click ? ' ng-click="' + item.click + '"' : '') +
  119. (item.target ? ' target="' + item.target + '"' : '') + (item.method ? ' data-method="' + item.method + '"' : '') +
  120. '>' + (item.text || '') + '</a>';
  121. if (item.submenu && item.submenu.length) {
  122. li += buildTemplate(item.submenu).join('\n');
  123. }
  124. li += '</li>';
  125. ul.splice(index + 1, 0, li);
  126. });
  127. return ul;
  128. }
  129. return {
  130. restrict: 'EA',
  131. scope: true,
  132. link: function postLink(scope, iElement, iAttrs) {
  133. var getter = $parse(iAttrs.gfDropdown), items = getter(scope);
  134. $timeout(function () {
  135. var placement = iElement.data('placement');
  136. var dropdown = angular.element(buildTemplate(items, placement).join(''));
  137. dropdown.insertAfter(iElement);
  138. $compile(iElement.next('ul.dropdown-menu'))(scope);
  139. });
  140. iElement.addClass('dropdown-toggle').attr('data-toggle', 'dropdown');
  141. }
  142. };
  143. });
  144. });