default_controls.js 12.7 KB
Newer Older
1 2 3
/*
 * noVNC: HTML5 VNC client
 * Copyright (C) 2010 Joel Martin
Joel Martin's avatar
Joel Martin committed
4
 * Licensed under LGPL-3 (see LICENSE.txt)
5 6 7 8
 *
 * See README.md for usage and integration instructions.
 */
"use strict";
9
/*jslint white: false */
10
/*global $, Util, RFB, Canvas, VNC_uri_prefix, Element, Fx */
11

12
var DefaultControls = {
13

14 15 16
settingsOpen : false,

// Render default controls and initialize settings menu
17
load: function(target) {
18
    var html, i, DC = DefaultControls, sheet, sheets, llevels;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

    /* Populate the 'target' DOM element with default controls */
    if (!target) { target = 'vnc'; }

    html = "";
    html += '<div id="VNC_controls">';
    html += '  <ul>';
    html += '    <li>Host: <input id="VNC_host"></li>';
    html += '    <li>Port: <input id="VNC_port"></li>';
    html += '    <li>Password: <input id="VNC_password"';
    html += '        type="password"></li>';
    html += '    <li><input id="VNC_connect_button" type="button"';
    html += '        value="Loading" disabled></li>';
    html += '  </ul>';
    html += '</div>';
    html += '<div id="VNC_screen">';
35 36 37
    html += '  <div id="VNC_status_bar" class="VNC_status_bar" style="margin-top: 0px;">';
    html += '    <table border=0 width=100%><tr>';
    html += '      <td><div id="VNC_status">Loading</div></td>';
38 39 40 41 42 43 44 45 46
    html += '      <td width=1%><div class="VNC_buttons_right">';
    html += '        <input type=button class="VNC_status_button" value="Settings"';
    html += '          id="menuButton"';
    html += '          onclick="DefaultControls.clickSettingsMenu();">';
    html += '        <span id="VNC_settings_menu"';
    html += '          onmouseover="DefaultControls.canvasBlur();"';
    html += '          onmouseout="DefaultControls.canvasFocus();">';
    html += '          <ul>';
    html += '            <li><input id="VNC_encrypt"';
47
    html += '                type="checkbox"> Encrypt</li>';
48 49 50
    html += '            <li><input id="VNC_true_color"';
    html += '                type="checkbox" checked> True Color</li>';
    html += '            <li><input id="VNC_cursor"';
51
    html += '                type="checkbox"> Local Cursor</li>';
52 53
    html += '            <li><input id="VNC_connectTimeout"';
    html += '                type="input"> Connect Timeout (s)</li>';
54 55 56 57 58 59 60
    html += '            <hr>';

    // Stylesheet selection dropdown
    html += '            <li><select id="VNC_stylesheet" name="vncStyle">';
    html += '              <option value="default">default</option>';
    sheet = Util.selectStylesheet();
    sheets = Util.getStylesheets();
61
    for (i = 0; i < sheets.length; i += 1) {
62 63 64 65 66 67 68
        html += '<option value="' + sheets[i].title + '">' + sheets[i].title + '</option>';
    }
    html += '              </select> Style</li>';

    // Logging selection dropdown
    html += '            <li><select id="VNC_logging" name="vncLogging">';
    llevels = ['error', 'warn', 'info', 'debug'];
69
    for (i = 0; i < llevels.length; i += 1) {
70 71 72 73 74 75 76 77 78 79 80
        html += '<option value="' + llevels[i] + '">' + llevels[i] + '</option>';
    }
    html += '              </select> Logging</li>';

    html += '            <hr>';
    html += '            <li><input type="button" id="VNC_apply" value="Apply"';
    html += '                onclick="DefaultControls.settingsApply()"></li>';
    html += '          </ul>';
    html += '        </span></div></td>';
    html += '      <td width=1%><div class="VNC_buttons_right">';
    html += '        <input type=button class="VNC_status_button" value="Send CtrlAltDel"';
81
    html += '          id="sendCtrlAltDelButton"';
82 83 84
    html += '          onclick="DefaultControls.sendCtrlAltDel();"></div></td>';
    html += '    </tr></table>';
    html += '  </div>';
85 86 87 88 89 90 91 92 93 94 95 96
    html += '  <canvas id="VNC_canvas" width="640px" height="20px">';
    html += '      Canvas not supported.';
    html += '  </canvas>';
    html += '</div>';
    html += '<br><br>';
    html += '<div id="VNC_clipboard">';
    html += '  VNC Clipboard:';
    html += '  <input id="VNC_clipboard_clear_button"';
    html += '      type="button" value="Clear"';
    html += '      onclick="DefaultControls.clipClear();">';
    html += '  <br>';
    html += '  <textarea id="VNC_clipboard_text" cols=80 rows=5';
97 98
    html += '    onfocus="DefaultControls.canvasBlur();"';
    html += '    onblur="DefaultControls.canvasFocus();"';
99 100 101 102
    html += '    onchange="DefaultControls.clipSend();"></textarea>';
    html += '</div>';
    $(target).innerHTML = html;

103
    // Settings with immediate effects
104
    DC.initSetting('logging', 'warn');
105 106
    Util.init_logging(DC.getSetting('logging'));
    DC.initSetting('stylesheet', 'default');
107 108

    Util.selectStylesheet(null); // call twice to get around webkit bug
109 110
    Util.selectStylesheet(DC.getSetting('stylesheet'));

111
    /* Populate the controls if defaults are provided in the URL */
112 113 114
    DC.initSetting('host', '');
    DC.initSetting('port', '');
    DC.initSetting('password', '');
115
    DC.initSetting('encrypt', false);
116
    DC.initSetting('true_color', true);
117
    DC.initSetting('cursor', false);
118
    DC.initSetting('connectTimeout', 2);
Joel Martin's avatar
Joel Martin committed
119

120 121 122 123 124
    DC.rfb = RFB({'target': 'VNC_canvas',
                  'updateState': DC.updateState,
                  'clipboardReceive': DC.clipReceive});

    // Unfocus clipboard when over the VNC area
Joel Martin's avatar
Joel Martin committed
125
    $('VNC_screen').onmousemove = function () {
126 127
            var canvas = DC.rfb.get_canvas();
            if ((! canvas) || (! canvas.get_focused())) {
Joel Martin's avatar
Joel Martin committed
128 129 130
                $('VNC_clipboard_text').blur();
            }
        };
131

132 133
},

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
// Read form control compatible setting from cookie
getSetting: function(name) {
    var val, ctrl = $('VNC_' + name);
    val = Util.readCookie(name);
    if (ctrl.type === 'checkbox') {
        if (val.toLowerCase() in {'0':1, 'no':1, 'false':1}) {
            val = false;
        } else {
            val = true;
        }
    }
    return val;
},

// Update cookie and form control setting. If value is not set, then
// updates from control to current cookie setting.
updateSetting: function(name, value) {
    var i, ctrl = $('VNC_' + name);
    // Save the cookie for this session
    if (typeof value !== 'undefined') {
        Util.createCookie(name, value);
    }

    // Update the settings control
    value = DefaultControls.getSetting(name);
    if (ctrl.type === 'checkbox') {
        ctrl.checked = value;
    } else if (typeof ctrl.options !== 'undefined') {
162
        for (i = 0; i < ctrl.options.length; i += 1) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            if (ctrl.options[i].value === value) {
                ctrl.selectedIndex = i;
                break;
            }
        }
    } else {
        ctrl.value = value;
    }
},

