lwsgt.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. function lwsgt_get_appropriate_ws_url()
  2. {
  3. var pcol;
  4. var u = document.URL;
  5. if (u.substring(0, 5) == "https") {
  6. pcol = "wss://";
  7. u = u.substr(8);
  8. } else {
  9. pcol = "ws://";
  10. if (u.substring(0, 4) == "http")
  11. u = u.substr(7);
  12. }
  13. return pcol + u;
  14. }
  15. function lwsgt_app_hdr(j, bc, ws)
  16. {
  17. var s = "", n, m = 0;
  18. ws.bcq = 0;
  19. for (n = 0; n < j.cols.length; n++)
  20. if (!j.cols[n].hide)
  21. m++;
  22. s = "<tr><td colspan=\"" + m + "\" class=\"lwsgt_title\">" + ws.lwsgt_title + "</td></tr>"
  23. if (!!bc) {
  24. s += "<tr><td colspan=\"" + m + "\" class=\"lwsgt_breadcrumbs\">";
  25. for (n = 0; n < bc.length; n++) {
  26. s += " / ";
  27. if (!bc[n].url && bc[n].url !== "")
  28. s += " " + lws_san(bc[n].name) + " ";
  29. else {
  30. s = s + "<a href=# id=\"bc_"+ ws.divname + ws.bcq + "\" h=\"" + ws.lwsgt_cb + "\" p=\""+ws.lwsgt_parent+"\" aa=\"="+
  31. lws_san(encodeURI(bc[n].url))+"\" m=\"-1\" n=\"-1\">" +
  32. lws_san(bc[n].name) + "</a> ";
  33. ws.bcq++;
  34. }
  35. }
  36. s += "</td></tr>";
  37. }
  38. s += "<tr>";
  39. for (n = 0; n < j.cols.length; n++)
  40. if (!j.cols[n].hide)
  41. s = s + "<td class=\"lwsgt_hdr\">" + lws_san(j.cols[n].name) + "</td>";
  42. s += "</tr>";
  43. return s;
  44. }
  45. function lwsgt_initial(title, pcol, divname, cb, gname)
  46. {
  47. this.divname = divname;
  48. lws_gray_out(true,{'zindex':'499'});
  49. if (typeof MozWebSocket != "undefined")
  50. this.lwsgt_ws = new MozWebSocket(lwsgt_get_appropriate_ws_url(), pcol);
  51. else
  52. this.lwsgt_ws = new WebSocket(lwsgt_get_appropriate_ws_url(), pcol);
  53. this.lwsgt_ws.divname = divname;
  54. this.lwsgt_ws.lwsgt_cb = cb;
  55. this.lwsgt_ws.lwsgt_parent = gname;
  56. this.lwsgt_ws.lwsgt_title = title;
  57. try {
  58. this.lwsgt_ws.onopen = function() {
  59. lws_gray_out(false);
  60. // document.getElementById("debug").textContent =
  61. // "ws opened " + lwsgt_get_appropriate_ws_url();
  62. }
  63. this.lwsgt_ws.onmessage = function got_packet(msg) {
  64. var s, m, n, j = JSON.parse(msg.data);
  65. document.getElementById("debug").textContent = msg.data;
  66. if (j.cols) {
  67. this.hdr = j;
  68. }
  69. if (j.breadcrumbs)
  70. this.breadcrumbs = j.breadcrumbs;
  71. if (j.data) {
  72. var q = 0;
  73. s = "<table class=\"lwsgt_table\">" +
  74. lwsgt_app_hdr(this.hdr, this.breadcrumbs, this);
  75. for (m = 0; m < j.data.length; m++) {
  76. s = s + "<tr class=\"lwsgt_tr\">";
  77. for (n = 0; n < this.hdr.cols.length; n++) {
  78. if (!this.hdr.cols[n].hide) {
  79. if (!this.hdr.cols[n].align)
  80. s = s + "<td class=\"lwsgt_td\">";
  81. else
  82. s = s + "<td class=\"lwsgt_td\" style=\"text-align: right\">";
  83. if (this.hdr.cols[n].href &&
  84. !!j.data[m][this.hdr.cols[n].href]) {
  85. s = s + "<a href=# id=\""+ this.divname + q + "\" h=\"" + this.lwsgt_cb + "\" p=\""+this.lwsgt_parent+"\" aa=\""+
  86. lws_san(encodeURI(j.data[m][this.hdr.cols[n].href]))+"\" m=\""+m+"\" n=\""+n+"\">" +
  87. lws_san(j.data[m][this.hdr.cols[n].name]) +
  88. "</a>";
  89. q++;
  90. }
  91. else
  92. s = s + lws_san(j.data[m][this.hdr.cols[n].name]);
  93. s = s + "</td>";
  94. }
  95. }
  96. s = s + "</tr>";
  97. }
  98. s = s + "</table>";
  99. document.getElementById(this.divname).innerHTML = s;
  100. for (n = 0; n < q; n++)
  101. document.getElementById(this.divname + n).onclick = lwsgt_click_callthru;
  102. for (n = 0; n < this.bcq; n++)
  103. document.getElementById("bc_" + this.divname + n).onclick = lwsgt_click_callthru;
  104. }
  105. }
  106. this.lwsgt_ws.onclose = function(){
  107. lws_gray_out(true,{'zindex':'499'});
  108. }
  109. } catch(exception) {
  110. alert('<p>Error' + exception);
  111. }
  112. }
  113. function lwsgt_click_callthru()
  114. {
  115. window[this.getAttribute("h")](this.getAttribute("p"), this.getAttribute("aa"), this.getAttribute("m"), this.getAttribute("n"));
  116. event.preventDefault();
  117. }