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