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

237 lines
6.7 KiB
JavaScript
Raw Normal View History

2016-10-13 03:12:24 +00:00
import ElCheckbox from 'element-ui/packages/checkbox/index.js';
import ElTag from 'element-ui/packages/tag/index.js';
2016-08-16 08:07:18 +00:00
export default {
name: 'el-table-header',
render(h) {
return (
<table
class="el-table__header"
cellspacing="0"
cellpadding="0"
border="0">
{
this._l(this.columns, column =>
<colgroup
name={ column.id }
width={ column.realWidth || column.width }
/>)
}
{
!this.fixed && this.layout.gutterWidth
? <colgroup name="gutter" width={ this.layout.scrollY ? this.layout.gutterWidth : '' }></colgroup>
: ''
}
<thead>
<tr>
{
this._l(this.columns, column =>
<th
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) }
class={ [column.id, column.direction, column.align] }>
2016-08-16 08:07:18 +00:00
{
[
column.headerTemplate
? column.headerTemplate.call(this._renderProxy, h, column.label)
: <div>{ column.label }</div>,
column.sortable
? <div class="caret-wrapper">
<i class="sort-caret ascending"></i>
<i class="sort-caret descending"></i>
</div>
: ''
]
2016-08-16 08:07:18 +00:00
}
</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: {
columns: {},
fixed: String,
store: {
required: true
},
layout: {
required: true
2016-08-16 08:07:18 +00:00
},
border: Boolean
},
components: {
ElCheckbox,
ElTag
},
computed: {
isAllSelected() {
return this.store.states.isAllSelected;
},
columns() {
if (this.fixed === true || this.fixed === 'left') {
return this.store.states.fixedColumns;
} else if (this.fixed === 'right') {
return this.store.states.rightFixedColumns;
}
return this.store.states.columns;
}
},
2016-08-16 08:07:18 +00:00
methods: {
toggleAllSelection() {
this.store.commit('toggleAllSelection');
2016-08-16 08:07:18 +00:00
},
handleMouseDown(event, column) {
if (this.draggingColumn && this.border) {
this.dragging = true;
this.$parent.resizeProxyVisible = true;
const tableEl = this.$parent.$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
columnEl.classList.add('noclick');
this.dragState = {
startMouseLeft: event.clientX,
startLeft: columnRect.right - tableLeft,
startColumnLeft: columnRect.left - tableLeft,
tableLeft
2016-08-16 08:07:18 +00:00
};
const resizeProxy = this.$parent.$refs.resizeProxy;
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) {
const finalLeft = parseInt(resizeProxy.style.left, 10);
const columnWidth = finalLeft - this.dragState.startColumnLeft;
column.width = column.realWidth = columnWidth;
this.store.scheduleLayout();
2016-08-16 08:07:18 +00:00
document.body.style.cursor = '';
this.dragging = false;
this.draggingColumn = null;
this.dragState = {};
this.$parent.resizeProxyVisible = false;
}
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
2016-08-16 08:07:18 +00:00
document.onselectstart = null;
document.ondragstart = null;
setTimeout(function() {
columnEl.classList.remove('noclick');
}, 0);
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
2016-08-16 08:07:18 +00:00
}
},
handleMouseMove(event, column) {
const target = event.target;
if (!column || !column.resizable) return;
if (!this.dragging && this.border) {
let rect = target.getBoundingClientRect();
var 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;
if (column.sortable) bodyStyle.cursor = 'pointer';
2016-08-16 08:07:18 +00:00
}
}
},
handleMouseOut() {
document.body.style.cursor = '';
},
handleHeaderClick(event, column) {
let target = event.target;
while (target && target.tagName !== 'TH') {
target = target.parentNode;
}
if (target && target.tagName === 'TH') {
if (target.classList.contains('noclick')) {
target.classList.remove('noclick');
return;
}
}
if (!column.sortable) return;
const sortCondition = this.store.states.sortCondition;
2016-08-16 08:07:18 +00:00
if (sortCondition.column !== column) {
if (sortCondition.column) {
sortCondition.column.direction = '';
2016-08-16 08:07:18 +00:00
}
sortCondition.column = column;
sortCondition.property = column.property;
2016-08-16 08:07:18 +00:00
}
if (!column.direction) {
column.direction = 'ascending';
} else if (column.direction === 'ascending') {
column.direction = 'descending';
} else {
column.direction = '';
sortCondition.column = null;
sortCondition.property = null;
2016-08-16 08:07:18 +00:00
}
sortCondition.direction = column.direction === 'descending' ? -1 : 1;
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
};
}
};