-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooltip.model.js
50 lines (39 loc) · 1.1 KB
/
tooltip.model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(function (window) {
"use strict";
var tooltipControl = function () {
var maxw = 300;
var $div = null;
return {
show: function (tooltipText) {
this.hide();
var selection = window.getSelection();
if (selection.rangeCount == 0) {
return;
}
var text = selection.toString();
var range = selection.getRangeAt(0);
var $span = $("<span></span>");
var newRange = document.createRange();
newRange.setStart(selection.focusNode, range.endOffset);
newRange.insertNode($span[0]);
var x = $span.offset().left;
var y = $span.offset().top;
$div = $("<div style='background-color:black; z-index:9999;color:white; position:absolute; padding: 5px;'></div>").appendTo($("body"));
$div.text(tooltipText);
$div.css({ left: x, top: y + (range.getBoundingClientRect().height) });
$span.remove();
if ($div.offsetWidth > maxw) {
$div.style.width = maxw + 'px';
}
},
hide: function () {
if ($div) {
$div.fadeOut("fast");
$div.remove();
$div = null;
}
}
};
};
window.tooltip = new tooltipControl();
})(window);