Browse Source

Global hackerspaces object holding data

Rorist 6 years ago
parent
commit
c74d244400
1 changed files with 21 additions and 16 deletions
  1. 21 16
      site/script/main.js

+ 21 - 16
site/script/main.js

@@ -1,12 +1,15 @@
+hackerspaces = {};
+
 $(document).ready(loadmap)
 
 function loadmap(){
     map = new OpenLayers.Map("map");
     map.addLayer(new OpenLayers.Layer.OSM());
     $.getJSON('list', function(data){
-        loadMarker(map, data);
-        createMenu(data);
-        loadByHash(data);
+        hackerspaces = data;
+        loadMarker(map);
+        createMenu();
+        loadByHash();
     });
     if (typeof String.prototype.startsWith != 'function') {
        //see below for better implementation!
@@ -38,7 +41,7 @@ function getPosition(data){
 function loadMarker(map, data) {
     var markersLayer = new OpenLayers.Layer.Markers("Markers");
     map.addLayer(markersLayer);
-    $.each(data, function(key, value) {
+    $.each(hackerspaces, function(key, value) {
         var size = new OpenLayers.Size(25,25);
         var offset = new OpenLayers.Pixel(-(size.w/2), -(size.h/2));
         var icon = new OpenLayers.Icon('images/hs-noinfo-marker.png', size, offset);
@@ -52,7 +55,7 @@ function loadMarker(map, data) {
         //accordingly
         var status_url = value.space_url;
         if (status_url) {
-            getStatus(status_url, marker);
+            getSpaceApiData(status_url, marker);
         }
     });
     map.zoomToExtent(markersLayer.getDataExtent());
@@ -64,7 +67,7 @@ function loadMarker(map, data) {
     var min_dist = 100000000;
 
     //display the nearest hackerspace
-    $.each(data, function(key, value){
+    $.each(hackerspaces, function(key, value){
         var lonLat = getPosition(value);
         var d = distance(lonLat, center);
         if (d < min_dist) {
@@ -76,11 +79,11 @@ function loadMarker(map, data) {
     populateData(min_key, min_value);
 }
 
-function populateData(key, data){
+function populateData(key){
     var hsdata = $('#hsdata');
     $("#hsname").text(key);
 
-    var logo = data.logo
+    var logo = hackerspaces[key].logo
     var logo_img = hsdata.find('#hslogo')
     if (logo) {
         logo_img.attr('src', logo);
@@ -91,7 +94,7 @@ function populateData(key, data){
     }
     var dl = hsdata.children('dl');
     dl.empty()
-    $.each(data, function(key, value){
+    $.each(hackerspaces[key], function(key, value){
         var dt = $('<dt>');
         dt.text(key.capitalize())
         dl.append(dt);
@@ -110,23 +113,25 @@ function populateData(key, data){
     });
 }
 
-function getStatus(url, marker) {
+function getSpaceApiData(url, marker) {
     $.getJSON(url, function(space_api) {
-        //set the icon according to the cursor
+        //set the status icon
         var open = space_api.open;
         if (open === true) {
             marker.setUrl('images/hs-open-marker.png');
         } else if (open === false) {
             marker.setUrl('images/hs-closed-marker.png');
         }
+        // Merge SpaceApi data
+        console.log(marker);
     });
 }
 
-function createMenu(data){
+function createMenu(){
     var menu = $('#hslist');
     var ul = $('<ul>');
     menu.children().replaceWith(ul);
-    $.each(data, function(k, v){
+    $.each(hackerspaces, function(k, v){
         var a = $('<a>');
         a.attr({'href': '#'+k})
         a.click(function(){
@@ -140,11 +145,11 @@ function createMenu(data){
     });
 }
 
-function loadByHash(data){
+function loadByHash(){
     var hash = window.location.hash;
     if(hash){
         var key = hash.split('#')[1];
-        populateData(key, data[key]);
-        map.setCenter(getPosition(data[key]), 13);
+        populateData(key, hackerspaces[key]);
+        map.setCenter(getPosition(hackerspaces[key]), 13);
     }
 }