index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. 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 = isComponent ? randomString(8) : 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. console.log('1', config)
  381. this.addItem(config)
  382. },
  383. addSourceChart (chart, position) {
  384. const { left, top } = this.$el.getBoundingClientRect()
  385. const _chart = JSON.parse(chart)
  386. let option = _chart.option
  387. if (_chart.type === 'customComponent') {
  388. option = {
  389. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  390. theme: this.pageConfig.customTheme === 'dark' ? 'transparent' : 'light'
  391. }
  392. }
  393. const config = {
  394. ..._chart,
  395. x: parseInt((position.x - left) / this.scale),
  396. y: parseInt((position.y - top) / this.scale),
  397. width: 200 * this.scale,
  398. height: 200 * this.scale,
  399. code: randomString(8),
  400. option
  401. }
  402. config.key = config.code
  403. this.addItem(config)
  404. },
  405. /**
  406. * 拖拽相同组合的组件
  407. * @param x 组合元素当前x
  408. * @param y 组合元素当前y
  409. * @param chart
  410. */
  411. dragGroupChart (x, y, chart) {
  412. if (chart.group) {
  413. const diffX = x - chart.x
  414. const diffY = y - chart.y
  415. const group = chart.group
  416. // 找到相同group的组件,并找到边界
  417. const groupChartList = this.chartList.filter(
  418. (groupChart) => groupChart.group === group
  419. )
  420. const groupMinX = Math.min(
  421. ...groupChartList?.map((groupChart) => groupChart.x + diffX)
  422. )
  423. const groupMinY = Math.min(
  424. ...groupChartList?.map((groupChart) => groupChart.y + diffY)
  425. )
  426. const groupMaxX = Math.max(
  427. ...groupChartList?.map(
  428. (groupChart) => groupChart.x + diffX + groupChart.w
  429. )
  430. )
  431. const groupMaxY = Math.max(
  432. ...groupChartList?.map(
  433. (groupChart) => groupChart.y + diffY + groupChart.h
  434. )
  435. )
  436. // 如果其中某个组件超出画布,则不移动 (此处无法阻止移动,故在拖拽结束后重置位置)
  437. if (
  438. (groupMinX <= 0 ||
  439. groupMinY <= 0 ||
  440. groupMaxX >= this.pageConfig.w ||
  441. groupMaxY >= this.pageConfig.h) &&
  442. // 偏移的绝对值要大于0
  443. (Math.abs(diffX) > 0 || Math.abs(diffY) > 0)
  444. ) {
  445. this.freeze = true
  446. return
  447. }
  448. // 移动相应的diff距离
  449. groupChartList?.map((groupChart) => {
  450. this.changeChartConfig({
  451. ...groupChart,
  452. x: groupChart.x + diffX,
  453. y: groupChart.y + diffY
  454. })
  455. })
  456. }
  457. },
  458. /**
  459. * 获取图片访问地址,如果是相对路径则拼接上文件访问前缀地址
  460. * @param url
  461. * @returns {*}
  462. */
  463. getCoverPicture (url) {
  464. return getFileUrl(url)
  465. }
  466. }
  467. }
  468. </script>
  469. <style lang="scss" scoped>
  470. .bs-render-wrap {
  471. position: relative;
  472. background-size: cover;
  473. .drag-item {
  474. cursor: move;
  475. }
  476. ::v-deep .vdr {
  477. border: none;
  478. }
  479. .h-line {
  480. border-bottom: 1px dashed #0089d0;
  481. }
  482. .v-line {
  483. border-left: 1px dashed #0089d0;
  484. }
  485. .ref-line {
  486. background-color: transparent;
  487. }
  488. }
  489. .design-drag-wrap {
  490. box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.5);
  491. }
  492. .multiple-selected {
  493. border: 1px solid #fff !important;
  494. }
  495. //调整拖拽插件的句柄样式
  496. //句柄公共样式
  497. ::v-deep .bs-handle-class{
  498. width: 16px!important;
  499. height: 16px!important;
  500. position: absolute;
  501. box-sizing: border-box;
  502. //background: #fff;
  503. border: 3px solid #c8ff00;
  504. }
  505. // 每个句柄不同样式
  506. ::v-deep .bs-handle-class-tl{
  507. top: -2px!important;
  508. left: -2px!important;
  509. display: block;
  510. cursor: nw-resize;
  511. border-right: none;
  512. border-bottom: none;
  513. }
  514. ::v-deep .bs-handle-class-tm{
  515. top: -2px!important;
  516. left: calc(50% - 8px)!important;
  517. display: block;
  518. cursor: n-resize;
  519. border-left: none;
  520. border-right: none;
  521. border-bottom: none;
  522. }
  523. ::v-deep .bs-handle-class-tr{
  524. top: -2px!important;
  525. right: -2px!important;
  526. display: block;
  527. cursor: ne-resize;
  528. border-left: none;
  529. border-bottom: none;
  530. }
  531. ::v-deep .bs-handle-class-mr{
  532. top: calc(50% - 8px)!important;
  533. right: -2px!important;
  534. display: block;
  535. cursor: e-resize;
  536. border-left: none;
  537. border-top: none;
  538. border-bottom: none;
  539. }
  540. ::v-deep .bs-handle-class-br{
  541. right: -2px!important;
  542. bottom: -2px!important;
  543. display: block;
  544. cursor: se-resize;
  545. border-left: none;
  546. border-top: none;
  547. }
  548. ::v-deep .bs-handle-class-bm{
  549. right: calc(50% - 8px)!important;
  550. bottom: -2px!important;
  551. display: block;
  552. cursor: s-resize;
  553. border-left: none;
  554. border-right: none;
  555. border-top: none;
  556. }
  557. ::v-deep .bs-handle-class-bl{
  558. left: -2px!important;
  559. bottom: -2px!important;
  560. display: block;
  561. cursor: sw-resize;
  562. border-right: none;
  563. border-top: none;
  564. }
  565. ::v-deep .bs-handle-class-ml{
  566. top: calc(50% - 8px)!important;
  567. left: -2px!important;
  568. display: block;
  569. cursor: w-resize;
  570. border-top: none;
  571. border-right: none;
  572. border-bottom: none;
  573. }
  574. </style>