]> git.proxmox.com Git - mirror_novnc.git/blame - core/util.js
Uncomment ES6 module syntax
[mirror_novnc.git] / core / util.js
CommitLineData
61dd52c9 1/*
15046f00 2 * noVNC: HTML5 VNC client
d58f8b51 3 * Copyright (C) 2012 Joel Martin
1d728ace 4 * Licensed under MPL 2.0 (see LICENSE.txt)
15046f00
JM
5 *
6 * See README.md for usage and integration instructions.
61dd52c9
JM
7 */
8
d21cd6c1
SR
9/* jshint white: false, nonstandard: true */
10/*global window, console, document, navigator, ActiveXObject, INCLUDE_URI */
61dd52c9 11
a59f1cd2 12var Util = {};
15046f00 13
d21cd6c1 14/*
15046f00
JM
15 * ------------------------------------------------------
16 * Namespaced in Util
17 * ------------------------------------------------------
18 */
19
8db09746
JM
20/*
21 * Logging/debug routines
22 */
23
c1eba48f 24Util._log_level = 'warn';
8db09746 25Util.init_logging = function (level) {
d21cd6c1 26 "use strict";
c1eba48f 27 if (typeof level === 'undefined') {
c1eba48f
JM
28 level = Util._log_level;
29 } else {
30 Util._log_level = level;
31 }
8db09746
JM
32
33 Util.Debug = Util.Info = Util.Warn = Util.Error = function (msg) {};
e4fef7be
SR
34 if (typeof window.console !== "undefined") {
35 /* jshint -W086 */
36 switch (level) {
37 case 'debug':
d314d2c2 38 Util.Debug = console.debug.bind(window.console);
e4fef7be 39 case 'info':
d314d2c2 40 Util.Info = console.info.bind(window.console);
e4fef7be 41 case 'warn':
d314d2c2 42 Util.Warn = console.warn.bind(window.console);
e4fef7be 43 case 'error':
d314d2c2 44 Util.Error = console.error.bind(window.console);
e4fef7be
SR
45 case 'none':
46 break;
47 default:
48 throw new Error("invalid logging type '" + level + "'");
49 }
50 /* jshint +W086 */
8db09746
JM
51 }
52};
c1eba48f 53Util.get_logging = function () {
8d5d2c82 54 return Util._log_level;
d3796c14 55};
8db09746 56// Initialize logging level
c1eba48f 57Util.init_logging();
8db09746 58
d21cd6c1
SR
59Util.make_property = function (proto, name, mode, type) {
60 "use strict";
a8edf9d8 61
d21cd6c1
SR
62 var getter;
63 if (type === 'arr') {
64 getter = function (idx) {
65 if (typeof idx !== 'undefined') {
66 return this['_' + name][idx];
67 } else {
68 return this['_' + name];
69 }
70 };
71 } else {
72 getter = function () {
73 return this['_' + name];
74 };
75 }
5210330a 76
d21cd6c1
SR
77 var make_setter = function (process_val) {
78 if (process_val) {
79 return function (val, idx) {
80 if (typeof idx !== 'undefined') {
81 this['_' + name][idx] = process_val(val);
82 } else {
83 this['_' + name] = process_val(val);
84 }
85 };
5210330a 86 } else {
d21cd6c1
SR
87 return function (val, idx) {
88 if (typeof idx !== 'undefined') {
89 this['_' + name][idx] = val;
90 } else {
91 this['_' + name] = val;
92 }
93 };
5210330a
JM
94 }
95 };
96
d21cd6c1
SR
97 var setter;
98 if (type === 'bool') {
99 setter = make_setter(function (val) {
100 if (!val || (val in {'0': 1, 'no': 1, 'false': 1})) {
101 return false;
d890e864 102 } else {
d21cd6c1 103 return true;
d890e864 104 }
d21cd6c1
SR
105 });
106 } else if (type === 'int') {
107 setter = make_setter(function (val) { return parseInt(val, 10); });
108 } else if (type === 'float') {
109 setter = make_setter(parseFloat);
110 } else if (type === 'str') {
111 setter = make_setter(String);
112 } else if (type === 'func') {
113 setter = make_setter(function (val) {
5210330a 114 if (!val) {
d21cd6c1
SR
115 return function () {};
116 } else {
117 return val;
5210330a 118 }
d21cd6c1
SR
119 });
120 } else if (type === 'arr' || type === 'dom' || type == 'raw') {
121 setter = make_setter();
122 } else {
123 throw new Error('Unknown property type ' + type); // some sanity checking
124 }
5210330a 125
d21cd6c1
SR
126 // set the getter
127 if (typeof proto['get_' + name] === 'undefined') {
128 proto['get_' + name] = getter;
125d8bbb 129 }
d890e864 130
d21cd6c1
SR
131 // set the setter if needed
132 if (typeof proto['set_' + name] === 'undefined') {
133 if (mode === 'rw') {
134 proto['set_' + name] = setter;
135 } else if (mode === 'wo') {
136 proto['set_' + name] = function (val, idx) {
137 if (typeof this['_' + name] !== 'undefined') {
138 throw new Error(name + " can only be set once");
139 }
140 setter.call(this, val, idx);
141 };
142 }
125d8bbb 143 }
ff36b127 144
d21cd6c1
SR
145 // make a special setter that we can use in set defaults
146 proto['_raw_set_' + name] = function (val, idx) {
147 setter.call(this, val, idx);
148 //delete this['_init_set_' + name]; // remove it after use
149 };
150};
151
152Util.make_properties = function (constructor, arr) {
153 "use strict";
154 for (var i = 0; i < arr.length; i++) {
155 Util.make_property(constructor.prototype, arr[i][0], arr[i][1], arr[i][2]);
ff36b127 156 }
125d8bbb
JM
157};
158
d21cd6c1
SR
159Util.set_defaults = function (obj, conf, defaults) {
160 var defaults_keys = Object.keys(defaults);
161 var conf_keys = Object.keys(conf);
162 var keys_obj = {};
5210330a 163 var i;
d21cd6c1
SR
164 for (i = 0; i < defaults_keys.length; i++) { keys_obj[defaults_keys[i]] = 1; }
165 for (i = 0; i < conf_keys.length; i++) { keys_obj[conf_keys[i]] = 1; }
166 var keys = Object.keys(keys_obj);
167
168 for (i = 0; i < keys.length; i++) {
169 var setter = obj['_raw_set_' + keys[i]];
7caa9c20
MA
170 if (!setter) {
171 Util.Warn('Invalid property ' + keys[i]);
172 continue;
173 }
d21cd6c1 174
cfc02e5e 175 if (keys[i] in conf) {
d21cd6c1
SR
176 setter.call(obj, conf[keys[i]]);
177 } else {
178 setter.call(obj, defaults[keys[i]]);
179 }
5210330a 180 }
ff4bfcb7 181};
125d8bbb 182
b7996b04 183/*
184 * Decode from UTF-8
185 */
d21cd6c1
SR
186Util.decodeUTF8 = function (utf8string) {
187 "use strict";
b7996b04 188 return decodeURIComponent(escape(utf8string));
d21cd6c1 189};
b7996b04 190
191
a8edf9d8 192
15046f00
JM
193/*
194 * Cross-browser routines
195 */
196
04b399e2 197Util.getPointerEvent = function (e) {
a0e3ec0a 198 return e.changedTouches ? e.changedTouches[0] : e.touches ? e.touches[0] : e;
04b399e2 199};
15046f00 200
d21cd6c1 201Util.stopEvent = function (e) {
b4ef49ea
SR
202 e.stopPropagation();
203 e.preventDefault();
15046f00
JM
204};
205
bea2b3fd 206// Touch detection
207Util.isTouchDevice = ('ontouchstart' in document.documentElement) ||
208 // requried for Chrome debugger
209 (document.ontouchstart !== undefined) ||
210 // required for MS Surface
211 (navigator.maxTouchPoints > 0) ||
212 (navigator.msMaxTouchPoints > 0);
213window.addEventListener('touchstart', function onFirstTouch() {
214 Util.isTouchDevice = true;
215 window.removeEventListener('touchstart', onFirstTouch, false);
216}, false);
217
58ded70d
SR
218Util._cursor_uris_supported = null;
219
220Util.browserSupportsCursorURIs = function () {
221 if (Util._cursor_uris_supported === null) {
222 try {
223 var target = document.createElement('canvas');
224 target.style.cursor = 'url("data:image/x-icon;base64,AAACAAEACAgAAAIAAgA4AQAAFgAAACgAAAAIAAAAEAAAAAEAIAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAD/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAA==") 2 2, default';
225
226 if (target.style.cursor) {
227 Util.Info("Data URI scheme cursor supported");
228 Util._cursor_uris_supported = true;
229 } else {
230 Util.Warn("Data URI scheme cursor not supported");
231 Util._cursor_uris_supported = false;
232 }
233 } catch (exc) {
234 Util.Error("Data URI scheme cursor test exception: " + exc);
235 Util._cursor_uris_supported = false;
236 }
237 }
238
239 return Util._cursor_uris_supported;
240};
15046f00
JM
241
242// Set browser engine versions. Based on mootools.
243Util.Features = {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)};
244
d21cd6c1
SR
245(function () {
246 "use strict";
247 // 'presto': (function () { return (!window.opera) ? false : true; }()),
248 var detectPresto = function () {
249 return !!window.opera;
250 };
251
252 // 'trident': (function () { return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4);
253 var detectTrident = function () {
254 if (!window.ActiveXObject) {
255 return false;
256 } else {
257 if (window.XMLHttpRequest) {
258 return (document.querySelectorAll) ? 6 : 5;
259 } else {
260 return 4;
261 }
262 }
263 };
264
265 // 'webkit': (function () { try { return (navigator.taintEnabled) ? false : ((Util.Features.xpath) ? ((Util.Features.query) ? 525 : 420) : 419); } catch (e) { return false; } }()),
266 var detectInitialWebkit = function () {
267 try {
268 if (navigator.taintEnabled) {
269 return false;
270 } else {
271 if (Util.Features.xpath) {
272 return (Util.Features.query) ? 525 : 420;
273 } else {
274 return 419;
275 }
276 }
277 } catch (e) {
278 return false;
279 }
280 };
281
282 var detectActualWebkit = function (initial_ver) {
283 var re = /WebKit\/([0-9\.]*) /;
284 var str_ver = (navigator.userAgent.match(re) || ['', initial_ver])[1];
285 return parseFloat(str_ver, 10);
286 };
287
288 // 'gecko': (function () { return (!document.getBoxObjectFor && window.mozInnerScreenX == null) ? false : ((document.getElementsByClassName) ? 19ssName) ? 19 : 18 : 18); }())
289 var detectGecko = function () {
290 /* jshint -W041 */
291 if (!document.getBoxObjectFor && window.mozInnerScreenX == null) {
292 return false;
293 } else {
294 return (document.getElementsByClassName) ? 19 : 18;
295 }
296 /* jshint +W041 */
297 };
298
299 Util.Engine = {
300 // Version detection break in Opera 11.60 (errors on arguments.callee.caller reference)
301 //'presto': (function() {
302 // return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); }()),
303 'presto': detectPresto(),
304 'trident': detectTrident(),
305 'webkit': detectInitialWebkit(),
9310577b 306 'gecko': detectGecko()
d21cd6c1
SR
307 };
308
309 if (Util.Engine.webkit) {
310 // Extract actual webkit version if available
311 Util.Engine.webkit = detectActualWebkit(Util.Engine.webkit);
312 }
313})();
15046f00 314
d21cd6c1
SR
315Util.Flash = (function () {
316 "use strict";
15046f00
JM
317 var v, version;
318 try {
319 v = navigator.plugins['Shockwave Flash'].description;
d21cd6c1 320 } catch (err1) {
15046f00
JM
321 try {
322 v = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
d21cd6c1 323 } catch (err2) {
15046f00
JM
324 v = '0 r0';
325 }
326 }
327 version = v.match(/\d+/g);
328 return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
d21cd6c1 329}());
ae510306 330
5af39a87
LJ
331
332Util.Localisation = {
3cdc603a
PO
333 // Currently configured language
334 language: 'en',
5af39a87 335
3cdc603a
PO
336 // Configure suitable language based on user preferences
337 setup: function (supportedLanguages) {
338 var userLanguages;
da88c287 339
3cdc603a 340 Util.Localisation.language = 'en'; // Default: US English
5af39a87 341
3cdc603a
PO
342 /*
343 * Navigator.languages only available in Chrome (32+) and FireFox (32+)
344 * Fall back to navigator.language for other browsers
345 */
5af39a87 346 if (typeof window.navigator.languages == 'object') {
3cdc603a 347 userLanguages = window.navigator.languages;
5af39a87 348 } else {
3cdc603a 349 userLanguages = [navigator.language || navigator.userLanguage];
5af39a87 350 }
5af39a87 351
3cdc603a
PO
352 for (var i = 0;i < userLanguages.length;i++) {
353 var userLang = userLanguages[i];
354 userLang = userLang.toLowerCase();
355 userLang = userLang.replace("_", "-");
356 userLang = userLang.split("-");
357
358 // Built-in default?
359 if ((userLang[0] === 'en') &&
360 ((userLang[1] === undefined) || (userLang[1] === 'us'))) {
361 return;
5af39a87 362 }
5af39a87 363
3cdc603a
PO
364 // First pass: perfect match
365 for (var j = 0;j < supportedLanguages.length;j++) {
366 var supLang = supportedLanguages[j];
367 supLang = supLang.toLowerCase();
368 supLang = supLang.replace("_", "-");
369 supLang = supLang.split("-");
370
371 if (userLang[0] !== supLang[0])
372 continue;
373 if (userLang[1] !== supLang[1])
374 continue;
375
376 Util.Localisation.language = supportedLanguages[j];
377 return;
378 }
379
380 // Second pass: fallback
381 for (var j = 0;j < supportedLanguages.length;j++) {
382 supLang = supportedLanguages[j];
383 supLang = supLang.toLowerCase();
384 supLang = supLang.replace("_", "-");
385 supLang = supLang.split("-");
386
387 if (userLang[0] !== supLang[0])
388 continue;
389 if (supLang[1] !== undefined)
390 continue;
391
392 Util.Localisation.language = supportedLanguages[j];
393 return;
394 }
395 }
5af39a87
LJ
396 },
397
398 // Retrieve localised text
399 get: function (id) {
9e26112d 400 if (typeof Language !== 'undefined' && Language[id]) {
5af39a87
LJ
401 return Language[id];
402 } else {
403 return id;
404 }
edffd9e2
PO
405 },
406
407 // Traverses the DOM and translates relevant fields
408 // See https://html.spec.whatwg.org/multipage/dom.html#attr-translate
409 translateDOM: function () {
410 function process(elem, enabled) {
411 function isAnyOf(searchElement, items) {
412 return items.indexOf(searchElement) !== -1;
413 }
414
415 function translateAttribute(elem, attr) {
416 var str = elem.getAttribute(attr);
417 str = Util.Localisation.get(str);
418 elem.setAttribute(attr, str);
419 }
420
421 function translateTextNode(node) {
422 var str = node.data.trim();
423 str = Util.Localisation.get(str);
424 node.data = str;
425 }
426
427 if (elem.hasAttribute("translate")) {
428 if (isAnyOf(elem.getAttribute("translate"), ["", "yes"])) {
429 enabled = true;
430 } else if (isAnyOf(elem.getAttribute("translate"), ["no"])) {
431 enabled = false;
432 }
433 }
434
435 if (enabled) {
436 if (elem.hasAttribute("abbr") &&
437 elem.tagName === "TH") {
438 translateAttribute(elem, "abbr");
439 }
440 if (elem.hasAttribute("alt") &&
441 isAnyOf(elem.tagName, ["AREA", "IMG", "INPUT"])) {
442 translateAttribute(elem, "alt");
443 }
444 if (elem.hasAttribute("download") &&
445 isAnyOf(elem.tagName, ["A", "AREA"])) {
446 translateAttribute(elem, "download");
447 }
448 if (elem.hasAttribute("label") &&
449 isAnyOf(elem.tagName, ["MENUITEM", "MENU", "OPTGROUP",
450 "OPTION", "TRACK"])) {
451 translateAttribute(elem, "label");
452 }
453 // FIXME: Should update "lang"
454 if (elem.hasAttribute("placeholder") &&
c3325dc6 455 isAnyOf(elem.tagName, ["INPUT", "TEXTAREA"])) {
edffd9e2
PO
456 translateAttribute(elem, "placeholder");
457 }
458 if (elem.hasAttribute("title")) {
459 translateAttribute(elem, "title");
460 }
461 if (elem.hasAttribute("value") &&
462 elem.tagName === "INPUT" &&
463 isAnyOf(elem.getAttribute("type"), ["reset", "button"])) {
464 translateAttribute(elem, "value");
465 }
466 }
467
468 for (var i = 0;i < elem.childNodes.length;i++) {
d55e4545 469 let node = elem.childNodes[i];
edffd9e2
PO
470 if (node.nodeType === node.ELEMENT_NODE) {
471 process(node, enabled);
472 } else if (node.nodeType === node.TEXT_NODE && enabled) {
473 translateTextNode(node);
474 }
475 }
476 }
477
478 process(document.body, true);
479 },
5af39a87
LJ
480};
481
86d15a49
PO
482// Emulate Element.setCapture() when not supported
483
484Util._captureRecursion = false;
485Util._captureProxy = function (e) {
486 // Recursion protection as we'll see our own event
487 if (Util._captureRecursion) return;
488
489 // Clone the event as we cannot dispatch an already dispatched event
490 var newEv = new e.constructor(e.type, e);
491
492 Util._captureRecursion = true;
493 Util._captureElem.dispatchEvent(newEv);
494 Util._captureRecursion = false;
495
16584665
SM
496 // Avoid double events
497 e.stopPropagation();
498
499 // Respect the wishes of the redirected event handlers
500 if (newEv.defaultPrevented) {
501 e.preventDefault();
502 }
503
86d15a49
PO
504 // Implicitly release the capture on button release
505 if ((e.type === "mouseup") || (e.type === "touchend")) {
506 Util.releaseCapture();
507 }
508};
509
8cbf1dd9
SM
510// Follow cursor style of target element
511Util._captureElemChanged = function() {
512 var captureElem = document.getElementById("noVNC_mouse_capture_elem");
513 captureElem.style.cursor = window.getComputedStyle(Util._captureElem).cursor;
514};
515Util._captureObserver = new MutationObserver(Util._captureElemChanged);
516
90ecc739
PO
517Util._captureIndex = 0;
518
86d15a49
PO
519Util.setCapture = function (elem) {
520 if (elem.setCapture) {
521
522 elem.setCapture();
523
524 // IE releases capture on 'click' events which might not trigger
525 elem.addEventListener('mouseup', Util.releaseCapture);
526 elem.addEventListener('touchend', Util.releaseCapture);
527
528 } else {
16584665
SM
529 // Release any existing capture in case this method is
530 // called multiple times without coordination
531 Util.releaseCapture();
532
86d15a49
PO
533 // Safari on iOS 9 has a broken constructor for TouchEvent.
534 // We are fine in this case however, since Safari seems to
535 // have some sort of implicit setCapture magic anyway.
536 if (window.TouchEvent !== undefined) {
537 try {
538 new TouchEvent("touchstart");
539 } catch (TypeError) {
540 return;
541 }
542 }
543
544 var captureElem = document.getElementById("noVNC_mouse_capture_elem");
545
546 if (captureElem === null) {
547 captureElem = document.createElement("div");
548 captureElem.id = "noVNC_mouse_capture_elem";
549 captureElem.style.position = "fixed";
550 captureElem.style.top = "0px";
551 captureElem.style.left = "0px";
552 captureElem.style.width = "100%";
553 captureElem.style.height = "100%";
554 captureElem.style.zIndex = 10000;
555 captureElem.style.display = "none";
556 document.body.appendChild(captureElem);
557
16584665
SM
558 // This is to make sure callers don't get confused by having
559 // our blocking element as the target
560 captureElem.addEventListener('contextmenu', Util._captureProxy);
561
86d15a49
PO
562 captureElem.addEventListener('mousemove', Util._captureProxy);
563 captureElem.addEventListener('mouseup', Util._captureProxy);
564
565 captureElem.addEventListener('touchmove', Util._captureProxy);
566 captureElem.addEventListener('touchend', Util._captureProxy);
567 }
568
569 Util._captureElem = elem;
90ecc739 570 Util._captureIndex++;
8cbf1dd9
SM
571
572 // Track cursor and get initial cursor
573 Util._captureObserver.observe(elem, {attributes:true});
574 Util._captureElemChanged();
575
86d15a49
PO
576 captureElem.style.display = null;
577
578 // We listen to events on window in order to keep tracking if it
579 // happens to leave the viewport
580 window.addEventListener('mousemove', Util._captureProxy);
581 window.addEventListener('mouseup', Util._captureProxy);
582
583 window.addEventListener('touchmove', Util._captureProxy);
584 window.addEventListener('touchend', Util._captureProxy);
585 }
586};
587
588Util.releaseCapture = function () {
589 if (document.releaseCapture) {
590
591 document.releaseCapture();
592
593 } else {
16584665
SM
594 if (!Util._captureElem) {
595 return;
596 }
597
598 // There might be events already queued, so we need to wait for
599 // them to flush. E.g. contextmenu in Microsoft Edge
90ecc739
PO
600 window.setTimeout(function(expected) {
601 // Only clear it if it's the expected grab (i.e. no one
602 // else has initiated a new grab)
603 if (Util._captureIndex === expected) {
604 Util._captureElem = null;
605 }
606 }, 0, Util._captureIndex);
16584665 607
8cbf1dd9
SM
608 Util._captureObserver.disconnect();
609
86d15a49 610 var captureElem = document.getElementById("noVNC_mouse_capture_elem");
86d15a49
PO
611 captureElem.style.display = "none";
612
613 window.removeEventListener('mousemove', Util._captureProxy);
614 window.removeEventListener('mouseup', Util._captureProxy);
615
616 window.removeEventListener('touchmove', Util._captureProxy);
617 window.removeEventListener('touchend', Util._captureProxy);
618 }
619};
620
3ae0bb09 621export default Util;