// Save control setting to cookie
saveSetting: function(name) {
    var val, ctrl = $('VNC_' + name);
    if (ctrl.type === 'checkbox') {
        val = ctrl.checked;
    } else if (typeof ctrl.options !== 'undefined') {
        val = ctrl.options[ctrl.selectedIndex].value;
    } else {
        val = ctrl.value;
    }
    Util.createCookie(name, val);
184
    //Util.Debug("Setting saved '" + name + "=" + val + "'");
185 186 187 188 189
    return val;
},

// Initial page load read/initialization of settings
initSetting: function(name, defVal) {
190
    var val;
191 192

    // Check Query string followed by cookie
193
    val = Util.getQueryVar(name);
194 195 196
    if (val === null) {
        val = Util.readCookie(name, defVal);
    }
197
    DefaultControls.updateSetting(name, val);
198
    //Util.Debug("Setting '" + name + "' initialized to '" + val + "'");
199 200 201 202 203 204 205 206 207
    return val;
},


// Toggle the settings menu:
//   On open, settings are refreshed from saved cookies.
//   On close, settings are applied
clickSettingsMenu: function() {
    var DC = DefaultControls;
208
    if (DC.settingsOpen) {
209 210 211 212 213 214
        DC.settingsApply();

        DC.closeSettingsMenu();
    } else {
        DC.updateSetting('encrypt');
        DC.updateSetting('true_color');
215
        if (DC.rfb.get_canvas().get_cursor_uri()) {
216 217
            DC.updateSetting('cursor');
        } else {
218
            DC.updateSetting('cursor', false);
219 220
            $('VNC_cursor').disabled = true;
        }
221
        DC.updateSetting('connectTimeout');
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
        DC.updateSetting('stylesheet');
        DC.updateSetting('logging');

        DC.openSettingsMenu();
    }
},

// Open menu
openSettingsMenu: function() {
    $('VNC_settings_menu').style.display = "block";
    DefaultControls.settingsOpen = true;
},

// Close menu (without applying settings)
closeSettingsMenu: function() {
    $('VNC_settings_menu').style.display = "none";
    DefaultControls.settingsOpen = false;
},

// Disable/enable controls depending on connection state
242 243
settingsDisabled: function(disabled, rfb) {
    //Util.Debug(">> settingsDisabled");
244 245
    $('VNC_encrypt').disabled = disabled;
    $('VNC_true_color').disabled = disabled;
246
    if (rfb && rfb.get_canvas().get_cursor_uri()) {
247 248
        $('VNC_cursor').disabled = disabled;
    } else {
249
        DefaultControls.updateSetting('cursor', false);
250 251
        $('VNC_cursor').disabled = true;
    }
252
    $('VNC_connectTimeout').disabled = disabled;
253
    //Util.Debug("<< settingsDisabled");
254 255 256 257
},

