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

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

Updated web

File size: 8.1 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);
[288]61 return '<span class="players_dateonline" title="' + date.toLocaleString() + '">' + prettyDate(date) + '</span>';
[279]62 }
[288]63 return text;
64 }
[279]65 ],
66 [ "ping", "Ping" ],
67 ];
68
69 // Add column headers to <table>
70 for (var i = 0; i < columns.length; i++) {
71 $(".players_columns").append ("<td>" + columns[i][1] + "</td>");
72 }
73
74 // Set pager colspan accordingly
75 $(".players_pager").attr ("colspan", columns.length);
76
77 // Define header names array for tablesorter
78 var headers = [];
79 for (var c = 0; c < columns.length; c++) {
80 headers.push (columns[c][1]);
81 }
82
83 // Build table formatter object
84 var formatterSettings = {};
85 for (var i = 0; i < columns.length; i++) {
86 if (columns[i].length > 3 && columns[i][3] != null) {
87 formatterSettings [i] = columns[i][3];
88 //var formatter = columns[i][3];
89 //var column
90 }
91 }
92
93
94 function FindColumnIndexByField (fieldname) {
95 for(var i = 0; i < columns.length; i++) {
96 if(columns[i][0] === fieldname) {
97 return i;
98 }
99 }
100 return -1;
101 }
102
103
104
105 $(".players_tablesorter")
106 .tablesorter({
107 theme: 'default',
108 widthFixed: true,
109 sortLocaleCompare: true, // needed for accented characters in the data
110 sortList: [ [FindColumnIndexByField("name"),0] ],
111 widgets: ['zebra', 'formatter', 'filter'],
112 widgetOptions: {
[280]113 formatter_column: formatterSettings
[279]114 }
115 })
116 .tablesorterPager({
117 container: $(".players_pager"),
118
119 // If you want to use ajaxUrl placeholders, here is an example:
120 // ajaxUrl: "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
121 // where {page} is replaced by the page number (or use {page+1} to get a one-based index),
122 // {size} is replaced by the number of records to show,
123 // {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds
124 // the filterList to the url into an "fcol" array.
125 // So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
126 // and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
127 ajaxUrl : '../api/getplayerlist?page={page}&rowsperpage={size}&{filterList:' + filterParamName + '}&{sortList:' + sortParamName + '}',
128
129 customAjaxUrl: function(table, url) {
130 var address = url.substring (0, url.indexOf ("?"));
131 var queryString = url.substring (url.indexOf ("?") + 1);
132 var params = queryString.split("&");
133 for (var i = 0; i < params.length; i++) {
134 var pair = params[i].split("=");
135
136 if ((pair.length == 2) && (pair[0].indexOf ("[") >= 0)) {
137 var paramName = pair[0].substring (0, pair[0].indexOf ("["));
138 var paramIndex = pair[0].substring (pair[0].indexOf ("[") + 1, pair[0].indexOf ("]"));
139 if ((paramName == sortParamName) || (paramName == filterParamName)) {
140 paramIndex = columns[paramIndex][0];
141 pair[0] = paramName + "[" + paramIndex + "]";
142 params[i] = pair.join("=");
143 }
144 }
145 }
146
147 queryString = params.join("&");
148
149 url = address + "?" + queryString;
150
151
152 $(table).trigger('changingUrl', url);
153 return url;
154 },
155
156 ajaxError: null,
157
158 // add more ajax settings here
159 // see http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
160 ajaxObject: {
161 dataType: 'json'
162 },
163
164 ajaxProcessing: function(data){
165 var rows = [];
166 for (var i=0; i < data.players.length; i++) {
167 var row = [];
168 for (var c = 0; c < columns.length; c++) {
169 var col = columns[c];
170 var val = data.players[i][col[0]];
171 if (col.length > 2 && col[2] != null) {
172 val = col[2] (val);
173 }
174 row.push (val);
175 }
176 rows.push (row);
177 }
178
179 return {
180 "total": data.total,
181 "headers": headers,
182 "rows": rows
183 };
184 },
185
186 // Set this option to false if your table data is preloaded into the table, but you are still using ajax
187 processAjaxOnInit: true,
188
189 // output string - default is '{page}/{totalPages}';
190 // possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
191 // also {page:input} & {startRow:input} will add a modifiable input in place of the value
192 output: '{startRow} to {endRow} ({totalRows})',
193
194 // apply disabled classname (cssDisabled option) to the pager arrows when the rows
195 // are at either extreme is visible; default is true
196 updateArrows: true,
197
198 // starting page of the pager (zero based index)
199 page: 0,
200
201 // Number of visible rows - default is 10
202 size: 25,
203
204 // Saves the current pager page size and number (requires storage widget)
205 savePages: false,
206
207 // Reset pager to this page after filtering; set to desired page number (zero-based index),
208 // or false to not change page at filter start
209 pageReset: 0,
210
211 // if true, the table will remain the same height no matter how many records are displayed.
212 // The space is made up by an empty table row set to a height to compensate; default is false
213 fixedHeight: false,
214
215 // remove rows from the table to speed up the sort of large tables.
216 // setting this to false, only hides the non-visible rows; needed if you plan to
217 // add/remove rows with the pager enabled.
218 removeRows: false,
219
220 // If true, child rows will be counted towards the pager set size
221 countChildRows: false,
222
223 // css class names of pager arrows
224 cssNext : '.players_next', // next page arrow
225 cssPrev : '.players_prev', // previous page arrow
226 cssFirst : '.players_first', // go to first page arrow
227 cssLast : '.players_last', // go to last page arrow
228 cssGoto : '.players_gotoPage', // page select dropdown - select dropdown that set the "page" option
229
230 cssPageDisplay : '.players_pagedisplay', // location of where the "output" is displayed
231 cssPageSize : '.players_pagesize', // page size selector - select dropdown that sets the "size" option
232
233 // class added to arrows when at the extremes; see the "updateArrows" option
234 // (i.e. prev/first arrows are "disabled" when on the first page)
235 cssDisabled : 'disabled', // Note there is no period "." in front of this class name
236 cssErrorRow : 'tablesorter-errorRow' // error information row
237
238 });
239/*
240 var $url = $('#players_url');
241 $('.players_tablesorter').on('changingUrl', function(e, url){
242 $url.html(url);
243 });
244*/
245}
246
247
Note: See TracBrowser for help on using the repository browser.