3
|
1 |
|
|
2 |
/* |
|
3 |
* |
|
4 |
__ __ _ _ _ _ |
|
5 |
| \/ | ___| |_ __ _ __| | __ _| |_ __ _ _ __ | | __ _ _ _ ___ _ __ |
|
6 |
| |\/| |/ _ \ __/ _` |/ _` |/ _` | __/ _` | '_ \| |/ _` | | | |/ _ \ '__| |
|
7 |
| | | | __/ || (_| | (_| | (_| | || (_| | |_) | | (_| | |_| | __/ | |
|
8 |
|_| |_|\___|\__\__,_|\__,_|\__,_|\__\__,_| .__/|_|\__,_|\__, |\___|_| |
|
9 |
|_| |___/ |
|
10 |
|
|
11 |
* Copyright 2010-2012 Institut de recherche et d'innovation |
|
12 |
* contributor(s) : Karim Hamidou, Samuel Huron, Raphael Velt, Thibaut Cavalie |
|
13 |
* |
|
14 |
* contact@iri.centrepompidou.fr |
|
15 |
* http://www.iri.centrepompidou.fr |
|
16 |
* |
|
17 |
* This software is a computer program whose purpose is to show and add annotations on a video . |
|
18 |
* This software is governed by the CeCILL-C license under French law and |
|
19 |
* abiding by the rules of distribution of free software. You can use, |
|
20 |
* modify and/ or redistribute the software under the terms of the CeCILL-C |
|
21 |
* license as circulated by CEA, CNRS and INRIA at the following URL |
|
22 |
* "http://www.cecill.info". |
|
23 |
* |
|
24 |
* The fact that you are presently reading this means that you have had |
|
25 |
* knowledge of the CeCILL-C license and that you accept its terms. |
|
26 |
*/ |
|
27 |
/* Initialization of the namespace */ |
|
28 |
|
|
29 |
if (typeof window.IriSP === "undefined") { |
32
|
30 |
window.IriSP = { |
|
31 |
VERSION: "0.3.1" |
|
32 |
}; |
3
|
33 |
} |
|
34 |
|
|
35 |
if (typeof IriSP.jQuery === "undefined" && typeof window.jQuery !== "undefined" && parseFloat(window.jQuery().jquery) >= 1.7) { |
|
36 |
IriSP.jQuery = window.jQuery; |
|
37 |
} |
|
38 |
|
|
39 |
if (typeof IriSP._ === "undefined" && typeof window._ !== "undefined" && parseFloat(window._.VERSION) >= 1.4) { |
|
40 |
IriSP._ = window._; |
|
41 |
} |
|
42 |
/* utils.js - various utils that don't belong anywhere else */ |
|
43 |
|
|
44 |
IriSP.jqEscape = function(_text) { |
|
45 |
return _text.replace(/(:|\.)/g,'\\$1'); |
|
46 |
}; |
|
47 |
|
|
48 |
IriSP.getLib = function(lib) { |
|
49 |
if (IriSP.libFiles.useCdn && typeof IriSP.libFiles.cdn[lib] == "string") { |
|
50 |
return IriSP.libFiles.cdn[lib]; |
|
51 |
} |
|
52 |
if (typeof IriSP.libFiles.locations[lib] == "string") { |
|
53 |
return IriSP.libFiles.locations[lib]; |
|
54 |
} |
|
55 |
if (typeof IriSP.libFiles.inDefaultDir[lib] == "string") { |
|
56 |
return IriSP.libFiles.defaultDir + '/' + IriSP.libFiles.inDefaultDir[lib]; |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
IriSP._cssCache = []; |
|
61 |
|
|
62 |
IriSP.loadCss = function(_cssFile) { |
|
63 |
if (IriSP._(IriSP._cssCache).indexOf(_cssFile) === -1) { |
|
64 |
IriSP.jQuery("<link>", { |
|
65 |
rel : "stylesheet", |
|
66 |
type : "text/css", |
|
67 |
href : _cssFile |
|
68 |
}).appendTo('head'); |
|
69 |
IriSP._cssCache.push(_cssFile); |
|
70 |
} |
|
71 |
}; |
|
72 |
|
|
73 |
IriSP.textFieldHtml = function(_text, _regexp, _extend) { |
|
74 |
var list = [], |
|
75 |
positions = [], |
|
76 |
text = _text.replace(/(^\s+|\s+$)/g,''); |
|
77 |
|
|
78 |
function addToList(_rx, _startHtml, _endHtml) { |
|
79 |
while(true) { |
|
80 |
var result = _rx.exec(text); |
|
81 |
if (!result) { |
|
82 |
break; |
|
83 |
} |
|
84 |
var end = _rx.lastIndex, |
|
85 |
start = result.index; |
|
86 |
list.push({ |
|
87 |
start: start, |
|
88 |
end: end, |
|
89 |
startHtml: (typeof _startHtml === "function" ? _startHtml(result) : _startHtml), |
|
90 |
endHtml: (typeof _endHtml === "function" ? _endHtml(result) : _endHtml) |
|
91 |
}); |
|
92 |
positions.push(start); |
|
93 |
positions.push(end); |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
if (_regexp) { |
|
98 |
addToList(_regexp, '<span class="Ldt-Highlight">', '</span>'); |
|
99 |
} |
|
100 |
|
32
|
101 |
addToList(/(https?:\/\/)?[\w\d\-]+\.[\w\d\-]+\S+/gm, function(matches) { |
87
|
102 |
return '<a href="' + (matches[1] ? '' : 'http://') + matches[0] + '" target="_blank">'; |
3
|
103 |
}, '</a>'); |
|
104 |
addToList(/@([\d\w]{1,15})/gm, function(matches) { |
87
|
105 |
return '<a href="http://twitter.com/' + matches[1] + '" target="_blank">'; |
3
|
106 |
}, '</a>'); |
|
107 |
addToList(/\*[^*]+\*/gm, '<b>', '</b>'); |
|
108 |
addToList(/[\n\r]+/gm, '', '<br />'); |
|
109 |
|
|
110 |
IriSP._(_extend).each(function(x) { |
|
111 |
addToList.apply(null, x); |
|
112 |
}); |
|
113 |
|
|
114 |
positions = IriSP._(positions) |
|
115 |
.chain() |
|
116 |
.uniq() |
87
|
117 |
.sortBy(function(p) { return parseInt(p); }) |
3
|
118 |
.value(); |
|
119 |
|
|
120 |
var res = "", lastIndex = 0; |
|
121 |
|
|
122 |
for (var i = 0; i < positions.length; i++) { |
|
123 |
var pos = positions[i]; |
|
124 |
res += text.substring(lastIndex, pos); |
|
125 |
for (var j = list.length - 1; j >= 0; j--) { |
|
126 |
var item = list[j]; |
|
127 |
if (item.start < pos && item.end >= pos) { |
|
128 |
res += item.endHtml; |
|
129 |
} |
|
130 |
} |
|
131 |
for (var j = 0; j < list.length; j++) { |
|
132 |
var item = list[j]; |
|
133 |
if (item.start <= pos && item.end > pos) { |
|
134 |
res += item.startHtml; |
|
135 |
} |
|
136 |
} |
|
137 |
lastIndex = pos; |
|
138 |
} |
|
139 |
|
|
140 |
res += text.substring(lastIndex); |
|
141 |
|
|
142 |
return res; |
|
143 |
|
|
144 |
}; |
|
145 |
|
|
146 |
IriSP.log = function() { |
|
147 |
if (typeof console !== "undefined" && typeof IriSP.logging !== "undefined" && IriSP.logging) { |
|
148 |
console.log.apply(console, arguments); |
|
149 |
} |
|
150 |
}; |
|
151 |
|
|
152 |
IriSP.attachDndData = function(jqSel, data) { |
|
153 |
jqSel.attr("draggable", "true").on("dragstart", function(_event) { |
|
154 |
var d = (typeof data === "function" ? data.call(this) : data); |
|
155 |
try { |
|
156 |
IriSP._(d).each(function(v, k) { |
|
157 |
if (v) { |
|
158 |
_event.originalEvent.dataTransfer.setData("text/x-iri-" + k, v); |
|
159 |
} |
|
160 |
}); |
|
161 |
} catch(err) { |
|
162 |
_event.originalEvent.dataTransfer.setData("Text", JSON.stringify(d)); |
|
163 |
} |
|
164 |
}); |
|
165 |
}; |
|
166 |
|
|
167 |
IriSP.FakeClass = function(properties) { |
|
168 |
var _this = this, |
|
169 |
noop = (function() {}); |
|
170 |
IriSP._(properties).each(function(p) { |
87
|
171 |
_this[p] = noop; |
3
|
172 |
}); |
87
|
173 |
}; |
3
|
174 |
|
|
175 |
/* js is where data is stored in a standard form, whatever the serializer */ |
|
176 |
|
|
177 |
//TODO: Separate Project-specific data from Source |
|
178 |
|
|
179 |
IriSP.Model = (function (ns) { |
|
180 |
|
|
181 |
function pad(n, x, b) { |
|
182 |
b = b || 10; |
|
183 |
var s = (x).toString(b); |
|
184 |
while (s.length < n) { |
|
185 |
s = "0" + s; |
|
186 |
} |
|
187 |
return s; |
|
188 |
} |
|
189 |
|
|
190 |
function rand16(n) { |
|
191 |
return pad(n, Math.floor(Math.random()*Math.pow(16,n)), 16); |
|
192 |
} |
|
193 |
|
|
194 |
var uidbase = rand16(8) + "-" + rand16(4) + "-", uidincrement = Math.floor(Math.random()*0x10000); |
|
195 |
|
|
196 |
var charsub = [ |
|
197 |
'[aáàâä]', |
|
198 |
'[cç]', |
|
199 |
'[eéèêë]', |
|
200 |
'[iíìîï]', |
|
201 |
'[oóòôö]', |
|
202 |
'[uùûü]' |
|
203 |
]; |
|
204 |
|
|
205 |
var removeChars = [ |
|
206 |
String.fromCharCode(768), String.fromCharCode(769), String.fromCharCode(770), String.fromCharCode(771), String.fromCharCode(807), |
|
207 |
"{", "}", "(", ")", "[", "]", "【", "】", "、", "・", "‥", "。", "「", "」", "『", "』", "〜", ":", "!", "?", " ", |
|
208 |
",", " ", ";", "(", ")", ".", "*", "+", "\\", "?", "|", "{", "}", "[", "]", "^", "#", "/" |
|
209 |
]; |
|
210 |
|
|
211 |
var Model = {}, |
|
212 |
_SOURCE_STATUS_EMPTY = Model._SOURCE_STATUS_EMPTY = 0, |
|
213 |
_SOURCE_STATUS_WAITING = Model._SOURCE_STATUS_WAITING = 1, |
|
214 |
_SOURCE_STATUS_READY = Model._SOURCE_STATUS_READY = 2, |
|
215 |
extendPrototype = Model.extendPrototype = function(toClass, fromClass) { |
|
216 |
var fromP = fromClass.prototype, |
|
217 |
toP = toClass.prototype; |
|
218 |
for (var k in fromP) { |
|
219 |
if (fromP.hasOwnProperty(k)) { |
|
220 |
toP[k] = fromP[k]; |
|
221 |
} |
|
222 |
} |
|
223 |
}, |
|
224 |
getUID = Model.getUID = function() { |
|
225 |
return uidbase + pad(4, (++uidincrement % 0x10000), 16) + "-" + rand16(4) + "-" + rand16(6) + rand16(6); |
|
226 |
}, |
|
227 |
isLocalURL = Model.isLocalURL = function(url) { |
|
228 |
var matches = url.match(/^(\w+:)\/\/([^/]+)/); |
|
229 |
if (matches) { |
87
|
230 |
return(matches[1] === document.location.protocol && matches[2] === document.location.host); |
3
|
231 |
} |
|
232 |
return true; |
|
233 |
}, |
|
234 |
regexpFromTextOrArray = Model.regexpFromTextOrArray = function(_textOrArray, _testOnly, _iexact) { |
|
235 |
var _testOnly = _testOnly || false, |
|
236 |
_iexact = _iexact || false; |
|
237 |
function escapeText(_text) { |
|
238 |
return _text.replace(/([\\\*\+\?\|\{\[\}\]\(\)\^\$\.\#\/])/gm, '\\$1'); |
|
239 |
} |
|
240 |
var _source = |
|
241 |
typeof _textOrArray === "string" |
|
242 |
? escapeText(_textOrArray) |
|
243 |
: ns._(_textOrArray).map(escapeText).join("|"), |
|
244 |
_flags = 'im'; |
|
245 |
if (!_testOnly) { |
|
246 |
_source = '(' + _source + ')'; |
|
247 |
_flags += 'g'; |
|
248 |
} |
|
249 |
if (_iexact) { |
|
250 |
_source = '^' + _source + '$'; |
|
251 |
} |
|
252 |
return new RegExp( _source, _flags); |
|
253 |
}, |
|
254 |
fullTextRegexps = Model.fullTextRegexps = function(_text) { |
|
255 |
var remsrc = "[\\" + removeChars.join("\\") + "]", |
|
256 |
remrx = new RegExp(remsrc,"gm"), |
87
|
257 |
txt = _text.toLowerCase().replace(remrx,""), |
3
|
258 |
res = [], |
|
259 |
charsrx = ns._(charsub).map(function(c) { |
|
260 |
return new RegExp(c); |
|
261 |
}), |
|
262 |
src = ""; |
|
263 |
for (var j = 0; j < txt.length; j++) { |
|
264 |
if (j) { |
|
265 |
src += remsrc + "*"; |
|
266 |
} |
|
267 |
var l = txt[j]; |
|
268 |
ns._(charsub).each(function(v, k) { |
|
269 |
l = l.replace(charsrx[k], v); |
|
270 |
}); |
|
271 |
src += l; |
|
272 |
} |
|
273 |
return "(" + src + ")"; |
|
274 |
}, |
|
275 |
isoToDate = Model.isoToDate = function(_str) { |
|
276 |
// http://delete.me.uk/2005/03/iso8601.html |
|
277 |
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})))?)?)?)?"; |
|
278 |
var d = _str.match(new RegExp(regexp)); |
|
279 |
|
|
280 |
var offset = 0; |
|
281 |
var date = new Date(d[1], 0, 1); |
|
282 |
|
|
283 |
if (d[3]) { date.setMonth(d[3] - 1); } |
|
284 |
if (d[5]) { date.setDate(d[5]); } |
|
285 |
if (d[7]) { date.setHours(d[7]); } |
|
286 |
if (d[8]) { date.setMinutes(d[8]); } |
|
287 |
if (d[10]) { date.setSeconds(d[10]); } |
|
288 |
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } |
|
289 |
if (d[14]) { |
|
290 |
offset = (Number(d[16]) * 60) + Number(d[17]); |
|
291 |
offset *= ((d[15] == '-') ? 1 : -1); |
|
292 |
} |
|
293 |
|
|
294 |
offset -= date.getTimezoneOffset(); |
|
295 |
time = (Number(date) + (offset * 60 * 1000)); |
|
296 |
var _res = new Date(); |
|
297 |
_res.setTime(Number(time)); |
|
298 |
return _res; |
|
299 |
}, |
|
300 |
dateToIso = Model.dateToIso = function(_d) { |
|
301 |
var d = _d ? new Date(_d) : new Date(); |
|
302 |
return d.getUTCFullYear()+'-' |
|
303 |
+ pad(2, d.getUTCMonth()+1)+'-' |
|
304 |
+ pad(2, d.getUTCDate())+'T' |
|
305 |
+ pad(2, d.getUTCHours())+':' |
|
306 |
+ pad(2, d.getUTCMinutes())+':' |
87
|
307 |
+ pad(2, d.getUTCSeconds())+'Z' ; |
3
|
308 |
}; |
|
309 |
|
|
310 |
/* |
|
311 |
* List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID) |
|
312 |
*/ |
|
313 |
var List = Model.List = function(_directory) { |
|
314 |
Array.call(this); |
|
315 |
this.directory = _directory; |
|
316 |
this.idIndex = []; |
|
317 |
this.__events = {}; |
|
318 |
if (typeof _directory == "undefined") { |
|
319 |
console.trace(); |
|
320 |
throw "Error : new List(directory): directory is undefined"; |
|
321 |
} |
|
322 |
var _this = this; |
|
323 |
this.on("clear-search", function() { |
|
324 |
_this.searching = false; |
|
325 |
_this.regexp = undefined; |
|
326 |
_this.forEach(function(_element) { |
|
327 |
_element.found = undefined; |
|
328 |
}); |
|
329 |
_this.trigger("search-cleared"); |
87
|
330 |
}); |
3
|
331 |
}; |
|
332 |
|
|
333 |
List.prototype = new Array(); |
|
334 |
|
|
335 |
List.prototype.hasId = function(_id) { |
|
336 |
return ns._(this.idIndex).include(_id); |
|
337 |
}; |
|
338 |
|
|
339 |
/* On recent browsers, forEach and map are defined and do what we want. |
|
340 |
* Otherwise, we'll use the Underscore.js functions |
|
341 |
*/ |
|
342 |
if (typeof Array.prototype.forEach === "undefined") { |
|
343 |
List.prototype.forEach = function(_callback) { |
|
344 |
var _this = this; |
|
345 |
ns._(this).forEach(function(_value, _key) { |
|
346 |
_callback(_value, _key, _this); |
|
347 |
}); |
87
|
348 |
}; |
3
|
349 |
}; |
|
350 |
|
|
351 |
if (typeof Array.prototype.map === "undefined") { |
|
352 |
List.prototype.map = function(_callback) { |
|
353 |
var _this = this; |
|
354 |
return ns._(this).map(function(_value, _key) { |
|
355 |
return _callback(_value, _key, _this); |
|
356 |
}); |
87
|
357 |
}; |
3
|
358 |
}; |
|
359 |
|
|
360 |
List.prototype.pluck = function(_key) { |
|
361 |
return this.map(function(_value) { |
|
362 |
return _value[_key]; |
|
363 |
}); |
|
364 |
}; |
|
365 |
|
|
366 |
/* We override Array's filter function because it doesn't return an List |
|
367 |
*/ |
|
368 |
List.prototype.filter = function(_callback) { |
|
369 |
var _this = this, |
|
370 |
_res = new List(this.directory); |
|
371 |
_res.addElements(ns._(this).filter(function(_value, _key) { |
|
372 |
return _callback(_value, _key, _this); |
|
373 |
})); |
|
374 |
return _res; |
|
375 |
}; |
|
376 |
|
|
377 |
List.prototype.slice = function(_start, _end) { |
|
378 |
var _res = new List(this.directory); |
|
379 |
_res.addElements(Array.prototype.slice.call(this, _start, _end)); |
|
380 |
return _res; |
|
381 |
}; |
|
382 |
|
|
383 |
List.prototype.splice = function(_start, _end) { |
|
384 |
var _res = new List(this.directory); |
|
385 |
_res.addElements(Array.prototype.splice.call(this, _start, _end)); |
|
386 |
this.idIndex.splice(_start, _end); |
|
387 |
return _res; |
|
388 |
}; |
|
389 |
|
|
390 |
/* Array has a sort function, but it's not as interesting as Underscore.js's sortBy |
|
391 |
* and won't return a new List |
|
392 |
*/ |
|
393 |
List.prototype.sortBy = function(_callback) { |
|
394 |
var _this = this, |
|
395 |
_res = new List(this.directory); |
|
396 |
_res.addElements(ns._(this).sortBy(function(_value, _key) { |
|
397 |
return _callback(_value, _key, _this); |
|
398 |
})); |
|
399 |
return _res; |
|
400 |
}; |
|
401 |
|
|
402 |
/* Title and Description are basic information for (almost) all element types, |
|
403 |
* here we can search by these criteria |
|
404 |
*/ |
|
405 |
List.prototype.searchByTitle = function(_text, _iexact) { |
|
406 |
var _iexact = _iexact || false, |
|
407 |
_rgxp = regexpFromTextOrArray(_text, true, _iexact); |
|
408 |
return this.filter(function(_element) { |
|
409 |
return _rgxp.test(_element.title); |
|
410 |
}); |
|
411 |
}; |
|
412 |
|
|
413 |
List.prototype.searchByDescription = function(_text, _iexact) { |
|
414 |
var _iexact = _iexact || false, |
|
415 |
_rgxp = regexpFromTextOrArray(_text, true, _iexact); |
|
416 |
return this.filter(function(_element) { |
|
417 |
return _rgxp.test(_element.description); |
|
418 |
}); |
|
419 |
}; |
|
420 |
|
|
421 |
List.prototype.searchByTextFields = function(_text, _iexact) { |
|
422 |
var _iexact = _iexact || false, |
|
423 |
_rgxp = regexpFromTextOrArray(_text, true, _iexact); |
|
424 |
return this.filter(function(_element) { |
|
425 |
var keywords = (_element.keywords || _element.getTagTexts() || []).join(", "); |
|
426 |
return _rgxp.test(_element.description) || _rgxp.test(_element.title) || _rgxp.test(keywords); |
|
427 |
}); |
|
428 |
}; |
|
429 |
|
|
430 |
List.prototype.search = function(_text) { |
|
431 |
if (!_text) { |
|
432 |
this.trigger("clear-search"); |
|
433 |
return this; |
|
434 |
} |
|
435 |
this.searching = true; |
|
436 |
this.trigger("search", _text); |
87
|
437 |
var rxsource = fullTextRegexps(_text), |
|
438 |
rgxp = new RegExp(rxsource,"im"); |
|
439 |
this.regexp = new RegExp(rxsource,"gim"); |
3
|
440 |
var res = this.filter(function(_element, _k) { |
|
441 |
var titlematch = rgxp.test(_element.title), |
|
442 |
descmatch = rgxp.test(_element.description), |
|
443 |
_isfound = !!(titlematch || descmatch); |
|
444 |
_element.found = _isfound; |
|
445 |
_element.trigger(_isfound ? "found" : "not-found"); |
|
446 |
return _isfound; |
|
447 |
}); |
|
448 |
this.trigger(res.length ? "found" : "not-found",res); |
|
449 |
return res; |
|
450 |
}; |
|
451 |
|
|
452 |
List.prototype.getTitles = function() { |
|
453 |
return this.map(function(_el) { |
|
454 |
return _el.title; |
|
455 |
}); |
|
456 |
}; |
|
457 |
|
|
458 |
List.prototype.addId = function(_id) { |
87
|
459 |
var _el = this.directory.getElement(_id); |
3
|
460 |
if (!this.hasId(_id) && typeof _el !== "undefined") { |
|
461 |
this.idIndex.push(_id); |
|
462 |
Array.prototype.push.call(this, _el); |
|
463 |
} |
|
464 |
}; |
|
465 |
|
|
466 |
List.prototype.push = function(_el) { |
|
467 |
if (typeof _el === "undefined") { |
|
468 |
return; |
|
469 |
} |
|
470 |
var _index = (ns._(this.idIndex).indexOf(_el.id)); |
|
471 |
if (_index === -1) { |
|
472 |
this.idIndex.push(_el.id); |
|
473 |
Array.prototype.push.call(this, _el); |
|
474 |
} else { |
|
475 |
this[_index] = _el; |
|
476 |
} |
|
477 |
}; |
|
478 |
|
|
479 |
List.prototype.addIds = function(_array) { |
|
480 |
var _l = _array.length, |
|
481 |
_this = this; |
|
482 |
ns._(_array).forEach(function(_id) { |
|
483 |
_this.addId(_id); |
|
484 |
}); |
|
485 |
}; |
|
486 |
|
|
487 |
List.prototype.addElements = function(_array) { |
|
488 |
var _this = this; |
|
489 |
ns._(_array).forEach(function(_el) { |
|
490 |
_this.push(_el); |
|
491 |
}); |
|
492 |
}; |
|
493 |
|
|
494 |
List.prototype.removeId = function(_id, _deleteFromDirectory) { |
|
495 |
var _deleteFromDirectory = _deleteFromDirectory || false, |
|
496 |
_index = (ns._(this.idIndex).indexOf(_id)); |
|
497 |
if (_index !== -1) { |
|
498 |
this.splice(_index,1); |
|
499 |
} |
|
500 |
if (_deleteFromDirectory) { |
|
501 |
delete this.directory.elements[_id]; |
|
502 |
} |
|
503 |
}; |
|
504 |
|
|
505 |
List.prototype.removeElement = function(_el, _deleteFromDirectory) { |
|
506 |
var _deleteFromDirectory = _deleteFromDirectory || false; |
|
507 |
this.removeId(_el.id); |
|
508 |
}; |
|
509 |
|
|
510 |
List.prototype.removeIds = function(_list, _deleteFromDirectory) { |
|
511 |
var _deleteFromDirectory = _deleteFromDirectory || false, |
|
512 |
_this = this; |
|
513 |
ns._(_list).forEach(function(_id) { |
|
514 |
_this.removeId(_id); |
|
515 |
}); |
|
516 |
}; |
|
517 |
|
|
518 |
List.prototype.removeElements = function(_list, _deleteFromDirectory) { |
|
519 |
var _deleteFromDirectory = _deleteFromDirectory || false, |
|
520 |
_this = this; |
|
521 |
ns._(_list).forEach(function(_el) { |
|
522 |
_this.removeElement(_el); |
|
523 |
}); |
|
524 |
}; |
|
525 |
|
|
526 |
List.prototype.on = function(_event, _callback) { |
|
527 |
if (typeof this.__events[_event] === "undefined") { |
|
528 |
this.__events[_event] = []; |
|
529 |
} |
|
530 |
this.__events[_event].push(_callback); |
|
531 |
}; |
|
532 |
|
|
533 |
List.prototype.off = function(_event, _callback) { |
|
534 |
if (typeof this.__events[_event] !== "undefined") { |
|
535 |
this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) { |
|
536 |
return _fn === _callback; |
|
537 |
}); |
|
538 |
} |
|
539 |
}; |
|
540 |
|
|
541 |
List.prototype.trigger = function(_event, _data) { |
|
542 |
var _list = this; |
|
543 |
ns._(this.__events[_event]).each(function(_callback) { |
|
544 |
_callback.call(_list, _data); |
|
545 |
}); |
|
546 |
}; |
|
547 |
|
|
548 |
/* A simple time management object, that helps converting millisecs to seconds and strings, |
|
549 |
* without the clumsiness of the original Date object. |
|
550 |
*/ |
|
551 |
|
|
552 |
var Time = Model.Time = function(_milliseconds) { |
|
553 |
this.milliseconds = 0; |
|
554 |
this.setMilliseconds(_milliseconds); |
|
555 |
}; |
|
556 |
|
|
557 |
Time.prototype.setMilliseconds = function(_milliseconds) { |
|
558 |
var _ante = this.milliseconds; |
|
559 |
switch(typeof _milliseconds) { |
|
560 |
case "string": |
|
561 |
this.milliseconds = parseInt(_milliseconds); |
|
562 |
break; |
|
563 |
case "number": |
|
564 |
this.milliseconds = Math.floor(_milliseconds); |
|
565 |
break; |
|
566 |
case "object": |
|
567 |
this.milliseconds = parseInt(_milliseconds.valueOf()); |
|
568 |
break; |
|
569 |
default: |
|
570 |
this.milliseconds = 0; |
|
571 |
} |
|
572 |
if (this.milliseconds === NaN) { |
|
573 |
this.milliseconds = _ante; |
|
574 |
} |
|
575 |
}; |
|
576 |
|
|
577 |
Time.prototype.setSeconds = function(_seconds) { |
|
578 |
this.milliseconds = 1000 * _seconds; |
|
579 |
}; |
|
580 |
|
|
581 |
Time.prototype.getSeconds = function() { |
|
582 |
return this.milliseconds / 1000; |
|
583 |
}; |
|
584 |
|
|
585 |
Time.prototype.getHMS = function() { |
|
586 |
var _totalSeconds = Math.abs(Math.floor(this.getSeconds())); |
|
587 |
return { |
|
588 |
hours : Math.floor(_totalSeconds / 3600), |
|
589 |
minutes : (Math.floor(_totalSeconds / 60) % 60), |
|
590 |
seconds : _totalSeconds % 60, |
|
591 |
milliseconds: this.milliseconds % 1000 |
87
|
592 |
}; |
3
|
593 |
}; |
|
594 |
|
|
595 |
Time.prototype.add = function(_milliseconds) { |
|
596 |
this.milliseconds += new Time(_milliseconds).milliseconds; |
|
597 |
}; |
|
598 |
|
|
599 |
Time.prototype.valueOf = function() { |
|
600 |
return this.milliseconds; |
|
601 |
}; |
|
602 |
|
|
603 |
Time.prototype.toString = function(showCs) { |
|
604 |
var _hms = this.getHMS(), |
|
605 |
_res = ''; |
|
606 |
if (_hms.hours) { |
87
|
607 |
_res += _hms.hours + ':'; |
3
|
608 |
} |
|
609 |
_res += pad(2, _hms.minutes) + ':' + pad(2, _hms.seconds); |
|
610 |
if (showCs) { |
87
|
611 |
_res += "." + Math.floor(_hms.milliseconds / 100); |
3
|
612 |
} |
|
613 |
return _res; |
|
614 |
}; |
|
615 |
|
|
616 |
/* Reference handles references between elements |
|
617 |
*/ |
|
618 |
|
|
619 |
var Reference = Model.Reference = function(_source, _idRef) { |
|
620 |
this.source = _source; |
|
621 |
this.id = _idRef; |
|
622 |
if (typeof _idRef === "object") { |
|
623 |
this.isList = true; |
|
624 |
} else { |
|
625 |
this.isList = false; |
|
626 |
} |
|
627 |
this.refresh(); |
|
628 |
}; |
|
629 |
|
|
630 |
Reference.prototype.refresh = function() { |
|
631 |
if (this.isList) { |
|
632 |
this.contents = new List(this.source.directory); |
|
633 |
this.contents.addIds(this.id); |
|
634 |
} else { |
|
635 |
this.contents = this.source.getElement(this.id); |
|
636 |
} |
|
637 |
|
|
638 |
}; |
|
639 |
|
|
640 |
Reference.prototype.getContents = function() { |
|
641 |
if (typeof this.contents === "undefined" || (this.isList && this.contents.length != this.id.length)) { |
|
642 |
this.refresh(); |
|
643 |
} |
|
644 |
return this.contents; |
|
645 |
}; |
|
646 |
|
|
647 |
Reference.prototype.isOrHasId = function(_idRef) { |
|
648 |
if (this.isList) { |
87
|
649 |
return (ns._(this.id).indexOf(_idRef) !== -1); |
3
|
650 |
} else { |
|
651 |
return (this.id == _idRef); |
|
652 |
} |
|
653 |
}; |
|
654 |
|
|
655 |
/* */ |
|
656 |
|
|
657 |
var BaseElement = Model.Element = function(_id, _source) { |
|
658 |
this.elementType = 'element'; |
|
659 |
this.title = ""; |
|
660 |
this.description = ""; |
87
|
661 |
this.__events = {}; |
3
|
662 |
if (typeof _source === "undefined") { |
|
663 |
return; |
|
664 |
} |
|
665 |
if (typeof _id === "undefined" || !_id) { |
|
666 |
_id = getUID(); |
|
667 |
} |
|
668 |
this.id = _id; |
|
669 |
this.source = _source; |
|
670 |
if (_source !== this) { |
|
671 |
this.source.directory.addElement(this); |
|
672 |
} |
|
673 |
}; |
|
674 |
|
|
675 |
BaseElement.prototype.toString = function() { |
|
676 |
return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : ''); |
|
677 |
}; |
|
678 |
|
|
679 |
BaseElement.prototype.setReference = function(_elementType, _idRef) { |
|
680 |
this[_elementType] = new Reference(this.source, _idRef); |
|
681 |
}; |
|
682 |
|
|
683 |
BaseElement.prototype.getReference = function(_elementType) { |
|
684 |
if (typeof this[_elementType] !== "undefined") { |
|
685 |
return this[_elementType].getContents(); |
|
686 |
} |
|
687 |
}; |
|
688 |
|
|
689 |
BaseElement.prototype.getRelated = function(_elementType, _global) { |
|
690 |
_global = (typeof _global !== "undefined" && _global); |
|
691 |
var _this = this; |
|
692 |
return this.source.getList(_elementType, _global).filter(function(_el) { |
|
693 |
var _ref = _el[_this.elementType]; |
|
694 |
return _ref && _ref.isOrHasId(_this.id); |
|
695 |
}); |
|
696 |
}; |
|
697 |
|
|
698 |
BaseElement.prototype.on = function(_event, _callback) { |
|
699 |
if (typeof this.__events[_event] === "undefined") { |
|
700 |
this.__events[_event] = []; |
|
701 |
} |
|
702 |
this.__events[_event].push(_callback); |
|
703 |
}; |
|
704 |
|
|
705 |
BaseElement.prototype.off = function(_event, _callback) { |
|
706 |
if (typeof this.__events[_event] !== "undefined") { |
|
707 |
this.__events[_event] = ns._(this.__events[_event]).reject(function(_fn) { |
|
708 |
return _fn === _callback; |
|
709 |
}); |
|
710 |
} |
|
711 |
}; |
|
712 |
|
|
713 |
BaseElement.prototype.trigger = function(_event, _data) { |
|
714 |
var _element = this; |
|
715 |
ns._(this.__events[_event]).each(function(_callback) { |
|
716 |
_callback.call(_element, _data); |
|
717 |
}); |
|
718 |
}; |
|
719 |
|
|
720 |
/* */ |
|
721 |
|
|
722 |
var Playable = Model.Playable = function(_id, _source) { |
|
723 |
BaseElement.call(this, _id, _source); |
|
724 |
if (typeof _source === "undefined") { |
|
725 |
return; |
|
726 |
} |
|
727 |
this.elementType = 'playable'; |
|
728 |
this.currentTime = new Time(); |
|
729 |
this.volume = .5; |
|
730 |
this.paused = true; |
|
731 |
this.muted = false; |
|
732 |
this.loadedMetadata = false; |
|
733 |
var _this = this; |
|
734 |
this.on("play", function() { |
|
735 |
_this.paused = false; |
|
736 |
}); |
|
737 |
this.on("pause", function() { |
|
738 |
_this.paused = true; |
|
739 |
}); |
|
740 |
this.on("timeupdate", function(_time) { |
|
741 |
_this.currentTime = _time; |
|
742 |
_this.getAnnotations().filter(function(_a) { |
87
|
743 |
return (_a.end <= _time || _a.begin > _time) && _a.playing; |
3
|
744 |
}).forEach(function(_a) { |
|
745 |
_a.playing = false; |
|
746 |
_a.trigger("leave"); |
|
747 |
_this.trigger("leave-annotation",_a); |
|
748 |
}); |
|
749 |
_this.getAnnotations().filter(function(_a) { |
87
|
750 |
return _a.begin <= _time && _a.end > _time && !_a.playing; |
3
|
751 |
}).forEach(function(_a) { |
|
752 |
_a.playing = true; |
|
753 |
_a.trigger("enter"); |
|
754 |
_this.trigger("enter-annotation",_a); |
|
755 |
}); |
|
756 |
}); |
|
757 |
this.on("loadedmetadata", function() { |
|
758 |
_this.loadedMetadata = true; |
|
759 |
}); |
|
760 |
}; |
|
761 |
|
|
762 |
extendPrototype(Playable, BaseElement); |
|
763 |
|
|
764 |
Playable.prototype.getCurrentTime = function() { |
|
765 |
return this.currentTime; |
|
766 |
}; |
|
767 |
|
|
768 |
Playable.prototype.getVolume = function() { |
|
769 |
return this.volume; |
|
770 |
}; |
|
771 |
|
|
772 |
Playable.prototype.getPaused = function() { |
|
773 |
return this.paused; |
|
774 |
}; |
|
775 |
|
|
776 |
Playable.prototype.getMuted = function() { |
|
777 |
return this.muted; |
|
778 |
}; |
|
779 |
|
|
780 |
Playable.prototype.setCurrentTime = function(_time) { |
|
781 |
this.trigger("setcurrenttime",_time); |
|
782 |
}; |
|
783 |
|
|
784 |
Playable.prototype.setVolume = function(_vol) { |
|
785 |
this.trigger("setvolume",_vol); |
|
786 |
}; |
|
787 |
|
|
788 |
Playable.prototype.setMuted = function(_muted) { |
|
789 |
this.trigger("setmuted",_muted); |
|
790 |
}; |
|
791 |
|
|
792 |
Playable.prototype.play = function() { |
|
793 |
this.trigger("setplay"); |
|
794 |
}; |
|
795 |
|
|
796 |
Playable.prototype.pause = function() { |
|
797 |
this.trigger("setpause"); |
|
798 |
}; |
|
799 |
|
|
800 |
Playable.prototype.show = function() {}; |
|
801 |
|
|
802 |
Playable.prototype.hide = function() {}; |
|
803 |
|
|
804 |
/* */ |
|
805 |
|
|
806 |
var Media = Model.Media = function(_id, _source) { |
|
807 |
Playable.call(this, _id, _source); |
|
808 |
this.elementType = 'media'; |
|
809 |
this.duration = new Time(); |
|
810 |
this.video = ''; |
|
811 |
var _this = this; |
|
812 |
}; |
|
813 |
|
|
814 |
extendPrototype(Media, Playable); |
|
815 |
|
|
816 |
/* Default functions to be overriden by players */ |
|
817 |
|
|
818 |
Media.prototype.setDuration = function(_durationMs) { |
|
819 |
this.duration.setMilliseconds(_durationMs); |
|
820 |
}; |
|
821 |
|
|
822 |
Media.prototype.getAnnotations = function() { |
|
823 |
return this.getRelated("annotation"); |
|
824 |
}; |
|
825 |
|
|
826 |
Media.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
827 |
var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id"); |
|
828 |
if (_annTypes.length) { |
|
829 |
return this.getAnnotations().filter(function(_annotation) { |
|
830 |
return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1; |
|
831 |
}); |
|
832 |
} else { |
87
|
833 |
return new List(this.source.directory); |
3
|
834 |
} |
|
835 |
}; |
|
836 |
|
|
837 |
/* */ |
|
838 |
|
|
839 |
var Tag = Model.Tag = function(_id, _source) { |
|
840 |
BaseElement.call(this, _id, _source); |
|
841 |
this.elementType = 'tag'; |
|
842 |
}; |
|
843 |
|
|
844 |
extendPrototype(Tag, BaseElement); |
|
845 |
|
|
846 |
Tag.prototype.getAnnotations = function() { |
|
847 |
return this.getRelated("annotation"); |
|
848 |
}; |
|
849 |
|
|
850 |
/* */ |
|
851 |
var AnnotationType = Model.AnnotationType = function(_id, _source) { |
|
852 |
BaseElement.call(this, _id, _source); |
|
853 |
this.elementType = 'annotationType'; |
|
854 |
}; |
|
855 |
|
|
856 |
extendPrototype(AnnotationType, BaseElement); |
|
857 |
|
|
858 |
AnnotationType.prototype.getAnnotations = function() { |
|
859 |
return this.getRelated("annotation"); |
|
860 |
}; |
|
861 |
|
|
862 |
/* Annotation |
|
863 |
* */ |
|
864 |
|
|
865 |
var Annotation = Model.Annotation = function(_id, _source) { |
|
866 |
BaseElement.call(this, _id, _source); |
|
867 |
this.elementType = 'annotation'; |
|
868 |
this.begin = new Time(); |
|
869 |
this.end = new Time(); |
|
870 |
this.tag = new Reference(_source, []); |
|
871 |
this.playing = false; |
|
872 |
var _this = this; |
|
873 |
this.on("click", function() { |
|
874 |
_this.getMedia().setCurrentTime(_this.begin); |
|
875 |
}); |
|
876 |
}; |
|
877 |
|
|
878 |
extendPrototype(Annotation, BaseElement); |
|
879 |
|
|
880 |
Annotation.prototype.setBegin = function(_beginMs) { |
|
881 |
this.begin.setMilliseconds(Math.max(0,_beginMs)); |
|
882 |
this.trigger("change-begin"); |
|
883 |
if (this.end < this.begin) { |
|
884 |
this.setEnd(this.begin); |
|
885 |
} |
|
886 |
}; |
|
887 |
|
|
888 |
Annotation.prototype.setEnd = function(_endMs) { |
|
889 |
this.end.setMilliseconds(Math.min(_endMs, this.getMedia().duration.milliseconds)); |
|
890 |
this.trigger("change-end"); |
|
891 |
if (this.end < this.begin) { |
|
892 |
this.setBegin(this.end); |
|
893 |
} |
|
894 |
}; |
|
895 |
|
|
896 |
Annotation.prototype.setDuration = function(_durMs) { |
|
897 |
this.setEnd(_durMs + this.begin.milliseconds); |
|
898 |
}; |
|
899 |
|
|
900 |
Annotation.prototype.setMedia = function(_idRef) { |
|
901 |
this.setReference("media", _idRef); |
|
902 |
}; |
|
903 |
|
|
904 |
Annotation.prototype.getMedia = function() { |
|
905 |
return this.getReference("media"); |
|
906 |
}; |
|
907 |
|
|
908 |
Annotation.prototype.setAnnotationType = function(_idRef) { |
|
909 |
this.setReference("annotationType", _idRef); |
|
910 |
}; |
|
911 |
|
|
912 |
Annotation.prototype.getAnnotationType = function() { |
|
913 |
return this.getReference("annotationType"); |
|
914 |
}; |
|
915 |
|
|
916 |
Annotation.prototype.setTags = function(_idRefs) { |
|
917 |
this.setReference("tag", _idRefs); |
|
918 |
}; |
|
919 |
|
|
920 |
Annotation.prototype.getTags = function() { |
|
921 |
return this.getReference("tag"); |
|
922 |
}; |
|
923 |
|
|
924 |
Annotation.prototype.getTagTexts = function() { |
|
925 |
return this.getTags().getTitles(); |
|
926 |
}; |
|
927 |
|
|
928 |
Annotation.prototype.getDuration = function() { |
87
|
929 |
return new Time(this.end.milliseconds - this.begin.milliseconds); |
3
|
930 |
}; |
|
931 |
|
|
932 |
/* */ |
|
933 |
|
|
934 |
var MashedAnnotation = Model.MashedAnnotation = function(_mashup, _annotation) { |
|
935 |
BaseElement.call(this, _mashup.id + "_" + _annotation.id, _annotation.source); |
|
936 |
this.elementType = 'mashedAnnotation'; |
|
937 |
this.annotation = _annotation; |
|
938 |
this.begin = new Time(); |
|
939 |
this.end = new Time(); |
|
940 |
this.duration = new Time(); |
|
941 |
this.title = this.annotation.title; |
|
942 |
this.description = this.annotation.description; |
|
943 |
this.color = this.annotation.color; |
|
944 |
var _this = this; |
|
945 |
this.on("click", function() { |
|
946 |
_mashup.setCurrentTime(_this.begin); |
|
947 |
}); |
|
948 |
this.on("enter", function() { |
|
949 |
_this.annotation.trigger("enter"); |
|
950 |
}); |
|
951 |
this.on("leave", function() { |
|
952 |
_this.annotation.trigger("leave"); |
|
953 |
}); |
|
954 |
}; |
|
955 |
|
|
956 |
extendPrototype(MashedAnnotation, BaseElement); |
|
957 |
|
|
958 |
MashedAnnotation.prototype.getMedia = function() { |
|
959 |
return this.annotation.getReference("media"); |
|
960 |
}; |
|
961 |
|
|
962 |
MashedAnnotation.prototype.getAnnotationType = function() { |
|
963 |
return this.annotation.getReference("annotationType"); |
|
964 |
}; |
|
965 |
|
|
966 |
MashedAnnotation.prototype.getTags = function() { |
|
967 |
return this.annotation.getReference("tag"); |
|
968 |
}; |
|
969 |
|
|
970 |
MashedAnnotation.prototype.getTagTexts = function() { |
|
971 |
return this.annotation.getTags().getTitles(); |
|
972 |
}; |
|
973 |
|
|
974 |
MashedAnnotation.prototype.getDuration = function() { |
|
975 |
return this.annotation.getDuration(); |
|
976 |
}; |
|
977 |
|
|
978 |
MashedAnnotation.prototype.setBegin = function(_begin) { |
|
979 |
this.begin.setMilliseconds(_begin); |
|
980 |
this.duration.setMilliseconds(this.annotation.getDuration()); |
|
981 |
this.end.setMilliseconds(_begin + this.duration); |
|
982 |
}; |
|
983 |
|
|
984 |
/* */ |
|
985 |
|
|
986 |
var Mashup = Model.Mashup = function(_id, _source) { |
|
987 |
Playable.call(this, _id, _source); |
|
988 |
this.elementType = 'mashup'; |
|
989 |
this.duration = new Time(); |
|
990 |
this.segments = new List(_source.directory); |
|
991 |
this.loaded = false; |
|
992 |
var _this = this; |
|
993 |
this._updateTimes = function() { |
|
994 |
_this.updateTimes(); |
|
995 |
_this.trigger("change"); |
87
|
996 |
}; |
3
|
997 |
this.on("add", this._updateTimes); |
|
998 |
this.on("remove", this._updateTimes); |
|
999 |
}; |
|
1000 |
|
|
1001 |
extendPrototype(Mashup, Playable); |
|
1002 |
|
|
1003 |
Mashup.prototype.updateTimes = function() { |
|
1004 |
var _time = 0; |
|
1005 |
this.segments.forEach(function(_segment) { |
|
1006 |
_segment.setBegin(_time); |
|
1007 |
_time = _segment.end; |
|
1008 |
}); |
|
1009 |
this.duration.setMilliseconds(_time); |
|
1010 |
}; |
|
1011 |
|
|
1012 |
Mashup.prototype.addAnnotation = function(_annotation, _defer) { |
|
1013 |
var _mashedAnnotation = new MashedAnnotation(this, _annotation), |
|
1014 |
_defer = _defer || false; |
|
1015 |
this.segments.push(_mashedAnnotation); |
|
1016 |
_annotation.on("change-begin", this._updateTimes); |
|
1017 |
_annotation.on("change-end", this._updateTimes); |
|
1018 |
if (!_defer) { |
|
1019 |
this.trigger("add"); |
|
1020 |
} |
|
1021 |
}; |
|
1022 |
|
|
1023 |
Mashup.prototype.addAnnotationById = function(_elId, _defer) { |
|
1024 |
var _annotation = this.source.getElement(_elId), |
|
1025 |
_defer = _defer || false; |
|
1026 |
if (typeof _annotation !== "undefined") { |
|
1027 |
this.addAnnotation(_annotation, _defer); |
|
1028 |
} |
|
1029 |
}; |
|
1030 |
|
|
1031 |
Mashup.prototype.addAnnotations = function(_segments) { |
|
1032 |
var _this = this; |
|
1033 |
ns._(_segments).forEach(function(_segment) { |
|
1034 |
_this.addAnnotation(_segment, true); |
|
1035 |
}); |
|
1036 |
this.trigger("add"); |
|
1037 |
}; |
|
1038 |
|
|
1039 |
Mashup.prototype.addAnnotationsById = function(_segments) { |
|
1040 |
var _this = this; |
|
1041 |
ns._(_segments).forEach(function(_segment) { |
|
1042 |
_this.addAnnotationById(_segment, true); |
|
1043 |
}); |
|
1044 |
this.trigger("add"); |
|
1045 |
}; |
|
1046 |
|
|
1047 |
Mashup.prototype.removeAnnotation = function(_annotation, _defer) { |
|
1048 |
var _defer = _defer || false; |
|
1049 |
_annotation.off("change-begin", this._updateTimes); |
|
1050 |
_annotation.off("change-end", this._updateTimes); |
|
1051 |
this.segments.removeId(this.id + "_" + _annotation.id); |
|
1052 |
if (!_defer) { |
|
1053 |
this.trigger("remove"); |
|
1054 |
} |
|
1055 |
}; |
|
1056 |
|
|
1057 |
Mashup.prototype.removeAnnotationById = function(_annId, _defer) { |
|
1058 |
var _defer = _defer || false; |
|
1059 |
var _annotation = this.source.getElement(_annId); |
|
1060 |
|
|
1061 |
if (_annotation) { |
|
1062 |
this.removeAnnotation(_annotation, _defer); |
|
1063 |
} |
|
1064 |
if (!_defer) { |
|
1065 |
this.trigger("remove"); |
|
1066 |
} |
|
1067 |
}; |
|
1068 |
|
|
1069 |
Mashup.prototype.setAnnotations = function(_segments) { |
|
1070 |
while (this.segments.length) { |
|
1071 |
this.removeAnnotation(this.segments[0].annotation, true); |
|
1072 |
} |
|
1073 |
this.addAnnotations(_segments); |
|
1074 |
}; |
|
1075 |
|
|
1076 |
Mashup.prototype.setAnnotationsById = function(_segments) { |
|
1077 |
while (this.segments.length) { |
|
1078 |
this.removeAnnotation(this.segments[0].annotation, true); |
|
1079 |
} |
|
1080 |
this.addAnnotationsById(_segments); |
|
1081 |
}; |
|
1082 |
|
|
1083 |
Mashup.prototype.hasAnnotation = function(_annotation) { |
|
1084 |
return !!ns._(this.segments).find(function(_s) { |
87
|
1085 |
return _s.annotation === _annotation; |
3
|
1086 |
}); |
|
1087 |
}; |
|
1088 |
|
|
1089 |
Mashup.prototype.getAnnotation = function(_annotation) { |
|
1090 |
return ns._(this.segments).find(function(_s) { |
87
|
1091 |
return _s.annotation === _annotation; |
3
|
1092 |
}); |
|
1093 |
}; |
|
1094 |
|
|
1095 |
Mashup.prototype.getAnnotationById = function(_id) { |
|
1096 |
return ns._(this.segments).find(function(_s) { |
87
|
1097 |
return _s.annotation.id === _id; |
3
|
1098 |
}); |
|
1099 |
}; |
|
1100 |
|
|
1101 |
Mashup.prototype.getAnnotations = function() { |
|
1102 |
return this.segments; |
|
1103 |
}; |
|
1104 |
|
|
1105 |
Mashup.prototype.getOriginalAnnotations = function() { |
|
1106 |
var annotations = new List(this.source.directory); |
|
1107 |
this.segments.forEach(function(_s) { |
|
1108 |
annotations.push(_s.annotation); |
|
1109 |
}); |
|
1110 |
return annotations; |
|
1111 |
}; |
|
1112 |
|
|
1113 |
Mashup.prototype.getMedias = function() { |
|
1114 |
var medias = new List(this.source.directory); |
|
1115 |
this.segments.forEach(function(_annotation) { |
87
|
1116 |
medias.push(_annotation.getMedia()); |
|
1117 |
}); |
3
|
1118 |
return medias; |
|
1119 |
}; |
|
1120 |
|
|
1121 |
Mashup.prototype.getAnnotationsByTypeTitle = function(_title) { |
|
1122 |
var _annTypes = this.source.getAnnotationTypes().searchByTitle(_title).pluck("id"); |
|
1123 |
if (_annTypes.length) { |
|
1124 |
return this.getAnnotations().filter(function(_annotation) { |
|
1125 |
return ns._(_annTypes).indexOf(_annotation.getAnnotationType().id) !== -1; |
|
1126 |
}); |
|
1127 |
} else { |
87
|
1128 |
return new List(this.source.directory); |
3
|
1129 |
} |
|
1130 |
}; |
|
1131 |
|
|
1132 |
Mashup.prototype.getAnnotationAtTime = function(_time) { |
|
1133 |
var _list = this.segments.filter(function(_annotation) { |
|
1134 |
return _annotation.begin <= _time && _annotation.end > _time; |
|
1135 |
}); |
|
1136 |
if (_list.length) { |
|
1137 |
return _list[0]; |
|
1138 |
} else { |
|
1139 |
return undefined; |
|
1140 |
} |
|
1141 |
}; |
|
1142 |
|
|
1143 |
Mashup.prototype.getMediaAtTime = function(_time) { |
|
1144 |
var _annotation = this.getAnnotationAtTime(_time); |
|
1145 |
if (typeof _annotation !== "undefined") { |
|
1146 |
return _annotation.getMedia(); |
|
1147 |
} else { |
|
1148 |
return undefined; |
|
1149 |
} |
|
1150 |
}; |
|
1151 |
|
|
1152 |
/* */ |
|
1153 |
|
|
1154 |
var Source = Model.Source = function(_config) { |
|
1155 |
BaseElement.call(this, false, this); |
|
1156 |
this.status = _SOURCE_STATUS_EMPTY; |
|
1157 |
this.elementType = "source"; |
|
1158 |
if (typeof _config !== "undefined") { |
|
1159 |
var _this = this; |
|
1160 |
ns._(_config).forEach(function(_v, _k) { |
|
1161 |
_this[_k] = _v; |
87
|
1162 |
}); |
3
|
1163 |
this.callbackQueue = []; |
|
1164 |
this.contents = {}; |
|
1165 |
this.get(); |
|
1166 |
} |
|
1167 |
}; |
|
1168 |
|
|
1169 |
extendPrototype(Source, BaseElement); |
|
1170 |
|
|
1171 |
Source.prototype.addList = function(_listId, _contents) { |
|
1172 |
if (typeof this.contents[_listId] === "undefined") { |
|
1173 |
this.contents[_listId] = new List(this.directory); |
|
1174 |
} |
|
1175 |
this.contents[_listId].addElements(_contents); |
|
1176 |
}; |
|
1177 |
|
|
1178 |
Source.prototype.getList = function(_listId, _global) { |
|
1179 |
_global = (typeof _global !== "undefined" && _global); |
|
1180 |
if (_global) { |
|
1181 |
return this.directory.getGlobalList().filter(function(_e) { |
|
1182 |
return (_e.elementType === _listId); |
|
1183 |
}); |
|
1184 |
} else { |
18
|
1185 |
if (typeof this.contents[_listId] === "undefined") { |
87
|
1186 |
this.contents[_listId] = new List(this.directory); |
18
|
1187 |
} |
|
1188 |
return this.contents[_listId]; |
3
|
1189 |
} |
|
1190 |
}; |
|
1191 |
|
|
1192 |
Source.prototype.forEach = function(_callback) { |
|
1193 |
var _this = this; |
|
1194 |
ns._(this.contents).forEach(function(_value, _key) { |
|
1195 |
_callback.call(_this, _value, _key); |
87
|
1196 |
}); |
3
|
1197 |
}; |
|
1198 |
|
|
1199 |
Source.prototype.getElement = function(_elId) { |
|
1200 |
return this.directory.getElement(_elId); |
|
1201 |
}; |
|
1202 |
|
|
1203 |
Source.prototype.get = function() { |
|
1204 |
this.status = _SOURCE_STATUS_WAITING; |
|
1205 |
this.handleCallbacks(); |
|
1206 |
}; |
|
1207 |
|
|
1208 |
/* We defer the callbacks calls so they execute after the queue is cleared */ |
|
1209 |
Source.prototype.deferCallback = function(_callback) { |
|
1210 |
var _this = this; |
|
1211 |
ns._.defer(function() { |
|
1212 |
_callback.call(_this); |
|
1213 |
}); |
|
1214 |
}; |
|
1215 |
|
|
1216 |
Source.prototype.handleCallbacks = function() { |
|
1217 |
this.status = _SOURCE_STATUS_READY; |
|
1218 |
while (this.callbackQueue.length) { |
|
1219 |
this.deferCallback(this.callbackQueue.splice(0,1)[0]); |
|
1220 |
} |
|
1221 |
}; |
|
1222 |
Source.prototype.onLoad = function(_callback) { |
|
1223 |
if (this.status === _SOURCE_STATUS_READY) { |
|
1224 |
this.deferCallback(_callback); |
|
1225 |
} else { |
|
1226 |
this.callbackQueue.push(_callback); |
|
1227 |
} |
|
1228 |
}; |
|
1229 |
|
|
1230 |
Source.prototype.serialize = function() { |
|
1231 |
return this.serializer.serialize(this); |
|
1232 |
}; |
|
1233 |
|
|
1234 |
Source.prototype.deSerialize = function(_data) { |
|
1235 |
this.serializer.deSerialize(_data, this); |
|
1236 |
}; |
|
1237 |
|
|
1238 |
Source.prototype.getAnnotations = function(_global) { |
|
1239 |
_global = (typeof _global !== "undefined" && _global); |
|
1240 |
return this.getList("annotation", _global); |
|
1241 |
}; |
|
1242 |
|
|
1243 |
Source.prototype.getMedias = function(_global) { |
|
1244 |
_global = (typeof _global !== "undefined" && _global); |
|
1245 |
return this.getList("media", _global); |
|
1246 |
}; |
|
1247 |
|
|
1248 |
Source.prototype.getTags = function(_global) { |
|
1249 |
_global = (typeof _global !== "undefined" && _global); |
|
1250 |
return this.getList("tag", _global); |
|
1251 |
}; |
|
1252 |
|
|
1253 |
Source.prototype.getMashups = function(_global) { |
|
1254 |
_global = (typeof _global !== "undefined" && _global); |
|
1255 |
return this.getList("mashup", _global); |
|
1256 |
}; |
|
1257 |
|
|
1258 |
Source.prototype.getAnnotationTypes = function(_global) { |
|
1259 |
_global = (typeof _global !== "undefined" && _global); |
|
1260 |
return this.getList("annotationType", _global); |
|
1261 |
}; |
|
1262 |
|
|
1263 |
Source.prototype.getAnnotationsByTypeTitle = function(_title, _global) { |
|
1264 |
_global = (typeof _global !== "undefined" && _global); |
|
1265 |
var _res = new List(this.directory), |
|
1266 |
_annTypes = this.getAnnotationTypes(_global).searchByTitle(_title); |
|
1267 |
_annTypes.forEach(function(_annType) { |
|
1268 |
_res.addElements(_annType.getAnnotations(_global)); |
87
|
1269 |
}); |
3
|
1270 |
return _res; |
|
1271 |
}; |
|
1272 |
|
|
1273 |
Source.prototype.getDuration = function() { |
|
1274 |
var _m = this.currentMedia; |
|
1275 |
if (typeof _m !== "undefined") { |
|
1276 |
return this.currentMedia.duration; |
|
1277 |
} |
|
1278 |
}; |
|
1279 |
|
|
1280 |
Source.prototype.getCurrentMedia = function(_opts) { |
|
1281 |
if (typeof this.currentMedia === "undefined") { |
|
1282 |
if (_opts.is_mashup) { |
|
1283 |
var _mashups = this.getMashups(); |
|
1284 |
if (_mashups.length) { |
|
1285 |
this.currentMedia = _mashups[0]; |
|
1286 |
} |
|
1287 |
} else { |
|
1288 |
var _medias = this.getMedias(); |
|
1289 |
if (_medias.length) { |
|
1290 |
this.currentMedia = _medias[0]; |
|
1291 |
} |
|
1292 |
} |
|
1293 |
} |
|
1294 |
return this.currentMedia; |
|
1295 |
}; |
|
1296 |
|
|
1297 |
Source.prototype.merge = function(_source) { |
|
1298 |
var _this = this; |
|
1299 |
_source.forEach(function(_value, _key) { |
|
1300 |
_this.getList(_key).addElements(_value); |
|
1301 |
}); |
|
1302 |
}; |
|
1303 |
|
|
1304 |
/* */ |
|
1305 |
|
|
1306 |
var RemoteSource = Model.RemoteSource = function(_config) { |
|
1307 |
Source.call(this, _config); |
|
1308 |
}; |
|
1309 |
|
|
1310 |
extendPrototype(RemoteSource, Source); |
|
1311 |
|
|
1312 |
RemoteSource.prototype.get = function() { |
|
1313 |
this.status = _SOURCE_STATUS_WAITING; |
|
1314 |
var _this = this, |
|
1315 |
urlparams = this.url_params || {}, |
|
1316 |
dataType = (isLocalURL(this.url) ? "json" : "jsonp"); |
|
1317 |
urlparams.format = dataType; |
|
1318 |
ns.jQuery.ajax({ |
|
1319 |
url: this.url, |
|
1320 |
dataType: dataType, |
|
1321 |
data: urlparams, |
|
1322 |
traditional: true, |
|
1323 |
success: function(_result) { |
|
1324 |
_this.deSerialize(_result); |
|
1325 |
_this.handleCallbacks(); |
|
1326 |
} |
|
1327 |
}); |
|
1328 |
}; |
|
1329 |
|
|
1330 |
/* */ |
|
1331 |
|
|
1332 |
var Directory = Model.Directory = function() { |
|
1333 |
this.remoteSources = {}; |
|
1334 |
this.elements = {}; |
|
1335 |
}; |
|
1336 |
|
|
1337 |
Directory.prototype.remoteSource = function(_properties) { |
|
1338 |
if (typeof _properties !== "object" || typeof _properties.url === "undefined") { |
|
1339 |
throw "Error : Directory.remoteSource(configuration): configuration.url is undefined"; |
|
1340 |
} |
|
1341 |
var _config = ns._({ directory: this }).extend(_properties); |
|
1342 |
_config.url_params = _config.url_params || {}; |
|
1343 |
var _hash = _config.url + "?" + ns.jQuery.param(_config.url_params); |
|
1344 |
if (typeof this.remoteSources[_hash] === "undefined") { |
|
1345 |
this.remoteSources[_hash] = new RemoteSource(_config); |
|
1346 |
} |
|
1347 |
return this.remoteSources[_hash]; |
|
1348 |
}; |
|
1349 |
|
|
1350 |
Directory.prototype.newLocalSource = function(_properties) { |
|
1351 |
var _config = ns._({ directory: this }).extend(_properties), |
|
1352 |
_res = new Source(_config); |
|
1353 |
return _res; |
|
1354 |
}; |
|
1355 |
|
|
1356 |
Directory.prototype.getElement = function(_id) { |
|
1357 |
return this.elements[_id]; |
|
1358 |
}; |
|
1359 |
|
|
1360 |
Directory.prototype.addElement = function(_element) { |
|
1361 |
this.elements[_element.id] = _element; |
|
1362 |
}; |
|
1363 |
|
|
1364 |
Directory.prototype.getGlobalList = function() { |
|
1365 |
var _res = new List(this); |
|
1366 |
_res.addIds(ns._(this.elements).keys()); |
|
1367 |
return _res; |
|
1368 |
}; |
|
1369 |
return Model; |
|
1370 |
|
|
1371 |
})(IriSP); |
|
1372 |
|
|
1373 |
/* END js */ |
|
1374 |
|
|
1375 |
/* HTML player, to be reused in a widget, or elsewhere */ |
|
1376 |
|
|
1377 |
IriSP.htmlPlayer = function(media, jqselector, options) { |
|
1378 |
|
|
1379 |
var opts = options || {}, |
|
1380 |
videoURL = opts.video || media.video; |
|
1381 |
|
|
1382 |
if (typeof opts.url_transform === "function") { |
|
1383 |
videoURL = opts.url_transform(videoURL); |
|
1384 |
} |
|
1385 |
|
|
1386 |
var videoEl = IriSP.jQuery('<video>'); |
|
1387 |
|
|
1388 |
videoEl.attr({ |
|
1389 |
width : opts.width || undefined, |
|
1390 |
height : opts.height || undefined, |
|
1391 |
controls : opts.controls || undefined, |
|
1392 |
autoplay : opts.autostart || opts.autoplay || undefined |
|
1393 |
}); |
|
1394 |
|
|
1395 |
if(typeof videoURL === "string"){ |
|
1396 |
videoEl.attr("src",videoURL); |
|
1397 |
} else { |
|
1398 |
for (var i = 0; i < videoURL.length; i++) { |
|
1399 |
var _srcNode = IriSP.jQuery('<source>'); |
|
1400 |
_srcNode.attr({ |
|
1401 |
src: videoURL[i].src, |
|
1402 |
type: videoURL[i].type |
|
1403 |
}); |
|
1404 |
videoEl.append(_srcNode); |
|
1405 |
} |
|
1406 |
} |
|
1407 |
|
|
1408 |
jqselector.html(videoEl); |
|
1409 |
|
|
1410 |
var mediaEl = videoEl[0]; |
|
1411 |
|
|
1412 |
// Binding HTML video functions to media events |
|
1413 |
media.on("setcurrenttime", function(_milliseconds) { |
|
1414 |
try { |
|
1415 |
mediaEl.currentTime = (_milliseconds / 1000); |
|
1416 |
} catch (err) { |
|
1417 |
|
|
1418 |
} |
|
1419 |
}); |
|
1420 |
|
|
1421 |
media.on("setvolume", function(_vol) { |
|
1422 |
media.volume = _vol; |
|
1423 |
try { |
|
1424 |
mediaEl.volume = _vol; |
|
1425 |
} catch (err) { |
|
1426 |
|
|
1427 |
} |
|
1428 |
}); |
|
1429 |
|
|
1430 |
media.on("setmuted", function(_muted) { |
|
1431 |
media.muted = _muted; |
|
1432 |
try { |
|
1433 |
mediaEl.muted = _muted; |
|
1434 |
} catch (err) { |
|
1435 |
|
|
1436 |
} |
|
1437 |
}); |
|
1438 |
|
|
1439 |
media.on("setplay", function() { |
|
1440 |
try { |
|
1441 |
mediaEl.play(); |
|
1442 |
} catch (err) { |
|
1443 |
|
|
1444 |
} |
|
1445 |
}); |
|
1446 |
|
|
1447 |
media.on("setpause", function() { |
|
1448 |
try { |
|
1449 |
mediaEl.pause(); |
|
1450 |
} catch (err) { |
|
1451 |
|
|
1452 |
} |
|
1453 |
}); |
|
1454 |
|
|
1455 |
// Binding DOM events to media |
|
1456 |
function getVolume() { |
|
1457 |
media.muted = mediaEl.muted; |
|
1458 |
media.volume = mediaEl.volume; |
|
1459 |
} |
|
1460 |
|
|
1461 |
videoEl.on("loadedmetadata", function() { |
|
1462 |
getVolume(); |
|
1463 |
media.trigger("loadedmetadata"); |
|
1464 |
media.trigger("volumechange"); |
87
|
1465 |
}); |
3
|
1466 |
|
|
1467 |
videoEl.on("timeupdate", function() { |
|
1468 |
media.trigger("timeupdate", new IriSP.Model.Time(1000*mediaEl.currentTime)); |
|
1469 |
}); |
|
1470 |
|
|
1471 |
videoEl.on("volumechange", function() { |
|
1472 |
getVolume(); |
|
1473 |
media.trigger("volumechange"); |
87
|
1474 |
}); |
3
|
1475 |
|
|
1476 |
videoEl.on("play", function() { |
|
1477 |
media.trigger("play"); |
|
1478 |
}); |
|
1479 |
|
|
1480 |
videoEl.on("pause", function() { |
|
1481 |
media.trigger("pause"); |
|
1482 |
}); |
|
1483 |
|
|
1484 |
videoEl.on("seeking", function() { |
|
1485 |
media.trigger("seeking"); |
|
1486 |
}); |
|
1487 |
|
|
1488 |
videoEl.on("seeked", function() { |
|
1489 |
media.trigger("seeked"); |
|
1490 |
}); |
|
1491 |
|
|
1492 |
|
|
1493 |
}; |