1
|
|
-<template>
|
2
|
|
- <div>
|
3
|
|
- <BasicDrawer
|
4
|
|
- v-bind="$attrs"
|
5
|
|
- :title="getTitle"
|
6
|
|
- @register="register"
|
7
|
|
- width="500px"
|
8
|
|
- @ok="handleSubmit"
|
9
|
|
- showFooter
|
10
|
|
- >
|
11
|
|
- <BasicForm @register="registerForm">
|
12
|
|
- <template #function>
|
13
|
|
- <Card title="转换函数" :bodyStyle="{ padding: 0, height: '280px' }">
|
14
|
|
- <template #extra>
|
15
|
|
- <Tag color="blue">Transform Function</Tag>
|
16
|
|
- <a-button @click="handleFormat" size="small">格式化</a-button>
|
17
|
|
- </template>
|
18
|
|
- <div class="ml-8">function Transform(msg, metadata) {</div>
|
19
|
|
- <div ref="aceRef" class="overflow-hidden"></div>
|
20
|
|
- <div class="ml-7">}</div>
|
21
|
|
- </Card>
|
22
|
|
- <a-button type="primary" class="mt-4" @click="testTransformFunc">测试转换功能</a-button>
|
23
|
|
- </template>
|
24
|
|
- </BasicForm>
|
25
|
|
- </BasicDrawer>
|
26
|
|
- </div>
|
27
|
|
-</template>
|
28
|
|
-
|
29
|
|
-<script lang="ts" setup>
|
30
|
|
- import { ref, computed, unref } from 'vue';
|
31
|
|
- import { useDrawerInner, BasicDrawer } from '/@/components/Drawer/index';
|
32
|
|
- import { useForm, BasicForm } from '/@/components/Form/index';
|
33
|
|
- import { formSchema } from '../config/config.data';
|
34
|
|
- import { Card, Tag } from 'ant-design-vue';
|
35
|
|
- import { createOrEditTransformScriptApi } from '/@/api/device/TransformScriptApi';
|
36
|
|
- import { useMessage } from '/@/hooks/web/useMessage';
|
37
|
|
- import ace from 'ace-builds';
|
38
|
|
- import 'ace-builds/src-noconflict/theme-chrome'; // 默认设置的主题
|
39
|
|
- import 'ace-builds/src-noconflict/theme-terminal'; // 默认设置的主题
|
40
|
|
- import 'ace-builds/src-noconflict/mode-javascript'; // 默认设置的语言模式
|
41
|
|
- import { beautify } from 'ace-builds/src-noconflict/ext-beautify.js';
|
42
|
|
- import { useAppStore } from '/@/store/modules/app';
|
43
|
|
-
|
44
|
|
- const emit = defineEmits(['register', 'isStatus', 'success']);
|
45
|
|
- const userStore = useAppStore();
|
46
|
|
- const getAceClass = computed((): string => userStore.getDarkMode);
|
47
|
|
- const isUpdate: any = ref(false);
|
48
|
|
- const isView = ref(true);
|
49
|
|
- const aceEditor = ref();
|
50
|
|
- const aceRef = ref();
|
51
|
|
- const getTitle = computed(() => (isUpdate.value ? '编辑转换脚本' : '新增转换脚本'));
|
52
|
|
- const editId = ref('');
|
53
|
|
- const [register, { setDrawerProps, closeDrawer }] = useDrawerInner((data) => {
|
54
|
|
- resetFields();
|
55
|
|
- setDrawerProps({ confirmLoading: false });
|
56
|
|
- isUpdate.value = data.isUpdate;
|
57
|
|
- initEditor(data.record?.configuration.jsScript);
|
58
|
|
- switch (isUpdate.value) {
|
59
|
|
- case 'view':
|
60
|
|
- isView.value = false;
|
61
|
|
- setDrawerProps({
|
62
|
|
- showFooter: unref(isView),
|
63
|
|
- title: '查看转换脚本',
|
64
|
|
- loading: false,
|
65
|
|
- });
|
66
|
|
- editId.value = data.record.id;
|
67
|
|
- setFieldsValue(data.record);
|
68
|
|
- break;
|
69
|
|
- case true:
|
70
|
|
- isView.value = true;
|
71
|
|
- setDrawerProps({
|
72
|
|
- showFooter: unref(isView),
|
73
|
|
- title: '编辑转换脚本',
|
74
|
|
- loading: false,
|
75
|
|
- });
|
76
|
|
- editId.value = data.record.id;
|
77
|
|
- setFieldsValue(data.record);
|
78
|
|
- break;
|
79
|
|
- case false:
|
80
|
|
- isView.value = true;
|
81
|
|
- setDrawerProps({
|
82
|
|
- showFooter: unref(isView),
|
83
|
|
- title: '新增转换脚本',
|
84
|
|
- loading: false,
|
85
|
|
- });
|
86
|
|
- break;
|
87
|
|
- }
|
88
|
|
- });
|
89
|
|
- const [registerForm, { validate, setFieldsValue, resetFields }] = useForm({
|
90
|
|
- showActionButtonGroup: false,
|
91
|
|
- colProps: { span: 24 },
|
92
|
|
- schemas: formSchema,
|
93
|
|
- });
|
94
|
|
-
|
95
|
|
- // 初始化编辑器
|
96
|
|
- const initEditor = (jsScript?: string) => {
|
97
|
|
- aceEditor.value = ace.edit(aceRef.value, {
|
98
|
|
- maxLines: 12, // 最大行数,超过会自动出现滚动条
|
99
|
|
- minLines: 12, // 最小行数,还未到最大行数时,编辑器会自动伸缩大小
|
100
|
|
- fontSize: 14, // 编辑器内字体大小
|
101
|
|
- theme: 'ace/theme/chrome', // 默认设置的主题
|
102
|
|
- mode: 'ace/mode/javascript', // 默认设置的语言模式
|
103
|
|
- tabSize: 2, // 制表符设置为 4 个空格大小
|
104
|
|
- });
|
105
|
|
-
|
106
|
|
- aceEditor.value.setOptions({
|
107
|
|
- enableBasicAutocompletion: true,
|
108
|
|
- enableLiveAutocompletion: true,
|
109
|
|
- theme: getAceClass.value === 'dark' ? 'ace/theme/terminal' : 'ace/theme/chrome',
|
110
|
|
- });
|
111
|
|
- aceEditor.value.setValue(jsScript ?? 'return {msg: msg, metadata: metadata};');
|
112
|
|
- beautify(aceEditor.value.session);
|
113
|
|
- };
|
114
|
|
-
|
115
|
|
- const testTransformFunc = () => {
|
116
|
|
- closeDrawer();
|
117
|
|
- const jsCode = aceEditor.value.getValue();
|
118
|
|
- emit('isStatus', { status: 1, jsCode });
|
119
|
|
- };
|
120
|
|
- const handleSubmit = async () => {
|
121
|
|
- const editIdPost = isUpdate.value ? { id: editId.value } : {};
|
122
|
|
- try {
|
123
|
|
- setDrawerProps({ confirmLoading: true });
|
124
|
|
- const fieldsValue = await validate();
|
125
|
|
- if (!fieldsValue) return;
|
126
|
|
- await createOrEditTransformScriptApi({
|
127
|
|
- configuration: {
|
128
|
|
- jsScript: aceEditor.value.getValue(),
|
129
|
|
- },
|
130
|
|
- type: 'org.thingsboard.rule.engine.transform.TbTransformMsgNode',
|
131
|
|
- ...fieldsValue,
|
132
|
|
- ...editIdPost,
|
133
|
|
- });
|
134
|
|
- closeDrawer();
|
135
|
|
- emit('success');
|
136
|
|
- const { createMessage } = useMessage();
|
137
|
|
- createMessage.success('保存成功');
|
138
|
|
- } catch (e) {
|
139
|
|
- } finally {
|
140
|
|
- setTimeout(() => {
|
141
|
|
- setDrawerProps({ confirmLoading: false });
|
142
|
|
- }, 300);
|
143
|
|
- }
|
144
|
|
- };
|
145
|
|
- const handleFormat = () => {
|
146
|
|
- beautify(aceEditor.value.session);
|
147
|
|
- };
|
148
|
|
- defineExpose({ aceEditor });
|
149
|
|
-</script> |
|
1
|
+<template>
|
|
2
|
+ <div>
|
|
3
|
+ <BasicDrawer
|
|
4
|
+ v-bind="$attrs"
|
|
5
|
+ :title="getTitle"
|
|
6
|
+ @register="register"
|
|
7
|
+ width="500px"
|
|
8
|
+ @ok="handleSubmit"
|
|
9
|
+ showFooter
|
|
10
|
+ >
|
|
11
|
+ <BasicForm @register="registerForm">
|
|
12
|
+ <template #function>
|
|
13
|
+ <Card title="转换函数" :bodyStyle="{ padding: 0, height: '280px' }">
|
|
14
|
+ <template #extra>
|
|
15
|
+ <Tag color="blue">Transform Function</Tag>
|
|
16
|
+ <a-button @click="handleFormat" size="small">格式化</a-button>
|
|
17
|
+ </template>
|
|
18
|
+ <div class="ml-8">function Transform(msg, metadata, msgType) {</div>
|
|
19
|
+ <div ref="aceRef" class="overflow-hidden"></div>
|
|
20
|
+ <div class="ml-7">}</div>
|
|
21
|
+ </Card>
|
|
22
|
+ <a-button type="primary" class="mt-4" @click="testTransformFunc">测试转换功能</a-button>
|
|
23
|
+ </template>
|
|
24
|
+ </BasicForm>
|
|
25
|
+ </BasicDrawer>
|
|
26
|
+ </div>
|
|
27
|
+</template>
|
|
28
|
+
|
|
29
|
+<script lang="ts" setup>
|
|
30
|
+ import { ref, computed, unref } from 'vue';
|
|
31
|
+ import { useDrawerInner, BasicDrawer } from '/@/components/Drawer/index';
|
|
32
|
+ import { useForm, BasicForm } from '/@/components/Form/index';
|
|
33
|
+ import { formSchema } from '../config/config.data';
|
|
34
|
+ import { Card, Tag } from 'ant-design-vue';
|
|
35
|
+ import { createOrEditTransformScriptApi } from '/@/api/device/TransformScriptApi';
|
|
36
|
+ import { useMessage } from '/@/hooks/web/useMessage';
|
|
37
|
+ import ace from 'ace-builds';
|
|
38
|
+ import 'ace-builds/src-noconflict/theme-chrome'; // 默认设置的主题
|
|
39
|
+ import 'ace-builds/src-noconflict/theme-terminal'; // 默认设置的主题
|
|
40
|
+ import 'ace-builds/src-noconflict/mode-javascript'; // 默认设置的语言模式
|
|
41
|
+ import { beautify } from 'ace-builds/src-noconflict/ext-beautify.js';
|
|
42
|
+ import { useAppStore } from '/@/store/modules/app';
|
|
43
|
+
|
|
44
|
+ const emit = defineEmits(['register', 'isStatus', 'success']);
|
|
45
|
+ const userStore = useAppStore();
|
|
46
|
+ const getAceClass = computed((): string => userStore.getDarkMode);
|
|
47
|
+ const isUpdate: any = ref(false);
|
|
48
|
+ const isView = ref(true);
|
|
49
|
+ const aceEditor = ref();
|
|
50
|
+ const aceRef = ref();
|
|
51
|
+ const getTitle = computed(() => (isUpdate.value ? '编辑转换脚本' : '新增转换脚本'));
|
|
52
|
+ const editId = ref('');
|
|
53
|
+ const [register, { setDrawerProps, closeDrawer }] = useDrawerInner((data) => {
|
|
54
|
+ resetFields();
|
|
55
|
+ setDrawerProps({ confirmLoading: false });
|
|
56
|
+ isUpdate.value = data.isUpdate;
|
|
57
|
+ initEditor(data.record?.configuration.jsScript);
|
|
58
|
+ switch (isUpdate.value) {
|
|
59
|
+ case 'view':
|
|
60
|
+ isView.value = false;
|
|
61
|
+ setDrawerProps({
|
|
62
|
+ showFooter: unref(isView),
|
|
63
|
+ title: '查看转换脚本',
|
|
64
|
+ loading: false,
|
|
65
|
+ });
|
|
66
|
+ editId.value = data.record.id;
|
|
67
|
+ setFieldsValue(data.record);
|
|
68
|
+ break;
|
|
69
|
+ case true:
|
|
70
|
+ isView.value = true;
|
|
71
|
+ setDrawerProps({
|
|
72
|
+ showFooter: unref(isView),
|
|
73
|
+ title: '编辑转换脚本',
|
|
74
|
+ loading: false,
|
|
75
|
+ });
|
|
76
|
+ editId.value = data.record.id;
|
|
77
|
+ setFieldsValue(data.record);
|
|
78
|
+ break;
|
|
79
|
+ case false:
|
|
80
|
+ isView.value = true;
|
|
81
|
+ setDrawerProps({
|
|
82
|
+ showFooter: unref(isView),
|
|
83
|
+ title: '新增转换脚本',
|
|
84
|
+ loading: false,
|
|
85
|
+ });
|
|
86
|
+ break;
|
|
87
|
+ }
|
|
88
|
+ });
|
|
89
|
+ const [registerForm, { validate, setFieldsValue, resetFields }] = useForm({
|
|
90
|
+ showActionButtonGroup: false,
|
|
91
|
+ colProps: { span: 24 },
|
|
92
|
+ schemas: formSchema,
|
|
93
|
+ });
|
|
94
|
+
|
|
95
|
+ // 初始化编辑器
|
|
96
|
+ const initEditor = (jsScript?: string) => {
|
|
97
|
+ aceEditor.value = ace.edit(aceRef.value, {
|
|
98
|
+ maxLines: 12, // 最大行数,超过会自动出现滚动条
|
|
99
|
+ minLines: 12, // 最小行数,还未到最大行数时,编辑器会自动伸缩大小
|
|
100
|
+ fontSize: 14, // 编辑器内字体大小
|
|
101
|
+ theme: 'ace/theme/chrome', // 默认设置的主题
|
|
102
|
+ mode: 'ace/mode/javascript', // 默认设置的语言模式
|
|
103
|
+ tabSize: 2, // 制表符设置为 4 个空格大小
|
|
104
|
+ });
|
|
105
|
+
|
|
106
|
+ aceEditor.value.setOptions({
|
|
107
|
+ enableBasicAutocompletion: true,
|
|
108
|
+ enableLiveAutocompletion: true,
|
|
109
|
+ theme: getAceClass.value === 'dark' ? 'ace/theme/terminal' : 'ace/theme/chrome',
|
|
110
|
+ });
|
|
111
|
+ aceEditor.value.setValue(
|
|
112
|
+ jsScript ?? 'return {msg: msg, metadata: metadata, msgType: msgType};'
|
|
113
|
+ );
|
|
114
|
+ beautify(aceEditor.value.session);
|
|
115
|
+ };
|
|
116
|
+
|
|
117
|
+ const testTransformFunc = () => {
|
|
118
|
+ closeDrawer();
|
|
119
|
+ const jsCode = aceEditor.value.getValue();
|
|
120
|
+ emit('isStatus', { status: 1, jsCode });
|
|
121
|
+ };
|
|
122
|
+ const handleSubmit = async () => {
|
|
123
|
+ const editIdPost = isUpdate.value ? { id: editId.value } : {};
|
|
124
|
+ try {
|
|
125
|
+ setDrawerProps({ confirmLoading: true });
|
|
126
|
+ const fieldsValue = await validate();
|
|
127
|
+ if (!fieldsValue) return;
|
|
128
|
+ await createOrEditTransformScriptApi({
|
|
129
|
+ configuration: {
|
|
130
|
+ jsScript: aceEditor.value.getValue(),
|
|
131
|
+ },
|
|
132
|
+ type: 'org.thingsboard.rule.engine.transform.TbTransformMsgNode',
|
|
133
|
+ ...fieldsValue,
|
|
134
|
+ ...editIdPost,
|
|
135
|
+ });
|
|
136
|
+ closeDrawer();
|
|
137
|
+ emit('success');
|
|
138
|
+ const { createMessage } = useMessage();
|
|
139
|
+ createMessage.success('保存成功');
|
|
140
|
+ } catch (e) {
|
|
141
|
+ } finally {
|
|
142
|
+ setTimeout(() => {
|
|
143
|
+ setDrawerProps({ confirmLoading: false });
|
|
144
|
+ }, 300);
|
|
145
|
+ }
|
|
146
|
+ };
|
|
147
|
+ const handleFormat = () => {
|
|
148
|
+ beautify(aceEditor.value.session);
|
|
149
|
+ };
|
|
150
|
+ defineExpose({ aceEditor });
|
|
151
|
+</script> |
...
|
...
|
|