src/js/serializers/JSONSerializer.js
branchrequire-js
changeset 238 6008172a0592
parent 150 40cf046b7049
equal deleted inserted replaced
237:8f99b0df3278 238:6008172a0592
       
     1 define(["IriSP", "data"], function() {
       
     2   IriSP.JSONSerializer = function(DataLoader, url) {
       
     3     IriSP.Serializer.call(this, DataLoader, url);
       
     4   };
     1 
     5 
     2 IriSP.JSONSerializer = function(DataLoader, url) {
     6   IriSP.JSONSerializer.prototype = new IriSP.Serializer();
     3   IriSP.Serializer.call(this, DataLoader, url);
       
     4 };
       
     5 
     7 
     6 IriSP.JSONSerializer.prototype = new IriSP.Serializer();
     8   IriSP.JSONSerializer.prototype.serialize = function(data) {
       
     9     return JSON.stringify(data);
       
    10   };
     7 
    11 
     8 IriSP.JSONSerializer.prototype.serialize = function(data) {
    12   IriSP.JSONSerializer.prototype.deserialize = function(data) {
     9   return JSON.stringify(data);
    13     return JSON.parse(data);
    10 };
    14   };
    11 
    15 
    12 IriSP.JSONSerializer.prototype.deserialize = function(data) {
    16   IriSP.JSONSerializer.prototype.sync = function(callback) {
    13   return JSON.parse(data);
    17     /* we don't have to do much because jQuery handles json for us */
    14 };
       
    15 
    18 
    16 IriSP.JSONSerializer.prototype.sync = function(callback) {
    19     var self = this;
    17   /* we don't have to do much because jQuery handles json for us */
       
    18 
    20 
    19   var self = this;
    21     var fn = function(data) {      
       
    22         self._data = data;      
       
    23         // sort the data too     
       
    24         self._data["annotations"].sort(function(a, b) 
       
    25             { var a_begin = +a.begin;
       
    26               var b_begin = +b.begin;
       
    27               return a_begin - b_begin;
       
    28             });
       
    29        
       
    30         callback(data);      
       
    31     };
       
    32     
       
    33     this._DataLoader.get(this._url, fn);
       
    34   };
    20 
    35 
    21   var fn = function(data) {      
    36   IriSP.JSONSerializer.prototype.currentMedia = function() {  
    22       self._data = data;      
    37     return this._data.medias[0]; /* FIXME: don't hardcode it */
    23       // sort the data too     
       
    24       self._data["annotations"].sort(function(a, b) 
       
    25           { var a_begin = +a.begin;
       
    26             var b_begin = +b.begin;
       
    27             return a_begin - b_begin;
       
    28           });
       
    29      
       
    30       callback(data);      
       
    31   };
    38   };
    32   
       
    33   this._DataLoader.get(this._url, fn);
       
    34 };
       
    35 
    39 
    36 IriSP.JSONSerializer.prototype.currentMedia = function() {  
    40   /* this function searches for an annotation which matches title, description and keyword 
    37   return this._data.medias[0]; /* FIXME: don't hardcode it */
    41      "" matches any field. 
    38 };
    42   */    
    39 
    43   IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
    40 /* this function searches for an annotation which matches title, description and keyword 
    44       var rTitle;
    41    "" matches any field. 
    45       var rDescription;
    42 */    
    46       var rKeyword;
    43 IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
       
    44     var rTitle;
       
    45     var rDescription;
       
    46     var rKeyword;
       
    47     
       
    48     /* match anything if given the empty string */
       
    49     if (title == "")
       
    50       title = ".*";
       
    51     if (description == "")
       
    52       description = ".*";
       
    53     if (keyword == "")
       
    54       keyword = ".*";
       
    55     
       
    56     rTitle = new RegExp(title, "i");  
       
    57     rDescription = new RegExp(description, "i");  
       
    58     rKeyword = new RegExp(keyword, "i");  
       
    59     
       
    60     var ret_array = [];
       
    61     
       
    62     var i;
       
    63     for (i in this._data.annotations) {
       
    64       var annotation = this._data.annotations[i];
       
    65       
    47       
    66       if (rTitle.test(annotation.content.title) && 
    48       /* match anything if given the empty string */
    67           rDescription.test(annotation.content.description)) {
    49       if (title == "")
    68           /* FIXME : implement keyword support */
    50         title = ".*";
    69           ret_array.push(annotation);
    51       if (description == "")
    70       }
    52         description = ".*";
    71     }
    53       if (keyword == "")
    72     
    54         keyword = ".*";
    73     return ret_array;
       
    74 };
       
    75 
       
    76 /* breaks a string in words and searches each of these words. Returns an array
       
    77    of objects with the id of the annotation and its number of occurences.
       
    78    
       
    79    FIXME: optimize ? seems to be n^2 in the worst case.
       
    80 */
       
    81 IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
       
    82   var ret = { };
       
    83   var keywords = searchString.split(/\s+/);
       
    84   
       
    85   for (var i in keywords) {
       
    86     var keyword = keywords[i];
       
    87     
       
    88     // search this keyword in descriptions and title
       
    89     var found_annotations = []
       
    90     found_annotations = found_annotations.concat(this.searchAnnotations(keyword, "", ""));
       
    91     found_annotations = found_annotations.concat(this.searchAnnotations("", keyword, ""));
       
    92     
       
    93     for (var j in found_annotations) {
       
    94       var current_annotation = found_annotations[j];
       
    95       
    55       
    96       if (!ret.hasOwnProperty(current_annotation.id)) {
    56       rTitle = new RegExp(title, "i");  
    97         ret[current_annotation.id] = 1;
    57       rDescription = new RegExp(description, "i");  
    98       } else {
    58       rKeyword = new RegExp(keyword, "i");  
    99         ret[current_annotation.id] += 1;
    59       
       
    60       var ret_array = [];
       
    61       
       
    62       var i;
       
    63       for (i in this._data.annotations) {
       
    64         var annotation = this._data.annotations[i];
       
    65         
       
    66         if (rTitle.test(annotation.content.title) && 
       
    67             rDescription.test(annotation.content.description)) {
       
    68             /* FIXME : implement keyword support */
       
    69             ret_array.push(annotation);
       
    70         }
   100       }
    71       }
   101       
    72       
   102     }
    73       return ret_array;
       
    74   };
   103 
    75 
       
    76   /* breaks a string in words and searches each of these words. Returns an array
       
    77      of objects with the id of the annotation and its number of occurences.
       
    78      
       
    79      FIXME: optimize ? seems to be n^2 in the worst case.
       
    80   */
       
    81   IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
       
    82     var ret = { };
       
    83     var keywords = searchString.split(/\s+/);
       
    84     
       
    85     for (var i in keywords) {
       
    86       var keyword = keywords[i];
       
    87       
       
    88       // search this keyword in descriptions and title
       
    89       var found_annotations = []
       
    90       found_annotations = found_annotations.concat(this.searchAnnotations(keyword, "", ""));
       
    91       found_annotations = found_annotations.concat(this.searchAnnotations("", keyword, ""));
       
    92       
       
    93       for (var j in found_annotations) {
       
    94         var current_annotation = found_annotations[j];
       
    95         
       
    96         if (!ret.hasOwnProperty(current_annotation.id)) {
       
    97           ret[current_annotation.id] = 1;
       
    98         } else {
       
    99           ret[current_annotation.id] += 1;
       
   100         }
       
   101         
       
   102       }
       
   103 
       
   104     };
       
   105     
       
   106     return ret;
   104   };
   107   };
   105   
   108 });
   106   return ret;
       
   107 };