Created
August 10, 2012 14:44
-
-
Save pablolmiranda/3314726 to your computer and use it in GitHub Desktop.
Extending Modernizr to accept Mobile Detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Titans.common.Device = Titans.oop.extend( Titans.oop.BaseClass, { | |
elements : { | |
html : null, | |
window : null | |
}, | |
devices : [ | |
{'name' : 'mobile', 'width' : {'min' : 0, 'max' : 480}}, | |
{'name' : 'desktop', 'width' : {'min' : 480, 'max' : 10240}} | |
], | |
modernizr : null, | |
init : function() { | |
var self = this; | |
self.detect(); | |
if(YUI.Env.UA >= 9){ | |
Profile.Y.on('windowresize', function() { | |
self.detect(); | |
}); | |
} | |
self.orientation(); | |
self.elements.window.on('orientationchange', self.orientation, self); | |
}, | |
detect : function() { | |
var i, device, oldDevice; | |
oldDevice = this.modernizr.device; | |
for (i = 0; i < this.devices.length; i++) { | |
device = this.devices[i]; | |
this.testDevice(device.name, device.width.min, device.width.max); | |
} | |
if (oldDevice != this.modernizr.device) { | |
Profile.Y.fire('Device::detected', this.modernizr.device); | |
} | |
}, | |
testDevice : function(name, minWidth, maxWidth) { | |
var self = this; | |
self.forgetDevice(name); | |
self.modernizr.addTest(name, function(){ | |
if (self.windowWidth() > minWidth && | |
self.windowWidth() <= maxWidth) { | |
self.modernizr.device = name; | |
return true; | |
} | |
return false; | |
}); | |
}, | |
forgetDevice : function(name) { | |
this.elements.html.removeClass(name); | |
this.elements.html.removeClass('no-'+name); | |
delete this.modernizr[name]; | |
}, | |
windowWidth : function() { | |
return this.elements.window.get('innerWidth'); | |
}, | |
orientation : function(){ | |
if (typeof(this.elements.window.get('orientation')) == 'undefined') { | |
return; | |
} | |
var degrees = Math.abs(this.elements.window.get('orientation')); | |
if (degrees == 90) { | |
this.elements.html.removeClass('portrait').addClass('landscape'); | |
return; | |
} | |
this.elements.html.removeClass('landscape').addClass('portrait'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment