summaryrefslogtreecommitdiffstats
path: root/static/js/kvmController.js
blob: 4f692f142ffee7dc38ccd6fc6712df39e28e63f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use strict";
angular.module('bmcApp').controller('KvmController', 
['$scope', '$location', '$window',
function($scope, $location, $window) {

    /*jslint white: false */
    /*global window, $, Util, RFB, */

    var desktopName;

    WebUtil.init_logging(WebUtil.getConfigVar('logging', 'debug'));
    var rfb;
    var host = $location.host();
    var port = $location.port();
    var encrypt = $location.protocol() === 'https';
    var password = "";
    var token = "1234";
    var path = "kvmws";
    var target = angular.element(document.querySelector('#noVNC_canvas'))[0];
    try {
        rfb = new RFB({'target':        target,
                        'encrypt':      encrypt,
                        'local_cursor': true,
                        'onUpdateState':  updateState,
                        //'onXvpInit':    xvpInit,
                        'onFBUComplete': FBUComplete,
                        'onDesktopName': updateDesktopName,
                        'foobar': 1
                        });
        rfb.connect(host, port, password, path);
    } catch (exc) {
        updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
        return; // don't continue trying to connect
    };

    $scope.$on("$destroy", function() {
        if (rfb) {
            rfb.disconnect();
        }
    });

    function UIresize() {
        if (WebUtil.getConfigVar('resize', false)) {
            var innerW = $window.innerWidth;
            var innerH = $window.innerHeight;
            var padding = 5;
            if (innerW !== undefined && innerH !== undefined)
                rfb.requestDesktopSize(innerW, innerH - controlbarH - padding);
        }
    };
    function updateDesktopName(rfb, name) {
        desktopName = name;
    };
    function FBUComplete(rfb, fbu) {
        UIresize();
        rfb.set_onFBUComplete(function() { });
    };
    function sendCtrlAltDel() {
        rfb.sendCtrlAltDel();
        return false;
    };

    function status(text, level) {
        var status_bar = angular.element(document.querySelector('#noVNC_status_bar'))[0];
        // Need to check if the status bar still exists.  On page change, it gets destroyed
        // when we swap to a different view.  The system will disconnect async
        if (status_bar){
            status_bar.textContent = text;
        }

        var status = angular.element(document.querySelector('#noVNC_status'))[0];
        switch (level) {
            case 'normal':
            case 'warn':
            case 'error':
                break;
            default:
                level = "warn";
        }
        if (status){
            status.setAttribute("class", "noVNC_status_" + level);
        }
    };

    function updateState(rfb, state, oldstate) {
        switch (state) {
            case 'connecting':
                status("Connecting", "normal");
                break;
            case 'connected':
                if (rfb && rfb.get_encrypt()) {
                    status("Connected (encrypted) to " +
                            desktopName, "normal");
                } else {
                    status("Connected (unencrypted) to " +
                            desktopName, "normal");
                }
                break;
            case 'disconnecting':
                status("Disconnecting", "normal");
                break;
            case 'disconnected':
                status("Disconnected", "normal");
                break;
            default:
                status(state, "warn");
                break;
        }

    };

    var resizeTimeout;
    $window.onresize = function () {
        // When the window has been resized, wait until the size remains
        // the same for 0.5 seconds before sending the request for changing
        // the resolution of the session
        clearTimeout(resizeTimeout);
        resizeTimeout = setTimeout(function(){
            UIresize();
        }, 500);
    }; 
}]);
OpenPOWER on IntegriCloud