]> git.proxmox.com Git - mirror_novnc.git/blob - include/util.js
No mootools dep outside of default_controls.js.
[mirror_novnc.git] / include / util.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2010 Joel Martin
4 * Licensed under LGPL-3 (see LICENSE.LGPL-3)
5 *
6 * See README.md for usage and integration instructions.
7 */
8
9 "use strict";
10 /*jslint bitwise: false, white: false */
11 /*global window, document, navigator, ActiveXObject*/
12
13 // Globals defined here
14 var Util = {}, $;
15
16 if ((!window.console) || (! /__debug__$/i.test(document.location.href))) {
17 // non-debug mode, an empty function
18 window.console = window.console || {};
19 window.console.log = function (message) {};
20 window.console.warn = function (message) {};
21 window.console.error = function (message) {};
22 }
23
24 // Simple DOM selector by ID
25 if (!window.$) {
26 $ = function (id) {
27 if (document.getElementById) {
28 return document.getElementById(id);
29 } else if (document.all) {
30 return document.all[id];
31 } else if (document.layers) {
32 return document.layers[id];
33 }
34 return undefined;
35 };
36 }
37
38 /*
39 * Make arrays quack
40 */
41
42 Array.prototype.shift8 = function () {
43 return this.shift();
44 };
45 Array.prototype.push8 = function (num) {
46 this.push(num & 0xFF);
47 };
48
49 Array.prototype.shift16 = function () {
50 return (this.shift() << 8) +
51 (this.shift() );
52 };
53 Array.prototype.push16 = function (num) {
54 this.push((num >> 8) & 0xFF,
55 (num ) & 0xFF );
56 };
57
58
59 Array.prototype.shift32 = function () {
60 return (this.shift() << 24) +
61 (this.shift() << 16) +
62 (this.shift() << 8) +
63 (this.shift() );
64 };
65 Array.prototype.get32 = function (off) {
66 return (this[off ] << 24) +
67 (this[off + 1] << 16) +
68 (this[off + 2] << 8) +
69 (this[off + 3] );
70 };
71 Array.prototype.push32 = function (num) {
72 this.push((num >> 24) & 0xFF,
73 (num >> 16) & 0xFF,
74 (num >> 8) & 0xFF,
75 (num ) & 0xFF );
76 };
77
78 Array.prototype.shiftStr = function (len) {
79 var arr = this.splice(0, len);
80 return arr.map(function (num) {
81 return String.fromCharCode(num); } ).join('');
82 };
83 Array.prototype.pushStr = function (str) {
84 var i, n = str.length;
85 for (i=0; i < n; i+=1) {
86 this.push(str.charCodeAt(i));
87 }
88 };
89
90 Array.prototype.shiftBytes = function (len) {
91 return this.splice(0, len);
92 };
93
94 /*
95 * ------------------------------------------------------
96 * Namespaced in Util
97 * ------------------------------------------------------
98 */
99
100 Util.dirObj = function (obj, depth, parent) {
101 var i, msg = "", val = "";
102 if (! depth) { depth=2; }
103 if (! parent) { parent= ""; }
104
105 // Print the properties of the passed-in object
106 for (i in obj) {
107 if ((depth > 1) && (typeof obj[i] === "object")) {
108 // Recurse attributes that are objects
109 msg += Util.dirObj(obj[i], depth-1, parent + "." + i);
110 } else {
111 //val = new String(obj[i]).replace("\n", " ");
112 val = obj[i].toString().replace("\n", " ");
113 if (val.length > 30) {
114 val = val.substr(0,30) + "...";
115 }
116 msg += parent + "." + i + ": " + val + "\n";
117 }
118 }
119 return msg;
120 };
121
122 /*
123 * Cross-browser routines
124 */
125
126 // Get DOM element position on page
127 Util.getPosition = function (obj) {
128 var x = 0, y = 0;
129 if (obj.offsetParent) {
130 do {
131 x += obj.offsetLeft;
132 y += obj.offsetTop;
133 obj = obj.offsetParent;
134 } while (obj);
135 }
136 return {'x': x, 'y': y};
137 };
138
139 // Get mouse event position in DOM element
140 Util.getEventPosition = function (e, obj) {
141 var evt, docX, docY, pos;
142 //if (!e) evt = window.event;
143 evt = (e ? e : window.event);
144 if (evt.pageX || evt.pageY) {
145 docX = evt.pageX;
146 docY = evt.pageY;
147 } else if (evt.clientX || evt.clientY) {
148 docX = evt.clientX + document.body.scrollLeft +
149 document.documentElement.scrollLeft;
150 docY = evt.clientY + document.body.scrollTop +
151 document.documentElement.scrollTop;
152 }
153 pos = Util.getPosition(obj);
154 return {'x': docX - pos.x, 'y': docY - pos.y};
155 };
156
157
158 // Event registration. Based on: http://www.scottandrew.com/weblog/articles/cbs-events
159 Util.addEvent = function (obj, evType, fn){
160 if (obj.addEventListener){
161 obj.addEventListener(evType, fn, false);
162 return true;
163 } else if (obj.attachEvent){
164 var r = obj.attachEvent("on"+evType, fn);
165 return r;
166 } else {
167 throw("Handler could not be attached");
168 }
169 };
170
171 Util.removeEvent = function(obj, evType, fn){
172 if (obj.removeEventListener){
173 obj.removeEventListener(evType, fn, false);
174 return true;
175 } else if (obj.detachEvent){
176 var r = obj.detachEvent("on"+evType, fn);
177 return r;
178 } else {
179 throw("Handler could not be removed");
180 }
181 };
182
183 Util.stopEvent = function(e) {
184 if (e.stopPropagation) { e.stopPropagation(); }
185 else { e.cancelBubble = true; }
186
187 if (e.preventDefault) { e.preventDefault(); }
188 else { e.returnValue = false; }
189 };
190
191
192 // Set browser engine versions. Based on mootools.
193 Util.Features = {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)};
194
195 Util.Engine = {
196 'presto': (function() {
197 return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925)); }()),
198 'trident': (function() {
199 return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? ((document.querySelectorAll) ? 6 : 5) : 4); }()),
200 'webkit': (function() {
201 return (navigator.taintEnabled) ? false : ((Util.Features.xpath) ? ((Util.Features.query) ? 525 : 420) : 419); }()),
202 'gecko': (function() {
203 return (!document.getBoxObjectFor && !window.mozInnerScreenX) ? false : ((document.getElementsByClassName) ? 19 : 18); }())
204 };
205
206 Util.Flash = (function(){
207 var v, version;
208 try {
209 v = navigator.plugins['Shockwave Flash'].description;
210 } catch(err1) {
211 try {
212 v = new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
213 } catch(err2) {
214 v = '0 r0';
215 }
216 }
217 version = v.match(/\d+/g);
218 return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
219 }());
220
221