Embedding External Controls
HENGSHI SENSE supports using external buttons to operate the embedded page, including refreshing the page content, exporting the page as PDF, PNG, etc.
Refresh Page
When a customer embeds a page that needs to refresh the dashboard data but does not want to reload the iframe, it can be refreshed via an external button. Below is a code example and display style for refreshing the embedded page via an external button, for reference.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>Refresh</button>
<iframe
frameborder="0"
importance="high"
allowFullScreen="true"
width="1080"
height="800"
src="https://develop.hengshi.org/private/share/app/EAD43BC71B87137928BB1D7D62E639078">
</iframe>
<script>
const button = document.querySelector('button');
// 1. Click the button outside the iframe
button.addEventListener('click', () => {
const iframe = document.querySelector('iframe');
// 2. Send a message to the iframe to refresh the dashboard
iframe.contentWindow.postMessage(JSON.stringify({
event: 'dispatchCustomEvent',
// 3. Refresh dashboard identifier `hs_dashboard_refresh`
data: ['hs_dashboard_refresh', {}]
}), '*');
});
</script>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Export Dashboard PDF/PNG
The embedded page can export the dashboard as PDF and PNG via external buttons. Below are the relevant code examples and display screenshots.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="exportPNG">Export PNG</button>
<button id="exportPDF">Export PDF</button>
<iframe
frameborder="0"
importance="high"
allowFullScreen="true"
width="1080"
height="800"
src="https://develop.hengshi.org/private/share/app/EAD43BC71B87137928BB1D7D62E639078">
</iframe>
<script>
document.addEventListener('click', e => {
if (!['exportPNG', 'exportPDF'].includes(e.target.id)) return;
// Get the export type, pdf or png
const type = e.target.id === 'exportPNG' ? 'png' : 'pdf';
const iframe = document.querySelector('iframe');
// Send the export event message
iframe.contentWindow.postMessage(JSON.stringify({
event: 'dispatchCustomEvent',
data: ['hs_dashboard_export', {
type,
}]
}), '*');
});
</script>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Listening to Embedded Page Events
During page interactions, broadcast the following events using postMessage
to facilitate better integration with the business. In the business page, use
window.addEventListener('message', function handlePostMessage(event) {});
to listen for the events we broadcast. Currently, the embedded scenario supports the following events.
- Directory and application changes, including the addition, modification, and deletion of directory folders and applications, and application republishing.
- Clicking on system elements, where the clicked object can be an application, folder, dashboard, chart, or dataset.
- URL change. The
handlePostMessage
receives anevent
which is a JavaScriptMessageEvent
. Theevent.data
is the broadcast content, which is a serialized JSON string. It can be used after being parsed withJSON.parse(event.data)
.
Catalog, App Changes
Catalog and app changes are communicated through the following structure, where data
is a stringified object that can be obtained by JSON.parse
.
data.event
is the event type, includingupdateFolder
folder change,updateApp
app change, andupdatePublish
app publish change.data.data
is the specific information of the event, includinginsert
,update
,delete
, andduplicate
.
{
// HENGSHI SENSE interactive event standard
"event": "HengshiInteractiveEvent",
"type": "HengshiInteractiveEvent",
"data": {
// Event type, update folder 'updateFolder'; update app 'updateApp'; update publish 'updatePublish'
"event": "updateFolder",
// Specific information of the event, insert, update, delete, duplicate
"data": "insert_2198",
}
}
2
3
4
5
6
7
8
9
10
11
Click System Element
// After JSON.parse(event.data):
{
event: 'HengshiInteractiveEvent',
data: {
event: 'clickItem',
data: {
type: 'app',
data: {
id: xxx,
...
},
},
},
}
2
3
4
5
6
7
8
9
10
11
12
13
14
The type
refers to the type of the clicked element, in addition to app
, there are also:
- publishApp: Published App
- dashboard: Dashboard
- chart: Chart
- publishChart: Published Chart
- dataset: Dataset
- folder: Folder
- datamart: Datamart