index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. w: width,
  251. h: height
  252. })
  253. }
  254. this.saveTimeLine(`改变${chart?.title}大小`)
  255. this.changeGridShow(false)
  256. },
  257. activated (chart) {
  258. this.rawChart = _.cloneDeep(chart)
  259. },
  260. dragstop (left, top, chart) {
  261. if (!this.freeze) {
  262. if (this.rawChart.x !== left || this.rawChart.y !== top) {
  263. this.changeChartConfig({
  264. ...chart,
  265. x: left,
  266. y: top
  267. })
  268. this.changeActiveItemConfig({
  269. ...chart,
  270. x: left,
  271. y: top
  272. })
  273. if (chart.code === this.activeCode) {
  274. this.changeActiveItemWH({
  275. x: left,
  276. y: top
  277. })
  278. }
  279. this.rawChart = _.cloneDeep(chart)
  280. }
  281. } else {
  282. const index = this.chartList.findIndex(
  283. (_chart) => _chart.code === chart.code
  284. )
  285. this.$set(this.chartList, index, chart)
  286. this.changeChartConfig({
  287. ...chart,
  288. updateKey: new Date().getTime()
  289. })
  290. }
  291. this.changeGridShow(false)
  292. this.freeze = false
  293. this.saveTimeLine(`拖拽${chart?.title}`)
  294. },
  295. // 辅助线
  296. getRefLineParams (params) {
  297. const { vLine, hLine } = params
  298. this.vLine = vLine
  299. this.hLine = hLine
  300. },
  301. // 新增元素
  302. addChart (chart, position) {
  303. const { left, top } = this.$el.getBoundingClientRect()
  304. const _chart = !chart.code ? JSON.parse(chart) : chart
  305. let option = _chart.option
  306. if (_chart.type === 'customComponent') {
  307. option = {
  308. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  309. theme: this.pageConfig.customTheme
  310. }
  311. }
  312. const config = {
  313. ..._chart,
  314. x: parseInt(!chart.code
  315. ? (position.x - left - _chart.offsetX) / this.scale
  316. : position.x),
  317. y: parseInt(!chart.code
  318. ? (position.y - top - _chart.offsetX) / this.scale
  319. : position.y),
  320. width: 200 * this.scale,
  321. height: 200 * this.scale,
  322. code: !chart.code ? randomString(8) : chart.code,
  323. option
  324. }
  325. config.key = config.code
  326. this.addItem(config)
  327. },
  328. addSourceChart (chart, position) {
  329. const { left, top } = this.$el.getBoundingClientRect()
  330. const _chart = JSON.parse(chart)
  331. let option = _chart.option
  332. if (_chart.type === 'customComponent') {
  333. option = {
  334. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  335. theme: this.pageConfig.customTheme
  336. }
  337. }
  338. const config = {
  339. ..._chart,
  340. x: parseInt((position.x - left) / this.scale),
  341. y: parseInt((position.y - top) / this.scale),
  342. width: 200 * this.scale,
  343. height: 200 * this.scale,
  344. code: randomString(8),
  345. option
  346. }
  347. config.key = config.code
  348. this.addItem(config)
  349. },
  350. /**
  351. * 拖拽相同组合的组件
  352. * @param x 组合元素当前x
  353. * @param y 组合元素当前y
  354. * @param chart
  355. */
  356. dragGroupChart (x, y, chart) {
  357. if (chart.group) {
  358. const diffX = x - chart.x
  359. const diffY = y - chart.y
  360. const group = chart.group
  361. // 找到相同group的组件,并找到边界
  362. const groupChartList = this.chartList.filter(
  363. (groupChart) => groupChart.group === group
  364. )
  365. const groupMinX = Math.min(
  366. ...groupChartList?.map((groupChart) => groupChart.x + diffX)
  367. )
  368. const groupMinY = Math.min(
  369. ...groupChartList?.map((groupChart) => groupChart.y + diffY)
  370. )
  371. const groupMaxX = Math.max(
  372. ...groupChartList?.map(
  373. (groupChart) => groupChart.x + diffX + groupChart.w
  374. )
  375. )
  376. const groupMaxY = Math.max(
  377. ...groupChartList?.map(
  378. (groupChart) => groupChart.y + diffY + groupChart.h
  379. )
  380. )
  381. // 如果其中某个组件超出画布,则不移动 (此处无法阻止移动,故在拖拽结束后重置位置)
  382. if (
  383. (groupMinX <= 0 ||
  384. groupMinY <= 0 ||
  385. groupMaxX >= this.pageConfig.w ||
  386. groupMaxY >= this.pageConfig.h) &&
  387. // 偏移的绝对值要大于0
  388. (Math.abs(diffX) > 0 || Math.abs(diffY) > 0)
  389. ) {
  390. this.freeze = true
  391. return
  392. }
  393. // 移动相应的diff距离
  394. groupChartList?.map((groupChart) => {
  395. this.changeChartConfig({
  396. ...groupChart,
  397. x: groupChart.x + diffX,
  398. y: groupChart.y + diffY
  399. })
  400. })
  401. }
  402. }
  403. }
  404. }
  405. </script>
  406. <style lang="scss" scoped>
  407. .bs-render-wrap {
  408. position: relative;
  409. background-size: cover;
  410. .drag-item {
  411. cursor: move;
  412. }
  413. /deep/ .vdr {
  414. border: none;
  415. }
  416. .h-line {
  417. border-bottom: 1px dashed #0089d0;
  418. }
  419. .v-line {
  420. border-left: 1px dashed #0089d0;
  421. }
  422. .ref-line {
  423. background-color: transparent;
  424. }
  425. }
  426. .design-drag-wrap {
  427. box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.5);
  428. }
  429. .multiple-selected {
  430. border: 1px dashed #fff !important;
  431. }
  432. </style>