default_controls.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. DefaultControls = {
  2. load: function(target) {
  3. var url;
  4. /* Handle state updates */
  5. RFB.setUpdateState(DefaultControls.updateState);
  6. RFB.setClipboardReceive(DefaultControls.clipReceive);
  7. /* Populate the 'target' DOM element with default controls */
  8. if (!target) { target = 'vnc'; }
  9. html = "";
  10. html += '<div id="VNC_controls">';
  11. html += ' <ul>';
  12. html += ' <li>Host: <input id="VNC_host"></li>';
  13. html += ' <li>Port: <input id="VNC_port"></li>';
  14. html += ' <li>Password: <input id="VNC_password"';
  15. html += ' type="password"></li>';
  16. html += ' <li>Encrypt: <input id="VNC_encrypt"';
  17. html += ' type="checkbox"></li>';
  18. html += ' <li>True Color: <input id="VNC_true_color"';
  19. html += ' type="checkbox" checked></li>';
  20. html += ' <li><input id="VNC_connect_button" type="button"';
  21. html += ' value="Loading" disabled></li>';
  22. html += ' </ul>';
  23. html += '</div>';
  24. html += '<div id="VNC_screen">';
  25. html += ' <div id="VNC_status">Loading</div>';
  26. html += ' <canvas id="VNC_canvas" width="640px" height="20px">';
  27. html += ' Canvas not supported.';
  28. html += ' </canvas>';
  29. html += '</div>';
  30. html += '<br><br>';
  31. html += '<div id="VNC_clipboard">';
  32. html += ' VNC Clipboard:';
  33. html += ' <input id="VNC_clipboard_clear_button"';
  34. html += ' type="button" value="Clear"';
  35. html += ' onclick="DefaultControls.clipClear();">';
  36. html += ' <br>';
  37. html += ' <textarea id="VNC_clipboard_text" cols=80 rows=5';
  38. html += ' onfocus="DefaultControls.clipFocus();"';
  39. html += ' onblur="DefaultControls.clipBlur();"';
  40. html += ' onchange="DefaultControls.clipSend();"></textarea>';
  41. html += '</div>';
  42. $(target).innerHTML = html;
  43. /* Populate the controls if defaults are provided in the URL */
  44. url = document.location.href;
  45. $('VNC_host').value = (url.match(/host=([A-Za-z0-9.\-]*)/) ||
  46. ['',''])[1];
  47. $('VNC_port').value = (url.match(/port=([0-9]*)/) ||
  48. ['',''])[1];
  49. $('VNC_password').value = (url.match(/password=([^&#]*)/) ||
  50. ['',''])[1];
  51. $('VNC_encrypt').checked = (url.match(/encrypt=([A-Za-z0-9]*)/) ||
  52. ['',''])[1];
  53. $('VNC_screen').onmousemove = function () {
  54. // Unfocus clipboard when over the VNC area
  55. if (RFB.clipboardFocus) {
  56. $('VNC_clipboard_text').blur();
  57. }
  58. };
  59. },
  60. updateState: function(state, msg) {
  61. var s, c, klass;
  62. s = $('VNC_status');
  63. c = $('VNC_connect_button');
  64. switch (state) {
  65. case 'failed':
  66. c.disabled = true;
  67. klass = "VNC_status_error";
  68. break;
  69. case 'normal':
  70. c.value = "Disconnect";
  71. c.onclick = DefaultControls.disconnect;
  72. c.disabled = false;
  73. klass = "VNC_status_normal";
  74. break;
  75. case 'disconnected':
  76. c.value = "Connect";
  77. c.onclick = DefaultControls.connect;
  78. c.disabled = false;
  79. klass = "VNC_status_normal";
  80. break;
  81. default:
  82. c.disabled = true;
  83. klass = "VNC_status_warn";
  84. break;
  85. }
  86. if (typeof(msg) !== 'undefined') {
  87. s.setAttribute("class", klass);
  88. s.innerHTML = msg;
  89. }
  90. },
  91. connect: function() {
  92. var host, port, password, encrypt, true_color;
  93. host = $('VNC_host').value;
  94. port = $('VNC_port').value;
  95. password = $('VNC_password').value;
  96. encrypt = $('VNC_encrypt').checked;
  97. true_color = $('VNC_true_color').checked;
  98. if ((!host) || (!port)) {
  99. alert("Must set host and port");
  100. return;
  101. }
  102. RFB.connect(host, port, password, encrypt, true_color);
  103. },
  104. disconnect: function() {
  105. RFB.disconnect();
  106. },
  107. clipFocus: function() {
  108. RFB.clipboardFocus = true;
  109. },
  110. clipBlur: function() {
  111. RFB.clipboardFocus = false;
  112. },
  113. clipClear: function() {
  114. $('VNC_clipboard_text').value = "";
  115. RFB.clipboardPasteFrom("");
  116. },
  117. clipReceive: function(text) {
  118. console.log(">> DefaultControls.clipReceive: " + text.substr(0,40) + "...");
  119. $('VNC_clipboard_text').value = text;
  120. console.log("<< DefaultControls.clipReceive");
  121. },
  122. clipSend: function() {
  123. var text = $('VNC_clipboard_text').value;
  124. console.log(">> DefaultControls.clipSend: " + text.substr(0,40) + "...");
  125. RFB.clipboardPasteFrom(text);
  126. console.log("<< DefaultControls.clipSend");
  127. }
  128. }