default_controls.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 += ' id="sendCtrlAltDelButton"';
  46. html += ' onclick="DefaultControls.sendCtrlAltDel();"></div></td>';
  47. html += ' </tr></table>';
  48. html += ' </div>';
  49. html += ' <canvas id="VNC_canvas" width="640px" height="20px">';
  50. html += ' Canvas not supported.';
  51. html += ' </canvas>';
  52. html += '</div>';
  53. html += '<br><br>';
  54. html += '<div id="VNC_clipboard">';
  55. html += ' VNC Clipboard:';
  56. html += ' <input id="VNC_clipboard_clear_button"';
  57. html += ' type="button" value="Clear"';
  58. html += ' onclick="DefaultControls.clipClear();">';
  59. html += ' <br>';
  60. html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5';
  61. html += ' onfocus="DefaultControls.clipFocus();"';
  62. html += ' onblur="DefaultControls.clipBlur();"';
  63. html += ' onchange="DefaultControls.clipSend();"></textarea>';
  64. html += '</div>';
  65. $(target).innerHTML = html;
  66. /* Populate the controls if defaults are provided in the URL */
  67. url = document.location.href;
  68. $('VNC_host').value = (url.match(/host=([A-Za-z0-9.\-]*)/) ||
  69. ['',''])[1];
  70. $('VNC_port').value = (url.match(/port=([0-9]*)/) ||
  71. ['',''])[1];
  72. $('VNC_password').value = (url.match(/password=([^&#]*)/) ||
  73. ['',''])[1];
  74. $('VNC_encrypt').checked = (url.match(/encrypt=([A-Za-z0-9]*)/) ||
  75. ['',''])[1];
  76. $('VNC_screen').onmousemove = function () {
  77. // Unfocus clipboard when over the VNC area
  78. if (! Canvas.focused) {
  79. $('VNC_clipboard_text').blur();
  80. }
  81. };
  82. },
  83. sendCtrlAltDel: function() {
  84. RFB.sendCtrlAltDel();
  85. },
  86. updateState: function(state, msg) {
  87. var s, c, klass;
  88. s = $('VNC_status');
  89. sb = $('VNC_status_bar');
  90. c = $('VNC_connect_button');
  91. cad = $('sendCtrlAltDelButton');
  92. switch (state) {
  93. case 'failed':
  94. c.disabled = true;
  95. cad.disabled = true;
  96. klass = "VNC_status_error";
  97. break;
  98. case 'normal':
  99. c.value = "Disconnect";
  100. c.onclick = DefaultControls.disconnect;
  101. c.disabled = false;
  102. cad.disabled = false;
  103. klass = "VNC_status_normal";
  104. break;
  105. case 'disconnected':
  106. c.value = "Connect";
  107. c.onclick = DefaultControls.connect;
  108. c.disabled = false;
  109. cad.disabled = true;
  110. klass = "VNC_status_normal";
  111. break;
  112. default:
  113. c.disabled = true;
  114. cad.disabled = true;
  115. klass = "VNC_status_warn";
  116. break;
  117. }
  118. if (typeof(msg) !== 'undefined') {
  119. s.setAttribute("class", klass);
  120. sb.setAttribute("class", klass);
  121. s.innerHTML = msg;
  122. }
  123. },
  124. connect: function() {
  125. var host, port, password, encrypt, true_color;
  126. host = $('VNC_host').value;
  127. port = $('VNC_port').value;
  128. password = $('VNC_password').value;
  129. encrypt = $('VNC_encrypt').checked;
  130. true_color = $('VNC_true_color').checked;
  131. if ((!host) || (!port)) {
  132. throw("Must set host and port");
  133. }
  134. RFB.connect(host, port, password, encrypt, true_color);
  135. },
  136. disconnect: function() {
  137. RFB.disconnect();
  138. },
  139. clipFocus: function() {
  140. Canvas.focused = false;
  141. },
  142. clipBlur: function() {
  143. Canvas.focused = true;
  144. },
  145. clipClear: function() {
  146. $('VNC_clipboard_text').value = "";
  147. RFB.clipboardPasteFrom("");
  148. },
  149. clipReceive: function(text) {
  150. console.log(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
  151. $('VNC_clipboard_text').value = text;
  152. console.log("<< DefaultControls.clipReceive");
  153. },
  154. clipSend: function() {
  155. var text = $('VNC_clipboard_text').value;
  156. console.log(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
  157. RFB.clipboardPasteFrom(text);
  158. console.log("<< DefaultControls.clipSend");
  159. }
  160. };