source: binary-improvements/webserver/js/players.js@ 280

Last change on this file since 280 was 280, checked in by alloc, 8 years ago

Mods

File size: 8.0 KB
RevLine 
[279]1function StartPlayersModule () {
2 var sortParamName = "sort";
3 var filterParamName = "filter";
4
5
6function prettyDate(date){
7 var diff = (((new Date()).getTime() - date.getTime()) / 1000),
8 day_diff = Math.floor(diff / 86400);
9 if ( isNaN(day_diff) || day_diff < 0 ) { return ''; }
10 return day_diff == 0 && (
11 diff < 60 && 'just now' ||
12 diff < 120 && '1 minute ago' ||
13 diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
14 diff < 7200 && '1 hour ago' ||
15 diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
16 day_diff == 1 && 'Yesterday' ||
17 day_diff < 7 && day_diff + ' days ago' ||
18 day_diff < 61 && Math.ceil( day_diff / 7 ) + ' weeks ago' ||
19 day_diff < 730 && Math.floor( day_diff / 30 ) + ' months ago' ||
20 Math.floor( day_diff / 365 ) + ' years ago';
21}
22
23
24 // Define columns to be shown
25 var columns = [
26 [ "steamid", "SteamID" ],
27 [ "entityid", "EntityID" ],
28 [ "ip", "IP" ],
29 [ "name", "Name" ],
30 [ "online", "Online", null,
31 function(text, data) {
32 // add text to data-attribute; this overrides the parser
33 data.$cell.attr(data.config.textAttribute, text);
34 if (text == 'false') {
35 return "<img src=\"img/oxygen-icons/32x32/status/task-reject.png\" width=\"16\">";
36 } else {
37 return "<img src=\"img/oxygen-icons/32x32/status/task-complete.png\" width=\"16\">";
38 }
39 }
40 ],
41 [ "position", "Position", function(inp) { return inp.x + "/" + inp.y + "/" + inp.z; } ],
42 [ "totalplaytime", "Total Playtime", null,
43 function(text, data) {
44 // add text to data-attribute; this overrides the parser
45 data.$cell.attr(data.config.textAttribute, text);
46 var minutes = Math.floor(text / 60);
47 var hours = Math.floor(minutes / 60);
48 minutes = minutes % 60;
49 if (hours > 0) {
50 return hours + " hours " + minutes + " minutes";
51 } else {
52 return minutes + " minutes";
53 }
54 }
55 ],
56 [ "lastonline", "Last Online", null,
57 function(text, data) {
58 var date = new Date(text);
59 if (date instanceof Date && isFinite(date) ) {
60 data.$cell.attr(data.config.textAttribute, text);
61 return '<em title="' + date.toLocaleString() + '">' + prettyDate(date) + '</em>';
62 }
63 return text; }
64 ],
65 [ "ping", "Ping" ],
66 ];
67
68 // Add column headers to <table>
69 for (var i = 0; i < columns.length; i++) {
70 $(".players_columns").append ("<td>" + columns[i][1] + "</td>");
71 }
72
73 // Set pager colspan accordingly
74 $(".players_pager").attr ("colspan", columns.length);
75
76 // Define header names array for tablesorter
77 var headers = [];
78 for (var c = 0; c < columns.length; c++) {
79 headers.push (columns[c][1]);
80 }
81
82 // Build table formatter object
83 var formatterSettings = {};
84 for (var i = 0; i < columns.length; i++) {
85 if (columns[i].length > 3 && columns[i][3] != null) {
86 formatterSettings [i] = columns[i][3];
87 //var formatter = columns[i][3];
88 //var column
89 }
90 }
91
92
93 function FindColumnIndexByField (fieldname) {
94 for(var i = 0; i < columns.length; i++) {
95 if(columns[i][0] === fieldname) {
96 return i;
97 }
98 }
99 return -1;
100 }
101
102
103
104 $(".players_tablesorter")
105 .tablesorter({
106 theme: 'default',
107 widthFixed: true,
108 sortLocaleCompare: true, // needed for accented characters in the data
109 sortList: [ [FindColumnIndexByField("name"),0] ],
110 widgets: ['zebra', 'formatter', 'filter'],
111 widgetOptions: {
[280]112 formatter_column: formatterSettings
[279]113 }
114 })
115 .tablesorterPager({
116 container: $(".players_pager"),
117
118 // If you want to use ajaxUrl placeholders, here is an example:
119 // ajaxUrl: "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
120 // where {page} is replaced by the page number (or use {page+1} to get a one-based index),
121 // {size} is replaced by the number of records to show,
122 // {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds
123 // the filterList to the url into an "fcol" array.
124 // So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
125 // and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
126 ajaxUrl : '../api/getplayerlist?page={page}&rowsperpage={size}&{filterList:' + filterParamName + '}&{sortList:' + sortParamName + '}',
127
128 customAjaxUrl: function(table, url) {
129 var address = url.substring (0, url.indexOf ("?"));
130 var queryString = url.substring (url.indexOf ("?") + 1);
131 var params = queryString.split("&");
132 for (var i = 0; i < params.length; i++) {
133 var pair = params[i].split("=");
134
135 if ((pair.length == 2) && (pair[0].indexOf ("[") >= 0)) {
136 var paramName = pair[0].substring (0, pair[0].indexOf ("["));
137 var paramIndex = pair[0].substring (pair[0].indexOf ("[") + 1, pair[0].indexOf ("]"));
138 if ((paramName == sortParamName) || (paramName == filterParamName)) {
139 paramIndex = columns[paramIndex][0];
140 pair[0] = paramName + "[" + paramIndex + "]";
141 params[i] = pair.join("=");
142 }
143 }
144 }
145
146 queryString = params.join("&");
147
148 url = address + "?" + queryString;
149
150
151 $(table).trigger('changingUrl', url);
152 return url;
153 },
154
155 ajaxError: null,
156
157 // add more ajax settings here
158 // see http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
159 ajaxObject: {
160 dataType: 'json'
161 },
162
163 ajaxProcessing: function(data){
164 var rows = [];
165 for (var i=0; i < data.players.length; i++) {
166 var row = [];
167 for (var c = 0; c < columns.length; c++) {
168 var col = columns[c];
169 var val = data.players[i][col[0]];
170 if (col.length > 2 && col[2] != null) {
171 val = col[2] (val);
172 }
173 row.push (val);
174 }
175 rows.push (row);
176 }
177
178 return {
179 "total": data.total,
180 "headers": headers,
181 "rows": rows
182 };
183 },
184
185 // Set this option to false if your table data is preloaded into the table, but you are still using ajax
186 processAjaxOnInit: true,
187
188 // output string - default is '{page}/{totalPages}';
189 // possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
190 // also {page:input} & {startRow:input} will add a modifiable input in place of the value
191 output: '{startRow} to {endRow} ({totalRows})',
192
193 // apply disabled classname (cssDisabled option) to the pager arrows when the rows
194 // are at either extreme is visible; default is true
195 updateArrows: true,
196
197 // starting page of the pager (zero based index)
198 page: 0,
199
200 // Number of visible rows - default is 10
201 size: 25,
202
203 // Saves the current pager page size and number (requires storage widget)
204 savePages: false,
205
206 // Reset pager to this page after filtering; set to desired page number (zero-based index),
207 // or false to not change page at filter start
208 pageReset: 0,
209
210 // if true, the table will remain the same height no matter how many records are displayed.
211 // The space is made up by an empty table row set to a height to compensate; default is false
212 fixedHeight: false,
213
214 // remove rows from the table to speed up the sort of large tables.
215 // setting this to false, only hides the non-visible rows; needed if you plan to
216 // add/remove rows with the pager enabled.
217 removeRows: false,
218
219 // If true, child rows will be counted towards the pager set size
220 countChildRows: false,
221
222 // css class names of pager arrows
223 cssNext : '.players_next', // next page arrow
224 cssPrev : '.players_prev', // previous page arrow
225 cssFirst : '.players_first', // go to first page arrow
226 cssLast : '.players_last', // go to last page arrow
227 cssGoto : '.players_gotoPage', // page select dropdown - select dropdown that set the "page" option
228
229 cssPageDisplay : '.players_pagedisplay', // location of where the "output" is displayed
230 cssPageSize : '.players_pagesize', // page size selector - select dropdown that sets the "size" option
231
232 // class added to arrows when at the extremes; see the "updateArrows" option
233 // (i.e. prev/first arrows are "disabled" when on the first page)
234 cssDisabled : 'disabled', // Note there is no period "." in front of this class name
235 cssErrorRow : 'tablesorter-errorRow' // error information row
236
237 });
238/*
239 var $url = $('#players_url');
240 $('.players_tablesorter').on('changingUrl', function(e, url){
241 $url.html(url);
242 });
243*/
244}
245
246
Note: See TracBrowser for help on using the repository browser.