Skip to content

Instantly share code, notes, and snippets.

@andyferra
Created July 22, 2010 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andyferra/486459 to your computer and use it in GitHub Desktop.
Save andyferra/486459 to your computer and use it in GitHub Desktop.
var AppState = Class.create({
PORTRAIT: 0,
LANDSCAPE: 1,
orientation: -1,
initialize: function() {
this.orientation = this.PORTRAIT; // default for desktop browser
this.setUpOrientationListener();
},
setUpOrientationListener : function() {
// add listener to window if it's orientation-capable
if( window.orientation !== undefined )
{
var self = this; // handles scope
window.onorientationchange = function (event)
{
if ( Math.abs( window.orientation ) % 180 == 90 )
{
self.orientation = self.LANDSCAPE;
}
else
{
self.orientation = self.PORTRAIT;
}
// send out custom event
var containerNode = $$('body');
containerNode[0].fire("app:orientationchange", { orientation: self.orientation });
}
// make sure local flag is set right away
window.onorientationchange(null);
}
}
});
/*
// example code for listening to custom event that fires on orientation change
document.observe("app:orientationchange", function(event) {
console.log( "Tag " + event.target.tagName + " with id of " + event.target.id + " says the orientation is now " + event.memo.orientation + ".");
});
*/
/*
// class initialization
var app_state = new AppState(),
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment