forked from vdumitrescu/epoch-converter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselection.js
81 lines (60 loc) · 1.85 KB
/
selection.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
$(document).ready(function() {
$('body').append("<div id=\"ec-bubble\"><div id=\"ec-bubble-text\"></div><div id=\"ec-bubble-close\"></div></div>");
$('#ec-bubble-close').click(function() {
hideBubble();
});
$(document).dblclick(function(e) {
hideBubble();
processSelection(e);
});
$(document).click(function(e) {
hideBubble();
processSelection(e);
});
$(document).bind('mouseup', function(e) {
processSelection(e);
});
});
function processSelection(e) {
var text = getSelectedText();
if ($.isNumeric(text)) {
var humanReadableDate = convertTimestamp(text);
if (humanReadableDate != "")
showBubble(e, humanReadableDate);
}
}
function getSelectedText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function convertTimestamp(ts) {
var date = new Date(ts * 1000);
if (ts.length > 10) date = new Date(parseInt(ts));
var dateStr = "";
var d = date.getDate();
var m = date.getMonth()+1;
var y = date.getFullYear();
dateStr += (m<=9?'0'+m:m) + "/" + (d<=9?'0'+d:d) + "/" + y + " - ";
var h = date.getHours();
var mi = date.getMinutes();
var s = date.getSeconds();
dateStr += (h<=9?'0'+h:h) + ":" + (mi<=9?'0'+mi:mi) + ":" + (s<=9?'0'+s:s);
if (dateStr.indexOf("NaN") > -1)
return "";
return dateStr;
}
function showBubble(e, text) {
$('#ec-bubble').css('top', e.pageY + 20 + "px");
$('#ec-bubble').css('left', e.pageX - 85 + "px");
$('#ec-bubble-text').html(text);
$('#ec-bubble').css('visibility', 'visible');
}
function hideBubble() {
$('#ec-bubble').css('visibility', 'hidden');
$('#ec-bubble-text').html("");
}