-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (58 loc) · 3.16 KB
/
index.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
var validator = require('./helpers/validators');
var links = require('./helpers/links');
var runner = require('./helpers/runner');
function Paginator(obj) {
if(! validator.isAlphaOrParen(obj.pageParam))
throw new Error("Invalid page parameter.");
if(! obj.hasOwnProperty('dbConn'))
throw new Error("Database connection not provided.");
this.db_connection = obj.dbConn;
this.table_name = obj.hasOwnProperty('tableName') ? obj.tableName : null;
this.records_per_page = obj.hasOwnProperty('pageSize') ? obj.pageSize : 25;
this.base_url = obj.hasOwnProperty('baseUrl') ? obj.baseUrl : null;
this.paging_param_name = obj.hasOwnProperty('pageParam') ? obj.pageParam : null;
this.link_ul_classes = obj.hasOwnProperty('linksClasses') ? obj.linksClasses : 'pagination';
this.link_inactive_li_classes = obj.hasOwnProperty('inactiveClasses') ? obj.inactiveClasses : '';
this.link_active_li_classes = obj.hasOwnProperty('activeClasses') ? obj.activeClasses : 'active';
this.custom_query = obj.customQuery ? obj.customQuery : null;
}
Paginator.prototype.configureInstance = function configureInstance(obj) {
if(obj.hasOwnProperty('pageSize') && ! validator.isPositiveInt(obj.pageSize))
throw new Error("Invalid page size.");
this.base_url = obj.hasOwnProperty('baseUrl') ? obj.baseUrl : this.base_url;
this.table_name = obj.hasOwnProperty('tableName') ? obj.tableName : this.table_name;
this.records_per_page = obj.hasOwnProperty('pageSize') ? obj.pageSize : this.records_per_page;
this.db_connection = obj.hasOwnProperty('dbConn') ? obj.dbConn : this.db_connection;
this.paging_param_name = obj.hasOwnProperty('pageParam') ? obj.pageParam : this.paging_param_name;
this.link_ul_classes = obj.hasOwnProperty('linksClasses') ? obj.linksClasses : 'pagination';
this.link_inactive_li_classes = obj.hasOwnProperty('inactiveClasses') ? obj.inactiveClasses : '';
this.link_active_li_classes = obj.hasOwnProperty('activeClasses') ? obj.activeClasses : 'active';
this.custom_query = obj.customQuery ? obj.customQuery : null;
};
Paginator.prototype.getPage = function getPage(_pageNum, success, error) {
if(! this.db_connection || (! this.table_name && ! this.custom_query) ||
! this.records_per_page || ! _pageNum || ! this.base_url || ! validator.isPositiveInt(_pageNum))
{
return error("Invalid configuration or parameters found.");
}
var self = this;
runner.getRecords(_pageNum, this.db_connection, this.table_name, this.records_per_page, this.custom_query,
function(err, result) {
if(err || result.length == 0) {
return error(err);
}
runner.getTotalCount(self.db_connection, self.table_name, self.custom_query,
function(_err, recordCount) {
if(_err || recordCount == 0) {
return error(err);
}
links_html =
links.getLinks(_pageNum, Math.ceil(recordCount / self.records_per_page), recordCount,
self.base_url, self.paging_param_name, self.link_ul_classes, self.link_active_li_classes, self.link_inactive_li_classes);
return success({records: result, links: links_html});
});
});
}
exports.createPaginator = function createPaginator(obj) {
return new Paginator(obj);
}