| author | veltr |
| Fri, 09 Nov 2012 18:56:29 +0100 | |
| changeset 42 | 40909e8d6855 |
| parent 41 | 3ec2343f2b85 |
| child 43 | 5a5024bc74e6 |
| permissions | -rw-r--r-- |
| 12 | 1 |
/* TODO: Separate Project-specific data from Source */ |
2 |
||
3 |
/* model.js is where data is stored in a standard form, whatever the serializer */ |
|
| 23 | 4 |
IriSP.Model = (function (ns) { |
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
5 |
|
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
6 |
function pad(n, x, b) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
7 |
b = b || 10; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
8 |
var s = (x).toString(b); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
9 |
while (s.length < n) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
10 |
s = "0" + s; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
11 |
} |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
12 |
return s; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
13 |
} |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
14 |
|
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
15 |
function rand16(n) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
16 |
return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
17 |
} |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
18 |
|
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
19 |
var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000); |
| 12 | 20 |
|
21 |
var Model = { |
|
22 |
_SOURCE_STATUS_EMPTY : 0, |
|
23 |
_SOURCE_STATUS_WAITING : 1, |
|
24 |
_SOURCE_STATUS_READY : 2, |
|
25 |
getUID : function() { |
|
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
26 |
return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6); |
| 12 | 27 |
}, |
| 23 | 28 |
regexpFromTextOrArray : function(_textOrArray, _testOnly, _iexact) { |
29 |
var _testOnly = _testOnly || false, |
|
30 |
_iexact = _iexact || false; |
|
| 12 | 31 |
function escapeText(_text) { |
32 |
return _text.replace(/([\\\*\+\?\|\{\[\}\]\(\)\^\$\.\#\/])/gm, '\\$1'); |
|
33 |
} |
|
34 |
var _source = |
|
35 |
typeof _textOrArray === "string" |
|
36 |
? escapeText(_textOrArray) |
|
| 23 | 37 |
: ns._(_textOrArray).map(escapeText).join("|"), |
38 |
_flags = 'im'; |
|
39 |
if (!_testOnly) { |
|
40 |
_source = '(' + _source + ')'; |
|
41 |
_flags += 'g'; |
|
| 12 | 42 |
} |
| 23 | 43 |
if (_iexact) { |
44 |
_source = '^' + _source + '$'; |
|
45 |
} |
|
46 |
return new RegExp( _source, _flags); |
|
| 12 | 47 |
}, |
48 |
isoToDate : function(_str) { |
|
49 |
// http://delete.me.uk/2005/03/iso8601.html |
|
50 |
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"; |
|
51 |
var d = _str.match(new RegExp(regexp)); |
|
52 |
|
|
53 |
var offset = 0; |
|
54 |
var date = new Date(d[1], 0, 1); |
|
55 |
|
|
56 |
if (d[3]) { date.setMonth(d[3] - 1); } |
|
57 |
if (d[5]) { date.setDate(d[5]); } |
|
58 |
if (d[7]) { date.setHours(d[7]); } |
|
59 |
if (d[8]) { date.setMinutes(d[8]); } |
|
60 |
if (d[10]) { date.setSeconds(d[10]); } |
|
61 |
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } |
|
62 |
if (d[14]) { |
|
63 |
offset = (Number(d[16]) * 60) + Number(d[17]); |
|
64 |
offset *= ((d[15] == '-') ? 1 : -1); |
|
65 |
} |
|
66 |
|
|
67 |
offset -= date.getTimezoneOffset(); |
|
68 |
time = (Number(date) + (offset * 60 * 1000)); |
|
69 |
var _res = new Date(); |
|
70 |
_res.setTime(Number(time)); |
|
71 |
return _res; |
|
72 |
}, |
|
| 42 | 73 |
dateToIso : function(_d) { |
74 |
var d = _d ? new Date(_d) : new Date(); |
|
| 12 | 75 |
return d.getUTCFullYear()+'-' |
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
76 |
+ pad(2, d.getUTCMonth()+1)+'-' |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
77 |
+ pad(2, d.getUTCDate())+'T' |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
78 |
+ pad(2, d.getUTCHours())+':' |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
79 |
+ pad(2, d.getUTCMinutes())+':' |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
80 |
+ pad(2, d.getUTCSeconds())+'Z' |
| 12 | 81 |
} |
82 |
} |
|
83 |
||
84 |
/* |
|
85 |
* Model.List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID) |
|
86 |
*/ |
|
87 |
Model.List = function(_directory) { |
|
88 |
Array.call(this); |
|
89 |
this.directory = _directory; |
|
90 |
this.idIndex = []; |
|
91 |
this.__events = {}; |
|
92 |
if (typeof _directory == "undefined") { |
|
93 |
console.trace(); |
|
94 |
throw "Error : new Model.List(directory): directory is undefined"; |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
Model.List.prototype = new Array(); |
|
99 |
||
100 |
Model.List.prototype.hasId = function(_id) { |
|
101 |
return ns._(this.idIndex).include(_id); |
|
102 |
} |
|
103 |
||
104 |
/* On recent browsers, forEach and map are defined and do what we want. |
|
105 |
* Otherwise, we'll use the Underscore.js functions |
|
106 |
*/ |
|
107 |
if (typeof Array.prototype.forEach === "undefined") { |
|
108 |
Model.List.prototype.forEach = function(_callback) { |
|
109 |
var _this = this; |
|
110 |
ns._(this).forEach(function(_value, _key) { |
|
111 |
_callback(_value, _key, _this); |
|
112 |
}); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
if (typeof Array.prototype.map === "undefined") { |
|
117 |
Model.List.prototype.map = function(_callback) { |
|
118 |
var _this = this; |
|
119 |
return ns._(this).map(function(_value, _key) { |
|
120 |
return _callback(_value, _key, _this); |
|
121 |
}); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
Model.List.prototype.pluck = function(_key) { |
|
126 |
return this.map(function(_value) { |
|
127 |
return _value[_key]; |
|
128 |
}); |
|
129 |
} |
|
130 |
||
131 |
/* We override Array's filter function because it doesn't return an Model.List |
|
132 |
*/ |
|
133 |
Model.List.prototype.filter = function(_callback) { |
|
134 |
var _this = this, |
|
135 |
_res = new Model.List(this.directory); |
|
136 |
_res.addElements(ns._(this).filter(function(_value, _key) { |
|
137 |
return _callback(_value, _key, _this); |
|
138 |
})); |
|
139 |
return _res; |
|
140 |
} |
|
141 |
||
142 |
Model.List.prototype.slice = function(_start, _end) { |
|
143 |
var _res = new Model.List(this.directory); |
|
144 |
_res.addElements(Array.prototype.slice.call(this, _start, _end)); |
|
145 |
return _res; |
|
146 |
} |
|
147 |
||
148 |
Model.List.prototype.splice = function(_start, _end) { |
|
149 |
var _res = new Model.List(this.directory); |
|
150 |
_res.addElements(Array.prototype.splice.call(this, _start, _end)); |
|
151 |
this.idIndex.splice(_start, _end); |
|
152 |
return _res; |
|
153 |
} |
|
154 |
||
155 |
/* Array has a sort function, but it's not as interesting as Underscore.js's sortBy |
|
156 |
* and won't return a new Model.List |
|
157 |
*/ |
|
158 |
Model.List.prototype.sortBy = function(_callback) { |
|
159 |
var _this = this, |
|
160 |
_res = new Model.List(this.directory); |
|
161 |
_res.addElements(ns._(this).sortBy(function(_value, _key) { |
|
162 |
return _callback(_value, _key, _this); |
|
163 |
})); |
|
164 |
return _res; |
|
165 |
} |
|
166 |
||
167 |
/* Title and Description are basic information for (almost) all element types, |
|
168 |
* here we can search by these criteria |
|
169 |
*/ |
|
| 23 | 170 |
Model.List.prototype.searchByTitle = function(_text, _iexact) { |
171 |
var _iexact = _iexact || false, |
|
172 |
_rgxp = Model.regexpFromTextOrArray(_text, true); |
|
| 12 | 173 |
return this.filter(function(_element) { |
174 |
return _rgxp.test(_element.title); |
|
175 |
}); |
|
176 |
} |
|
177 |
||
| 23 | 178 |
Model.List.prototype.searchByDescription = function(_text, _iexact) { |
179 |
var _iexact = _iexact || false, |
|
180 |
_rgxp = Model.regexpFromTextOrArray(_text, true); |
|
| 12 | 181 |
return this.filter(function(_element) { |
182 |
return _rgxp.test(_element.description); |
|
183 |
}); |
|
184 |
} |
|
185 |
||
| 23 | 186 |
Model.List.prototype.searchByTextFields = function(_text, _iexact) { |
187 |
var _iexact = _iexact || false, |
|
188 |
_rgxp = Model.regexpFromTextOrArray(_text, true); |
|
| 12 | 189 |
return this.filter(function(_element) { |
190 |
return _rgxp.test(_element.description) || _rgxp.test(_element.title); |
|
191 |
}); |
|
192 |
} |
|
193 |
||
194 |
Model.List.prototype.getTitles = function() { |
|
195 |
return this.map(function(_el) { |
|
196 |
return _el.title; |
|
197 |
}); |
|
198 |
} |
|
199 |
||
200 |
Model.List.prototype.addId = function(_id) { |
|
201 |
var _el = this.directory.getElement(_id) |
|
202 |
if (!this.hasId(_id) && typeof _el !== "undefined") { |
|
203 |
this.idIndex.push(_id); |
|
204 |
Array.prototype.push.call(this, _el); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
Model.List.prototype.push = function(_el) { |
|
209 |
if (typeof _el === "undefined") { |
|
210 |
return; |
|
211 |
} |
|
212 |
var _index = (ns._(this.idIndex).indexOf(_el.id)); |
|
213 |
if (_index === -1) { |
|
214 |
this.idIndex.push(_el.id); |
|
215 |
Array.prototype.push.call(this, _el); |
|
216 |
} else { |
|
217 |
this[_index] = _el; |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
Model.List.prototype.addIds = function(_array) { |
|
222 |
var _l = _array.length, |
|
223 |
_this = this; |
|
224 |
ns._(_array).forEach(function(_id) { |
|
225 |
_this.addId(_id); |
|
226 |
}); |
|
227 |
} |
|
228 |
||
229 |
Model.List.prototype.addElements = function(_array) { |
|
230 |
var _this = this; |
|
231 |
ns._(_array).forEach(function(_el) { |
|
232 |
_this.push(_el); |
|
233 |
}); |
|
234 |
} |
|
235 |
||
236 |
Model.List.prototype.removeId = function(_id, _deleteFromDirectory) { |
|
237 |
var _deleteFromDirectory = _deleteFromDirectory || false, |
|
238 |
_index = (ns._(this.idIndex).indexOf(_id)); |
|
239 |
if (_index !== -1) { |
|
240 |
this.splice(_index,1); |
|
241 |
} |
|
242 |
if (_deleteFromDirectory) { |
|
243 |
delete this.directory.elements[_id]; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
Model.List.prototype.removeElement = function(_el, _deleteFromDirectory) { |
|
248 |
var _deleteFromDirectory = _deleteFromDirectory || false; |
|
249 |
this.removeId(_el.id); |
|
250 |
} |
|
251 |
||
252 |
Model.List.prototype.removeIds = function(_list, _deleteFromDirectory) { |
|
253 |
var _deleteFromDirectory = _deleteFromDirectory || false, |
|
254 |
_this = this; |
|
255 |
ns._(_list).forEach(function(_id) { |
|
256 |
_this.removeId(_id); |
|
257 |
}); |
|
258 |
} |
|
259 |
||
260 |
Model.List.prototype.removeElements = function(_list, _deleteFromDirectory) { |
|
261 |
var _deleteFromDirectory = _deleteFromDirectory || false, |
|
262 |
_this = this; |
|
263 |
ns._(_list).forEach(function(_el) { |
|
264 |
_this.removeElement(_el); |
|
265 |
}); |
|
266 |
} |
|
267 |
||
268 |
Model.List.prototype.on = function(_event, _callback) { |
|
269 |
if (typeof this.__events[_event] === "undefined") { |
|
270 |
this.__events[_event] = []; |
|
271 |
} |
|
272 |
this.__events[_event].push(_callback); |
|
273 |
} |
|
274 |
||
275 |
Model.List.prototype.off = function(_event, _callback) { |
|
276 |
if (typeof this.__events[_event] !== "undefined") { |
|
277 |
this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) { |
|
278 |
return _fn === _callback; |
|
279 |
}); |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
Model.List.prototype.trigger = function(_event, _data) { |
|
284 |
var _list = this; |
|
285 |
ns._(this.__events[_event]).each(function(_callback) { |
|
286 |
_callback.call(_list, _data); |
|
287 |
}); |
|
288 |
} |
|
289 |
||
290 |
/* A simple time management object, that helps converting millisecs to seconds and strings, |
|
291 |
* without the clumsiness of the original Date object. |
|
292 |
*/ |
|
293 |
||
294 |
Model.Time = function(_milliseconds) { |
|
295 |
this.milliseconds = 0; |
|
296 |
this.setMilliseconds(_milliseconds); |
|
297 |
} |
|
298 |
||
299 |
Model.Time.prototype.setMilliseconds = function(_milliseconds) { |
|
| 27 | 300 |
var _ante = this.milliseconds; |
| 12 | 301 |
switch(typeof _milliseconds) { |
302 |
case "string": |
|
| 27 | 303 |
this.milliseconds = parseInt(_milliseconds); |
| 12 | 304 |
break; |
305 |
case "number": |
|
| 27 | 306 |
this.milliseconds = Math.floor(_milliseconds); |
| 12 | 307 |
break; |
308 |
case "object": |
|
| 27 | 309 |
this.milliseconds = parseInt(_milliseconds.valueOf()); |
| 12 | 310 |
break; |
311 |
default: |
|
312 |
this.milliseconds = 0; |
|
313 |
} |
|
314 |
if (this.milliseconds === NaN) { |
|
315 |
this.milliseconds = _ante; |
|
316 |
} |
|
317 |
} |
|
318 |
||
319 |
Model.Time.prototype.setSeconds = function(_seconds) { |
|
320 |
this.milliseconds = 1000 * _seconds; |
|
321 |
} |
|
322 |
||
323 |
Model.Time.prototype.getSeconds = function() { |
|
324 |
return this.milliseconds / 1000; |
|
325 |
} |
|
326 |
||
327 |
Model.Time.prototype.getHMS = function() { |
|
328 |
var _totalSeconds = Math.abs(Math.floor(this.getSeconds())); |
|
329 |
return { |
|
330 |
hours : Math.floor(_totalSeconds / 3600), |
|
331 |
minutes : (Math.floor(_totalSeconds / 60) % 60), |
|
| 27 | 332 |
seconds : _totalSeconds % 60, |
333 |
milliseconds: this.milliseconds % 1000 |
|
| 12 | 334 |
} |
335 |
} |
|
336 |
||
337 |
Model.Time.prototype.add = function(_milliseconds) { |
|
338 |
this.milliseconds += new Model.Time(_milliseconds).milliseconds; |
|
339 |
} |
|
340 |
||
341 |
Model.Time.prototype.valueOf = function() { |
|
342 |
return this.milliseconds; |
|
343 |
} |
|
344 |
||
| 27 | 345 |
Model.Time.prototype.toString = function(showCs) { |
| 12 | 346 |
var _hms = this.getHMS(), |
347 |
_res = ''; |
|
348 |
if (_hms.hours) { |
|
| 25 | 349 |
_res += _hms.hours + ':' |
| 12 | 350 |
} |
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
351 |
_res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds); |
| 27 | 352 |
if (showCs) { |
353 |
_res += "." + Math.round(_hms.milliseconds / 100) |
|
354 |
} |
|
| 12 | 355 |
return _res; |
356 |
} |
|
357 |
||
358 |
/* Model.Reference handles references between elements |
|
359 |
*/ |
|
360 |
||
361 |
Model.Reference = function(_source, _idRef) { |
|
362 |
this.source = _source; |
|
363 |
this.id = _idRef; |
|
364 |
if (typeof _idRef === "object") { |
|
365 |
this.isList = true; |
|
366 |
} else { |
|
367 |
this.isList = false; |
|
368 |
} |
|
369 |
this.refresh(); |
|
370 |
} |
|
371 |
||
372 |
Model.Reference.prototype.refresh = function() { |
|
373 |
if (this.isList) { |
|
374 |
this.contents = new Model.List(this.source.directory); |
|
375 |
this.contents.addIds(this.id); |
|
376 |
} else { |
|
377 |
this.contents = this.source.getElement(this.id); |
|
378 |
} |
|
379 |
|
|
380 |
} |
|
381 |
||
382 |
Model.Reference.prototype.getContents = function() { |
|
383 |
if (typeof this.contents === "undefined" || (this.isList && this.contents.length != this.id.length)) { |
|
384 |
this.refresh(); |
|
385 |
} |
|
386 |
return this.contents; |
|
387 |
} |
|
388 |
||
389 |
Model.Reference.prototype.isOrHasId = function(_idRef) { |
|
390 |
if (this.isList) { |
|
391 |
return (ns._(this.id).indexOf(_idRef) !== -1) |
|
392 |
} else { |
|
393 |
return (this.id == _idRef); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
/* */ |
|
398 |
||
399 |
Model.Element = function(_id, _source) { |
|
400 |
this.elementType = 'element'; |
|
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
401 |
this.title = ""; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
402 |
this.description = ""; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
403 |
this.__events = {} |
| 12 | 404 |
if (typeof _source === "undefined") { |
405 |
return; |
|
406 |
} |
|
407 |
if (typeof _id === "undefined" || !_id) { |
|
408 |
_id = Model.getUID(); |
|
409 |
} |
|
| 42 | 410 |
this.id = _id; |
| 12 | 411 |
this.source = _source; |
| 42 | 412 |
if (_source !== this) { |
413 |
this.source.directory.addElement(this); |
|
414 |
} |
|
| 12 | 415 |
} |
416 |
||
417 |
Model.Element.prototype.toString = function() { |
|
418 |
return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : ''); |
|
419 |
} |
|
420 |
||
421 |
Model.Element.prototype.setReference = function(_elementType, _idRef) { |
|
422 |
this[_elementType] = new Model.Reference(this.source, _idRef); |
|
423 |
} |
|
424 |
||
425 |
Model.Element.prototype.getReference = function(_elementType) { |
|
426 |
if (typeof this[_elementType] !== "undefined") { |
|
427 |
return this[_elementType].getContents(); |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
Model.Element.prototype.getRelated = function(_elementType, _global) { |
|
432 |
_global = (typeof _global !== "undefined" && _global); |
|
433 |
var _this = this; |
|
434 |
return this.source.getList(_elementType, _global).filter(function(_el) { |
|
435 |
var _ref = _el[_this.elementType]; |
|
436 |
return _ref && _ref.isOrHasId(_this.id); |
|
437 |
}); |
|
438 |
} |
|
439 |
||
440 |
Model.Element.prototype.on = function(_event, _callback) { |
|
441 |
if (typeof this.__events[_event] === "undefined") { |
|
442 |
this.__events[_event] = []; |
|
443 |
} |
|
444 |
this.__events[_event].push(_callback); |
|
445 |
} |
|
446 |
||
447 |
Model.Element.prototype.off = function(_event, _callback) { |
|
448 |
if (typeof this.__events[_event] !== "undefined") { |
|
449 |
this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) { |
|
450 |
return _fn === _callback; |
|
451 |
}); |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
Model.Element.prototype.trigger = function(_event, _data) { |
|
456 |
var _element = this; |
|
457 |
ns._(this.__events[_event]).each(function(_callback) { |
|
458 |
_callback.call(_element, _data); |
|
459 |
}); |
|
460 |
} |
|
461 |
||
462 |
/* */ |
|
463 |
||
464 |
Model.Playable = function(_id, _source) { |
|
465 |
Model.Element.call(this, _id, _source); |
|
466 |
if (typeof _source === "undefined") { |
|
467 |
return; |
|
468 |
} |
|
469 |
this.elementType = 'playable'; |
|
470 |
this.currentTime = new Model.Time(); |
|
471 |
this.volume = .5; |
|
472 |
this.paused = true; |
|
473 |
this.muted = false; |
|
474 |
var _this = this; |
|
475 |
this.on("play", function() { |
|
476 |
_this.paused = false; |
|
477 |
}); |
|
478 |
this.on("pause", function() { |
|
479 |
_this.paused = true; |
|
480 |
}); |
|
481 |
this.on("timeupdate", function(_time) { |
|
482 |
_this.currentTime = _time; |
|
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
483 |
_this.getAnnotations().filter(function(_a) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
484 |
return (_a.end <= _time || _a.begin > _time) && _a.playing |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
485 |
}).forEach(function(_a) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
486 |
_a.playing = false; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
487 |
_a.trigger("leave"); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
488 |
_this.trigger("leave-annotation",_a); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
489 |
}); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
490 |
_this.getAnnotations().filter(function(_a) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
491 |
return _a.begin <= _time && _a.end > _time && !_a.playing |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
492 |
}).forEach(function(_a) { |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
493 |
_a.playing = true; |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
494 |
_a.trigger("enter"); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
495 |
_this.trigger("enter-annotation",_a); |
|
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
496 |
}); |
| 12 | 497 |
}); |
498 |
} |
|
499 |
||
500 |
Model.Playable.prototype = new Model.Element(); |
|
501 |
||
502 |
Model.Playable.prototype.getCurrentTime = function() { |
|
503 |
return this.currentTime; |
|
504 |
} |
|
505 |
||
506 |
Model.Playable.prototype.getVolume = function() { |
|
507 |
return this.volume; |
|
508 |
} |
|
509 |
||
510 |
Model.Playable.prototype.getPaused = function() { |
|
511 |
return this.paused; |
|
512 |
} |
|
513 |
||
514 |
Model.Playable.prototype.getMuted = function() { |
|
515 |
return this.muted; |
|
516 |
} |
|
517 |
||
518 |
Model.Playable.prototype.setCurrentTime = function(_time) { |
|
519 |
this.trigger("setcurrenttime",_time); |
|
520 |
} |
|
521 |
||
522 |
Model.Playable.prototype.setVolume = function(_vol) { |
|
523 |
this.trigger("setvolume",_vol); |
|
524 |
} |
|
525 |
||
526 |
Model.Playable.prototype.setMuted = function(_muted) { |
|
527 |
this.trigger("setmuted",_muted); |
|
528 |
} |
|
529 |
||
530 |
Model.Playable.prototype.play = function() { |
|
531 |
this.trigger("setplay"); |
|
532 |
} |
|
533 |
||
534 |
Model.Playable.prototype.pause = function() { |
|
535 |
this.trigger("setpause"); |
|
536 |
} |
|
537 |
||
| 27 | 538 |
Model.Playable.prototype.show = function() {} |
539 |
||
540 |
Model.Playable.prototype.hide = function() {} |
|
| 12 | 541 |
|
542 |
/* */ |
|
543 |
||
544 |
Model.Media = function(_id, _source) { |
|
545 |
Model.Playable.call(this, _id, _source); |
|
546 |
this.elementType = 'media'; |
|
547 |
this.duration = new Model.Time(); |
|
548 |
this.video = ''; |
|
549 |
var _this = this; |
|
550 |
} |
|
551 |
||
552 |
Model.Media.prototype = new Model.Playable(); |
|
553 |
||
554 |
/* Default functions to be overriden by players */ |
|
555 |
|
|
556 |
Model.Media.prototype.setDuration = function(_durationMs) { |
|
557 |
this.duration.setMilliseconds(_durationMs); |
|
558 |
} |
|
559 |
||
560 |
Model.Media.prototype.getAnnotations = function() { |
|
561 |
return this.getRelated("annotation"); |
|
562 |
} |
|
563 |
||
564 |
Model.Media.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
565 |
var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id"); |
|
566 |
if (_annTypes.length) { |
|
567 |
return this.getAnnotations().filter(function(_annotation) { |
|
568 |
return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1; |
|
569 |
}); |
|
570 |
} else { |
|
571 |
return new Model.List(this.source.directory) |
|
572 |
} |
|
573 |
} |
|
574 |
||
575 |
/* */ |
|
576 |
||
577 |
Model.Tag = function(_id, _source) { |
|
578 |
Model.Element.call(this, _id, _source); |
|
579 |
this.elementType = 'tag'; |
|
580 |
} |
|
581 |
||
582 |
Model.Tag.prototype = new Model.Element(); |
|
583 |
||
584 |
Model.Tag.prototype.getAnnotations = function() { |
|
585 |
return this.getRelated("annotation"); |
|
586 |
} |
|
587 |
||
588 |
/* */ |
|
589 |
Model.AnnotationType = function(_id, _source) { |
|
590 |
Model.Element.call(this, _id, _source); |
|
591 |
this.elementType = 'annotationType'; |
|
592 |
} |
|
593 |
||
594 |
Model.AnnotationType.prototype = new Model.Element(); |
|
595 |
||
596 |
Model.AnnotationType.prototype.getAnnotations = function() { |
|
597 |
return this.getRelated("annotation"); |
|
598 |
} |
|
599 |
||
600 |
/* Annotation |
|
601 |
* */ |
|
602 |
||
603 |
Model.Annotation = function(_id, _source) { |
|
604 |
Model.Element.call(this, _id, _source); |
|
605 |
this.elementType = 'annotation'; |
|
606 |
this.begin = new Model.Time(); |
|
607 |
this.end = new Model.Time(); |
|
608 |
this.tag = new Model.Reference(_source, []); |
|
609 |
this.playing = false; |
|
610 |
var _this = this; |
|
611 |
this.on("click", function() { |
|
612 |
_this.getMedia().setCurrentTime(_this.begin); |
|
613 |
}); |
|
614 |
} |
|
615 |
||
616 |
Model.Annotation.prototype = new Model.Element(); |
|
617 |
||
618 |
Model.Annotation.prototype.setBegin = function(_beginMs) { |
|
| 25 | 619 |
this.begin.setMilliseconds(Math.max(0,_beginMs)); |
620 |
this.trigger("change-begin"); |
|
621 |
if (this.end < this.begin) { |
|
622 |
this.setEnd(this.begin); |
|
623 |
} |
|
| 12 | 624 |
} |
625 |
||
| 25 | 626 |
Model.Annotation.prototype.setEnd = function(_endMs) { |
627 |
this.end.setMilliseconds(Math.min(_endMs, this.getMedia().duration.milliseconds)); |
|
628 |
this.trigger("change-end"); |
|
629 |
if (this.end < this.begin) { |
|
630 |
this.setBegin(this.end); |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
Model.Annotation.prototype.setDuration = function(_durMs) { |
|
635 |
this.setEnd(_durMs + this.begin.milliseconds); |
|
| 12 | 636 |
} |
637 |
||
638 |
Model.Annotation.prototype.setMedia = function(_idRef) { |
|
639 |
this.setReference("media", _idRef); |
|
640 |
} |
|
641 |
||
642 |
Model.Annotation.prototype.getMedia = function() { |
|
643 |
return this.getReference("media"); |
|
644 |
} |
|
645 |
||
646 |
Model.Annotation.prototype.setAnnotationType = function(_idRef) { |
|
647 |
this.setReference("annotationType", _idRef); |
|
648 |
} |
|
649 |
||
650 |
Model.Annotation.prototype.getAnnotationType = function() { |
|
651 |
return this.getReference("annotationType"); |
|
652 |
} |
|
653 |
||
654 |
Model.Annotation.prototype.setTags = function(_idRefs) { |
|
655 |
this.setReference("tag", _idRefs); |
|
656 |
} |
|
657 |
||
658 |
Model.Annotation.prototype.getTags = function() { |
|
659 |
return this.getReference("tag"); |
|
660 |
} |
|
661 |
||
662 |
Model.Annotation.prototype.getTagTexts = function() { |
|
663 |
return this.getTags().getTitles(); |
|
664 |
} |
|
665 |
||
666 |
Model.Annotation.prototype.getDuration = function() { |
|
667 |
return new Model.Time(this.end.milliseconds - this.begin.milliseconds) |
|
668 |
} |
|
669 |
||
670 |
/* */ |
|
671 |
||
672 |
Model.MashedAnnotation = function(_mashup, _annotation) { |
|
673 |
Model.Element.call(this, _mashup.id + "_" + _annotation.id, _annotation.source); |
|
674 |
this.elementType = 'mashedAnnotation'; |
|
675 |
this.annotation = _annotation; |
|
| 25 | 676 |
this.begin = new Model.Time(); |
677 |
this.end = new Model.Time(); |
|
678 |
this.duration = new Model.Time(); |
|
| 12 | 679 |
this.title = this.annotation.title; |
680 |
this.description = this.annotation.description; |
|
681 |
this.color = this.annotation.color; |
|
682 |
var _this = this; |
|
683 |
this.on("click", function() { |
|
684 |
_mashup.setCurrentTime(_this.begin); |
|
685 |
}); |
|
| 27 | 686 |
this.on("enter", function() { |
687 |
_this.annotation.trigger("enter"); |
|
688 |
}); |
|
689 |
this.on("leave", function() { |
|
690 |
_this.annotation.trigger("leave"); |
|
691 |
}); |
|
| 12 | 692 |
} |
693 |
||
694 |
Model.MashedAnnotation.prototype = new Model.Element(null); |
|
695 |
||
696 |
Model.MashedAnnotation.prototype.getMedia = function() { |
|
697 |
return this.annotation.getReference("media"); |
|
698 |
} |
|
699 |
||
700 |
Model.MashedAnnotation.prototype.getAnnotationType = function() { |
|
701 |
return this.annotation.getReference("annotationType"); |
|
702 |
} |
|
703 |
||
704 |
Model.MashedAnnotation.prototype.getTags = function() { |
|
705 |
return this.annotation.getReference("tag"); |
|
706 |
} |
|
707 |
||
708 |
Model.MashedAnnotation.prototype.getTagTexts = function() { |
|
709 |
return this.annotation.getTags().getTitles(); |
|
710 |
} |
|
711 |
||
712 |
Model.MashedAnnotation.prototype.getDuration = function() { |
|
713 |
return this.annotation.getDuration(); |
|
714 |
} |
|
715 |
||
| 25 | 716 |
Model.MashedAnnotation.prototype.setBegin = function(_begin) { |
717 |
this.begin.setMilliseconds(_begin); |
|
718 |
this.duration.setMilliseconds(this.annotation.getDuration()); |
|
719 |
this.end.setMilliseconds(_begin + this.duration); |
|
720 |
} |
|
721 |
||
| 12 | 722 |
/* */ |
723 |
||
724 |
Model.Mashup = function(_id, _source) { |
|
725 |
Model.Playable.call(this, _id, _source); |
|
726 |
this.elementType = 'mashup'; |
|
727 |
this.duration = new Model.Time(); |
|
728 |
this.segments = new Model.List(_source.directory); |
|
| 27 | 729 |
this.loaded = false; |
| 12 | 730 |
var _this = this; |
| 25 | 731 |
this._updateTimes = function() { |
732 |
_this.updateTimes(); |
|
| 27 | 733 |
_this.trigger("change"); |
| 25 | 734 |
} |
| 27 | 735 |
this.on("add", this._updateTimes); |
736 |
this.on("remove", this._updateTimes); |
|
| 12 | 737 |
} |
738 |
||
739 |
Model.Mashup.prototype = new Model.Playable(); |
|
740 |
||
| 27 | 741 |
Model.Mashup.prototype.checkLoaded = function() { |
742 |
var loaded = !!this.segments.length; |
|
743 |
this.getMedias().forEach(function(_m) { |
|
744 |
loaded = loaded && _m.loaded; |
|
745 |
}); |
|
746 |
this.loaded = loaded; |
|
747 |
if (loaded) { |
|
748 |
this.trigger("loadedmetadata"); |
|
749 |
} |
|
750 |
} |
|
751 |
||
| 25 | 752 |
Model.Mashup.prototype.updateTimes = function() { |
753 |
var _time = 0; |
|
754 |
this.segments.forEach(function(_segment) { |
|
755 |
_segment.setBegin(_time); |
|
756 |
_time = _segment.end; |
|
757 |
}); |
|
758 |
this.duration.setMilliseconds(_time); |
|
759 |
} |
|
760 |
||
| 27 | 761 |
Model.Mashup.prototype.addAnnotation = function(_annotation, _defer) { |
| 25 | 762 |
var _mashedAnnotation = new Model.MashedAnnotation(this, _annotation), |
763 |
_defer = _defer || false; |
|
| 12 | 764 |
this.segments.push(_mashedAnnotation); |
| 25 | 765 |
_annotation.on("change-begin", this._updateTimes); |
766 |
_annotation.on("change-end", this._updateTimes); |
|
767 |
if (!_defer) { |
|
| 27 | 768 |
this.trigger("add"); |
769 |
} |
|
770 |
} |
|
771 |
||
772 |
Model.Mashup.prototype.addAnnotationById = function(_elId, _defer) { |
|
773 |
var _annotation = this.source.getElement(_elId), |
|
774 |
_defer = _defer || false; |
|
775 |
if (typeof _annotation !== "undefined") { |
|
776 |
this.addAnnotation(_annotation, _defer); |
|
| 25 | 777 |
} |
778 |
} |
|
779 |
||
| 27 | 780 |
Model.Mashup.prototype.addAnnotations = function(_segments) { |
781 |
var _this = this; |
|
782 |
ns._(_segments).forEach(function(_segment) { |
|
783 |
_this.addAnnotation(_segment, true); |
|
784 |
}); |
|
785 |
this.trigger("add"); |
|
786 |
} |
|
787 |
||
788 |
Model.Mashup.prototype.addAnnotationsById = function(_segments) { |
|
789 |
var _this = this; |
|
790 |
ns._(_segments).forEach(function(_segment) { |
|
791 |
_this.addAnnotationById(_segment, true); |
|
792 |
}); |
|
793 |
this.trigger("add"); |
|
794 |
} |
|
795 |
||
796 |
Model.Mashup.prototype.removeAnnotation = function(_annotation, _defer) { |
|
797 |
var _defer = _defer || false; |
|
798 |
_annotation.off("change-begin", this._updateTimes); |
|
799 |
_annotation.off("change-end", this._updateTimes); |
|
800 |
this.segments.removeId(this.id + "_" + _annotation.id); |
|
801 |
if (!_defer) { |
|
802 |
this.trigger("remove"); |
|
| 25 | 803 |
} |
| 12 | 804 |
} |
805 |
||
| 27 | 806 |
Model.Mashup.prototype.removeAnnotationById = function(_annId, _defer) { |
807 |
var _defer = _defer || false; |
|
808 |
var _annotation = this.source.getElement(_annId); |
|
| 25 | 809 |
|
| 27 | 810 |
if (_annotation) { |
811 |
this.removeAnnotation(_annotation, _defer); |
|
812 |
} |
|
| 25 | 813 |
if (!_defer) { |
| 27 | 814 |
this.trigger("remove"); |
| 25 | 815 |
} |
816 |
} |
|
817 |
||
| 27 | 818 |
Model.Mashup.prototype.setAnnotations = function(_segments) { |
819 |
while (this.segments.length) { |
|
820 |
this.removeAnnotation(this.segments[0].annotation, true); |
|
| 25 | 821 |
} |
| 27 | 822 |
this.addAnnotations(_segments); |
823 |
} |
|
824 |
||
825 |
Model.Mashup.prototype.setAnnotationsById = function(_segments) { |
|
826 |
while (this.segments.length) { |
|
827 |
this.removeAnnotation(this.segments[0].annotation, true); |
|
| 12 | 828 |
} |
| 27 | 829 |
this.addAnnotationsById(_segments); |
| 12 | 830 |
} |
831 |
||
| 27 | 832 |
Model.Mashup.prototype.hasAnnotation = function(_annotation) { |
833 |
return !!ns._(this.segments).find(function(_s) { |
|
834 |
return _s.annotation === _annotation |
|
835 |
}); |
|
836 |
} |
|
837 |
||
838 |
Model.Mashup.prototype.getAnnotation = function(_annotation) { |
|
839 |
return ns._(this.segments).find(function(_s) { |
|
840 |
return _s.annotation === _annotation |
|
841 |
}); |
|
842 |
} |
|
843 |
||
844 |
Model.Mashup.prototype.getAnnotationById = function(_id) { |
|
845 |
return ns._(this.segments).find(function(_s) { |
|
846 |
return _s.annotation.id === _id |
|
847 |
}); |
|
848 |
} |
|
849 |
||
|
41
3ec2343f2b85
Refactoring to have common code between editor and player
veltr
parents:
27
diff
changeset
|
850 |
Model.Mashup.prototype.getAnnotations = function() { |
| 12 | 851 |
return this.segments; |
852 |
} |
|
853 |
||
| 42 | 854 |
Model.Mashup.prototype.getOriginalAnnotations = function() { |
855 |
var annotations = new Model.List(this.source.directory); |
|
856 |
this.segments.forEach(function(_s) { |
|
857 |
annotations.push(_s.annotation); |
|
858 |
}); |
|
859 |
return annotations; |
|
860 |
} |
|
861 |
||
| 12 | 862 |
Model.Mashup.prototype.getMedias = function() { |
| 27 | 863 |
var medias = new Model.List(this.source.directory); |
864 |
this.segments.forEach(function(_annotation) { |
|
| 25 | 865 |
medias.push(_annotation.getMedia()) |
866 |
}) |
|
867 |
return medias; |
|
| 12 | 868 |
} |
869 |
||
870 |
Model.Mashup.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
871 |
var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id"); |
|
872 |
if (_annTypes.length) { |
|
873 |
return this.getAnnotations().filter(function(_annotation) { |
|
874 |
return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1; |
|
875 |
}); |
|
876 |
} else { |
|
877 |
return new Model.List(this.source.directory) |
|
878 |
} |
|
879 |
} |
|
880 |
||
881 |
Model.Mashup.prototype.getAnnotationAtTime = function(_time) { |
|
882 |
var _list = this.segments.filter(function(_annotation) { |
|
883 |
return _annotation.begin <= _time && _annotation.end > _time; |
|
884 |
}); |
|
885 |
if (_list.length) { |
|
886 |
return _list[0]; |
|
887 |
} else { |
|
888 |
return undefined; |
|
889 |
} |
|
890 |
} |
|
891 |
||
892 |
Model.Mashup.prototype.getMediaAtTime = function(_time) { |
|
893 |
var _annotation = this.getAnnotationAtTime(_time); |
|
894 |
if (typeof _annotation !== "undefined") { |
|
895 |
return _annotation.getMedia(); |
|
896 |
} else { |
|
897 |
return undefined; |
|
898 |
} |
|
899 |
} |
|
900 |
||
901 |
/* */ |
|
902 |
||
903 |
Model.Source = function(_config) { |
|
| 42 | 904 |
Model.Element.call(this, false, this); |
| 12 | 905 |
this.status = Model._SOURCE_STATUS_EMPTY; |
906 |
this.elementType = "source"; |
|
907 |
if (typeof _config !== "undefined") { |
|
908 |
var _this = this; |
|
909 |
ns._(_config).forEach(function(_v, _k) { |
|
910 |
_this[_k] = _v; |
|
911 |
}) |
|
912 |
this.callbackQueue = []; |
|
913 |
this.contents = {}; |
|
914 |
this.get(); |
|
915 |
} |
|
916 |
} |
|
917 |
||
918 |
Model.Source.prototype = new Model.Element(); |
|
919 |
||
920 |
Model.Source.prototype.addList = function(_listId, _contents) { |
|
921 |
if (typeof this.contents[_listId] === "undefined") { |
|
922 |
this.contents[_listId] = new Model.List(this.directory); |
|
923 |
} |
|
924 |
this.contents[_listId].addElements(_contents); |
|
925 |
} |
|
926 |
||
927 |
Model.Source.prototype.getList = function(_listId, _global) { |
|
928 |
_global = (typeof _global !== "undefined" && _global); |
|
929 |
if (_global || typeof this.contents[_listId] === "undefined") { |
|
930 |
return this.directory.getGlobalList().filter(function(_e) { |
|
931 |
return (_e.elementType === _listId); |
|
932 |
}); |
|
933 |
} else { |
|
934 |
return this.contents[_listId]; |
|
935 |
} |
|
936 |
} |
|
937 |
||
938 |
Model.Source.prototype.forEach = function(_callback) { |
|
939 |
var _this = this; |
|
940 |
ns._(this.contents).forEach(function(_value, _key) { |
|
941 |
_callback.call(_this, _value, _key); |
|
942 |
}) |
|
943 |
} |
|
944 |
||
945 |
Model.Source.prototype.getElement = function(_elId) { |
|
946 |
return this.directory.getElement(_elId); |
|
947 |
} |
|
948 |
||
949 |
Model.Source.prototype.get = function() { |
|
950 |
this.status = Model._SOURCE_STATUS_WAITING; |
|
951 |
this.handleCallbacks(); |
|
952 |
} |
|
953 |
||
954 |
/* We defer the callbacks calls so they execute after the queue is cleared */ |
|
955 |
Model.Source.prototype.deferCallback = function(_callback) { |
|
956 |
var _this = this; |
|
957 |
ns._.defer(function() { |
|
958 |
_callback.call(_this); |
|
959 |
}); |
|
960 |
} |
|
961 |
||
962 |
Model.Source.prototype.handleCallbacks = function() { |
|
963 |
this.status = Model._SOURCE_STATUS_READY; |
|
964 |
while (this.callbackQueue.length) { |
|
965 |
this.deferCallback(this.callbackQueue.splice(0,1)[0]); |
|
966 |
} |
|
967 |
} |
|
968 |
Model.Source.prototype.onLoad = function(_callback) { |
|
969 |
if (this.status === Model._SOURCE_STATUS_READY) { |
|
970 |
this.deferCallback(_callback); |
|
971 |
} else { |
|
972 |
this.callbackQueue.push(_callback); |
|
973 |
} |
|
974 |
} |
|
975 |
||
976 |
Model.Source.prototype.serialize = function() { |
|
977 |
return this.serializer.serialize(this); |
|
978 |
} |
|
979 |
||
980 |
Model.Source.prototype.deSerialize = function(_data) { |
|
981 |
this.serializer.deSerialize(_data, this); |
|
982 |
} |
|
983 |
||
984 |
Model.Source.prototype.getAnnotations = function(_global) { |
|
985 |
_global = (typeof _global !== "undefined" && _global); |
|
986 |
return this.getList("annotation", _global); |
|
987 |
} |
|
988 |
||
989 |
Model.Source.prototype.getMedias = function(_global) { |
|
990 |
_global = (typeof _global !== "undefined" && _global); |
|
991 |
return this.getList("media", _global); |
|
992 |
} |
|
993 |
||
994 |
Model.Source.prototype.getTags = function(_global) { |
|
995 |
_global = (typeof _global !== "undefined" && _global); |
|
996 |
return this.getList("tag", _global); |
|
997 |
} |
|
998 |
||
999 |
Model.Source.prototype.getMashups = function(_global) { |
|
1000 |
_global = (typeof _global !== "undefined" && _global); |
|
1001 |
return this.getList("mashup", _global); |
|
1002 |
} |
|
1003 |
||
1004 |
Model.Source.prototype.getAnnotationTypes = function(_global) { |
|
1005 |
_global = (typeof _global !== "undefined" && _global); |
|
1006 |
return this.getList("annotationType", _global); |
|
1007 |
} |
|
1008 |
||
1009 |
Model.Source.prototype.getAnnotationsByTypeTitle = function(_title, _global) { |
|
1010 |
_global = (typeof _global !== "undefined" && _global); |
|
1011 |
var _res = new Model.List(this.directory), |
|
1012 |
_annTypes = this.getAnnotationTypes(_global).searchByTitle(_title); |
|
1013 |
_annTypes.forEach(function(_annType) { |
|
1014 |
_res.addElements(_annType.getAnnotations(_global)); |
|
1015 |
}) |
|
1016 |
return _res; |
|
1017 |
} |
|
1018 |
||
1019 |
Model.Source.prototype.getDuration = function() { |
|
1020 |
var _m = this.currentMedia; |
|
1021 |
if (typeof _m !== "undefined") { |
|
1022 |
return this.currentMedia.duration; |
|
1023 |
} |
|
1024 |
} |
|
1025 |
||
1026 |
Model.Source.prototype.getCurrentMedia = function(_opts) { |
|
1027 |
if (typeof this.currentMedia === "undefined") { |
|
1028 |
if (_opts.is_mashup) { |
|
1029 |
var _mashups = this.getMashups(); |
|
1030 |
if (_mashups.length) { |
|
1031 |
this.currentMedia = _mashups[0]; |
|
1032 |
} |
|
1033 |
} else { |
|
1034 |
var _medias = this.getMedias(); |
|
1035 |
if (_medias.length) { |
|
1036 |
this.currentMedia = _medias[0]; |
|
1037 |
} |
|
1038 |
} |
|
1039 |
} |
|
1040 |
return this.currentMedia; |
|
1041 |
} |
|
1042 |
||
1043 |
Model.Source.prototype.merge = function(_source) { |
|
1044 |
var _this = this; |
|
1045 |
_source.forEach(function(_value, _key) { |
|
1046 |
_this.getList(_key).addElements(_value); |
|
1047 |
}); |
|
1048 |
} |
|
1049 |
||
1050 |
/* */ |
|
1051 |
||
1052 |
Model.RemoteSource = function(_config) { |
|
1053 |
Model.Source.call(this, _config); |
|
1054 |
} |
|
1055 |
||
1056 |
Model.RemoteSource.prototype = new Model.Source(); |
|
1057 |
||
1058 |
Model.RemoteSource.prototype.get = function() { |
|
1059 |
this.status = Model._SOURCE_STATUS_WAITING; |
|
1060 |
var _this = this; |
|
1061 |
this.serializer.loadData(this.url, function(_result) { |
|
1062 |
_this.deSerialize(_result); |
|
1063 |
_this.handleCallbacks(); |
|
1064 |
}); |
|
1065 |
} |
|
1066 |
||
1067 |
/* */ |
|
1068 |
||
1069 |
Model.Directory = function() { |
|
1070 |
this.remoteSources = {}; |
|
1071 |
this.elements = {}; |
|
1072 |
} |
|
1073 |
||
1074 |
Model.Directory.prototype.remoteSource = function(_properties) { |
|
1075 |
if (typeof _properties !== "object" || typeof _properties.url === "undefined") { |
|
1076 |
throw "Error : Model.Directory.remoteSource(configuration): configuration.url is undefined"; |
|
1077 |
} |
|
1078 |
var _config = ns._({ directory: this }).extend(_properties); |
|
1079 |
if (typeof this.remoteSources[_properties.url] === "undefined") { |
|
1080 |
this.remoteSources[_properties.url] = new Model.RemoteSource(_config); |
|
1081 |
} |
|
1082 |
return this.remoteSources[_properties.url]; |
|
1083 |
} |
|
1084 |
||
1085 |
Model.Directory.prototype.newLocalSource = function(_properties) { |
|
1086 |
var _config = ns._({ directory: this }).extend(_properties), |
|
1087 |
_res = new Model.Source(_config); |
|
1088 |
return _res; |
|
1089 |
} |
|
1090 |
||
1091 |
Model.Directory.prototype.getElement = function(_id) { |
|
1092 |
return this.elements[_id]; |
|
1093 |
} |
|
1094 |
||
1095 |
Model.Directory.prototype.addElement = function(_element) { |
|
1096 |
this.elements[_element.id] = _element; |
|
1097 |
} |
|
1098 |
||
1099 |
Model.Directory.prototype.getGlobalList = function() { |
|
1100 |
var _res = new Model.List(this); |
|
1101 |
_res.addIds(ns._(this.elements).keys()); |
|
1102 |
return _res; |
|
1103 |
} |
|
1104 |
||
| 23 | 1105 |
return Model; |
| 12 | 1106 |
|
1107 |
})(IriSP); |