index.vue 14 KB

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