Debian Codegraph @

closure-library/closure/goog/events/browserfeature.js

raw
1 // Copyright 2010 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Browser capability checks for the events package.
17 *
18 */
19
20
21 goog.provide('goog.events.BrowserFeature');
22
23 goog.require('goog.userAgent');
24 goog.scope(function() {
25
26
27
28 /**
29 * Enum of browser capabilities.
30 * @enum {boolean}
31 */
32 goog.events.BrowserFeature = {
33 /**
34 * Whether the button attribute of the event is W3C compliant. False in
35 * Internet Explorer prior to version 9; document-version dependent.
36 */
37 HAS_W3C_BUTTON:
38 !goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(9),
39
40 /**
41 * Whether the browser supports full W3C event model.
42 */
43 HAS_W3C_EVENT_SUPPORT:
44 !goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(9),
45
46 /**
47 * To prevent default in IE7-8 for certain keydown events we need set the
48 * keyCode to -1.
49 */
50 SET_KEY_CODE_TO_PREVENT_DEFAULT:
51 goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9'),
52
53 /**
54 * Whether the {@code navigator.onLine} property is supported.
55 */
56 HAS_NAVIGATOR_ONLINE_PROPERTY:
57 !goog.userAgent.WEBKIT || goog.userAgent.isVersionOrHigher('528'),
58
59 /**
60 * Whether HTML5 network online/offline events are supported.
61 */
62 HAS_HTML5_NETWORK_EVENT_SUPPORT:
63 goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9b') ||
64 goog.userAgent.IE && goog.userAgent.isVersionOrHigher('8') ||
65 goog.userAgent.OPERA && goog.userAgent.isVersionOrHigher('9.5') ||
66 goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('528'),
67
68 /**
69 * Whether HTML5 network events fire on document.body, or otherwise the
70 * window.
71 */
72 HTML5_NETWORK_EVENTS_FIRE_ON_BODY:
73 goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('8') ||
74 goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9'),
75
76 /**
77 * Whether touch is enabled in the browser.
78 */
79 TOUCH_ENABLED:
80 ('ontouchstart' in goog.global ||
81 !!(goog.global['document'] && document.documentElement &&
82 'ontouchstart' in document.documentElement) ||
83 // IE10 uses non-standard touch events, so it has a different check.
84 !!(goog.global['navigator'] &&
85 (goog.global['navigator']['maxTouchPoints'] ||
86 goog.global['navigator']['msMaxTouchPoints']))),
87
88 /**
89 * Whether addEventListener supports W3C standard pointer events.
90 * http://www.w3.org/TR/pointerevents/
91 */
92 POINTER_EVENTS: ('PointerEvent' in goog.global),
93
94 /**
95 * Whether addEventListener supports MSPointer events (only used in IE10).
96 * http://msdn.microsoft.com/en-us/library/ie/hh772103(v=vs.85).aspx
97 * http://msdn.microsoft.com/library/hh673557(v=vs.85).aspx
98 */
99 MSPOINTER_EVENTS:
100 ('MSPointerEvent' in goog.global &&
101 !!(goog.global['navigator'] &&
102 goog.global['navigator']['msPointerEnabled'])),
103
104 /**
105 * Whether addEventListener supports {passive: true}.
106 * https://developers.google.com/web/updates/2016/06/passive-event-listeners
107 */
108 PASSIVE_EVENTS: purify(function() {
109 // If we're in a web worker or other custom environment, we can't tell.
110 if (!goog.global.addEventListener || !Object.defineProperty) { // IE 8
111 return false;
112 }
113
114 var passive = false;
115 var options = Object.defineProperty({}, 'passive', {
116 get: function() {
117 passive = true;
118 }
119 });
120 goog.global.addEventListener('test', goog.nullFunction, options);
121 goog.global.removeEventListener('test', goog.nullFunction, options);
122
123 return passive;
124 })
125 };
126
127
128 /**
129 * Tricks Closure Compiler into believing that a function is pure. The compiler
130 * assumes that any `valueOf` function is pure, without analyzing its contents.
131 *
132 * @param {function(): T} fn
133 * @return {T}
134 * @template T
135 */
136 function purify(fn) {
137 return ({valueOf: fn}).valueOf();
138 }
139 }); // goog.scope