Improved www.ttss.krakow.pl
Jacek Kowalski
2019-05-04 7e722179b6df2f7c6d0fc68c3f1f374df230d774
Add Codacy badge and fix some errors reported by the checker
5 files modified
291 ■■■■ changed files
README.md 1 ●●●● patch | view | raw | blame | history
common.js 69 ●●●● patch | view | raw | blame | history
index.css 2 ●●●●● patch | view | raw | blame | history
index.js 201 ●●●● patch | view | raw | blame | history
map.js 18 ●●●●● patch | view | raw | blame | history
README.md
@@ -8,6 +8,7 @@
Contact and suggestions: mpk_AT_jacekk.info
[![Build Status](https://travis-ci.org/jacekkow/mpk-ttss.svg?branch=master)](https://travis-ci.org/jacekkow/mpk-ttss)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/530c81cbab3449a9b24327591361bec8)](https://www.codacy.com/app/jacekkow/mpk-ttss)
## Differences
common.js
@@ -1,4 +1,4 @@
"use strict";
'use strict';
var ttss_urls = {
    t: 'proxy_tram.php',
@@ -26,7 +26,7 @@
        promise: promise,
        request: request,
        abort: function() {
            request.abort.bind(request)
            request.abort.bind(request);
            return Deferred(promise, request);
        },
        done: function(func) {
@@ -70,7 +70,7 @@
                    }
                }
            };
            request.open("GET", url, true);
            request.open('GET', url, true);
            request.send();
        });
        return Deferred(promise, request);
@@ -108,6 +108,35 @@
function checkVersionInit() {
    checkVersion();
    setInterval(checkVersion, 3600000);
}
/*******
 * DOM *
 *******/
