util.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { IViewModel, ISqlColumn, IView, IFormedView, IViewRoleRaw, IViewRole, IKeyOfViewModelProps } from './types'
  2. import { SqlTypes } from 'app/globalConstants'
  3. import { DefaultModelTypeSqlTypeSetting, VisualTypeSqlTypeSetting, ViewModelVisualTypes, ViewModelTypes } from './constants'
  4. import { hasProperty } from 'utils/util'
  5. export function getFormedView (view: IView): IFormedView {
  6. const { model, variable, roles } = view
  7. const formedView = {
  8. ...view,
  9. model: JSON.parse((model || '{}')),
  10. variable: JSON.parse((variable || '[]')),
  11. roles: (roles as IViewRoleRaw[]).map<IViewRole>(({ roleId, columnAuth, rowAuth }) => ({
  12. roleId,
  13. columnAuth: JSON.parse(columnAuth || '[]'),
  14. rowAuth: JSON.parse(rowAuth || '[]')
  15. }))
  16. }
  17. return formedView
  18. }
  19. function getMapKeyByValue (value: SqlTypes, map: typeof VisualTypeSqlTypeSetting | typeof DefaultModelTypeSqlTypeSetting) {
  20. let result
  21. Object.entries(map).some(([key, values]) => {
  22. if (values.includes(value)) {
  23. result = key
  24. return true
  25. }
  26. })
  27. return result
  28. }
  29. export function getValidModel (model: IViewModel, sqlColumns: ISqlColumn[]) {
  30. if (!Array.isArray(sqlColumns)) { return {} }
  31. const validModel = sqlColumns.reduce<IViewModel>((accModel, column) => {
  32. const { name: columnName, type: columnType } = column
  33. const modelItem = model[columnName]
  34. if (!modelItem) {
  35. accModel[columnName] = {
  36. sqlType: columnType,
  37. // model item which columnType not registered with SQL_TYPES in globalConstants.ts
  38. // its default visualType is String and modelType is Category
  39. visualType: getMapKeyByValue(columnType, VisualTypeSqlTypeSetting) || ViewModelVisualTypes.String,
  40. modelType: getMapKeyByValue(columnType, DefaultModelTypeSqlTypeSetting) || ViewModelTypes.Category
  41. }
  42. } else {
  43. accModel[columnName] = { ...modelItem, sqlType: columnType } // update newest sqlType
  44. // verify visualType are valid by the sqlType or not
  45. // @TODO recover visualType validation after filter visualType select options by columnType in step2
  46. // if (SQL_TYPES.includes(columnType)) { // model item which columnType not registered with SQL_TYPES do not need verify
  47. // if (VisualTypeSqlTypeSetting[modelItem.visualType]
  48. // && !VisualTypeSqlTypeSetting[modelItem.visualType].includes(columnType)) {
  49. // accModel[columnName].visualType = getMapKeyByValue(columnType, VisualTypeSqlTypeSetting)
  50. // }
  51. // }
  52. // @TODO changed visualType need be shown in step2 corresponding model table cell
  53. }
  54. return accModel
  55. }, {})
  56. return validModel
  57. }
  58. export function getValidRoleModelNames (model: IViewModel, modelNames: string[]) {
  59. if (!Array.isArray(modelNames)) { return [] }
  60. const validModelNames = modelNames.filter((name) => !!model[name])
  61. return validModelNames
  62. }
  63. export function getTypesOfModelCollect (model: IViewModel, type: IKeyOfViewModelProps) {
  64. let target = {}
  65. if (model) {
  66. return target = Object.keys(model).reduce((iteratee, current) => {
  67. iteratee[current] = hasProperty(model[current], type)
  68. return iteratee
  69. }, {})
  70. }
  71. return target
  72. }
  73. function cacheManager (model: IViewModel, type: IKeyOfViewModelProps) {
  74. const cache = {}
  75. return function (name: string, callback) {
  76. if (cache['prevModel'] === model) {
  77. cache[type] = cache[type] || getTypesOfModelCollect(model, type)
  78. } else {
  79. cache['prevModel'] = model
  80. cache[type] = getTypesOfModelCollect(model, type)
  81. }
  82. return callback(cache)
  83. }
  84. }
  85. export const getTypesOfModelByKeyName = (model: IViewModel, type: IKeyOfViewModelProps) => (name: string) => {
  86. return cacheManager(model, type)(name, (cache) => {
  87. if (cache && cache[type]) {
  88. return hasProperty(cache[type], name)
  89. }
  90. })
  91. }
  92. export const getListsByViewModelTypes = (model: IViewModel, type: IKeyOfViewModelProps) => (modelType: ViewModelTypes) => {
  93. return cacheManager(model, type)(name, (cache) => {
  94. const target = cache[type]
  95. return Object.keys(target).reduce((iteratee, current) => {
  96. return iteratee.concat(target[current] === modelType ? current : [])
  97. }, [])
  98. })
  99. }