index.vue 16 KB

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