function deleteChildren(element) {
    while(element.lastChild) element.removeChild(element.lastChild);
}
function addElementWithText(parent, element, text) {
    var elem = document.createElement(element);
    elem.appendChild(document.createTextNode(text));
    parent.appendChild(elem);
    return elem;
}
function addCellWithText(parent, text) {
    return addElementWithText(parent, 'td', text);
}
function addParaWithText(parent, text) {
    return addElementWithText(parent, 'p', text);
}
function setText(element, text) {
    deleteChildren(element);
    element.appendChild(document.createTextNode(text));
}
@@ -189,14 +218,15 @@
}
function depotIdToVehicleId(depotId, typeHelper) {
    var prop;
    if(typeHelper) {
        for(var prop in vehicles_info) {
        for(prop in vehicles_info) {
            if(prop.substr(0,1) == typeHelper && vehicles_info[prop]['num'].substr(2) == depotId) {
                return prop;
            }
        }
    } else {
        for(var prop in vehicles_info) {
        for(prop in vehicles_info) {
            if(vehicles_info[prop]['num'] == depotId) {
                return prop;
            }
@@ -228,33 +258,4 @@
        .replace('$floor', floor_type);
    
    return span;
}
/*******
 * DOM *
 *******/
function deleteChildren(element) {
    while(element.lastChild) element.removeChild(element.lastChild);
}
function addElementWithText(parent, element, text) {
    var elem = document.createElement(element);
    elem.appendChild(document.createTextNode(text));
    parent.appendChild(elem);
    return elem;
}
function addCellWithText(parent, text) {
    return addElementWithText(parent, 'td', text);
}
function addParaWithText(parent, text) {
    return addElementWithText(parent, 'p', text);
}
function setText(element, text) {
    deleteChildren(element);
    element.appendChild(document.createTextNode(text));
}
index.css
@@ -86,6 +86,8 @@
/* Loading animation */
nav.loading {
    background-image: -webkit-linear-gradient(45deg,rgba(0,0,0,.1) 25%,transparent 25%,transparent 50%,rgba(0,0,0,.1) 50%,rgba(0,0,0,.1) 75%,transparent 75%,transparent);
    background-image: -moz-linear-gradient(45deg,rgba(0,0,0,.1) 25%,transparent 25%,transparent 50%,rgba(0,0,0,.1) 50%,rgba(0,0,0,.1) 75%,transparent 75%,transparent);
    background-image: -ms-linear-gradient(45deg,rgba(0,0,0,.1) 25%,transparent 25%,transparent 50%,rgba(0,0,0,.1) 50%,rgba(0,0,0,.1) 75%,transparent 75%,transparent);
    background-image: -o-linear-gradient(45deg,rgba(0,0,0,.1) 25%,transparent 25%,transparent 50%,rgba(0,0,0,.1) 50%,rgba(0,0,0,.1) 75%,transparent 75%,transparent);
    background-image: linear-gradient(45deg,rgba(0,0,0,.1) 25%,transparent 25%,transparent 50%,rgba(0,0,0,.1) 50%,rgba(0,0,0,.1) 75%,transparent 75%,transparent);
    -webkit-background-size: 40px 40px;
index.js
@@ -1,4 +1,4 @@
"use strict";
'use strict';
var ttss_refresh = 20000; // 20 seconds
@@ -76,6 +76,98 @@
function loading_end() {
    nav.className = nav.className.replace(' loading', '');
}
function startTimer(date) {
    if(date) {
        setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now));
        refresh_time = date;
    }
    if(!refresh_time) return;
    if(refresh_timer) clearInterval(refresh_timer);
    var now = new Date();
    var ms = now.getTime() - refresh_time.getTime();
    var interval = 1000;
    if(ms >= 120000) interval = 60000;
    refresh_timer = setInterval(function() {
        var now = new Date();
        var ms = now.getTime() - refresh_time.getTime();
        if(ms >= 120000) {
            setText(refresh_text, lang.last_refreshed.replace(
                '$time',
                lang.time_minutes_ago_prefix + Math.floor(ms / 60000)
                    + lang.time_minutes_ago_suffix
            ));
            startTimer();
        } else {
            setText(refresh_text, lang.last_refreshed.replace(
                '$time',
                lang.time_seconds_ago_prefix + Math.floor(ms / 1000)
                    + lang.time_seconds_ago_suffix
            ));
        }
    }, interval);
}
function loadRoute(tripId, vehicleInfo) {
    if(!tripId) tripId = route_id;
    if(!tripId) return;
    if(vehicleInfo === undefined) vehicleInfo = route_vehicle_info;
    console.log('loadRoute(' + tripId + ')');
    var prefix = tripId.substr(0, 1);
    var trip = tripId.substr(1);
    route_id = tripId;
    route_vehicle_info = vehicleInfo;
    if(route_xhr) route_xhr.abort();
    route_xhr = $.get(
        ttss_urls[prefix] + '/services/tripInfo/tripPassages'
            + '?tripId=' + encodeURIComponent(trip)
            + '&mode=departure'
    ).done(function(data) {
        if(!data.routeName || !data.directionText || data.old.length + data.actual.length == 0) {
            route_id = null;
            return;
        }
        setText(route_line, data.routeName + ' ' + data.directionText);
        deleteChildren(route_vehicle);
        if(vehicleInfo) {
            var span = displayVehicle(vehicleInfo);
            if(span) {
                setText(route_vehicle, span.title);
            }
            route_vehicle.insertBefore(span, route_vehicle.firstChild);
        }
        deleteChildren(route_table);
        var all_departures = data.old.concat(data.actual);
        var tr;
        for(var i = 0, il = all_departures.length; i < il; i++) {
            tr = document.createElement('tr');
            addCellWithText(tr, all_departures[i].actualTime || all_departures[i].plannedTime);
            addCellWithText(tr, all_departures[i].stop_seq_num + '. ' + all_departures[i].stop.name);
            if(i < data.old.length) {
                tr.className = 'active';
            } else if(all_departures[i].status === 'STOPPING') {
                tr.className = 'success';
            }
            tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + all_departures[i].stop.shortName) );
            route_table.appendChild(tr);
        }
    }).fail(fail_ajax);
    return route_xhr;
}
function loadTimes(stopId) {
@@ -189,98 +281,6 @@
    return times_xhr;
}
function loadRoute(tripId, vehicleInfo) {
    if(!tripId) tripId = route_id;
    if(!tripId) return;
    if(vehicleInfo === undefined) vehicleInfo = route_vehicle_info;
    console.log('loadRoute(' + tripId + ')');
    var prefix = tripId.substr(0, 1);
    var trip = tripId.substr(1);
    route_id = tripId;
    route_vehicle_info = vehicleInfo;
    if(route_xhr) route_xhr.abort();
    route_xhr = $.get(
        ttss_urls[prefix] + '/services/tripInfo/tripPassages'
            + '?tripId=' + encodeURIComponent(trip)
            + '&mode=departure'
    ).done(function(data) {
        if(!data.routeName || !data.directionText || data.old.length + data.actual.length == 0) {
            route_id = null;
            return;
        }
        setText(route_line, data.routeName + ' ' + data.directionText);
        deleteChildren(route_vehicle);
        if(vehicleInfo) {
            var span = displayVehicle(vehicleInfo);
            if(span) {
                setText(route_vehicle, span.title);
            }
            route_vehicle.insertBefore(span, route_vehicle.firstChild);
        }
        deleteChildren(route_table);
        var all_departures = data.old.concat(data.actual);
        var tr;
        for(var i = 0, il = all_departures.length; i < il; i++) {
            tr = document.createElement('tr');
            addCellWithText(tr, all_departures[i].actualTime || all_departures[i].plannedTime);
            addCellWithText(tr, all_departures[i].stop_seq_num + '. ' + all_departures[i].stop.name);
            if(i < data.old.length) {
                tr.className = 'active';
            } else if(all_departures[i].status === 'STOPPING') {
                tr.className = 'success';
            }
            tr.addEventListener('click', function(stopId){ return function(){ loadTimes(stopId); } }(prefix + all_departures[i].stop.shortName) );
            route_table.appendChild(tr);
        }
    }).fail(fail_ajax);
    return route_xhr;
}
function startTimer(date) {
    if(date) {
        setText(refresh_text, lang.last_refreshed.replace('$time', lang.time_now));
        refresh_time = date;
    }
    if(!refresh_time) return;
    if(refresh_timer) clearInterval(refresh_timer);
    var now = new Date();
    var ms = now.getTime() - refresh_time.getTime();
    var interval = 1000;
    if(ms >= 120000) interval = 60000;
    refresh_timer = setInterval(function() {
        var now = new Date();
        var ms = now.getTime() - refresh_time.getTime();
        if(ms >= 120000) {
            setText(refresh_text, lang.last_refreshed.replace(
                '$time',
                lang.time_minutes_ago_prefix + Math.floor(ms / 60000)
                    + lang.time_minutes_ago_suffix
            ));
            startTimer();
        } else {
            setText(refresh_text, lang.last_refreshed.replace(
                '$time',
                lang.time_seconds_ago_prefix + Math.floor(ms / 1000)
                    + lang.time_seconds_ago_suffix
            ));
        }
    }, interval);
}
function translate() {
    var elements = document.querySelectorAll('*[data-translate]');
    
@@ -343,23 +343,24 @@
        return;
    }
    
    var stop;
    if(window.location.hash.match(/^#![0-9]+$/)) {
        loadTimes('t' + window.location.hash.substr(2));
    } else if(window.location.hash.match(/^#![bt][0-9]+$/)) {
        loadTimes(window.location.hash.substr(2));
    } else if(window.location.hash.match(/^#![a-z]{2}[0-9]*$/)) {
        var stop = 't' + window.location.hash.substr(4);
        if(stop) stop_id = stop;
        stop = window.location.hash.substr(4);
        if(stop) stop_id = 't' + stop;
        
        if(!change_language(window.location.hash.substr(2, 2))) {
            loadTimes(stop);
            loadTimes();
        }
    } else if(window.location.hash.match(/^#![a-z]{2}[bt][0-9]*$/)) {
        var stop = window.location.hash.substr(4);
        stop = window.location.hash.substr(4);
        if(stop) stop_id = stop;
        
        if(!change_language(window.location.hash.substr(2, 2))) {
            loadTimes(stop);
            loadTimes();
        }
    }
}
@@ -384,11 +385,11 @@
}
function init() {
    lang_select.addEventListener('input', function(e) {
    lang_select.addEventListener('input', function() {
        change_language(lang_select.value);
    });
    
    stop_name.addEventListener('input', function(e) {
    stop_name.addEventListener('input', function() {
        if(!stop_name.value) return;
        if(stop_name_autocomplete_timer) clearTimeout(stop_name_autocomplete_timer);
        
map.js
@@ -1,4 +1,4 @@
"use strict";
'use strict';
var ttss_refresh = 10000; // 10 seconds
var ttss_position_type = 'RAW';
@@ -228,7 +228,6 @@
    
    var feature, prefix;
    for(var i = 0; i < stops.length; i++) {
        feature = null;
        if(stops[i].getId) {
            feature = stops[i];
        } else {
@@ -310,8 +309,9 @@
function updateStopSource(stops, prefix) {
    var source = stops_source[prefix];
    var mapping = stops_mapping[prefix];
    var stop;
    for(var i = 0; i < stops.length; i++) {
        var stop = stops[i];
        stop = stops[i];
        
        if(stop.category == 'other') continue;
        if(stops_ignored.includes(stop.shortName)) continue;
@@ -347,7 +347,7 @@
    }).fail(fail_ajax);
}
function vehiclePath(feature, tripId) {
function vehiclePath(feature) {
    if(path_xhr) path_xhr.abort();
    
    var featureId = feature.getId();
@@ -484,11 +484,8 @@
        return;
    }
    
    var coordinates = feature.getGeometry().getCoordinates();
    var div = document.createElement('div');
    
    var typeName;
    var name = normalizeName(feature.get('name'));
    var additional;
    var table = document.createElement('table');
@@ -628,6 +625,8 @@
        if(feature.getId()) features.push(feature);
    });
    
    var feature = features[0];
    if(features.length > 1) {
        featureClicked();
        
@@ -635,7 +634,7 @@
        
        addParaWithText(div, lang.select_feature);
        
        var feature, p, a, full_type, typeName;
        var p, a, full_type, typeName;
        for(var i = 0; i < features.length; i++) {
            feature = features[i];
            
@@ -664,7 +663,6 @@
        return;
    }
    
    var feature = features[0];
    if(!feature) {
        stops_type.forEach(function(type) {
            if(stops_layer[type].getVisible()) {
@@ -914,7 +912,7 @@
    });
    
    // Change layer visibility on zoom
    var change_resolution = function(e) {
    var change_resolution = function() {
        stops_type.forEach(function(type) {
            if(type.startsWith('p')) {
                stops_layer[type].setVisible(map.getView().getZoom() >= 16);