default_controls.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (see LICENSE.LGPL-3)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*global console, $, RFB, Canvas, VNC_uri_prefix, Element, Fx */
  10. // Load mootools
  11. (function () {
  12. var pre = (typeof VNC_uri_prefix !== "undefined") ?
  13. VNC_uri_prefix : "include/";
  14. document.write("<script src='" + pre + "mootools.js'><\/script>");
  15. }());
  16. var DefaultControls = {
  17. load: function(target) {
  18. var url, html;
  19. /* Handle state updates */
  20. RFB.setUpdateState(DefaultControls.updateState);
  21. RFB.setClipboardReceive(DefaultControls.clipReceive);
  22. /* Populate the 'target' DOM element with default controls */
  23. if (!target) { target = 'vnc'; }
  24. html = "";
  25. html += '<div id="VNC_controls">';
  26. html += ' <ul>';
  27. html += ' <li>Host: <input id="VNC_host"></li>';
  28. html += ' <li>Port: <input id="VNC_port"></li>';
  29. html += ' <li>Password: <input id="VNC_password"';
  30. html += ' type="password"></li>';
  31. html += ' <li>Encrypt: <input id="VNC_encrypt"';
  32. html += ' type="checkbox"></li>';
  33. html += ' <li>True Color: <input id="VNC_true_color"';
  34. html += ' type="checkbox" checked></li>';
  35. html += ' <li><input id="VNC_connect_button" type="button"';
  36. html += ' value="Loading" disabled></li>';
  37. html += ' </ul>';
  38. html += '</div>';
  39. html += '<div id="VNC_screen">';
  40. html += ' <div id="VNC_status_bar" class="VNC_status_bar" style="margin-top: 0px;">';
  41. html += ' <table border=0 width=100%><tr>';
  42. html += ' <td><div id="VNC_status">Loading</div></td>';
  43. html += ' <td width=10%><div id="VNC_buttons">';
  44. html += ' <input type=button value="Send CtrlAltDel"';
  45. html += ' onclick="DefaultControls.sendCtrlAltDel();"></div></td>';
  46. html += ' </tr></table>';
  47. html += ' </div>';
  48. html += ' <canvas id="VNC_canvas" width="640px" height="20px">';
  49. html += ' Canvas not supported.';
  50. html += ' </canvas>';
  51. html += '</div>';
  52. html += '<br><br>';
  53. html += '<div id="VNC_clipboard">';
  54. html += ' VNC Clipboard:';
  55. html += ' <input id="VNC_clipboard_clear_button"';
  56. html += ' type="button" value="Clear"';
  57. html += ' onclick="DefaultControls.clipClear();">';
  58. html += ' <br>';
  59. html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5';
  60. html += ' onfocus="DefaultControls.clipFocus();"';
  61. html += ' onblur="DefaultControls.clipBlur();"';
  62. html += ' onchange="DefaultControls.clipSend();"></textarea>';
  63. html += '</div>';
  64. $(target).innerHTML = html;
  65. /* Populate the controls if defaults are provided in the URL */
  66. url = document.location.href;
  67. $('VNC_host').value = (url.match(/host=([A-Za-z0-9.\-]*)/) ||
  68. ['',''])[1];
  69. $('VNC_port').value = (url.match(/port=([0-9]*)/) ||
  70. ['',''])[1];
  71. $('VNC_password').value = (url.match(/password=([^&#]*)/) ||
  72. ['',''])[1];
  73. $('VNC_encrypt').checked = (url.match(/encrypt=([A-Za-z0-9]*)/) ||
  74. ['',''])[1];
  75. $('VNC_screen').onmousemove = function () {
  76. // Unfocus clipboard when over the VNC area
  77. if (! Canvas.focused) {
  78. $('VNC_clipboard_text').blur();
  79. }
  80. };
  81. },
  82. sendCtrlAltDel: function() {
  83. RFB.sendCtrlAltDel();
  84. },
  85. updateState: function(state, msg) {
  86. var s, c, klass;
  87. s = $('VNC_status');
  88. sb = $('VNC_status_bar');
  89. c = $('VNC_connect_button');
  90. switch (state) {
  91. case 'failed':
  92. c.disabled = true;
  93. klass = "VNC_status_error";
  94. break;
  95. case 'normal':
  96. c.value = "Disconnect";
  97. c.onclick = DefaultControls.disconnect;
  98. c.disabled = false;
  99. klass = "VNC_status_normal";
  100. break;
  101. case 'disconnected':
  102. c.value = "Connect";
  103. c.onclick = DefaultControls.connect;
  104. c.disabled = false;
  105. klass = "VNC_status_normal";
  106. break;
  107. default:
  108. c.disabled = true;
  109. klass = "VNC_status_warn";
  110. break;
  111. }
  112. if (typeof(msg) !== 'undefined') {
  113. s.setAttribute("class", klass);
  114. sb.setAttribute("class", klass);
  115. s.innerHTML = msg;
  116. }
  117. },
  118. connect: function() {
  119. var host, port, password, encrypt, true_color;
  120. host = $('VNC_host').value;
  121. port = $('VNC_port').value;
  122. password = $('VNC_password').value;
  123. encrypt = $('VNC_encrypt').checked;
  124. true_color = $('VNC_true_color').checked;
  125. if ((!host) || (!port)) {
  126. throw("Must set host and port");
  127. }
  128. RFB.connect(host, port, password, encrypt, true_color);
  129. },
  130. disconnect: function() {
  131. RFB.disconnect();
  132. },
  133. clipFocus: function() {
  134. Canvas.focused = false;
  135. },
  136. clipBlur: function() {
  137. Canvas.focused = true;
  138. },
  139. clipClear: function() {
  140. $('VNC_clipboard_text').value = "";
  141. RFB.clipboardPasteFrom("");
  142. },
  143. clipReceive: function(text) {
  144. console.log(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
  145. $('VNC_clipboard_text').value = text;
  146. console.log("<< DefaultControls.clipReceive");
  147. },
  148. clipSend: function() {
  149. var text = $('VNC_clipboard_text').value;
  150. console.log(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
  151. RFB.clipboardPasteFrom(text);
  152. console.log("<< DefaultControls.clipSend");
  153. }
  154. };