index.vue 12 KB

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