PenStatus.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <template>
  2. <div class="props">
  3. <div v-if="props.pen.triggers && props.pen.triggers.length">
  4. <div
  5. class="flex between tigger-item"
  6. v-for="(trigger, i) in props.pen.triggers"
  7. :value="i"
  8. >
  9. <t-input
  10. style="width: 190px; margin-left: 20px"
  11. v-model="trigger.name"
  12. :placeholder="$t('状态场景名')"
  13. />
  14. <div class="flex operation">
  15. <div class="icon-box icon-item-box" @click="onEditTrigger(trigger)">
  16. <Edit2Icon />
  17. </div>
  18. <t-popconfirm :content="$t('确认删除这条状态场景吗?')" placement="bottom" @confirm="delTrigger(i)">
  19. <div class="icon-box">
  20. <DeleteIcon />
  21. </div>
  22. </t-popconfirm>
  23. </div>
  24. </div>
  25. </div>
  26. <div class="flex column center blank" v-else>
  27. <img src="/img/blank.png">
  28. <div class="gray center">{{$t('还没有状态场景')}}</div>
  29. <!-- <div class="mt-8">
  30. <t-button style="height: 30px" @click="onAddTrigger">
  31. 添加状态
  32. </t-button>
  33. </div> -->
  34. </div>
  35. <div class="flex center mt-8">
  36. <t-button style="height: 30px; width: 90%; margin: 12px" @click="onAddTrigger">
  37. {{$t('添加状态场景')}}
  38. </t-button>
  39. </div>
  40. </div>
  41. <t-drawer
  42. v-model:visible="drawer.visible"
  43. :footer="false"
  44. :header="drawer.header"
  45. :size="'800px'"
  46. >
  47. <div v-if="trigger">
  48. <t-collapse
  49. v-model="openedCollapses"
  50. :borderless="true"
  51. :expand-on-row-click="true"
  52. >
  53. <t-collapse-panel v-for="(state, i) in trigger.status" :value="i">
  54. <template #header>
  55. <t-input v-model="state.name" style="width:90%" @click="panelInputClick($event)" :placeholder="$t('状态名')"></t-input>
  56. </template>
  57. <template #headerRightContent>
  58. <t-space size="small" @click.stop>
  59. <t-popconfirm placement="left" @confirm="trigger.status.splice(i, 1)" :content="$t('确认删除该状态吗?')">
  60. <delete-icon class="hover"></delete-icon>
  61. </t-popconfirm>
  62. </t-space>
  63. </template>
  64. <!-- <Conditions :key="temCKey" :data="state" /> -->
  65. <div class="props">
  66. <div v-if="state.conditions && state.conditions.length > 0" class="form-item banner">
  67. <label>{{$t('满足条件')}}</label>
  68. <div class="w-full flex middle between">
  69. <div></div>
  70. <t-radio-group class="ml-8" v-model="state.conditionType">
  71. <t-radio value="and"> {{$t('所有')}} </t-radio>
  72. <t-radio value="or"> {{$t('任意')}} </t-radio>
  73. </t-radio-group>
  74. </div>
  75. </div>
  76. <div v-for="(c, index) in state.conditions" class="mb-12">
  77. <div class="flex middle between" style="margin-top:10px;">
  78. <div class="flex middle">
  79. <arrow-right-icon class="mr-4"></arrow-right-icon>
  80. {{$t(' 条件')}}{{ index + 1 }}
  81. </div>
  82. <close-icon
  83. class="hover"
  84. @click="state.conditions.splice(index, 1)"
  85. />
  86. </div>
  87. <div class="">
  88. <div class="form-item mt-4 px-16">
  89. <label>{{$t('条件类型')}}</label>
  90. <t-radio-group class="ml-8" v-model="c.type">
  91. <t-radio value> {{$t('关系条件')}} </t-radio>
  92. <t-radio value="fn"> {{$t('高级条件')}} </t-radio>
  93. </t-radio-group>
  94. </div>
  95. <template v-if="!c.type">
  96. <div class="form-item mt-8 px-16">
  97. <label>{{$t( '比较条件' )}}</label>
  98. <t-select-input
  99. v-model:inputValue="c.key"
  100. :value="c.keyLabel"
  101. v-model:popupVisible="c.keyPopupVisible"
  102. allow-input
  103. clearable
  104. @clear="c.keyLabel = undefined"
  105. @focus="c.keyPopupVisible = true"
  106. @blur="c.keyPopupVisible = false"
  107. @input-change="onKeyInput(c)"
  108. class="shrink-0"
  109. style="width: 132px"
  110. >
  111. <template #panel>
  112. <ul style="padding: 8px 12px">
  113. <li
  114. v-for="item in cprops"
  115. :key="item.value"
  116. @click="
  117. c.key = item.value;
  118. c.keyLabel = item.label;
  119. c.keyPopupVisible = false;
  120. "
  121. >
  122. {{ item.label }}
  123. </li>
  124. </ul>
  125. </template>
  126. </t-select-input>
  127. <t-select
  128. style="width: 132px"
  129. v-model="c.operator"
  130. :placeholder="$t('关系运算')"
  131. :options="operatorOptions"
  132. clearable
  133. class="shrink-0"
  134. />
  135. <t-select
  136. style="width: 132px"
  137. v-model="c.valueType"
  138. class="shrink-0"
  139. :placeholder="$t('固定值')"
  140. >
  141. <t-option key="" value="" :label="$t('固定值')">
  142. {{$t( '固定值' )}}
  143. </t-option>
  144. <t-option key="prop" value="prop" :label="$t('对象属性值')">
  145. {{$t('对象属性')}}
  146. </t-option>
  147. </t-select>
  148. <t-input
  149. v-if="!c.valueType"
  150. v-model="c.value"
  151. @change="valueChange($event, c)"
  152. class="shrink-0"
  153. />
  154. <template v-else>
  155. <t-tree-select
  156. style="width: 132px"
  157. v-model="c.target"
  158. :data="penTree"
  159. filterable
  160. :placeholder="$t('对象')"
  161. class="shrink-0"
  162. @change="onChangeTriggerTarget(c)"
  163. />
  164. <t-select-input
  165. v-model:inputValue="c.value"
  166. :value="c.label"
  167. v-model:popupVisible="c.popupVisible"
  168. allow-input
  169. clearable
  170. @clear="c.label = undefined"
  171. @focus="c.popupVisible = true"
  172. @blur="c.popupVisible = false"
  173. @input-change="onInput(c)"
  174. class="shrink-0"
  175. :placeholder="$t('属性')"
  176. >
  177. <template #panel>
  178. <ul style="padding: 8px 12px">
  179. <li
  180. v-for="item in cprops"
  181. :key="item.value"
  182. @click="
  183. c.value = item.value;
  184. c.label = item.label;
  185. c.popupVisible = false;
  186. "
  187. >
  188. {{ item.label }}
  189. </li>
  190. </ul>
  191. </template>
  192. </t-select-input>
  193. </template>
  194. </div>
  195. </template>
  196. <template v-else>
  197. <div class="px-16">
  198. <div>function condition(pen) {</div>
  199. <CodeEditor
  200. class="mt-4"
  201. @change="codeChange($event, c)"
  202. v-model="c.fnJs"
  203. />
  204. <div class="mt-4">}</div>
  205. </div>
  206. </template>
  207. </div>
  208. </div>
  209. <div class="mt-8">
  210. <a @click="addTriggerCondition(state)"> + {{$t('添加触发条件')}} </a>
  211. </div>
  212. </div>
  213. <div class="form-item banner mt-16">
  214. <label>{{$t('执行动作')}}</label>
  215. </div>
  216. <Actions class="mt-8" :key="temAKey" :data="state" />
  217. </t-collapse-panel>
  218. </t-collapse>
  219. <div class="flex center mt-8">
  220. <t-button
  221. style="height: 30px; width: 100%; margin: 12px"
  222. @click="addTriggerStatus(trigger)"
  223. variant="outline"
  224. >
  225. <template #icon><add-icon /></template>
  226. {{$t( '添加状态' )}}
  227. </t-button>
  228. </div>
  229. </div>
  230. </t-drawer>
  231. </template>
  232. <script lang="ts" setup>
  233. import { reactive, defineComponent, ref, getCurrentInstance } from 'vue';
  234. import Actions from './Actions.vue';
  235. // import Conditions from './Conditions.vue';
  236. import { Edit2Icon, DeleteIcon, ArrowRightIcon, CloseIcon, AddIcon } from 'tdesign-icons-vue-next';
  237. import { getPenTree, typeOptions, changeType } from '@/services/common';
  238. import CodeEditor from '@/views/components/common/CodeEditor.vue';
  239. const { proxy } = getCurrentInstance();
  240. const $t = proxy.$t
  241. const props = defineProps<{
  242. pen: any;
  243. }>();
  244. const trigger = ref();
  245. const drawer = reactive({
  246. visible: false,
  247. header: $t('编辑状态'),
  248. });
  249. const openedCollapses = ref([0]);
  250. const onAddTrigger = () => {
  251. if (!props.pen.triggers) {
  252. props.pen.triggers = [];
  253. }
  254. const i = props.pen.triggers.length;
  255. props.pen.triggers.push({
  256. name: $t('状态场景')+(i + 1),
  257. status: [
  258. {
  259. name: $t('状态1'),
  260. conditions: [],
  261. actions: [],
  262. },
  263. ],
  264. // key: '', //属性key
  265. });
  266. openedCollapses.value.push(i);
  267. };
  268. const delTrigger = (i: number) => {
  269. props.pen.triggers.splice(i, 1);
  270. };
  271. // const temCKey = ref(0);
  272. const temAKey = ref(0);
  273. const onEditTrigger = (data: any) => {
  274. // 辅助更新代码块数据
  275. // temCKey.value = Math.random();
  276. data.status?.forEach((item: any) => {
  277. item.conditions?.forEach((c: any) => {
  278. if (!c.keyLabel) {
  279. c.keyLabel = c.key;
  280. }
  281. });
  282. });
  283. temAKey.value = Math.random();
  284. trigger.value = data;
  285. drawer.header = data.name;
  286. drawer.visible = true;
  287. penTree.value = getPenTree();
  288. };
  289. const editLast = (index) => {
  290. if(index>-1){
  291. onEditTrigger(props.pen.triggers[index]);
  292. }
  293. };
  294. const addTriggerStatus = (triger: any) => {
  295. const i = props.pen.triggers.length;
  296. triger.status.push({
  297. name: $t('状态') + i,
  298. conditionType: 'and',
  299. conditions: [],
  300. actions: [],
  301. });
  302. };
  303. const panelInputClick = (e) => {
  304. e.e.stopPropagation();
  305. };
  306. const penTree: any = ref([]);
  307. const onChangeTriggerTarget = (c: any) => {
  308. const target: any = meta2d.findOne(c.target);
  309. if (target && target.realTimes) {
  310. for (const item of target.realTimes) {
  311. const found = cprops.value.findIndex(
  312. (elem: any) => elem.value === item.key
  313. );
  314. if (found < 0) {
  315. cprops.value.push({
  316. value: item.key,
  317. label: item.label,
  318. });
  319. }
  320. }
  321. }
  322. };
  323. const addTriggerCondition = (data: any) => {
  324. if (!data.conditionType) {
  325. data.conditionType = 'and';
  326. }
  327. data.conditions.push({
  328. type: '',
  329. operator: '=',
  330. valueType: '',
  331. });
  332. };
  333. const onKeyInput = (item: any) => {
  334. item.keyLabel = item.key;
  335. };
  336. const onInput = (item: any) => {
  337. item.label = item.value;
  338. };
  339. const cprops = ref<any>([
  340. {
  341. value: 'x',
  342. label: 'X',
  343. },
  344. {
  345. value: 'y',
  346. label: 'Y',
  347. },
  348. {
  349. value: 'width',
  350. label: $t('宽'),
  351. },
  352. {
  353. value: 'height',
  354. label: $t('高'),
  355. },
  356. {
  357. value: 'visible',
  358. label: $t('显示'),
  359. },
  360. {
  361. value: 'text',
  362. label: $t('文字'),
  363. },
  364. {
  365. value: 'color',
  366. label: $t('颜色'),
  367. },
  368. {
  369. value: 'background',
  370. label: $t('背景颜色'),
  371. },
  372. {
  373. value: 'progress',
  374. label: $t('进度'),
  375. },
  376. {
  377. value: 'progressColor',
  378. label: $t('进度颜色'),
  379. },
  380. {
  381. value: 'showChild',
  382. label: $t('状态'),
  383. },
  384. {
  385. value: 'rotate',
  386. label: $t('旋转'),
  387. },
  388. {
  389. value: 'disabled',
  390. label: $t('禁用'),
  391. },
  392. {
  393. value: 'selectedKey',
  394. label: $t('单选选中值'),
  395. }
  396. ]);
  397. const operatorOptions = [
  398. { label: '=', value: '=' },
  399. { label: '!=', value: '!=' },
  400. { label: '>', value: '>' },
  401. { label: '<', value: '<' },
  402. { label: '>=', value: '>=' },
  403. { label: '<=', value: '<=' },
  404. { label: $t('包含'), value: '[)' },
  405. { label: $t('不包含'), value: '![)' },
  406. ];
  407. const valueChange = (e, c: any) => {
  408. c.value = changeType(e);
  409. };
  410. const codeChange = (e: any, a: any) => {
  411. a.fn = null;
  412. };
  413. defineExpose({
  414. editLast,
  415. });
  416. </script>
  417. <style lang="postcss" scoped>
  418. .tigger-item {
  419. /* height: 50px; */
  420. /* line-height: 50px; */
  421. margin: 12px 8px;
  422. align-items: center;
  423. :deep(.t-input) {
  424. border-color: transparent;
  425. &:hover {
  426. border-color: var(--color-border-input);
  427. }
  428. }
  429. .operation {
  430. height: 40px;
  431. /* line-height: 40px; */
  432. align-items: center;
  433. .icon-box {
  434. width: 24px;
  435. height: 24px;
  436. margin: 4px;
  437. text-align: center;
  438. line-height: 24px;
  439. border-radius: 4px;
  440. &:hover {
  441. background: var(--td-brand-color-light);
  442. }
  443. .t-icon {
  444. width: 14px;
  445. height: 14px;
  446. }
  447. }
  448. }
  449. &:hover {
  450. background: var(--color-background-input);
  451. border-radius: 4px;
  452. }
  453. }
  454. :deep(.t-collapse) {
  455. .t-collapse-panel__header {
  456. .t-input {
  457. border-color: transparent;
  458. &:hover {
  459. border-color: var(--color-border-input);
  460. }
  461. }
  462. }
  463. .t-collapse-panel__icon:hover {
  464. background: none;
  465. svg {
  466. color: var(--color-primary);
  467. }
  468. }
  469. .banner {
  470. background-color: var(--color-background-input);
  471. padding: 0 12px;
  472. }
  473. }
  474. /* .props {
  475. :deep(.value-input) {
  476. max-width: 95px;
  477. }
  478. } */
  479. /* :deep(.value-label) {
  480. width: 50px;
  481. overflow: hidden;
  482. text-overflow: ellipsis;
  483. white-space: nowrap;
  484. } */
  485. </style>