-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-vasttrafik.js
123 lines (110 loc) · 4.41 KB
/
jquery-vasttrafik.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* jquery-vasttrafik v1.2
* (c) 2015-2021 Joel Ericsson
* https://github.com/joel-eriksson/jquery-vasttrafik/blob/master/LICENSE
*/
(function($) {
$.fn.vasttrafik = function(options, callback) {
var target = this;
var api;
var response = {};
var defaults = {
url: "",
stopId: "",
method: "GET",
crossDomain: true,
cache: false,
async: true,
departureNow: "now",
departureNone: "No departures",
lineColors: {}
};
var settings = $.extend({}, defaults, options);
function timeDiff(fromUnix, toUnix) {
if (isNaN(fromUnix) || isNaN(toUnix)) {
return NaN;
}
return Math.abs(Math.floor(fromUnix - toUnix));
}
$.ajax({
url: settings.url + settings.stopId,
method: settings.method,
crossDomain: settings.crossDomain,
cache: settings.cache,
async: settings.async,
dataType: "json",
statusCode: {
200: function() {
response.statusCode = 200;
response.statusDescription = "OK";
},
401: function() {
response.statusCode = 401;
response.statusDescription = "Unauthorized";
},
403: function() {
response.statusCode = 403;
response.statusDescription = "Forbidden";
},
404: function() {
response.statusCode = 404;
response.statusDescription = "Not Found";
},
500: function() {
response.statusCode = 500;
response.statusDescription = "Internal Server Error";
},
501: function() {
response.statusCode = 501;
response.statusDescription = "Not Implemented";
},
502: function() {
response.statusCode = 502;
response.statusDescription = "Bad Gateway";
},
503: function() {
response.statusCode = 503;
response.statusDescription = "Service Unavailable";
}
},
success: function(data) {
response.success= true;
api = data;
if (api.trafik !== null) {
var serverTime = api.serverDateTime.unixTimestamp;
$(target).html("");
$.each(api.trafik, function(currentFirst, linjeinfo) {
$.each(linjeinfo.avgang, function(currentSecond, avgang) {
var nasta = timeDiff(avgang.unixTimestamp[0], serverTime) / 60;
var darefter = timeDiff(avgang.unixTimestamp[1], serverTime) / 60;
if (isNaN(darefter)) {
darefter = "-";
}
if (nasta <= 0) {
nasta = settings.departureNow;
}
if (darefter <= 0) {
darefter = settings.departureNow;
}
if (typeof settings.lineColors[linjeinfo.linje] === typeof {}) {
linjeinfo.bgFarg = settings.lineColors[linjeinfo.linje].bgColor;
linjeinfo.fgFarg = settings.lineColors[linjeinfo.linje].fgColor;
}
$(target).append("<tr><td class='linje' style='background-color: " + linjeinfo.bgFarg + "; color: " + linjeinfo.fgFarg + ";'>" + linjeinfo.linje + "</td><td> " + avgang.resmal + " </td><td class='lage'>" + avgang.lage[0] + "</td><td class='tid'>" + nasta + "</td><td class='tid'>" + darefter + "</td>");
});
});
} else {
$(target).html("<tr><td class='linje' style='background-color: #F44336; color: #FFFFFF'>-</td><td>" + settings.departureNone + "</td><td class='lage'>-</td><td class='tid'>60+</td><td class='tid'>-</td>");
}
},
error: function(error) {
response.success = false;
}
});
response.target = target;
if (typeof callback == "function") {
return callback(response);
}
return response;
};
}(jQuery));