CrossingMultiView.vue 10 KB

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