1/*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2011 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 Util, window, document */
12
13// Globals defined here
14var WebUtil = {}, $D;
15
16/*
17 * Simple DOM selector by ID
18 */
19if (!window.$D) {
20    window.$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
40WebUtil.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};
47WebUtil.init_logging();
48
49
50WebUtil.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            if (typeof(obj[i]) === "undefined") {
63                val = "undefined";
64            } else {
65                val = obj[i].toString().replace("\n", " ");
66            }
67            if (val.length > 30) {
68                val = val.substr(0,30) + "...";
69            }
70            msg += parent + "." + i + ": " + val + "\n";
71        }
72    }
73    return msg;
74};
75
76// Read a query string variable
77WebUtil.getQueryVar = function(name, defVal) {
78    var re = new RegExp('[?][^#]*' + name + '=([^&#]*)');
79    if (typeof defVal === 'undefined') { defVal = null; }
80    return (document.location.href.match(re) || ['',defVal])[1];
81};
82
83
84/*
85 * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
86 */
87
88// No days means only for this browser session
89WebUtil.createCookie = function(name,value,days) {
90    var date, expires;
91    if (days) {
92        date = new Date();
93        date.setTime(date.getTime()+(days*24*60*60*1000));
94        expires = "; expires="+date.toGMTString();
95    }
96    else {
97        expires = "";
98    }
99    document.cookie = name+"="+value+expires+"; path=/";
100};
101
102WebUtil.readCookie = function(name, defaultValue) {
103    var i, c, nameEQ = name + "=", ca = document.cookie.split(';');
104    for(i=0; i < ca.length; i += 1) {
105        c = ca[i];
106        while (c.charAt(0) === ' ') { c = c.substring(1,c.length); }
107        if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); }
108    }
109    return (typeof defaultValue !== 'undefined') ? defaultValue : null;
110};
111
112WebUtil.eraseCookie = function(name) {
113    WebUtil.createCookie(name,"",-1);
114};
115
116/*
117 * Alternate stylesheet selection
118 */
119WebUtil.getStylesheets = function() { var i, links, sheets = [];
120    links = document.getElementsByTagName("link");
121    for (i = 0; i < links.length; i += 1) {
122        if (links[i].title &&
123            links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
124            sheets.push(links[i]);
125        }
126    }
127    return sheets;
128};
129
130// No sheet means try and use value from cookie, null sheet used to
131// clear all alternates.
132WebUtil.selectStylesheet = function(sheet) {
133    var i, link, sheets = WebUtil.getStylesheets();
134    if (typeof sheet === 'undefined') {
135        sheet = 'default';
136    }
137    for (i=0; i < sheets.length; i += 1) {
138        link = sheets[i];
139        if (link.title === sheet) {
140            Util.Debug("Using stylesheet " + sheet);
141            link.disabled = false;
142        } else {
143            //Util.Debug("Skipping stylesheet " + link.title);
144            link.disabled = true;
145        }
146    }
147    return sheet;
148};
149