src/js/data.js
author hamidouk
Wed, 12 Oct 2011 11:54:26 +0200
branchpopcorn-port
changeset 65 6a8cae20f190
parent 61 dc7b5ed7b02b
child 69 3f7a2e8a948f
permissions -rw-r--r--
Added new unit tests and changes to the data classes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     1
/* data.js - this file deals with how the players gets and sends data */
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
     2
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     3
IriSP.DataLoader = function() {
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     4
  this._cache = {};
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     5
};
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     6
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     7
IriSP.DataLoader.prototype.get = function(url, callback) {  
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     8
  if (this._cache.hasOwnProperty(url)) {
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
     9
    callback(this._cache[url]);
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    10
  } else {
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    11
    /* we need a closure because this gets lost when it's called back */
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    12
    IriSP.jQuery.get(url, (function(obj) {      
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    13
                               return function(data) {
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    14
                                  obj._cache[url] = data;      
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    15
                                  callback(obj._cache[url]);
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    16
                                }; 
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    17
                              })(this));
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    18
       
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    19
  }
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    20
}
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    21
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    22
/* the base abstract "class" */
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    23
IriSP.Serializer = function(DataLoader, url) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    24
  this._DataLoader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    25
  this._url = url;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    26
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    27
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    28
IriSP.Serializer.prototype.serialize = function(data) { };
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    29
IriSP.Serializer.prototype.deserialize = function(data) {};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    30
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    31
IriSP.JSONSerializer = function(DataLoader, url) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    32
  IriSP.Serializer.call(this, DataLoader, url);
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    33
}
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    34
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    35
IriSP.JSONSerializer.prototype = IriSP.Serializer;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    36
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    37
IriSP.JSONSerializer.prototype.serialize = function(data) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    38
  return JSON.stringify(data);
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    39
};
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    40
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    41
IriSP.JSONSerializer.prototype.deserialize = function(data) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    42
  return JSON.parse(data);
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    43
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    44
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    45
IriSP.SerializerFactory = function(DataLoader) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    46
  this._dataloader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    47
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    48
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    49
IriSP.SerializerFactory.prototype.getSerializer = function(config) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    50
  /* This function returns serializer set-up with the correct
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    51
     configuration
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    52
  */
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    53
  switch(config.metadata.load) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    54
    case "json":
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    55
      return new IriSP.JSONSerializer(this._dataloader, config.metadata.src);
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    56
    default:
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    57
      return undefined;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    58
  }
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    59
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    60
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    61
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    62
IriSP.getMetadata = function() {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    63
	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    64
	IriSP.jQuery.ajax({
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    65
		  dataType: IriSP.config.metadata.load,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    66
		  url:IriSP.config.metadata.src,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    67
		  success : function( json ){
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    68
		  
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    69
				IriSP.trace( "ajax", "success" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    70
				
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    71
				// START PARSING ----------------------- 
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    72
				if( json === "" ){
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    73
					alert( "Json load error" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    74
				} else {							  							  
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    75
					// # CREATE MEDIA  							//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    76
					// # JUSTE ONE PLAYER FOR THE MOMENT		//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    77
					//__IriSP.jQuery("<div></div>").appendTo("#output");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    78
					var MyMedia = new  __IriSP.Media(
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    79
														json.medias[0].id,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    80
														json.medias[0].href,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    81
														json.medias[0]['meta']['dc:duration'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    82
														json.medias[0]['dc:title'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    83
														json.medias[0]['dc:description']);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    84
					
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    85
					IriSP.trace( "__IriSP.MyApiPlayer",
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    86
														IriSP.config.gui.width+"   "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    87
														+ IriSP.config.gui.height + " "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    88
														+ json.medias[0].href + " "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    89
														+ json.medias[0]['meta']['dc:duration'] + " "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    90
														+ json.medias[0]['meta']['item']['value']);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    91
					
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    92
					// Create APIplayer
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    93
					IriSP.MyApiPlayer = new __IriSP.APIplayer (
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    94
														IriSP.config.gui.width,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    95
														IriSP.config.gui.height,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    96
														json.medias[0].href,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    97
														json.medias[0]['meta']['dc:duration'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    98
														json.medias[0]['meta']['item']['value']);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    99
				
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   100
					// # CREATE THE FIRST LINE  				//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   101
					IriSP.trace( "__IriSP.init.main","__IriSP.Ligne" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   102
					IriSP.MyLdt = new __IriSP.Ligne(
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   103
														json['annotation-types'][0].id,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   104
														json['annotation-types'][0]['dc:title'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   105
														json['annotation-types'][0]['dc:description'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   106
														json.medias[0]['meta']['dc:duration']);			
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   107
					
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   108
					// CREATE THE TAG CLOUD 					//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   109
					IriSP.trace( "__IriSP.init.main","__IriSP.Tags" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   110
					IriSP.MyTags =  new __IriSP.Tags( json.tags );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   111
				
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   112
					// CREATE THE ANNOTATIONS  				    //
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   113
					// JUSTE FOR THE FIRST TYPE   			 	//
44
1e295123f2a1 added a FIXME
hamidouk
parents: 31
diff changeset
   114
					/* FIXME: make it support more than one ligne de temps */
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   115
					IriSP.jQuery.each( json.annotations, function(i,item) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   116
						if (item.meta['id-ref'] == IriSP.MyLdt.id) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   117
							//__IriSP.trace("__IriSP.init.main","__IriSP.MyLdt.addAnnotation");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   118
							IriSP.MyLdt.addAnnotation(
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   119
										item.id,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   120
										item.begin,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   121
										item.end,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   122
										item.media,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   123
										item.content.title,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   124
										item.content.description,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   125
										item.content.color,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   126
										item.tags);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   127
						}
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   128
							//MyTags.addAnnotation(item);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   129
					} );	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   130
					IriSP.jQuery.each( json.lists, function(i,item) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   131
						IriSP.trace("lists","");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   132
					} );	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   133
					IriSP.jQuery.each( json.views, function(i,item) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   134
						IriSP.trace("views","");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   135
					} );	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   136
				}
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   137
				// END PARSING ----------------------- //  
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   138
			
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   139
							
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   140
		}, error : function(data){
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   141
			  alert("ERROR : "+data);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   142
		}
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   143
	  });	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   144
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   145
}