index.vue 17 KB

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