2016-10-05 14:09:12 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* User: Martin Donnelly
|
|
|
|
* Date: 2016-10-03
|
|
|
|
* Time: 14:20
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-03-22 17:01:58 +00:00
|
|
|
let TrainModel = Backbone.Model.extend({
|
2017-04-02 10:58:54 +00:00
|
|
|
initialize: function () {
|
2017-04-02 23:07:55 +00:00
|
|
|
|
2017-04-02 10:58:54 +00:00
|
|
|
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();
|
2017-03-22 17:01:58 +00:00
|
|
|
},
|
2017-04-02 10:58:54 +00:00
|
|
|
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 () {
|
2017-03-22 17:01:58 +00:00
|
|
|
let url = this.get('url');
|
|
|
|
let self = this;
|
|
|
|
$.ajax({
|
2017-04-02 10:58:54 +00:00
|
|
|
type: 'GET',
|
|
|
|
url: url,
|
|
|
|
data: '',
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10000,
|
|
|
|
headers: {
|
2017-03-22 17:01:58 +00:00
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
|
|
|
|
'Access-Control-Allow-Headers': 'Content-Type'
|
2017-04-02 10:58:54 +00:00
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
/*
|
|
|
|
console.log(data);
|
|
|
|
*/
|
2017-03-22 17:01:58 +00:00
|
|
|
self.set('trainData', data);
|
2017-04-02 10:58:54 +00:00
|
|
|
},
|
|
|
|
error: function (xhr, type) {
|
2017-03-22 17:01:58 +00:00
|
|
|
console.log('ajax error');
|
|
|
|
console.log(xhr);
|
|
|
|
console.log(type);
|
2017-04-02 10:58:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
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);
|
2017-04-02 23:07:55 +00:00
|
|
|
//console.log('Got', data);
|
2017-04-02 10:58:54 +00:00
|
|
|
|
|
|
|
self.set('route', data);
|
|
|
|
self.set('visible', true);
|
|
|
|
},
|
|
|
|
error: function (xhr, type) {
|
2017-04-02 23:07:55 +00:00
|
|
|
console.error('ajax error');
|
|
|
|
console.error(xhr);
|
|
|
|
console.error(type);
|
2017-04-02 10:58:54 +00:00
|
|
|
}
|
2017-03-22 17:01:58 +00:00
|
|
|
});
|
2017-04-02 10:58:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 17:01:58 +00:00
|
|
|
}
|
|
|
|
});
|
2016-10-05 14:09:12 +00:00
|
|
|
|
2017-04-02 10:58:54 +00:00
|
|
|
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 () {
|
2017-04-02 23:07:55 +00:00
|
|
|
|
2017-04-02 10:58:54 +00:00
|
|
|
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 = `<div>${route.locationName} TO ${route.filterLocationName}</div>
|
|
|
|
<table class="mui-table mui-table-bordered">
|
|
|
|
<tr><th>Destination</th>
|
|
|
|
<th>Time</th>
|
|
|
|
<th>Status</th>
|
|
|
|
<th>Platform</th></tr>
|
|
|
|
`;
|
|
|
|
|
|
|
|
if (typeof route.trainServices === 'object' && route.trainServices !== null) {
|
2017-04-02 23:07:55 +00:00
|
|
|
for (let item of route.trainServices) {
|
2017-04-02 10:58:54 +00:00
|
|
|
let dest = item.destination[0];
|
|
|
|
let via = dest.via !== null ? `<em>${dest.via}</em>` : '';
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!item.isCancelled) {
|
|
|
|
ws = ws + `<tr>
|
|
|
|
<td>${dest.locationName} ${via}</td>
|
|
|
|
<td>${time}</td>
|
|
|
|
<td>${status}</td>
|
|
|
|
<td>${platform}</td>
|
|
|
|
</tr>`;
|
|
|
|
} else {
|
|
|
|
ws = ws + `<tr>
|
|
|
|
<td>${dest.locationName} ${via}</td>
|
|
|
|
<td>${time}</td>
|
|
|
|
<td colspan="2">❌ ${item.cancelReason}</td>
|
|
|
|
|
|
|
|
</tr>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof route.busServices === 'object' && route.busServices !== null) {
|
2017-04-02 23:07:55 +00:00
|
|
|
for (let item of route.busServices) {
|
2017-04-02 10:58:54 +00:00
|
|
|
let dest = item.destination[0];
|
|
|
|
let via = dest.via !== null ? `<em>${dest.via}</em>` : '';
|
|
|
|
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;
|
|
|
|
|
|
|
|
// ws = ws + `<div class="mui-row"><div class="mui-col-md-12"><strong>${dest.locationName} Bus</strong> ${via}</div></div>`;
|
|
|
|
// ws = ws + '<div class="mui-row"><div class="mui-col-md-12">' + item.sta + '</div></div>';
|
|
|
|
ws = ws + `<tr>
|
|
|
|
<td>🚌 ${dest.locationName} ${via}</td>
|
|
|
|
<td>${time}</td>
|
|
|
|
<td>${status}</td>
|
|
|
|
<td>${platform}</td>
|
|
|
|
</tr>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws = ws + '</table>';
|
|
|
|
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');
|
2017-04-02 23:07:55 +00:00
|
|
|
let html = `<div class='mui-col-xs-12 mui-col-md-6'>${target.toUpperCase()}: <button class="mui-btn mui-btn--flat" id="${target}"></button></div>`;
|
2017-04-02 10:58:54 +00:00
|
|
|
this.$html = $(html);
|
|
|
|
this.$html.on('click', function () {
|
2017-04-02 23:07:55 +00:00
|
|
|
// console.log(self)
|
2017-04-02 10:58:54 +00:00
|
|
|
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');
|
2016-10-05 14:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|