WithIconSelect.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <script setup lang="ts">
  2. import type { DropdownInstance } from 'element-plus'
  3. import type { IconDropdownItem } from '@/types/dataAnalysis'
  4. import { onMounted, ref, reactive, watch } from 'vue'
  5. import { initLoadResouce } from '@/utils/resource'
  6. import { useCommonStore } from '@/stores/useCommon'
  7. const { selectInfo, tempMultipleChioce, saveSelectInfo, saveTempMultipleChioce } = useCommonStore()
  8. interface DropdownInfo {
  9. isRadio?: boolean // 是否是单选
  10. slectInfo: Array<IconDropdownItem>
  11. }
  12. // props
  13. const props = withDefaults(defineProps<DropdownInfo>(), {
  14. isRadio: true
  15. })
  16. /**
  17. * @description: 初始化选择信息
  18. * @return {*}
  19. */
  20. const selectBaseInfo = reactive<Array<IconDropdownItem>>(
  21. JSON.parse(JSON.stringify(props.slectInfo))
  22. )
  23. // 判断是不是通过确认框关闭的。主要服务于多选的情况
  24. // 原理是因为点击确认关闭的话,确认事件会先触发,然后再触发关闭事件
  25. // 由此来去判断是否需要把多选框的信息恢复
  26. const isConfimClose = ref<Boolean>(false)
  27. // emits
  28. const emits = defineEmits(['changePf'])
  29. // 下拉框
  30. const dropDownRef = ref<DropdownInstance>()
  31. // 资源的加载路径
  32. const resourceInfo: Record<string, string> = selectBaseInfo.reduce(
  33. (acc, item) => {
  34. acc[item.value] = item.icon
  35. return acc
  36. },
  37. {} as Record<string, string>
  38. )
  39. // 使用blob的资源路径信息
  40. const blobUrlInfo = reactive<Record<string, string>>({})
  41. // 备份信息
  42. const backupInfo = reactive<Array<IconDropdownItem>>([])
  43. /**
  44. * @description: 确认选择
  45. * 这里主要用于多选的情况使用,对于单选,即使点击确认按钮也不会有二次请求,因为seleinfo没有改变
  46. * @return {*}
  47. */
  48. const confirmSelect = () => {
  49. isConfimClose.value = true // 通过确认事件关闭
  50. // 如果没有选中任何平台,则直接返回,不进行任何操作
  51. if (selectBaseInfo.filter((item) => item.isSelected).length === 0) {
  52. dropDownRef.value?.handleClose()
  53. return
  54. }
  55. Object.assign(backupInfo, JSON.parse(JSON.stringify(selectBaseInfo)))
  56. dropDownRef.value?.handleClose()
  57. if (props.isRadio) return
  58. // 去把state的数据更新一下
  59. tempMultipleChioce.pf = selectBaseInfo.filter((item) => item.isSelected).map((item) => item.value)
  60. saveTempMultipleChioce() // 保存到本地
  61. }
  62. /**
  63. * @description: 取消选择,当取消选择后,需要恢复到原来的状态
  64. * @return {*}
  65. */
  66. const cancleSelect = () => {
  67. dropDownRef.value?.handleClose()
  68. Object.assign(selectBaseInfo, backupInfo)
  69. }
  70. /**
  71. * @description: 下拉框出现与消失的处理
  72. * @param {*} state 展示状态,值为true或者false,代表打开或者关闭
  73. * @return {*}
  74. */
  75. const dropdownVis = (state: boolean) => {
  76. if (state) {
  77. // 打开的时候,需要备份一下当前选择
  78. Object.assign(backupInfo, JSON.parse(JSON.stringify(selectBaseInfo)))
  79. } else {
  80. // 关闭的时候,如果一个都没有选中,那么就需要把备份的信息恢复回去
  81. // 这里只针对于多选情况,因为单选的逻辑中不存在一个都没有选中的情况,因为无法取消已经选中
  82. // 而多选的情况下,因为即使可以取消,也不会立即改变selectInfo中的信息,只有点击了确认按钮才会改变,所以这里只需要恢复props的selectinfo就行了
  83. // 不是点击确认按钮关闭的多选框,且是多选按钮,也需要恢复
  84. if (!selectBaseInfo.find((item) => item.isSelected) || (!isConfimClose.value && !props.isRadio))
  85. Object.assign(selectBaseInfo, backupInfo)
  86. if (isConfimClose.value) isConfimClose.value = false // 把状态重置
  87. }
  88. }
  89. /**
  90. * @description: 选择事件,当目前可以选择或者是这个选项已经被选中的时候,就允许改变他的状态
  91. * @param {*} item 当前的选项信息
  92. * @return {*}
  93. */
  94. const selectPf = (item: any) => {
  95. if (props.isRadio) {
  96. selectInfo.pf = []
  97. selectInfo.pf.push(item.value)
  98. saveSelectInfo() // 选择之后保存到本地一次
  99. } else {
  100. // 多选就取反就行
  101. item.isSelected = !item.isSelected
  102. }
  103. }
  104. /**
  105. * @description: 同步selectinfo的信息到页面上
  106. * @return {*}
  107. */
  108. const syncSelectInfo = () => {
  109. selectBaseInfo.forEach((item) => {
  110. item.isSelected = selectInfo.pf.includes(item.value)
  111. })
  112. }
  113. /**
  114. * @description: 同步tempMultipleChioce的信息到页面上
  115. * @return {*}
  116. */
  117. const syncTempMultipleChioce = () => {
  118. selectBaseInfo.forEach((item) => {
  119. item.isSelected = tempMultipleChioce.pf.includes(item.value)
  120. })
  121. }
  122. /**
  123. * @description: 监听selectinfo的变化,然后同步到页面上
  124. * @return {*}
  125. */
  126. const watchSelectPf = watch(
  127. () => selectInfo.pf,
  128. () => {
  129. syncSelectInfo()
  130. },
  131. { deep: true }
  132. )
  133. /**
  134. * @description: 监听tempMultipleChioce的变化,然后同步到页面上
  135. * @return {*}
  136. */
  137. const watchTempMultipleChioce = watch(
  138. () => tempMultipleChioce.pf,
  139. () => {
  140. syncTempMultipleChioce()
  141. },
  142. { deep: true }
  143. )
  144. /**
  145. * @description: 根据本地保存的方案信息,去初始化selectinfo的信息
  146. * @return {*}
  147. */
  148. const initSelectInfo = () => {
  149. if (props.isRadio) {
  150. watchTempMultipleChioce() // 如果是单选进来的,在这儿把多选的事件取消掉
  151. syncSelectInfo() // 同步一下数据
  152. } else {
  153. watchSelectPf() // 与上面同理
  154. syncTempMultipleChioce() // 同理
  155. }
  156. }
  157. // 去加载一下本地的信息
  158. initSelectInfo()
  159. onMounted(() => {
  160. // 去加载所有需要的资源
  161. initLoadResouce(resourceInfo).then((data) => {
  162. Object.assign(blobUrlInfo, data)
  163. })
  164. })
  165. </script>
  166. <template>
  167. <div>
  168. <el-dropdown
  169. trigger="click"
  170. @visible-change="dropdownVis"
  171. :hide-on-click="props.isRadio"
  172. ref="dropDownRef"
  173. >
  174. <span class="displayBox">
  175. <div class="displayIcon">
  176. <span class="iconItem" v-for="item in selectBaseInfo">
  177. <el-image
  178. v-if="item.isSelected"
  179. style="width: 20px; height: 20px; margin-right: 5px"
  180. :src="blobUrlInfo[item.value]"
  181. :fit="'cover'"
  182. />
  183. </span>
  184. </div>
  185. <el-icon class="el-icon--right">
  186. <arrow-down />
  187. </el-icon>
  188. </span>
  189. <template #dropdown>
  190. <el-dropdown-menu :class="{ radioMenu: isRadio }">
  191. <el-dropdown-item
  192. v-for="item in selectBaseInfo"
  193. :key="item.value"
  194. :value="item.value"
  195. @click="selectPf(item)"
  196. >
  197. <el-image
  198. style="width: 20px; height: 20px; margin-right: 5px"
  199. :src="blobUrlInfo[item.value]"
  200. :fit="'cover'"
  201. />
  202. <!-- 禁用掉原生的点击事件,自己实现点击 -->
  203. <el-checkbox v-model="item.isSelected" size="small" @click.native.prevent="return" />
  204. </el-dropdown-item>
  205. </el-dropdown-menu>
  206. <span class="btnGroup" v-if="!isRadio">
  207. <el-button class="btnItem" size="small" type="primary" @click="confirmSelect"
  208. >确认</el-button
  209. >
  210. <el-button class="btnItem" size="small" @click="cancleSelect">取消</el-button>
  211. </span>
  212. </template>
  213. </el-dropdown>
  214. </div>
  215. </template>
  216. <style scoped>
  217. .displayBox {
  218. display: flex;
  219. align-items: center;
  220. }
  221. .displayIcon {
  222. display: flex;
  223. align-items: center;
  224. }
  225. .btnGroup {
  226. display: flex;
  227. justify-content: center;
  228. }
  229. .btnItem {
  230. margin-right: 4px;
  231. margin-left: 4px;
  232. }
  233. .radioMenu {
  234. margin-right: 15px;
  235. }
  236. .disabledSelect {
  237. cursor: not-allowed !important;
  238. }
  239. </style>