/
mdtAjaxPaginationHelperFactory.js
159 lines (127 loc) · 5.56 KB
/
mdtAjaxPaginationHelperFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(function(){
'use strict';
function mdtAjaxPaginationHelperFactory(_){
function mdtAjaxPaginationHelper(params){
this.tableDataStorageService = params.tableDataStorageService;
this.rowOptions = params.mdtRowOptions;
this.paginatorFunction = params.mdtRowPaginatorFunction;
this.mdtRowPaginatorErrorMessage = params.mdtRowPaginatorErrorMessage || 'Ajax error during loading contents.';
this.mdtRowPaginatorNoResultsMessage = params.mdtRowPaginatorNoResultsMessage || 'No results.';
this.mdtTriggerRequest = params.mdtTriggerRequest;
if(params.paginationSetting &&
params.paginationSetting.hasOwnProperty('rowsPerPageValues') &&
params.paginationSetting.rowsPerPageValues.length > 0){
this.rowsPerPageValues = params.paginationSetting.rowsPerPageValues;
}else{
this.rowsPerPageValues = [10,20,30,50,100];
}
this.rowsPerPage = this.rowsPerPageValues[0];
this.page = 1;
this.totalResultCount = 0;
this.totalPages = 0;
this.isLoading = false;
//fetching the 1st page
this.fetchPage(this.page);
//triggering ajax call manually
if(this.mdtTriggerRequest) {
params.mdtTriggerRequest({
loadPageCallback: this.fetchPage.bind(this)
});
}
}
mdtAjaxPaginationHelper.prototype.getStartRowIndex = function(){
return (this.page-1) * this.rowsPerPage;
};
mdtAjaxPaginationHelper.prototype.getEndRowIndex = function(){
var lastItem = this.getStartRowIndex() + this.rowsPerPage - 1;
if(this.totalResultCount < lastItem){
return this.totalResultCount - 1;
}
return lastItem;
};
mdtAjaxPaginationHelper.prototype.getTotalRowsCount = function(){
return this.totalResultCount;
};
mdtAjaxPaginationHelper.prototype.getRows = function(){
return this.tableDataStorageService.storage;
};
mdtAjaxPaginationHelper.prototype.previousPage = function(){
var that = this;
if(this.hasPreviousPage()){
this.fetchPage(this.page-1).then(function(){
that.page--;
});
}
};
mdtAjaxPaginationHelper.prototype.nextPage = function(){
var that = this;
if(this.hasNextPage()){
this.fetchPage(this.page+1).then(function(){
that.page++;
});
}
};
mdtAjaxPaginationHelper.prototype.hasNextPage = function(){
return this.page < this.totalPages;
};
mdtAjaxPaginationHelper.prototype.hasPreviousPage = function(){
return this.page > 1;
};
mdtAjaxPaginationHelper.prototype.fetchPage = function(page){
this.isLoading = true;
var that = this;
return this.paginatorFunction({page: page, pageSize: this.rowsPerPage})
.then(function(data){
that.tableDataStorageService.storage = [];
that.setRawDataToStorage(that, data.results, that.rowOptions['table-row-id-key'], that.rowOptions['column-keys']);
that.totalResultCount = data.totalResultCount;
that.totalPages = Math.ceil(data.totalResultCount / that.rowsPerPage);
if(that.totalResultCount == 0){
that.isNoResults = true;
}else{
that.isNoResults = false;
}
that.isLoadError = false;
that.isLoading = false;
}, function(){
that.tableDataStorageService.storage = [];
that.isLoadError = true;
that.isLoading = false;
that.isNoResults = true;
});
};
mdtAjaxPaginationHelper.prototype.setRawDataToStorage = function(that, data, tableRowIdKey, columnKeys){
var rowId;
var columnValues = [];
_.each(data, function(row){
rowId = _.get(row, tableRowIdKey);
columnValues = [];
_.each(columnKeys, function(columnKey){
//TODO: centralize adding column values into one place.
// Duplication occurs at mdtCellDirective's link function.
columnValues.push({
attributes: {
editableField: false
},
columnKey: columnKey,
value: _.get(row, columnKey)
});
});
that.tableDataStorageService.addRowData(rowId, columnValues);
});
};
mdtAjaxPaginationHelper.prototype.setRowsPerPage = function(rowsPerPage){
this.rowsPerPage = rowsPerPage;
this.page = 1;
this.fetchPage(this.page);
};
return {
getInstance: function(tableDataStorageService, isEnabled, paginatorFunction, rowOptions){
return new mdtAjaxPaginationHelper(tableDataStorageService, isEnabled, paginatorFunction, rowOptions);
}
};
}
angular
.module('mdDataTable')
.service('mdtAjaxPaginationHelperFactory', mdtAjaxPaginationHelperFactory);
}());