app.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var ws = new WebSocket("ws://localhost:8080/test");
  2. var handles = get_handles(ws);
  3. ws.onopen = function () {
  4. // ws.send("Hello, world");
  5. };
  6. ws.onmessage = function (evt) {
  7. console.log(">>>", evt.data);
  8. var msg = JSON.parse(evt.data);
  9. handles[msg.command](msg.spec, msg.coro_id);
  10. };
  11. var input_item = {
  12. html: '',
  13. set_invalid: msg => {
  14. },
  15. set_valid: msg => {
  16. },
  17. };
  18. function InputItem(ele, name) {
  19. this.ele = ele;
  20. this.name = name;
  21. this.set_invalid;
  22. this.set_valid;
  23. this.set_attr; // 设置input的tag属性
  24. }
  25. function get_handles(ws) {
  26. var md_parser = new Mditor.Parser();
  27. var output_container = $('#markdown-body');
  28. var input_container = $('#input-container');
  29. var handles = {
  30. text_input: function (spec, coro_id) {
  31. var html = `<h5 class="card-header">需要您输入</h5>
  32. <div class="card-body">
  33. <form action="" id="input-form">
  34. <label for="input-1">name</label>
  35. <div class="input-group mb-3">
  36. <input type="text" class="form-control" placeholder="${spec.prompt}"
  37. aria-label="${spec.prompt}" id="input-1" autocomplete="off">
  38. <div class="input-group-append">
  39. <button class="btn btn-outline-secondary" type="submit" id="button-submit" >提交</button>
  40. </div>
  41. </div>
  42. </form>
  43. </div>`;
  44. input_container.empty();
  45. input_container.html(html);
  46. input_container.show(100);
  47. $('#input-1').focus();
  48. // console.log(spec, html, input_container.html());
  49. $('#input-form').submit(function (e) {
  50. e.preventDefault(); // avoid to execute the actual submit of the form.
  51. ws.send(JSON.stringify({coro_id: coro_id, msg_id: spec.msg_id, data: $('#input-1').val()}));
  52. input_container.hide(100);
  53. input_container.empty();
  54. // setTimeout(function () {
  55. //
  56. // }, 200);
  57. });
  58. },
  59. text_print: function (spec) {
  60. output_container[0].innerHTML += md_parser.parse(spec.content);
  61. }
  62. };
  63. return handles;
  64. }