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