index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <template>
  2. <div
  3. ref="bs-render-wrap"
  4. :key="`${pageInfo.pageConfig.w}${pageInfo.pageConfig.h}`"
  5. class="bs-render-wrap design-drag-wrap render-theme-wrap"
  6. :style="{
  7. width: pageInfo.pageConfig.w + 'px',
  8. height: pageInfo.pageConfig.h + 'px',
  9. backgroundColor:pageInfo.pageConfig.customTheme ==='light' ? pageInfo.pageConfig.lightBgColor:pageInfo.pageConfig.bgColor ,
  10. backgroundImage:pageInfo.pageConfig.customTheme ==='light' ? `url(${this.getCoverPicture(pageInfo.pageConfig.lightBg)})`:`url(${this.getCoverPicture(pageInfo.pageConfig.bg)})`
  11. }"
  12. @drop="drop($event)"
  13. @dragover.prevent
  14. @click="handleClickOutside($event)"
  15. >
  16. <vdr
  17. v-for="chart in chartList"
  18. :id="chart.code"
  19. :key="chart.updateKey || chart.code"
  20. class="drag-item"
  21. :class="{
  22. 'multiple-selected': activeCodes.includes(chart.code),
  23. }"
  24. :scale-ratio="scale"
  25. :x="chart.x"
  26. :y="chart.y"
  27. :w="chart.w"
  28. :h="chart.h"
  29. :min-width="10"
  30. :min-height="10"
  31. :draggable="!chart.locked"
  32. :resizable="!chart.locked"
  33. :parent="true"
  34. :debug="false"
  35. :is-conflict-check="false"
  36. :snap="true"
  37. :snap-tolerance="snapTolerance"
  38. :style="{
  39. zIndex: chart.z || 0,
  40. }"
  41. :perspective="parseInt(`${chart.perspective == undefined ? 0 : chart.perspective}`)"
  42. :transform="`skew(${chart.skewX == undefined ? 0 : chart.skewX}deg, ${chart.skewY == undefined? 0 : chart.skewY}deg) rotateX(${chart.rotateX == undefined ? 0 : chart.rotateX}deg) rotateY(${chart.rotateY == undefined ? 0 : chart.rotateY}deg) rotateZ(${chart.rotateZ == undefined ? 0 : chart.rotateZ}deg)`"
  43. :grid="[1,1]"
  44. :handles="handlesList"
  45. class-name-handle="bs-handle-class"
  46. @activated="activated(...arguments, chart)"
  47. @dragging="onDrag(...arguments, chart)"
  48. @resizing="onResize(...arguments, chart)"
  49. @resizestop="resizestop(...arguments, chart)"
  50. @dragstop="dragstop(...arguments, chart)"
  51. @refLineParams="getRefLineParams"
  52. @mouseleave.native="resetPresetLineDelay"
  53. >
  54. <Configuration
  55. v-if="isInit"
  56. :config="chart"
  57. @openRightPanel="openRightPanel"
  58. @openDataViewDialog="openDataViewDialog"
  59. >
  60. <RenderCard
  61. :ref="'RenderCard' + chart.code"
  62. :config="chart"
  63. @styleHandler="styleHandler"
  64. />
  65. </Configuration>
  66. </vdr>
  67. <span
  68. v-for="(vl, index) in vLine"
  69. v-show="vl.display"
  70. :key="index + 'vLine'"
  71. class="ref-line v-line"
  72. :style="{ left: vl.position, top: vl.origin, height: vl.lineLength }"
  73. />
  74. <span
  75. v-for="(hl, index) in hLine"
  76. v-show="hl.display"
  77. :key="index + 'hLine'"
  78. class="ref-line h-line"
  79. :style="{ top: hl.position, left: hl.origin, width: hl.lineLength }"
  80. />
  81. </div>
  82. </template>
  83. <script>
  84. import { mapState, mapMutations } from 'vuex'
  85. import RenderCard from './RenderCard.vue'
  86. import Configuration from './Configuration.vue'
  87. // import _ from 'lodash'
  88. import cloneDeep from 'lodash/cloneDeep'
  89. import vdr from '@gcpaas/vue-draggable-resizable-gorkys'
  90. import 'gc-vue-draggable-resizable/dist/VueDraggableResizable.css'
  91. import { randomString } from '../js/utils'
  92. import { compile } from 'tiny-sass-compiler/dist/tiny-sass-compiler.esm-browser.prod.js'
  93. import plotList, { getCustomPlots } from '../G2Plots/plotList'
  94. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  95. import { getFileUrl } from 'data-room-ui/js/utils/file'
  96. export default {
  97. name: 'BigScreenRender',
  98. components: {
  99. RenderCard,
  100. Configuration,
  101. vdr
  102. },
  103. props: {
  104. ruleKey: {
  105. type: Number,
  106. default: 0
  107. }
  108. },
  109. data () {
  110. return {
  111. handlesList: ['tl', 'tm', 'tr', 'mr', 'br', 'bm', 'bl', 'ml'], // 缩放手柄的数组
  112. vLine: [],
  113. hLine: [],
  114. themeCss: '',
  115. // 临时冻结拖拽
  116. freeze: false,
  117. plotList,
  118. rawChart: []
  119. }
  120. },
  121. computed: {
  122. ...mapState({
  123. pageConfig: (state) => state.bigScreen.pageInfo.pageConfig,
  124. pageInfo: (state) => state.bigScreen.pageInfo,
  125. chartList: (state) => state.bigScreen.pageInfo.chartList,
  126. activeCode: (state) => state.bigScreen.activeCode,
  127. activeCodes: (state) => state.bigScreen.activeCodes,
  128. hoverCode: (state) => state.bigScreen.hoverCode,
  129. themeJson: (state) => state.bigScreen.pageInfo.pageConfig.themeJson,
  130. isInit: (state) => !state.bigScreen.pageLoading,
  131. scale: (state) => state.bigScreen.zoom / 100,
  132. snapTolerance: (state) => state.bigScreen.snapTolerance
  133. })
  134. },
  135. watch: {
  136. pageConfig: {
  137. handler (pageConfig) {
  138. this.$nextTick(() => {
  139. const style = document.createElement('style')
  140. if (
  141. pageConfig &&
  142. pageConfig.themeJson &&
  143. pageConfig.themeJson.themeCss
  144. ) {
  145. const themeCss = pageConfig.themeJson.themeCss
  146. if (themeCss) {
  147. const themeStr = compile(themeCss).code
  148. style.type = 'text/css'
  149. style.innerText = themeStr
  150. document.getElementsByTagName('head')[0].appendChild(style)
  151. }
  152. }
  153. })
  154. },
  155. deep: true,
  156. immediate: true
  157. }
  158. },
  159. mounted () {
  160. this.styleSet()
  161. this.plotList = [...this.plotList, ...getCustomPlots()]
  162. },
  163. methods: {
  164. ...mapMutations('bigScreen', [
  165. 'changeLayout',
  166. 'changeActiveCode',
  167. 'changeChartConfig',
  168. 'changeActiveItemConfig',
  169. 'changeActiveItemWH',
  170. 'addItem',
  171. 'delItem',
  172. 'resetPresetLine',
  173. 'changeGridShow',
  174. 'setPresetLine',
  175. 'saveTimeLine',
  176. 'changeActiveCodes'
  177. ]),
  178. // 判断鼠标点击的是画布中的高亮元素(被框选的)还是非高亮元素或者空白区域
  179. // 如果是高亮元素则不会取消高亮状态,如果不是则取消高亮状态
  180. handleClickOutside (event) {
  181. // 获取被点击的元素
  182. const clickedElement = event.target
  183. const elementToHighlights = []
  184. // 获取需要高亮的元素的引用
  185. for (const code of this.activeCodes) {
  186. if (this.$refs['RenderCard' + code] && this.$refs['RenderCard' + code].length && this.$refs['RenderCard' + code][0]) {
  187. elementToHighlights.push(this.$refs['RenderCard' + code][0])
  188. }
  189. }
  190. const isElementInHighlights = elementToHighlights.some((elementToHighlight) => {
  191. return elementToHighlight?.$el?.contains(clickedElement)
  192. })
  193. if (!isElementInHighlights) {
  194. this.changeActiveCodes([])
  195. }
  196. },
  197. // 切换主题时针对远程组件触发样式修改的方法
  198. styleHandler (config) {
  199. this.$nextTick(() => {
  200. this.$refs['RenderCard' + config.code][0]?.$refs[
  201. config.code
  202. ]?.changeStyle(cloneDeep(config), true)
  203. })
  204. },
  205. // 获取到后端传来的主题样式并进行修改
  206. styleSet () {
  207. const style = document.createElement('style')
  208. if (this.themeJson && this.themeJson.themeCss) {
  209. const styleStr = this.themeJson.themeCss
  210. const themeCss = compile(styleStr).code
  211. style.type = 'text/css'
  212. style.innerText = themeCss
  213. document.getElementsByTagName('head')[0].appendChild(style)
  214. } else {
  215. style.remove()
  216. }
  217. },
  218. resetPresetLineDelay () {
  219. setTimeout(() => {
  220. this.resetPresetLine()
  221. }, 500)
  222. },
  223. // 点击当前组件时打开右侧面板
  224. openRightPanel (config) {
  225. this.$emit('openRightPanel', config)
  226. },
  227. // 查看数据
  228. openDataViewDialog (config) {
  229. this.$emit('openDataViewDialog', config)
  230. },
  231. drop (e) {
  232. e.preventDefault()
  233. // 解决:火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片的问题。
  234. e.stopPropagation()
  235. const transferData = e.dataTransfer.getData('dragComponent')
  236. if (transferData) {
  237. this.addChart(transferData, { x: e?.x, y: e?.y })
  238. }
  239. },
  240. /**
  241. * 改变组件大小
  242. * @param x
  243. * @param y
  244. * @param width
  245. * @param height
  246. * @param chart
  247. */
  248. onResize (x, y, width, height, chart) {
  249. chart.x = x
  250. chart.y = y
  251. chart.w = width
  252. chart.h = height
  253. this.changeGridShow(true)
  254. this.setPresetLine({
  255. ...chart
  256. })
  257. },
  258. /**
  259. *
  260. * @param x
  261. * @param y
  262. * @param chart
  263. */
  264. onDrag (x, y, chart) {
  265. // 防止事件冒泡
  266. event.stopPropagation()
  267. if (chart.group) {
  268. // 查找和自己是一个组合的组件
  269. this.dragGroupChart(x, y, chart)
  270. } else {
  271. chart.x = x
  272. chart.y = y
  273. }
  274. this.changeGridShow(true)
  275. this.setPresetLine({
  276. ...chart
  277. })
  278. },
  279. resizestop (left, top, width, height, chart) {
  280. this.changeChartConfig({
  281. ...chart,
  282. w: width,
  283. h: height,
  284. x: left,
  285. y: top
  286. })
  287. this.changeActiveItemConfig({
  288. ...chart,
  289. w: width,
  290. h: height,
  291. x: left,
  292. y: top
  293. })
  294. if (chart.code === this.activeCode) {
  295. this.changeActiveItemWH({
  296. code: chart.code,
  297. w: width,
  298. h: height
  299. })
  300. }
  301. this.saveTimeLine(`改变${chart?.title}大小`)
  302. this.changeGridShow(false)
  303. },
  304. activated (chart) {
  305. this.rawChart = cloneDeep(chart)
  306. },
  307. dragstop (left, top, chart) {
  308. if (!this.freeze) {
  309. if (this.rawChart.x !== left || this.rawChart.y !== top) {
  310. this.changeChartConfig({
  311. ...chart,
  312. x: left,
  313. y: top
  314. })
  315. this.changeActiveItemConfig({
  316. ...chart,
  317. x: left,
  318. y: top
  319. })
  320. if (chart.code === this.activeCode) {
  321. this.changeActiveItemWH({
  322. code: chart.code,
  323. x: left,
  324. y: top
  325. })
  326. }
  327. this.rawChart = cloneDeep(chart)
  328. }
  329. } else {
  330. const index = this.chartList.findIndex(
  331. (_chart) => _chart.code === chart.code
  332. )
  333. this.$set(this.chartList, index, chart)
  334. this.changeChartConfig({
  335. ...chart,
  336. updateKey: new Date().getTime()
  337. })
  338. }
  339. this.changeGridShow(false)
  340. this.freeze = false
  341. this.saveTimeLine(`拖拽${chart?.title}`)
  342. },
  343. // 辅助线
  344. getRefLineParams (params) {
  345. const { vLine, hLine } = params
  346. this.vLine = vLine
  347. this.hLine = hLine
  348. },
  349. // 新增元素
  350. addChart (chart, position, isComponent) {
  351. const { left, top } = this.$el.getBoundingClientRect()
  352. const _chart = !chart.code ? JSON.parse(chart) : chart
  353. let option = _chart.option
  354. if (_chart.type === 'customComponent') {
  355. option = {
  356. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  357. theme: this.pageConfig.customTheme === 'dark' ? 'transparent' : 'light'
  358. }
  359. }
  360. const config = {
  361. ..._chart,
  362. x: parseInt(!chart.code
  363. ? (position.x - left - _chart.offsetX) / this.scale
  364. : position.x),
  365. y: parseInt(!chart.code
  366. ? (position.y - top - _chart.offsetX) / this.scale
  367. : position.y),
  368. width: 200 * this.scale,
  369. height: 200 * this.scale,
  370. code: !chart.code ? randomString(8) : chart.code,
  371. option
  372. }
  373. config.key = config.code
  374. // isComponent = false 从左侧新增时需要初始化theme的内容
  375. // isComponent = true从组件库添加自定义组件时不用初始化
  376. if (!isComponent) {
  377. config.theme = settingToTheme(config, 'dark')
  378. config.theme = settingToTheme(config, 'light')
  379. }
  380. this.addItem(config)
  381. },
  382. addSourceChart (chart, position) {
  383. const { left, top } = this.$el.getBoundingClientRect()
  384. const _chart = JSON.parse(chart)
  385. let option = _chart.option
  386. if (_chart.type === 'customComponent') {
  387. option = {
  388. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  389. theme: this.pageConfig.customTheme === 'dark' ? 'transparent' : 'light'
  390. }
  391. }
  392. const config = {
  393. ..._chart,
  394. x: parseInt((position.x - left) / this.scale),
  395. y: parseInt((position.y - top) / this.scale),
  396. width: 200 * this.scale,
  397. height: 200 * this.scale,
  398. code: randomString(8),
  399. option
  400. }
  401. config.key = config.code
  402. this.addItem(config)
  403. },
  404. /**
  405. * 拖拽相同组合的组件
  406. * @param x 组合元素当前x
  407. * @param y 组合元素当前y
  408. * @param chart
  409. */
  410. dragGroupChart (x, y, chart) {
  411. if (chart.group) {
  412. const diffX = x - chart.x
  413. const diffY = y - chart.y
  414. const group = chart.group
  415. // 找到相同group的组件,并找到边界
  416. const groupChartList = this.chartList.filter(
  417. (groupChart) => groupChart.group === group
  418. )
  419. const groupMinX = Math.min(
  420. ...groupChartList?.map((groupChart) => groupChart.x + diffX)
  421. )
  422. const groupMinY = Math.min(
  423. ...groupChartList?.map((groupChart) => groupChart.y + diffY)
  424. )
  425. const groupMaxX = Math.max(
  426. ...groupChartList?.map(
  427. (groupChart) => groupChart.x + diffX + groupChart.w
  428. )
  429. )
  430. const groupMaxY = Math.max(
  431. ...groupChartList?.map(
  432. (groupChart) => groupChart.y + diffY + groupChart.h
  433. )
  434. )
  435. // 如果其中某个组件超出画布,则不移动 (此处无法阻止移动,故在拖拽结束后重置位置)
  436. if (
  437. (groupMinX <= 0 ||
  438. groupMinY <= 0 ||
  439. groupMaxX >= this.pageConfig.w ||
  440. groupMaxY >= this.pageConfig.h) &&
  441. // 偏移的绝对值要大于0
  442. (Math.abs(diffX) > 0 || Math.abs(diffY) > 0)
  443. ) {
  444. this.freeze = true
  445. return
  446. }
  447. // 移动相应的diff距离
  448. groupChartList?.map((groupChart) => {
  449. this.changeChartConfig({
  450. ...groupChart,
  451. x: groupChart.x + diffX,
  452. y: groupChart.y + diffY
  453. })
  454. })
  455. }
  456. },
  457. /**
  458. * 获取图片访问地址,如果是相对路径则拼接上文件访问前缀地址
  459. * @param url
  460. * @returns {*}
  461. */
  462. getCoverPicture (url) {
  463. return getFileUrl(url)
  464. }
  465. }
  466. }
  467. </script>
  468. <style lang="scss" scoped>
  469. .bs-render-wrap {
  470. position: relative;
  471. background-size: cover;
  472. .drag-item {
  473. cursor: move;
  474. }
  475. ::v-deep .vdr {
  476. border: none;
  477. }
  478. .h-line {
  479. border-bottom: 1px dashed #0089d0;
  480. }
  481. .v-line {
  482. border-left: 1px dashed #0089d0;
  483. }
  484. .ref-line {
  485. background-color: transparent;
  486. }
  487. }
  488. .design-drag-wrap {
  489. box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.5);
  490. }
  491. .multiple-selected {
  492. border: 1px solid #fff !important;
  493. }
  494. //调整拖拽插件的句柄样式
  495. //句柄公共样式
  496. ::v-deep .bs-handle-class{
  497. width: 16px!important;
  498. height: 16px!important;
  499. position: absolute;
  500. box-sizing: border-box;
  501. //background: #fff;
  502. border: 3px solid #c8ff00;
  503. }
  504. // 每个句柄不同样式
  505. ::v-deep .bs-handle-class-tl{
  506. top: -2px!important;
  507. left: -2px!important;
  508. display: block;
  509. cursor: nw-resize;
  510. border-right: none;
  511. border-bottom: none;
  512. }
  513. ::v-deep .bs-handle-class-tm{
  514. top: -2px!important;
  515. left: calc(50% - 8px)!important;
  516. display: block;
  517. cursor: n-resize;
  518. border-left: none;
  519. border-right: none;
  520. border-bottom: none;
  521. }
  522. ::v-deep .bs-handle-class-tr{
  523. top: -2px!important;
  524. right: -2px!important;
  525. display: block;
  526. cursor: ne-resize;
  527. border-left: none;
  528. border-bottom: none;
  529. }
  530. ::v-deep .bs-handle-class-mr{
  531. top: calc(50% - 8px)!important;
  532. right: -2px!important;
  533. display: block;
  534. cursor: e-resize;
  535. border-left: none;
  536. border-top: none;
  537. border-bottom: none;
  538. }
  539. ::v-deep .bs-handle-class-br{
  540. right: -2px!important;
  541. bottom: -2px!important;
  542. display: block;
  543. cursor: se-resize;
  544. border-left: none;
  545. border-top: none;
  546. }
  547. ::v-deep .bs-handle-class-bm{
  548. right: calc(50% - 8px)!important;
  549. bottom: -2px!important;
  550. display: block;
  551. cursor: s-resize;
  552. border-left: none;
  553. border-right: none;
  554. border-top: none;
  555. }
  556. ::v-deep .bs-handle-class-bl{
  557. left: -2px!important;
  558. bottom: -2px!important;
  559. display: block;
  560. cursor: sw-resize;
  561. border-right: none;
  562. border-top: none;
  563. }
  564. ::v-deep .bs-handle-class-ml{
  565. top: calc(50% - 8px)!important;
  566. left: -2px!important;
  567. display: block;
  568. cursor: w-resize;
  569. border-top: none;
  570. border-right: none;
  571. border-bottom: none;
  572. }
  573. </style>