index.vue 13 KB

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