src/js/serializers/JSONSerializer.js
branchpopcorn-port
changeset 528 a5894f09825a
parent 513 4589b90fe1ad
child 536 b7e545e35287
equal deleted inserted replaced
527:90d77cc01ec0 528:a5894f09825a
     1 
     1 /** @class This class implement a serializer for the JSON-Cinelab format
       
     2     @params DataLoader a dataloader reference
       
     3     @url the url from which to get our cinelab
       
     4  */
     2 IriSP.JSONSerializer = function(DataLoader, url) {
     5 IriSP.JSONSerializer = function(DataLoader, url) {
     3   IriSP.Serializer.call(this, DataLoader, url);
     6   IriSP.Serializer.call(this, DataLoader, url);
     4 };
     7 };
     5 
     8 
     6 IriSP.JSONSerializer.prototype = new IriSP.Serializer();
     9 IriSP.JSONSerializer.prototype = new IriSP.Serializer();
     7 
    10 
       
    11 /** serialize data */
     8 IriSP.JSONSerializer.prototype.serialize = function(data) {
    12 IriSP.JSONSerializer.prototype.serialize = function(data) {
     9   return JSON.stringify(data);
    13   return JSON.stringify(data);
    10 };
    14 };
    11 
    15 
       
    16 /** deserialize data */
    12 IriSP.JSONSerializer.prototype.deserialize = function(data) {
    17 IriSP.JSONSerializer.prototype.deserialize = function(data) {
    13   return JSON.parse(data);
    18   return JSON.parse(data);
    14 };
    19 };
    15 
    20 
       
    21 /** load JSON-cinelab data and also sort the annotations by start time
       
    22     @param callback function to call when the data is ready.
       
    23  */
    16 IriSP.JSONSerializer.prototype.sync = function(callback) {
    24 IriSP.JSONSerializer.prototype.sync = function(callback) {
    17   /* we don't have to do much because jQuery handles json for us */
    25   /* we don't have to do much because jQuery handles json for us */
    18 
    26 
    19   var self = this;
    27   var self = this;
    20 
    28 
    31   };
    39   };
    32   
    40   
    33   this._DataLoader.get(this._url, fn);
    41   this._DataLoader.get(this._url, fn);
    34 };
    42 };
    35 
    43 
       
    44 /** @return the metadata about the media being read FIXME: always return the first media. */
    36 IriSP.JSONSerializer.prototype.currentMedia = function() {  
    45 IriSP.JSONSerializer.prototype.currentMedia = function() {  
    37   return this._data.medias[0]; /* FIXME: don't hardcode it */
    46   return this._data.medias[0]; /* FIXME: don't hardcode it */
    38 };
    47 };
    39 
    48 
    40 /* this function searches for an annotation which matches title, description and keyword 
    49 /** searches for an annotation which matches title, description and keyword 
    41    "" matches any field. 
    50    "" matches any field. 
    42    Note: it ignores tweets.
    51    Note: it ignores tweets.
       
    52    @return a list of matching ids.
    43 */    
    53 */    
    44 IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
    54 IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
    45     /* we can have many types of annotations. We want search to only look for regular segments */
    55     /* we can have many types of annotations. We want search to only look for regular segments */
    46     /* the next two lines are a bit verbose because for some test data, _serializer.data.view is either
    56     /* the next two lines are a bit verbose because for some test data, _serializer.data.view is either
    47        null or undefined.
    57        null or undefined.
   102 
   112 
   103     return this.searchAnnotationsFilter(title, description, keyword, filterfn);
   113     return this.searchAnnotationsFilter(title, description, keyword, filterfn);
   104 
   114 
   105 };
   115 };
   106 
   116 
   107 /*
   117 /**
   108   the previous function call this one, which is more general:
   118   search an annotation according to its title, description and keyword
       
   119   @param filter a function to filter the results with. Used to select between annotation types.
   109  */    
   120  */    
   110 IriSP.JSONSerializer.prototype.searchAnnotationsFilter = function(title, description, keyword, filter) {
   121 IriSP.JSONSerializer.prototype.searchAnnotationsFilter = function(title, description, keyword, filter) {
   111 
   122 
   112     var rTitle;
   123     var rTitle;
   113     var rDescription;
   124     var rDescription;
   143     }
   154     }
   144     
   155     
   145     return ret_array;
   156     return ret_array;
   146 };
   157 };
   147 
   158 
   148 /* breaks a string in words and searches each of these words. Returns an array
   159 /** breaks a string in words and searches each of these words. Returns an array
   149    of objects with the id of the annotation and its number of occurences.
   160    of objects with the id of the annotation and its number of occurences.
   150    
   161    
       
   162    @param searchString a string of words.
   151    FIXME: optimize ? seems to be n^2 in the worst case.
   163    FIXME: optimize ? seems to be n^2 in the worst case.
   152 */
   164 */
   153 IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
   165 IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
   154   var ret = { };
   166   var ret = { };
   155   var keywords = searchString.split(/\s+/);
   167   var keywords = searchString.split(/\s+/);
   176   };
   188   };
   177   
   189   
   178   return ret;
   190   return ret;
   179 };
   191 };
   180 
   192 
   181 /* breaks a string in words and searches each of these words. Returns an array
   193 /** breaks a string in words and searches each of these words. Returns an array
   182    of objects with the id of the annotation and its number of occurences.
   194    of objects with the id of the annotation and its number of occurences.
   183    
   195    
   184    FIXME: optimize ? seems to be n^2 in the worst case.
   196    FIXME: optimize ? seems to be n^2 in the worst case.
   185 */
   197 */
   186 IriSP.JSONSerializer.prototype.searchTweetsOccurences = function(searchString) {
   198 IriSP.JSONSerializer.prototype.searchTweetsOccurences = function(searchString) {
   209   };
   221   };
   210   
   222   
   211   return ret;
   223   return ret;
   212 };
   224 };
   213 
   225 
   214 /* takes the currentTime and returns all the annotations that are displayable at the moment 
   226 /** returns all the annotations that are displayable at the moment 
   215    NB: only takes account the first type of annotations - ignores tweets 
   227    NB: only takes account the first type of annotations - ignores tweets 
   216    currentTime is in seconds.
   228    currentTime is in seconds.
       
   229    
       
   230    @param currentTime the time at which we search.
   217  */
   231  */
   218 
   232 
   219 IriSP.JSONSerializer.prototype.currentAnnotations = function(currentTime) {
   233 IriSP.JSONSerializer.prototype.currentAnnotations = function(currentTime) {
   220   var view;
   234   var view;
   221   var currentTimeMs = 1000 * currentTime;
   235   var currentTimeMs = 1000 * currentTime;
   242 
   256 
   243   return ret_array;
   257   return ret_array;
   244 };
   258 };
   245 
   259 
   246 
   260 
   247 /* this function returns a list of ids of tweet lines */
   261 /** returns a list of ids of tweet lines (aka: groups in cinelab) */
   248 IriSP.JSONSerializer.prototype.getTweetIds = function() {
   262 IriSP.JSONSerializer.prototype.getTweetIds = function() {
   249   if (typeof(this._data.lists) === "undefined" || this._data.lists === null)
   263   if (typeof(this._data.lists) === "undefined" || this._data.lists === null)
   250     return [];
   264     return [];
   251 
   265 
   252   var tweetsId = [];
   266   var tweetsId = [];
   258   tweetsId = IriSP.underscore.pluck(tweets[0].items, "id-ref");
   272   tweetsId = IriSP.underscore.pluck(tweets[0].items, "id-ref");
   259 
   273 
   260   return tweetsId;
   274   return tweetsId;
   261 };
   275 };
   262 
   276 
   263 /* this function returns a list of lines which are not tweet lines */
   277 /** this function returns a list of lines which are not tweet lines */
   264 IriSP.JSONSerializer.prototype.getNonTweetIds = function() {
   278 IriSP.JSONSerializer.prototype.getNonTweetIds = function() {
   265   if (typeof(this._data.lists) === "undefined" || this._data.lists === null)
   279   if (typeof(this._data.lists) === "undefined" || this._data.lists === null)
   266     return [];
   280     return [];
   267   
   281   
   268   /* get all the ids */
   282   /* get all the ids */