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+/); |
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; |
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 */ |