index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _Table = require('./Table');
  10. var _Table2 = _interopRequireDefault(_Table);
  11. var _vueRef = require('vue-ref');
  12. var _vueRef2 = _interopRequireDefault(_vueRef);
  13. var _vue = require('vue');
  14. var _vue2 = _interopRequireDefault(_vue);
  15. var _propsUtil = require('../_util/props-util');
  16. var _base = require('../base');
  17. var _base2 = _interopRequireDefault(_base);
  18. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  19. _vue2['default'].use(_vueRef2['default'], { name: 'ant-ref' });
  20. var Table = {
  21. name: 'ATable',
  22. Column: _Table2['default'].Column,
  23. ColumnGroup: _Table2['default'].ColumnGroup,
  24. props: _Table2['default'].props,
  25. methods: {
  26. normalize: function normalize() {
  27. var _this = this;
  28. var elements = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  29. var columns = [];
  30. elements.forEach(function (element) {
  31. if (!element.tag) {
  32. return;
  33. }
  34. var key = (0, _propsUtil.getKey)(element);
  35. var style = (0, _propsUtil.getStyle)(element);
  36. var cls = (0, _propsUtil.getClass)(element);
  37. var props = (0, _propsUtil.getOptionProps)(element);
  38. var events = (0, _propsUtil.getEvents)(element);
  39. var listeners = {};
  40. Object.keys(events).forEach(function (e) {
  41. /*
  42. Convert events on template Column to function props onPropAbcChange in Table.columns prop.
  43. If you write template code like below:
  44. <Column @prop-abc-change="f1" @update:prop-abc="f2" :prop-abc.sync="dataAbc" />
  45. You will get these events:
  46. {
  47. 'prop-abc-change': this.f1,
  48. 'update:prop-abc': [this.f2, e => this.dataAbc = e],
  49. 'update:propAbc': e => this.dataAbc = e,
  50. }
  51. All of these events would be treat as column.onPropAbcChange,
  52. but only one of them will be valid, which can not be determined.
  53. */
  54. var k = void 0;
  55. if (e.startsWith('update:')) {
  56. k = 'on-' + e.substr('update:'.length) + '-change';
  57. } else {
  58. k = 'on-' + e;
  59. }
  60. listeners[(0, _propsUtil.camelize)(k)] = events[e];
  61. });
  62. var _getSlots = (0, _propsUtil.getSlots)(element),
  63. children = _getSlots['default'],
  64. restSlots = (0, _objectWithoutProperties3['default'])(_getSlots, ['default']);
  65. var column = (0, _extends3['default'])({}, restSlots, props, { style: style, 'class': cls }, listeners);
  66. if (key) {
  67. column.key = key;
  68. }
  69. if ((0, _propsUtil.getSlotOptions)(element).__ANT_TABLE_COLUMN_GROUP) {
  70. column.children = _this.normalize(typeof children === 'function' ? children() : children);
  71. } else {
  72. var customRender = element.data && element.data.scopedSlots && element.data.scopedSlots['default'];
  73. column.customRender = column.customRender || customRender;
  74. }
  75. columns.push(column);
  76. });
  77. return columns;
  78. },
  79. updateColumns: function updateColumns() {
  80. var _this2 = this;
  81. var cols = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  82. var columns = [];
  83. var $slots = this.$slots,
  84. $scopedSlots = this.$scopedSlots;
  85. cols.forEach(function (col) {
  86. var _col$slots = col.slots,
  87. slots = _col$slots === undefined ? {} : _col$slots,
  88. _col$scopedSlots = col.scopedSlots,
  89. scopedSlots = _col$scopedSlots === undefined ? {} : _col$scopedSlots,
  90. restProps = (0, _objectWithoutProperties3['default'])(col, ['slots', 'scopedSlots']);
  91. var column = (0, _extends3['default'])({}, restProps);
  92. Object.keys(slots).forEach(function (key) {
  93. var name = slots[key];
  94. if (column[key] === undefined && $slots[name]) {
  95. column[key] = $slots[name].length === 1 ? $slots[name][0] : $slots[name];
  96. }
  97. });
  98. Object.keys(scopedSlots).forEach(function (key) {
  99. var name = scopedSlots[key];
  100. if (column[key] === undefined && $scopedSlots[name]) {
  101. column[key] = $scopedSlots[name];
  102. }
  103. });
  104. // if (slotScopeName && $scopedSlots[slotScopeName]) {
  105. // column.customRender = column.customRender || $scopedSlots[slotScopeName]
  106. // }
  107. if (col.children) {
  108. column.children = _this2.updateColumns(column.children);
  109. }
  110. columns.push(column);
  111. });
  112. return columns;
  113. }
  114. },
  115. render: function render() {
  116. var h = arguments[0];
  117. var $slots = this.$slots,
  118. normalize = this.normalize,
  119. $scopedSlots = this.$scopedSlots;
  120. var props = (0, _propsUtil.getOptionProps)(this);
  121. var columns = props.columns ? this.updateColumns(props.columns) : normalize($slots['default']);
  122. var title = props.title,
  123. footer = props.footer;
  124. var slotTitle = $scopedSlots.title,
  125. slotFooter = $scopedSlots.footer,
  126. _$scopedSlots$expande = $scopedSlots.expandedRowRender,
  127. expandedRowRender = _$scopedSlots$expande === undefined ? props.expandedRowRender : _$scopedSlots$expande,
  128. expandIcon = $scopedSlots.expandIcon;
  129. title = title || slotTitle;
  130. footer = footer || slotFooter;
  131. var tProps = {
  132. props: (0, _extends3['default'])({}, props, {
  133. columns: columns,
  134. title: title,
  135. footer: footer,
  136. expandedRowRender: expandedRowRender,
  137. expandIcon: this.$props.expandIcon || expandIcon
  138. }),
  139. on: (0, _propsUtil.getListeners)(this)
  140. };
  141. return h(_Table2['default'], tProps);
  142. }
  143. };
  144. /* istanbul ignore next */
  145. Table.install = function (Vue) {
  146. Vue.use(_base2['default']);
  147. Vue.component(Table.name, Table);
  148. Vue.component(Table.Column.name, Table.Column);
  149. Vue.component(Table.ColumnGroup.name, Table.ColumnGroup);
  150. };
  151. exports['default'] = Table;