blob: b8b442e006d15f842aa4f3dfc34779269a9c0405 [file] [log] [blame]
Ed Tanous1ccd57c2017-03-21 13:15:58 -07001angular.module('bmcApp').controller('KvmController',
2['$scope', '$location', '$window',
3function($scope, $location, $window) {
Ed Tanous904063f2017-03-02 16:48:24 -08004
5
6 /*jslint white: false */
7 /*global window, $, Util, RFB, */
8 "use strict";
Ed Tanous904063f2017-03-02 16:48:24 -08009
10 var rfb;
Ed Tanousc4771fb2017-03-13 13:39:49 -070011 var host = $location.host();
12 var port = $location.port();
Ed Tanous1ccd57c2017-03-21 13:15:58 -070013 var encrypt = $location.protocol() === 'https';
Ed Tanousc4771fb2017-03-13 13:39:49 -070014 var password = "";
15 var token = "1234";
16 var path = "kvmws";
17 var target = angular.element(document.querySelector('#noVNC_canvas'))[0];
18 try {
19 rfb = new RFB({'target': target,
20 'encrypt': encrypt,
21 'local_cursor': true,
22 'onUpdateState': updateState,
23 //'onXvpInit': xvpInit,
24 'onFBUComplete': FBUComplete,
25 'resize': true});
26 rfb.connect(host, port, password, path);
27 } catch (exc) {
28 updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
29 return; // don't continue trying to connect
30 }
Ed Tanous904063f2017-03-02 16:48:24 -080031
Ed Tanousc4771fb2017-03-13 13:39:49 -070032
33
34 $scope.$on("$destroy", function() {
35 if (rfb) {
36 rfb.disconnect();
37 }
38 });
Ed Tanous904063f2017-03-02 16:48:24 -080039
40 function UIresize() {
41 if (WebUtil.getConfigVar('resize', false)) {
Ed Tanousc4771fb2017-03-13 13:39:49 -070042 var innerW = $window.innerWidth;
43 var innerH = $window.innerHeight;
Ed Tanous904063f2017-03-02 16:48:24 -080044 var padding = 5;
45 if (innerW !== undefined && innerH !== undefined)
46 rfb.requestDesktopSize(innerW, innerH - controlbarH - padding);
47 }
48 }
49 function FBUComplete(rfb, fbu) {
50 UIresize();
51 rfb.set_onFBUComplete(function() { });
52 }
53 function sendCtrlAltDel() {
54 rfb.sendCtrlAltDel();
55 return false;
56 }
Ed Tanousc4771fb2017-03-13 13:39:49 -070057
Ed Tanous904063f2017-03-02 16:48:24 -080058 function updateState(rfb, state, oldstate, msg) {
59 var s, sb, cad, level;
Ed Tanousc4771fb2017-03-13 13:39:49 -070060 s = angular.element(document.querySelector('#noVNC_status'))[0];
61 sb = angular.element(document.querySelector('#noVNC_status_bar'))[0];
Ed Tanous904063f2017-03-02 16:48:24 -080062 switch (state) {
63 case 'failed': level = "error"; break;
64 case 'fatal': level = "error"; break;
65 case 'normal': level = "normal"; break;
66 case 'disconnected': level = "normal"; break;
67 case 'loaded': level = "normal"; break;
68 default: level = "warn"; break;
69 }
70
Ed Tanous904063f2017-03-02 16:48:24 -080071 if (typeof(msg) !== 'undefined') {
Ed Tanousc4771fb2017-03-13 13:39:49 -070072 // at this point, it's possible the window has already been destroyed, so make sure
73 // the handles exist before writing.
74 if (typeof(sb) !== 'undefined'){
75 sb.setAttribute("class", "noVNC_status_" + level);
76 }
77 if (typeof(sb) !== 'undefined'){
78 s.innerHTML = msg;
79 }
Ed Tanous904063f2017-03-02 16:48:24 -080080 }
81 }
82
Ed Tanousc4771fb2017-03-13 13:39:49 -070083 var resizeTimeout;
84 $window.onresize = function () {
Ed Tanous904063f2017-03-02 16:48:24 -080085 // When the window has been resized, wait until the size remains
86 // the same for 0.5 seconds before sending the request for changing
87 // the resolution of the session
88 clearTimeout(resizeTimeout);
89 resizeTimeout = setTimeout(function(){
90 UIresize();
91 }, 500);
Ed Tanousc4771fb2017-03-13 13:39:49 -070092 };
Ed Tanous1ccd57c2017-03-21 13:15:58 -070093}]);