helper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. function get(url) {
  2. return new Promise(function(resolve, reject) {
  3. var req = new XMLHttpRequest();
  4. req.open('GET', url);
  5. req.onload = function() {
  6. if (req.status == 200) {
  7. resolve(req.response);
  8. }
  9. else {
  10. reject(Error(req.statusText));
  11. }
  12. };
  13. req.onerror = function() {
  14. reject(Error("Network Error"));
  15. };
  16. req.send();
  17. });
  18. }
  19. function getJSON(url) {
  20. return get(url).then(JSON.parse)
  21. }
  22. function sortByKey(key, d) {
  23. return d.slice().sort( function (a, b) {
  24. return a[key] - b[key]
  25. }).reverse()
  26. }
  27. function limit(key, m, d) {
  28. return d.filter( function (d) {
  29. return d[key].isAfter(m)
  30. })
  31. }
  32. function sum(a) {
  33. return a.reduce( function (a, b) {
  34. return a + b
  35. }, 0)
  36. }
  37. function one() {
  38. return 1
  39. }
  40. function trueDefault(d) {
  41. return d === undefined ? true : d
  42. }
  43. function dictGet(dict, key) {
  44. var k = key.shift()
  45. if (!(k in dict))
  46. return null
  47. if (key.length == 0)
  48. return dict[k]
  49. return dictGet(dict[k], key)
  50. }
  51. /* Helpers working with nodes */
  52. function offline(d) {
  53. return !d.flags.online
  54. }
  55. function online(d) {
  56. return d.flags.online
  57. }
  58. function has_location(d) {
  59. return "location" in d.nodeinfo
  60. }
  61. function subtract(a, b) {
  62. var ids = {}
  63. b.forEach( function (d) {
  64. ids[d.nodeinfo.node_id] = true
  65. })
  66. return a.filter( function (d) {
  67. return !(d.nodeinfo.node_id in ids)
  68. })
  69. }
  70. /* Helpers working with links */
  71. function showDistance(d) {
  72. if (isNaN(d.distance))
  73. return
  74. return (new Intl.NumberFormat("de-DE", {maximumFractionDigits: 0}).format(d.distance)) + " m"
  75. }
  76. function showTq(d) {
  77. var opts = { maximumFractionDigits: 0 }
  78. return (new Intl.NumberFormat("de-DE", opts).format(100/d.tq)) + "%"
  79. }
  80. function linkId(d) {
  81. var ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id]
  82. return ids.sort().join("-")
  83. }
  84. /* Infobox stuff (XXX: move to module) */
  85. function attributeEntry(el, label, value) {
  86. if (value === null || value == undefined)
  87. return
  88. var tr = document.createElement("tr")
  89. var th = document.createElement("th")
  90. th.textContent = label
  91. tr.appendChild(th)
  92. var td = document.createElement("td")
  93. if (typeof value == "function")
  94. value(td)
  95. else
  96. td.appendChild(document.createTextNode(value))
  97. tr.appendChild(td)
  98. el.appendChild(tr)
  99. return td
  100. }