1 Notes about generic-table
2 =========================
4 @section gtint What is generic-table?
6 Generic-table is a JSON schema and client-side JS file that makes it easy to
7 display live, table structured HTML over a ws link.
9 An example plugin and index.html using it are provided, but lwsgt itself doesn't
10 have its own plugin, it's just a JSON schema and client-side JS that other
11 plugins can use to simplify displaying live, table-based data without having
12 to reinvent the wheel each time.
14 The ws protocol sends JSON describing the table, and then JSON updating the table
15 contents when it chooses, the brower table is updated automatically, live.
17 \image html lwsgt-overview.png
19 - Example protocol plugin (displays directory contents): https://github.com/warmcat/libwebsockets/tree/master/plugins/generic-table/protocol_table_dirlisting.c
21 - Example HTML: https://github.com/warmcat/libwebsockets/tree/master/plugins/generic-table/assets/index.html
23 - lwsgt.js (client-side table rendering / ws link management): https://github.com/warmcat/libwebsockets/tree/master/plugins/generic-table/assets/lwsgt.js
26 @section gteb Enabling for build
28 Enable the demo plugin at CMake with -DLWS_WITH_PLUGINS=1
31 @section gtinth Integrating with your html
33 - In your HEAD section, include lwsgt.js
36 <script src="lwsgt.js"></script>
39 - Also in your HEAD section, style the lwsgt CSS, eg
43 .lwsgt_title { font-size: 24; text-align:center }
44 .lwsgt_breadcrumbs { font-size: 18; text-align:left }
45 .lwsgt_table { font-size: 14; padding:12px; margin: 12px; align:center }
46 .lwsgt_hdr { font-size: 18; text-align:center;
47 background-color: rgba(40, 40, 40, 0.8); color: white }
48 .lwsgt_tr { padding: 10px }
49 .lwsgt_td { padding: 3px }
53 You can skip this but the result will be less beautiful until some CSS is
56 - In your body section, declare a div with an id (can be whatever you want)
59 <tr><td><div id="lwsgt1" class="group1"></div></td></tr>
62 lwsgt JS will put its content there.
64 - Finally in a <script> at the end of your page, instantiate lwsgt and
65 provide a custom callback for clickable links
69 var v1 = new lwsgt_initial("Dir listing demo",
70 "protocol-lws-table-dirlisting",
71 "lwsgt1", "lwsgt_dir_click", "v1");
73 function lwsgt_dir_click(gt, u, col, row)
75 if (u[0] == '=') { /* change directory */
76 window[gt].lwsgt_ws.send(u.substring(1, u.length));
79 var win = window.open(u, '_blank');
86 In the callback, you can recover the ws object by `window[gt].lwsgt_ws`.
89 @section gtc Lwsgt constructor
91 To instantiate the ws link and lwsgt instance, your HTML must call a lwsgt
92 constructor for each region on the page managed by lwsgt.
94 `var myvar = new lwsgt_initial(title, ws_protocol, div_id, click_cb, myvar);`
96 All of the arguments are strings.
98 | Parameter | Description |
99 |-----------------|---------------------------------------------------------|
100 | title | Title string to go above the table |
101 | ws_protocol | Protocol name string to use when making ws connection |
102 | div_id | HTML id of div to fill with content |
103 | click_cb | Callback function name string to handle clickable links |
104 | myvar | Name of var used to hold this instantiation globally |
106 Note "myvar" is needed so it can be passed to the click handling callback.
109 @section gtclick Lwsgt click handling function
111 When a clickable link produced by lwsgt is clicked, the function named in the
112 click_cb parameter to lwsgt_initial is called.
114 That function is expected to take four parameters, eg
116 `function lwsgt_dir_click(gt, u, col, row)`
118 | Parameter | Description |
119 |------- ---|-----------------------------------------------------------|
120 | gt | Name of global var holding this lwsgt context (ie, myvar) |
121 | u | Link "url" string |
122 | col | Table column number link is from |
123 | row | Table row number link is from |
127 @section gtgj Generic-table JSON
131 When the ws connection is established, the protocol should send a JSON message
132 describing the table columns. For example
137 { "name": "Size", "align": "right" },
139 { "name": "Name", "href": "uri"},
140 { "name": "uri", "hide": "1" }
145 - This describes 5 columns
147 - Only four columns (not "uri") should be visible
149 - "Name" should be presented as a clickable link using "uri" as the
150 destination, when a "uri" field is presented.
152 - "Size" field should be presented aligned to the right
156 When a view is hierarchical, it's useful to provide a "path" with links back
157 in the "path", known as "breadcrumbs".
159 Elements before the last one should provide a "url" member as well as the
160 displayable name, which is used to create the link destination.
162 The last element, being the current displayed page should not have a url
163 member and be displayed without link style.
167 "breadcrumbs":[{"name":"top", "url": "/" }, {"name":"mydir"}]
172 The actual file data consists of an array of rows, containing the columns
173 mentioned in the original "cols" section.
179 "Date":"2015-Feb-06 03:08:35 +0000",
181 "uri":"./serve//favicon.ico",
188 @section gtdirl Setting up protocol-lws-table-dirlisting
190 The example protocol needs two mounts, one to provide the index.html, js and
195 "mountpoint": "/dirtest",
196 "origin": "file:///usr/share/libwebsockets-test-server/generic-table",
197 "origin": "callback://protocol-lws-table-dirlisting",
198 "default": "index.html",
200 "dir": "/usr/share/libwebsockets-test-server"
205 The protocol wants a per-mount option (PMO) to tell it the base directory it
206 is serving from, named "dir".
208 The other mount is there to simply serve items that get clicked on from the
209 table in a secure way
213 "mountpoint": "/dirtest/serve",
214 "origin": "file:///usr/share/libwebsockets-test-server",
215 "default": "index.html"
219 This last bit is not related to using lwsgt itself.