index.vue 16 KB

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