jquery_extended.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import $ from 'jquery';
  2. import angular from 'angular';
  3. import _ 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. $tooltip.css('left', x + opts.offset + width > $win.width() ? x - opts.offset - width : x + opts.offset);
  36. $tooltip.css('top', y + opts.offset + height > $win.height() ? y - opts.offset - height : y + opts.offset);
  37. });
  38. };
  39. })();