widgetConfig.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import controlMigrationRecorder from './control'
  21. import { IMigrationRecorder } from '.'
  22. import {
  23. IWidgetConfig,
  24. IChartStyles,
  25. IWidgetDimension
  26. } from 'app/containers/Widget/components/Widget'
  27. import barDefaultConfig from 'app/containers/Widget/config/chart/bar'
  28. import { EmptyStack } from 'app/containers/Widget/components/Config/Stack/constants'
  29. import {
  30. IFieldSortConfig,
  31. FieldSortTypes
  32. } from 'app/containers/Widget/components/Config/Sort'
  33. import ChartTypes from 'app/containers/Widget/config/chart/ChartTypes'
  34. import { ControlQueryMode } from 'app/components/Control/constants'
  35. interface IWidgetConfigMigrationRecorder extends IMigrationRecorder {
  36. recorders: {
  37. beta5(config: IWidgetConfig): IWidgetConfig
  38. beta6(config: IWidgetConfig): IWidgetConfig
  39. beta9(config: IWidgetConfig, options?): IWidgetConfig
  40. }
  41. }
  42. const widgetConfigMigrationRecorder: IWidgetConfigMigrationRecorder = {
  43. versions: ['beta5', 'beta6', 'beta9'],
  44. recorders: {
  45. beta5(config) {
  46. return {
  47. ...config,
  48. controls: config.controls.map((control) =>
  49. controlMigrationRecorder.beta5(control)
  50. )
  51. }
  52. },
  53. beta6(config) {
  54. const { mode, selectedChart } = config
  55. // bar chartStyles migration
  56. let chartStyles = { ...config.chartStyles }
  57. if (mode === 'chart' && selectedChart === ChartTypes.Bar) {
  58. const { bar: barConfig, spec: barSpec } = chartStyles
  59. const { bar: defaultBarConfig } = barDefaultConfig.style as IChartStyles
  60. if (!barConfig) {
  61. chartStyles = {
  62. ...chartStyles,
  63. bar: {
  64. ...defaultBarConfig,
  65. barChart: !!barSpec.barChart,
  66. stack: {
  67. ...defaultBarConfig.stack,
  68. on: !!barSpec.stack,
  69. percentage: barSpec.percentage
  70. }
  71. }
  72. }
  73. } else {
  74. if (!barConfig.stack) {
  75. chartStyles = {
  76. ...chartStyles,
  77. bar: {
  78. ...barConfig,
  79. stack: {
  80. ...EmptyStack,
  81. on: !!barSpec.stack,
  82. percentage: barSpec.percentage
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. // cols and rows migration
  90. const cols = config.cols.map((c) => beta6DimensionFix(c))
  91. const rows = config.rows.map((r) => beta6DimensionFix(r))
  92. // autoLoadData migration
  93. const autoLoadData =
  94. config.autoLoadData === void 0 ? true : config.autoLoadData
  95. return {
  96. ...config,
  97. chartStyles,
  98. cols,
  99. rows,
  100. autoLoadData
  101. }
  102. },
  103. beta9(config, options) {
  104. return {
  105. ...config,
  106. controls: config.controls.map((control) =>
  107. controlMigrationRecorder.beta9(control, options)
  108. ),
  109. queryMode:
  110. config.queryMode === void 0
  111. ? ControlQueryMode.Manually
  112. : config.queryMode,
  113. limit: config.limit === void 0 ? null : config.limit
  114. }
  115. }
  116. }
  117. }
  118. function beta6DimensionFix(dimension: IWidgetDimension): IWidgetDimension {
  119. const { sort } = dimension
  120. if (typeof sort === 'string') {
  121. const sortConfig: IFieldSortConfig = {
  122. sortType: sort as FieldSortTypes
  123. }
  124. return {
  125. ...dimension,
  126. sort: sortConfig
  127. }
  128. }
  129. return dimension
  130. }
  131. export default widgetConfigMigrationRecorder