// Save/apply settings when 'Apply' button is pressed
settingsApply: function() {
258
    //Util.Debug(">> settingsApply");
259
    var DC = DefaultControls;
260 261
    DC.saveSetting('encrypt');
    DC.saveSetting('true_color');
262
    if (DC.rfb.get_canvas().get_cursor_uri()) {
263 264
        DC.saveSetting('cursor');
    }
265
    DC.saveSetting('connectTimeout');
266 267 268 269 270 271 272
    DC.saveSetting('stylesheet');
    DC.saveSetting('logging');

    // Settings with immediate (non-connected related) effect
    Util.selectStylesheet(DC.getSetting('stylesheet'));
    Util.init_logging(DC.getSetting('logging'));

273
    //Util.Debug("<< settingsApply");
274 275 276 277
},



Joel Martin's avatar
Joel Martin committed
278
setPassword: function() {
279
    DefaultControls.rfb.sendPassword($('VNC_password').value);
Joel Martin's avatar
Joel Martin committed
280 281 282
    return false;
},

283
sendCtrlAltDel: function() {
284
    DefaultControls.rfb.sendCtrlAltDel();
285 286
},

287
updateState: function(rfb, state, oldstate, msg) {
288
    var s, sb, c, cad, klass;
289
    s = $('VNC_status');
290
    sb = $('VNC_status_bar');
291
    c = $('VNC_connect_button');
292
    cad = $('sendCtrlAltDelButton');
293 294
    switch (state) {
        case 'failed':
Joel Martin's avatar
Joel Martin committed
295
        case 'fatal':
296
            c.disabled = true;
297
            cad.disabled = true;
298
            DefaultControls.settingsDisabled(true, rfb);
299 300 301 302 303 304
            klass = "VNC_status_error";
            break;
        case 'normal':
            c.value = "Disconnect";
            c.onclick = DefaultControls.disconnect;
            c.disabled = false;
305
            cad.disabled = false;
306
            DefaultControls.settingsDisabled(true, rfb);
307 308 309
            klass = "VNC_status_normal";
            break;
        case 'disconnected':
Joel Martin's avatar
Joel Martin committed
310
        case 'loaded':
311 312 313 314
            c.value = "Connect";
            c.onclick = DefaultControls.connect;

            c.disabled = false;
315
            cad.disabled = true;
316
            DefaultControls.settingsDisabled(false, rfb);
317 318
            klass = "VNC_status_normal";
            break;
Joel Martin's avatar
Joel Martin committed
319 320 321 322 323 324
        case 'password':
            c.value = "Send Password";
            c.onclick = DefaultControls.setPassword;

            c.disabled = false;
            cad.disabled = true;
325
            DefaultControls.settingsDisabled(true, rfb);
Joel Martin's avatar
Joel Martin committed
326 327
            klass = "VNC_status_warn";
            break;
328 329
        default:
            c.disabled = true;
330
            cad.disabled = true;
331
            DefaultControls.settingsDisabled(true, rfb);
332 333 334 335 336 337
            klass = "VNC_status_warn";
            break;
    }

    if (typeof(msg) !== 'undefined') {
        s.setAttribute("class", klass);
338
        sb.setAttribute("class", klass);
339 340 341 342 343
        s.innerHTML = msg;
    }

},

344 345 346 347 348 349 350
clipReceive: function(rfb, text) {
    Util.Debug(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
    $('VNC_clipboard_text').value = text;
    Util.Debug("<< DefaultControls.clipReceive");
},


351
connect: function() {
352 353 354 355
    var host, port, password, DC = DefaultControls;

    DC.closeSettingsMenu();

356 357 358 359
    host = $('VNC_host').value;
    port = $('VNC_port').value;
    password = $('VNC_password').value;
    if ((!host) || (!port)) {
360
        throw("Must set host and port");
361 362
    }

363 364 365
    DC.rfb.set_encrypt(DC.getSetting('encrypt'));
    DC.rfb.set_true_color(DC.getSetting('true_color'));
    DC.rfb.set_local_cursor(DC.getSetting('cursor'));
366
    DC.rfb.set_connectTimeout(DC.getSetting('connectTimeout'));
367

368
    DC.rfb.connect(host, port, password);
369 370 371
},

disconnect: function() {
372 373
    DefaultControls.closeSettingsMenu();

374
    DefaultControls.rfb.disconnect();
375 376
},

377
canvasBlur: function() {
378
    DefaultControls.rfb.get_canvas().set_focused(false);
379 380
},

381
canvasFocus: function() {
382
    DefaultControls.rfb.get_canvas().set_focused(true);
383 384 385 386
},

clipClear: function() {
    $('VNC_clipboard_text').value = "";
387
    DefaultControls.rfb.clipboardPasteFrom("");
388 389 390 391
},

clipSend: function() {
    var text = $('VNC_clipboard_text').value;
392
    Util.Debug(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
393
    DefaultControls.rfb.clipboardPasteFrom(text);
394
    Util.Debug("<< DefaultControls.clipSend");
395 396
}

397
};