misc.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import angular from 'angular';
  2. import Clipboard from 'clipboard';
  3. import coreModule from '../core_module';
  4. import kbn from 'app/core/utils/kbn';
  5. /** @ngInject */
  6. function tip($compile) {
  7. return {
  8. restrict: 'E',
  9. link: function(scope, elem, attrs) {
  10. var _t =
  11. '<i class="grafana-tip fa fa-' +
  12. (attrs.icon || 'question-circle') +
  13. '" bs-tooltip="\'' +
  14. kbn.addslashes(elem.text()) +
  15. '\'"></i>';
  16. _t = _t.replace(/{/g, '\\{').replace(/}/g, '\\}');
  17. elem.replaceWith($compile(angular.element(_t))(scope));
  18. },
  19. };
  20. }
  21. function clipboardButton() {
  22. return {
  23. scope: {
  24. getText: '&clipboardButton',
  25. },
  26. link: function(scope, elem) {
  27. scope.clipboard = new Clipboard(elem[0], {
  28. text: function() {
  29. return scope.getText();
  30. },
  31. });
  32. scope.$on('$destroy', function() {
  33. if (scope.clipboard) {
  34. scope.clipboard.destroy();
  35. }
  36. });
  37. },
  38. };
  39. }
  40. /** @ngInject */
  41. function compile($compile) {
  42. return {
  43. restrict: 'A',
  44. link: function(scope, element, attrs) {
  45. scope.$watch(
  46. function(scope) {
  47. return scope.$eval(attrs.compile);
  48. },
  49. function(value) {
  50. element.html(value);
  51. $compile(element.contents())(scope);
  52. }
  53. );
  54. },
  55. };
  56. }
  57. function watchChange() {
  58. return {
  59. scope: { onchange: '&watchChange' },
  60. link: function(scope, element) {
  61. element.on('input', function() {
  62. scope.$apply(function() {
  63. scope.onchange({ inputValue: element.val() });
  64. });
  65. });
  66. },
  67. };
  68. }
  69. /** @ngInject */
  70. function editorOptBool($compile) {
  71. return {
  72. restrict: 'E',
  73. link: function(scope, elem, attrs) {
  74. var ngchange = attrs.change ? ' ng-change="' + attrs.change + '"' : '';
  75. var tip = attrs.tip ? ' <tip>' + attrs.tip + '</tip>' : '';
  76. var showIf = attrs.showIf ? ' ng-show="' + attrs.showIf + '" ' : '';
  77. var template =
  78. '<div class="editor-option gf-form-checkbox text-center"' +
  79. showIf +
  80. '>' +
  81. ' <label for="' +
  82. attrs.model +
  83. '" class="small">' +
  84. attrs.text +
  85. tip +
  86. '</label>' +
  87. '<input class="cr1" id="' +
  88. attrs.model +
  89. '" type="checkbox" ' +
  90. ' ng-model="' +
  91. attrs.model +
  92. '"' +
  93. ngchange +
  94. ' ng-checked="' +
  95. attrs.model +
  96. '"></input>' +
  97. ' <label for="' +
  98. attrs.model +
  99. '" class="cr1"></label>';
  100. elem.replaceWith($compile(angular.element(template))(scope));
  101. },
  102. };
  103. }
  104. /** @ngInject */
  105. function editorCheckbox($compile, $interpolate) {
  106. return {
  107. restrict: 'E',
  108. link: function(scope, elem, attrs) {
  109. var text = $interpolate(attrs.text)(scope);
  110. var model = $interpolate(attrs.model)(scope);
  111. var ngchange = attrs.change ? ' ng-change="' + attrs.change + '"' : '';
  112. var tip = attrs.tip ? ' <tip>' + attrs.tip + '</tip>' : '';
  113. var label = '<label for="' + scope.$id + model + '" class="checkbox-label">' + text + tip + '</label>';
  114. var template =
  115. '<input class="cr1" id="' +
  116. scope.$id +
  117. model +
  118. '" type="checkbox" ' +
  119. ' ng-model="' +
  120. model +
  121. '"' +
  122. ngchange +
  123. ' ng-checked="' +
  124. model +
  125. '"></input>' +
  126. ' <label for="' +
  127. scope.$id +
  128. model +
  129. '" class="cr1"></label>';
  130. template = template + label;
  131. elem.addClass('gf-form-checkbox');
  132. elem.html($compile(angular.element(template))(scope));
  133. },
  134. };
  135. }
  136. /** @ngInject */
  137. function gfDropdown($parse, $compile, $timeout) {
  138. function buildTemplate(items, placement?) {
  139. var upclass = placement === 'top' ? 'dropup' : '';
  140. var ul = ['<ul class="dropdown-menu ' + upclass + '" role="menu" aria-labelledby="drop1">', '</ul>'];
  141. for (let index = 0; index < items.length; index++) {
  142. let item = items[index];
  143. if (item.divider) {
  144. ul.splice(index + 1, 0, '<li class="divider"></li>');
  145. continue;
  146. }
  147. var li =
  148. '<li' +
  149. (item.submenu && item.submenu.length ? ' class="dropdown-submenu"' : '') +
  150. '>' +
  151. '<a tabindex="-1" ng-href="' +
  152. (item.href || '') +
  153. '"' +
  154. (item.click ? ' ng-click="' + item.click + '"' : '') +
  155. (item.target ? ' target="' + item.target + '"' : '') +
  156. (item.method ? ' data-method="' + item.method + '"' : '') +
  157. '>' +
  158. (item.text || '') +
  159. '</a>';
  160. if (item.submenu && item.submenu.length) {
  161. li += buildTemplate(item.submenu).join('\n');
  162. }
  163. li += '</li>';
  164. ul.splice(index + 1, 0, li);
  165. }
  166. return ul;
  167. }
  168. return {
  169. restrict: 'EA',
  170. scope: true,
  171. link: function postLink(scope, iElement, iAttrs) {
  172. var getter = $parse(iAttrs.gfDropdown),
  173. items = getter(scope);
  174. $timeout(function() {
  175. var placement = iElement.data('placement');
  176. var dropdown = angular.element(buildTemplate(items, placement).join(''));
  177. dropdown.insertAfter(iElement);
  178. $compile(iElement.next('ul.dropdown-menu'))(scope);
  179. });
  180. iElement.addClass('dropdown-toggle').attr('data-toggle', 'dropdown');
  181. },
  182. };
  183. }
  184. coreModule.directive('tip', tip);
  185. coreModule.directive('clipboardButton', clipboardButton);
  186. coreModule.directive('compile', compile);
  187. coreModule.directive('watchChange', watchChange);
  188. coreModule.directive('editorOptBool', editorOptBool);
  189. coreModule.directive('editorCheckbox', editorCheckbox);
  190. coreModule.directive('gfDropdown', gfDropdown);