-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordLogger.js
59 lines (49 loc) · 1.96 KB
/
wordLogger.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
51
52
53
54
55
56
57
58
59
document.body.addEventListener('dblclick', function(event){
//show popover everywhere except input
if (isStaticPageText(event.target)){
let word = window.getSelection().toString();
if (word.length > 1){
let callback = function(translation){
showPopoverTranslation(word, translation);
}
memoTranslater.translate(word, "en", "ru", callback);
}
}
});
function showPopoverTranslation(word, translation){
let a = document.createElement("span");
a.className = "MemoPopoverSpan";
a.setAttribute('tabindex', "0");
a.setAttribute('data-container', "body");
a.setAttribute('data-template', memoPopoverTemplate);
a.setAttribute('data-toggle', "popover");
a.setAttribute('data-placement', "auto top");
a.setAttribute('data-trigger', "focus");
a.setAttribute('data-html', "true");
a.setAttribute('data-content', "<b>" + translation + "</b>");
let wordElem = window.getSelection();
let range = wordElem.getRangeAt(0).cloneRange();
range.surroundContents(a);
wordElem.removeAllRanges();
wordElem.addRange(range);
$('.MemoPopoverSpan').popover().on('shown.bs.popover', function (eventShown) {
let $popup = $('#' + $(eventShown.target).attr('aria-describedby'));
$popup.find('button.memo-popover-add').click(function (e) {
memoPopoverAddClick(word, translation);
});
});
setTimeout(function(){
$('.MemoPopoverSpan').popover("show");
}, 50);
}
// popover disabling (because focus doesn't work after "show")
$(document).on('click', function(e) {
$('.MemoPopoverSpan').each(function() {
//the 'is' for buttons that trigger popups
//the 'has' for icons within a button that triggers a popup
if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
$(this).popover('hide').data('bs.popover').inState.click = false // fix for BS 3.3.6
$(this).contents().unwrap();
}
});
});