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