2018-06-28 12:43:59 +00:00
|
|
|
import ngModule from '../../module';
|
2021-01-12 06:45:13 +00:00
|
|
|
import './style.scss';
|
2018-06-28 12:43:59 +00:00
|
|
|
|
2020-11-20 14:05:57 +00:00
|
|
|
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];
|
2019-02-10 21:52:35 +00:00
|
|
|
$element.on('click', () => this.onToggleOrder());
|
2018-06-28 12:43:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 21:52:35 +00:00
|
|
|
/**
|
2019-04-05 13:20:12 +00:00
|
|
|
* Changes the order if the cell has a field and defaultOrder property
|
|
|
|
*/
|
2018-06-28 12:43:59 +00:00
|
|
|
$onInit() {
|
|
|
|
if (!this.field) return;
|
|
|
|
|
2021-05-12 10:11:27 +00:00
|
|
|
if (this.defaultOrder) {
|
2018-06-28 12:43:59 +00:00
|
|
|
this.order = this.defaultOrder;
|
2021-05-12 10:11:27 +00:00
|
|
|
this.table.applyOrder(this.field, this.order);
|
|
|
|
this.updateArrow();
|
|
|
|
}
|
2018-06-28 12:43:59 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:52:35 +00:00
|
|
|
/**
|
2019-04-05 13:20:12 +00:00
|
|
|
* Toggle order ASC/DESC
|
|
|
|
*/
|
2018-06-28 12:43:59 +00:00
|
|
|
toggleOrder() {
|
2019-04-05 13:20:12 +00:00
|
|
|
if (this.order === 'ASC')
|
2018-06-28 12:43:59 +00:00
|
|
|
this.order = 'DESC';
|
2019-04-05 13:20:12 +00:00
|
|
|
else
|
2018-06-28 12:43:59 +00:00
|
|
|
this.order = 'ASC';
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:52:35 +00:00
|
|
|
/**
|
2019-04-05 13:20:12 +00:00
|
|
|
* Applies a new filter order to the model and
|
|
|
|
* updates the cell arrow
|
|
|
|
*/
|
2018-06-28 12:43:59 +00:00
|
|
|
onToggleOrder() {
|
|
|
|
if (!this.field) return;
|
|
|
|
|
2019-04-05 13:20:12 +00:00
|
|
|
if (this.table.field == this.field)
|
2018-06-29 06:41:53 +00:00
|
|
|
this.toggleOrder();
|
2019-04-05 13:20:12 +00:00
|
|
|
else
|
2018-06-29 06:41:53 +00:00
|
|
|
this.table.setOrder(this.field, this.order);
|
2019-04-05 13:20:12 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-10 21:52:35 +00:00
|
|
|
/**
|
2019-04-05 13:20:12 +00:00
|
|
|
* Set cell class to asc/desc
|
|
|
|
*/
|
2018-06-28 12:43:59 +00:00
|
|
|
updateArrow() {
|
|
|
|
this.column.classList.remove('asc', 'desc');
|
|
|
|
|
2019-04-05 13:20:12 +00:00
|
|
|
if (this.order === 'DESC')
|
2018-06-28 12:43:59 +00:00
|
|
|
this.column.classList.add('desc');
|
2019-04-05 13:20:12 +00:00
|
|
|
else
|
2018-06-28 12:43:59 +00:00
|
|
|
this.column.classList.add('asc');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:05:57 +00:00
|
|
|
Th.$inject = ['$element'];
|
2018-06-28 12:43:59 +00:00
|
|
|
|
2020-07-24 15:04:34 +00:00
|
|
|
ngModule.vnComponent('vnTh', {
|
2020-11-20 14:05:57 +00:00
|
|
|
template: require('./index.html'),
|
2018-06-28 12:43:59 +00:00
|
|
|
transclude: true,
|
|
|
|
controller: Th,
|
|
|
|
bindings: {
|
|
|
|
defaultOrder: '@?'
|
|
|
|
},
|
|
|
|
require: {
|
|
|
|
table: '^^vnTable'
|
|
|
|
}
|
|
|
|
});
|