2018-06-28 12:43:59 +00:00
|
|
|
import ngModule from '../../module';
|
|
|
|
|
|
|
|
export default class Th {
|
|
|
|
constructor($element) {
|
2018-06-29 06:41:53 +00:00
|
|
|
this._order = 'ASC';
|
2018-06-28 12:43:59 +00:00
|
|
|
this.column = $element[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the order if the cell has a field and defaultOrder property
|
|
|
|
*/
|
|
|
|
$onInit() {
|
|
|
|
if (!this.field) return;
|
|
|
|
|
|
|
|
if (this.defaultOrder)
|
|
|
|
this.order = this.defaultOrder;
|
|
|
|
|
|
|
|
this.updateArrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
set order(order) {
|
|
|
|
this._order = order;
|
|
|
|
this.table.setOrder(this.field, order);
|
|
|
|
}
|
|
|
|
|
|
|
|
get order() {
|
|
|
|
return this._order;
|
|
|
|
}
|
|
|
|
|
|
|
|
get field() {
|
|
|
|
return this.column.getAttribute('field');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle order ASC/DESC
|
|
|
|
*/
|
|
|
|
toggleOrder() {
|
|
|
|
if (this.order === 'ASC') {
|
|
|
|
this.order = 'DESC';
|
|
|
|
} else {
|
|
|
|
this.order = 'ASC';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies a new filter order to the model and
|
|
|
|
* updates the cell arrow
|
|
|
|
*/
|
|
|
|
onToggleOrder() {
|
|
|
|
if (!this.field) return;
|
|
|
|
|
2018-06-29 06:41:53 +00:00
|
|
|
if (this.table.field == this.field) {
|
|
|
|
this.toggleOrder();
|
|
|
|
} else {
|
|
|
|
this.table.setOrder(this.field, this.order);
|
|
|
|
}
|
|
|
|
|
2018-06-28 12:43:59 +00:00
|
|
|
this.updateArrow();
|
|
|
|
|
2018-07-16 06:00:04 +00:00
|
|
|
this.table.applyOrder(this.field, this.order);
|
2018-06-28 12:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set cell class to asc/desc
|
|
|
|
*/
|
|
|
|
updateArrow() {
|
|
|
|
this.column.classList.remove('asc', 'desc');
|
|
|
|
|
|
|
|
if (this.order === 'DESC') {
|
|
|
|
this.column.classList.add('desc');
|
|
|
|
} else {
|
|
|
|
this.column.classList.add('asc');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Th.$inject = ['$element'];
|
|
|
|
|
|
|
|
ngModule.component('vnTh', {
|
|
|
|
template: require('./index.html'),
|
|
|
|
transclude: true,
|
|
|
|
controller: Th,
|
|
|
|
bindings: {
|
|
|
|
defaultOrder: '@?'
|
|
|
|
},
|
|
|
|
require: {
|
|
|
|
table: '^^vnTable'
|
|
|
|
}
|
|
|
|
});
|