CrossingMultiView.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <div class="crossing-multi-view">
  3. <!-- 展开模式下的返回栏 -->
  4. <div v-if="expandedId" class="expand-toolbar">
  5. <span class="back-btn" @click="exitExpand">&larr; 返回多路口视图</span>
  6. </div>
  7. <draggable
  8. v-model="localSlots"
  9. class="multi-view-grid"
  10. :class="gridClass"
  11. :style="gridStyle"
  12. :animation="300"
  13. :swap-threshold="0.5"
  14. ghost-class="drag-ghost"
  15. chosen-class="drag-chosen"
  16. drag-class="drag-active"
  17. handle=".drag-handle"
  18. :disabled="!!expandedId"
  19. @end="onDragEnd"
  20. >
  21. <div
  22. v-for="(slot, index) in visibleSlots"
  23. :key="'panel-' + slot.data.id"
  24. class="grid-cell"
  25. @dblclick="handleDblClick(slot)"
  26. >
  27. <div class="cell-header">
  28. <div class="drag-handle" title="拖拽换位" v-if="!expandedId">
  29. <span class="drag-icon">&#x2807;</span>
  30. </div>
  31. <CrossingDetailHeader
  32. :currentRoute="slot.headerData ? slot.headerData.currentRoute : { name: slot.data.label || slot.data.name }"
  33. :intersectionData="slot.headerData ? slot.headerData.intersectionData : {}"
  34. :cycleLength="slot.headerData ? slot.headerData.cycleLength : 0"
  35. />
  36. <span class="cell-close" @click.stop="handleRemove(slot.data.id)">✕</span>
  37. </div>
  38. <div class="cell-body">
  39. <CrossingDetailPanel
  40. :id="slot.data.id"
  41. v-bind="slot.data"
  42. :preloadedData="slot.headerData"
  43. />
  44. </div>
  45. </div>
  46. </draggable>
  47. </div>
  48. </template>
  49. <script>
  50. import draggable from 'vuedraggable';
  51. import CrossingDetailPanel from '@/components/ui/CrossingDetailPanel.vue';
  52. import CrossingDetailHeader from '@/components/ui/CrossingDetailHeader.vue';
  53. import { apiGetCrossingDetailData } from '@/api';
  54. export default {
  55. name: 'CrossingMultiView',
  56. components: {
  57. draggable,
  58. CrossingDetailPanel,
  59. CrossingDetailHeader
  60. },
  61. props: {
  62. crossings: {
  63. type: Array,
  64. default: () => []
  65. },
  66. maxSlots: {
  67. type: Number,
  68. default: 1,
  69. validator: v => v >= 1 && v <= 6
  70. },
  71. onRemove: {
  72. type: Function,
  73. default: null
  74. },
  75. onReorder: {
  76. type: Function,
  77. default: null
  78. }
  79. },
  80. data() {
  81. return {
  82. localSlots: [],
  83. expandedId: null,
  84. rebuildVersion: 0
  85. };
  86. },
  87. computed: {
  88. // 实际面板数量
  89. panelCount() {
  90. return this.crossings.length;
  91. },
  92. visibleSlots() {
  93. if (this.expandedId) {
  94. const found = this.localSlots.find(
  95. s => s.data && s.data.id === this.expandedId
  96. );
  97. return found ? [found] : this.localSlots;
  98. }
  99. return this.localSlots;
  100. },
  101. // 根据实际选中数量决定网格:1→1x1, 2→2x1, 3~4→2x2
  102. gridCols() {
  103. if (this.expandedId) return 1;
  104. if (this.panelCount <= 1) return 1;
  105. return 2;
  106. },
  107. gridRows() {
  108. if (this.expandedId) return 1;
  109. if (this.panelCount <= 2) return 1;
  110. return 2;
  111. },
  112. gridStyle() {
  113. return {
  114. gridTemplateColumns: `repeat(${this.gridCols}, 1fr)`,
  115. gridTemplateRows: `repeat(${this.gridRows}, 1fr)`
  116. };
  117. },
  118. gridClass() {
  119. if (this.expandedId) return 'slots-1';
  120. return 'slots-' + this.panelCount;
  121. }
  122. },
  123. watch: {
  124. crossings: {
  125. handler() { this.rebuildSlots(); },
  126. deep: true,
  127. immediate: true
  128. },
  129. maxSlots() { this.rebuildSlots(); }
  130. },
  131. methods: {
  132. async rebuildSlots() {
  133. // 保留已加载过的 headerData,避免重复请求
  134. const oldMap = {};
  135. this.localSlots.forEach(s => {
  136. if (s.data && s.headerData) oldMap[s.data.id] = s.headerData;
  137. });
  138. this.localSlots = this.crossings.map(c => ({
  139. type: 'panel', data: c, headerData: oldMap[c.id] || c._preloadedData || null
  140. }));
  141. // 如果展开的路口被外部移除了,退出展开
  142. if (this.expandedId) {
  143. const stillExists = this.crossings.find(c => c.id === this.expandedId);
  144. if (!stillExists) this.expandedId = null;
  145. }
  146. this.$nextTick(() => {
  147. window.dispatchEvent(new Event('resize'));
  148. });
  149. // 只对还没有 headerData 的路口发请求
  150. const needLoad = this.localSlots
  151. .map((s, i) => (!s.headerData ? { index: i, id: s.data.id } : null))
  152. .filter(Boolean);
  153. if (needLoad.length === 0) return;
  154. const version = ++this.rebuildVersion;
  155. const results = await Promise.all(
  156. needLoad.map(item => apiGetCrossingDetailData(item.id).catch(() => null))
  157. );
  158. if (version !== this.rebuildVersion) return;
  159. results.forEach((data, i) => {
  160. const slotIndex = needLoad[i].index;
  161. if (data && this.localSlots[slotIndex]) {
  162. this.$set(this.localSlots[slotIndex], 'headerData', data);
  163. }
  164. });
  165. },
  166. handleDblClick(slot) {
  167. if (slot.type !== 'panel') return;
  168. if (this.panelCount <= 1) return;
  169. const id = slot.data.id;
  170. if (this.expandedId === id) {
  171. this.expandedId = null;
  172. } else {
  173. this.expandedId = id;
  174. }
  175. this.$nextTick(() => {
  176. window.dispatchEvent(new Event('resize'));
  177. });
  178. },
  179. exitExpand() {
  180. this.expandedId = null;
  181. this.$nextTick(() => {
  182. window.dispatchEvent(new Event('resize'));
  183. });
  184. },
  185. handleRemove(id) {
  186. if (this.expandedId === id) {
  187. this.expandedId = null;
  188. }
  189. if (typeof this.onRemove === 'function') {
  190. this.onRemove(id);
  191. }
  192. },
  193. onDragEnd() {
  194. const newOrder = this.localSlots
  195. .filter(s => s.type === 'panel')
  196. .map(s => s.data);
  197. if (typeof this.onReorder === 'function') {
  198. this.onReorder(newOrder);
  199. }
  200. this.$nextTick(() => {
  201. window.dispatchEvent(new Event('resize'));
  202. });
  203. }
  204. }
  205. };
  206. </script>
  207. <style scoped>
  208. .crossing-multi-view {
  209. width: 100%;
  210. height: 100%;
  211. display: flex;
  212. flex-direction: column;
  213. }
  214. /* ===== 展开模式返回栏 ===== */
  215. .expand-toolbar {
  216. flex-shrink: 0;
  217. padding: 6px 12px;
  218. border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  219. }
  220. .back-btn {
  221. color: #4da8ff;
  222. cursor: pointer;
  223. font-size: 13px;
  224. user-select: none;
  225. }
  226. .back-btn:hover {
  227. text-decoration: underline;
  228. }
  229. /* ===== 网格容器 ===== */
  230. .multi-view-grid {
  231. display: grid;
  232. gap: 4px;
  233. width: 100%;
  234. flex: 1;
  235. min-height: 0;
  236. }
  237. .grid-cell {
  238. overflow: hidden;
  239. min-width: 0;
  240. min-height: 0;
  241. position: relative;
  242. display: flex;
  243. flex-direction: column;
  244. }
  245. /* ===== 面板标题栏 ===== */
  246. .cell-header {
  247. flex-shrink: 0;
  248. display: flex;
  249. align-items: center;
  250. padding: 4px 10px;
  251. background: linear-gradient(180deg, rgba(65, 115, 205, 0.4) 0%, transparent 100%);
  252. color: #e0e6f1;
  253. font-size: 13px;
  254. gap: 6px;
  255. }
  256. .cell-title {
  257. flex: 1;
  258. overflow: hidden;
  259. text-overflow: ellipsis;
  260. white-space: nowrap;
  261. font-weight: 600;
  262. letter-spacing: 1px;
  263. }
  264. .cell-close {
  265. cursor: pointer;
  266. opacity: 0.6;
  267. font-size: 14px;
  268. flex-shrink: 0;
  269. }
  270. .cell-close:hover {
  271. opacity: 1;
  272. }
  273. .cell-body {
  274. flex: 1;
  275. min-height: 0;
  276. overflow: hidden;
  277. padding: 6px 10px 10px 10px;
  278. }
  279. /* ===== 拖拽把手 ===== */
  280. .drag-handle {
  281. display: flex;
  282. align-items: center;
  283. justify-content: center;
  284. cursor: grab;
  285. opacity: 0;
  286. transition: opacity 0.2s;
  287. flex-shrink: 0;
  288. }
  289. .cell-header:hover .drag-handle {
  290. opacity: 1;
  291. }
  292. .drag-handle:active {
  293. cursor: grabbing;
  294. }
  295. .drag-icon {
  296. color: rgba(255, 255, 255, 0.8);
  297. font-size: 14px;
  298. line-height: 1;
  299. }
  300. /* ===== 拖拽状态样式 ===== */
  301. .drag-ghost {
  302. opacity: 0.3;
  303. border: 2px dashed rgba(77, 168, 255, 0.6);
  304. border-radius: 4px;
  305. }
  306. .drag-chosen {
  307. box-shadow: 0 0 20px rgba(77, 168, 255, 0.4);
  308. }
  309. .drag-active {
  310. opacity: 0.9;
  311. box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  312. border-radius: 4px;
  313. }
  314. </style>