Sidebar-Basic-Components.js
11.8 KB
1
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(function () {
// Adds Atlassian shapes
Sidebar.prototype.addBasicComponentsPalette = function () {
this.setCurrentSearchEntryLibrary('basicComponent');
const dt = 'basicComponent'
const gn = `mxgraph.${dt}`
const currentDate = getCurrentDate();
const basicAttr = this.enumCellBasicAttribute
const componentType = this.enumComponentType
const { INTERACTION, DYNAMIC_EFFECT, DATA_SOURCE, VAR_IMAGE } = this.enumPermissionPanel
const transparentBg = 'text;strokeColor=none;fillColor=none;html=1;fontSize=24;fontStyle=1;verticalAlign=middle;align=center;'
const fns = [
this.addEntry(this.getTagsForStencil(gn, 'Title', dt).join(' '), mxUtils.bind(this, function () {
const cell = new mxCell('Title', new mxGeometry(0, 0, 100, 40), transparentBg);
cell.setVertex(true)
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.TITLE })
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, 'Title');
})),
this.addEntry(this.getTagsForStencil(gn, 'variable', dt).join(' '), mxUtils.bind(this, function () {
const cell = new mxCell('变量', new mxGeometry(0, 0, 100, 40), transparentBg);
cell.setVertex(true)
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.VARIABLE })
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, '变量');
})),
this.addEntry(this.getTagsForStencil(gn, 'line', dt).join(' '), mxUtils.bind(this, function () {
const style = 'shape=filledEdge;rounded=1;fixDash=1;endArrow=none;strokeWidth=10;fillColor=none;edgeStyle=orthogonalEdgeStyle;flowAnimation=1;strokeColor=#6666FF;endFill=1;metaEdit=0;backgroundOutline=0;'
const cell = new mxCell('', new mxGeometry(0, 0, 60, 40), style);
cell.geometry.setTerminalPoint(new mxPoint(0, 40), true);
cell.geometry.setTerminalPoint(new mxPoint(60, 0), false);
cell.geometry.relative = true;
cell.edge = true;
cell.setVertex(true)
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.LINE })
return this.createEdgeTemplateFromCells([cell], 60, 40, '线条');
})),
this.addEntry('real time', mxUtils.bind(this, function () {
const template = `<div class="thingKit-component__real-time"><div class="real-time__date">%currentDate%</div> <div style="font-size:30px" class="real-time__now">HH:mm:ss</div></div>`
const cell = new mxCell(template, new mxGeometry(0, 0, 180, 60), 'text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;overflow=hidden;');
cell.vertex = true;
this.setCellAttributes(cell, {
[basicAttr.COMPONENT_TYPE]: componentType.REAL_TIME,
placeholders: '1',
currentDate: currentDate,
})
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, '实时时间');
})),
this.addEntry(this.getTagsForStencil(gn, '变量图片', 'basic').join(' '), mxUtils.bind(this, function () {
const cell = new mxCell('', new mxGeometry(0, 0, 194, 95), 'image;image=images/thingskit/img-placeholder.png;imageAspect=0;');
cell.setVertex(true)
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.VAR_IMAGE })
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, '变量图片');
})),
this.addEntry(this.getTagsForStencil(gn, '视频', 'basic').join(' '), mxUtils.bind(this, function () {
const template = createVideoTemplate(300, 150)
const cell = new mxCell(template, new mxGeometry(0, 0, 300, 150), 'text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;overflow=hidden;');
cell.setVertex(true)
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.VIDEO })
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, '视频');
})),
this.addEntry(this.getTagsForStencil(gn, '图片', 'basic').join(' '), mxUtils.bind(this, function () {
const cell = new mxCell(`<img class="basic-component__image" src="${Proxy_Prefix}/images/thingskit/img-placeholder.png" />`, new mxGeometry(0, 0, 190, 190), 'text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;overflow=hidden;');
cell.setVertex(true)
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.IMAGE })
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, '图片');
})),
this.addEntry(this.getTagsForStencil(gn, '告警列表', 'basic').join(' '), mxUtils.bind(this, function () {
const id = AlarmListComponent.genId()
const template = AlarmListComponent.createAlarmList(null, 280, 200, id)
const cell = new mxCell(template, new mxGeometry(0, 0, 280, 200), 'text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;overflow=hidden;');
cell.setVertex(true)
const { UUID } = AlarmListComponent.getAttributeKeys()
this.setCellAttributes(cell, { [basicAttr.COMPONENT_TYPE]: componentType.ALARM_LIST, [UUID]: id })
return this.createVertexTemplateFromCells([cell], cell.geometry.width, cell.geometry.height, '告警列表');
})),
];
this.addPaletteFunctions(dt, '基础元件', true, fns);
this.setCurrentSearchEntryLibrary();
};
Sidebar.prototype.isVideo = function (cell) {
const basicAttr = Sidebar.prototype.enumCellBasicAttribute
const componentType = Sidebar.prototype.enumComponentType
return cell.getAttribute(basicAttr.COMPONENT_TYPE) === componentType.VIDEO
}
Sidebar.prototype.isAlarmList = function (cell) {
const basicAttr = Sidebar.prototype.enumCellBasicAttribute
const componentType = Sidebar.prototype.enumComponentType
return cell.getAttribute(basicAttr.COMPONENT_TYPE) === componentType.ALARM_LIST
}
/**
* @description charts cell发生resize时改变charts size
* @type {Function}
*/
const cellResized = mxGraph.prototype.cellResized
mxGraph.prototype.cellResized = function (cell, rect) {
if (Sidebar.prototype.isVideo(cell)) {
const { width, height } = rect
cell.setAttribute('label', createVideoTemplate(width, height))
}
if (AlarmListComponent.isAlarmList(cell)) {
const { width, height } = rect
cell.setAttribute('label', AlarmListComponent.createAlarmList(null, width, height))
}
cellResized.apply(this, arguments)
}
function createVideoTemplate(width = '100%', height = '100%') {
const m3u8 = 'http://d2zihajmogu5jn.cloudfront.net/bipbop-advanced/bipbop_16x9_variant.m3u8'
// const m3u8 = 'http://113.204.115.250:83/openUrl/iVnkGzK/live.m3u8'
const mp4 = 'http://vjs.zencdn.net/v/oceans.mp4'
const m3u8Type = 'application/x-mpegURL'
const mp4Type = 'video/mp4'
const poster = `${Proxy_Prefix}/images/youtube.png`
// const cell = new mxCell('<video></video>', new mxGeometry(0, 0, 100, 95), 'image;image=images/thingskit/video.svg;imageAspect=0;');
const template = `<video controls preload="auto" muted="muted" width="${width}" height="${height}" poster="${poster}" data-setup='{}'>
<source src="${m3u8}" type="${m3u8Type}">
<p class="vjs-no-js">
要查看此视频,请启用JavaScript,并考虑升级web浏览器.
</p>
</video>`
return template
}
//封装日期函数
function getCurrentDate() {
var date = new Date(); //创建日期对象
var year = date.getFullYear();
var month = date.getMonth() + 1;
var dates = date.getDate();
arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
var day = date.getDay();
return year + '年' + month + '月' + dates + '日 ' + arr[day];
}
/**
* @description 初始化实时事件组件
*/
function initRealTimeComponent() {
RAFSetInterval(() => {
const allTimeNode = document.querySelectorAll('.thingKit-component__real-time .real-time__now')
const allDateNode = document.querySelectorAll('.thingKit-component__real-time .real-time__date')
for (const time of allTimeNode) {
const date = new Date()
time.innerHTML = [date.getHours(), date.getMinutes(), date.getSeconds()].map(item => item.toString().padStart(2, 0)).join(':')
}
for (const date of allDateNode) {
date.innerHTML = getCurrentDate()
}
}, 1000)
}
initRealTimeComponent()
})();
function AlarmListComponent() {
}
/**
*
* @param {{deviceName: string, status: string, startTs: string}[]} list
* @param {number} width
* @param {number} height
* @param {string} id
* @returns
*/
AlarmListComponent.createAlarmList = function (list, width = 280, height = 200, id) {
list = list || Array.from({ length: 10 }, ((_, index) => ({
deviceName: `示例设备${index + 1}`, startTs: Date.now(), status: [
"CLEARED_UNACK",
"ACTIVE_UNACK",
"CLEARED_ACK",
"ACTIVE_ACK"
][index % 4]
})))
id = id || AlarmListComponent.genId()
var template = `
<div id="${id}" class="alarm-list" style="overflow-y: auto; overflow-x: hidden; width: ${width}px; height: ${height}px;">
<div class="list-wrapper" style="font-size: 12px;" data-auto-scroll>
${AlarmListComponent.createAlarmItem(list)}
</div>
</div>
`
return template.replace(/\n/g, '')
}
/**
*
* @param {{deviceName: string, status: string, startTs: string}[] | {deviceName: string, status: string, startTs: string}} record
* @returns
*/
AlarmListComponent.createAlarmItem = function (record) {
if (record && !Array.isArray(record)) {
record = [record]
}
var itemHeight = 50
function formatTime(time) {
try {
var date = new Date(time)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return `${year}-${month.toString().padStart(2, 0)}-${day.toString().padStart(2, 0)} ${hour.toString().padStart(2, 0)}:${minute.toString().padStart(2, 0)}:${second.toString().padStart(2, 0)}`
} catch (error) {
return '暂无时间 '
}
}
return (record || []).map(item => {
var { alias, deviceName, status, startTs } = item || {}
var stateStyle = {
CLEARED_UNACK: 'color: #cf1322;background: #fff1f0;border-color: #ffa39e;',
ACTIVE_UNACK: 'color: #d46b08;background: #fff7e6;border-color: #ffd591;',
CLEARED_ACK: 'color: #08979c;background: #e6fffb;border-color: #87e8de;',
ACTIVE_ACK: 'color: #389e0d;background: #f6ffed;border-color: #b7eb8f;',
}
var stateName = {
CLEARED_UNACK: '清除未确认',
ACTIVE_UNACK: '激活未确认',
CLEARED_ACK: '清除已确认',
ACTIVE_ACK: '激活已确认',
}
var template = `
<div class="alarm-list-item" style="height: ${itemHeight}px; box-sizing: border-box; display: flex; flex-direction: column; justify-content: center; border-bottom: 1px solid black; text-align: left; min-width: 280px; width: 100%;">
<div style="padding: 0 10px;">
<span style="margin-right: 5px;">设备:</span>
<span>${alias || deviceName}</span>
</div>
<div style="padding: 5px 10px;">
<span style="margin-right: 5px;">时间:</span>
<span>${formatTime(startTs)}</span>
<span style="padding: 5px; border-radius: 5px; margin-left: 5px; border: 1px solid; font-size: 12px; ${stateStyle[status]}">${stateName[status]}</span>
</div>
</div>`
return template
}).join('').replace(/\n/g, '')
}
AlarmListComponent.genId = function () {
return `alarm-list-${Number(Math.random().toString().substring(2)).toString(16)}`
}
AlarmListComponent.isAlarmList = function (cell) {
const basicAttr = Sidebar.prototype.enumCellBasicAttribute
const componentType = Sidebar.prototype.enumComponentType
return cell.getAttribute(basicAttr.COMPONENT_TYPE) === componentType.ALARM_LIST
}
AlarmListComponent.getAttributeKeys = function () {
return {
UUID: 'uuid',
}
}
AlarmListComponent.setAutoScroll = function (cell) {
// RAFSetInterval(() => {
// console.log(cell)
// }, 2000)
}