default_controls.js 12.3 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 54 55 56 57 58
    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();
59
    for (i = 0; i < sheets.length; i += 1) {
60 61 62 63 64 65 66
        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'];
67
    for (i = 0; i < llevels.length; i += 1) {
68 69 70 71 72 73 74 75 76 77 78
        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"';
79
    html += '          id="sendCtrlAltDelButton"';
80 81 82
    html += '          onclick="DefaultControls.sendCtrlAltDel();"></div></td>';
    html += '    </tr></table>';
    html += '  </div>';
83 84 85 86 87 88 89 90 91 92 93 94
    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';
95 96
    html += '    onfocus="DefaultControls.canvasBlur();"';
    html += '    onblur="DefaultControls.canvasFocus();"';
97 98 99 100
    html += '    onchange="DefaultControls.clipSend();"></textarea>';
    html += '</div>';
    $(target).innerHTML = html;

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

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

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

117 118 119 120 121
    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
122
    $('VNC_screen').onmousemove = function () {
123 124
            var canvas = DC.rfb.get_canvas();
            if ((! canvas) || (! canvas.get_focused())) {
Joel Martin's avatar
Joel Martin committed
125 126 127
                $('VNC_clipboard_text').blur();
            }
        };
128

129 130
},

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
// 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') {
159
        for (i = 0; i < ctrl.options.length; i += 1) {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            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);
181
    //Util.Debug("Setting saved '" + name + "=" + val + "'");
182 183 184 185 186
    return val;
},

// Initial page load read/initialization of settings
initSetting: function(name, defVal) {
187
    var val;
188 189

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


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

        DC.closeSettingsMenu();
    } else {
        DC.updateSetting('encrypt');
        DC.updateSetting('true_color');
212
        if (DC.rfb.get_canvas().get_cursor_uri()) {
213 214
            DC.updateSetting('cursor');
        } else {
215
            DC.updateSetting('cursor', false);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            $('VNC_cursor').disabled = true;
        }
        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
238 239
settingsDisabled: function(disabled, rfb) {
    //Util.Debug(">> settingsDisabled");
240 241
    $('VNC_encrypt').disabled = disabled;
    $('VNC_true_color').disabled = disabled;
242
    if (rfb && rfb.get_canvas().get_cursor_uri()) {
243 244
        $('VNC_cursor').disabled = disabled;
    } else {
245
        DefaultControls.updateSetting('cursor', false);
246 247
        $('VNC_cursor').disabled = true;
    }
248
    //Util.Debug("<< settingsDisabled");
249 250 251 252
},

// Save/apply settings when 'Apply' button is pressed
settingsApply: function() {
253
    //Util.Debug(">> settingsApply");
254
    var DC = DefaultControls;
255 256
    DC.saveSetting('encrypt');
    DC.saveSetting('true_color');
257
    if (DC.rfb.get_canvas().get_cursor_uri()) {
258 259 260 261 262 263 264 265 266
        DC.saveSetting('cursor');
    }
    DC.saveSetting('stylesheet');
    DC.saveSetting('logging');

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

267
    //Util.Debug("<< settingsApply");
268 269 270 271
},



Joel Martin's avatar
Joel Martin committed
272
setPassword: function() {
273
    DefaultControls.rfb.sendPassword($('VNC_password').value);
Joel Martin's avatar
Joel Martin committed
274 275 276
    return false;
},

277
sendCtrlAltDel: function() {
278
    DefaultControls.rfb.sendCtrlAltDel();
279 280
},

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

            c.disabled = false;
309
            cad.disabled = true;
310
            DefaultControls.settingsDisabled(false, rfb);
311 312
            klass = "VNC_status_normal";
            break;
Joel Martin's avatar
Joel Martin committed
313 314 315 316 317 318
        case 'password':
            c.value = "Send Password";
            c.onclick = DefaultControls.setPassword;

            c.disabled = false;
            cad.disabled = true;
319
            DefaultControls.settingsDisabled(true, rfb);
Joel Martin's avatar
Joel Martin committed
320 321
            klass = "VNC_status_warn";
            break;
322 323
        default:
            c.disabled = true;
324
            cad.disabled = true;
325
            DefaultControls.settingsDisabled(true, rfb);
326 327 328 329 330 331
            klass = "VNC_status_warn";
            break;
    }

    if (typeof(msg) !== 'undefined') {
        s.setAttribute("class", klass);
332
        sb.setAttribute("class", klass);
333 334 335 336 337
        s.innerHTML = msg;
    }

},

338 339 340 341 342 343 344
clipReceive: function(rfb, text) {
    Util.Debug(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
    $('VNC_clipboard_text').value = text;
    Util.Debug("<< DefaultControls.clipReceive");
},


345
connect: function() {
346 347 348 349
    var host, port, password, DC = DefaultControls;

    DC.closeSettingsMenu();

350 351 352 353
    host = $('VNC_host').value;
    port = $('VNC_port').value;
    password = $('VNC_password').value;
    if ((!host) || (!port)) {
354
        throw("Must set host and port");
355 356
    }

357 358 359
    DC.rfb.set_encrypt(DC.getSetting('encrypt'));
    DC.rfb.set_true_color(DC.getSetting('true_color'));
    DC.rfb.set_local_cursor(DC.getSetting('cursor'));
360

361
    DC.rfb.connect(host, port, password);
362 363 364
},

disconnect: function() {
365 366
    DefaultControls.closeSettingsMenu();

367
    DefaultControls.rfb.disconnect();
368 369
},

370
canvasBlur: function() {
371
    DefaultControls.rfb.get_canvas().set_focused(false);
372 373
},

374
canvasFocus: function() {
375
    DefaultControls.rfb.get_canvas().set_focused(true);
376 377 378 379
},

clipClear: function() {
    $('VNC_clipboard_text').value = "";
380
    DefaultControls.rfb.clipboardPasteFrom("");
381 382 383 384
},

clipSend: function() {
    var text = $('VNC_clipboard_text').value;
385
    Util.Debug(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
386
    DefaultControls.rfb.clipboardPasteFrom(text);
387
    Util.Debug("<< DefaultControls.clipSend");
388 389
}

390
};