blob: e3dcb1465fb4531c5ca272215660279b641dc3ca [file] [log] [blame]
Ed Tanousc81ca422017-03-21 16:18:49 -07001"use strict";
Ed Tanous1ccd57c2017-03-21 13:15:58 -07002angular.module('bmcApp').controller('KvmController',
3['$scope', '$location', '$window',
4function($scope, $location, $window) {
Ed Tanous904063f2017-03-02 16:48:24 -08005
Ed Tanous904063f2017-03-02 16:48:24 -08006 /*jslint white: false */
7 /*global window, $, Util, RFB, */
Ed Tanous904063f2017-03-02 16:48:24 -08008
Ed Tanousc81ca422017-03-21 16:18:49 -07009 var desktopName;
10
11 WebUtil.init_logging(WebUtil.getConfigVar('logging', 'debug'));
Ed Tanous904063f2017-03-02 16:48:24 -080012 var rfb;
Ed Tanousc4771fb2017-03-13 13:39:49 -070013 var host = $location.host();
14 var port = $location.port();
Ed Tanous1ccd57c2017-03-21 13:15:58 -070015 var encrypt = $location.protocol() === 'https';
Ed Tanousc4771fb2017-03-13 13:39:49 -070016 var password = "";
17 var token = "1234";
18 var path = "kvmws";
19 var target = angular.element(document.querySelector('#noVNC_canvas'))[0];
20 try {
21 rfb = new RFB({'target': target,
22 'encrypt': encrypt,
23 'local_cursor': true,
24 'onUpdateState': updateState,
25 //'onXvpInit': xvpInit,
26 'onFBUComplete': FBUComplete,
Ed Tanousb4d29f42017-03-24 16:39:25 -070027 'onDesktopName': updateDesktopName,
28 'foobar': 1
Ed Tanousc81ca422017-03-21 16:18:49 -070029 });
Ed Tanousc4771fb2017-03-13 13:39:49 -070030 rfb.connect(host, port, password, path);
31 } catch (exc) {
32 updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
33 return; // don't continue trying to connect
Ed Tanousc81ca422017-03-21 16:18:49 -070034 };
Ed Tanous904063f2017-03-02 16:48:24 -080035
Ed Tanousc4771fb2017-03-13 13:39:49 -070036
37
38 $scope.$on("$destroy", function() {
39 if (rfb) {
40 rfb.disconnect();
41 }
42 });
Ed Tanous904063f2017-03-02 16:48:24 -080043
44 function UIresize() {
45 if (WebUtil.getConfigVar('resize', false)) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070046 var innerW = $window.innerWidth;
47 var innerH = $window.innerHeight;
Ed Tanous904063f2017-03-02 16:48:24 -080048 var padding = 5;
49 if (innerW !== undefined && innerH !== undefined)
50 rfb.requestDesktopSize(innerW, innerH - controlbarH - padding);
51 }
Ed Tanousc81ca422017-03-21 16:18:49 -070052 };
53 function updateDesktopName(rfb, name) {
54 desktopName = name;
55 };
Ed Tanous904063f2017-03-02 16:48:24 -080056 function FBUComplete(rfb, fbu) {
57 UIresize();
58 rfb.set_onFBUComplete(function() { });
Ed Tanousc81ca422017-03-21 16:18:49 -070059 };
Ed Tanous904063f2017-03-02 16:48:24 -080060 function sendCtrlAltDel() {
61 rfb.sendCtrlAltDel();
62 return false;
Ed Tanousc81ca422017-03-21 16:18:49 -070063 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070064
Ed Tanousc81ca422017-03-21 16:18:49 -070065 function status(text, level) {
66 switch (level) {
67 case 'normal':
68 case 'warn':
69 case 'error':
70 break;
71 default:
72 level = "warn";
73 }
74 angular.element(document.querySelector('#noVNC_status'))[0].textContent = text;
75 angular.element(document.querySelector('#noVNC_status_bar'))[0].setAttribute("class", "noVNC_status_" + level);
76 };
77
78 function updateState(rfb, state, oldstate) {
Ed Tanous904063f2017-03-02 16:48:24 -080079 switch (state) {
Ed Tanousc81ca422017-03-21 16:18:49 -070080 case 'connecting':
81 status("Connecting", "normal");
82 break;
83 case 'connected':
84 if (rfb && rfb.get_encrypt()) {
85 status("Connected (encrypted) to " +
86 desktopName, "normal");
87 } else {
88 status("Connected (unencrypted) to " +
89 desktopName, "normal");
90 }
91 break;
92 case 'disconnecting':
93 status("Disconnecting", "normal");
94 break;
95 case 'disconnected':
96 status("Disconnected", "normal");
97 break;
98 default:
99 status(state, "warn");
100 break;
Ed Tanous904063f2017-03-02 16:48:24 -0800101 }
102
Ed Tanousc81ca422017-03-21 16:18:49 -0700103 };
Ed Tanous904063f2017-03-02 16:48:24 -0800104
Ed Tanousc4771fb2017-03-13 13:39:49 -0700105 var resizeTimeout;
106 $window.onresize = function () {
Ed Tanous904063f2017-03-02 16:48:24 -0800107 // When the window has been resized, wait until the size remains
108 // the same for 0.5 seconds before sending the request for changing
109 // the resolution of the session
110 clearTimeout(resizeTimeout);
111 resizeTimeout = setTimeout(function(){
112 UIresize();
113 }, 500);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700114 };
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700115}]);