index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <template>
  2. <el-date-picker
  3. :key="config.customize.type"
  4. v-model="value"
  5. :picker-options="config.customize.pickerOptions"
  6. :type="config.customize.type"
  7. clearable
  8. :class="['basic-component-date-picker', `date-picker-${config.code}`]"
  9. :popper-class="'basic-component-date-picker date-picker-popper-' + config.code"
  10. :value-format="config.customize.format"
  11. :format="config.customize.format"
  12. :default-value="value"
  13. size="large"
  14. @focus="focusEvent"
  15. @change="changeValue"
  16. @mouseenter.native="mouseenter"
  17. />
  18. </template>
  19. <script>
  20. import moment from 'moment'
  21. import cloneDeep from 'lodash/cloneDeep'
  22. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  23. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  24. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  25. import { mapState } from 'vuex'
  26. window.dataSetFields = []
  27. export default {
  28. name: 'BasicComponentSelect',
  29. components: {},
  30. mixins: [commonMixins, linkageMixins],
  31. props: {
  32. // 组件配置
  33. config: {
  34. type: Object,
  35. default: () => ({})
  36. }
  37. },
  38. data () {
  39. return {
  40. value: '',
  41. innerConfig: {}
  42. }
  43. },
  44. computed: {
  45. ...mapState({
  46. chartList: state => state.bigScreen.pageInfo.chartList
  47. }),
  48. isPreview () {
  49. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  50. }
  51. },
  52. watch: {
  53. 'config.customize.formatType': {
  54. handler (val) {
  55. const newFomat = this.config.customize.format.replace(/y/g, 'Y').replace(/d/g, 'D')
  56. if (val === 'timestamp') {
  57. // this.value = 0
  58. if (['year', 'month', 'date', 'week', 'datetime'].includes(this.config.customize.type)) {
  59. this.value = moment(new Date()).format(newFomat)
  60. } else {
  61. this.value = [
  62. moment(new Date()).subtract(7, 'days').valueOf(),
  63. moment(new Date()).valueOf()
  64. ]
  65. }
  66. } else if (val === 'custom') {
  67. if (['year', 'month', 'date', 'week', 'datetime'].includes(this.config.customize.type)) {
  68. this.value = moment(new Date()).format(newFomat)
  69. } else {
  70. this.value = [
  71. moment(new Date()).subtract(7, 'days').format(newFomat),
  72. moment(new Date()).format(newFomat)
  73. ]
  74. }
  75. }
  76. },
  77. immediate: true
  78. },
  79. 'config.customize.type': {
  80. handler (val) {
  81. this.$nextTick(() => {
  82. if (!this.isPreview) {
  83. document.querySelector(`.date-picker-${this.config.code}`).style.pointerEvents = 'none'
  84. }
  85. })
  86. const newFomat = this.config.customize.format.replace(/y/g, 'Y').replace(/d/g, 'D')
  87. if (['year', 'month', 'date', 'week', 'datetime'].includes(val)) {
  88. if (this.config.customize.formatType === 'timestamp') {
  89. this.value = moment(new Date()).valueOf()
  90. } else {
  91. this.value = moment(new Date()).format(newFomat)
  92. }
  93. } else {
  94. if (this.config.customize.formatType === 'timestamp') {
  95. this.value = [
  96. moment(new Date()).subtract(7, 'days').valueOf(),
  97. moment(new Date()).valueOf()
  98. ]
  99. } else {
  100. this.value = [
  101. moment(new Date()).subtract(7, 'days').format(newFomat),
  102. moment(new Date()).format(newFomat)
  103. ]
  104. }
  105. }
  106. },
  107. immediate: true
  108. },
  109. 'config.customize.format': {
  110. handler (val) {
  111. this.$nextTick(() => {
  112. if (!this.isPreview) {
  113. document.querySelector(`.date-picker-${this.config.code}`).style.pointerEvents = 'none'
  114. }
  115. })
  116. const newFomat = val?.replace(/y/g, 'Y')?.replace(/d/g, 'D')
  117. if (['year', 'month', 'date', 'week', 'datetime'].includes(this.config.customize.type)) {
  118. this.value = moment(new Date()).format(newFomat)
  119. if (this.config.customize.formatType === 'timestamp') {
  120. this.value = moment(new Date()).valueOf()
  121. } else {
  122. this.value = moment(new Date()).format(newFomat)
  123. }
  124. } else {
  125. if (this.config.customize.formatType === 'timestamp') {
  126. this.value = [
  127. moment(new Date()).subtract(7, 'days').valueOf(),
  128. moment(new Date()).valueOf()
  129. ]
  130. } else {
  131. this.value = [
  132. moment(new Date()).subtract(7, 'days').format(newFomat),
  133. moment(new Date()).format(newFomat)
  134. ]
  135. }
  136. }
  137. }
  138. }
  139. },
  140. created () { },
  141. mounted () {
  142. if (!this.isPreview) {
  143. document.querySelector(`.date-picker-${this.config.code}`).style.pointerEvents = 'none'
  144. }
  145. this.changeStyle(this.config)
  146. if (this.value === '') {
  147. const newFomat = this.config.customize.format.replace(/y/g, 'Y').replace(/d/g, 'D')
  148. this.value = [
  149. moment(new Date()).subtract(7, 'days').format(newFomat),
  150. moment(new Date()).format(newFomat)
  151. ]
  152. }
  153. },
  154. beforeDestroy () {
  155. },
  156. methods: {
  157. dataFormatting (config, data) {
  158. config.option.data = []
  159. return config
  160. },
  161. changeStyle (config) {
  162. config = { ...this.config, ...config }
  163. // 样式改变时更新主题配置
  164. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  165. this.changeChartConfig(config)
  166. this.innerConfig = config
  167. // 时间选择器元素
  168. const { bgColor, fontColor, fontSize } = config.customize
  169. this.$nextTick(() => {
  170. const timePickerEl = document.querySelector(`.date-picker-${config.code}`)
  171. timePickerEl.style.backgroundColor = bgColor
  172. // 时间选择器输入框元素
  173. const timePickerInput = timePickerEl.querySelector('.el-input__inner')
  174. if (timePickerInput) {
  175. // 时间选择器输入框背景颜色
  176. timePickerInput.style.backgroundColor = bgColor
  177. // 时间选择器输入框字体颜色
  178. timePickerInput.style.color = fontColor
  179. // 时间选择器输入框字体大小
  180. timePickerInput.style.fontSize = fontSize + 'px'
  181. }
  182. // 时间范围选择器输入框元素
  183. const timePickerRangeInput = timePickerEl.querySelectorAll('.el-range-input')
  184. if (timePickerRangeInput.length > 0) {
  185. // 连接符
  186. const timePickerRangeSeparator = timePickerEl.querySelector('.el-range-separator')
  187. if (timePickerRangeSeparator) {
  188. // 宽度和字体大小保持一致
  189. timePickerRangeSeparator.style.width = fontSize + 'px'
  190. timePickerRangeSeparator.style.color = fontColor
  191. timePickerRangeSeparator.style.fontSize = fontSize + 'px'
  192. }
  193. timePickerRangeInput.forEach((el) => {
  194. // 时间范围选择器输入框背景颜色
  195. el.style.backgroundColor = bgColor
  196. // 时间范围选择器输入框字体颜色
  197. el.style.color = fontColor
  198. // 时间范围选择器输入框字体大小
  199. el.style.fontSize = fontSize + 'px'
  200. })
  201. }
  202. // 时间选择器图标
  203. const timePickerIcon = timePickerEl.querySelector('.el-input__icon')
  204. if (timePickerIcon) {
  205. timePickerIcon.style.width = fontSize + 'px'
  206. timePickerIcon.style.fontSize = fontSize + 'px'
  207. }
  208. })
  209. },
  210. // 组件联动
  211. changeValue (val) {
  212. // 判断如果val是数组,需要将它转成字符串
  213. if (Array.isArray(val)) {
  214. val = val.join(',')
  215. }
  216. this.linkage({ [this.config.code]: val })
  217. },
  218. focusEvent () {
  219. this.$nextTick(() => {
  220. const { code } = this.innerConfig
  221. const { bgColor, fontColor, hoverFontColor, hoverBgColor, selectedFontColor, rangeBgColor, inputBgColor } = this.innerConfig.customize.dropDownBox
  222. const timePickerPopper = document.querySelector(`.date-picker-popper-${code}`)
  223. if (timePickerPopper) {
  224. // 去除边框
  225. timePickerPopper.style.border = 'none'
  226. // 确保下拉项的箭头颜色与下拉框的背景颜色保持一致
  227. timePickerPopper.style.color = bgColor
  228. }
  229. // 下拉项元素
  230. const pickerDropdownPanleContent = document.querySelector(`.date-picker-popper-${code}`)
  231. if (pickerDropdownPanleContent) {
  232. // 文字颜色
  233. pickerDropdownPanleContent.style.color = fontColor
  234. // 背景颜色
  235. pickerDropdownPanleContent.style.backgroundColor = bgColor
  236. // 下拉项添加var变量
  237. const dropdown = pickerDropdownPanleContent.style
  238. dropdown.setProperty('--fontColor', fontColor)
  239. dropdown.setProperty('--hoverFontColor', hoverFontColor)
  240. dropdown.setProperty('--bgColor', bgColor)
  241. dropdown.setProperty('--inputBgColor', inputBgColor)
  242. dropdown.setProperty('--selectedFontColor', selectedFontColor)
  243. dropdown.setProperty('--hoverBgColor', hoverBgColor)
  244. dropdown.setProperty('--rangeBgColor', rangeBgColor)
  245. // 选中项字体颜色
  246. const selectedEl = pickerDropdownPanleContent.querySelector('.selected')
  247. if (selectedEl) {
  248. selectedEl.style.color = selectedFontColor
  249. }
  250. // 选择过的,需要将选中颜色重置
  251. const pickerItemEl = document.querySelectorAll(`.date-picker-popper-${code} .el-time-spinner__item`)
  252. pickerItemEl.forEach((el) => {
  253. el.style.color = fontColor
  254. })
  255. }
  256. })
  257. },
  258. mouseenter () {
  259. if (this.value) {
  260. setTimeout(() => {
  261. // 清空图标
  262. const timePickerCloseIcon = document.querySelector(`.date-picker-${this.innerConfig.code} .el-icon-circle-close`)
  263. if (timePickerCloseIcon) {
  264. timePickerCloseIcon.style.fontSize = this.innerConfig.customize.fontSize + 'px'
  265. }
  266. }, 25)
  267. }
  268. }
  269. }
  270. }
  271. </script>
  272. <style lang="scss">
  273. .basic-component-date-picker {
  274. color: '';
  275. // 清空图标
  276. .el-icon-circle-close {
  277. display: flex;
  278. align-items: center;
  279. }
  280. // 时间选择器
  281. .el-icon-time {
  282. display: flex;
  283. align-items: center;
  284. }
  285. .el-time-panel {
  286. border: none;
  287. background-color: var(--bgColor);
  288. }
  289. // 选择日期 时间区域
  290. .el-date-picker__time-header {
  291. border-bottom: var(--bgColor);
  292. .el-input__inner {
  293. border: none;
  294. // 添加一点阴影
  295. box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
  296. color: var(--fontColor);
  297. background-color: var(--inputBgColor);
  298. }
  299. }
  300. // 头部,修改文字颜色和图标颜色
  301. .el-date-picker__header {
  302. color: var(--fontColor);
  303. .el-date-picker__header-label {
  304. color: var(--fontColor);
  305. }
  306. // 左右箭头图标颜色
  307. .el-picker-panel__icon-btn {
  308. color: var(--fontColor);
  309. }
  310. }
  311. // datetimerange
  312. .el-date-range-picker__time-header {
  313. border-color: var(--fontColor);
  314. // 中间箭头图标颜色
  315. .el-icon-arrow-right {
  316. color: var(--fontColor);
  317. }
  318. // 时间选择器输入框
  319. .el-input__inner {
  320. border: none;
  321. color: var(--fontColor);
  322. // 添加一点阴影
  323. background-color: var(--inputBgColor);
  324. box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.1);
  325. }
  326. }
  327. // datetimerange
  328. .el-picker-panel__content {
  329. .el-icon-d-arrow-left {
  330. color: var(--fontColor);
  331. &:after {
  332. color: var(--fontColor);
  333. }
  334. }
  335. .el-icon-arrow-left {
  336. color: var(--fontColor);
  337. &:after {
  338. color: var(--fontColor);
  339. }
  340. }
  341. .el-icon-d-arrow-right {
  342. color: var(--fontColor);
  343. &:after {
  344. color: var(--fontColor);
  345. }
  346. }
  347. .el-icon-arrow-right {
  348. color: var(--fontColor);
  349. &:after {
  350. color: var(--fontColor);
  351. }
  352. }
  353. }
  354. .el-date-range-picker__content.is-left {
  355. border-color: var(--fontColor);
  356. }
  357. .el-date-table {
  358. th {
  359. border-color: var(--fontColor);
  360. }
  361. td {
  362. div {
  363. color: var(--fontColor);
  364. &:hover {
  365. color: var(--hoverFontColor);
  366. }
  367. }
  368. }
  369. }
  370. // 范围选择器背景颜色
  371. .in-range {
  372. div {
  373. // 下拉范围选中背景颜色
  374. background-color: var(--rangeBgColor) !important;
  375. }
  376. }
  377. .today {
  378. span {
  379. color: var(--selectedFontColor) !important;
  380. }
  381. }
  382. .el-time-panel__content::before {
  383. content: "";
  384. top: 50%;
  385. position: absolute;
  386. margin-top: -15px;
  387. height: 32px;
  388. z-index: 1;
  389. left: 0;
  390. right: 0;
  391. box-sizing: border-box;
  392. padding-top: 6px;
  393. text-align: left;
  394. border-top: 1px solid var(--fontColor);
  395. border-bottom: 1px solid var(--fontColor);
  396. }
  397. // 脚部
  398. .el-picker-panel__footer {
  399. border-color: var(--fontColor);
  400. background-color: var(--bgColor);
  401. // 清空按钮
  402. .el-picker-panel__link-btn {
  403. span {
  404. color: var(--fontColor);
  405. }
  406. }
  407. // 确定按钮
  408. .el-button--default {
  409. border: none;
  410. color: var(--fontColor);
  411. background-color: var(--bgColor);
  412. }
  413. .is-disabled {
  414. span {
  415. color: #999;
  416. }
  417. }
  418. }
  419. .el-time-spinner {
  420. margin-bottom: 0px;
  421. .el-time-spinner__item {
  422. &:hover {
  423. color: var(--hoverFontColor);
  424. background-color: var(--hoverBgColor);
  425. }
  426. }
  427. .active {
  428. color: var(--selectedFontColor);
  429. &:hover {
  430. color: var(--selectedFontColor);
  431. background-color: transparent;
  432. }
  433. }
  434. }
  435. .popper__arrow {
  436. bottom: -6px;
  437. border-bottom-color: var(--bgColor) !important;
  438. border-top-color: var(--bgColor) !important;
  439. &::after {
  440. bottom: 0px;
  441. border-bottom-color: var(--bgColor) !important;
  442. border-top-color: var(--bgColor) !important;
  443. }
  444. }
  445. .cancel {
  446. color: var(--fontColor);
  447. }
  448. .confirm {
  449. color: var(--selectedFontColor);
  450. }
  451. .el-time-panel__footer {
  452. border-top: 1px solid var(--fontColor);
  453. .cancel {
  454. span {
  455. color: var(--fontColor);
  456. }
  457. }
  458. // 确定按钮
  459. .confirm {
  460. border: none;
  461. color: var(--fontColor);
  462. background-color: var(--bgColor);
  463. }
  464. }
  465. // 年选择器
  466. .el-year-table {
  467. a {
  468. color: var(--fontColor);
  469. &:hover {
  470. color: var(--hoverFontColor);
  471. }
  472. }
  473. }
  474. // 月选择器
  475. .el-month-table {
  476. a {
  477. color: var(--fontColor);
  478. &:hover {
  479. color: var(--hoverFontColor);
  480. }
  481. }
  482. }
  483. // 上月 下月 字体颜色置灰
  484. .prev-month {
  485. span {
  486. color: #999 !important;
  487. &:hover {
  488. color: var(--hoverFontColor) !important;
  489. }
  490. }
  491. }
  492. .next-month {
  493. span {
  494. color: #999 !important;
  495. &:hover {
  496. color: var(--hoverFontColor) !important;
  497. }
  498. }
  499. }
  500. }
  501. </style>
  502. <style lang="scss" scoped>
  503. .basic-component-date-picker {
  504. width: 100% !important;
  505. height: 100% !important;
  506. // 范围时间选择器连接符
  507. ::v-deep .el-range-separator {
  508. display: flex !important;
  509. align-items: center !important;
  510. }
  511. .el-input--mini ::v-deep .el-input__inner {
  512. height: 100% !important;
  513. line-height: 100% !important;
  514. }
  515. .el-tag.el-tag--info {
  516. color: var(--bs-el-text) !important;
  517. }
  518. }
  519. ::v-deep .el-input__inner {
  520. height: 100% !important;
  521. line-height: 100% !important;
  522. }
  523. ::v-deep .el-range-input {
  524. width: 45% !important;
  525. }</style>