misc.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 =
  114. '<label for="' +
  115. scope.$id +
  116. model +
  117. '" class="checkbox-label">' +
  118. text +
  119. tip +
  120. '</label>';
  121. var template =
  122. '<input class="cr1" id="' +
  123. scope.$id +
  124. model +
  125. '" type="checkbox" ' +
  126. ' ng-model="' +
  127. model +
  128. '"' +
  129. ngchange +
  130. ' ng-checked="' +
  131. model +
  132. '"></input>' +
  133. ' <label for="' +
  134. scope.$id +
  135. model +
  136. '" class="cr1"></label>';
  137. template = template + label;
  138. elem.addClass('gf-form-checkbox');
  139. elem.html($compile(angular.element(template))(scope));
  140. },
  141. };
  142. }
  143. /** @ngInject */
  144. function gfDropdown($parse, $compile, $timeout) {
  145. function buildTemplate(items, placement?) {
  146. var upclass = placement === 'top' ? 'dropup' : '';
  147. var ul = [
  148. '<ul class="dropdown-menu ' +
  149. upclass +
  150. '" role="menu" aria-labelledby="drop1">',
  151. '</ul>',
  152. ];
  153. for (let index = 0; index < items.length; index++) {
  154. let item = items[index];
  155. if (item.divider) {
  156. ul.splice(index + 1, 0, '<li class="divider"></li>');
  157. continue;
  158. }
  159. var li =
  160. '<li' +
  161. (item.submenu && item.submenu.length
  162. ? ' class="dropdown-submenu"'
  163. : '') +
  164. '>' +
  165. '<a tabindex="-1" ng-href="' +
  166. (item.href || '') +
  167. '"' +
  168. (item.click ? ' ng-click="' + item.click + '"' : '') +
  169. (item.target ? ' target="' + item.target + '"' : '') +
  170. (item.method ? ' data-method="' + item.method + '"' : '') +
  171. '>' +
  172. (item.text || '') +
  173. '</a>';
  174. if (item.submenu && item.submenu.length) {
  175. li += buildTemplate(item.submenu).join('\n');
  176. }
  177. li += '</li>';
  178. ul.splice(index + 1, 0, li);
  179. }
  180. return ul;
  181. }
  182. return {
  183. restrict: 'EA',
  184. scope: true,
  185. link: function postLink(scope, iElement, iAttrs) {
  186. var getter = $parse(iAttrs.gfDropdown),
  187. items = getter(scope);
  188. $timeout(function() {
  189. var placement = iElement.data('placement');
  190. var dropdown = angular.element(
  191. buildTemplate(items, placement).join('')
  192. );
  193. dropdown.insertAfter(iElement);
  194. $compile(iElement.next('ul.dropdown-menu'))(scope);
  195. });
  196. iElement.addClass('dropdown-toggle').attr('data-toggle', 'dropdown');
  197. },
  198. };
  199. }
  200. coreModule.directive('tip', tip);
  201. coreModule.directive('clipboardButton', clipboardButton);
  202. coreModule.directive('compile', compile);
  203. coreModule.directive('watchChange', watchChange);
  204. coreModule.directive('editorOptBool', editorOptBool);
  205. coreModule.directive('editorCheckbox', editorCheckbox);
  206. coreModule.directive('gfDropdown', gfDropdown);