Skip to content

Commit

Permalink
Move JS into head for faster previewing
Browse files Browse the repository at this point in the history
  • Loading branch information
adamschwartz committed Jul 31, 2015
1 parent 1750c1d commit b9a20d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 12 additions & 8 deletions eager-lead-box.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function(){
if (!document.body.addEventListener || !document.body.setAttribute || !document.body.querySelector || !document.body.classList || !document.body.classList.add) {
if (!window.addEventListener || !document.documentElement.setAttribute || !document.querySelector || !document.documentElement.classList || !document.documentElement.classList.add) {
return
}

Expand All @@ -17,9 +17,9 @@
' box-shadow: 0 0 .0625em ' + options.color + ' !important' +
' }' +
'';
document.body.appendChild(style);

el = document.createElement('eager-lead-box');
el.addEventListener('touchstart', function(){}, false); // iOS :hover CSS hack
el.className = 'eager-lead-box';
el.innerHTML = '' +
' <div class="eager-lead-box-close-button"></div>' +
Expand Down Expand Up @@ -101,7 +101,6 @@
xhr.send(params);
});
}
document.body.appendChild(el);

show = function() {
el.classList.add('eager-lead-box-show');
Expand All @@ -112,22 +111,27 @@
el.classList.add('eager-lead-box-hide');
el.classList.remove('eager-lead-box-show');
};
el.querySelector('.eager-lead-box-close-button').addEventListener('click', hide);

checkScroll = function() {
if (!document.body) {
return;
}

if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
show();
window.removeEventListener('scroll', checkScroll);
}
};

window.addEventListener('scroll', checkScroll);
el.querySelector('.eager-lead-box-close-button').addEventListener('click', hide);

isPreview = window.Eager && window.Eager.installs && window.Eager.installs.preview && window.Eager.installs.preview.appId === 'IgyOK_i5Ib3E';
if (isPreview) {
setTimeout(show, 500);
show();
}

// iOS :hover CSS hack
el.addEventListener('touchstart', function(){}, false);
document.addEventListener('DOMContentLoaded', function(){
document.body.appendChild(style);
document.body.appendChild(el);
});
})();
4 changes: 1 addition & 3 deletions install.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
{
"type": "style",
"src": "eager-lead-box.css"
}
],
"body": [
},
{
"type": "script",
"src": "eager-lead-box.js"
Expand Down

0 comments on commit b9a20d6

Please sign in to comment.