|
| 1 | +# Demo of `bootstrap` usage with `RWebWindow` |
| 2 | + |
| 3 | +Source of demo placed in repository: https://github.com/linev/bootstrap-example |
| 4 | + |
| 5 | +It is modified [Start Bootstrap - Freelancer](https://github.com/StartBootstrap/startbootstrap-freelancer) example |
| 6 | +with connection to ROOT application via RWebWindow functionality. |
| 7 | + |
| 8 | +Example also shows possiblity to embed `TWebCanvas` in such widget and provide regular `TCanvas::Update()` from ROOT application. |
| 9 | + |
| 10 | +## Run example |
| 11 | + |
| 12 | + root --web=chrome tutorials/visualisation/webgui/bootstrap/webwindow.cxx |
| 13 | + |
| 14 | + |
| 15 | +## Modify example |
| 16 | + |
| 17 | +First checkout repository |
| 18 | + |
| 19 | + git clone https://github.com/linev/bootstrap-example.git |
| 20 | + |
| 21 | + |
| 22 | +Then modify sources and rebuild |
| 23 | + |
| 24 | + cd bootstrap-example |
| 25 | + npm install |
| 26 | + npm run build |
| 27 | + |
| 28 | +Finally run example again |
| 29 | + |
| 30 | + root --web=chrome webwindow.cxx |
| 31 | + |
| 32 | + |
| 33 | +## Use of custom HTML/JS/CSS files with RWebWindow |
| 34 | + |
| 35 | +When creating RWebWindow, one can specify location on main HTML file created by framework such as bootstrap. |
| 36 | + |
| 37 | + window->SetDefaultPage("file:" + fdir + "dist/index.html"); |
| 38 | + |
| 39 | +Typically all realted files (css, scripts, images) also located in same directory as html file (`dist/` in this case). RWebWindow automatically provides access to files in directory where main page is located and configured with `SetDefaultPage("file:" + ...) ` call. |
| 40 | + |
| 41 | + |
| 42 | +## Loading JSROOT functionality |
| 43 | + |
| 44 | +To be able use `import { something } from 'jsroot';` statements one should configure correspondent importmap. |
| 45 | +Most simple way is to add `<!--jsroot_importmap-->` comment into HTML file into head section. Then `THttpServer` |
| 46 | +automatically will insert required importmap entry. To put such comment in bootstrap-created HTML file one can simply add to `index.pug` file: |
| 47 | + |
| 48 | + //jsroot_importmap |
| 49 | + |
| 50 | + |
| 51 | +## Connect with RWebWindow instance of ROOT |
| 52 | + |
| 53 | +This done with following JS code: |
| 54 | + |
| 55 | +```javascript |
| 56 | + import { connectWebWindow } from 'jsroot/webwindow'; |
| 57 | + |
| 58 | + connectWebWindow({ |
| 59 | + receiver: { |
| 60 | + onWebsocketOpened(handle) {}, |
| 61 | + onWebsocketMsg(handle, msg, offset) {}, |
| 62 | + onWebsocketClosed(handle) {} |
| 63 | + } |
| 64 | + }); |
| 65 | +``` |
| 66 | + |
| 67 | +This code placed in `webwindow.js` file and directly included in main HTML page. |
| 68 | +All necessary credentials to connect with ROOT applicatoin will be provided in URL string. |
| 69 | + |
| 70 | + |
| 71 | +## Communication with RWebWindow |
| 72 | + |
| 73 | +In `receiver` object one specifies `onWebsocketOpened`, `onWebsocketMsg`, `onWebsocketClosed` methods which are |
| 74 | +called when correspondent event produced by websocket connection. Example shows how binary and string data can be transferred from the ROOT applition and displayed in custom HTML element. |
| 75 | + |
| 76 | +To send some data to the server, one should call `handle.send('Some string message');`. Only strings are supported. |
| 77 | +On the ROOT side one assigns `window->SetDataCallBack(ProcessData);` function which is invoked when message received from the client. |
| 78 | + |
| 79 | + |
| 80 | +## Embeding TWebCanvas |
| 81 | + |
| 82 | +`TWebCanvas` is web-based implementation for `TCanvas`. It can be used to embed full-functional TCanvas in any web widgets - reusing existing RWebWindow connection. Following step should be done: |
| 83 | + |
| 84 | +1. Create channel inside existing connection on the client side: |
| 85 | + |
| 86 | +```javascript |
| 87 | + const conn = handle.createChannel(); |
| 88 | +``` |
| 89 | + |
| 90 | +2. Create JSROOT `TCanvasPainter` and configure to use it in with such communication channel: |
| 91 | + |
| 92 | +```javascript |
| 93 | + import { TCanvasPainter } from 'jsroot'; |
| 94 | + |
| 95 | + const dom = document.body.querySelector('#rootapp_canvas'); |
| 96 | + |
| 97 | + const painter = new TCanvasPainter(dom, null); |
| 98 | + |
| 99 | + painter.online_canvas = true; // indicates that canvas gets data from running server |
| 100 | + painter.embed_canvas = true; // use to indicate that canvas ui should not close complete window when closing |
| 101 | + painter.use_openui = false; // use by default ui5 widgets |
| 102 | + |
| 103 | + painter.useWebsocket(conn); |
| 104 | +``` |
| 105 | + |
| 106 | +3. Communicate channel id to the ROOT application: |
| 107 | + |
| 108 | +```javascript |
| 109 | + handle.send('channel:' + conn.getChannelId()); |
| 110 | +``` |
| 111 | + |
| 112 | +4. On server create web `TCanvas` and use received channel id to show it |
| 113 | + |
| 114 | +```cpp |
| 115 | + int chid = std::stoi(arg.substr(8)); |
| 116 | + // create and configure canvas |
| 117 | + auto canvas = TWebCanvas::CreateWebCanvas("name", "title"); |
| 118 | + // access web implementation |
| 119 | + auto web_canv = static_cast<TWebCanvas *>(canvas->GetCanvasImp()); |
| 120 | + // add web canvas to main connection |
| 121 | + web_canv->ShowWebWindow({ window, connid, chid }); |
| 122 | +``` |
| 123 | +
|
| 124 | +After this canvas can be regulary modified and updated from the ROOT application. |
| 125 | +Any changes in the canvas will be reflected on the client side. |
| 126 | +From client ROOT functionality can be accessed via context menu. |
| 127 | +
|
0 commit comments