Fix error when sorting Job list by object.
[ganeti_webmgr.git] / ganeti_web / static / js / noVNC.js
blobdce43ec7239c6204c793e9fe9b0e468178cc8157
1 /*
2 * Copyright (C) 2010 Oregon State University et al.
3 *
4 * Javascript for custom noVNC interface.
5 */
7 var PROXY_REQUEST_URI;
8 var POPOUT_URL;
10 $(function () {
12 var popout = $('#popout');
13 var connect = $('#connect');
14 var encrypt = $('#encrypt');
15 var encrypt_check = $('#encrypt_check');
16 var ctrlaltdelete = $('#ctrlaltdelete');
17 var vnc_errors = $("#vnc_errors");
18 var vnc_status_bar = $("#VNC_status_bar");
19 var vnc_canvas = $('#VNC_canvas');
20 var vnc_screen = $('#VNC_screen');
22 // XXX remap document.write to a dom function so that it works after DOM is
23 // loaded function will be reset after noVNC scripts are loaded.
24 var old = document.write;
25 document.write = function(str) {$(document).append(str)};
26 document.write('<script type="text/javascript" src="'+INCLUDE_URI+'vnc.js"><//script>');
27 document.write = old;
29 // XXX manually call __initialize(). This normally happens onload, but
30 // won't work for us since document may have been loaded already
31 if (!Websock_native) {WebSocket.__initialize();}
33 var rfb;
34 var host, port, password; // VNC proxy connection settings
35 var connected = false;
37 // When the VNC Canvas is resized, this is called and resizes
38 // the window if it's a popup
39 function resize_popup() {
40 // Height will be VNC_canvas + a little bit
41 var cHeight = vnc_canvas.height();
42 // Width will be VNC_canvas + a little bit
43 var cWidth = vnc_canvas.width();
45 // Without this next line, the status hangs off to the right
46 // of the buttons. That looks really bad.
47 vnc_status_bar.width(cWidth);
49 // Resize
50 vnc_screen.width(cWidth);
51 window.resizeTo(cWidth + 50, cHeight + 100);
54 connect.click(function(event) {
55 event.preventDefault();
56 var $this = $(this);
57 if($this.hasClass('enabled')) {
58 rfb.disconnect();
59 connected = false;
60 } else {
61 connected = true;
62 start();
65 // When connecting, change the window size to have
66 // the canvas fully viewable
67 resize_popup();
68 });
70 encrypt.click(function(event){
71 event.preventDefault();
72 var $this = $(this);
73 if (!connected) {
74 if ($this.hasClass('enabled')){
75 encrypt_check.attr('checked',false);
76 $this.removeClass('enabled')
77 } else {
78 encrypt_check.attr('checked',true);
79 $this.addClass('enabled')
82 });
84 ctrlaltdelete
85 .click(function(event){
86 event.preventDefault();
87 if (!$(this).hasClass('disabled')) {
88 rfb.sendCtrlAltDel();
90 });
92 if (POPOUT_URL != undefined) {
93 popout.click(function(event) {
94 event.preventDefault();
95 var url;
96 if (rfb == undefined) {
97 url = POPOUT_URL;
98 } else {
99 url = POPOUT_URL + '?auto_connect=1';
100 stop();
102 window.open(url, 'popout', 'height=550,width=775,status=no,toolbar=no,menubar=no,location=no');
106 // users exits the page by following link or closing the tab or sth else
107 $(window).bind("unload", stop);
110 function show_errors() {
111 if (host===false || port===false || password===false) {
112 connected = false;
113 vnc_status_bar
114 .attr("class", "VNC_status_error")
115 .html("Probably your proxy is not running or some errors occured. Try again.");
116 return false;
118 return true;
121 function updateState(rfb, state, oldstate, msg) {
122 var klass;
123 switch (state) {
124 case 'failed':
125 case 'fatal':
126 klass = "VNC_status_error";
127 break;
128 case 'normal':
129 klass = "VNC_status_normal";
130 break;
131 case 'disconnected':
132 case 'loaded':
133 klass = "VNC_status_normal";
134 break;
135 case 'password':
136 msg = 'Password required';
137 klass = "VNC_status_warn";
138 break;
139 default:
140 klass = "VNC_status_warn";
143 if (state == "normal") {
144 connected = true;
145 connect
146 .addClass('enabled')
147 .text('Disconnect');
148 ctrlaltdelete.removeClass('disabled');
149 vnc_canvas.addClass("connected");
150 } else {
151 vnc_canvas.removeClass("connected");
152 connected = false;
153 connect
154 .removeClass('enabled')
155 .text('Connect');
156 ctrlaltdelete.addClass('disabled')
159 if (msg != undefined) {
160 vnc_status_bar
161 .attr("class", klass)
162 .html(msg);
166 function stop() {
167 if (rfb != undefined) {
168 rfb.disconnect();
169 rfb = undefined;
173 function start() {
174 vnc_errors.hide();
176 if (encrypt_check.attr('checked')) {
177 is_encrypted = true;
178 data = {"tls": true};
179 } else {
180 is_encrypted = false;
181 data = {};
184 $.ajax({
185 "async": false,
186 "url": PROXY_REQUEST_URI,
187 "type": "POST",
188 "data": data,
189 "dataType": "json",
190 "success": function(data){
191 host = data[0];
192 port = data[1];
193 password = data[2];
196 if (!show_errors()) return false;
198 rfb = new RFB({
199 // jQuery doesn't work with that, need to stick
200 // to pure DOM
201 'target': document.getElementById('VNC_canvas'),
202 'encrypt': is_encrypted,
203 'true_color': true,
204 'local_cursor': true,
205 'shared': true,
206 'updateState': updateState
208 rfb.connect(host, port, password);
209 return false;