element/packages/table/src/table-header.js

443 lines
13 KiB
JavaScript
Raw Normal View History

2017-06-29 06:43:28 +00:00
import { hasClass, addClass, removeClass } from 'element-ui/src/utils/dom';
2016-10-25 13:35:41 +00:00
import ElCheckbox from 'element-ui/packages/checkbox';
import ElTag from 'element-ui/packages/tag';
2016-10-27 13:45:21 +00:00
import Vue from 'vue';
import FilterPanel from './filter-panel.vue';
2016-08-16 08:07:18 +00:00
const getAllColumns = (columns) => {
const result = [];
columns.forEach((column) => {
if (column.children) {
result.push(column);
result.push.apply(result, getAllColumns(column.children));
} else {
result.push(column);
}
});
return result;
};
const convertToRows = (originColumns) => {
let maxLevel = 1;
const traverse = (column, parent) => {
if (parent) {
column.level = parent.level + 1;
if (maxLevel < column.level) {
maxLevel = column.level;
}
}
if (column.children) {
let colSpan = 0;
column.children.forEach((subColumn) => {
traverse(subColumn, column);
colSpan += subColumn.colSpan;
});
column.colSpan = colSpan;
} else {
column.colSpan = 1;
}
};
originColumns.forEach((column) => {
column.level = 1;
traverse(column);
});
const rows = [];
for (let i = 0; i < maxLevel; i++) {
rows.push([]);
}
const allColumns = getAllColumns(originColumns);
allColumns.forEach((column) => {
if (!column.children) {
column.rowSpan = maxLevel - column.level + 1;
} else {
column.rowSpan = 1;
}
rows[column.level - 1].push(column);
});
return rows;
};
2016-08-16 08:07:18 +00:00
export default {
name: 'ElTableHeader',
2016-08-16 08:07:18 +00:00
render(h) {
const originColumns = this.store.states.originColumns;
const columnRows = convertToRows(originColumns, this.columns);
2016-08-16 08:07:18 +00:00
return (
<table
class="el-table__header"
cellspacing="0"
cellpadding="0"
border="0">
<colgroup>
{
this._l(this.columns, column =>
<col
name={ column.id }
width={ column.realWidth || column.width }
/>)
}
{
!this.fixed && this.layout.gutterWidth
? <col name="gutter" width={ this.layout.scrollY ? this.layout.gutterWidth : '' }></col>
: ''
}
</colgroup>
<thead>
{
this._l(columnRows, (columns, rowIndex) =>
<tr>
2017-01-03 08:27:42 +00:00
{
this._l(columns, (column, cellIndex) =>
<th
colspan={ column.colSpan }
rowspan={ column.rowSpan }
on-mousemove={ ($event) => this.handleMouseMove($event, column) }
on-mouseout={ this.handleMouseOut }
on-mousedown={ ($event) => this.handleMouseDown($event, column) }
on-click={ ($event) => this.handleHeaderClick($event, column) }
2017-09-06 02:16:34 +00:00
class={ [column.id, column.order, column.headerAlign, column.className || '', rowIndex === 0 && this.isCellHidden(cellIndex, columns) ? 'is-hidden' : '', !column.children ? 'is-leaf' : '', column.labelClassName, column.sortable ? 'is-sortable' : ''] }>
2017-03-08 11:14:36 +00:00
<div class={ ['cell', column.filteredValue && column.filteredValue.length > 0 ? 'highlight' : '', column.labelClassName] }>
2017-01-03 08:27:42 +00:00
{
column.renderHeader
? column.renderHeader.call(this._renderProxy, h, { column, $index: cellIndex, store: this.store, _self: this.$parent.$vnode.context })
: column.label
}
{
column.sortable
? <span class="caret-wrapper" on-click={ ($event) => this.handleSortClick($event, column) }>
<i class="sort-caret ascending" on-click={ ($event) => this.handleSortClick($event, column, 'ascending') }></i>
<i class="sort-caret descending" on-click={ ($event) => this.handleSortClick($event, column, 'descending') }></i>
2017-01-03 08:27:42 +00:00
</span>
: ''
}
{
column.filterable
? <span class="el-table__column-filter-trigger" on-click={ ($event) => this.handleFilterClick($event, column) }><i class={ ['el-icon-arrow-down', column.filterOpened ? 'el-icon-arrow-up' : ''] }></i></span>
: ''
}
</div>
</th>
)
}
{
!this.fixed && this.layout.gutterWidth
? <th class="gutter" style={{ width: this.layout.scrollY ? this.layout.gutterWidth + 'px' : '0' }}></th>
: ''
}
</tr>
)
}
</thead>
2016-08-16 08:07:18 +00:00
</table>
);
},
props: {
fixed: String,
store: {
required: true
},
layout: {
required: true
2016-08-16 08:07:18 +00:00
},
2017-01-03 12:08:43 +00:00
border: Boolean,
2017-01-14 06:51:44 +00:00
defaultSort: {
type: Object,
default() {
return {
prop: '',
order: ''
};
}
}
2016-08-16 08:07:18 +00:00
},
components: {
ElCheckbox,
ElTag
},
computed: {
isAllSelected() {
return this.store.states.isAllSelected;
},
columnsCount() {
return this.store.states.columns.length;
},
leftFixedCount() {
return this.store.states.fixedColumns.length;
},
rightFixedCount() {
return this.store.states.rightFixedColumns.length;
},
columns() {
return this.store.states.columns;
}
},
2016-10-27 13:45:21 +00:00
created() {
this.filterPanels = {};
},
2017-01-03 12:08:43 +00:00
mounted() {
2017-01-14 06:51:44 +00:00
if (this.defaultSort.prop) {
const states = this.store.states;
2017-01-14 06:51:44 +00:00
states.sortProp = this.defaultSort.prop;
states.sortOrder = this.defaultSort.order || 'ascending';
this.$nextTick(_ => {
for (let i = 0, length = this.columns.length; i < length; i++) {
2017-01-11 01:48:09 +00:00
let column = this.columns[i];
2017-01-14 06:51:44 +00:00
if (column.property === states.sortProp) {
column.order = states.sortOrder;
2017-01-11 01:48:09 +00:00
states.sortingColumn = column;
break;
}
2017-01-03 12:08:43 +00:00
}
2017-01-11 01:48:09 +00:00
if (states.sortingColumn) {
this.store.commit('changeSortCondition');
}
});
}
2017-01-03 12:08:43 +00:00
},
2016-10-27 13:45:21 +00:00
beforeDestroy() {
const panels = this.filterPanels;
for (let prop in panels) {
if (panels.hasOwnProperty(prop) && panels[prop]) {
panels[prop].$destroy(true);
}
}
},
2016-08-16 08:07:18 +00:00
methods: {
isCellHidden(index, columns) {
if (this.fixed === true || this.fixed === 'left') {
return index >= this.leftFixedCount;
} else if (this.fixed === 'right') {
let before = 0;
for (let i = 0; i < index; i++) {
before += columns[i].colSpan;
}
return before < this.columnsCount - this.rightFixedCount;
} else {
return (index < this.leftFixedCount) || (index >= this.columnsCount - this.rightFixedCount);
}
},
toggleAllSelection() {
this.store.commit('toggleAllSelection');
2016-08-16 08:07:18 +00:00
},
2016-10-27 13:45:21 +00:00
handleFilterClick(event, column) {
event.stopPropagation();
const target = event.target;
const cell = target.parentNode;
const table = this.$parent;
let filterPanel = this.filterPanels[column.id];
if (filterPanel && column.filterOpened) {
filterPanel.showPopper = false;
return;
}
if (!filterPanel) {
filterPanel = new Vue(FilterPanel);
this.filterPanels[column.id] = filterPanel;
if (column.filterPlacement) {
filterPanel.placement = column.filterPlacement;
}
2016-10-27 13:45:21 +00:00
filterPanel.table = table;
filterPanel.cell = cell;
filterPanel.column = column;
2016-12-26 02:45:20 +00:00
!this.$isServer && filterPanel.$mount(document.createElement('div'));
2016-10-27 13:45:21 +00:00
}
setTimeout(() => {
filterPanel.showPopper = true;
}, 16);
},
handleHeaderClick(event, column) {
if (!column.filters && column.sortable) {
this.handleSortClick(event, column);
} else if (column.filters && !column.sortable) {
this.handleFilterClick(event, column);
}
this.$parent.$emit('header-click', column, event);
},
2016-08-16 08:07:18 +00:00
handleMouseDown(event, column) {
2016-12-26 02:45:20 +00:00
if (this.$isServer) return;
if (column.children && column.children.length > 0) return;
2016-10-27 15:25:03 +00:00
/* istanbul ignore if */
2016-08-16 08:07:18 +00:00
if (this.draggingColumn && this.border) {
this.dragging = true;
this.$parent.resizeProxyVisible = true;
2017-02-23 05:09:25 +00:00
const table = this.$parent;
const tableEl = table.$el;
const tableLeft = tableEl.getBoundingClientRect().left;
2016-08-16 08:07:18 +00:00
const columnEl = this.$el.querySelector(`th.${column.id}`);
const columnRect = columnEl.getBoundingClientRect();
const minLeft = columnRect.left - tableLeft + 30;
2016-08-16 08:07:18 +00:00
2017-06-29 06:43:28 +00:00
addClass(columnEl, 'noclick');
2016-08-16 08:07:18 +00:00
this.dragState = {
startMouseLeft: event.clientX,
startLeft: columnRect.right - tableLeft,
startColumnLeft: columnRect.left - tableLeft,
tableLeft
2016-08-16 08:07:18 +00:00
};
2017-02-23 05:09:25 +00:00
const resizeProxy = table.$refs.resizeProxy;
2016-08-16 08:07:18 +00:00
resizeProxy.style.left = this.dragState.startLeft + 'px';
document.onselectstart = function() { return false; };
document.ondragstart = function() { return false; };
const handleMouseMove = (event) => {
2016-08-16 08:07:18 +00:00
const deltaLeft = event.clientX - this.dragState.startMouseLeft;
const proxyLeft = this.dragState.startLeft + deltaLeft;
resizeProxy.style.left = Math.max(minLeft, proxyLeft) + 'px';
};
const handleMouseUp = () => {
2016-08-16 08:07:18 +00:00
if (this.dragging) {
2017-02-23 05:09:25 +00:00
const {
startColumnLeft,
startLeft
} = this.dragState;
2016-08-16 08:07:18 +00:00
const finalLeft = parseInt(resizeProxy.style.left, 10);
2017-02-23 05:09:25 +00:00
const columnWidth = finalLeft - startColumnLeft;
2016-08-16 08:07:18 +00:00
column.width = column.realWidth = columnWidth;
2017-02-23 05:09:25 +00:00
table.$emit('header-dragend', column.width, startLeft - startColumnLeft, column, event);
2016-08-16 08:07:18 +00:00
this.store.scheduleLayout();
2016-08-16 08:07:18 +00:00
document.body.style.cursor = '';
this.dragging = false;
this.draggingColumn = null;
this.dragState = {};
2017-02-23 05:09:25 +00:00
table.resizeProxyVisible = false;
2016-08-16 08:07:18 +00:00
}
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
2016-08-16 08:07:18 +00:00
document.onselectstart = null;
document.ondragstart = null;
setTimeout(function() {
2017-06-29 06:43:28 +00:00
removeClass(columnEl, 'noclick');
2016-08-16 08:07:18 +00:00
}, 0);
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
2016-08-16 08:07:18 +00:00
}
},
handleMouseMove(event, column) {
if (column.children && column.children.length > 0) return;
2016-10-27 13:45:21 +00:00
let target = event.target;
while (target && target.tagName !== 'TH') {
target = target.parentNode;
}
2016-08-16 08:07:18 +00:00
if (!column || !column.resizable) return;
if (!this.dragging && this.border) {
let rect = target.getBoundingClientRect();
const bodyStyle = document.body.style;
2016-08-16 08:07:18 +00:00
if (rect.width > 12 && rect.right - event.pageX < 8) {
bodyStyle.cursor = 'col-resize';
2016-08-16 08:07:18 +00:00
this.draggingColumn = column;
} else if (!this.dragging) {
bodyStyle.cursor = '';
2016-08-16 08:07:18 +00:00
this.draggingColumn = null;
}
}
},
handleMouseOut() {
2016-12-26 02:45:20 +00:00
if (this.$isServer) return;
2016-08-16 08:07:18 +00:00
document.body.style.cursor = '';
},
toggleOrder(order) {
return !order ? 'ascending' : order === 'ascending' ? 'descending' : null;
2017-01-03 12:08:43 +00:00
},
handleSortClick(event, column, givenOrder) {
event.stopPropagation();
let order = givenOrder || this.toggleOrder(column.order);
2017-01-03 12:08:43 +00:00
2016-08-16 08:07:18 +00:00
let target = event.target;
while (target && target.tagName !== 'TH') {
target = target.parentNode;
}
if (target && target.tagName === 'TH') {
2017-06-29 06:43:28 +00:00
if (hasClass(target, 'noclick')) {
removeClass(target, 'noclick');
2016-08-16 08:07:18 +00:00
return;
}
}
if (!column.sortable) return;
2016-10-27 17:24:13 +00:00
const states = this.store.states;
let sortProp = states.sortProp;
let sortOrder;
const sortingColumn = states.sortingColumn;
2016-08-16 08:07:18 +00:00
2016-10-27 17:24:13 +00:00
if (sortingColumn !== column) {
if (sortingColumn) {
sortingColumn.order = null;
2016-08-16 08:07:18 +00:00
}
2016-10-27 17:24:13 +00:00
states.sortingColumn = column;
sortProp = column.property;
2016-08-16 08:07:18 +00:00
}
if (!order) {
2016-10-27 17:24:13 +00:00
sortOrder = column.order = null;
states.sortingColumn = null;
sortProp = null;
2016-12-23 04:11:17 +00:00
} else {
sortOrder = column.order = order;
2016-08-16 08:07:18 +00:00
}
2016-12-23 04:11:17 +00:00
2016-10-27 17:24:13 +00:00
states.sortProp = sortProp;
states.sortOrder = sortOrder;
2016-08-16 08:07:18 +00:00
this.store.commit('changeSortCondition');
2016-08-16 08:07:18 +00:00
}
},
data() {
return {
draggingColumn: null,
dragging: false,
dragState: {}
2016-08-16 08:07:18 +00:00
};
}
};