]> git.proxmox.com Git - mirror_novnc.git/blob - include/webutil.js
Move advanced, integration, troubleshoot to wiki.
[mirror_novnc.git] / include / webutil.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2010 Joel Martin
4 * Licensed under LGPL-3 (see LICENSE.txt)
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 */
12
13 // Globals defined here
14 var WebUtil = {}, $D;
15
16 /*
17 * Simple DOM selector by ID
18 */
19 if (!window.$D) {
20 $D = function (id) {
21 if (document.getElementById) {
22 return document.getElementById(id);
23 } else if (document.all) {
24 return document.all[id];
25 } else if (document.layers) {
26 return document.layers[id];
27 }
28 return undefined;
29 };
30 }
31
32
33 /*
34 * ------------------------------------------------------
35 * Namespaced in WebUtil
36 * ------------------------------------------------------
37 */
38
39 // init log level reading the logging HTTP param
40 WebUtil.init_logging = function() {
41 Util._log_level = (document.location.href.match(
42 /logging=([A-Za-z0-9\._\-]*)/) ||
43 ['', Util._log_level])[1];
44
45 Util.init_logging()
46 }
47 WebUtil.init_logging();
48
49
50 WebUtil.dirObj = function (obj, depth, parent) {
51 var i, msg = "", val = "";
52 if (! depth) { depth=2; }
53 if (! parent) { parent= ""; }
54
55 // Print the properties of the passed-in object
56 for (i in obj) {
57 if ((depth > 1) && (typeof obj[i] === "object")) {
58 // Recurse attributes that are objects
59 msg += WebUtil.dirObj(obj[i], depth-1, parent + "." + i);
60 } else {
61 //val = new String(obj[i]).replace("\n", " ");
62 val = obj[i].toString().replace("\n", " ");
63 if (val.length > 30) {
64 val = val.substr(0,30) + "...";
65 }
66 msg += parent + "." + i + ": " + val + "\n";
67 }
68 }
69 return msg;
70 };
71
72 // Read a query string variable
73 WebUtil.getQueryVar = function(name, defVal) {
74 var re = new RegExp('[?][^#]*' + name + '=([^&#]*)');
75 if (typeof defVal === 'undefined') { defVal = null; }
76 return (document.location.href.match(re) || ['',defVal])[1];
77 };
78
79
80 /*
81 * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
82 */
83
84 // No days means only for this browser session
85 WebUtil.createCookie = function(name,value,days) {
86 var date, expires;
87 if (days) {
88 date = new Date();
89 date.setTime(date.getTime()+(days*24*60*60*1000));
90 expires = "; expires="+date.toGMTString();
91 }
92 else {
93 expires = "";
94 }
95 document.cookie = name+"="+value+expires+"; path=/";
96 };
97
98 WebUtil.readCookie = function(name, defaultValue) {
99 var i, c, nameEQ = name + "=", ca = document.cookie.split(';');
100 for(i=0; i < ca.length; i += 1) {
101 c = ca[i];
102 while (c.charAt(0) === ' ') { c = c.substring(1,c.length); }
103 if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); }
104 }
105 return (typeof defaultValue !== 'undefined') ? defaultValue : null;
106 };
107
108 WebUtil.eraseCookie = function(name) {
109 WebUtil.createCookie(name,"",-1);
110 };
111
112 /*
113 * Alternate stylesheet selection
114 */
115 WebUtil.getStylesheets = function() { var i, links, sheets = [];
116 links = document.getElementsByTagName("link");
117 for (i = 0; i < links.length; i += 1) {
118 if (links[i].title &&
119 links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
120 sheets.push(links[i]);
121 }
122 }
123 return sheets;
124 };
125
126 // No sheet means try and use value from cookie, null sheet used to
127 // clear all alternates.
128 WebUtil.selectStylesheet = function(sheet) {
129 var i, link, sheets = WebUtil.getStylesheets();
130 if (typeof sheet === 'undefined') {
131 sheet = 'default';
132 }
133 for (i=0; i < sheets.length; i += 1) {
134 link = sheets[i];
135 if (link.title === sheet) {
136 Util.Debug("Using stylesheet " + sheet);
137 link.disabled = false;
138 } else {
139 //Util.Debug("Skipping stylesheet " + link.title);
140 link.disabled = true;
141 }
142 }
143 return sheet;
144 };