src/js/serializers/JSONSerializer.js
author hamidouk
Wed, 26 Oct 2011 11:32:21 +0200
branchpopcorn-port
changeset 147 955119f901b4
parent 137 ef6c1252c459
child 149 a10198c95808
permissions -rw-r--r--
added a function to search annotations to the json serializer.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
108
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     1
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     2
IriSP.JSONSerializer = function(DataLoader, url) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     3
  IriSP.Serializer.call(this, DataLoader, url);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     4
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     5
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
     6
IriSP.JSONSerializer.prototype = new IriSP.Serializer();
108
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     7
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     8
IriSP.JSONSerializer.prototype.serialize = function(data) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
     9
  return JSON.stringify(data);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    10
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    11
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    12
IriSP.JSONSerializer.prototype.deserialize = function(data) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    13
  return JSON.parse(data);
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    14
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    15
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    16
IriSP.JSONSerializer.prototype.sync = function(callback) {
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    17
  /* we don't have to do much because jQuery handles json for us */
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    18
137
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    19
  var self = this;
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    20
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    21
  var fn = function(data) {      
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    22
      self._data = data;      
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    23
      // sort the data too     
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    24
      self._data["annotations"].sort(function(a, b) 
108
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    25
          { var a_begin = +a.begin;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    26
            var b_begin = +b.begin;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    27
            return a_begin - b_begin;
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    28
          });
137
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    29
     
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    30
      callback(data);      
108
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    31
  };
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    32
  
137
ef6c1252c459 clarified the closure code.
hamidouk
parents: 128
diff changeset
    33
  this._DataLoader.get(this._url, fn);
108
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    34
};
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    35
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    36
IriSP.JSONSerializer.prototype.currentMedia = function() {  
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    37
  return this._data.medias[0]; /* FIXME: don't hardcode it */
62da43e72e30 broke the serializers across multiple files. added a newline to the end of
hamidouk
parents:
diff changeset
    38
};
147
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    39
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    40
/* this function searches for an annotation which matches title, description and keyword 
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    41
   "" matches any field. 
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    42
*/    
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    43
IriSP.JSONSerializer.prototype.searchAnnotations = function(title, description, keyword) {
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    44
    var rTitle;
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    45
    var rDescription;
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    46
    var rKeyword;
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    47
    
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    48
    /* match anything if given the empty string */
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    49
    if (title == "")
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    50
      title = ".*";
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    51
    if (description == "")
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    52
      description = ".*";
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    53
    if (keyword == "")
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    54
      keyword = ".*";
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    55
    
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    56
    rTitle = new RegExp(title, "i");  
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    57
    rDescription = new RegExp(description, "i");  
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    58
    rKeyword = new RegExp(keyword, "i");  
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    59
    
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    60
    var ret_array = [];
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    61
    
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    62
    var i;
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    63
    for (i in this._data.annotations) {
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    64
      var annotation = this._data.annotations[i];
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    65
      
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    66
      if (rTitle.test(annotation.content.title) && 
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    67
          rDescription.test(annotation.content.description)) {
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    68
          /* FIXME : implement keyword support */
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    69
          ret_array.push(annotation);
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    70
      }
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    71
    }
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    72
    
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    73
    return ret_array;
955119f901b4 added a function to search annotations to the json serializer.
hamidouk
parents: 137
diff changeset
    74
}