123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
-
-
- import MicroEvent from './microevent';
-
- var create = function create() {
-
-
-
- var obj = {
-
-
-
-
-
-
-
-
-
-
-
-
-
- _isPortrait: false,
-
-
-
- isPortrait: function isPortrait() {
- return this._isPortrait();
- },
-
-
-
- isLandscape: function isLandscape() {
- return !this.isPortrait();
- },
-
- _init: function _init() {
- document.addEventListener('DOMContentLoaded', this._onDOMContentLoaded.bind(this), false);
-
- if ('orientation' in window) {
- window.addEventListener('orientationchange', this._onOrientationChange.bind(this), false);
- } else {
- window.addEventListener('resize', this._onResize.bind(this), false);
- }
-
- this._isPortrait = function () {
- return window.innerHeight > window.innerWidth;
- };
-
- return this;
- },
-
- _onDOMContentLoaded: function _onDOMContentLoaded() {
- this._installIsPortraitImplementation();
- this.emit('change', { isPortrait: this.isPortrait() });
- },
-
- _installIsPortraitImplementation: function _installIsPortraitImplementation() {
- var isPortrait = window.innerWidth < window.innerHeight;
-
- if (!('orientation' in window)) {
- this._isPortrait = function () {
- return window.innerHeight > window.innerWidth;
- };
- } else if (window.orientation % 180 === 0) {
- this._isPortrait = function () {
- return Math.abs(window.orientation % 180) === 0 ? isPortrait : !isPortrait;
- };
- } else {
- this._isPortrait = function () {
- return Math.abs(window.orientation % 180) === 90 ? isPortrait : !isPortrait;
- };
- }
- },
-
- _onOrientationChange: function _onOrientationChange() {
- var _this = this;
-
- var isPortrait = this._isPortrait();
-
-
-
- var nIter = 0;
- var interval = setInterval(function () {
- nIter++;
-
- var w = window.innerWidth;
- var h = window.innerHeight;
-
- if (isPortrait && w <= h || !isPortrait && w >= h) {
- _this.emit('change', { isPortrait: isPortrait });
- clearInterval(interval);
- } else if (nIter === 50) {
- _this.emit('change', { isPortrait: isPortrait });
- clearInterval(interval);
- }
- }, 20);
- },
-
-
- _onResize: function _onResize() {
- this.emit('change', { isPortrait: this.isPortrait() });
- }
- };
-
- MicroEvent.mixin(obj);
-
- return obj;
- };
-
- export default create()._init();
|