Created
July 22, 2010 19:28
-
-
Save andyferra/486459 to your computer and use it in GitHub Desktop.
This file contains 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
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