| author | veltr |
| Tue, 17 Apr 2012 20:19:46 +0200 | |
| branch | new-model |
| changeset 868 | a525cc2214e7 |
| parent 866 | 3bf7aa8216e5 |
| child 870 | 2c025db10a10 |
| permissions | -rw-r--r-- |
| 854 | 1 |
/* model.js is where data is stored in a standard form, whatever the serializer */ |
2 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
3 |
IriSP.Model = { |
| 868 | 4 |
_SOURCE_STATUS_EMPTY : 0, |
5 |
_SOURCE_STATUS_WAITING : 1, |
|
6 |
_SOURCE_STATUS_READY : 2, |
|
7 |
_ID_AUTO_INCREMENT : 0, |
|
| 860 | 8 |
getAI : function() { |
| 868 | 9 |
return "autoid-" + (++this._ID_AUTO_INCREMENT); |
| 860 | 10 |
}, |
11 |
isoToDate : function(_str) { |
|
12 |
// http://delete.me.uk/2005/03/iso8601.html |
|
13 |
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})))?)?)?)?"; |
|
14 |
var d = _str.match(new RegExp(regexp)); |
|
15 |
|
|
16 |
var offset = 0; |
|
17 |
var date = new Date(d[1], 0, 1); |
|
18 |
|
|
19 |
if (d[3]) { date.setMonth(d[3] - 1); } |
|
20 |
if (d[5]) { date.setDate(d[5]); } |
|
21 |
if (d[7]) { date.setHours(d[7]); } |
|
22 |
if (d[8]) { date.setMinutes(d[8]); } |
|
23 |
if (d[10]) { date.setSeconds(d[10]); } |
|
24 |
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } |
|
25 |
if (d[14]) { |
|
26 |
offset = (Number(d[16]) * 60) + Number(d[17]); |
|
27 |
offset *= ((d[15] == '-') ? 1 : -1); |
|
28 |
} |
|
29 |
|
|
30 |
offset -= date.getTimezoneOffset(); |
|
31 |
time = (Number(date) + (offset * 60 * 1000)); |
|
32 |
var _res = new Date(); |
|
33 |
_res.setTime(Number(time)); |
|
34 |
return _res; |
|
35 |
}, |
|
36 |
dateToIso : function(d) { |
|
37 |
function pad(n){return n<10 ? '0'+n : n} |
|
38 |
return d.getUTCFullYear()+'-' |
|
39 |
+ pad(d.getUTCMonth()+1)+'-' |
|
40 |
+ pad(d.getUTCDate())+'T' |
|
41 |
+ pad(d.getUTCHours())+':' |
|
42 |
+ pad(d.getUTCMinutes())+':' |
|
43 |
+ pad(d.getUTCSeconds())+'Z' |
|
44 |
} |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
45 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
46 |
|
| 868 | 47 |
/* |
48 |
* IriSP.Model.List is a class for a list of elements (e.g. annotations, medias, etc. that each have a distinct ID) |
|
49 |
*/ |
|
| 858 | 50 |
IriSP.Model.List = function(_directory) { |
| 866 | 51 |
Array.call(this); |
| 858 | 52 |
this.directory = _directory; |
| 866 | 53 |
this.idIndex = []; |
| 860 | 54 |
if (typeof _directory == "undefined") { |
55 |
throw("Error : new IriSP.Model.List(directory): directory is undefined"); |
|
56 |
} |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
57 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
58 |
|
| 866 | 59 |
IriSP.Model.List.prototype = new Array(); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
60 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
61 |
IriSP.Model.List.prototype.getElement = function(_id) { |
| 866 | 62 |
if (this.hasId(_id)) { |
63 |
return this; |
|
| 858 | 64 |
} |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
65 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
66 |
|
| 864 | 67 |
IriSP.Model.List.prototype.hasId = function(_id) { |
| 866 | 68 |
return (IriSP._(this.idIndex).indexOf(_id) !== -1); |
| 864 | 69 |
} |
70 |
||
| 868 | 71 |
/* On recent browsers, forEach and map are defined and do what we want. |
72 |
* Otherwise, we'll use the Underscore.js functions |
|
73 |
*/ |
|
| 866 | 74 |
if (typeof Array.prototype.forEach === "undefined") { |
75 |
IriSP.Model.List.prototype.forEach = function(_callback) { |
|
76 |
var _this = this; |
|
77 |
IriSP._(this).forEach(function(_value, _key) { |
|
78 |
_callback(_value, _key, _this); |
|
79 |
}); |
|
80 |
} |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
81 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
82 |
|
| 866 | 83 |
if (typeof Array.prototype.map === "undefined") { |
84 |
IriSP.Model.List.prototype.map = function(_callback) { |
|
85 |
var _this = this; |
|
86 |
return IriSP._(this).map(function(_value, _key) { |
|
87 |
return _callback(_value, _key, _this); |
|
88 |
}); |
|
89 |
} |
|
| 858 | 90 |
} |
91 |
||
| 868 | 92 |
/* We override Array's filter function because it doesn't return an IriSP.Model.List |
93 |
*/ |
|
| 858 | 94 |
IriSP.Model.List.prototype.filter = function(_callback) { |
95 |
var _this = this, |
|
96 |
_res = new IriSP.Model.List(this.directory); |
|
| 866 | 97 |
_res.addElements(IriSP._(this).filter(function(_value, _key) { |
98 |
return _callback(_value, _key, _this); |
|
99 |
})); |
|
| 858 | 100 |
return _res; |
101 |
} |
|
102 |
||
| 868 | 103 |
/* Array has a sort function, but it's not as interesting as Underscore.js's sortBy |
104 |
* and won't return a new IriSP.Model.List |
|
105 |
*/ |
|
| 864 | 106 |
IriSP.Model.List.prototype.sortBy = function(_callback) { |
107 |
var _this = this, |
|
108 |
_res = new IriSP.Model.List(this.directory); |
|
| 866 | 109 |
_res.contents = IriSP._(this).sortBy(function(_value, _key) { |
110 |
return _callback(_value, _key, _this); |
|
| 864 | 111 |
}); |
112 |
return _res; |
|
113 |
} |
|
114 |
||
| 868 | 115 |
/* Title and Description are basic information for (almost) all element types, |
116 |
* here we can search by these criteria |
|
117 |
*/ |
|
| 858 | 118 |
IriSP.Model.List.prototype.searchByTitle = function(_text) { |
119 |
var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim'); |
|
120 |
return this.filter(function(_element) { |
|
| 864 | 121 |
return _rgxp.test(_element.title); |
| 858 | 122 |
}); |
123 |
} |
|
124 |
||
125 |
IriSP.Model.List.prototype.searchByDescription = function(_text) { |
|
126 |
var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim'); |
|
127 |
return this.filter(function(_element) { |
|
128 |
return _rgxp.test(_element.description); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
129 |
}); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
130 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
131 |
|
| 858 | 132 |
IriSP.Model.List.prototype.searchByTextFields = function(_text) { |
133 |
var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim'); |
|
134 |
return this.filter(function(_element) { |
|
| 864 | 135 |
return _rgxp.test(_element.description) || _rgxp.test(_element.title); |
| 858 | 136 |
}); |
137 |
} |
|
138 |
||
139 |
IriSP.Model.List.prototype.addId = function(_id) { |
|
| 866 | 140 |
var _el = this.directory.getElement(_id) |
141 |
if (!this.hasId(_id) && typeof _el !== "undefined") { |
|
142 |
this.idIndex.push(_id); |
|
143 |
Array.prototype.push.call(this, _el); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
144 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
145 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
146 |
|
| 866 | 147 |
IriSP.Model.List.prototype.push = function(_el) { |
148 |
if (typeof this.directory.getElement(_el.id) === "undefined") { |
|
149 |
this.directory.addElement(_el); |
|
150 |
} |
|
| 868 | 151 |
var _index = (IriSP._(this.idIndex).indexOf(_el.id)); |
152 |
if (_index === -1) { |
|
153 |
this.idIndex.push(_el.id); |
|
154 |
Array.prototype.push.call(this, _el); |
|
155 |
} else { |
|
156 |
this[_index] = _el; |
|
157 |
} |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
158 |
} |
| 858 | 159 |
|
| 866 | 160 |
IriSP.Model.List.prototype.addIds = function(_array) { |
161 |
var _l = _array.length, |
|
162 |
_this = this; |
|
163 |
IriSP._(_array).forEach(function(_id) { |
|
164 |
_this.addId(_id); |
|
165 |
}); |
|
| 858 | 166 |
} |
167 |
||
| 866 | 168 |
IriSP.Model.List.prototype.addElements = function(_array) { |
169 |
var _l = _array.length, |
|
170 |
_this = this; |
|
171 |
IriSP._(_array).forEach(function(_el) { |
|
172 |
_this.push(_el); |
|
173 |
}); |
|
| 858 | 174 |
} |
175 |
||
| 868 | 176 |
/* A simple time management object, that helps converting millisecs to seconds and strings, |
177 |
* without the clumsiness of the original Date object. |
|
178 |
*/ |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
179 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
180 |
IriSP.Model.Time = function(_milliseconds) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
181 |
this.milliseconds = parseInt(typeof _milliseconds !== "undefined" ? _milliseconds : 0); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
182 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
183 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
184 |
IriSP.Model.Time.prototype.setSeconds = function(_seconds) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
185 |
this.milliseconds = 1000 * _seconds; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
186 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
187 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
188 |
IriSP.Model.Time.prototype.getSeconds = function() { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
189 |
return Math.floor(this.milliseconds / 1000); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
190 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
191 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
192 |
IriSP.Model.Time.prototype.getHMS = function() { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
193 |
var _totalSeconds = Math.abs(this.getSeconds()); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
194 |
return { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
195 |
hours : Math.floor(_totalSeconds / 3600), |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
196 |
minutes : (Math.floor(_totalSeconds / 60) % 60), |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
197 |
seconds : _totalSeconds % 60 |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
198 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
199 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
200 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
201 |
IriSP.Model.Time.prototype.toString = function() { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
202 |
function pad(_n) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
203 |
var _res = _n.toString(); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
204 |
while (_res.length < 2) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
205 |
_res = '0' + _res; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
206 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
207 |
return _res; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
208 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
209 |
var _hms = this.getHMS(), |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
210 |
_res = ''; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
211 |
if (_hms.hours) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
212 |
_res += pad(_hms.hours) + ':' |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
213 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
214 |
_res += pad(_hms.minutes) + ':' + pad(_hms.seconds); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
215 |
return _res; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
216 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
217 |
|
| 868 | 218 |
/* IriSP.Model.Reference handles references between elements |
219 |
*/ |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
220 |
|
| 860 | 221 |
IriSP.Model.Reference = function(_source, _idRef) { |
222 |
this.source = _source; |
|
| 858 | 223 |
if (typeof _idRef === "object") { |
224 |
this.isList = true; |
|
| 860 | 225 |
this.contents = new IriSP.Model.List(this.source.directory); |
| 866 | 226 |
this.contents.addIds(IriSP._(_idRef).map(function(_id) { |
| 860 | 227 |
return _source.getNamespaced(_id).fullname; |
228 |
})); |
|
| 858 | 229 |
} else { |
230 |
this.isList = false; |
|
| 868 | 231 |
this.contents = this.source.directory.getElement(_source.getNamespaced(_idRef).fullname); |
| 858 | 232 |
} |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
233 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
234 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
235 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
236 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
237 |
IriSP.Model.Element = function(_id, _source) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
238 |
this.elementType = 'element'; |
| 860 | 239 |
if (typeof _source !== "undefined") { |
240 |
if (typeof _id === "undefined" || !_id) { |
|
241 |
_id = IriSP.Model.getAI(); |
|
242 |
} |
|
| 858 | 243 |
this.source = _source; |
| 860 | 244 |
this.id = _source.getNamespaced(_id).fullname; |
| 858 | 245 |
this.title = ""; |
246 |
this.description = ""; |
|
247 |
this.source.directory.addElement(this); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
248 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
249 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
250 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
251 |
IriSP.Model.Element.prototype.toString = function() { |
| 858 | 252 |
return this.elementType + (this.elementType !== 'element' ? ', id=' + this.id + ', title="' + this.title + '"' : ''); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
253 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
254 |
|
| 858 | 255 |
IriSP.Model.Element.prototype.setReference = function(_elementType, _idRef) { |
| 860 | 256 |
this[_elementType] = new IriSP.Model.Reference(this.source, _idRef); |
| 854 | 257 |
} |
258 |
||
| 858 | 259 |
IriSP.Model.Element.prototype.getReference = function(_elementType) { |
260 |
if (typeof this[_elementType] !== "undefined") { |
|
| 868 | 261 |
return this[_elementType].contents; |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
262 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
263 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
264 |
|
| 864 | 265 |
IriSP.Model.Element.prototype.getRelated = function(_elementType) { |
266 |
var _this = this; |
|
267 |
return this.source.getList(_elementType).filter(function(_el) { |
|
268 |
var _ref = _el[_this.elementType]; |
|
269 |
if (_ref.isList) { |
|
270 |
return _ref.contents.hasId(_this.id); |
|
271 |
} |
|
272 |
else { |
|
| 868 | 273 |
return _ref.contents.id === _this.id; |
| 864 | 274 |
} |
275 |
}); |
|
276 |
} |
|
277 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
278 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
279 |
|
| 864 | 280 |
IriSP.Model.Media = function(_id, _source) { |
281 |
IriSP.Model.Element.call(this, _id, _source); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
282 |
this.elementType = 'media'; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
283 |
this.duration = new IriSP.Model.Time(); |
| 866 | 284 |
this.video = ''; |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
285 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
286 |
|
| 858 | 287 |
IriSP.Model.Media.prototype = new IriSP.Model.Element(); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
288 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
289 |
IriSP.Model.Media.prototype.setDuration = function(_durationMs) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
290 |
this.duration.milliseconds = _durationMs; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
291 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
292 |
|
| 864 | 293 |
IriSP.Model.Media.prototype.getAnnotations = function() { |
294 |
return this.getRelated("annotation"); |
|
295 |
} |
|
296 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
297 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
298 |
|
| 864 | 299 |
IriSP.Model.Tag = function(_id, _source) { |
300 |
IriSP.Model.Element.call(this, _id, _source); |
|
| 858 | 301 |
this.elementType = 'tag'; |
302 |
} |
|
303 |
||
304 |
IriSP.Model.Tag.prototype = new IriSP.Model.Element(); |
|
305 |
||
| 864 | 306 |
IriSP.Model.Tag.prototype.getAnnotations = function() { |
307 |
return this.getRelated("annotation"); |
|
308 |
} |
|
309 |
||
| 858 | 310 |
/* */ |
311 |
||
| 864 | 312 |
IriSP.Model.AnnotationType = function(_id, _source) { |
313 |
IriSP.Model.Element.call(this, _id, _source); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
314 |
this.elementType = 'annotationType'; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
315 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
316 |
|
| 858 | 317 |
IriSP.Model.AnnotationType.prototype = new IriSP.Model.Element(); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
318 |
|
| 864 | 319 |
IriSP.Model.AnnotationType.prototype.getAnnotations = function() { |
320 |
return this.getRelated("annotation"); |
|
321 |
} |
|
322 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
323 |
/* Annotation |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
324 |
* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
325 |
|
| 864 | 326 |
IriSP.Model.Annotation = function(_id, _source) { |
327 |
IriSP.Model.Element.call(this, _id, _source); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
328 |
this.elementType = 'annotation'; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
329 |
this.begin = new IriSP.Model.Time(); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
330 |
this.end = new IriSP.Model.Time(); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
331 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
332 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
333 |
IriSP.Model.Annotation.prototype = new IriSP.Model.Element(null); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
334 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
335 |
IriSP.Model.Annotation.prototype.setBegin = function(_beginMs) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
336 |
this.begin.milliseconds = _beginMs; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
337 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
338 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
339 |
IriSP.Model.Annotation.prototype.setEnd = function(_beginMs) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
340 |
this.end.milliseconds = _beginMs; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
341 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
342 |
|
| 858 | 343 |
IriSP.Model.Annotation.prototype.setMedia = function(_idRef) { |
344 |
this.setReference("media", _idRef); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
345 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
346 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
347 |
IriSP.Model.Annotation.prototype.getMedia = function() { |
| 858 | 348 |
return this.getReference("media"); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
349 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
350 |
|
| 858 | 351 |
IriSP.Model.Annotation.prototype.setAnnotationType = function(_idRef) { |
352 |
this.setReference("annotationType", _idRef); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
353 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
354 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
355 |
IriSP.Model.Annotation.prototype.getAnnotationType = function() { |
| 858 | 356 |
return this.getReference("annotationType"); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
357 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
358 |
|
| 858 | 359 |
IriSP.Model.Annotation.prototype.setTags = function(_idRefs) { |
360 |
this.setReference("tag", _idRefs); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
361 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
362 |
|
| 858 | 363 |
IriSP.Model.Annotation.prototype.getTags = function() { |
364 |
return this.getReference("tag"); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
365 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
366 |
|
| 858 | 367 |
/* */ |
368 |
||
| 860 | 369 |
IriSP.Model.Source = function(_config) { |
| 868 | 370 |
this.status = IriSP.Model._SOURCE_STATUS_EMPTY; |
| 860 | 371 |
if (typeof _config !== "undefined") { |
372 |
var _this = this; |
|
| 866 | 373 |
IriSP._(_config).forEach(function(_v, _k) { |
| 860 | 374 |
_this[_k] = _v; |
375 |
}) |
|
376 |
this.callbackQueue = []; |
|
377 |
this.contents = {}; |
|
378 |
if (typeof this.namespace === "undefined") { |
|
379 |
this.namespace = IriSP.Model.getAI(); |
|
380 |
} |
|
381 |
if (typeof this.namespaceUrl === "undefined") { |
|
382 |
this.namespaceUrl = (typeof this.url !== "undefined" ? this.url : this.namespaceUrl); |
|
383 |
} |
|
384 |
this.directory.namespaces[this.namespace] = this.namespaceUrl; |
|
385 |
this.get(); |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
IriSP.Model.Source.prototype.getNamespaced = function(_id) { |
|
390 |
var _tab = _id.split(':'); |
|
391 |
if (_tab.length > 1) { |
|
392 |
return { |
|
393 |
namespace : _tab[0], |
|
394 |
name : _tab[1], |
|
395 |
fullname : _id |
|
396 |
} |
|
397 |
} else { |
|
398 |
return { |
|
399 |
namespace : this.namespace, |
|
400 |
name : _id, |
|
401 |
fullname : this.namespace + ':' + _id |
|
402 |
} |
|
403 |
} |
|
404 |
} |
|
405 |
|
|
406 |
IriSP.Model.Source.prototype.unNamespace = function(_id) { |
|
407 |
return _id.replace(this.namespace + ':', ''); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
408 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
409 |
|
| 858 | 410 |
IriSP.Model.Source.prototype.addList = function(_listId, _contents) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
411 |
if (typeof this.contents[_listId] === "undefined") { |
| 860 | 412 |
this.contents[_listId] = new IriSP.Model.List(this.directory); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
413 |
} |
| 866 | 414 |
this.contents[_listId].addElements(_contents); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
415 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
416 |
|
| 858 | 417 |
IriSP.Model.Source.prototype.getList = function(_listId) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
418 |
if (typeof this.contents[_listId] === "undefined") { |
| 860 | 419 |
return this.directory.getGlobalList().filter(function(_e) { |
| 858 | 420 |
return (_e.elType === _listId); |
421 |
}); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
422 |
} else { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
423 |
return this.contents[_listId]; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
424 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
425 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
426 |
|
| 866 | 427 |
IriSP.Model.Source.prototype.forEach = function(_callback) { |
| 860 | 428 |
var _this = this; |
| 866 | 429 |
IriSP._(this.contents).forEach(function(_value, _key) { |
| 860 | 430 |
_callback.call(_this, _value, _key); |
431 |
}) |
|
432 |
} |
|
433 |
||
| 866 | 434 |
IriSP.Model.Source.prototype.getElement = function(_elId) { |
435 |
return this.directory.getElement(_elId); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
436 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
437 |
|
| 858 | 438 |
IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
439 |
if (typeof _idRef !== "undefined") { |
| 866 | 440 |
this.currentMedia = this.getMedias().getElement(this.getNamespaced(_idRef).fullname); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
441 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
442 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
443 |
|
| 858 | 444 |
IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() { |
| 866 | 445 |
if (typeof this.currentMedia === "undefined" && this.getMedias().length) { |
446 |
this.currentMedia = this.getMedias()[0]; |
|
| 854 | 447 |
} |
448 |
} |
|
449 |
||
| 864 | 450 |
IriSP.Model.Source.prototype.listNamespaces = function(_excludeSelf) { |
| 860 | 451 |
var _this = this, |
| 864 | 452 |
_nsls = [], |
453 |
_excludeSelf = (typeof _excludeSelf !== "undefined" && _excludeSelf); |
|
| 866 | 454 |
this.forEach(function(_list) { |
455 |
IriSP._(_list).forEach(function(_el) { |
|
456 |
var _ns = _el.id.replace(/:.*$/,''); |
|
| 864 | 457 |
if (IriSP._(_nsls).indexOf(_ns) === -1 && (!_excludeSelf || _ns !== _this.namespace)) { |
| 860 | 458 |
_nsls.push(_ns); |
459 |
} |
|
460 |
}) |
|
461 |
}); |
|
462 |
return _nsls; |
|
463 |
} |
|
464 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
465 |
IriSP.Model.Source.prototype.get = function() { |
| 868 | 466 |
this.status = IriSP.Model._SOURCE_STATUS_READY; |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
467 |
var _this = this; |
| 858 | 468 |
if (_this.callbackQueue.length) { |
| 866 | 469 |
IriSP._(_this.callbackQueue).forEach(function(_callback) { |
| 858 | 470 |
_callback.call(_this); |
471 |
}); |
|
472 |
} |
|
473 |
_this.callbackQueue = []; |
|
| 854 | 474 |
} |
475 |
||
| 860 | 476 |
IriSP.Model.Source.prototype.serialize = function() { |
477 |
return this.serializer.serialize(this); |
|
478 |
} |
|
479 |
||
| 866 | 480 |
IriSP.Model.Source.prototype.onLoad = function(_callback) { |
| 868 | 481 |
if (this.status === IriSP.Model._SOURCE_STATUS_READY) { |
482 |
console.log("Called on load, Ready"); |
|
483 |
var _this = this; |
|
484 |
IriSP._.defer(function() { |
|
485 |
_callback.call(_this); |
|
486 |
}); |
|
| 854 | 487 |
} else { |
| 868 | 488 |
console.log("Called on load, not ready"); |
| 854 | 489 |
this.callbackQueue.push(_callback); |
490 |
} |
|
491 |
} |
|
492 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
493 |
IriSP.Model.Source.prototype.getAnnotations = function() { |
| 858 | 494 |
return this.getList("annotation"); |
| 854 | 495 |
} |
496 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
497 |
IriSP.Model.Source.prototype.getMedias = function() { |
| 858 | 498 |
return this.getList("media"); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
499 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
500 |
|
| 864 | 501 |
IriSP.Model.Source.prototype.getAnnotationTypes = function() { |
502 |
return this.getList("annotationType"); |
|
503 |
} |
|
504 |
||
505 |
IriSP.Model.Source.prototype.getAnnotationTypeByTitle = function(_title) { |
|
506 |
var _res = this.getAnnotationTypes().searchByTitle(_title); |
|
| 866 | 507 |
if (_res.length) { |
508 |
return _res[0]; |
|
| 864 | 509 |
} |
510 |
} |
|
511 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
512 |
IriSP.Model.Source.prototype.getDuration = function() { |
| 866 | 513 |
var _m = this.currentMedia; |
514 |
if (typeof _m !== "undefined") { |
|
515 |
return this.currentMedia.duration; |
|
516 |
} |
|
| 858 | 517 |
} |
518 |
||
519 |
/* */ |
|
520 |
||
| 860 | 521 |
IriSP.Model.RemoteSource = function(_config) { |
522 |
IriSP.Model.Source.call(this, _config); |
|
| 858 | 523 |
} |
524 |
||
525 |
IriSP.Model.RemoteSource.prototype = new IriSP.Model.Source(); |
|
526 |
||
527 |
IriSP.Model.RemoteSource.prototype.get = function() { |
|
| 868 | 528 |
this.status = IriSP.Model._SOURCE_STATUS_WAITING; |
| 858 | 529 |
var _this = this; |
530 |
IriSP.jQuery.getJSON(this.url, function(_result) { |
|
531 |
_this.serializer.deSerialize(_result, _this); |
|
| 868 | 532 |
console.log('Received data, we have '+_this.callbackQueue.length+' callbacks waiting'); |
| 858 | 533 |
if (_this.callbackQueue.length) { |
| 866 | 534 |
IriSP._(_this.callbackQueue).forEach(function(_callback) { |
| 858 | 535 |
_callback.call(_this); |
536 |
}); |
|
537 |
} |
|
538 |
_this.callbackQueue = []; |
|
| 868 | 539 |
_this.status = IriSP.Model._SOURCE_STATUS_READY; |
| 858 | 540 |
}); |
| 854 | 541 |
} |
542 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
543 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
544 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
545 |
IriSP.Model.Directory = function() { |
| 858 | 546 |
this.remoteSources = {}; |
547 |
this.elements = {}; |
|
| 860 | 548 |
this.namespaces = {}; |
| 858 | 549 |
} |
550 |
||
551 |
IriSP.Model.Directory.prototype.remoteSource = function(_properties) { |
|
| 860 | 552 |
var _config = IriSP._({ directory: this }).extend(_properties); |
| 858 | 553 |
if (typeof this.remoteSources[_properties.url] === "undefined") { |
| 860 | 554 |
this.remoteSources[_properties.url] = new IriSP.Model.RemoteSource(_config); |
| 858 | 555 |
} |
556 |
return this.remoteSources[_properties.url]; |
|
| 854 | 557 |
} |
558 |
||
| 860 | 559 |
IriSP.Model.Directory.prototype.newLocalSource = function(_properties) { |
560 |
var _config = IriSP._({ directory: this }).extend(_properties), |
|
561 |
_res = new IriSP.Model.Source(_config); |
|
562 |
return _res; |
|
563 |
} |
|
564 |
||
| 858 | 565 |
IriSP.Model.Directory.prototype.getElement = function(_id) { |
566 |
return this.elements[_id]; |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
567 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
568 |
|
| 858 | 569 |
IriSP.Model.Directory.prototype.addElement = function(_element) { |
570 |
this.elements[_element.id] = _element; |
|
571 |
} |
|
572 |
||
573 |
IriSP.Model.Directory.prototype.getGlobalList = function() { |
|
574 |
var _res = new IriSP.Model.List(this); |
|
| 866 | 575 |
_res.addIds(IriSP._(this.elements).keys()); |
| 858 | 576 |
return _res; |
| 854 | 577 |
} |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
578 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
579 |
/* */ |