index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. 'changeActiveItemWH',
  155. 'addItem',
  156. 'delItem',
  157. 'resetPresetLine',
  158. 'changeGridShow',
  159. 'setPresetLine',
  160. 'saveTimeLine'
  161. ]),
  162. // 获取到后端传来的主题样式并进行修改
  163. styleSet () {
  164. const style = document.createElement('style')
  165. if (this.themeJson && this.themeJson.themeCss) {
  166. const styleStr = this.themeJson.themeCss
  167. const themeCss = compile(styleStr).code
  168. style.type = 'text/css'
  169. style.innerText = themeCss
  170. document.getElementsByTagName('head')[0].appendChild(style)
  171. } else {
  172. style.remove()
  173. }
  174. },
  175. resetPresetLineDelay () {
  176. setTimeout(() => {
  177. this.resetPresetLine()
  178. }, 500)
  179. },
  180. // 点击当前组件时打开右侧面板
  181. openRightPanel (config) {
  182. this.$emit('openRightPanel', config)
  183. },
  184. drop (e) {
  185. e.preventDefault()
  186. // 解决:火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片的问题。
  187. e.stopPropagation()
  188. const transferData = e.dataTransfer.getData('dragComponent')
  189. if (transferData) {
  190. this.addChart(transferData, { x: e?.x, y: e?.y })
  191. }
  192. },
  193. /**
  194. * 改变组件大小
  195. * @param x
  196. * @param y
  197. * @param width
  198. * @param height
  199. * @param chart
  200. */
  201. onResize (x, y, width, height, chart) {
  202. chart.x = x
  203. chart.y = y
  204. chart.w = width
  205. chart.h = height
  206. this.changeGridShow(true)
  207. this.setPresetLine({
  208. ...chart
  209. })
  210. },
  211. /**
  212. *
  213. * @param x
  214. * @param y
  215. * @param chart
  216. */
  217. onDrag (x, y, chart) {
  218. // 防止事件冒泡
  219. event.stopPropagation()
  220. if (chart.group) {
  221. // 查找和自己是一个组合的组件
  222. this.dragGroupChart(x, y, chart)
  223. } else {
  224. chart.x = x
  225. chart.y = y
  226. }
  227. this.changeGridShow(true)
  228. this.setPresetLine({
  229. ...chart
  230. })
  231. },
  232. resizestop (left, top, width, height, chart) {
  233. this.changeChartConfig({
  234. ...chart,
  235. w: width,
  236. h: height,
  237. x: left,
  238. y: top
  239. })
  240. if (chart.code === this.activeCode) {
  241. this.changeActiveItemWH({
  242. w: width,
  243. h: height
  244. })
  245. }
  246. this.saveTimeLine(`改变${chart?.title}大小`)
  247. this.changeGridShow(false)
  248. },
  249. activated (chart) {
  250. this.rawChart = _.cloneDeep(chart)
  251. },
  252. dragstop (left, top, chart) {
  253. if (!this.freeze) {
  254. if (this.rawChart.x !== left || this.rawChart.y !== top) {
  255. this.changeChartConfig({
  256. ...chart,
  257. x: left,
  258. y: top
  259. })
  260. if (chart.code === this.activeCode) {
  261. this.changeActiveItemWH({
  262. x: left,
  263. y: top
  264. })
  265. }
  266. this.rawChart = _.cloneDeep(chart)
  267. }
  268. } else {
  269. const index = this.chartList.findIndex(
  270. (_chart) => _chart.code === chart.code
  271. )
  272. this.$set(this.chartList, index, chart)
  273. this.changeChartConfig({
  274. ...chart,
  275. updateKey: new Date().getTime()
  276. })
  277. }
  278. this.changeGridShow(false)
  279. this.freeze = false
  280. this.saveTimeLine(`拖拽${chart?.title}`)
  281. },
  282. // 辅助线
  283. getRefLineParams (params) {
  284. const { vLine, hLine } = params
  285. this.vLine = vLine
  286. this.hLine = hLine
  287. },
  288. // 新增元素
  289. addChart (chart, position) {
  290. const { left, top } = this.$el.getBoundingClientRect()
  291. const _chart = !chart.code ? JSON.parse(chart) : chart
  292. let option = _chart.option
  293. if (_chart.type === 'customComponent') {
  294. option = {
  295. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  296. theme: this.pageConfig.customTheme
  297. }
  298. }
  299. const config = {
  300. ..._chart,
  301. x: parseInt(!chart.code
  302. ? (position.x - left - _chart.offsetX) / this.scale
  303. : position.x),
  304. y: parseInt(!chart.code
  305. ? (position.y - top - _chart.offsetX) / this.scale
  306. : position.y),
  307. width: 200 * this.scale,
  308. height: 200 * this.scale,
  309. code: !chart.code ? randomString(8) : chart.code,
  310. option
  311. }
  312. config.key = config.code
  313. this.addItem(config)
  314. },
  315. addSourceChart (chart, position) {
  316. const { left, top } = this.$el.getBoundingClientRect()
  317. const _chart = JSON.parse(chart)
  318. let option = _chart.option
  319. if (_chart.type === 'customComponent') {
  320. option = {
  321. ...this.plotList?.find((plot) => plot.name === _chart.name)?.option,
  322. theme: this.pageConfig.customTheme
  323. }
  324. }
  325. const config = {
  326. ..._chart,
  327. x: parseInt((position.x - left) / this.scale),
  328. y: parseInt((position.y - top) / this.scale),
  329. width: 200 * this.scale,
  330. height: 200 * this.scale,
  331. code: randomString(8),
  332. option
  333. }
  334. config.key = config.code
  335. this.addItem(config)
  336. },
  337. /**
  338. * 拖拽相同组合的组件
  339. * @param x 组合元素当前x
  340. * @param y 组合元素当前y
  341. * @param chart
  342. */
  343. dragGroupChart (x, y, chart) {
  344. if (chart.group) {
  345. const diffX = x - chart.x
  346. const diffY = y - chart.y
  347. const group = chart.group
  348. // 找到相同group的组件,并找到边界
  349. const groupChartList = this.chartList.filter(
  350. (groupChart) => groupChart.group === group
  351. )
  352. const groupMinX = Math.min(
  353. ...groupChartList?.map((groupChart) => groupChart.x + diffX)
  354. )
  355. const groupMinY = Math.min(
  356. ...groupChartList?.map((groupChart) => groupChart.y + diffY)
  357. )
  358. const groupMaxX = Math.max(
  359. ...groupChartList?.map(
  360. (groupChart) => groupChart.x + diffX + groupChart.w
  361. )
  362. )
  363. const groupMaxY = Math.max(
  364. ...groupChartList?.map(
  365. (groupChart) => groupChart.y + diffY + groupChart.h
  366. )
  367. )
  368. // 如果其中某个组件超出画布,则不移动 (此处无法阻止移动,故在拖拽结束后重置位置)
  369. if (
  370. (groupMinX <= 0 ||
  371. groupMinY <= 0 ||
  372. groupMaxX >= this.pageConfig.w ||
  373. groupMaxY >= this.pageConfig.h) &&
  374. // 偏移的绝对值要大于0
  375. (Math.abs(diffX) > 0 || Math.abs(diffY) > 0)
  376. ) {
  377. this.freeze = true
  378. return
  379. }
  380. // 移动相应的diff距离
  381. groupChartList?.map((groupChart) => {
  382. this.changeChartConfig({
  383. ...groupChart,
  384. x: groupChart.x + diffX,
  385. y: groupChart.y + diffY
  386. })
  387. })
  388. }
  389. }
  390. }
  391. }
  392. </script>
  393. <style lang="scss" scoped>
  394. .bs-render-wrap {
  395. position: relative;
  396. background-size: cover;
  397. .drag-item {
  398. cursor: move;
  399. }
  400. /deep/ .vdr {
  401. border: none;
  402. }
  403. .h-line {
  404. border-bottom: 1px dashed #0089d0;
  405. }
  406. .v-line {
  407. border-left: 1px dashed #0089d0;
  408. }
  409. .ref-line {
  410. background-color: transparent;
  411. }
  412. }
  413. .design-drag-wrap {
  414. box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.5);
  415. }
  416. .multiple-selected {
  417. border: 1px dashed #fff !important;
  418. }
  419. </style>