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