src/js/serializers/JSONSerializer.js
branchrequire-js
changeset 238 6008172a0592
parent 150 40cf046b7049
--- a/src/js/serializers/JSONSerializer.js	Mon Nov 14 16:12:13 2011 +0100
+++ b/src/js/serializers/JSONSerializer.js	Mon Nov 14 17:19:26 2011 +0100
@@ -1,107 +1,108 @@
+define(["IriSP", "data"], function() {
+  IriSP.JSONSerializer = function(DataLoader, url) {
+    IriSP.Serializer.call(this, DataLoader, url);
+  };
 
-IriSP.JSONSerializer = function(DataLoader, url) {
-  IriSP.Serializer.call(this, DataLoader, url);
-};
-
-IriSP.JSONSerializer.prototype = new IriSP.Serializer();
+  IriSP.JSONSerializer.prototype = new IriSP.Serializer();
 
-IriSP.JSONSerializer.prototype.serialize = function(data) {
-  return JSON.stringify(data);
-};
+  IriSP.JSONSerializer.prototype.serialize = function(data) {
+    return JSON.stringify(data);
+  };
 
-IriSP.JSONSerializer.prototype.deserialize = function(data) {
-  return JSON.parse(data);
-};
-
-IriSP.JSONSerializer.prototype.sync = function(callback) {
-  /* we don't have to do much because jQuery handles json for us */
-
-  var self = this;
+  IriSP.JSONSerializer.prototype.deserialize = function(data) {
+    return JSON.parse(data);
+  };
 
-  var fn = function(data) {      
-      self._data = data;      
-      // sort the data too     
-      self._data["annotations"].sort(function(a, b) 
-          { var a_begin = +a.begin;
-            var b_begin = +b.begin;
-            return a_begin - b_begin;
-          });
-     
-      callback(data);      
-  };
-  
-  this._DataLoader.get(this._url, fn);
-};
+  IriSP.JSONSerializer.prototype.sync = function(callback) {
+    /* we don't have to do much because jQuery handles json for us */
+
+    var self = this;
 
-IriSP.JSONSerializer.prototype.currentMedia = function() {  
-  return this._data.medias[0]; /* FIXME: don't hardcode it */
-};
-
-/* this function searches for an annotation which matches title, description and keyword 
-   "" matches any field. 
-*/    
-IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
-    var rTitle;
-    var rDescription;
-    var rKeyword;
+    var fn = function(data) {      
+        self._data = data;      
+        // sort the data too     
+        self._data["annotations"].sort(function(a, b) 
+            { var a_begin = +a.begin;
+              var b_begin = +b.begin;
+              return a_begin - b_begin;
+            });
+       
+        callback(data);      
+    };
     
-    /* match anything if given the empty string */
-    if (title == "")
-      title = ".*";
-    if (description == "")
-      description = ".*";
-    if (keyword == "")
-      keyword = ".*";
-    
-    rTitle = new RegExp(title, "i");  
-    rDescription = new RegExp(description, "i");  
-    rKeyword = new RegExp(keyword, "i");  
-    
-    var ret_array = [];
-    
-    var i;
-    for (i in this._data.annotations) {
-      var annotation = this._data.annotations[i];
+    this._DataLoader.get(this._url, fn);
+  };
+
+  IriSP.JSONSerializer.prototype.currentMedia = function() {  
+    return this._data.medias[0]; /* FIXME: don't hardcode it */
+  };
+
+  /* this function searches for an annotation which matches title, description and keyword 
+     "" matches any field. 
+  */    
+  IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
+      var rTitle;
+      var rDescription;
+      var rKeyword;
       
-      if (rTitle.test(annotation.content.title) && 
-          rDescription.test(annotation.content.description)) {
-          /* FIXME : implement keyword support */
-          ret_array.push(annotation);
-      }
-    }
-    
-    return ret_array;
-};
-
-/* breaks a string in words and searches each of these words. Returns an array
-   of objects with the id of the annotation and its number of occurences.
-   
-   FIXME: optimize ? seems to be n^2 in the worst case.
-*/
-IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
-  var ret = { };
-  var keywords = searchString.split(/\s+/);
-  
-  for (var i in keywords) {
-    var keyword = keywords[i];
-    
-    // search this keyword in descriptions and title
-    var found_annotations = []
-    found_annotations = found_annotations.concat(this.searchAnnotations(keyword, "", ""));
-    found_annotations = found_annotations.concat(this.searchAnnotations("", keyword, ""));
-    
-    for (var j in found_annotations) {
-      var current_annotation = found_annotations[j];
+      /* match anything if given the empty string */
+      if (title == "")
+        title = ".*";
+      if (description == "")
+        description = ".*";
+      if (keyword == "")
+        keyword = ".*";
+      
+      rTitle = new RegExp(title, "i");  
+      rDescription = new RegExp(description, "i");  
+      rKeyword = new RegExp(keyword, "i");  
       
-      if (!ret.hasOwnProperty(current_annotation.id)) {
-        ret[current_annotation.id] = 1;
-      } else {
-        ret[current_annotation.id] += 1;
+      var ret_array = [];
+      
+      var i;
+      for (i in this._data.annotations) {
+        var annotation = this._data.annotations[i];
+        
+        if (rTitle.test(annotation.content.title) && 
+            rDescription.test(annotation.content.description)) {
+            /* FIXME : implement keyword support */
+            ret_array.push(annotation);
+        }
       }
       
-    }
-
+      return ret_array;
   };
-  
-  return ret;
-};
\ No newline at end of file
+
+  /* breaks a string in words and searches each of these words. Returns an array
+     of objects with the id of the annotation and its number of occurences.
+     
+     FIXME: optimize ? seems to be n^2 in the worst case.
+  */
+  IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
+    var ret = { };
+    var keywords = searchString.split(/\s+/);
+    
+    for (var i in keywords) {
+      var keyword = keywords[i];
+      
+      // search this keyword in descriptions and title
+      var found_annotations = []
+      found_annotations = found_annotations.concat(this.searchAnnotations(keyword, "", ""));
+      found_annotations = found_annotations.concat(this.searchAnnotations("", keyword, ""));
+      
+      for (var j in found_annotations) {
+        var current_annotation = found_annotations[j];
+        
+        if (!ret.hasOwnProperty(current_annotation.id)) {
+          ret[current_annotation.id] = 1;
+        } else {
+          ret[current_annotation.id] += 1;
+        }
+        
+      }
+
+    };
+    
+    return ret;
+  };
+});
\ No newline at end of file