Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 1 | "use strict"; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 2 | angular.module('bmcApp').controller('KvmController', |
| 3 | ['$scope', '$location', '$window', |
| 4 | function($scope, $location, $window) { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 5 | |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 6 | /*jslint white: false */ |
| 7 | /*global window, $, Util, RFB, */ |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 8 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 9 | var desktopName; |
| 10 | |
| 11 | WebUtil.init_logging(WebUtil.getConfigVar('logging', 'debug')); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 12 | var rfb; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 13 | var host = $location.host(); |
| 14 | var port = $location.port(); |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 15 | var encrypt = $location.protocol() === 'https'; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 16 | 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 Tanous | b4d29f4 | 2017-03-24 16:39:25 -0700 | [diff] [blame^] | 27 | 'onDesktopName': updateDesktopName, |
| 28 | 'foobar': 1 |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 29 | }); |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 30 | 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 Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 34 | }; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 35 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | $scope.$on("$destroy", function() { |
| 39 | if (rfb) { |
| 40 | rfb.disconnect(); |
| 41 | } |
| 42 | }); |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 43 | |
| 44 | function UIresize() { |
| 45 | if (WebUtil.getConfigVar('resize', false)) { |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 46 | var innerW = $window.innerWidth; |
| 47 | var innerH = $window.innerHeight; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 48 | var padding = 5; |
| 49 | if (innerW !== undefined && innerH !== undefined) |
| 50 | rfb.requestDesktopSize(innerW, innerH - controlbarH - padding); |
| 51 | } |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 52 | }; |
| 53 | function updateDesktopName(rfb, name) { |
| 54 | desktopName = name; |
| 55 | }; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 56 | function FBUComplete(rfb, fbu) { |
| 57 | UIresize(); |
| 58 | rfb.set_onFBUComplete(function() { }); |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 59 | }; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 60 | function sendCtrlAltDel() { |
| 61 | rfb.sendCtrlAltDel(); |
| 62 | return false; |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 63 | }; |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 64 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 65 | 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 79 | switch (state) { |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 80 | 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 Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Ed Tanous | c81ca42 | 2017-03-21 16:18:49 -0700 | [diff] [blame] | 103 | }; |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 104 | |
Ed Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 105 | var resizeTimeout; |
| 106 | $window.onresize = function () { |
Ed Tanous | 904063f | 2017-03-02 16:48:24 -0800 | [diff] [blame] | 107 | // 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 Tanous | c4771fb | 2017-03-13 13:39:49 -0700 | [diff] [blame] | 114 | }; |
Ed Tanous | 1ccd57c | 2017-03-21 13:15:58 -0700 | [diff] [blame] | 115 | }]); |