helper.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. function localStorageTest() {
  52. var test = 'test'
  53. try {
  54. localStorage.setItem(test, test)
  55. localStorage.removeItem(test)
  56. return true
  57. } catch(e) {
  58. return false
  59. }
  60. }
  61. /* Helpers working with nodes */
  62. function offline(d) {
  63. return !d.flags.online
  64. }
  65. function online(d) {
  66. return d.flags.online
  67. }
  68. function has_location(d) {
  69. return "location" in d.nodeinfo &&
  70. Math.abs(d.nodeinfo.location.latitude) < 90 &&
  71. Math.abs(d.nodeinfo.location.longitude) < 180
  72. }
  73. function subtract(a, b) {
  74. var ids = {}
  75. b.forEach( function (d) {
  76. ids[d.nodeinfo.node_id] = true
  77. })
  78. return a.filter( function (d) {
  79. return !(d.nodeinfo.node_id in ids)
  80. })
  81. }
  82. /* Helpers working with links */
  83. function showDistance(d) {
  84. if (isNaN(d.distance))
  85. return
  86. return numeral(d.distance).format("0,0") + " m"
  87. }
  88. function showTq(d) {
  89. return numeral(1/d.tq).format("0%")
  90. }
  91. /* Infobox stuff (XXX: move to module) */
  92. function attributeEntry(el, label, value) {
  93. if (value === null || value == undefined)
  94. return
  95. var tr = document.createElement("tr")
  96. var th = document.createElement("th")
  97. th.textContent = label
  98. tr.appendChild(th)
  99. var td = document.createElement("td")
  100. if (typeof value == "function")
  101. value(td)
  102. else
  103. td.appendChild(document.createTextNode(value))
  104. tr.appendChild(td)
  105. el.appendChild(tr)
  106. return td
  107. }