EntityCrud.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <template>
  2. <slot v-if="currentComponent === 'entity-create'"
  3. name="before-entity-create" />
  4. <slot v-if="currentComponent === 'entity-show'"
  5. name="before-entity-show" />
  6. <slot v-if="currentComponent === 'entity-edit'"
  7. name="before-entity-edit" />
  8. <slot v-if="currentComponent === 'entity-index'"
  9. name="before-entity-index" />
  10. <component :is="currentComponent"
  11. v-bind="neededConfig"
  12. ref="entityComponent"
  13. v-model:value="getNeededInputs"
  14. :before-load-input-data="getBeforeLoadInputDataProp"
  15. :after-load-input-data="getAfterLoadInputDataProp"
  16. :before-get-data="getBeforeGetEditDataProp">
  17. <template v-slot:before-form-builder>
  18. <slot v-if="currentComponent === 'entity-create'"
  19. name="entity-create-before-form-builder" />
  20. <slot v-if="currentComponent === 'entity-show'"
  21. name="entity-show-before-form-builder" />
  22. <slot v-if="currentComponent === 'entity-edit'"
  23. name="entity-edit-before-form-builder" />
  24. <slot v-if="currentComponent === 'entity-index'"
  25. name="entity-index-before-form-builder" />
  26. </template>
  27. <template v-slot:after-form-builder>
  28. <slot v-if="currentComponent === 'entity-create'"
  29. name="entity-create-after-form-builder" />
  30. <slot v-if="currentComponent === 'entity-show'"
  31. name="entity-show-after-form-builder" />
  32. <slot v-if="currentComponent === 'entity-edit'"
  33. name="entity-edit-after-form-builder" />
  34. <slot v-if="currentComponent === 'entity-index'"
  35. name="entity-index-after-form-builder" />
  36. </template>
  37. <template v-slot:before-index-table>
  38. <slot v-if="currentComponent === 'entity-index'"
  39. name="entity-crud-before-index-table" />
  40. </template>
  41. <template v-slot:after-index-table>
  42. <slot v-if="currentComponent === 'entity-index'"
  43. name="entity-crud-after-index-table" />
  44. </template>
  45. <template v-slot:table-cell="{inputData, showConfirmRemoveDialog}">
  46. <slot v-if="currentComponent === 'entity-index'"
  47. name="entity-crud-table-cell"
  48. :inputData="inputData"
  49. :showConfirmRemoveDialog="showConfirmRemoveDialog" />
  50. </template>
  51. </component>
  52. <slot v-if="currentComponent === 'entity-create'"
  53. name="after-entity-create" />
  54. <slot v-if="currentComponent === 'entity-show'"
  55. name="after-entity-show" />
  56. <slot v-if="currentComponent === 'entity-edit'"
  57. name="after-entity-edit" />
  58. <slot v-if="currentComponent === 'entity-index'"
  59. name="after-entity-index" />
  60. </template>
  61. <script>
  62. import { EntityEdit, EntityCreate, EntityIndex, EntityShow } from 'quasar-crud'
  63. export default {
  64. name: 'EntityCrud',
  65. components: {
  66. EntityEdit,
  67. EntityCreate,
  68. EntityIndex,
  69. EntityShow
  70. },
  71. props: {
  72. createInputs: {
  73. type: Array,
  74. default () {
  75. return []
  76. }
  77. },
  78. showInputs: {
  79. type: Array,
  80. default () {
  81. return []
  82. }
  83. },
  84. editInputs: {
  85. type: Array,
  86. default () {
  87. return []
  88. }
  89. },
  90. indexInputs: {
  91. type: Array,
  92. default () {
  93. return []
  94. }
  95. },
  96. defaultInputs: {
  97. type: Array,
  98. default () {
  99. return []
  100. }
  101. },
  102. beforeGetEditData: {
  103. default: () => {},
  104. type: Function
  105. },
  106. beforeGetShowData: {
  107. default: () => {},
  108. type: Function
  109. },
  110. beforeLoadEditInputData: {
  111. default: () => {},
  112. type: Function
  113. },
  114. afterLoadEditInputData: {
  115. default: () => {},
  116. type: Function
  117. },
  118. beforeLoadShowInputData: {
  119. default: () => {},
  120. type: Function
  121. },
  122. afterLoadShowInputData: {
  123. default: () => {},
  124. type: Function
  125. },
  126. beforeLoadIndexInputData: {
  127. default: () => {},
  128. type: Function
  129. },
  130. afterLoadIndexInputData: {
  131. default: () => {},
  132. type: Function
  133. },
  134. beforeLoadCreateInputData: {
  135. default: () => {},
  136. type: Function
  137. },
  138. afterLoadCreateInputData: {
  139. default: () => {},
  140. type: Function
  141. },
  142. config: {
  143. type: Object,
  144. default () {
  145. return {}
  146. }
  147. },
  148. customInitialMode: {
  149. type: String,
  150. default () {
  151. return ''
  152. }
  153. }
  154. },
  155. emits: [
  156. 'update:defaultInputs',
  157. 'update:indexInputs',
  158. 'update:editInputs',
  159. 'update:createInputs',
  160. 'update:showInputs'
  161. ],
  162. data () {
  163. return {
  164. currentComponent: '',
  165. currentMode: '',
  166. neededConfig: {}
  167. }
  168. },
  169. computed: {
  170. getNeededInputs () {
  171. if (this[this.currentMode + 'Inputs'].length > 0) {
  172. return this[this.currentMode + 'Inputs']
  173. }
  174. return this.inputDefaultValue
  175. },
  176. getBeforeLoadInputDataProp () {
  177. if (this.$props['beforeLoad' + this.capitalizeFirstLetter(this.currentMode) + 'InputData']) {
  178. return this.$props['beforeLoad' + this.capitalizeFirstLetter(this.currentMode) + 'InputData']
  179. }
  180. return null
  181. },
  182. getAfterLoadInputDataProp () {
  183. if (this.$props['afterLoad' + this.capitalizeFirstLetter(this.currentMode) + 'InputData']) {
  184. return this.$props['afterLoad' + this.capitalizeFirstLetter(this.currentMode) + 'InputData']
  185. }
  186. return null
  187. },
  188. getBeforeGetEditDataProp () {
  189. if (this.$props['beforeGet' + this.capitalizeFirstLetter(this.currentMode) + 'Data']) {
  190. return this.$props['beforeGet' + this.capitalizeFirstLetter(this.currentMode) + 'Data']
  191. }
  192. return null
  193. },
  194. inputDefaultValue: {
  195. get () {
  196. return this.defaultInputs
  197. },
  198. set (value) {
  199. this.$emit('update:defaultInputs', value)
  200. }
  201. },
  202. indexDefaultValue: {
  203. get () {
  204. return this.indexInputs
  205. },
  206. set (value) {
  207. this.$emit('update:indexInputs', value)
  208. }
  209. },
  210. showDefaultValue: {
  211. get () {
  212. return this.showInputs
  213. },
  214. set (value) {
  215. this.$emit('update:showInputs', value)
  216. }
  217. },
  218. editDefaultValue: {
  219. get () {
  220. return this.editInputs
  221. },
  222. set (value) {
  223. this.$emit('update:editInputs', value)
  224. }
  225. },
  226. createDefaultValue: {
  227. get () {
  228. return this.createInputs
  229. },
  230. set (value) {
  231. this.$emit('update:createInputs', value)
  232. }
  233. },
  234. getRouteChange () {
  235. return this.$route.path
  236. }
  237. },
  238. watch: {
  239. getRouteChange () {
  240. // to, from
  241. this.getComponent()
  242. }
  243. },
  244. created () {
  245. this.getComponent()
  246. },
  247. mounted () {},
  248. methods: {
  249. getComponent () {
  250. const cName = this.getRoutesMode()
  251. let componentName = ''
  252. if (cName) {
  253. componentName = 'entity-' + cName
  254. this.createComponentConfig(cName)
  255. }
  256. this.currentComponent = componentName
  257. this.currentMode = cName
  258. },
  259. SetApiId () {
  260. if (this.$route.params.id) {
  261. this.neededConfig.api += this.$route.params.id
  262. }
  263. },
  264. createComponentConfig (mode) {
  265. const componentConfig = {}
  266. let currentModeProps = {}
  267. if (mode === 'show') {
  268. currentModeProps = EntityShow.props
  269. } else if (mode === 'index') {
  270. currentModeProps = EntityIndex.props
  271. } else if (mode === 'edit') {
  272. currentModeProps = EntityEdit.props
  273. } else if (mode === 'create') {
  274. currentModeProps = EntityCreate.props
  275. }
  276. for (const key in currentModeProps) {
  277. if (this.checkIfPropertyExists(key)) {
  278. componentConfig[key] = this.config[key]
  279. }
  280. }
  281. this.neededConfig = this.getModdedComponentProperties(mode, componentConfig)
  282. if (mode !== ('index' || 'create')) {
  283. this.SetApiId()
  284. }
  285. },
  286. getModdedComponentProperties (mode, componentConfig) {
  287. if (this.config.api[mode]) {
  288. componentConfig.api = this.config.api[mode]
  289. }
  290. if (this.config.title[mode]) {
  291. componentConfig.title = this.config.title[mode]
  292. }
  293. return componentConfig
  294. },
  295. checkIfPropertyExists (key) {
  296. return !!(this.config[key])
  297. },
  298. capitalizeFirstLetter (word) {
  299. const str = word
  300. return str.charAt(0).toUpperCase() + str.slice(1)
  301. },
  302. getRoutesMode () {
  303. const allModes = ['show', 'index', 'edit', 'create']
  304. const routeMode = this.$route.name.toLowerCase()
  305. return this.customInitialMode ? this.customInitialMode : allModes.find(mode => routeMode.includes('.' + mode))
  306. }
  307. }
  308. }
  309. </script>
  310. <style scoped>
  311. </style>