@uex/web-extensions
Version:
Uex extensions for Angular 6+ web projects
1,454 lines • 127 kB
JavaScript
/**
* @fileoverview added by tsickle
* Generated from: lib/modules/table/classes/table.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
import { EventEmitter } from "@angular/core";
import * as _ from "lodash";
import { TableRemote } from "./remote";
import { TableColumn } from "./column";
import { TableGroup } from "./group";
import { TableRow } from "./row";
export class Table {
/**
* @param {?} _tableSettings
* @param {?} _orderBy
* @param {?} _filterBy
*/
constructor(_tableSettings, _orderBy, _filterBy) {
this._tableSettings = _tableSettings;
this._orderBy = _orderBy;
this._filterBy = _filterBy;
this._columns = [];
this._totals = [];
this._is_grouped = false;
this._all_selected_icon = "check_box_outline_blank";
this.onUpdateStart = new EventEmitter();
this.onUpdateError = new EventEmitter();
this.onUpdateSuccess = new EventEmitter();
this.onUpdateEnd = new EventEmitter();
this.onCacheLoaded = new EventEmitter();
this.onDataChange = new EventEmitter();
this.onGroupChange = new EventEmitter();
this.onTotalsChange = new EventEmitter();
this.onPaginationChange = new EventEmitter();
this.onFilterChange = new EventEmitter();
this.onSortChange = new EventEmitter();
this.onSelectionChange = new EventEmitter();
this.onRowClick = new EventEmitter();
this.loading = true;
this.data = [];
this.setColumns(this._tableSettings.columns, true);
if (!this._tableSettings.sort) {
this.setSort(null, true);
}
if (!this._tableSettings.pagination) {
this.setPagination(null, true);
}
if (_.isBoolean(this._tableSettings.loading)) {
this.loading = this._tableSettings.loading;
}
this._remote = new TableRemote(this._tableSettings, this);
this._loadCache();
if (!this._remote.valid ||
(this._remote.valid && this._remote.initial_loading)) {
this.update();
}
}
/**
* @return {?}
*/
get id() {
return this._tableSettings.id;
}
/**
* @return {?}
*/
get cache() {
return this._tableSettings.cache;
}
/**
* @return {?}
*/
get remote() {
return this._remote;
}
/**
* @return {?}
*/
get columns() {
return this._columns;
}
/**
* @return {?}
*/
get raw_data() {
return this._tableSettings.data;
}
/**
* @param {?} raw_data
* @return {?}
*/
set raw_data(raw_data) {
this._tableSettings.data = raw_data;
}
/**
* @return {?}
*/
get empty_text() {
return this._tableSettings.empty_text
? this._tableSettings.empty_text
: "Nenhum registro encontrado.";
}
/**
* @param {?} empty_text
* @return {?}
*/
set empty_text(empty_text) {
this._tableSettings.empty_text = empty_text;
}
/**
* @return {?}
*/
get action_text() {
return this._tableSettings.action_text
? this._tableSettings.action_text
: "Ações";
}
/**
* @param {?} action_text
* @return {?}
*/
set action_text(action_text) {
this._tableSettings.action_text = action_text;
}
/**
* @return {?}
*/
get selectable() {
if (_.isBoolean(this._tableSettings.selectable) ||
_.isFunction(this._tableSettings.selectable)) {
return this._tableSettings.selectable;
}
return false;
}
/**
* @param {?} selectable
* @return {?}
*/
set selectable(selectable) {
this._tableSettings.selectable = selectable;
}
/**
* @return {?}
*/
get clickable() {
if (_.isBoolean(this._tableSettings.clickable) ||
_.isFunction(this._tableSettings.clickable)) {
return this._tableSettings.clickable;
}
return false;
}
/**
* @param {?} clickable
* @return {?}
*/
set clickable(clickable) {
this._tableSettings.clickable = clickable;
}
/**
* @return {?}
*/
get row_class() {
return this._tableSettings.row_class;
}
/**
* @return {?}
*/
get row_style() {
return this._tableSettings.row_style;
}
/**
* @return {?}
*/
get totals() {
return this._totals;
}
/**
* @return {?}
*/
get group() {
return this._tableSettings.group;
}
/**
* @return {?}
*/
get is_grouped() {
return this._is_grouped;
}
/**
* @return {?}
*/
get filter() {
if (this._tableSettings.filter) {
return this._tableSettings.filter;
}
return {
value: null,
accessor: []
};
}
/**
* @return {?}
*/
get sort() {
/** @type {?} */
const sort = this._tableSettings.sort;
if (sort && sort.accessor && sort.direction && sort.icon) {
return sort;
}
else {
this.setSort(sort, true);
return sort;
}
}
/**
* @return {?}
*/
get pagination() {
if (this._tableSettings.pagination) {
return _.merge({}, this._getDefaultPagination(), this._tableSettings.pagination);
}
return this._getDefaultPagination();
}
/**
* @return {?}
*/
get all_selected_icon() {
return this._all_selected_icon;
}
/**
* @return {?}
*/
getCacheData() {
return this._cache;
}
/**
* @param {?} remote
* @param {?=} skip_update
* @return {?}
*/
setRemote(remote, skip_update = false) {
if (!_.isEqual(remote, this._tableSettings.remote)) {
this._tableSettings.remote = remote;
this._remote = new TableRemote(this._tableSettings, this);
if (!skip_update) {
this.update();
}
return true;
}
return false;
}
/**
* @param {?} columns
* @param {?=} skip_update
* @return {?}
*/
setColumns(columns, skip_update = false) {
/** @type {?} */
const new_columns = columns
.filter((/**
* @param {?} columns
* @return {?}
*/
columns => !!columns))
.map((/**
* @param {?} column
* @return {?}
*/
column => new TableColumn(column, this)));
if (this._tableSettings.selectable) {
new_columns.unshift(new TableColumn({
selectable: true,
header: {
label: "Selecionar todos"
}
}, this));
}
if (_.isEqual(new_columns, this._columns))
return false;
this._columns = new_columns;
if (!skip_update)
this.update();
return true;
}
/**
* @param {?} raw_data
* @param {?=} skip_update
* @return {?}
*/
setData(raw_data, skip_update = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!_.isEqual(raw_data, this.raw_data)) {
this.raw_data = _.cloneDeep(raw_data);
this.loading = false;
if (!skip_update) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
}));
return;
}
}
resolve(this);
}));
}
/**
* @param {?} totals
* @param {?=} skip_update
* @return {?}
*/
setTotals(totals, skip_update = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!_.isEqual(totals, this._tableSettings.totals)) {
this._tableSettings.totals = totals;
if (!skip_update) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onTotalsChange.emit(this._tableSettings.totals);
}));
return;
}
this.onTotalsChange.emit(this._tableSettings.totals);
}
resolve(this);
}));
}
/**
* @param {?} group
* @param {?=} skip_update
* @return {?}
*/
setGroup(group, skip_update = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!_.isEqual(group, this._tableSettings.group)) {
this._tableSettings.group = group;
if (!skip_update) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onGroupChange.emit(this._tableSettings.group);
}));
return;
}
this.onGroupChange.emit(this._tableSettings.group);
}
resolve(this);
}));
}
/**
* @param {?} filter
* @param {?=} skip_update
* @return {?}
*/
setFilter(filter, skip_update = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!_.isEqual(filter, this._tableSettings.filter)) {
this._tableSettings.filter = filter;
if (!this._tableSettings.pagination.disabled) {
this._tableSettings.pagination.number = 1;
}
if (!skip_update) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onFilterChange.emit(this.filter);
}));
return;
}
this.onFilterChange.emit(this.filter);
}
resolve(this);
}));
}
/**
* @param {?} sort
* @param {?=} skip_update_or_event
* @return {?}
*/
setSort(sort, skip_update_or_event = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
/** @type {?} */
const default_sort = this._getDefaultSort();
/** @type {?} */
const new_sort = _.merge({}, default_sort, this._tableSettings.sort, sort);
if (!_.isEqual(new_sort, this._tableSettings.sort)) {
this._tableSettings.sort = {
manual: new_sort.manual,
accessor: new_sort.accessor,
direction: new_sort.direction,
icon: new_sort.icon
? new_sort.icon
: this._getSortDirectionIcon(new_sort)
};
if (!skip_update_or_event && !this._tableSettings.sort.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onSortChange.emit(this.sort);
}));
return;
}
if (!this._tableSettings.sort.manual ||
(this._tableSettings.sort.manual && !skip_update_or_event)) {
this.onSortChange.emit(this.sort);
}
}
resolve(this);
}));
}
/**
* @param {?} page
* @param {?=} skip_update_or_event
* @return {?}
*/
setPagination(page, skip_update_or_event = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
/** @type {?} */
const new_page = _.merge({}, this.pagination, page);
if (!_.isEqual(new_page, this._tableSettings.pagination)) {
this._tableSettings.pagination = new_page;
if (!skip_update_or_event && !this._tableSettings.pagination.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
return;
}
if (!this._tableSettings.pagination.manual ||
(this._tableSettings.pagination.manual && !skip_update_or_event)) {
this.onPaginationChange.emit(this.pagination);
}
}
resolve(this);
}));
}
/**
* @param {?} size
* @param {?=} skip_update_or_event
* @return {?}
*/
setPageSize(size, skip_update_or_event = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (size != this.pagination.size) {
this._tableSettings.pagination.size = size;
if (!skip_update_or_event && !this._tableSettings.pagination.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
return;
}
if (!this._tableSettings.pagination.manual ||
(this._tableSettings.pagination.manual && !skip_update_or_event)) {
this.onPaginationChange.emit(this.pagination);
}
}
resolve(this);
}));
}
/**
* @param {?} number
* @param {?=} skip_update_or_event
* @return {?}
*/
setPageNumber(number, skip_update_or_event = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (number != this.pagination.number) {
this._tableSettings.pagination.number = number;
if (!skip_update_or_event && !this._tableSettings.pagination.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
return;
}
if (!this._tableSettings.pagination.manual ||
(this._tableSettings.pagination.manual && !skip_update_or_event)) {
this.onPaginationChange.emit(this.pagination);
}
}
resolve(this);
}));
}
/**
* @param {?} row_data
* @return {?}
*/
getRowSelectable(row_data) {
if (_.isBoolean(this.selectable)) {
return (/** @type {?} */ (this.selectable));
}
else {
/** @type {?} */
const event = {
table_row: row_data,
data: row_data.getData()
};
return ((/** @type {?} */ (this.selectable)))(event)
? true
: false;
}
}
/**
* @param {?} row_data
* @return {?}
*/
getRowClickable(row_data) {
if (_.isBoolean(this.clickable)) {
return (/** @type {?} */ (this.clickable));
}
else {
/** @type {?} */
const event = {
table_row: row_data,
data: row_data.getData()
};
return ((/** @type {?} */ (this.clickable)))(event) ? true : false;
}
}
/**
* @param {?} row_data
* @return {?}
*/
getRowCssClass(row_data) {
if (_.isString(this.row_class)) {
return (/** @type {?} */ (this.row_class));
}
else if (_.isFunction(this.row_class)) {
/** @type {?} */
const event = {
table_row: row_data,
data: row_data.getData()
};
return ((/** @type {?} */ (this.row_class)))(event) || null;
}
return null;
}
/**
* @param {?} row_data
* @return {?}
*/
getRowStyle(row_data) {
if (_.isPlainObject(this.row_style)) {
return (/** @type {?} */ (this.row_style));
}
else if (_.isFunction(this.row_style)) {
/** @type {?} */
const event = {
table_row: row_data,
data: row_data.getData()
};
/** @type {?} */
const response = ((/** @type {?} */ (this.row_style)))(event);
return _.isPlainObject(response) ? response : {};
}
return {};
}
/**
* @return {?}
*/
update() {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
this.onUpdateStart.emit();
this._saveCache();
this._setAttributes();
this.updateAllSelectedIcon();
if (this.remote && this.remote.valid) {
this._updateRemote((/**
* @return {?}
*/
() => resolve(this)));
}
else {
this._updateLocal((/**
* @return {?}
*/
() => resolve(this)));
}
}));
}
/**
* @param {?} column
* @param {?=} skip_update
* @return {?}
*/
updateSort(column, skip_update = false) {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!column.sortable) {
resolve(this);
return;
}
/** @type {?} */
const new_sort = this._tableSettings.sort
? _.cloneDeep(this._tableSettings.sort)
: this._getDefaultSort();
if (column.accessor === this.sort.accessor) {
new_sort.direction = new_sort.direction === "asc" ? "desc" : "asc";
}
else {
new_sort.accessor = column.accessor;
}
new_sort.icon = this._getSortDirectionIcon(new_sort);
if (!_.isEqual(this._tableSettings.sort, new_sort)) {
this._tableSettings.sort = new_sort;
if (this._tableSettings.sort.manual) {
this.loading = true;
}
if (!skip_update && !this._tableSettings.sort.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onSortChange.emit(this._tableSettings.sort);
}));
return;
}
}
resolve(this);
}));
}
/**
* @return {?}
*/
updateTotals() {
this._totals = [];
/** @type {?} */
const totals = !_.isEmpty(this._tableSettings.totals)
? this._tableSettings.totals
: [];
if (totals.length == 0)
return;
/** @type {?} */
let colspan = 1;
/** @type {?} */
let total_cols = 0;
this._totals = this.columns
.map((/**
* @param {?} column
* @return {?}
*/
(column) => {
/** @type {?} */
const total_column = totals.filter((/**
* @param {?} total_column
* @return {?}
*/
total_column => total_column.accessor == column.accessor))[0];
if (total_column) {
/** @type {?} */
const label = total_column.format
? total_column.format(this.getRawData(), this.getData())
: "";
/** @type {?} */
const title = total_column.title
? total_column.title(this.getRawData(), this.getData())
: label.replace(/<(?:.|\n)*?>/gm, "");
/** @type {?} */
const numeric = _.isBoolean(total_column.numeric)
? total_column.numeric
: column.numeric;
if (total_column.colspan && total_column.colspan > 1) {
colspan = total_column.colspan;
}
else if (colspan > 1) {
colspan--;
}
else {
colspan = 1;
}
total_cols += colspan;
return {
label,
title,
colspan,
html: total_column.html,
numeric
};
}
else {
if (total_cols >= this._columns.length) {
return null;
}
if (colspan > 1) {
colspan--;
return null;
}
total_cols++;
return {
label: "",
title: "",
colspan: 1,
html: false,
numeric: column.numeric
};
}
}))
.filter((/**
* @param {?} total
* @return {?}
*/
total => !!total));
return this._totals;
}
/**
* @return {?}
*/
updateColumns() {
this._columns.forEach((/**
* @param {?} column
* @return {?}
*/
column => column.update()));
}
/**
* @return {?}
*/
hasColumns() {
return this.columns && this.columns && this.columns.length > 0;
}
/**
* @return {?}
*/
hasSelectableRows() {
/** @type {?} */
let data = this.getData();
if (data.length == 0) {
return false;
}
data = data.filter((/**
* @param {?} row
* @return {?}
*/
row => row.selectable));
return data.length > 0;
}
/**
* @param {?} column
* @return {?}
*/
getSortIcon(column) {
return column.accessor === this.sort.accessor ? this.sort.icon : "sort";
}
/**
* @return {?}
*/
decreasePage() {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!this.pagination.previous) {
resolve(this);
return;
}
this._tableSettings.pagination.number = this.pagination.previous;
this._tableSettings.pagination.previous =
this.pagination.previous > 1 ? this.pagination.previous - 1 : null;
this._tableSettings.pagination.next = this.pagination.next - 1;
if (!this.pagination.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
return;
}
else {
this.loading = true;
}
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
}
/**
* @return {?}
*/
increasePage() {
return new Promise((/**
* @param {?} resolve
* @return {?}
*/
resolve => {
if (!this.pagination.next) {
resolve(this);
return;
}
this._tableSettings.pagination.number = this.pagination.next;
this._tableSettings.pagination.next =
this.pagination.next < this.pagination.last
? this.pagination.next + 1
: null;
this._tableSettings.pagination.previous = this.pagination.previous + 1;
if (!this.pagination.manual) {
this.update().then((/**
* @return {?}
*/
() => {
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
return;
}
else {
this.loading = true;
}
resolve(this);
this.onPaginationChange.emit(this.pagination);
}));
}
/**
* @return {?}
*/
getData() {
switch (this.is_grouped) {
case true:
return _.flatten(((/** @type {?} */ (this.data))).map((/**
* @param {?} group
* @return {?}
*/
group => group.getData())));
case false:
return (/** @type {?} */ (this.data));
}
}
/**
* @return {?}
*/
getRawData() {
switch (this.is_grouped) {
case true:
return _.flatten(((/** @type {?} */ (this.data))).map((/**
* @param {?} group
* @return {?}
*/
group => group.getRawData())));
case false:
return ((/** @type {?} */ (this.data))).map((/**
* @param {?} item
* @return {?}
*/
item => item.getData()));
}
}
/**
* @param {?=} raw_data
* @return {?}
*/
getSelectedData(raw_data = false) {
/** @type {?} */
let data = this.getData();
if (data.length == 0) {
return [];
}
data = data.filter((/**
* @param {?} item
* @return {?}
*/
item => item.selected));
return _.flatten(data).map((/**
* @param {?} _item
* @return {?}
*/
_item => (raw_data ? _item.getData() : _item)));
}
/**
* @param {?=} raw_data
* @return {?}
*/
getSelectableData(raw_data = false) {
/** @type {?} */
let data = this.getData();
if (data.length == 0) {
return [];
}
data = data.filter((/**
* @param {?} row
* @return {?}
*/
row => row.selectable));
return _.flatten(data).map((/**
* @param {?} _item
* @return {?}
*/
_item => (raw_data ? _item.getData() : _item)));
}
/**
* @return {?}
*/
updateAllSelectedIcon() {
/** @type {?} */
const selected_count = this.getSelectedData().length;
/** @type {?} */
const all_selectable_count = this.getSelectableData().length;
this._all_selected_icon =
all_selectable_count > 0 && selected_count == all_selectable_count
? "check_box"
: selected_count > 0
? "indeterminate_check_box"
: "check_box_outline_blank";
}
/**
* @return {?}
*/
toggleSelectAll() {
/** @type {?} */
const selected_count = this.getSelectedData().length;
/** @type {?} */
const all_selectable_count = this.getSelectableData().length;
/** @type {?} */
const all_selected = selected_count == all_selectable_count;
this.getSelectableData().forEach((/**
* @param {?} item
* @return {?}
*/
item => item.setSelected(all_selected ? false : true, true)));
this.onSelectionChange.emit(this.getSelectedData(true));
}
/**
* @return {?}
*/
getTrackRowBy() {
return (/**
* @param {?} index
* @param {?} row
* @return {?}
*/
(index, row) => {
/** @type {?} */
const row_data = row.getData();
return row_data.id ? row_data.id : index;
});
}
/**
* @return {?}
*/
getTrackItemBy() {
return (/**
* @param {?} index
* @param {?} item
* @return {?}
*/
(index, item) => {
return item.accessor || index;
// const row_data = item.row_data;
// let accessor_data = item.accessor ? _.at(row_data, item.accessor)[0] : null;
// if (item.accessor && (!accessor_data || _.isObjectLike(accessor_data))) {
// accessor_data = `${item.accessor}_${index}`;
// }
// return accessor_data ? accessor_data : index;
});
}
/**
* @private
* @return {?}
*/
_afterUpdate() {
this.updateTotals();
this.updateColumns();
}
/**
* @private
* @param {?=} callback
* @return {?}
*/
_updateRemote(callback) {
this.raw_data = [];
this.data = [];
this._totals = [];
this.loading = true;
this.remote
.call()
.then((/**
* @param {?} data
* @return {?}
*/
data => {
this.raw_data = data;
/** @type {?} */
let indexed_data = _.cloneDeep(this.raw_data).map((/**
* @param {?} row_data
* @param {?} index
* @return {?}
*/
(row_data, index) => {
row_data.index = index;
return row_data;
}));
indexed_data = indexed_data.map((/**
* @param {?} row_data
* @param {?} index
* @return {?}
*/
(row_data, index) => new TableRow(this, this.columns, index, this.raw_data[row_data.index])));
this.data = this._applyGroup(indexed_data);
this._afterUpdate();
this.loading = false;
/** @type {?} */
const _data = this.getData();
if (_.isFunction(callback)) {
callback(_data);
}
this.onDataChange.emit(_data);
this.onUpdateSuccess.emit();
this.onUpdateEnd.emit();
}))
.catch((/**
* @param {?} err
* @return {?}
*/
err => {
console.warn(err);
this.raw_data = [];
this.data = [];
this._afterUpdate();
this.loading = false;
/** @type {?} */
const _data = this.getData();
if (_.isFunction(callback)) {
callback(_data);
}
this.onDataChange.emit(_data);
this.onUpdateError.emit();
this.onUpdateEnd.emit();
}));
}
/**
* @private
* @param {?=} callback
* @return {?}
*/
_updateLocal(callback) {
if (_.isEmpty(this.raw_data)) {
this.data = [];
this._afterUpdate();
/** @type {?} */
const _data = this.getData();
if (_.isFunction(callback)) {
callback(_data);
}
this.onDataChange.emit(_data);
this.onUpdateSuccess.emit();
this.onUpdateEnd.emit();
return;
}
/** @type {?} */
let filtered_data;
/** @type {?} */
const filter_value = _.isFunction(this.filter.value)
? this.filter.value()
: this.filter.value;
// Set index to bind with real data
/** @type {?} */
const indexed_data = _.cloneDeep(this.raw_data).map((/**
* @param {?} row_data
* @param {?} index
* @return {?}
*/
(row_data, index) => {
row_data.index = index;
return row_data;
}));
if (!_.isEmpty(filter_value) && !_.isEmpty(this.filter.accessor)) {
filtered_data = this._filterBy.transform(indexed_data, this.filter.accessor, filter_value);
}
else {
filtered_data = indexed_data;
}
if (!this.pagination.disabled && !this.pagination.manual) {
this.pagination.last = Math.floor(filtered_data.length / this.pagination.size);
if (this.pagination.last < 1) {
this.pagination.last = 1;
}
if (this.pagination.number > this.pagination.last) {
this.pagination.number = this.pagination.last;
}
this.pagination.next =
this.pagination.number < this.pagination.last
? this.pagination.number + 1
: null;
this.pagination.previous =
this.pagination.number > 0 ? this.pagination.number - 1 : null;
this.pagination.count = filtered_data.length;
/** @type {?} */
const start_index = (this.pagination.number > 0 ? this.pagination.number - 1 : 0) *
this.pagination.size;
/** @type {?} */
const end_index = start_index + this.pagination.size;
/** @type {?} */
const ordered_data = this._applySort(filtered_data, start_index, end_index);
/** @type {?} */
const instantiated_data = this._createRows(ordered_data);
this.data = this._applyGroup(instantiated_data);
}
else {
/** @type {?} */
const ordered_data = this._applySort(filtered_data);
/** @type {?} */
const instantiated_data = this._createRows(ordered_data);
this.data = this._applyGroup(instantiated_data);
}
this._afterUpdate();
/** @type {?} */
const _data = this.getData();
if (_.isFunction(callback)) {
callback(_data);
}
this.onDataChange.emit(_data);
this.onUpdateSuccess.emit();
this.onUpdateEnd.emit();
}
/**
* @private
* @param {?} filtered_data
* @return {?}
*/
_createRows(filtered_data) {
return filtered_data.map((/**
* @param {?} row_data
* @return {?}
*/
row_data => new TableRow(this, this.columns, row_data.index, this.raw_data[row_data.index])));
}
/**
* @private
* @param {?} filtered_data
* @param {?=} page_start_index
* @param {?=} page_end_index
* @return {?}
*/
_applySort(filtered_data, page_start_index, page_end_index) {
if (this.sort.manual) {
return filtered_data;
}
/** @type {?} */
const ordered_data = this._orderBy.transform(filtered_data, this._getSortProperty());
if (page_start_index >= 0 && page_end_index >= 0) {
return ordered_data.slice(page_start_index, page_end_index);
}
return ordered_data;
}
/**
* @private
* @param {?} ordered_data
* @return {?}
*/
_applyGroup(ordered_data) {
if (!this._is_grouped) {
return ordered_data;
}
/** @type {?} */
const groups = _.groupBy(ordered_data, `_rowData.${this.group.accessor}`);
/** @type {?} */
const parsed_groups = _.map(groups, (/**
* @param {?} data
* @param {?} key
* @return {?}
*/
(data, key) => {
return {
key: key,
data: data
};
}));
/** @type {?} */
const ordered_groups = _.orderBy(parsed_groups, "key", "asc");
/** @type {?} */
let prev_group;
/** @type {?} */
const grouped_data = ordered_groups.map((/**
* @param {?} group
* @return {?}
*/
group => {
/** @type {?} */
const key = group.key == "null" || group.key == "undefined" ? null : group.key;
/** @type {?} */
const instance = new TableGroup(this, this.group, key, group.data, prev_group);
prev_group = instance;
return instance;
}));
return grouped_data;
}
/**
* @private
* @return {?}
*/
_getSortProperty() {
return `${this._getSortDirectionSignal()}${this.sort.accessor}`;
}
/**
* @private
* @return {?}
*/
_getSortDirectionSignal() {
return this.sort.direction == "asc" ? "+" : "-";
}
/**
* @private
* @param {?=} sort
* @return {?}
*/
_getSortDirectionIcon(sort = this.sort) {
return `sort-${sort.direction == "asc" ? "down" : "up"}`;
}
/**
* @private
* @return {?}
*/
_getDefaultSortAccessor() {
/** @type {?} */
let first_column_sortable;
this.columns.forEach((/**
* @param {?} column
* @return {?}
*/
column => {
if (!first_column_sortable && column.sortable) {
first_column_sortable = column;
}
}));
return first_column_sortable
? first_column_sortable.accessor
: this.columns[0].accessor;
}
/**
* @private
* @return {?}
*/
_getDefaultSort() {
return {
manual: false,
direction: "asc",
accessor: this._getDefaultSortAccessor(),
icon: "sort-down"
};
}
/**
* @private
* @return {?}
*/
_getDefaultPagination() {
return {
disabled: false,
manual: false,
number: 1,
last: 1,
next: null,
previous: null,
count: 0,
size: 50
};
}
/**
* @private
* @return {?}
*/
_setAttributes() {
this._is_grouped = this.group && this.group.accessor ? true : false;
}
/**
* @private
* @return {?}
*/
_loadCache() {
if (!this._tableSettings.id || !this._tableSettings.cache) {
return;
}
try {
this._cache = JSON.parse(localStorage.getItem(`uex-table-cache:${this._tableSettings.id}`));
}
catch (e) {
console.warn(e);
this._cache = null;
}
if (!_.isEmpty(this._cache)) {
if (this._cache.sort) {
this.setSort(this._cache.sort, true);
}
if (this._cache.filter) {
this.setFilter(this._cache.filter, true);
}
if (this._cache.pagination) {
this.setPagination(this._cache.pagination, true);
}
this.onCacheLoaded.emit(this._cache);
}
}
/**
* @private
* @return {?}
*/
_saveCache() {
if (!this._tableSettings.id || !this._tableSettings.cache) {
return;
}
this._cache = {
sort: this.sort,
filter: this.filter,
pagination: this.pagination
};
try {
localStorage.setItem(`uex-table-cache:${this._tableSettings.id}`, JSON.stringify(this._cache));
}
catch (e) {
console.warn(e);
try {
localStorage.removeItem(`uex-table-cache:${this._tableSettings.id}`);
}
catch (e) { }
}
}
}
if (false) {
/**
* @type {?}
* @private
*/
Table.prototype._remote;
/**
* @type {?}
* @private
*/
Table.prototype._cache;
/**
* @type {?}
* @private
*/
Table.prototype._columns;
/**
* @type {?}
* @private
*/
Table.prototype._totals;
/**
* @type {?}
* @private
*/
Table.prototype._is_grouped;
/**
* @type {?}
* @private
*/
Table.prototype._all_selected_icon;
/** @type {?} */
Table.prototype.onUpdateStart;
/** @type {?} */
Table.prototype.onUpdateError;
/** @type {?} */
Table.prototype.onUpdateSuccess;
/** @type {?} */
Table.prototype.onUpdateEnd;
/** @type {?} */
Table.prototype.onCacheLoaded;
/** @type {?} */
Table.prototype.onDataChange;
/** @type {?} */
Table.prototype.onGroupChange;
/** @type {?} */
Table.prototype.onTotalsChange;
/** @type {?} */
Table.prototype.onPaginationChange;
/** @type {?} */
Table.prototype.onFilterChange;
/** @type {?} */
Table.prototype.onSortChange;
/** @type {?} */
Table.prototype.onSelectionChange;
/** @type {?} */
Table.prototype.onRowClick;
/** @type {?} */
Table.prototype.loading;
/** @type {?} */
Table.prototype.data;
/**
* @type {?}
* @private
*/
Table.prototype._tableSettings;
/**
* @type {?}
* @private
*/
Table.prototype._orderBy;
/**
* @type {?}
* @private
*/
Table.prototype._filterBy;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFibGUuanMiLCJzb3VyY2VSb290Ijoibmc6Ly9AdWV4L3dlYi1leHRlbnNpb25zLyIsInNvdXJjZXMiOlsibGliL21vZHVsZXMvdGFibGUvY2xhc3Nlcy90YWJsZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSxlQUFlLENBQUM7QUFHN0MsT0FBTyxLQUFLLENBQUMsTUFBTSxRQUFRLENBQUM7QUFFNUIsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLFVBQVUsQ0FBQztBQUN2QyxPQUFPLEVBQUUsV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