default_controls.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 $, RFB, Canvas, VNC_uri_prefix, Element, Fx */
  10. var DefaultControls = {
  11. load: function(target) {
  12. var url, html;
  13. /* Handle state updates */
  14. RFB.setUpdateState(DefaultControls.updateState);
  15. RFB.setClipboardReceive(DefaultControls.clipReceive);
  16. /* Populate the 'target' DOM element with default controls */
  17. if (!target) { target = 'vnc'; }
  18. html = "";
  19. html += '<div id="VNC_controls">';
  20. html += ' <ul>';
  21. html += ' <li>Host: <input id="VNC_host"></li>';
  22. html += ' <li>Port: <input id="VNC_port"></li>';
  23. html += ' <li>Password: <input id="VNC_password"';
  24. html += ' type="password"></li>';
  25. html += ' <li>Encrypt: <input id="VNC_encrypt"';
  26. html += ' type="checkbox"></li>';
  27. html += ' <li>True Color: <input id="VNC_true_color"';
  28. html += ' type="checkbox" checked></li>';
  29. html += ' <li><input id="VNC_connect_button" type="button"';
  30. html += ' value="Loading" disabled></li>';
  31. html += ' </ul>';
  32. html += '</div>';
  33. html += '<div id="VNC_screen">';
  34. html += ' <div id="VNC_status_bar" class="VNC_status_bar" style="margin-top: 0px;">';
  35. html += ' <table border=0 width=100%><tr>';
  36. html += ' <td><div id="VNC_status">Loading</div></td>';
  37. html += ' <td width=10%><div id="VNC_buttons">';
  38. html += ' <input type=button value="Send CtrlAltDel"';
  39. html += ' id="sendCtrlAltDelButton"';
  40. html += ' onclick="DefaultControls.sendCtrlAltDel();"></div></td>';
  41. html += ' </tr></table>';
  42. html += ' </div>';
  43. html += ' <canvas id="VNC_canvas" width="640px" height="20px">';
  44. html += ' Canvas not supported.';
  45. html += ' </canvas>';
  46. html += '</div>';
  47. html += '<br><br>';
  48. html += '<div id="VNC_clipboard">';
  49. html += ' VNC Clipboard:';
  50. html += ' <input id="VNC_clipboard_clear_button"';
  51. html += ' type="button" value="Clear"';
  52. html += ' onclick="DefaultControls.clipClear();">';
  53. html += ' <br>';
  54. html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5';
  55. html += ' onfocus="DefaultControls.clipFocus();"';
  56. html += ' onblur="DefaultControls.clipBlur();"';
  57. html += ' onchange="DefaultControls.clipSend();"></textarea>';
  58. html += '</div>';
  59. $(target).innerHTML = html;
  60. /* Populate the controls if defaults are provided in the URL */
  61. url = document.location.href;
  62. $('VNC_host').value = (url.match(/host=([A-Za-z0-9.\-]*)/) ||
  63. ['',''])[1];
  64. $('VNC_port').value = (url.match(/port=([0-9]*)/) ||
  65. ['',''])[1];
  66. $('VNC_password').value = (url.match(/password=([^&#]*)/) ||
  67. ['',''])[1];
  68. $('VNC_encrypt').checked = (url.match(/encrypt=([A-Za-z0-9]*)/) ||
  69. ['',''])[1];
  70. $('VNC_screen').onmousemove = function () {
  71. // Unfocus clipboard when over the VNC area
  72. if (! Canvas.focused) {
  73. $('VNC_clipboard_text').blur();
  74. }
  75. };
  76. },
  77. sendCtrlAltDel: function() {
  78. RFB.sendCtrlAltDel();
  79. },
  80. updateState: function(state, msg) {
  81. var s, c, klass;
  82. s = $('VNC_status');
  83. sb = $('VNC_status_bar');
  84. c = $('VNC_connect_button');
  85. cad = $('sendCtrlAltDelButton');
  86. switch (state) {
  87. case 'failed':
  88. c.disabled = true;
  89. cad.disabled = true;
  90. klass = "VNC_status_error";
  91. break;
  92. case 'normal':
  93. c.value = "Disconnect";
  94. c.onclick = DefaultControls.disconnect;
  95. c.disabled = false;
  96. cad.disabled = false;
  97. klass = "VNC_status_normal";
  98. break;
  99. case 'disconnected':
  100. c.value = "Connect";
  101. c.onclick = DefaultControls.connect;
  102. c.disabled = false;
  103. cad.disabled = true;
  104. klass = "VNC_status_normal";
  105. break;
  106. default:
  107. c.disabled = true;
  108. cad.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. Util.Debug(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
  145. $('VNC_clipboard_text').value = text;
  146. Util.Debug("<< DefaultControls.clipReceive");
  147. },
  148. clipSend: function() {
  149. var text = $('VNC_clipboard_text').value;
  150. Util.Debug(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
  151. RFB.clipboardPasteFrom(text);
  152. Util.Debug("<< DefaultControls.clipSend");
  153. }
  154. };