index.vue 16 KB

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