Skip to content

Commit

Permalink
synced with mobile-boilerplate but removed the javascript ipad splash…
Browse files Browse the repository at this point in the history
… solution in favour of the media query solution
  • Loading branch information
Ley Missailidis committed Dec 9, 2011
1 parent 5dd77f2 commit c8e08d9
Showing 1 changed file with 122 additions and 23 deletions.
145 changes: 122 additions & 23 deletions assets/js/mylibs/helper.js 100755 → 100644
Expand Up @@ -12,7 +12,7 @@ MBP.viewportmeta = document.querySelector && document.querySelector('meta[name="
MBP.ua = navigator.userAgent;

MBP.scaleFix = function () {
if (MBP.viewportmeta && /iPhone|iPad/.test(MBP.ua) && !/Opera Mini/.test(MBP.ua)) {
if (MBP.viewportmeta && /iPhone|iPad|iPod/.test(MBP.ua) && !/Opera Mini/.test(MBP.ua)) {
MBP.viewportmeta.content = "width=device-width, minimum-scale=1.0, maximum-scale=1.0";
document.addEventListener("gesturestart", MBP.gestureStart, false);
}
Expand All @@ -22,29 +22,69 @@ MBP.gestureStart = function () {
};


// Hide URL Bar for iOS
// http://remysharp.com/2010/08/05/doing-it-right-skipping-the-iphone-url-bar/
// Hide URL Bar for iOS and Android by Scott Jehl
// https://gist.github.com/1183357

MBP.hideUrlBar = function () {
/iPhone/.test(MBP.ua) && !pageYOffset && !location.hash && setTimeout(function () {
window.scrollTo(0, 1);
}, 1000);
var win = window,
doc = win.document;

// If there's a hash, or addEventListener is undefined, stop here
if( !location.hash && win.addEventListener ){

//scroll to 1
window.scrollTo( 0, 1 );
var scrollTop = 1,

//reset to 0 on bodyready, if needed
bodycheck = setInterval(function(){
if( doc.body ){
clearInterval( bodycheck );
scrollTop = "scrollTop" in doc.body ? doc.body.scrollTop : 1;
win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
}
}, 15 );

win.addEventListener( "load", function(){
setTimeout(function(){
//reset to hide addr bar at onload
win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
}, 0);
}, false );
}
};


// Fast Buttons - read wiki below before using
// https://github.com/shichuan/mobile-html5-boilerplate/wiki/JavaScript-Helper

// https://github.com/h5bp/mobile-boilerplate/wiki/JavaScript-Helper
MBP.fastButton = function (element, handler) {
this.element = element;
this.handler = handler;
if (element.addEventListener) {
element.addEventListener('touchstart', this, false);
element.addEventListener('click', this, false);
}

addEvt(element, "touchstart", this, false);
addEvt(element, "click", this, false);

/*if (element.addEventListener) {
try {
element.addEventListener('touchstart', this, false);
element.addEventListener('click', this, false);
} catch (e) {
element.addEventListener('click', handler, false);
}
} else if ("attachEvent" in element) {
if(typeof this == "object" && this.handleEvent) {
element.attachEvent("onclick", function(){
// Bind fn as this
this.handleEvent.call(this);
});
} else {
element.attachEvent("onclick", handler);
}
}*/
};

MBP.fastButton.prototype.handleEvent = function(event) {
event = event || window.event;
switch (event.type) {
case 'touchstart': this.onTouchStart(event); break;
case 'touchmove': this.onTouchMove(event); break;
Expand All @@ -63,13 +103,15 @@ MBP.fastButton.prototype.onTouchStart = function(event) {
};

MBP.fastButton.prototype.onTouchMove = function(event) {
if(Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) {
if(Math.abs(event.touches[0].clientX - this.startX) > 10 ||
Math.abs(event.touches[0].clientY - this.startY) > 10 ) {
this.reset();
}
};

MBP.fastButton.prototype.onClick = function(event) {
event.stopPropagation();
event = event || window.event;
if (event.stopPropagation) { event.stopPropagation(); }
this.reset();
this.handler(event);
if(event.type == 'touchend') {
Expand All @@ -79,8 +121,8 @@ MBP.fastButton.prototype.onClick = function(event) {
};

MBP.fastButton.prototype.reset = function() {
this.element.removeEventListener('touchend', this, false);
document.body.removeEventListener('touchmove', this, false);
rmEvt(this.element, "touchend", this, false);
rmEvt(document.body, "touchmove", this, false);
this.element.style.backgroundColor = "";
};

Expand Down Expand Up @@ -108,14 +150,63 @@ if (document.addEventListener) {

MBP.coords = [];

// fn arg can be an object or a function, thanks to handleEvent
// read more about the explanation at: http://www.thecssninja.com/javascript/handleevent
function addEvt(el, evt, fn, bubble) {
if("addEventListener" in el) {
// BBOS6 doesn't support handleEvent, catch and polyfill
try {
el.addEventListener(evt, fn, bubble);
} catch(e) {
if(typeof fn == "object" && fn.handleEvent) {
el.addEventListener(evt, function(e){
// Bind fn as this and set first arg as event object
fn.handleEvent.call(fn,e);
}, bubble);
} else {
throw e;
}
}
} else if("attachEvent" in el) {
// check if the callback is an object and contains handleEvent
if(typeof fn == "object" && fn.handleEvent) {
el.attachEvent("on" + evt, function(){
// Bind fn as this
fn.handleEvent.call(fn);
});
} else {
el.attachEvent("on" + evt, fn);
}
}
}

// iOS Startup Image
// https://github.com/shichuan/mobile-html5-boilerplate/issues#issue/2

MBP.splash = function () {
var filename = navigator.platform === 'iPad' ? 'h/' : 'l/';
document.write('<link rel="apple-touch-startup-image" href="/img/' + filename + 'splash.png" />' );
};
function rmEvt(el, evt, fn, bubble) {
if("removeEventListener" in el) {
// BBOS6 doesn't support handleEvent, catch and polyfill
try {
el.removeEventListener(evt, fn, bubble);
} catch(e) {
if(typeof fn == "object" && fn.handleEvent) {
el.removeEventListener(evt, function(e){
// Bind fn as this and set first arg as event object
fn.handleEvent.call(fn,e);
}, bubble);
} else {
throw e;
}
}
} else if("detachEvent" in el) {
// check if the callback is an object and contains handleEvent
if(typeof fn == "object" && fn.handleEvent) {
el.detachEvent("on" + evt, function(){
// Bind fn as this
fn.handleEvent.call(fn);
});
} else {
el.detachEvent("on" + evt, fn);
}
}
}


// Autogrow
Expand Down Expand Up @@ -145,3 +236,11 @@ MBP.autogrow = function (element, lh) {

})(document);


// Prevent iOS from zooming onfocus
// http://nerd.vasilis.nl/prevent-ios-from-zooming-onfocus/

var $viewportMeta = $('meta[name="viewport"]');
$('input, select, textarea').bind('focus blur', function(event) {
$viewportMeta.attr('content', 'width=device-width,initial-scale=1,maximum-scale=' + (event.type == 'blur' ? 10 : 1));
});

0 comments on commit c8e08d9

Please sign in to comment.