blob: d10a223e934fc2b25b907b60055931e7d6397c56 [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 Tanousc81ca422017-03-21 16:18:49 -070027 'onDesktopName': updateDesktopName
28 });
Ed Tanousc4771fb2017-03-13 13:39:49 -070029 rfb.connect(host, port, password, path);
30 } catch (exc) {
31 updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
32 return; // don't continue trying to connect
Ed Tanousc81ca422017-03-21 16:18:49 -070033 };
Ed Tanous904063f2017-03-02 16:48:24 -080034
Ed Tanousc4771fb2017-03-13 13:39:49 -070035
36
37 $scope.$on("$destroy", function() {
38 if (rfb) {
39 rfb.disconnect();
40 }
41 });
Ed Tanous904063f2017-03-02 16:48:24 -080042
43 function UIresize() {
44 if (WebUtil.getConfigVar('resize', false)) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070045 var innerW = $window.innerWidth;
46 var innerH = $window.innerHeight;
Ed Tanous904063f2017-03-02 16:48:24 -080047 var padding = 5;
48 if (innerW !== undefined && innerH !== undefined)
49 rfb.requestDesktopSize(innerW, innerH - controlbarH - padding);
50 }
Ed Tanousc81ca422017-03-21 16:18:49 -070051 };
52 function updateDesktopName(rfb, name) {
53 desktopName = name;
54 };
Ed Tanous904063f2017-03-02 16:48:24 -080055 function FBUComplete(rfb, fbu) {
56 UIresize();
57 rfb.set_onFBUComplete(function() { });
Ed Tanousc81ca422017-03-21 16:18:49 -070058 };
Ed Tanous904063f2017-03-02 16:48:24 -080059 function sendCtrlAltDel() {
60 rfb.sendCtrlAltDel();
61 return false;
Ed Tanousc81ca422017-03-21 16:18:49 -070062 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070063
Ed Tanousc81ca422017-03-21 16:18:49 -070064 function status(text, level) {
65 switch (level) {
66 case 'normal':
67 case 'warn':
68 case 'error':
69 break;
70 default:
71 level = "warn";
72 }
73 angular.element(document.querySelector('#noVNC_status'))[0].textContent = text;
74 angular.element(document.querySelector('#noVNC_status_bar'))[0].setAttribute("class", "noVNC_status_" + level);
75 };
76
77 function updateState(rfb, state, oldstate) {
Ed Tanous904063f2017-03-02 16:48:24 -080078 switch (state) {
Ed Tanousc81ca422017-03-21 16:18:49 -070079 case 'connecting':
80 status("Connecting", "normal");
81 break;
82 case 'connected':
83 if (rfb && rfb.get_encrypt()) {
84 status("Connected (encrypted) to " +
85 desktopName, "normal");
86 } else {
87 status("Connected (unencrypted) to " +
88 desktopName, "normal");
89 }
90 break;
91 case 'disconnecting':
92 status("Disconnecting", "normal");
93 break;
94 case 'disconnected':
95 status("Disconnected", "normal");
96 break;
97 default:
98 status(state, "warn");
99 break;
Ed Tanous904063f2017-03-02 16:48:24 -0800100 }
101
Ed Tanousc81ca422017-03-21 16:18:49 -0700102 };
Ed Tanous904063f2017-03-02 16:48:24 -0800103
Ed Tanousc4771fb2017-03-13 13:39:49 -0700104 var resizeTimeout;
105 $window.onresize = function () {
Ed Tanous904063f2017-03-02 16:48:24 -0800106 // When the window has been resized, wait until the size remains
107 // the same for 0.5 seconds before sending the request for changing
108 // the resolution of the session
109 clearTimeout(resizeTimeout);
110 resizeTimeout = setTimeout(function(){
111 UIresize();
112 }, 500);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700113 };
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700114}]);