UNPKG

wiki-plugin-bikeshare

Version:
141 lines (130 loc) 4.09 kB
(function() { var bind, emit, escape, lineup, nearby, parse; escape = function(text) { return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); }; parse = function(text) { var config, line, m, _i, _len, _ref; config = { lines: [] }; _ref = text.split(/\n/); for (_i = 0, _len = _ref.length; _i < _len; _i++) { line = _ref[_i]; if (m = line.match(/^STATION *(.*)/)) { config.station = m[1]; config.lines.push("<span class=station><i>waiting for stations</i></span>"); } else if (m = line.match(/^NEARBY/)) { config.nearby = true; } else { config.lines.push(escape(line)); } } return config; }; lineup = function($item) { var candidates, div, markers, who, _i, _len; if (typeof wiki === "undefined" || wiki === null) { return [ { lat: 51.5, lon: 0.0, label: 'North Greenwich' } ]; } markers = []; candidates = $(".item:lt(" + ($('.item').index($item)) + ")"); if ((who = candidates.filter(".marker-source")).size()) { for (_i = 0, _len = who.length; _i < _len; _i++) { div = who[_i]; markers = markers.concat(div.markerData()); } } return markers; }; nearby = function(stops, stations) { var dist, dx, dy, k, keeps, q, quad, quads, r, rank, s, stop, v, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _results; keeps = {}; for (_i = 0, _len = stops.length; _i < _len; _i++) { stop = stops[_i]; quads = [[], [], [], []]; for (_j = 0, _len1 = stations.length; _j < _len1; _j++) { s = stations[_j]; s.weblink || (s.weblink = stop.weblink); dx = s.lon - stop.lon; dy = s.lat - stop.lat; quad = (dx + dy > 0 ? 2 : 0) + (dx - dy > 0 ? 1 : 0); dist = Math.abs(s.lat - stop.lat) + Math.abs(s.lon - stop.lon); quads[quad].push([dist, s]); } for (_k = 0, _len2 = quads.length; _k < _len2; _k++) { q = quads[_k]; rank = q.sort(function(a, b) { return a[0] - b[0]; }); _ref = rank.slice(0, 1); for (_l = 0, _len3 = _ref.length; _l < _len3; _l++) { r = _ref[_l]; keeps[r[1].name] = r[1]; } } } _results = []; for (k in keeps) { v = keeps[k]; _results.push(v); } return _results; }; emit = function($item, item) { var config, qualification; config = parse(item.text); $item.append("<p style=\"background-color:#eee;padding:15px; text-align:center\">\n " + (config.lines.join('<br>')) + "\n</p>"); if (config.station) { qualification = ''; $.getJSON(config.station, function(result) { var markers; config.stations = config.nearby ? (markers = lineup($item), markers.length ? (qualification = 'nearby', nearby(markers, result.data.stations)) : result.data.stations) : result.data.stations; return $item.find('.station').empty().append("" + config.stations.length + " stations " + qualification); }); } $item.addClass('marker-source'); return $item.get(0).markerData = function() { var s, _i, _len, _ref, _results; if (config.stations) { _ref = config.stations; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { s = _ref[_i]; _results.push({ lat: s.lat, lon: s.lon, label: s.name, weblink: s.weblink }); } return _results; } else { return []; } }; }; bind = function($item, item) { return $item.dblclick(function() { return wiki.textEditor($item, item); }); }; if (typeof window !== "undefined" && window !== null) { window.plugins.bikeshare = { emit: emit, bind: bind }; } if (typeof module !== "undefined" && module !== null) { module.exports = { parse: parse }; } }).call(this); //# sourceMappingURL=bikeshare.js.map