|
1 /* TODO: Separate Project-specific data from Source */ |
|
2 |
|
3 /* model.js is where data is stored in a standard form, whatever the serializer */ |
|
4 |
|
5 (function (ns) { |
|
6 |
|
7 var Model = { |
|
8 _SOURCE_STATUS_EMPTY : 0, |
|
9 _SOURCE_STATUS_WAITING : 1, |
|
10 _SOURCE_STATUS_READY : 2, |
|
11 _ID_AUTO_INCREMENT : 0, |
|
12 _ID_BASE : (function(_d) { |
|
13 function pad(n){return n<10 ? '0'+n : n} |
|
14 function fillrand(n) { |
|
15 var _res = '' |
|
16 for (var i=0; i<n; i++) { |
|
17 _res += Math.floor(16*Math.random()).toString(16); |
|
18 } |
|
19 return _res; |
|
20 } |
|
21 return _d.getUTCFullYear() + '-' |
|
22 + pad(_d.getUTCMonth()+1) + '-' |
|
23 + pad(_d.getUTCDate()) + '-' |
|
24 + fillrand(16); |
|
25 })(new Date()), |
|
26 getUID : function() { |
|
27 var _n = (++this._ID_AUTO_INCREMENT).toString(); |
|
28 while (_n.length < 4) { |
|
29 _n = '0' + _n |
|
30 } |
|
31 return "autoid-" + this._ID_BASE + '-' + _n; |
|
32 }, |
|
33 regexpFromTextOrArray : function(_textOrArray, _testOnly) { |
|
34 var _testOnly = _testOnly || false; |
|
35 function escapeText(_text) { |
|
36 return _text.replace(/([\\\*\+\?\|\{\[\}\]\(\)\^\$\.\#\/])/gm, '\\$1'); |
|
37 } |
|
38 var _source = |
|
39 typeof _textOrArray === "string" |
|
40 ? escapeText(_textOrArray) |
|
41 : ns._(_textOrArray).map(escapeText).join("|"); |
|
42 if (_testOnly) { |
|
43 return new RegExp( _source, 'im'); |
|
44 } else { |
|
45 return new RegExp( '(' + _source + ')', 'gim'); |
|
46 } |
|
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 }, |
|
73 dateToIso : function(d) { |
|
74 function pad(n){return n<10 ? '0'+n : n} |
|
75 return d.getUTCFullYear()+'-' |
|
76 + pad(d.getUTCMonth()+1)+'-' |
|
77 + pad(d.getUTCDate())+'T' |
|
78 + pad(d.getUTCHours())+':' |
|
79 + pad(d.getUTCMinutes())+':' |
|
80 + pad(d.getUTCSeconds())+'Z' |
|
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 */ |
|
170 Model.List.prototype.searchByTitle = function(_text) { |
|
171 var _rgxp = Model.regexpFromTextOrArray(_text, true); |
|
172 return this.filter(function(_element) { |
|
173 return _rgxp.test(_element.title); |
|
174 }); |
|
175 } |
|
176 |
|
177 Model.List.prototype.searchByDescription = function(_text) { |
|
178 var _rgxp = Model.regexpFromTextOrArray(_text, true); |
|
179 return this.filter(function(_element) { |
|
180 return _rgxp.test(_element.description); |
|
181 }); |
|
182 } |
|
183 |
|
184 Model.List.prototype.searchByTextFields = function(_text) { |
|
185 var _rgxp = Model.regexpFromTextOrArray(_text, true); |
|
186 return this.filter(function(_element) { |
|
187 return _rgxp.test(_element.description) || _rgxp.test(_element.title); |
|
188 }); |
|
189 } |
|
190 |
|
191 Model.List.prototype.getTitles = function() { |
|
192 return this.map(function(_el) { |
|
193 return _el.title; |
|
194 }); |
|
195 } |
|
196 |
|
197 Model.List.prototype.addId = function(_id) { |
|
198 var _el = this.directory.getElement(_id) |
|
199 if (!this.hasId(_id) && typeof _el !== "undefined") { |
|
200 this.idIndex.push(_id); |
|
201 Array.prototype.push.call(this, _el); |
|
202 } |
|
203 } |
|
204 |
|
205 Model.List.prototype.push = function(_el) { |
|
206 if (typeof _el === "undefined") { |
|
207 return; |
|
208 } |
|
209 var _index = (ns._(this.idIndex).indexOf(_el.id)); |
|
210 if (_index === -1) { |
|
211 this.idIndex.push(_el.id); |
|
212 Array.prototype.push.call(this, _el); |
|
213 } else { |
|
214 this[_index] = _el; |
|
215 } |
|
216 } |
|
217 |
|
218 Model.List.prototype.addIds = function(_array) { |
|
219 var _l = _array.length, |
|
220 _this = this; |
|
221 ns._(_array).forEach(function(_id) { |
|
222 _this.addId(_id); |
|
223 }); |
|
224 } |
|
225 |
|
226 Model.List.prototype.addElements = function(_array) { |
|
227 var _this = this; |
|
228 ns._(_array).forEach(function(_el) { |
|
229 _this.push(_el); |
|
230 }); |
|
231 } |
|
232 |
|
233 Model.List.prototype.removeId = function(_id, _deleteFromDirectory) { |
|
234 var _deleteFromDirectory = _deleteFromDirectory || false, |
|
235 _index = (ns._(this.idIndex).indexOf(_id)); |
|
236 if (_index !== -1) { |
|
237 this.splice(_index,1); |
|
238 } |
|
239 if (_deleteFromDirectory) { |
|
240 delete this.directory.elements[_id]; |
|
241 } |
|
242 } |
|
243 |
|
244 Model.List.prototype.removeElement = function(_el, _deleteFromDirectory) { |
|
245 var _deleteFromDirectory = _deleteFromDirectory || false; |
|
246 this.removeId(_el.id); |
|
247 } |
|
248 |
|
249 Model.List.prototype.removeIds = function(_list, _deleteFromDirectory) { |
|
250 var _deleteFromDirectory = _deleteFromDirectory || false, |
|
251 _this = this; |
|
252 ns._(_list).forEach(function(_id) { |
|
253 _this.removeId(_id); |
|
254 }); |
|
255 } |
|
256 |
|
257 Model.List.prototype.removeElements = function(_list, _deleteFromDirectory) { |
|
258 var _deleteFromDirectory = _deleteFromDirectory || false, |
|
259 _this = this; |
|
260 ns._(_list).forEach(function(_el) { |
|
261 _this.removeElement(_el); |
|
262 }); |
|
263 } |
|
264 |
|
265 Model.List.prototype.on = function(_event, _callback) { |
|
266 if (typeof this.__events[_event] === "undefined") { |
|
267 this.__events[_event] = []; |
|
268 } |
|
269 this.__events[_event].push(_callback); |
|
270 } |
|
271 |
|
272 Model.List.prototype.off = function(_event, _callback) { |
|
273 if (typeof this.__events[_event] !== "undefined") { |
|
274 this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) { |
|
275 return _fn === _callback; |
|
276 }); |
|
277 } |
|
278 } |
|
279 |
|
280 Model.List.prototype.trigger = function(_event, _data) { |
|
281 var _list = this; |
|
282 ns._(this.__events[_event]).each(function(_callback) { |
|
283 _callback.call(_list, _data); |
|
284 }); |
|
285 } |
|
286 |
|
287 /* A simple time management object, that helps converting millisecs to seconds and strings, |
|
288 * without the clumsiness of the original Date object. |
|
289 */ |
|
290 |
|
291 Model.Time = function(_milliseconds) { |
|
292 this.milliseconds = 0; |
|
293 this.setMilliseconds(_milliseconds); |
|
294 } |
|
295 |
|
296 Model.Time.prototype.setMilliseconds = function(_milliseconds) { |
|
297 var _ante = _milliseconds; |
|
298 switch(typeof _milliseconds) { |
|
299 case "string": |
|
300 this.milliseconds = parseFloat(_milliseconds); |
|
301 break; |
|
302 case "number": |
|
303 this.milliseconds = _milliseconds; |
|
304 break; |
|
305 case "object": |
|
306 this.milliseconds = parseFloat(_milliseconds.valueOf()); |
|
307 break; |
|
308 default: |
|
309 this.milliseconds = 0; |
|
310 } |
|
311 if (this.milliseconds === NaN) { |
|
312 this.milliseconds = _ante; |
|
313 } |
|
314 } |
|
315 |
|
316 Model.Time.prototype.setSeconds = function(_seconds) { |
|
317 this.milliseconds = 1000 * _seconds; |
|
318 } |
|
319 |
|
320 Model.Time.prototype.getSeconds = function() { |
|
321 return this.milliseconds / 1000; |
|
322 } |
|
323 |
|
324 Model.Time.prototype.getHMS = function() { |
|
325 var _totalSeconds = Math.abs(Math.floor(this.getSeconds())); |
|
326 return { |
|
327 hours : Math.floor(_totalSeconds / 3600), |
|
328 minutes : (Math.floor(_totalSeconds / 60) % 60), |
|
329 seconds : _totalSeconds % 60 |
|
330 } |
|
331 } |
|
332 |
|
333 Model.Time.prototype.add = function(_milliseconds) { |
|
334 this.milliseconds += new Model.Time(_milliseconds).milliseconds; |
|
335 } |
|
336 |
|
337 Model.Time.prototype.valueOf = function() { |
|
338 return this.milliseconds; |
|
339 } |
|
340 |
|
341 Model.Time.prototype.toString = function() { |
|
342 function pad(_n) { |
|
343 var _res = _n.toString(); |
|
344 while (_res.length < 2) { |
|
345 _res = '0' + _res; |
|
346 } |
|
347 return _res; |
|
348 } |
|
349 var _hms = this.getHMS(), |
|
350 _res = ''; |
|
351 if (_hms.hours) { |
|
352 _res += pad(_hms.hours) + ':' |
|
353 } |
|
354 _res += pad(_hms.minutes) + ':' + pad(_hms.seconds); |
|
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'; |
|
401 if (typeof _source === "undefined") { |
|
402 return; |
|
403 } |
|
404 if (typeof _id === "undefined" || !_id) { |
|
405 _id = Model.getUID(); |
|
406 } |
|
407 this.source = _source; |
|
408 this.id = _id; |
|
409 this.title = ""; |
|
410 this.description = ""; |
|
411 this.__events = {} |
|
412 this.source.directory.addElement(this); |
|
413 } |
|
414 |
|
415 Model.Element.prototype.toString = function() { |
|
416 return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : ''); |
|
417 } |
|
418 |
|
419 Model.Element.prototype.setReference = function(_elementType, _idRef) { |
|
420 this[_elementType] = new Model.Reference(this.source, _idRef); |
|
421 } |
|
422 |
|
423 Model.Element.prototype.getReference = function(_elementType) { |
|
424 if (typeof this[_elementType] !== "undefined") { |
|
425 return this[_elementType].getContents(); |
|
426 } |
|
427 } |
|
428 |
|
429 Model.Element.prototype.getRelated = function(_elementType, _global) { |
|
430 _global = (typeof _global !== "undefined" && _global); |
|
431 var _this = this; |
|
432 return this.source.getList(_elementType, _global).filter(function(_el) { |
|
433 var _ref = _el[_this.elementType]; |
|
434 return _ref && _ref.isOrHasId(_this.id); |
|
435 }); |
|
436 } |
|
437 |
|
438 Model.Element.prototype.on = function(_event, _callback) { |
|
439 if (typeof this.__events[_event] === "undefined") { |
|
440 this.__events[_event] = []; |
|
441 } |
|
442 this.__events[_event].push(_callback); |
|
443 } |
|
444 |
|
445 Model.Element.prototype.off = function(_event, _callback) { |
|
446 if (typeof this.__events[_event] !== "undefined") { |
|
447 this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) { |
|
448 return _fn === _callback; |
|
449 }); |
|
450 } |
|
451 } |
|
452 |
|
453 Model.Element.prototype.trigger = function(_event, _data) { |
|
454 var _element = this; |
|
455 ns._(this.__events[_event]).each(function(_callback) { |
|
456 _callback.call(_element, _data); |
|
457 }); |
|
458 } |
|
459 |
|
460 /* */ |
|
461 |
|
462 Model.Playable = function(_id, _source) { |
|
463 Model.Element.call(this, _id, _source); |
|
464 if (typeof _source === "undefined") { |
|
465 return; |
|
466 } |
|
467 this.elementType = 'playable'; |
|
468 this.currentTime = new Model.Time(); |
|
469 this.volume = .5; |
|
470 this.paused = true; |
|
471 this.muted = false; |
|
472 var _this = this; |
|
473 this.on("play", function() { |
|
474 _this.paused = false; |
|
475 }); |
|
476 this.on("pause", function() { |
|
477 _this.paused = true; |
|
478 }); |
|
479 this.on("timeupdate", function(_time) { |
|
480 _this.currentTime = _time; |
|
481 }); |
|
482 } |
|
483 |
|
484 Model.Playable.prototype = new Model.Element(); |
|
485 |
|
486 Model.Playable.prototype.getCurrentTime = function() { |
|
487 return this.currentTime; |
|
488 } |
|
489 |
|
490 Model.Playable.prototype.getVolume = function() { |
|
491 return this.volume; |
|
492 } |
|
493 |
|
494 Model.Playable.prototype.getPaused = function() { |
|
495 return this.paused; |
|
496 } |
|
497 |
|
498 Model.Playable.prototype.getMuted = function() { |
|
499 return this.muted; |
|
500 } |
|
501 |
|
502 Model.Playable.prototype.setCurrentTime = function(_time) { |
|
503 this.trigger("setcurrenttime",_time); |
|
504 } |
|
505 |
|
506 Model.Playable.prototype.setVolume = function(_vol) { |
|
507 this.trigger("setvolume",_vol); |
|
508 } |
|
509 |
|
510 Model.Playable.prototype.setMuted = function(_muted) { |
|
511 this.trigger("setmuted",_muted); |
|
512 } |
|
513 |
|
514 Model.Playable.prototype.play = function() { |
|
515 this.trigger("setplay"); |
|
516 } |
|
517 |
|
518 Model.Playable.prototype.pause = function() { |
|
519 this.trigger("setpause"); |
|
520 } |
|
521 |
|
522 |
|
523 /* */ |
|
524 |
|
525 Model.Media = function(_id, _source) { |
|
526 Model.Playable.call(this, _id, _source); |
|
527 this.elementType = 'media'; |
|
528 this.duration = new Model.Time(); |
|
529 this.video = ''; |
|
530 |
|
531 var _this = this; |
|
532 this.on("timeupdate", function(_time) { |
|
533 _this.getAnnotations().filter(function(_a) { |
|
534 return (_a.end <= _time || _a.begin > _time) && _a.playing |
|
535 }).forEach(function(_a) { |
|
536 _a.playing = false; |
|
537 _a.trigger("leave"); |
|
538 }); |
|
539 _this.getAnnotations().filter(function(_a) { |
|
540 return _a.begin <= _time && _a.end > _time && !_a.playing |
|
541 }).forEach(function(_a) { |
|
542 _a.playing = true; |
|
543 _a.trigger("enter"); |
|
544 }); |
|
545 }); |
|
546 } |
|
547 |
|
548 Model.Media.prototype = new Model.Playable(); |
|
549 |
|
550 /* Default functions to be overriden by players */ |
|
551 |
|
552 Model.Media.prototype.setDuration = function(_durationMs) { |
|
553 this.duration.setMilliseconds(_durationMs); |
|
554 } |
|
555 |
|
556 Model.Media.prototype.getAnnotations = function() { |
|
557 return this.getRelated("annotation"); |
|
558 } |
|
559 |
|
560 Model.Media.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
561 var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id"); |
|
562 if (_annTypes.length) { |
|
563 return this.getAnnotations().filter(function(_annotation) { |
|
564 return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1; |
|
565 }); |
|
566 } else { |
|
567 return new Model.List(this.source.directory) |
|
568 } |
|
569 } |
|
570 |
|
571 /* */ |
|
572 |
|
573 Model.Tag = function(_id, _source) { |
|
574 Model.Element.call(this, _id, _source); |
|
575 this.elementType = 'tag'; |
|
576 } |
|
577 |
|
578 Model.Tag.prototype = new Model.Element(); |
|
579 |
|
580 Model.Tag.prototype.getAnnotations = function() { |
|
581 return this.getRelated("annotation"); |
|
582 } |
|
583 |
|
584 /* */ |
|
585 Model.AnnotationType = function(_id, _source) { |
|
586 Model.Element.call(this, _id, _source); |
|
587 this.elementType = 'annotationType'; |
|
588 } |
|
589 |
|
590 Model.AnnotationType.prototype = new Model.Element(); |
|
591 |
|
592 Model.AnnotationType.prototype.getAnnotations = function() { |
|
593 return this.getRelated("annotation"); |
|
594 } |
|
595 |
|
596 /* Annotation |
|
597 * */ |
|
598 |
|
599 Model.Annotation = function(_id, _source) { |
|
600 Model.Element.call(this, _id, _source); |
|
601 this.elementType = 'annotation'; |
|
602 this.begin = new Model.Time(); |
|
603 this.end = new Model.Time(); |
|
604 this.tag = new Model.Reference(_source, []); |
|
605 this.playing = false; |
|
606 var _this = this; |
|
607 this.on("click", function() { |
|
608 _this.getMedia().setCurrentTime(_this.begin); |
|
609 }); |
|
610 } |
|
611 |
|
612 Model.Annotation.prototype = new Model.Element(); |
|
613 |
|
614 Model.Annotation.prototype.setBegin = function(_beginMs) { |
|
615 this.begin.setMilliseconds(_beginMs); |
|
616 } |
|
617 |
|
618 Model.Annotation.prototype.setEnd = function(_beginMs) { |
|
619 this.end.setMilliseconds(_beginMs); |
|
620 } |
|
621 |
|
622 Model.Annotation.prototype.setMedia = function(_idRef) { |
|
623 this.setReference("media", _idRef); |
|
624 } |
|
625 |
|
626 Model.Annotation.prototype.getMedia = function() { |
|
627 return this.getReference("media"); |
|
628 } |
|
629 |
|
630 Model.Annotation.prototype.setAnnotationType = function(_idRef) { |
|
631 this.setReference("annotationType", _idRef); |
|
632 } |
|
633 |
|
634 Model.Annotation.prototype.getAnnotationType = function() { |
|
635 return this.getReference("annotationType"); |
|
636 } |
|
637 |
|
638 Model.Annotation.prototype.setTags = function(_idRefs) { |
|
639 this.setReference("tag", _idRefs); |
|
640 } |
|
641 |
|
642 Model.Annotation.prototype.getTags = function() { |
|
643 return this.getReference("tag"); |
|
644 } |
|
645 |
|
646 Model.Annotation.prototype.getTagTexts = function() { |
|
647 return this.getTags().getTitles(); |
|
648 } |
|
649 |
|
650 Model.Annotation.prototype.getDuration = function() { |
|
651 return new Model.Time(this.end.milliseconds - this.begin.milliseconds) |
|
652 } |
|
653 |
|
654 /* */ |
|
655 |
|
656 Model.MashedAnnotation = function(_mashup, _annotation) { |
|
657 Model.Element.call(this, _mashup.id + "_" + _annotation.id, _annotation.source); |
|
658 this.elementType = 'mashedAnnotation'; |
|
659 this.annotation = _annotation; |
|
660 this.begin = new Model.Time(_mashup.duration); |
|
661 this.end = new Model.Time(_mashup.duration + _annotation.getDuration()); |
|
662 this.title = this.annotation.title; |
|
663 this.description = this.annotation.description; |
|
664 this.color = this.annotation.color; |
|
665 var _this = this; |
|
666 this.on("click", function() { |
|
667 _mashup.setCurrentTime(_this.begin); |
|
668 }); |
|
669 } |
|
670 |
|
671 Model.MashedAnnotation.prototype = new Model.Element(null); |
|
672 |
|
673 Model.MashedAnnotation.prototype.getMedia = function() { |
|
674 return this.annotation.getReference("media"); |
|
675 } |
|
676 |
|
677 Model.MashedAnnotation.prototype.getAnnotationType = function() { |
|
678 return this.annotation.getReference("annotationType"); |
|
679 } |
|
680 |
|
681 Model.MashedAnnotation.prototype.getTags = function() { |
|
682 return this.annotation.getReference("tag"); |
|
683 } |
|
684 |
|
685 Model.MashedAnnotation.prototype.getTagTexts = function() { |
|
686 return this.annotation.getTags().getTitles(); |
|
687 } |
|
688 |
|
689 Model.MashedAnnotation.prototype.getDuration = function() { |
|
690 return this.annotation.getDuration(); |
|
691 } |
|
692 |
|
693 /* */ |
|
694 |
|
695 Model.Mashup = function(_id, _source) { |
|
696 Model.Playable.call(this, _id, _source); |
|
697 this.elementType = 'mashup'; |
|
698 this.duration = new Model.Time(); |
|
699 this.segments = new Model.List(_source.directory); |
|
700 this.medias = new Model.List(_source.directory); |
|
701 var _currentMedia = null; |
|
702 var _this = this; |
|
703 this.on("timeupdate", function(_time) { |
|
704 _this.getAnnotations().filter(function(_a) { |
|
705 return (_a.end <= _time || _a.begin > _time) && _a.playing |
|
706 }).forEach(function(_a) { |
|
707 _a.playing = false; |
|
708 _a.trigger("leave"); |
|
709 }); |
|
710 _this.getAnnotations().filter(function(_a) { |
|
711 return _a.begin <= _time && _a.end > _time && !_a.playing |
|
712 }).forEach(function(_a) { |
|
713 _a.playing = true; |
|
714 _a.trigger("enter"); |
|
715 var _m = _a.getMedia(); |
|
716 if (_m !== _currentMedia) { |
|
717 if (_currentMedia) { |
|
718 _currentMedia.trigger("leave"); |
|
719 } |
|
720 _m.trigger("enter"); |
|
721 _currentMedia = _m; |
|
722 } |
|
723 }); |
|
724 }); |
|
725 } |
|
726 |
|
727 Model.Mashup.prototype = new Model.Playable(); |
|
728 |
|
729 Model.Mashup.prototype.addSegment = function(_annotation) { |
|
730 var _mashedAnnotation = new Model.MashedAnnotation(this, _annotation); |
|
731 this.duration.setMilliseconds(_mashedAnnotation.end); |
|
732 this.segments.push(_mashedAnnotation); |
|
733 this.medias.push(_annotation.getMedia()); |
|
734 } |
|
735 |
|
736 Model.Mashup.prototype.addSegmentById = function(_elId) { |
|
737 var _annotation = this.source.getElement(_elId); |
|
738 if (typeof _annotation !== "undefined") { |
|
739 this.addSegment(_annotation); |
|
740 } |
|
741 } |
|
742 |
|
743 Model.Mashup.prototype.getAnnotations = function() { |
|
744 return this.segments; |
|
745 } |
|
746 |
|
747 Model.Mashup.prototype.getMedias = function() { |
|
748 return this.medias; |
|
749 } |
|
750 |
|
751 Model.Mashup.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
752 var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id"); |
|
753 if (_annTypes.length) { |
|
754 return this.getAnnotations().filter(function(_annotation) { |
|
755 return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1; |
|
756 }); |
|
757 } else { |
|
758 return new Model.List(this.source.directory) |
|
759 } |
|
760 } |
|
761 |
|
762 Model.Mashup.prototype.getAnnotationAtTime = function(_time) { |
|
763 var _list = this.segments.filter(function(_annotation) { |
|
764 return _annotation.begin <= _time && _annotation.end > _time; |
|
765 }); |
|
766 if (_list.length) { |
|
767 return _list[0]; |
|
768 } else { |
|
769 return undefined; |
|
770 } |
|
771 } |
|
772 |
|
773 Model.Mashup.prototype.getMediaAtTime = function(_time) { |
|
774 var _annotation = this.getAnnotationAtTime(_time); |
|
775 if (typeof _annotation !== "undefined") { |
|
776 return _annotation.getMedia(); |
|
777 } else { |
|
778 return undefined; |
|
779 } |
|
780 } |
|
781 |
|
782 /* */ |
|
783 |
|
784 Model.Source = function(_config) { |
|
785 this.status = Model._SOURCE_STATUS_EMPTY; |
|
786 this.elementType = "source"; |
|
787 if (typeof _config !== "undefined") { |
|
788 var _this = this; |
|
789 ns._(_config).forEach(function(_v, _k) { |
|
790 _this[_k] = _v; |
|
791 }) |
|
792 this.callbackQueue = []; |
|
793 this.contents = {}; |
|
794 this.get(); |
|
795 } |
|
796 } |
|
797 |
|
798 Model.Source.prototype = new Model.Element(); |
|
799 |
|
800 Model.Source.prototype.addList = function(_listId, _contents) { |
|
801 if (typeof this.contents[_listId] === "undefined") { |
|
802 this.contents[_listId] = new Model.List(this.directory); |
|
803 } |
|
804 this.contents[_listId].addElements(_contents); |
|
805 } |
|
806 |
|
807 Model.Source.prototype.getList = function(_listId, _global) { |
|
808 _global = (typeof _global !== "undefined" && _global); |
|
809 if (_global || typeof this.contents[_listId] === "undefined") { |
|
810 return this.directory.getGlobalList().filter(function(_e) { |
|
811 return (_e.elementType === _listId); |
|
812 }); |
|
813 } else { |
|
814 return this.contents[_listId]; |
|
815 } |
|
816 } |
|
817 |
|
818 Model.Source.prototype.forEach = function(_callback) { |
|
819 var _this = this; |
|
820 ns._(this.contents).forEach(function(_value, _key) { |
|
821 _callback.call(_this, _value, _key); |
|
822 }) |
|
823 } |
|
824 |
|
825 Model.Source.prototype.getElement = function(_elId) { |
|
826 return this.directory.getElement(_elId); |
|
827 } |
|
828 |
|
829 Model.Source.prototype.get = function() { |
|
830 this.status = Model._SOURCE_STATUS_WAITING; |
|
831 this.handleCallbacks(); |
|
832 } |
|
833 |
|
834 /* We defer the callbacks calls so they execute after the queue is cleared */ |
|
835 Model.Source.prototype.deferCallback = function(_callback) { |
|
836 var _this = this; |
|
837 ns._.defer(function() { |
|
838 _callback.call(_this); |
|
839 }); |
|
840 } |
|
841 |
|
842 Model.Source.prototype.handleCallbacks = function() { |
|
843 this.status = Model._SOURCE_STATUS_READY; |
|
844 while (this.callbackQueue.length) { |
|
845 this.deferCallback(this.callbackQueue.splice(0,1)[0]); |
|
846 } |
|
847 } |
|
848 Model.Source.prototype.onLoad = function(_callback) { |
|
849 if (this.status === Model._SOURCE_STATUS_READY) { |
|
850 this.deferCallback(_callback); |
|
851 } else { |
|
852 this.callbackQueue.push(_callback); |
|
853 } |
|
854 } |
|
855 |
|
856 Model.Source.prototype.serialize = function() { |
|
857 return this.serializer.serialize(this); |
|
858 } |
|
859 |
|
860 Model.Source.prototype.deSerialize = function(_data) { |
|
861 this.serializer.deSerialize(_data, this); |
|
862 } |
|
863 |
|
864 Model.Source.prototype.getAnnotations = function(_global) { |
|
865 _global = (typeof _global !== "undefined" && _global); |
|
866 return this.getList("annotation", _global); |
|
867 } |
|
868 |
|
869 Model.Source.prototype.getMedias = function(_global) { |
|
870 _global = (typeof _global !== "undefined" && _global); |
|
871 return this.getList("media", _global); |
|
872 } |
|
873 |
|
874 Model.Source.prototype.getTags = function(_global) { |
|
875 _global = (typeof _global !== "undefined" && _global); |
|
876 return this.getList("tag", _global); |
|
877 } |
|
878 |
|
879 Model.Source.prototype.getMashups = function(_global) { |
|
880 _global = (typeof _global !== "undefined" && _global); |
|
881 return this.getList("mashup", _global); |
|
882 } |
|
883 |
|
884 Model.Source.prototype.getAnnotationTypes = function(_global) { |
|
885 _global = (typeof _global !== "undefined" && _global); |
|
886 return this.getList("annotationType", _global); |
|
887 } |
|
888 |
|
889 Model.Source.prototype.getAnnotationsByTypeTitle = function(_title, _global) { |
|
890 _global = (typeof _global !== "undefined" && _global); |
|
891 var _res = new Model.List(this.directory), |
|
892 _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title); |
|
893 _annTypes.forEach(function(_annType) { |
|
894 _res.addElements(_annType.getAnnotations(_global)); |
|
895 }) |
|
896 return _res; |
|
897 } |
|
898 |
|
899 Model.Source.prototype.getDuration = function() { |
|
900 var _m = this.currentMedia; |
|
901 if (typeof _m !== "undefined") { |
|
902 return this.currentMedia.duration; |
|
903 } |
|
904 } |
|
905 |
|
906 Model.Source.prototype.getCurrentMedia = function(_opts) { |
|
907 if (typeof this.currentMedia === "undefined") { |
|
908 if (_opts.is_mashup) { |
|
909 var _mashups = this.getMashups(); |
|
910 if (_mashups.length) { |
|
911 this.currentMedia = _mashups[0]; |
|
912 } |
|
913 } else { |
|
914 var _medias = this.getMedias(); |
|
915 if (_medias.length) { |
|
916 this.currentMedia = _medias[0]; |
|
917 } |
|
918 } |
|
919 } |
|
920 return this.currentMedia; |
|
921 } |
|
922 |
|
923 Model.Source.prototype.merge = function(_source) { |
|
924 var _this = this; |
|
925 _source.forEach(function(_value, _key) { |
|
926 _this.getList(_key).addElements(_value); |
|
927 }); |
|
928 } |
|
929 |
|
930 /* */ |
|
931 |
|
932 Model.RemoteSource = function(_config) { |
|
933 Model.Source.call(this, _config); |
|
934 } |
|
935 |
|
936 Model.RemoteSource.prototype = new Model.Source(); |
|
937 |
|
938 Model.RemoteSource.prototype.get = function() { |
|
939 this.status = Model._SOURCE_STATUS_WAITING; |
|
940 var _this = this; |
|
941 this.serializer.loadData(this.url, function(_result) { |
|
942 _this.deSerialize(_result); |
|
943 _this.handleCallbacks(); |
|
944 }); |
|
945 } |
|
946 |
|
947 /* */ |
|
948 |
|
949 Model.Directory = function() { |
|
950 this.remoteSources = {}; |
|
951 this.elements = {}; |
|
952 } |
|
953 |
|
954 Model.Directory.prototype.remoteSource = function(_properties) { |
|
955 if (typeof _properties !== "object" || typeof _properties.url === "undefined") { |
|
956 throw "Error : Model.Directory.remoteSource(configuration): configuration.url is undefined"; |
|
957 } |
|
958 var _config = ns._({ directory: this }).extend(_properties); |
|
959 if (typeof this.remoteSources[_properties.url] === "undefined") { |
|
960 this.remoteSources[_properties.url] = new Model.RemoteSource(_config); |
|
961 } |
|
962 return this.remoteSources[_properties.url]; |
|
963 } |
|
964 |
|
965 Model.Directory.prototype.newLocalSource = function(_properties) { |
|
966 var _config = ns._({ directory: this }).extend(_properties), |
|
967 _res = new Model.Source(_config); |
|
968 return _res; |
|
969 } |
|
970 |
|
971 Model.Directory.prototype.getElement = function(_id) { |
|
972 return this.elements[_id]; |
|
973 } |
|
974 |
|
975 Model.Directory.prototype.addElement = function(_element) { |
|
976 this.elements[_element.id] = _element; |
|
977 } |
|
978 |
|
979 Model.Directory.prototype.getGlobalList = function() { |
|
980 var _res = new Model.List(this); |
|
981 _res.addIds(ns._(this.elements).keys()); |
|
982 return _res; |
|
983 } |
|
984 |
|
985 ns.Model = Model; |
|
986 |
|
987 })(IriSP); |