1 /**
  2  * Hilo
  3  * Copyright 2015 alibaba.com
  4  * Licensed under the MIT License
  5  */
  6 
  7 /**
  8  * @class 浏览器特性集合
  9  * @static
 10  * @module hilo/util/browser
 11  */
 12 var browser = (function(){
 13     var ua = navigator.userAgent;
 14     var doc = document;
 15     var win = window;
 16     var docElem = doc.documentElement;
 17 
 18     var data = /** @lends browser */ {
 19         /**
 20          * 是否是iphone
 21          * @type {Boolean}
 22          */
 23         iphone: /iphone/i.test(ua),
 24 
 25         /**
 26          * 是否是ipad
 27          * @type {Boolean}
 28          */
 29         ipad: /ipad/i.test(ua),
 30 
 31         /**
 32          * 是否是ipod
 33          * @type {Boolean}
 34          */
 35         ipod: /ipod/i.test(ua),
 36 
 37         /**
 38          * 是否是ios
 39          * @type {Boolean}
 40          */
 41         ios: /iphone|ipad|ipod/i.test(ua),
 42 
 43         /**
 44          * 是否是android
 45          * @type {Boolean}
 46          */
 47         android: /android/i.test(ua),
 48 
 49         /**
 50          * 是否是webkit
 51          * @type {Boolean}
 52          */
 53         webkit: /webkit/i.test(ua),
 54 
 55         /**
 56          * 是否是chrome
 57          * @type {Boolean}
 58          */
 59         chrome: /chrome/i.test(ua),
 60 
 61         /**
 62          * 是否是safari
 63          * @type {Boolean}
 64          */
 65         safari: /safari/i.test(ua),
 66 
 67         /**
 68          * 是否是firefox
 69          * @type {Boolean}
 70          */
 71         firefox: /firefox/i.test(ua),
 72 
 73         /**
 74          * 是否是ie
 75          * @type {Boolean}
 76          */
 77         ie: /msie/i.test(ua),
 78 
 79         /**
 80          * 是否是opera
 81          * @type {Boolean}
 82          */
 83         opera: /opera/i.test(ua),
 84         /**
 85          * 是否支持触碰事件。
 86          * @type {String}
 87          */
 88         supportTouch: 'ontouchstart' in win,
 89 
 90         /**
 91          * 是否支持canvas元素。
 92          * @type {Boolean}
 93          */
 94         supportCanvas: doc.createElement('canvas').getContext != null,
 95         /**
 96          * 是否支持本地存储localStorage。
 97          * @type {Boolean}
 98          */
 99         supportStorage: false,
100 
101         /**
102          * 是否支持检测设备方向orientation。
103          * @type {Boolean}
104          */
105         supportOrientation: 'orientation' in win || 'orientation' in win.screen,
106 
107         /**
108          * 是否支持检测加速度devicemotion。
109          * @type {Boolean}
110          */
111         supportDeviceMotion: 'ondevicemotion' in win
112     };
113 
114     //`localStorage` is null or `localStorage.setItem` throws error in some cases (e.g. localStorage is disabled)
115     try{
116         var value = 'hilo';
117         localStorage.setItem(value, value);
118         localStorage.removeItem(value);
119         data.supportStorage = true;
120     }catch(e){}
121 
122     /**
123      * 浏览器厂商CSS前缀的js值。比如:webkit。
124      * @type {String}
125      */
126     var jsVendor = data.jsVendor = data.webkit ? 'webkit' : data.firefox ? 'webkit' : data.opera ? 'o' : data.ie ? 'ms' : '';
127     /**
128      * 浏览器厂商CSS前缀的css值。比如:-webkit-。
129      * @type {String}
130      */
131     var cssVendor = data.cssVendor = '-' + jsVendor + '-';
132 
133     //css transform/3d feature dectection
134     var testElem = doc.createElement('div'), style = testElem.style;
135     /**
136      * 是否支持CSS Transform变换。
137      * @type {Boolean}
138      */
139     var supportTransform = style[jsVendor + 'Transform'] != undefined;
140 
141     /**
142      * 是否支持CSS Transform 3D变换。
143      * @type {Boolean}
144      */
145     var supportTransform3D = style[jsVendor + 'Perspective'] != undefined;
146     if(supportTransform3D){
147         testElem.id = 'test3d';
148         style = doc.createElement('style');
149         style.textContent = '@media ('+ cssVendor +'transform-3d){#test3d{height:3px}}';
150         doc.head.appendChild(style);
151 
152         docElem.appendChild(testElem);
153         supportTransform3D = testElem.offsetHeight == 3;
154         doc.head.removeChild(style);
155         docElem.removeChild(testElem);
156     }
157     data.supportTransform = supportTransform;
158     data.supportTransform3D = supportTransform3D;
159 
160     var supportTouch = data.supportTouch;
161     
162     /**
163      * 鼠标或触碰开始事件。对应touchstart或mousedown。
164      * @type {String}
165      */
166     var POINTER_START = supportTouch ? 'touchstart' : 'mousedown';
167 
168     /**
169      * 鼠标或触碰移动事件。对应touchmove或mousemove。
170      * @type {String}
171      */
172     var POINTER_MOVE = supportTouch ? 'touchmove' : 'mousemove';
173 
174     /**
175      * 鼠标或触碰结束事件。对应touchend或mouseup。
176      * @type {String}
177      */
178     var POINTER_END = supportTouch ? 'touchend' : 'mouseup';
179 
180     data.POINTER_START = POINTER_START;
181     data.POINTER_MOVE = POINTER_MOVE;
182     data.POINTER_END = POINTER_END;
183     
184     return data;
185 })();