import-panel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { VxeUI } from '../../../ui';
  2. import XEUtils from 'xe-utils';
  3. import { parseFile } from '../../../ui/src/utils';
  4. import { errLog } from '../../../ui/src/log';
  5. const { getI18n, getIcon, globalMixins, renderEmptyElement } = VxeUI;
  6. export default {
  7. name: 'VxeTableImportPanel',
  8. mixins: [
  9. globalMixins.sizeMixin
  10. ],
  11. props: {
  12. defaultOptions: Object,
  13. storeData: Object
  14. },
  15. components: {
  16. // VxeModal,
  17. // VxeRadio
  18. },
  19. inject: {
  20. $xeTable: {
  21. default: null
  22. }
  23. },
  24. data() {
  25. return {
  26. loading: false
  27. };
  28. },
  29. computed: {
  30. selectName() {
  31. return `${this.storeData.filename}.${this.storeData.type}`;
  32. },
  33. hasFile() {
  34. return this.storeData.file && this.storeData.type;
  35. },
  36. parseTypeLabel() {
  37. const { storeData } = this;
  38. const { type, typeList } = storeData;
  39. if (type) {
  40. const selectItem = XEUtils.find(typeList, item => type === item.value);
  41. return selectItem ? selectItem.label : '*.*';
  42. }
  43. return `*.${typeList.map((item) => item.value).join(', *.')}`;
  44. }
  45. },
  46. created() {
  47. const $xeTableImportPanel = this;
  48. const VxeUIModalComponent = VxeUI.getComponent('VxeModal');
  49. const VxeUIButtonComponent = VxeUI.getComponent('VxeButton');
  50. const VxeUISelectComponent = VxeUI.getComponent('VxeSelect');
  51. $xeTableImportPanel.$nextTick(() => {
  52. if (!VxeUIModalComponent) {
  53. errLog('vxe.error.reqComp', ['vxe-modal']);
  54. }
  55. if (!VxeUIButtonComponent) {
  56. errLog('vxe.error.reqComp', ['vxe-button']);
  57. }
  58. if (!VxeUISelectComponent) {
  59. errLog('vxe.error.reqComp', ['vxe-select']);
  60. }
  61. });
  62. },
  63. render(h) {
  64. const $xeTable = this.$parent;
  65. const $xeGrid = $xeTable.$xeGrid;
  66. const $xeGantt = $xeTable.$xeGantt;
  67. const { hasFile, loading, parseTypeLabel, defaultOptions, storeData, selectName } = this;
  68. const slots = defaultOptions.slots || {};
  69. const topSlot = slots.top;
  70. const bottomSlot = slots.bottom;
  71. const defaultSlot = slots.default;
  72. const footerSlot = slots.footer;
  73. return h('vxe-modal', {
  74. ref: 'modal',
  75. props: {
  76. id: 'VXE_IMPORT_MODAL',
  77. value: storeData.visible,
  78. title: getI18n('vxe.import.impTitle'),
  79. width: 540,
  80. minWidth: 360,
  81. minHeight: 240,
  82. mask: true,
  83. lockView: true,
  84. showFooter: true,
  85. escClosable: true,
  86. maskClosable: true,
  87. showMaximize: true,
  88. resize: true,
  89. loading
  90. },
  91. on: {
  92. input(value) {
  93. storeData.visible = value;
  94. },
  95. show: this.showEvent
  96. },
  97. scopedSlots: {
  98. default: () => {
  99. const params = {
  100. $table: $xeTable,
  101. $grid: $xeGrid,
  102. $gantt: $xeGantt,
  103. options: defaultOptions,
  104. params: defaultOptions.params
  105. };
  106. return h('div', {
  107. class: 'vxe-table-export--panel'
  108. }, [
  109. topSlot
  110. ? h('div', {
  111. class: 'vxe-table-export--panel-top'
  112. }, $xeTable.callSlot(topSlot, params, h))
  113. : renderEmptyElement(this),
  114. h('div', {
  115. class: 'vxe-table-export--panel-body'
  116. }, defaultSlot
  117. ? $xeTable.callSlot(defaultSlot, params, h)
  118. : [
  119. h('table', {
  120. class: 'vxe-table-export--panel-table',
  121. attrs: {
  122. cellspacing: 0,
  123. cellpadding: 0,
  124. border: 0
  125. }
  126. }, [
  127. h('tbody', [
  128. h('tr', [
  129. h('td', getI18n('vxe.import.impFile')),
  130. h('td', [
  131. hasFile
  132. ? h('div', {
  133. class: 'vxe-table-export--selected--file',
  134. attrs: {
  135. title: selectName
  136. }
  137. }, [
  138. h('span', selectName),
  139. h('i', {
  140. class: getIcon().INPUT_CLEAR,
  141. on: {
  142. click: this.clearFileEvent
  143. }
  144. })
  145. ])
  146. : h('button', {
  147. ref: 'fileBtn',
  148. class: 'vxe-table-export--select--file',
  149. attrs: {
  150. type: 'button'
  151. },
  152. on: {
  153. click: this.selectFileEvent
  154. }
  155. }, getI18n('vxe.import.impSelect'))
  156. ])
  157. ]),
  158. h('tr', [
  159. h('td', getI18n('vxe.import.impType')),
  160. h('td', parseTypeLabel)
  161. ]),
  162. h('tr', [
  163. h('td', getI18n('vxe.import.impMode')),
  164. h('td', [
  165. h('vxe-select', {
  166. props: {
  167. value: defaultOptions.mode,
  168. options: storeData.modeList
  169. },
  170. on: {
  171. modelValue(value) {
  172. defaultOptions.mode = value;
  173. }
  174. }
  175. })
  176. ])
  177. ])
  178. ])
  179. ])
  180. ]),
  181. bottomSlot
  182. ? h('div', {
  183. class: 'vxe-table-export--panel-bottom'
  184. }, $xeTable.callSlot(bottomSlot, params, h))
  185. : renderEmptyElement(this)
  186. ]);
  187. },
  188. footer: () => {
  189. const params = {
  190. $table: $xeTable,
  191. $grid: $xeGrid,
  192. $gantt: $xeGantt,
  193. options: defaultOptions,
  194. params: defaultOptions.params
  195. };
  196. return h('div', {
  197. class: 'vxe-table-export--panel-footer'
  198. }, footerSlot
  199. ? $xeTable.callSlot(footerSlot, params, h)
  200. : [
  201. h('div', {
  202. class: 'vxe-table-export--panel-btns'
  203. }, [
  204. h('vxe-button', {
  205. on: {
  206. click: this.cancelEvent
  207. }
  208. }, getI18n('vxe.import.impCancel')),
  209. h('vxe-button', {
  210. props: {
  211. status: 'primary',
  212. disabled: !hasFile || loading
  213. },
  214. on: {
  215. click: this.importEvent
  216. }
  217. }, getI18n('vxe.import.impConfirm'))
  218. ])
  219. ]);
  220. }
  221. }
  222. });
  223. },
  224. methods: {
  225. clearFileEvent() {
  226. Object.assign(this.storeData, {
  227. filename: '',
  228. sheetName: '',
  229. type: ''
  230. });
  231. },
  232. selectFileEvent() {
  233. const $xeTable = this.$parent;
  234. $xeTable.readFile(this.defaultOptions).then((params) => {
  235. const { file } = params;
  236. Object.assign(this.storeData, parseFile(file), { file });
  237. }).catch((e) => e);
  238. },
  239. showEvent() {
  240. this.$nextTick(() => {
  241. const { $refs } = this;
  242. const targetElem = $refs.fileBtn;
  243. if (targetElem) {
  244. targetElem.focus();
  245. }
  246. });
  247. },
  248. cancelEvent() {
  249. this.storeData.visible = false;
  250. },
  251. importEvent() {
  252. const $xeTable = this.$parent;
  253. this.loading = true;
  254. $xeTable.importByFile(this.storeData.file, Object.assign({}, $xeTable.importOpts, this.defaultOptions)).then(() => {
  255. this.loading = false;
  256. this.storeData.visible = false;
  257. }).catch(() => {
  258. this.loading = false;
  259. });
  260. }
  261. }
  262. };