index.vue 16 KB

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