/** * * User: Martin Donnelly * Date: 2016-10-03 * Time: 14:20 * */ let TrainModel = Backbone.Model.extend({ initialize: function () { let fromStation = this.get('from'); let toStation = this.get('to'); let url = '/getnexttraintimes?from=' + fromStation + '&to=' + toStation; let routeUrl = '/gettrains?from=' + fromStation + '&to=' + toStation; let target = fromStation + toStation; this.set('url', url); this.set('routeUrl', routeUrl); this.set('target', target); this.set('visible', false); this.update(); }, update: function () { this.getTrain(); const now = new Date; const mod = 60000 - (now.getTime() % 60000); let trainUpdateFn = function () { this.update(); }; setTimeout(trainUpdateFn.bind(this), mod + 10); }, getTrain: function () { let url = this.get('url'); let 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) { /* console.log(data); */ self.set('trainData', data); }, error: function (xhr, type) { console.log('ajax error'); console.log(xhr); console.log(type); } }); }, getRoute: function () { let url = this.get('routeUrl'); let 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); } }); } } }); let 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 () { let output, status; let obj = this.model.get('trainData'); let visible = this.model.get('visible'); let route = this.model.get('route'); output = (obj.eta.toLowerCase() === 'on time') ? obj.sta : obj.eta; status = (obj.eta.toLowerCase() === 'on time') ? 'ontime' : 'delayed'; this.$button.html(output); this.$button.removeClass('delayed').removeClass('ontime').addClass(status); if (visible) { let ws = `
${route.locationName} TO ${route.filterLocationName}
`; let services = []; if (typeof route.trainServices === 'object' && route.trainServices !== null) { for (let item of route.trainServices) { let dest = item.destination[0]; let via = dest.via !== null ? `${dest.via}` : ''; let platform = item.platform !== null ? item.platform : '💠'; let time = item.sta !== null ? item.sta : `D ${item.std}`; let status = item.eta !== null ? item.eta : item.etd; services.push({location:dest.locationName, time:time,status:status,platform:platform, cancel:item.cancelReason, type:'train'}); if (!item.isCancelled) { ws = ws + ``; } else { ws = ws + ``; } } } if (typeof route.busServices === 'object' && route.busServices !== null) { for (let item of route.busServices) { let dest = item.destination[0]; let via = dest.via !== null ? `${dest.via}` : ''; let platform = item.platform !== null ? item.platform : ''; let time = item.sta !== null ? item.sta : `D ${item.std}`; let status = item.eta !== null ? item.eta : item.etd; services.push({location:dest.locationName, time:time,status:status,platform:platform, cancel:item.cancelReason, type:'bus'}); // ws = ws + `
${dest.locationName} Bus ${via}
`; // ws = ws + '
' + item.sta + '
'; ws = ws + ``; } } ws = ws + '
Destination Time Status Platform
${dest.locationName} ${via} ${time} ${status} ${platform}
${dest.locationName} ${via} ${time} ❌ ${item.cancelReason}
🚌 ${dest.locationName} ${via} ${time} ${status} ${platform}
'; this.$traintext.empty().html(ws); this.$traintext.removeClass('mui--hide').addClass('mui--show'); } else { this.$traintext.removeClass('mui--show').addClass('mui--hide'); } }, initView: function () { //el: $('#myView').get(0) let self = this; let target = this.model.get('target'); let html = `
${target.toUpperCase()}:
`; this.$html = $(html); this.$html.on('click', function () { // console.log(self) self.model.getRoute(); }); this.$trains.append(this.$html); // this.el = `#${target}`; this.$button = $(`#${target}`); let cevent = `click #$(target)`; this.events[cevent] = "showTrains"; }, showTrains: function () { console.log('Show train'); } });