blob: 4f692f142ffee7dc38ccd6fc6712df39e28e63f5 [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 $scope.$on("$destroy", function() {
37 if (rfb) {
38 rfb.disconnect();
39 }
40 });
Ed Tanous904063f2017-03-02 16:48:24 -080041
42 function UIresize() {
43 if (WebUtil.getConfigVar('resize', false)) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070044 var innerW = $window.innerWidth;
45 var innerH = $window.innerHeight;
Ed Tanous904063f2017-03-02 16:48:24 -080046 var padding = 5;
47 if (innerW !== undefined && innerH !== undefined)
48 rfb.requestDesktopSize(innerW, innerH - controlbarH - padding);
49 }
Ed Tanousc81ca422017-03-21 16:18:49 -070050 };
51 function updateDesktopName(rfb, name) {
52 desktopName = name;
53 };
Ed Tanous904063f2017-03-02 16:48:24 -080054 function FBUComplete(rfb, fbu) {
55 UIresize();
56 rfb.set_onFBUComplete(function() { });
Ed Tanousc81ca422017-03-21 16:18:49 -070057 };
Ed Tanous904063f2017-03-02 16:48:24 -080058 function sendCtrlAltDel() {
59 rfb.sendCtrlAltDel();
60 return false;
Ed Tanousc81ca422017-03-21 16:18:49 -070061 };
Ed Tanousc4771fb2017-03-13 13:39:49 -070062
Ed Tanousc81ca422017-03-21 16:18:49 -070063 function status(text, level) {
Ed Tanous8041f312017-04-03 09:47:01 -070064 var status_bar = angular.element(document.querySelector('#noVNC_status_bar'))[0];
65 // Need to check if the status bar still exists. On page change, it gets destroyed
66 // when we swap to a different view. The system will disconnect async
67 if (status_bar){
68 status_bar.textContent = text;
69 }
70
71 var status = angular.element(document.querySelector('#noVNC_status'))[0];
Ed Tanousc81ca422017-03-21 16:18:49 -070072 switch (level) {
73 case 'normal':
74 case 'warn':
75 case 'error':
76 break;
77 default:
78 level = "warn";
79 }
Ed Tanous8041f312017-04-03 09:47:01 -070080 if (status){
81 status.setAttribute("class", "noVNC_status_" + level);
82 }
Ed Tanousc81ca422017-03-21 16:18:49 -070083 };
84
85 function updateState(rfb, state, oldstate) {
Ed Tanous904063f2017-03-02 16:48:24 -080086 switch (state) {
Ed Tanousc81ca422017-03-21 16:18:49 -070087 case 'connecting':
88 status("Connecting", "normal");
89 break;
90 case 'connected':
91 if (rfb && rfb.get_encrypt()) {
92 status("Connected (encrypted) to " +
93 desktopName, "normal");
94 } else {
95 status("Connected (unencrypted) to " +
96 desktopName, "normal");
97 }
98 break;
99 case 'disconnecting':
100 status("Disconnecting", "normal");
101 break;
102 case 'disconnected':
103 status("Disconnected", "normal");
104 break;
105 default:
106 status(state, "warn");
107 break;
Ed Tanous904063f2017-03-02 16:48:24 -0800108 }
109
Ed Tanousc81ca422017-03-21 16:18:49 -0700110 };
Ed Tanous904063f2017-03-02 16:48:24 -0800111
Ed Tanousc4771fb2017-03-13 13:39:49 -0700112 var resizeTimeout;
113 $window.onresize = function () {
Ed Tanous904063f2017-03-02 16:48:24 -0800114 // When the window has been resized, wait until the size remains
115 // the same for 0.5 seconds before sending the request for changing
116 // the resolution of the session
117 clearTimeout(resizeTimeout);
118 resizeTimeout = setTimeout(function(){
119 UIresize();
120 }, 500);
Ed Tanousc4771fb2017-03-13 13:39:49 -0700121 };
Ed Tanous1ccd57c2017-03-21 13:15:58 -0700122}]);