|
| 1 | +<template> |
| 2 | + <div class="meta-array-wrap"> |
| 3 | + <meta-list> |
| 4 | + <template #items> |
| 5 | + <vue-draggable-next |
| 6 | + :list="itemsOptions.optionsList" |
| 7 | + :disabled="disableDrag" |
| 8 | + handle=".tiny-svg-size" |
| 9 | + @change="dragEnd" |
| 10 | + > |
| 11 | + <div |
| 12 | + v-for="(item, index) in itemsOptions.optionsList" |
| 13 | + :key="index" |
| 14 | + > |
| 15 | + <meta-list-item |
| 16 | + :item="item" |
| 17 | + :index="index" |
| 18 | + :dataScource="itemsOptions" |
| 19 | + :currentIndex="state.currentIndex" |
| 20 | + :expand="expand" |
| 21 | + @changeItem="changeItem" |
| 22 | + @deleteItem="deleteItem" |
| 23 | + @editItem="editItem" |
| 24 | + @hideItem="hideItem" |
| 25 | + @showItem="showItem" |
| 26 | + > |
| 27 | + <template #content> |
| 28 | + <span>{{ translate(item[itemsOptions.textField]) || item.type }}</span> |
| 29 | + </template> |
| 30 | + <template #metaForm> |
| 31 | + <meta-child-item |
| 32 | + type="array" |
| 33 | + :meta="meta" |
| 34 | + :index="index" |
| 35 | + :arrayIndex="state.currentIndex" |
| 36 | + @update:modelValue="onValueChange(index, $event)" |
| 37 | + ></meta-child-item> |
| 38 | + </template> |
| 39 | + </meta-list-item> |
| 40 | + </div> |
| 41 | + </vue-draggable-next> |
| 42 | + </template> |
| 43 | + <template #bottom> |
| 44 | + <div |
| 45 | + class="add" |
| 46 | + @click="addItem" |
| 47 | + > |
| 48 | + <svg-icon name="plus"></svg-icon> |
| 49 | + <span>新增</span> |
| 50 | + </div> |
| 51 | + </template> |
| 52 | + </meta-list> |
| 53 | + </div> |
| 54 | +</template> |
| 55 | + |
| 56 | +<script> |
| 57 | +import { computed, reactive } from 'vue'; |
| 58 | +import { iconDel, iconEdit, iconEyeclose, iconEyeopen } from '@opentiny/vue-icon'; |
| 59 | +import MetaListItem from './MetaListItem.vue'; |
| 60 | +import MetaChildItem from './MetaChildItem.vue'; |
| 61 | +import MetaList from './MetaList.vue'; |
| 62 | +import { TranslateService } from '@opentiny/tiny-engine'; |
| 63 | +import { VueDraggableNext } from 'vue-draggable-next'; |
| 64 | +
|
| 65 | +export default { |
| 66 | + name: 'ArrayItemConfigurator', |
| 67 | + components: { |
| 68 | + MetaList, |
| 69 | + MetaListItem, |
| 70 | + MetaChildItem, |
| 71 | + VueDraggableNext, |
| 72 | + }, |
| 73 | + inheritAttrs: false, |
| 74 | + props: { |
| 75 | + meta: { |
| 76 | + type: Object, |
| 77 | + default: () => {}, |
| 78 | + }, |
| 79 | + expand: { |
| 80 | + type: Boolean, |
| 81 | + default: false, |
| 82 | + }, |
| 83 | + disableDrag: { |
| 84 | + type: Boolean, |
| 85 | + default: false, |
| 86 | + }, |
| 87 | + }, |
| 88 | + setup(props, { emit }) { |
| 89 | + const { translate } = TranslateService.apis; |
| 90 | + const columnsList = computed(() => { |
| 91 | + return props.meta.widget.props.modelValue?.value || props.meta.widget.props.modelValue || []; |
| 92 | + }); |
| 93 | +
|
| 94 | + const itemsOptions = computed(() => ({ |
| 95 | + valueField: 'prop', |
| 96 | + textField: props.meta.widget.props.textField || 'value', |
| 97 | + btnList: [ |
| 98 | + { |
| 99 | + title: '编辑', |
| 100 | + type: 'edit', |
| 101 | + icon: iconEdit(), |
| 102 | + }, |
| 103 | + { |
| 104 | + title: '删除', |
| 105 | + type: 'delete', |
| 106 | + icon: iconDel(), |
| 107 | + }, |
| 108 | + { |
| 109 | + title: '显示', |
| 110 | + type: 'show', |
| 111 | + icon: iconEyeclose(), |
| 112 | + }, |
| 113 | + { |
| 114 | + title: '隐藏', |
| 115 | + type: 'hide', |
| 116 | + icon: iconEyeopen(), |
| 117 | + }, |
| 118 | + ], |
| 119 | + optionsList: columnsList.value, |
| 120 | + name: props.name, |
| 121 | + draggable: true, |
| 122 | + })); |
| 123 | +
|
| 124 | + const state = reactive({ |
| 125 | + currentIndex: -1, |
| 126 | + }); |
| 127 | +
|
| 128 | + const editItem = (data) => { |
| 129 | + state.currentIndex = data.index; |
| 130 | + }; |
| 131 | +
|
| 132 | + const updatedColumns = () => { |
| 133 | + emit('update:modelValue', { ...props.meta.widget.props.modelValue, value: columnsList.value }); |
| 134 | + }; |
| 135 | +
|
| 136 | + const addItem = () => { |
| 137 | + const defaultValue = props.meta.defaultValue?.[0] || null; |
| 138 | + const newOption = ['string', 'boolean', 'number'].includes(props.meta.widget.props.type) |
| 139 | + ? defaultValue |
| 140 | + : { ...defaultValue }; |
| 141 | +
|
| 142 | + columnsList.value.push(newOption); |
| 143 | + state.currentIndex = columnsList.value.length - 1; |
| 144 | + updatedColumns(); |
| 145 | + }; |
| 146 | +
|
| 147 | + const deleteItem = (params) => { |
| 148 | + columnsList.value.splice(params.index, 1); |
| 149 | + updatedColumns(); |
| 150 | + }; |
| 151 | +
|
| 152 | + const changeItem = (item) => { |
| 153 | + columnsList.value[item.index] = item.data; |
| 154 | + updatedColumns(); |
| 155 | + }; |
| 156 | +
|
| 157 | + const hideItem = (params) => { |
| 158 | + columnsList.value[params.index].itemVisible = false; |
| 159 | + updatedColumns(); |
| 160 | + }; |
| 161 | +
|
| 162 | + const showItem = (params) => { |
| 163 | + columnsList.value[params.index].itemVisible = true; |
| 164 | + updatedColumns(); |
| 165 | + }; |
| 166 | +
|
| 167 | + const onValueChange = (index, { propertyKey, propertyValue }) => { |
| 168 | + if (propertyValue === '' || propertyValue === undefined || propertyValue === null) { |
| 169 | + delete columnsList.value[index][propertyKey]; |
| 170 | + } else { |
| 171 | + columnsList.value[index][propertyKey] = propertyValue; |
| 172 | + } |
| 173 | + updatedColumns(); |
| 174 | + }; |
| 175 | +
|
| 176 | + const dragEnd = () => { |
| 177 | + updatedColumns(); |
| 178 | + }; |
| 179 | +
|
| 180 | + return { |
| 181 | + state, |
| 182 | + itemsOptions, |
| 183 | + columnsList, |
| 184 | + editItem, |
| 185 | + addItem, |
| 186 | + deleteItem, |
| 187 | + changeItem, |
| 188 | + hideItem, |
| 189 | + showItem, |
| 190 | + onValueChange, |
| 191 | + translate, |
| 192 | + dragEnd, |
| 193 | + }; |
| 194 | + }, |
| 195 | +}; |
| 196 | +</script> |
| 197 | +
|
| 198 | +<style lang="less" scoped> |
| 199 | +.meta-array-wrap { |
| 200 | + font-size: 12px; |
| 201 | + display: block; |
| 202 | +} |
| 203 | +.add { |
| 204 | + display: flex; |
| 205 | + align-items: center; |
| 206 | + color: var(--te-configurator-common-text-color-emphasize); |
| 207 | + &:hover { |
| 208 | + cursor: pointer; |
| 209 | + } |
| 210 | +
|
| 211 | + & .icon-plus { |
| 212 | + font-size: 14px; |
| 213 | + margin-right: 5px; |
| 214 | + } |
| 215 | +} |
| 216 | +</style> |
0 commit comments