misc.js 5.3 KB

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