The-M-Project icon indicating copy to clipboard operation
The-M-Project copied to clipboard

Better orientation detection

Open rochejul opened this issue 13 years ago • 0 comments

Hi,

With tablet (I test on Android for the moment), orientation to 0 is equivalent to M.LANDSCAPE_LEFT, not M.PORTRAIT_TOP.

Here a patch for the M.Environment.getOrientation method:

M.Environment = M.Object.extend( { getOrientation: function() { if(window.orientation == null){ M.Logger.log('This device does not support orientation detection.', M.WARN); return NO; }

        var orientation;
        if(this.isDeviceTablet()){ // From issue 84
            switch(window.orientation){
                case 0:
                    orientation = M.LANDSCAPE_LEFT;
                    break;

                case 90:
                    orientation = M.PORTRAIT_BOTTOM;
                    break;

                case -90:
                    orientation = M.PORTRAIT_TOP;
                    break;

                case 180:
                    orientation = M.LANDSCAPE_RIGHT;
                    break;

                default:
                    M.Logger.log('This device does not support orientation detection.', M.WARN);
                    orientation = NO;
                    break;
            }

        } else {
            switch(window.orientation) {
                case 0:
                    orientation = M.PORTRAIT_TOP;
                case false:
                    orientation = M.PORTRAIT_BOTTOM;
                case 90:
                    orientation = M.LANDSCAPE_LEFT;
                case -90:
                    orientation = M.LANDSCAPE_RIGHT;
                default:
                    M.Logger.log('This device does not support orientation detection.', M.WARN);
                    orientation = NO;
            }
        }

        return orientation;
    }
}

);

Cheers

rochejul avatar Oct 03 '12 16:10 rochejul