Skip to content

Instantly share code, notes, and snippets.

@trevorsheridan
Created March 7, 2012 20:12
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 trevorsheridan/1995716 to your computer and use it in GitHub Desktop.
Save trevorsheridan/1995716 to your computer and use it in GitHub Desktop.
(function() {
var Detective = window.Detective = Backbone.Base.extend({
initialize: function() {
snack.publisher(this);
this.ua = window.navigator.userAgent;
return this;
},
watch: function() {
var orientationevent = 'onorientationchange' in window ? 'orientationchange' : 'resize';
window.addEventListener(orientationevent, snack.bind(function(e) {
this.publish('orientation', [this.orientation()]);
}, this));
return this;
},
orientation: function() {
if ('onorientationchange' in window)
return window.orientation & 2 ? 'landscape' : 'portrait';
else
return window.innerWidth / window.innerHeight << 0 ? 'landscape' : 'portrait';
},
device: function() {
var processed = /(iPad.*?|iPhone.*?|iPod.*?)?;.*(CPU\s.*\d?|Android\s.*\d|MSIE.*?|BlackBerry\s.*\d?)(OS X\)|;)/i.exec(this.ua);
var device = '';
processed = processed ? processed : device;
if (typeof processed === 'object' && typeof processed[2] === 'string') {
device = processed[2];
if (/iPad.*?|iPhone.*?|iPod.*?/i.test(processed[1]))
device = processed[1] + ' ' + /(OS.*\d)/.exec(processed[2])[1];
}
return device.replace(/\s/g, '-');
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment