src/js/data.js
author hamidouk
Tue, 15 Nov 2011 16:54:09 +0100
branchpopcorn-port
changeset 247 69bc26f879e6
parent 184 a4be54204b71
child 248 fc01dc03f135
permissions -rw-r--r--
activated url caching.
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
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
     7
IriSP.DataLoader.prototype.get = function(url, callback) {
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
     8
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
     9
  var base_url = url.split("&")[0]
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    10
  if (this._cache.hasOwnProperty(url)) {
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    11
    callback(this._cache[base_url]);
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    12
  } else {
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    13
    /* we need a closure because this gets lost when it's called back */
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    14
    // uncomment you don't want to use caching.
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    15
    // IriSP.jQuery.get(url, callback);
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    16
    
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    17
    IriSP.jQuery.get(url, (function(obj) {      
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    18
                               return function(data) {
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    19
                                  obj._cache[base_url] = data;      
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    20
                                  callback(obj._cache[base_url]);
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    21
                                }; 
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    22
                              })(this));
247
69bc26f879e6 activated url caching.
hamidouk
parents: 184
diff changeset
    23
    
61
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    24
       
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    25
  }
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    26
}
dc7b5ed7b02b DataLoader, first draft.
hamidouk
parents: 44
diff changeset
    27
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    28
/* the base abstract "class" */
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    29
IriSP.Serializer = function(DataLoader, url) {
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    30
  this._DataLoader = DataLoader;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    31
  this._url = url;
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    32
  this._data = [];
65
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.Serializer.prototype.serialize = function(data) { };
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    36
IriSP.Serializer.prototype.deserialize = function(data) {};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    37
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    38
IriSP.Serializer.prototype.currentMedia = function() {  
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    39
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    40
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    41
IriSP.Serializer.prototype.sync = function(callback) {  
184
a4be54204b71 fixed subtle bug (fun.apply vs fun.call) in sync.
hamidouk
parents: 174
diff changeset
    42
  callback.call(this, this._data);  
70
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    43
};
3f86d4126491 added some changes to IriSP.Serializer.
hamidouk
parents: 69
diff changeset
    44
65
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
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    49
IriSP.SerializerFactory.prototype.getSerializer = function(metadataOptions) {
65
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
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    51
     configuration - takes a metadata struct describing the metadata source
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    52
  */
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    53
  
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    54
  if (metadataOptions === undefined)
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    55
    /* return an empty serializer */
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    56
    return IriSP.Serializer("", "");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    57
            
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    58
  switch(metadataOptions.type) {
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    59
    case "json":
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    60
      return new IriSP.JSONSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    61
      break;
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    62
    
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    63
    case "dummy": /* only used for unit testing - not defined in production */
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    64
      return new IriSP.MockSerializer(this._dataloader, metadataOptions.src);
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    65
      break;
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    66
    
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    67
    case "empty":
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    68
      return new IriSP.Serializer("", "empty");
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    69
      break;
128
f3fec80dd31c renames and inheritance bug fixes.
hamidouk
parents: 108
diff changeset
    70
      
174
7a6450d251fc added a new serializer - the "empty" serializer, for those widgets who don't
hamidouk
parents: 128
diff changeset
    71
    default:      
65
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    72
      return undefined;
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    73
  }
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    74
};
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    75
6a8cae20f190 Added new unit tests and changes to the data classes.
hamidouk
parents: 61
diff changeset
    76
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    77
IriSP.getMetadata = function() {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    78
	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    79
	IriSP.jQuery.ajax({
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    80
		  dataType: IriSP.config.metadata.load,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    81
		  url:IriSP.config.metadata.src,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    82
		  success : function( json ){
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    83
		  
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    84
				IriSP.trace( "ajax", "success" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    85
				
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    86
				// START PARSING ----------------------- 
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    87
				if( json === "" ){
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    88
					alert( "Json load error" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    89
				} else {							  							  
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    90
					// # CREATE MEDIA  							//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    91
					// # JUSTE ONE PLAYER FOR THE MOMENT		//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    92
					//__IriSP.jQuery("<div></div>").appendTo("#output");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    93
					var MyMedia = new  __IriSP.Media(
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    94
														json.medias[0].id,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    95
														json.medias[0].href,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    96
														json.medias[0]['meta']['dc:duration'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    97
														json.medias[0]['dc:title'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
    98
														json.medias[0]['dc:description']);
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
					IriSP.trace( "__IriSP.MyApiPlayer",
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   101
														IriSP.config.gui.width+"   "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   102
														+ IriSP.config.gui.height + " "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   103
														+ json.medias[0].href + " "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   104
														+ json.medias[0]['meta']['dc:duration'] + " "
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   105
														+ json.medias[0]['meta']['item']['value']);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   106
					
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   107
					// Create APIplayer
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   108
					IriSP.MyApiPlayer = new __IriSP.APIplayer (
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   109
														IriSP.config.gui.width,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   110
														IriSP.config.gui.height,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   111
														json.medias[0].href,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   112
														json.medias[0]['meta']['dc:duration'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   113
														json.medias[0]['meta']['item']['value']);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   114
				
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   115
					// # CREATE THE FIRST LINE  				//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   116
					IriSP.trace( "__IriSP.init.main","__IriSP.Ligne" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   117
					IriSP.MyLdt = new __IriSP.Ligne(
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   118
														json['annotation-types'][0].id,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   119
														json['annotation-types'][0]['dc:title'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   120
														json['annotation-types'][0]['dc:description'],
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   121
														json.medias[0]['meta']['dc:duration']);			
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   122
					
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   123
					// CREATE THE TAG CLOUD 					//
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   124
					IriSP.trace( "__IriSP.init.main","__IriSP.Tags" );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   125
					IriSP.MyTags =  new __IriSP.Tags( json.tags );
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   126
				
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   127
					// CREATE THE ANNOTATIONS  				    //
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   128
					// JUSTE FOR THE FIRST TYPE   			 	//
44
1e295123f2a1 added a FIXME
hamidouk
parents: 31
diff changeset
   129
					/* FIXME: make it support more than one ligne de temps */
31
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   130
					IriSP.jQuery.each( json.annotations, function(i,item) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   131
						if (item.meta['id-ref'] == IriSP.MyLdt.id) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   132
							//__IriSP.trace("__IriSP.init.main","__IriSP.MyLdt.addAnnotation");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   133
							IriSP.MyLdt.addAnnotation(
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   134
										item.id,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   135
										item.begin,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   136
										item.end,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   137
										item.media,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   138
										item.content.title,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   139
										item.content.description,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   140
										item.content.color,
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   141
										item.tags);
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
							//MyTags.addAnnotation(item);
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
					IriSP.jQuery.each( json.lists, function(i,item) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   146
						IriSP.trace("lists","");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   147
					} );	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   148
					IriSP.jQuery.each( json.views, function(i,item) {
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   149
						IriSP.trace("views","");
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   150
					} );	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   151
				}
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   152
				// END PARSING ----------------------- //  
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   153
			
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   154
							
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   155
		}, error : function(data){
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   156
			  alert("ERROR : "+data);
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   157
		}
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   158
	  });	
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   159
cbb1425bc769 begun breaking the code across multiple files.
hamidouk
parents:
diff changeset
   160
}