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

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

Mod stuff

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