Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 1 | angular.module('bmcApp').controller('KvmController', |
| 2 | ['$scope', '$location', '$window', |
| 3 | function($scope, $location, $window) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 4 | |
| 5 | |
| 6 | /*jslint white: false */ |
| 7 | /*global window, $, Util, RFB, */ |
| 8 | "use strict"; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 9 | |
| 10 | var rfb; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 11 | var host = $location.host(); |
| 12 | var port = $location.port(); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 13 | var encrypt = $location.protocol() === 'https'; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 14 | 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 31 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | $scope.$on("$destroy", function() { |
| 35 | if (rfb) { |
| 36 | rfb.disconnect(); |
| 37 | } |
| 38 | }); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 39 | |
| 40 | function UIresize() { |
| 41 | if (WebUtil.getConfigVar('resize', false)) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 42 | var innerW = $window.innerWidth; |
| 43 | var innerH = $window.innerHeight; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 44 | 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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 57 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 58 | function updateState(rfb, state, oldstate, msg) { |
| 59 | var s, sb, cad, level; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 60 | s = angular.element(document.querySelector('#noVNC_status'))[0]; |
| 61 | sb = angular.element(document.querySelector('#noVNC_status_bar'))[0]; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 62 | 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 71 | if (typeof(msg) !== 'undefined') { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 72 | // 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 83 | var resizeTimeout; |
| 84 | $window.onresize = function () { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 85 | // 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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 92 | }; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame^] | 93 | }]); |