Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PTHAFASTableBodyBuilder.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. "use strict";
  2. class PTHAFASTableBodyBuilder {
  3. constructor(config) {
  4. this.config = config;
  5. }
  6. getDeparturesTableBody(departures, noDepartureMessage) {
  7. let tBody = document.createElement("tbody");
  8. tBody.className = "light";
  9. if (departures.length === 0) {
  10. let row = this.getDeparturesTableNoDeparturesRow(noDepartureMessage);
  11. tBody.appendChild(row);
  12. return tBody;
  13. }
  14. let reachableCount = departures.length;
  15. let unreachableCount = departures.filter(departure => !departure.isReachable).length;
  16. departures.forEach((departure, index) => {
  17. let row = this.getDeparturesTableRow(departure, index, reachableCount, unreachableCount);
  18. tBody.appendChild(row);
  19. let nextDeparture = departures[index + 1];
  20. this.insertRulerIfNecessary(tBody, departure, nextDeparture, noDepartureMessage);
  21. });
  22. return tBody;
  23. }
  24. insertRulerIfNecessary(tBody, departure, nextDeparture, noDepartureMessage) {
  25. if (nextDeparture && !departure.isReachable && nextDeparture.isReachable) {
  26. tBody.appendChild(this.getRulerRow());
  27. }
  28. if (!departure.isReachable && !nextDeparture) {
  29. tBody.appendChild(this.getRulerRow());
  30. tBody.appendChild(this.getDeparturesTableNoDeparturesRow(noDepartureMessage));
  31. }
  32. }
  33. getTableCell(content, cssClass = "") {
  34. let cell = document.createElement("td");
  35. cell.className = cssClass;
  36. if (typeof content === "string") {
  37. cell.innerHTML = content;
  38. } else {
  39. cell.appendChild(content);
  40. }
  41. return cell;
  42. }
  43. getDeparturesTableNoDeparturesRow(noDepartureMessage) {
  44. let row = document.createElement("tr");
  45. row.className = "dimmed";
  46. let cell = document.createElement("td");
  47. cell.colSpan = 3;
  48. cell.innerHTML = noDepartureMessage;
  49. row.appendChild(cell);
  50. return row;
  51. }
  52. getDeparturesTableRow(departure, index, departuresCount, unreachableCount) {
  53. let row = document.createElement("tr");
  54. row.className = "bright";
  55. if (departure.isReachable) {
  56. row.style.opacity = this.getRowOpacity(index - unreachableCount, departuresCount);
  57. } else {
  58. row.style.opacity = this.getUnreachableRowOpacity(index, unreachableCount);
  59. }
  60. this.config.tableHeaderOrder.forEach((key) => {
  61. let cell = this.getCell(key, departure);
  62. row.appendChild(cell);
  63. });
  64. return row;
  65. }
  66. getCell(key, departure) {
  67. let cell;
  68. switch (key) {
  69. case "time":
  70. let time = departure.when;
  71. let delay = departure.delay;
  72. // Use planned time if canceled
  73. if (departure.canceled == true) time = departure.plannedWhen;
  74. // Get time cell
  75. cell = this.getTimeCell(time, delay);
  76. // Add class if canceled
  77. if (departure.canceled == true) cell.className += " pthCanceled";
  78. break;
  79. case "line":
  80. let line = departure.line.name;
  81. cell = this.getLineCell(line);
  82. break;
  83. case "direction":
  84. let direction = departure.direction;
  85. cell = this.getDirectionCell(direction);
  86. break;
  87. case "platform":
  88. let platform = departure.platform;
  89. if (platform == null) platform = departure.plannedPlatform;
  90. if (platform == null) platform = "";
  91. cell = this.getPlatformCell(platform);
  92. break;
  93. }
  94. return cell;
  95. }
  96. getTimeCell(departure, delay) {
  97. let time = this.getDisplayDepartureTime(departure, delay);
  98. let cell = document.createElement("td");
  99. if (moment(departure).isValid()) {
  100. cell.className = "pthTimeCell";
  101. cell.appendChild(document.createTextNode(time));
  102. if (this.config.showAbsoluteTime) {
  103. cell.appendChild(this.getDelaySpan(delay));
  104. }
  105. } else {
  106. cell.className = "pthTimeCanceled";
  107. cell.appendChild(document.createTextNode("Fällt aus"));
  108. }
  109. return cell;
  110. }
  111. getDelaySpan(delay) {
  112. let delaySpan = document.createElement("span");
  113. delaySpan.innerHTML = this.getDelay(delay);
  114. let cssClass = "dimmed";
  115. // +n === +n --> Test, if n is numeric
  116. if (this.config.useColorForRealtimeInfo && (+delay === +delay)) {
  117. cssClass = delay > 0 ? "pthHasDelay" : "pthIsTooEarly";
  118. }
  119. delaySpan.className = "pthDelay " + cssClass;
  120. return delaySpan;
  121. }
  122. // +n === +n --> Test, if n is numeric
  123. getDelay(delay) {
  124. if (+delay === +delay) {
  125. let sign = delay < 0 ? "-" : "+";
  126. return "&nbsp;" + sign + delay / 60 + "&nbsp;";
  127. } else {
  128. return "&nbsp; +? &nbsp;";
  129. }
  130. }
  131. getDisplayDepartureTime(when, delay) {
  132. if (this.config.showAbsoluteTime) {
  133. let time = moment(when).subtract(delay, "seconds");
  134. return time.format("LT");
  135. } else {
  136. let time = moment(when);
  137. return time.fromNow();
  138. }
  139. }
  140. getLineId(lineName) {
  141. let lineId = lineName;
  142. if (lineName.search(" ") == -1) {
  143. let lineNameWithoutSpaces = lineName.replace(/\s/g, "");
  144. let firstNumberPosition = lineNameWithoutSpaces.search(/\d/);
  145. lineId = lineNameWithoutSpaces;
  146. if (firstNumberPosition > 0) {
  147. lineId = lineNameWithoutSpaces.slice(firstNumberPosition);
  148. }
  149. } else {
  150. lineId = lineName.split(" ")[1];
  151. }
  152. return lineId;
  153. }
  154. getLineCell(lineName) {
  155. let line;
  156. if (this.config.showOnlyLineNumbers) {
  157. line = this.getLineId(lineName);
  158. } else {
  159. line = lineName;
  160. }
  161. let lineDiv = document.createElement("div");
  162. lineDiv.innerHTML = line;
  163. lineDiv.className = this.getLineCssClass(lineName) + " pthTextCenter";
  164. return this.getTableCell(lineDiv);
  165. }
  166. getLineCssClass(lineName) {
  167. if (this.config.showColoredLineSymbols) {
  168. return this.getColoredCssClass(lineName);
  169. } else {
  170. return "pthSign pthBWLineSign";
  171. }
  172. }
  173. /**
  174. * Get product of a line name
  175. *
  176. * Some HAFAS interfaces output line names with a space after the product name and
  177. * some do not. As an example: `RB50` <->` RB 50`.
  178. * This function returns the product name. In the two examples already mentioned
  179. * (`RB50` and` RB 50`) the string `RB` would be returned. If there is no product name
  180. * (if the line name starts with a digit), `undefined` is returned.
  181. *
  182. * @param {String} lineName The line name as it was delivered by the HAFAS API.
  183. * @return {String} product The product ('RB', 'S', 'U', ...).
  184. */
  185. getProduct(lineName) {
  186. let product = lineName;
  187. if (lineName.search(" ") == -1) {
  188. let lineNameWithoutSpaces = lineName.replace(/\s/g, "");
  189. let firstNumberPosition = lineNameWithoutSpaces.search(/\d/);
  190. product = lineNameWithoutSpaces;
  191. if (firstNumberPosition > 0) {
  192. product = lineNameWithoutSpaces.slice(0, firstNumberPosition);
  193. }
  194. } else {
  195. product = lineName.split(" ")[0];
  196. }
  197. return product;
  198. }
  199. /**
  200. * Get css class names
  201. *
  202. * Class names are returned depending on the line name. This enables CSS styles
  203. * to be defined on the basis of various properties.
  204. *
  205. * @param {String} lineName The linename as it was delivered by the HAFAS API.
  206. * @return {String} classNames Series of class names
  207. */
  208. getColoredCssClass(lineName) {
  209. let classNames = "pthSign";
  210. let product = this.getProduct(lineName);
  211. let dbProducts = ["IC", "ICE", "RE", "RB", "S"];
  212. let ignoreShowOnlyLineNumbers = ["IC", "ICE", "RE", "RB", "S", "U"];
  213. if (dbProducts.includes(product)) {
  214. classNames += " pthDbStandard";
  215. }
  216. if (ignoreShowOnlyLineNumbers.includes(product) && this.config.showOnlyLineNumbers) {
  217. classNames += " " + product.toLowerCase() + "WithProductName";
  218. }
  219. classNames += " " + product.toLowerCase();
  220. classNames += " " + lineName.replace(/\s/g, "").toLowerCase();
  221. return classNames;
  222. }
  223. getDirectionCell(direction) {
  224. let truncatePosition = 26;
  225. let content = this.getProcessedDirection(direction);
  226. let className = "pthDirectionCell";
  227. if (this.config.marqueeLongDirections && content.length > truncatePosition) {
  228. content = document.createElement("span");
  229. content.innerHTML = this.getProcessedDirection(direction);
  230. className += " pthMarquee";
  231. }
  232. if (!this.config.showAbsoluteTime) {
  233. className += " pthTextLeft";
  234. }
  235. return this.getTableCell(content, className);
  236. }
  237. getProcessedDirection(direction) {
  238. let replacements = this.config.replaceInDirections;
  239. let processed = direction;
  240. Object.keys(replacements).forEach((key) => {
  241. processed = processed.replace(new RegExp(key, "g"), replacements[key]);
  242. });
  243. return processed;
  244. }
  245. getPlatformCell(platform) {
  246. let className = "pthPlatformCell pthTextCenter";
  247. return this.getTableCell(platform, className);
  248. }
  249. getRowOpacity(index, departuresCount) {
  250. if (!this.config.fadeReachableDepartures) {
  251. return 1.0;
  252. }
  253. let threshold = departuresCount * this.config.fadePointForReachableDepartures;
  254. let opacity = 1;
  255. let startOpacity = 0.8;
  256. let endOpacity = 0.2;
  257. let opacityDiff = (startOpacity - endOpacity) / (departuresCount - threshold);
  258. if (index > threshold) {
  259. let fadingIndex = index - threshold;
  260. let currentOpacity = fadingIndex * opacityDiff;
  261. opacity = startOpacity - currentOpacity;
  262. }
  263. return opacity;
  264. }
  265. getUnreachableRowOpacity(index, count) {
  266. if (!this.config.fadeUnreachableDepartures) {
  267. return 1.0;
  268. }
  269. let startOpacity = 0.3;
  270. let endOpacity = 0.6;
  271. let opacityDiff = (endOpacity - startOpacity) / count;
  272. if (index + 1 === count) {
  273. return endOpacity;
  274. } else {
  275. return startOpacity + opacityDiff * index;
  276. }
  277. }
  278. getRulerRow() {
  279. let row = document.createElement("tr");
  280. let cell = document.createElement("td");
  281. cell.colSpan = 3;
  282. cell.className = "pthRulerCell";
  283. row.appendChild(cell);
  284. return row;
  285. }
  286. }