@xg4/device
Version:
Device detection (phone, tablet, desktop, os)
2 lines (1 loc) • 4.6 kB
JavaScript
!function(i,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((i=i||self).Device={})}(this,function(i){"use strict";function e(i){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(i){return typeof i}:function(i){return i&&"function"==typeof Symbol&&i.constructor===Symbol&&i!==Symbol.prototype?"symbol":typeof i})(i)}function n(i,e){if(!(i instanceof e))throw new TypeError("Cannot call a class as a function")}function t(i,e){for(var n=0;n<e.length;n++){var t=e[n];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(i,t.key,t)}}function o(i,e,n){return e&&t(i.prototype,e),n&&t(i,n),i}function r(i,e){return Object.prototype.hasOwnProperty.call(i,e)}function s(){return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"").slice(2).toLowerCase()}var u=function(){function i(){n(this,i)}return o(i,[{key:"listen",value:function(i){var e=this,n=r(window,"onorientationchange")?"orientationchange":"resize";window.addEventListener(n,function(n){i&&i(n,e.type)},!1)}},{key:"isPortrait",value:function(){return screen.orientation&&r(window,"onorientationchange")?screen.orientation.type.includes("portrait"):window.innerHeight/window.innerWidth>1}},{key:"isLandscape",value:function(){return screen.orientation&&r(window,"onorientationchange")?screen.orientation.type.includes("landscape"):window.innerHeight/window.innerWidth<1}},{key:"type",get:function(){return this.isLandscape()?"landscape":"portrait"}}]),i}(),a=function(){function i(e){n(this,i);try{this.userAgent=e||window.navigator.userAgent}catch(i){this.userAgent=""}this.lowerCase=this.userAgent.toLowerCase()}return o(i,[{key:"type",get:function(){var i=this;return s(["isMobile","isTablet","isDesktop"].find(function(e){return i[e]()}))||null}},{key:"os",get:function(){var i=this;return s(["isIos","isAndroid","isBlackberry","isMac","isWindows","isFirefox","isMeego","isTv"].find(function(e){return i[e]()}))||null}}]),o(i,[{key:"match",value:function(i){return i.test(this.userAgent)}},{key:"find",value:function(i){return this.lowerCase.includes(i)}},{key:"isMac",value:function(){return this.find("mac")}},{key:"isIphone",value:function(){return!this.isWindows()&&this.find("iphone")}},{key:"isIpod",value:function(){return this.find("ipod")}},{key:"isIpad",value:function(){return this.find("ipad")}},{key:"isIos",value:function(){return this.isIphone()||this.isIpad()||this.isIpod()}},{key:"isWindows",value:function(){return this.find("windows")}},{key:"isWindowsPhone",value:function(){return this.isWindows()&&this.find("phone")}},{key:"isWindowsTablet",value:function(){return this.isWindows()&&this.find("touch")&&!this.isWindowsPhone()}},{key:"isAndroid",value:function(){return!this.isWindows()&&this.find("android")}},{key:"isAndroidPhone",value:function(){return this.isAndroid()&&this.find("mobile")}},{key:"isAndroidTablet",value:function(){return this.isAndroid()&&!this.find("mobile")}},{key:"isBlackberry",value:function(){return this.find("blackberry")||this.find("bb10")||this.find("rim")}},{key:"isBlackberryPhone",value:function(){return this.isBlackberry()&&!this.find("tablet")}},{key:"isBlackberryTablet",value:function(){return this.isBlackberry()&&this.find("tablet")}},{key:"isFirefox",value:function(){return(this.find("(mobile")||this.find("(tablet"))&&this.find(" rv:")}},{key:"isFirefoxPhone",value:function(){return this.isFirefox()&&this.find("mobile")}},{key:"isFirefoxTablet",value:function(){return this.isFirefox()&&this.find("tablet")}},{key:"isMeego",value:function(){return this.find("meego")}},{key:"isCordova",value:function(){return"undefined"!=typeof window&&window.cordova&&"file:"===location.protocol}},{key:"isNode",value:function(){return"object"===("undefined"==typeof process?"undefined":e(process))}},{key:"isMobile",value:function(){return this.isAndroidPhone()||this.isIphone()||this.isIpod()||this.isWindowsPhone()||this.isBlackberryPhone()||this.isFirefoxPhone()||this.isMeego()}},{key:"isTablet",value:function(){return this.isIpad()||this.isAndroidTablet()||this.isBlackberryTablet()||this.isWindowsTablet()||this.isFirefoxTablet()}},{key:"isDesktop",value:function(){return!this.isTablet()&&!this.isMobile()}},{key:"isTv",value:function(){var i=this;return["googletv","viera","smarttv","internet.tv","netcast","nettv","appletv","boxee","kylo","roku","dlnadoc","roku","pov_tv","hbbtv","ce-html"].some(function(e){return i.find(e)})}}]),i}();i.Orientation=u,i.default=a,Object.defineProperty(i,"__esModule",{value:!0})});