1
0
Fork 0

General improvements and fixes

This commit is contained in:
Gregory Eremin 2014-07-05 15:06:56 +07:00
parent 086df7418e
commit bf5ded7569
1 changed files with 32 additions and 21 deletions

View File

@ -1,4 +1,6 @@
(function() { (function() {
log('Loaded');
var SHOW_TIMEOUT = 1000, var SHOW_TIMEOUT = 1000,
LOAD_TIMEOUT = SHOW_TIMEOUT / 4, LOAD_TIMEOUT = SHOW_TIMEOUT / 4,
STORAGE = {}; STORAGE = {};
@ -21,9 +23,8 @@
var el = links[i]; var el = links[i];
el.addEventListener('mouseover', function(e) { el.addEventListener('mouseover', function(e) {
log('The mouse is OVER!');
if (STORAGE[i].result === undefined) { if (STORAGE[i].result === undefined) {
log('Loading page in '+ LOAD_TIMEOUT +'ms');
STORAGE[i].load_timeout = after(LOAD_TIMEOUT, function() { STORAGE[i].load_timeout = after(LOAD_TIMEOUT, function() {
STORAGE[i].xhr = loadPageInfo(el.href, function(res) { STORAGE[i].xhr = loadPageInfo(el.href, function(res) {
log('Result loaded:', res); log('Result loaded:', res);
@ -36,6 +37,8 @@
} }
}); });
}); });
} else {
log('Page already loaded');
} }
STORAGE[i].show_timeout = after(SHOW_TIMEOUT, function() { STORAGE[i].show_timeout = after(SHOW_TIMEOUT, function() {
@ -46,9 +49,9 @@
}); });
}); });
el.addEventListener('mouseout', function(e) { var abort = function(e) {
log('Aborting');
hideTooltip(); hideTooltip();
log('The mouse is OUT!');
if (STORAGE[i].xhr !== undefined) { if (STORAGE[i].xhr !== undefined) {
var state = STORAGE[i].xhr.readyState, var state = STORAGE[i].xhr.readyState,
@ -59,9 +62,17 @@
log('XHR aborted!'); log('XHR aborted!');
} }
} }
window.clearTimeout(STORAGE[i].load_timeout); window.clearTimeout(STORAGE[i].load_timeout);
window.clearTimeout(STORAGE[i].show_timeout); window.clearTimeout(STORAGE[i].show_timeout);
});
delete STORAGE[i].xhr;
delete STORAGE[i].e;
delete STORAGE[i].load_timeout;
delete STORAGE[i].show_timeout;
}
el.addEventListener('mouseout', abort);
el.addEventListener('click', abort);
el.addEventListener('mousemove', function(e) { el.addEventListener('mousemove', function(e) {
STORAGE[i].e = e; STORAGE[i].e = e;
@ -70,43 +81,43 @@
} }
function showTooltip(el, info, e) { function showTooltip(el, info, e) {
var tt = document.getElementById('whats-there-tooltip'); var t = document.getElementById('whats-there-tooltip');
if (tt !== null) { if (t !== null) {
// Don't show a tooltip if there's one already log('Not showing tooltip, there is one already');
return; return;
} }
if (info.title === undefined && info.description === undefined) { if (info.title === undefined && info.description === undefined) {
// Don't show a tooltip if there's no title nor descriptions for the page log('Not showing tooltip, missing both title and description attributes');
return; return;
} }
var tt = document.createElement('div'), var t = document.createElement('div'),
body = document.getElementsByTagName('body')[0], body = document.getElementsByTagName('body')[0],
bounds = el.getBoundingClientRect(); bounds = el.getBoundingClientRect();
tt.setAttribute('class', 'whats-there-tooltip'); t.setAttribute('class', 'whats-there-tooltip');
tt.setAttribute('id', 'whats-there-tooltip'); t.setAttribute('id', 'whats-there-tooltip');
if (info.image_url !== undefined) { if (info.image_url !== undefined) {
var div = document.createElement('div'); var div = document.createElement('div');
div.setAttribute('class', 'whats-there-img'); div.setAttribute('class', 'whats-there-img');
div.style.backgroundImage = 'url(' + info.image_url + ')'; div.style.backgroundImage = 'url(' + info.image_url + ')';
tt.appendChild(div); t.appendChild(div);
} }
if (info.title !== undefined) { if (info.title !== undefined) {
var div = document.createElement('div'); var div = document.createElement('div');
div.setAttribute('class', 'whats-there-title'); div.setAttribute('class', 'whats-there-title');
div.innerText = info.title; div.innerText = info.title;
tt.appendChild(div); t.appendChild(div);
} }
if (info.description !== undefined) { if (info.description !== undefined) {
var div = document.createElement('div'); var div = document.createElement('div');
div.setAttribute('class', 'whats-there-description'); div.setAttribute('class', 'whats-there-description');
div.innerText = info.description; div.innerText = info.description;
tt.appendChild(div); t.appendChild(div);
} }
var site_name; var site_name;
@ -118,16 +129,16 @@
var div = document.createElement('div'); var div = document.createElement('div');
div.setAttribute('class', 'whats-there-site'); div.setAttribute('class', 'whats-there-site');
div.innerText = site_name; div.innerText = site_name;
tt.appendChild(div); t.appendChild(div);
body.appendChild(tt); body.appendChild(t);
moveTooltip(e); moveTooltip(e);
} }
function hideTooltip() { function hideTooltip() {
var tt = document.getElementById('whats-there-tooltip'); var t = document.getElementById('whats-there-tooltip');
if (tt !== null) { if (t !== null) {
tt.parentNode.removeChild(tt); t.parentNode.removeChild(t);
} }
} }
@ -193,7 +204,7 @@
var ct = this.getResponseHeader('Content-Type'); var ct = this.getResponseHeader('Content-Type');
// Aborting page load if it's not HTML // Aborting page load if it's not HTML
if (ct === null || ct.indexOf('text/html') != 0) { if (ct === null || ct.indexOf('text/html') !== 0) {
this.abort(); this.abort();
} }
} else if (this.readyState === this.DONE && this.status === 200) { } else if (this.readyState === this.DONE && this.status === 200) {