Skip to content

Instantly share code, notes, and snippets.

@cristianferrarig
Forked from mrrooijen/add-to-homescreen.js
Created November 30, 2013 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristianferrarig/7714484 to your computer and use it in GitHub Desktop.
Save cristianferrarig/7714484 to your computer and use it in GitHub Desktop.
/* Initialisation / Button events */
// If added to home screen...
if(navigator.standalone === undefined || !!navigator.standalone) {
// On Android scroll the screen by 1 pixel to hide the address bar
if (navigator.userAgent.match(/Android/i)) {
window.addEventListener("load", function() { window.scrollTo(0,1); }, false);
}
// Show the converter & hide the install option by default
$('#wrapper').style.display = 'block';
$('#install').style.display = 'none';
// Calculator button events
var buttons = $('#input-pad p');
for (var i = 0, ii = buttons.length; i < ii; i++) {
if (!!buttons[i].id.length) continue;
buttons[i].touch(function() { Calculator.add(this.innerText); });
}
$('#clear').touch(function(e) {
Calculator.clear();
});
$('#input').touch(function(e) {
addClass($('body'), 'edit-rates-from');
});
$('#output').touch(function(e) {
addClass($('body'), 'edit-rates-to');
});
// Flip currencies
$('#flip').touch(function(e) {
addClass($('body'), 'flip');
setTimeout(function() {
var last = { from: window.from_to['from'], to: window.from_to['to'] }
Converter.update_currency_display(last.to, last.from);
if (Calculator.input.innerHTML.length >= 9 || Calculator.output.innerHTML.length >= 9) {
Calculator.clear();
}
}, 130);
setTimeout(function() { removeClass($('body'), 'flip'); }, 275);
});
// Draw the currency list
Converter.draw_currencies();
// Currency selection events
var rates = $('#rate-selection a');
for (var i = 0, ii = rates.length; i < ii; i++) {
rates[i].touch(function(e) {
e.preventDefault();
var id = this.id.split('-');
args = id[0] == 'from' ? [id[1], null] : [null, id[1]];
Converter.update_currency_display.apply(Converter, args);
Calculator.clear();
removeClass($('body'), 'edit-rates-\\w+');
});
}
// Show the credits screen when in landscape orientation
var detectOrientation = function() {
if (window.orientation) addClass($('body'), 'credits');
else removeClass($('body'), 'credits');
}
detectOrientation();
window.addEventListener('orientationchange', detectOrientation);
// If working in offline mode, update the display to reflect this
if (!navigator.onLine) $('#network-status').className = 'offline';
// Update the currency display on load
Converter.update_currency_display();
// Delay making the ajax request to update the exchange rates
// This prevents the app locking up while the request is made
setTimeout(function() { Converter.update_rates(); }, 100);
// Update the app cache when ready
window.applicationCache.addEventListener('updateready', function(){
window.applicationCache.swapCache();
}, false);
// If opened in mobile safari directly,
} else {
// show the install screen
$('#wrapper').style.display = 'none';
$('#install').style.display = 'block';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment