1
0
mirror of https://github.com/danbee/cv synced 2025-03-04 08:59:12 +00:00
cv/public/js/mylibs/helper.js
2013-03-20 11:33:36 +00:00

122 lines
3.8 KiB
JavaScript

/*
* MBP - Mobile boilerplate helper functions
*/
window.MBP = window.MBP || {};
// Hide URL Bar for iOS
// http://remysharp.com/2010/08/05/doing-it-right-skipping-the-iphone-url-bar/
MBP.hideUrlBar = function () {
/mobile/i.test(navigator.userAgent) && !pageYOffset && !location.hash && setTimeout(function () {
window.scrollTo(0, 1);
}, 1000);
}
// Fast Buttons
// http://code.google.com/mobile/articles/fast_buttons.html
MBP.fastButton = function (element, handler) {
this.element = element;
this.handler = handler;
element.addEventListener('touchstart', this, false);
element.addEventListener('click', this, false);
};
MBP.fastButton.prototype.handleEvent = function(event) {
switch (event.type) {
case 'touchstart': this.onTouchStart(event); break;
case 'touchmove': this.onTouchMove(event); break;
case 'touchend': this.onClick(event); break;
case 'click': this.onClick(event); break;
}
};
MBP.fastButton.prototype.onTouchStart = function(event) {
event.stopPropagation();
this.element.addEventListener('touchend', this, false);
document.body.addEventListener('touchmove', this, false);
this.startX = event.touches[0].clientX;
this.startY = event.touches[0].clientY;
this.element.style.backgroundColor = "rgba(0,0,0,.7)";
};
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) {
this.reset();
}
};
MBP.fastButton.prototype.onClick = function(event) {
event.stopPropagation();
this.reset();
this.handler(event);
if(event.type == 'touchend') {
MBP.preventGhostClick(this.startX, this.startY);
}
this.element.style.backgroundColor = "";
};
MBP.fastButton.prototype.reset = function() {
this.element.removeEventListener('touchend', this, false);
document.body.removeEventListener('touchmove', this, false);
this.element.style.backgroundColor = "";
};
MBP.preventGhostClick = function (x, y) {
MBP.coords.push(x, y);
window.setTimeout(function (){
MBP.coords.splice(0, 2);
}, 2500);
};
;
MBP.ghostClickHandler = function (event) {
for(var i = 0, len = MBP.coords.length; i < len; i += 2) {
var x = MBP.coords[i];
var y = MBP.coords[i + 1];
if(Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) {
event.stopPropagation();
event.preventDefault();
}
}
};
document.addEventListener('click', MBP.ghostClickHandler, true);
MBP.coords = [];
// 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" />' );
}
// Autogrow
// http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series.html
MBP.autogrow = function (element, lh) {
function handler(e){
var newHeight = this.scrollHeight,
currentHeight = this.clientHeight;
if (newHeight > currentHeight) {
this.style.height = newHeight + 3 * textLineHeight + "px";
}
}
var setLineHeight = (lh) ? lh : 12,
textLineHeight = element.currentStyle ? element.currentStyle.lineHeight :
getComputedStyle(element, null).lineHeight;
textLineHeight = (textLineHeight.indexOf("px") == -1) ? setLineHeight :
parseInt(textLineHeight, 10);
element.style.overflow = "hidden";
element.addEventListener ? element.addEventListener('keyup', handler, false) :
element.attachEvent('onkeyup', handler);
}