1// Requires: jQuery 2 3window.stm = { 4 5 /* 6 * SETTINGS 7 */ 8 settings: { 9 endpoint: 'http://stmi.stage.tp1.ca', 10 language: 'fr', 11 limit: 5 12 }, 13 14 requests: [], 15 16 /* 17 * METHODS 18 */ 19 20 init: function() {}, 21 22 str: function(french, english) { 23 if (this.settings.language == 'fr') 24 return french; 25 return english; 26 }, 27 28 /* 29 stm.get(uri, callback) 30 stm.get(uri, data, callback) 31 */ 32 get: function(uri, data, callback) { 33 var self = this; 34 if (arguments.length <= 2) { 35 callback = data; 36 data = null; 37 } 38 uri = uri || '/'; 39 data = data || {}; 40 data['o'] = 'web' 41 callback = callback || this.handleData; 42 43 url = this.settings.endpoint; 44 url += '/' + this.settings.language; 45 url += uri; 46 xhr = jQuery.ajax({ 47 type: 'GET', 48 url: url, 49 data: data, 50 dataType: 'json', 51 //headers: { 52 // "Authorization": "Basic " + btoa(config_data.usr + ":" + config_data.pwd) 53 //}, 54 beforeSend: function(xhr, settings) { 55 self.requests.push(xhr); 56 57 // Cleanup global 58 jQuery.each(self.requests, function(k, v) { 59 if (v && v.readyState == 4) self.requests.splice(k, 1); 60 }); 61 }, 62 success: callback 63 }); 64 return xhr; 65 }, 66 67 /* 68 Get data 69 */ 70 71 /* 72 stm.fetchBixi(callback) 73 */ 74 75 fetchBixi: function(callback) { 76 return this.get('/bixis', callback); 77 }, 78 79 /* 80 stm.fetchRetailers(callback) 81 */ 82 83 fetchRetailers : function(callback) { 84 return this.get('/detaillants', callback) 85 }, 86 87 /* 88 stm.getLinesByGeography(lat, lon, callback) 89 */ 90 91 getLinesByGeography: function(lat, lon, callback) { 92 return this.get('/lines/getLinesByGeography', { lat: lat, lon: lon }, callback); 93 }, 94 95 getCalendarData: function(callback) { 96 return this.get('/variation/calendar', callback); 97 }, 98 99 getLineStopTimeVariation: function(stop, line, date, limit, accessible, callback) { 100 if (!this.checkStopcode(stop) || !this.checkLine(line)) return false; 101 102 var direction = line.match(/[NSEW]$/g) ? line.match(/[NSEW]$/g)[0] : null; 103 line = line.match(/^[1-9][0-9]*/g); 104 105 if (arguments.length <= 5) { 106 callback = accessible; 107 accessible = 0; 108 } 109 110 if (arguments.length <= 4) { 111 callback = limit; 112 limit = this.settings.limit; 113 } 114 115 if (arguments.length <= 3) { 116 callback = date; 117 date = null; 118 } 119 120 if (!date) date = new Date(); 121 122 time = null; 123 124 if (date != null) { 125 date = this.parseDate(date); 126 127 time = date.slice(-4); 128 date = date.substr(0, 8); 129 } 130 131 var data = { direction: direction, wheelchair: (accessible ? '1' : '0') }; 132 if (limit > 0) data.limit = limit; 133 134 return this.get('/lines/' + line + '/stops/' + stop + '/variation', data, callback); 135 }, 136 137 getVariationLine: function(line, callback) { 138 line = line.match(/^[1-9][0-9]*/g); 139 return this.get('/lines/' + line + '/variation', callback); 140 }, 141 142 /* 143 stm.getStopsByGeography(lat, lon, callback) 144 */ 145 146 getStopsByGeography: function(lat, lon, callback) { 147 return this.get('/stops/getStopsByGeography', { lat: lat, lon: lon }, callback); 148 }, 149 150 /* 151 stm.getStopLines(stop, callback) 152 */ 153 154 getStopLines: function(stop, callback) { 155 if (!this.checkStopcode(stop)) return false; 156 return this.get('/stops/' + stop + '/lines', callback); 157 }, 158 159 /* 160 stm.getStopLineTime(stop, line, callback) 161 stm.getStopLineTime(stop, line, date, callback) 162 stm.getStopLineTime(stop, line, date, limit, callback) 163 stm.getStopLineTime(stop, line, date, limit, accessible, callback) 164 */ 165 166 getLineStopTimeNightExeption: function(stop, line, date, limit, accessible, callback) { 167 if (!this.checkStopcode(stop) || !this.checkLine(line)) return false;
168 169 var direction = line.match(/[NSEW]$/g) ? line.match(/[NSEW]$/g)[0] : null; 170 line = line.match(/^[1-9][0-9]*/g); 171 172 if (arguments.length <= 5) { 173 callback = accessible; 174 accessible = 0; 175 } 176 177 if (arguments.length <= 4) { 178 callback = limit; 179 limit = this.settings.limit; 180 } 181 182 if (arguments.length <= 3) { 183 callback = date; 184 date = null; 185 } 186 187 188 189 if (!date) date = new Date(); 190 191 time = null; 192 //var dateSend = date; 193 if (line >= 300 && line <= 400) { 194 date.setDate(date.getDate() - 1); 195 } 196 197 if (date != null) { 198 date = this.parseDate(date); 199 200 time = date.slice(-4); 201 date = date.substr(0, 8); 202 } 203 204 var data = { d: date, t: time, direction: direction, wheelchair: (accessible ? '1' : '0') }; 205 if (limit > 0) data.limit = limit; 206 207 return this.get('/lines/' + line + '/stops/' + stop + '/arrivals', data, callback); 208 }, 209 210 getLineStopTime: function(stop, line, date, limit, accessible, web_mips, callback) { 211 if (!this.checkStopcode(stop) || !this.checkLine(line)) return false; 212 213 var direction = line.match(/[NSEW]$/g) ? line.match(/[NSEW]$/g)[0] : null; 214 line = line.match(/^[1-9][0-9]*/g); 215 216 if (arguments.length <= 5) { 217 callback = accessible; 218 accessible = 0; 219 } 220 221 if (arguments.length <= 4) { 222 callback = limit; 223 limit = this.settings.limit; 224 } 225 226 if (arguments.length <= 3) { 227 callback = date; 228 date = null; 229 } 230 231 if (!date) date = new Date(); 232 233 time = null; 234 235 if (date != null) { 236 date = this.parseDate(date); 237 238 time = date.slice(-4); 239 date = date.substr(0, 8); 240 } 241 242 var data = { d: date, t: time, direction: direction, wheelchair: (accessible ? '1' : '0'), web_mips: web_mips }; 243 if (limit > 0) data.limit = limit; 244 245 return this.get('/lines/' + line + '/stops/' + stop + '/arrivals', data, callback); 246 }, 247 248 /** 249 * @type {stmx.LineStopTimeParams} params Params 250 * @type {Function} callback Callback function. 251 * @return {?XMLHttpRequest} 252 */ 253 getLineStopTimeV2: function(params, callback) { 254 var xhr = null; 255 256 var limit = params.limit; 257 var line = params.line; 258 var stop = params.stop; 259 260 var dateRaw = params.date || new Date(); 261 var parsedDate = this.parseDate(dateRaw); 262 var time = parsedDate.slice(-4); 263 var date = parsedDate.substr(0, 8); 264 265 var wheelchair = params.accessible === true ? 1 : 0; 266 var web_mips = 1; 267 268 if (this.checkStopcode(stop) && this.checkLine(line)) { 269 270 var direction = line.match(/[NSEW]$/g) ? line.match(/[NSEW]$/g)[0] : null; 271 line = line.match(/^[1-9][0-9]*/g); 272 273 var data = { 274 d: date, 275 t: time, 276 direction: direction, 277 wheelchair: wheelchair, 278 web_mips: web_mips 279 }; 280 281 if (limit !== undefined) { 282 data.limit = limit; 283 } 284 285 xhr = this.get( 286 '/lines/' + line + '/stops/' + stop + '/arrivals', 287 data, 288 callback 289 ); 290 } 291 292 return xhr; 293 }, 294 295 getLineStops: function(line, withconnection, callback) { 296 if (!this.checkLine(line)) return false; 297 298 if (arguments.length <= 2) { 299 callback = withconnection; 300 withconnection = false; 301 } 302 303 var direction = line.match(/[NSEWO]$/g) ? line.match(/[NSEWO]$/g)[0] : null; 304 if (direction == 'O') direction = 'W'; 305 line = line.match(/^[1-9][0-9]*/g); 306 return this.get('/lines/' + line + '/stops', { direction: direction, withconnection: withconnection ? 1 : 0, detoured: 1, canceled: 1 }, callback); 307 }, 308 309 getLineStopsCorrespondance: function(line, stop, withconnection, callback) { 310 if (!this.checkLine(line)) return false;
311 312 if (arguments.length <= 3) { 313 callback = withconnection; 314 withconnection = false; 315 } 316 317 var direction = line.match(/[NSEWO]$/g) ? line.match(/[NSEWO]$/g)[0] : null; 318 if (direction == 'O') direction = 'W'; 319 320 line = line.match(/^[1-9][0-9]*/g); 321 return this.get('/lines/' + line + '/stops/' + stop, { direction: direction, withconnection: withconnection ? 1 : 0 }, callback); 322 }, 323 324 325 getLineTrace: function(line, trace, callback) { 326 if (arguments.length < 3) { 327 callback = trace; 328 trace = null; 329 } 330 331 if (!trace) trace = 'default'; 332 333 var direction = line.match(/[NSEW]$/g) ? line.match(/[NSEW]$/g)[0] : null; 334 line = line.match(/^[1-9][0-9]*/g); 335 336 return this.get('/lines/' + line + '/routes/' + trace, { direction: direction, detoured: 1, canceled: 1 }, callback); 337 }, 338 339 getLineFamilies: function(callback) { 340 return this.get('/lines/categories', callback); 341 }, 342 343 getFilteredLines: function(filter, callback) { 344 return this.get('/lines', { filter: filter }, callback); 345 }, 346 347 search: function(q, callback) { 348 q = this.removeDiacritical(q); 349 return this.get('/lines/search', { q: q }, callback); 350 }, 351 352 /* 353 * stm.searchLine(q, callback) 354 */ 355 searchLine: function(q, callback) { 356 return this.get('/lines/searchLine', { q: q }, callback); 357 }, 358 359 /* 360 stm.getLinesById(id, callback) 361 */ 362 getLinesById: function(id, callback) { 363 return this.get('/lines/' + id, callback); 364 }, 365 366 /* 367 stm.getLineMessageByLineId(id, callback) 368 */ 369 getLineMessageByLineId: function(id, callback) { 370 return this.get('/lines/' + id + '/messages?type=Bus&web_mips=1', callback); //uncomment to activate web_mips 371 //return this.get('/lines/' + id + '/messages?type=Bus&web_mips=0', callback); 372 }, 373 374 /** 375 * Itinerary calls. 376 */ 377 itinerarySearch: function(q, callback) { 378 return this.get('/itineraries/search', { q: q }, callback); 379 }, 380 381 getItineraries: function(data, callback) { 382 return this.get('/itineraries', data, callback); 383 }, 384 385 /* 386 * Position calls 387 */ 388 getPositions: function(line, accessible, callback) { 389 if (!this.checkLine(line)) return false; 390 391 if (arguments.length <= 2) { 392 callback = accessible; 393 accessible = 0; 394 } 395 396 var direction = line.match(/[NSEWO]$/g) ? line.match(/[NSEWO]$/g)[0] : null; 397 if (direction == 'O') direction = 'W'; 398 399 line = line.match(/^[1-9][0-9]*/g); 400 return this.get('/lines/' + line + '/positions', { direction: direction, wheelchair: accessible ? 1 : 0 }, callback); 401 }, 402 403 /* 404 Helper functions 405 */ 406 407 parseDate: function(date) { 408 // Convert object to string format YYYMMDDHHMM 409 if (typeof date == 'object') { 410 date = date.getFullYear() + 411 String('0' + (date.getMonth() + 1)).slice(-2) + 412 String('0' + date.getDate()).slice(-2) + 413 String('0' + date.getHours()).slice(-2) + 414 String('0' + date.getMinutes()).slice(-2) 415 } 416 return date; 417 }, 418 419 checkStopcode: function(x) { 420 if (x && x.toString().match(/^[5-6][0-9][0-9][0-9][0-9]$/g) != null) 421 return true; 422 423 return false; 424 }, 425 426 checkLine: function(x) { 427 if (x && x.toString().match(/^[1-9][0-9]*[NSEWO]{0,1}$/g) != null && x.length < 13) 428 return true; 429 430 return false; 431 }, 432 433 handleData: function(data) { 434 //console.log(data); 435 }, 436 437 getMetroOccupancy: function(line, station, date_prediction, callback) { 438 return this.get('/line/' + line + '/station/' + station + '/occupancy?date_prediction=' + date_prediction, callback); 439 }, 440 441 removeDiacritical: function(str){ 442 return str.normalize('NFD').replace(/[\u0300-\u036f]/g, ''); 443 } 444 445};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.