index.vue 12 KB

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