jquery_extended.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import angular from 'angular';
  2. import $ from 'jquery';
  3. import { extend } from 'lodash';
  4. const $win = $(window);
  5. $.fn.place_tt = (() => {
  6. const defaults = {
  7. offset: 5,
  8. };
  9. return function (this: any, x: number, y: number, opts: any) {
  10. opts = $.extend(true, {}, defaults, opts);
  11. return this.each(() => {
  12. const $tooltip = $(this);
  13. let width, height;
  14. $tooltip.addClass('grafana-tooltip');
  15. $('#tooltip').remove();
  16. $tooltip.appendTo(document.body);
  17. if (opts.compile) {
  18. angular
  19. .element(document)
  20. .injector()
  21. .invoke([
  22. '$compile',
  23. '$rootScope',
  24. ($compile, $rootScope) => {
  25. const tmpScope = $rootScope.$new(true);
  26. extend(tmpScope, opts.scopeData);
  27. $compile($tooltip)(tmpScope);
  28. tmpScope.$digest();
  29. tmpScope.$destroy();
  30. },
  31. ]);
  32. }
  33. width = $tooltip.outerWidth(true)!;
  34. height = $tooltip.outerHeight(true)!;
  35. const left = x + opts.offset + width > $win.width()! ? x - opts.offset - width : x + opts.offset;
  36. const top = y + opts.offset + height > $win.height()! ? y - opts.offset - height : y + opts.offset;
  37. $tooltip.css('left', left > 0 ? left : 0);
  38. $tooltip.css('top', top > 0 ? top : 0);
  39. });
  40. };
  41. })();