]>
git.proxmox.com Git - mirror_novnc.git/blob - include/util.js
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2010 Joel Martin
4 * Licensed under LGPL-3 (see LICENSE.LGPL-3)
6 * See README.md for usage and integration instructions.
10 /*jslint bitwise: false, white: false */
11 /*global window, document, navigator, ActiveXObject*/
13 // Globals defined here
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
) {};
24 // Simple DOM selector by 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
];
42 Array
.prototype.shift8 = function () {
45 Array
.prototype.push8 = function (num
) {
46 this.push(num
& 0xFF);
49 Array
.prototype.shift16 = function () {
50 return (this.shift() << 8) +
53 Array
.prototype.push16 = function (num
) {
54 this.push((num
>> 8) & 0xFF,
59 Array
.prototype.shift32 = function () {
60 return (this.shift() << 24) +
61 (this.shift() << 16) +
65 Array
.prototype.get32 = function (off
) {
66 return (this[off
] << 24) +
67 (this[off
+ 1] << 16) +
68 (this[off
+ 2] << 8) +
71 Array
.prototype.push32 = function (num
) {
72 this.push((num
>> 24) & 0xFF,
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('');
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
));
90 Array
.prototype.shiftBytes = function (len
) {
91 return this.splice(0, len
);
95 * ------------------------------------------------------
97 * ------------------------------------------------------
100 Util
.dirObj = function (obj
, depth
, parent
) {
101 var i
, msg
= "", val
= "";
102 if (! depth
) { depth
=2; }
103 if (! parent
) { parent
= ""; }
105 // Print the properties of the passed-in object
107 if ((depth
> 1) && (typeof obj
[i
] === "object")) {
108 // Recurse attributes that are objects
109 msg
+= Util
.dirObj(obj
[i
], depth
-1, parent
+ "." + i
);
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) + "...";
116 msg
+= parent
+ "." + i
+ ": " + val
+ "\n";
123 * Cross-browser routines
126 // Get DOM element position on page
127 Util
.getPosition = function (obj
) {
129 if (obj
.offsetParent
) {
133 obj
= obj
.offsetParent
;
136 return {'x': x
, 'y': y
};
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
) {
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
;
153 pos
= Util
.getPosition(obj
);
154 return {'x': docX
- pos
.x
, 'y': docY
- pos
.y
};
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);
163 } else if (obj
.attachEvent
){
164 var r
= obj
.attachEvent("on"+evType
, fn
);
167 throw("Handler could not be attached");
171 Util
.removeEvent = function(obj
, evType
, fn
){
172 if (obj
.removeEventListener
){
173 obj
.removeEventListener(evType
, fn
, false);
175 } else if (obj
.detachEvent
){
176 var r
= obj
.detachEvent("on"+evType
, fn
);
179 throw("Handler could not be removed");
183 Util
.stopEvent = function(e
) {
184 if (e
.stopPropagation
) { e
.stopPropagation(); }
185 else { e
.cancelBubble
= true; }
187 if (e
.preventDefault
) { e
.preventDefault(); }
188 else { e
.returnValue
= false; }
192 // Set browser engine versions. Based on mootools.
193 Util
.Features
= {xpath
: !!(document
.evaluate
), air
: !!(window
.runtime
), query
: !!(document
.querySelector
)};
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); }())
206 Util
.Flash
= (function(){
209 v
= navigator
.plugins
['Shockwave Flash'].description
;
212 v
= new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
217 version
= v
.match(/\d+/g);
218 return {version
: parseInt(version
[0] || 0 + '.' + version
[1], 10) || 0, build
: parseInt(version
[2], 10) || 0};