Rufus Pollock avatar Rufus Pollock committed 9793c98

[model/Note][m]: get geo lookup working on unparsed geo locations when creating a note from a summary.

* Use geonames for this (test utility function)
* Have to refactor quite heavily as now have a callback structure (plus rework parseNoteSummary and how it interacts with Note creation).

Comments (0)

Files changed (4)

       tags: [],
       location: {
         unparsed: '',
+        centroid: [null, null]
       },
       start: {
         unparsed: ''
     localStorage: new Store("note")
   });
 
+  // Create a note from a summary string.
+  // Require callback as make calls to remote services for e.g. geolocation
+  my.createNoteFromSummary = function(summary, callback) {
+    var parsed = HyperNotes.Util.parseNoteSummary(summary);
+    if (parsed.location && parsed.location.unparsed != '') {
+      HyperNotes.Util.lookupLocation(parsed.location.unparsed, function(data) {
+        parsed.location.geonames = data;
+        parsed.location.centroid = [
+          data.lng,
+          data.lat
+        ];
+        callback(new my.Note(parsed));
+      });
+    } else {
+      callback(new my.Note(parsed));
+    }
+  },
+
   my.NoteList = Backbone.Collection.extend({
     model: my.Note,
     localStorage: new Store("note")
 HyperNotes.Util = function() {
   my = {};
 
+  // Parse a summary to extract title, tags, location and start and end
   my.parseNoteSummary = function(text) {
     var result = {
       title: '',
-      tags: [],
-      location: {
-      },
-      start: {
-      },
-      end: {
-      }
+      tags: []
     };
     var ourtext = text;
     regex = / #([\w-\.]+)/;
       result.tags.push(out);
       ourtext = ourtext.replace(regex, '');
     }
-    regex = / @([^@]+)@/;
+    regex = / ?@([^@]+)@/;
     if(ourtext.search(regex)!=-1) {
       var out = ourtext.match(regex)[1];
-      result.location.unparsed = out;
+      result['location'] = { unparsed: out };
       ourtext = ourtext.replace(regex, '');
     }
-    regex = / \^([^^]+)\^/;
+    regex = / ?\^([^^]+)\^/;
     var tmp = [];
     while(ourtext.search(regex)!=-1) {
       var out = ourtext.match(regex)[1];
       ourtext = ourtext.replace(regex, '');
     }
     if (tmp.length>=1) {
-      result.start.unparsed = tmp[0];
+      result['start'] = { unparsed: tmp[0] };
     }
     if (tmp.length>=2) {
-      result.end.unparsed = tmp[1];
+      result['end'] = { unparsed: tmp[1] };
     }
 
-    result.title = ourtext;
+    result.title = $.trim(ourtext);
     return result;
   };
 
+  my.lookupLocation = function(name, callback) {
+    var geonamesApi = 'http://api.geonames.org/searchJSON?maxRows=1&username=demo&q=';
+    var queryUrl = geonamesApi + name;
+    $.getJSON(queryUrl, function(data) {
+      // strip off stuff
+      if (data.totalResultsCount >= 1) {
+        var result = data.geonames[0];
+      } else {
+        // TODO: raise error?
+      }
+      callback(result);
+    });
+  };
+
   return my;
 }();
 
       this.collection.each(this.addOne);
     },
 
-    newAttributes: function() {
-      var summary = this.input.val();
-      return HyperNotes.Util.parseNoteSummary(summary);
-    },
-
     createOnEnter: function(e) {
+      var self = this;
       // enter key
       if (e.keyCode != 13) return;
-      this.collection.create(this.newAttributes());
+      var summary = this.input.val();
+      HyperNotes.Model.createNoteFromSummary(summary, function(newNote) {
+        self.collection.add(newNote);
+      });
       this.input.val('');
     }
   });
       $.each(this.collection.models, function(idx, model) {
         model = model.toJSON();
         var timemapObj = {
+          title : model.title,
           start : model.start.unparsed,
           end : model.end.unparsed,
-          point : {
-              lat : 43.7717,
-              lon : 11.2536
+          point: {
+              lon : model.location.centroid[0],
+              lat : model.location.centroid[1]
            },
-          title : model.title,
           options : {}
-        }
+        };
         datasets[0].options.items.push(timemapObj);
       });
       return datasets;

test/test_util.js

       input: "A test note",
       output: {
         title: "A test note",
-        tags: [],
-        location: {},
-        start: {},
-        end: {}
+        tags: []
       }
     }
     , {
       input: "A test note #abc",
       output: {
         title: "A test note",
-        tags: ['abc'],
-        location: {},
-        start: {},
-        end: {}
+        tags: ['abc']
       }
     }
     , {
       input: "A test note #abc #xyz",
       output: {
         title: "A test note",
-        tags: ['abc', 'xyz'],
-        location: {},
-        start: {},
-        end: {}
+        tags: ['abc', 'xyz']
       }
     }
     , {
         tags: ['abc'],
         location: {
           unparsed: 'London'
-        },
-        start: {},
-        end: {}
+        }
+      }
+    }
+    , {
+      input: "@London@ #abc A test note",
+      output: {
+        title: "A test note",
+        tags: ['abc'],
+        location: {
+          unparsed: 'London'
+        }
       }
     }
     , {
       output: {
         title: "A test note",
         tags: ['abc'],
-        location: {},
         start: {
           unparsed: '1st January 1900'
         },
   }
 });
 
+test('lookupLocation', function() {
+  // Pause the test  
+  stop();  
+
+  utils.lookupLocation('London, UK', function(data) {
+    equals(data.geonameId, 2643743);
+  })  
+
+  utils.lookupLocation('London, Canada', function(data) {  
+    equals(data.geonameId, 6058560);
+  })  
+
+  setTimeout(function() {  
+      start();  
+  }, 1000);  
+});
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.