formCreateIndex.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import is, { hasProperty } from '@form-create/utils/lib/type'
  2. import { parseFn } from '@form-create/utils/lib/json'
  3. import toCase from '@form-create/utils/lib/tocase'
  4. import { computed, isRef, unref, ref } from 'vue'
  5. import ZhCn from '../lang/zh-cn'
  6. export function makeRequiredRule() {
  7. return {
  8. type: 'Required',
  9. field: 'formCreate$required',
  10. title: '是否必填'
  11. }
  12. }
  13. export function makeOptionsRule(t, to, flag) {
  14. const options = [
  15. { label: t('props.optionsType.json'), value: 0 },
  16. { label: t('props.optionsType.fetch'), value: 1 }
  17. ]
  18. const control = [
  19. {
  20. value: 0,
  21. rule: [
  22. {
  23. type: 'Struct',
  24. field: 'formCreate' + upper(to).replace('.', '>'),
  25. props: { defaultValue: [] }
  26. }
  27. ]
  28. },
  29. {
  30. value: 1,
  31. rule: [
  32. {
  33. type: 'Fetch',
  34. field: 'formCreateEffect>fetch',
  35. props: {
  36. to
  37. }
  38. }
  39. ]
  40. }
  41. ]
  42. if (flag !== false) {
  43. options.splice(0, 0, { label: t('props.optionsType.struct'), value: 2 })
  44. control.push({
  45. value: 2,
  46. rule: [
  47. {
  48. type: 'TableOptions',
  49. field: 'formCreate' + upper(to).replace('.', '>'),
  50. props: { defaultValue: [] }
  51. }
  52. ]
  53. })
  54. }
  55. return {
  56. type: 'radio',
  57. title: t('props.options'),
  58. field: '_optionType',
  59. value: flag !== false ? 2 : 0,
  60. options,
  61. props: {
  62. type: 'button'
  63. },
  64. control
  65. }
  66. }
  67. export function upper(str) {
  68. return str.replace(str[0], str[0].toLocaleUpperCase())
  69. }
  70. export const toJSON = function (val) {
  71. const type = /object ([a-zA-Z]*)/.exec(Object.prototype.toString.call(val))
  72. if (type && _toJSON[type[1].toLowerCase()]) {
  73. return _toJSON[type[1].toLowerCase()](val)
  74. } else {
  75. return val
  76. }
  77. }
  78. const _toJSON = {
  79. object: function (val) {
  80. const json = []
  81. for (const i in val) {
  82. if (!hasProperty(val, i)) continue
  83. json.push(toJSON(i) + ': ' + (val[i] != null ? toJSON(val[i]) : 'null'))
  84. }
  85. return '{\n ' + json.join(',\n ') + '\n}'
  86. },
  87. function: function (val) {
  88. val = '' + val
  89. const exec = /^ *([\w]+) *\(/.exec(val)
  90. if (exec && exec[1] !== 'function') {
  91. return 'function ' + val
  92. }
  93. return val
  94. },
  95. array: function (val) {
  96. for (var i = 0, json = []; i < val.length; i++) json[i] = val[i] != null ? toJSON(val[i]) : 'null'
  97. return '[' + json.join(', ') + ']'
  98. },
  99. string: function (val) {
  100. const tmp = val.split('')
  101. for (let i = 0; i < tmp.length; i++) {
  102. let c = tmp[i]
  103. c >= ' '
  104. ? c === '\\'
  105. ? (tmp[i] = '\\\\')
  106. : c === '"'
  107. ? (tmp[i] = '\\"')
  108. : 0
  109. : (tmp[i] =
  110. c === '\n'
  111. ? '\\n'
  112. : c === '\r'
  113. ? '\\r'
  114. : c === '\t'
  115. ? '\\t'
  116. : c === '\b'
  117. ? '\\b'
  118. : c === '\f'
  119. ? '\\f'
  120. : ((c = c.charCodeAt()), '\\u00' + (c > 15 ? 1 : 0) + (c % 16)))
  121. }
  122. return '"' + tmp.join('') + '"'
  123. }
  124. }
  125. export const deepParseFn = function (target) {
  126. if (target && typeof target === 'object') {
  127. for (const key in target) {
  128. if (Object.prototype.hasOwnProperty.call(target, key)) {
  129. const data = target[key]
  130. if (Array.isArray(data) || is.Object(data)) {
  131. deepParseFn(data)
  132. }
  133. if (is.String(data)) {
  134. target[key] = parseFn(data)
  135. }
  136. }
  137. }
  138. }
  139. return target
  140. }
  141. function get(object, path, defaultValue) {
  142. path = (path || '').split('.')
  143. let index = 0,
  144. length = path.length
  145. while (object != null && index < length) {
  146. object = object[path[index++]]
  147. }
  148. return index && index === length ? (object !== undefined ? object : defaultValue) : defaultValue
  149. }
  150. export const buildTranslator = locale => (path, option) => translate(path, option, unref(locale))
  151. export const translate = (path, option, locale) => get(locale, path, '').replace(/\{(\w+)\}/g, (_, key) => `${option?.[key] ?? `{${key}}`}`)
  152. export const buildLocaleContext = locale => {
  153. const lang = computed(() => unref(locale).name)
  154. const name = computed(() => upper(toCase(lang.value || '')))
  155. const localeRef = isRef(locale) ? locale : ref(locale)
  156. return {
  157. lang,
  158. name,
  159. locale: localeRef,
  160. t: buildTranslator(locale)
  161. }
  162. }
  163. export const useLocale = locale => {
  164. return buildLocaleContext(computed(() => locale.value || ZhCn))
  165. }
  166. export const localeProps = (t, prefix, rules) => {
  167. return rules.map(rule => {
  168. if (rule.field === 'formCreate$required') {
  169. rule.title = t('props.required') || rule.title
  170. } else if (rule.field && rule.field !== '_optionType') {
  171. rule.title = t('components.' + prefix + '.' + rule.field) || rule.title
  172. }
  173. return rule
  174. })
  175. }