BaseCurdList.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import BaseCurd from './BaseCurd'
  2. import MapBuilder from './MapBuilder'
  3. import bindPrototype from './bindPrototype'
  4. export default (Target, hasPagination = true) => ({
  5. name: 'BaseCurdList',
  6. extends: BaseCurd(Target),
  7. data: () => ({
  8. optionGroup: Object.freeze({}),
  9. query: Target.$$getQuery() || {},
  10. oQuery: {},
  11. pagination: {
  12. pageNo: 0,
  13. pageSize: 20
  14. },
  15. oPagination: {
  16. pageNo: 0,
  17. pageSize: 20
  18. },
  19. list: Object.freeze([]),
  20. total: 0,
  21. selectedData: null,
  22. selectedList: Object.freeze([]),
  23. editorName: 'editor'
  24. }),
  25. methods: {
  26. afterLoadList () {
  27. this.selectedList = Object.freeze([])
  28. this.selectedData = null
  29. setTimeout(() => {
  30. this.$refs.table && this.$refs.table.doLayout()
  31. }, 0)
  32. },
  33. loadList () {
  34. let query = Object.assign({}, this.query)
  35. if (hasPagination) {
  36. Object.assign(query, this.pagination)
  37. this.$$request(Target.$$api.getPagination, this.query, this.pagination)
  38. .then(data => {
  39. const { count, list } = data
  40. const { pagination } = this
  41. const { pageSize } = pagination
  42. if (list.length > 0 || pagination.pageNo === 1) {
  43. this.total = count
  44. this.list = Object.freeze(bindPrototype(data.list, Target))
  45. this.afterLoadList()
  46. return
  47. }
  48. pagination.pageNo = Math.floor(count / pageSize)
  49. this.loadList()
  50. }).catch(console.error).finally(() => {})
  51. } else {
  52. this.$$request(Target.$$api.getList, this.query).then(list => {
  53. this.total = list.length
  54. this.list = Object.freeze(bindPrototype(list, Target))
  55. this.afterLoadList()
  56. }).catch(console.error).finally(() => {})
  57. }
  58. },
  59. currentPageNoChange () {
  60. this.loadList()
  61. },
  62. onResetBtnClick () {
  63. this.query = JSON.parse(JSON.stringify(this.oQuery))
  64. this.pagination = { ...this.oPagination }
  65. this.loadList()
  66. },
  67. onSearchBtnClick () {
  68. this.pagination.pageNo = 1
  69. this.loadList()
  70. },
  71. onSelectionChange (selection) {
  72. this.selectedList = Object.freeze(selection)
  73. },
  74. onRowClick (data) {
  75. this.selectedData = data
  76. },
  77. onOpenEditorBtnClick (data, isView) {
  78. const editor = this.getEditor()
  79. data instanceof Event ? editor.open() : editor.open(data, isView)
  80. },
  81. deleteSuccess () {
  82. this.loadList()
  83. this.$notify({
  84. title: '成功',
  85. message: '删除成功',
  86. type: 'success',
  87. position: 'bottom-right'
  88. })
  89. },
  90. deleteFailed () {
  91. this.$notify.error({
  92. title: '失败',
  93. message: '删除失败',
  94. position: 'bottom-right'
  95. })
  96. },
  97. deleteData (data) {
  98. this.$$request(Target.$$api.delete, data, Target)
  99. .then(this.deleteSuccess)
  100. .catch(this.deleteFailed)
  101. .finally(() => {})
  102. },
  103. onDeleteBtnClick (data) {
  104. const name = this[this.$$nameProp]
  105. this.$confirm(`此操作将删除${name ? `'${name}'` : '该记录'}, 是否继续?`, '确认', {
  106. confirmButtonText: '确定',
  107. cancelButtonText: '取消',
  108. confirmButtonClass: 'confirm-dialog-confirm-btn',
  109. cancelButtonClass: 'confirm-dialog-cancel-btn',
  110. type: 'warning'
  111. }).then(() => {
  112. this.deleteData(data)
  113. }).catch(() => {})
  114. },
  115. onSaved () {
  116. this.loadList()
  117. },
  118. getEditor () {
  119. return this.$refs[this.editorName]
  120. },
  121. getOptions (optionTargetConfigs, optionGroup) {
  122. const { query } = this
  123. return Promise.all(
  124. optionTargetConfigs.map(
  125. optionTargetConfig => {
  126. const optionTarget = optionTargetConfig.Target
  127. return this.$$request(optionTarget.$$api.getList, typeof optionTargetConfig.getQuery === 'function' ? optionTargetConfig.getQuery(optionGroup) : undefined).then(options => {
  128. options = bindPrototype(options, optionTarget)
  129. optionGroup[optionTarget.name] = {
  130. list: options,
  131. map: MapBuilder.getObjectMap(options, optionTarget.$$idProp),
  132. Target: optionTarget
  133. }
  134. const { setQuery } = optionTargetConfig
  135. typeof setQuery === 'function' && setQuery(options, query, optionTarget)
  136. })
  137. })
  138. )
  139. },
  140. async getOptionGroup () {
  141. const optionGroup = {}
  142. const { $$optionTargetConfigGroup } = Target
  143. if (Array.isArray($$optionTargetConfigGroup)) {
  144. const $$optionTargetConfigGroupLen = $$optionTargetConfigGroup.length
  145. for (let i = 0; i < $$optionTargetConfigGroupLen; i++) {
  146. const optionTargetConfigs = $$optionTargetConfigGroup[i]
  147. const optionTargetConfigsLen = optionTargetConfigs.length
  148. if (optionTargetConfigsLen > 0) {
  149. await this.getOptions(optionTargetConfigs, optionGroup)
  150. }
  151. }
  152. }
  153. return Object.freeze(optionGroup)
  154. },
  155. setOptionGroup () {
  156. this.getOptionGroup(Target).then(optionGroup => {
  157. this.optionGroup = optionGroup
  158. Target.$$optionData = optionGroup
  159. this.oQuery = Object.freeze(JSON.parse(JSON.stringify(this.query)))
  160. this.loadList()
  161. })
  162. }
  163. },
  164. created () {
  165. this.setOptionGroup()
  166. }
  167. })