/** * * User: Martin Donnelly * Date: 2016-10-03 * Time: 14:20 * */ const $ = require('jquery'); const _ = require('underscore'); const Backbone = require('backbone'); const TrainModel = Backbone.Model.extend({ 'initialize': function () { const fromStation = this.get('from'); const toStation = this.get('to'); const url = `https://traintimes.silvrgit.co.uk/getnexttraintimes?from=${ this.get('from') }&to=${ this.get('to')}`; const routeUrl = `https://traintimes.silvrgit.co.uk/gettrains?from=${ this.get('from') }&to=${ this.get('to')}`; const target = this.get('from') + this.get('to'); this.set('url', url); this.set('routeUrl', routeUrl); this.set('target', target); this.set('visible', false); this.set('trainData', { 'eta':'OFF', 'sta': 'OFF' }); this.update(); }, 'update': function () { const now = new Date; const hours = now.getHours(); const limit = (hours < 6) ? 3600000 : 60000; const mod = limit - (now.getTime() % limit); if (hours >= 6) this.getTrain(); else this.set('trainData', { 'eta':'OFF', 'sta': 'OFF' }); const trainUpdateFn = function () { this.update(); }; setTimeout(trainUpdateFn.bind(this), mod + 10); }, 'getTrain': function () { const url = this.get('url'); const self = this; $.ajax({ 'type': 'GET', 'url': url, 'data': '', 'dataType': 'json', 'timeout': 10000, 'headers': { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type' }, 'success': function (data) { self.set('trainData', data); }, 'error': function (xhr, type) { console.log('ajax error'); console.log(xhr); console.log(type); } }); }, 'getRoute': function () { const url = this.get('routeUrl'); const self = this; if (this.get('visible') === true) this.set('visible', false); else $.ajax({ 'type': 'GET', 'url': url, 'data': '', 'dataType': 'json', 'timeout': 10000, 'headers': { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type' }, 'success': function (data) { // getTrainsCB(data); // console.log('Got', data); self.set('route', data); self.set('visible', true); }, 'error': function (xhr, type) { console.error('ajax error'); console.error(xhr); console.error(type); } }); } }); const TrainView = Backbone.View.extend({ 'tagName': 'div', 'initialize': function () { _.bindAll(this, 'render'); this.model.bind('change', this.render); this.$trains = $('#trains'); this.$traininfo = $('#traininfo'); this.$traintext = $('#trainResults'); this.$el = this.$trains; this.initView(); }, 'events': { 'click': 'showTrains' }, 'render': function () { const obj = this.model.get('trainData'); const visible = this.model.get('visible'); const route = this.model.get('route'); const output = (obj.eta.toLowerCase() === 'on time') ? obj.sta : obj.eta; const status = (obj.eta.toLowerCase() === 'on time') ? 'ontime' : 'delayed'; this.$button.html(output); this.$button.removeClass('delayed').removeClass('ontime').addClass(status); if (visible) { let ws = `
Destination | Time | Status | Platform |
---|---|---|---|
${dest.locationName} ${via} | ${time} | ${status} | ${platform} |
${dest.locationName} ${via} | ${time} | ❌ ${item.cancelReason} | |
🚌 ${dest.locationName} ${via} | ${time} | ${status} | ${platform} |