-
Notifications
You must be signed in to change notification settings - Fork 7
/
renderer.js
143 lines (121 loc) · 4.96 KB
/
renderer.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
/**
* @namespace M.local_mr
*/
M.local_mr = M.local_mr || {};
/**
* @type {M.local_mr.LiveLog}
*/
M.local_mr.tableLiveLog = undefined;
/**
* Render mr_html_table and mr_html_paging with YUI
*
* @namespace M.local_mr
* @function
* @param Y
* @param {object} args
*/
M.local_mr.init_mr_html_table = function(Y, args) {
// Load this regardless of auto-load.
M.local_mr.init_table_live_log(Y);
if (!args.autoload) {
var thisInstance = this;
var theseArgs = arguments;
args.autoload = true;
// Create a function reference so this can be called later to load the table
var loadFunction = window[args.id + "_load"] = function() {
thisInstance.init_mr_html_table.apply(thisInstance, theseArgs);
};
return;
}
// Table's DataSource
var myDataSource = new Y.YUI2.util.DataSource(args.url);
myDataSource.responseType = Y.YUI2.util.DataSource.TYPE_JSON;
myDataSource.maxCacheEntries = 0;
myDataSource.responseSchema = {
resultsList: "records",
fields: args.columns,
metaFields: {
totalRecords: "totalRecords", // Access to value in the server response
emptyMessage: "emptyMessage"
}
};
// Table pagination configuration
var myPaginatorConfig = {
rowsPerPage: args.perpage,
alwaysVisible: false,
totalRecords: Number.MAX_VALUE, // Setting totalRecords arbitrarily high so that the initialPage setting will work. This doesn't affect behavior as it is overwritten when actual data is loaded.
initialPage: args.page + 1,
firstPageLinkLabel : "<< " + M.str.local_mr.paginatorfirstlabel,
firstPageLinkTitle : M.str.local_mr.paginatorfirsttitle,
lastPageLinkLabel : M.str.local_mr.paginatorlastlabel + " >>",
lastPageLinkTitle : M.str.local_mr.paginatorlasttitle,
previousPageLinkLabel : "< " + M.str.local_mr.paginatorprevlabel,
previousPageLinkTitle : M.str.local_mr.paginatorprevtitle,
nextPageLinkLabel : M.str.local_mr.paginatornextlabel + " >",
nextPageLinkTitle : M.str.local_mr.paginatornexttitle
};
// Add per page options to paginator
if (args.perpageopts.length > 0) {
myPaginatorConfig.template = Y.YUI2.widget.Paginator.TEMPLATE_ROWS_PER_PAGE;
myPaginatorConfig.rowsPerPageOptions = args.perpageopts;
myPaginatorConfig.alwaysVisible = true;
}
// Build custom requests
var myRequestBuilder = function(oState, oSelf) {
// Get states or use defaults
oState = oState || { pagination: null, sortedBy: null };
var sort = (oState.sortedBy) ? oState.sortedBy.key : args.sort;
var page = (oState.pagination) ? oState.pagination.recordOffset : (args.page * args.perpage);
var perpage = (oState.pagination) ? oState.pagination.rowsPerPage : args.perpage;
if (oState.sortedBy) {
var dir = oState.sortedBy.dir === Y.YUI2.widget.DataTable.CLASS_DESC ? args.desc : args.asc;
} else {
var dir = args.order;
}
if (page != 0) {
page = (page / perpage);
}
return "&tsort=" + sort +
"&torder=" + dir +
"&tpage=" + page +
"&tperpage=" + perpage;
};
// DataTable configuration
var myDataTableConfigs = {
initialRequest: myRequestBuilder(),
MSG_LOADING: args.loadingmsg,
dynamicData: true,
paginator: new Y.YUI2.widget.Paginator(myPaginatorConfig),
generateRequest: myRequestBuilder,
sortedBy: {
key: args.sort,
dir: (args.order == args.asc) ? Y.YUI2.widget.DataTable.CLASS_ASC : Y.YUI2.widget.DataTable.CLASS_DESC
}
};
if (args.summary !== undefined) {
myDataTableConfigs.summary = args.summary;
}
if (args.caption !== undefined) {
myDataTableConfigs.caption = args.caption;
}
// DataTable instance
var myDataTable = new Y.YUI2.widget.DataTable(args.id, args.columns, myDataSource, myDataTableConfigs);
// Update totalRecords and empty message on the fly with value from server
myDataTable.handleDataReturnPayload = function(oRequest, oResponse, oPayload) {
oPayload.totalRecords = oResponse.meta.totalRecords;
myDataTable.set('MSG_EMPTY', oResponse.meta.emptyMessage);
return oPayload;
};
myDataTable.subscribe('columnSortEvent', function(e) {
var identifier = e.dir === Y.YUI2.widget.DataTable.CLASS_DESC ? 'tablesortedbydesc' : 'tablesortedbyasc';
M.local_mr.tableLiveLog.log_text(M.util.get_string(identifier, 'local_mr', e.column.label));
});
//Store a reference to the table so it can be accessed easily later
window[args.id] = myDataTable;
};
// Singleton!
M.local_mr.init_table_live_log = function(Y) {
if (Y.Lang.isUndefined(M.local_mr.tableLiveLog)) {
M.local_mr.tableLiveLog = M.local_mr.init_livelog();
}
};