| author | veltr |
| Tue, 17 Apr 2012 15:03:40 +0200 | |
| branch | new-model |
| changeset 866 | 3bf7aa8216e5 |
| parent 864 | 5e76a06b961c |
| child 868 | a525cc2214e7 |
| 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 = { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
4 |
SOURCE_STATUS_EMPTY : 0, |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
5 |
SOURCE_STATUS_WAITING : 1, |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
6 |
SOURCE_STATUS_READY : 2, |
| 860 | 7 |
ID_AUTO_INCREMENT : 0, |
8 |
getAI : function() { |
|
9 |
return "autoid-" + (++this.ID_AUTO_INCREMENT); |
|
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 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
47 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
48 |
|
| 858 | 49 |
IriSP.Model.List = function(_directory) { |
| 866 | 50 |
Array.call(this); |
| 858 | 51 |
this.directory = _directory; |
| 866 | 52 |
this.idIndex = []; |
| 860 | 53 |
if (typeof _directory == "undefined") { |
54 |
throw("Error : new IriSP.Model.List(directory): directory is undefined"); |
|
55 |
} |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
56 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
57 |
|
| 866 | 58 |
IriSP.Model.List.prototype = new Array(); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
59 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
60 |
IriSP.Model.List.prototype.getElement = function(_id) { |
| 866 | 61 |
if (this.hasId(_id)) { |
62 |
return this; |
|
| 858 | 63 |
} |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
64 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
65 |
|
| 864 | 66 |
IriSP.Model.List.prototype.hasId = function(_id) { |
| 866 | 67 |
return (IriSP._(this.idIndex).indexOf(_id) !== -1); |
| 864 | 68 |
} |
69 |
||
| 866 | 70 |
if (typeof Array.prototype.forEach === "undefined") { |
71 |
IriSP.Model.List.prototype.forEach = function(_callback) { |
|
72 |
var _this = this; |
|
73 |
IriSP._(this).forEach(function(_value, _key) { |
|
74 |
_callback(_value, _key, _this); |
|
75 |
}); |
|
76 |
} |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
77 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
78 |
|
| 866 | 79 |
if (typeof Array.prototype.map === "undefined") { |
80 |
IriSP.Model.List.prototype.map = function(_callback) { |
|
81 |
var _this = this; |
|
82 |
return IriSP._(this).map(function(_value, _key) { |
|
83 |
return _callback(_value, _key, _this); |
|
84 |
}); |
|
85 |
} |
|
| 858 | 86 |
} |
87 |
||
| 866 | 88 |
/* We override Array's filter function because it doesn't return an IriSP.Model.List */ |
| 858 | 89 |
IriSP.Model.List.prototype.filter = function(_callback) { |
90 |
var _this = this, |
|
91 |
_res = new IriSP.Model.List(this.directory); |
|
| 866 | 92 |
_res.addElements(IriSP._(this).filter(function(_value, _key) { |
93 |
return _callback(_value, _key, _this); |
|
94 |
})); |
|
| 858 | 95 |
return _res; |
96 |
} |
|
97 |
||
| 864 | 98 |
IriSP.Model.List.prototype.sortBy = function(_callback) { |
99 |
var _this = this, |
|
100 |
_res = new IriSP.Model.List(this.directory); |
|
| 866 | 101 |
_res.contents = IriSP._(this).sortBy(function(_value, _key) { |
102 |
return _callback(_value, _key, _this); |
|
| 864 | 103 |
}); |
104 |
return _res; |
|
105 |
} |
|
106 |
||
| 858 | 107 |
IriSP.Model.List.prototype.searchByTitle = function(_text) { |
108 |
var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim'); |
|
109 |
return this.filter(function(_element) { |
|
| 864 | 110 |
return _rgxp.test(_element.title); |
| 858 | 111 |
}); |
112 |
} |
|
113 |
||
114 |
IriSP.Model.List.prototype.searchByDescription = function(_text) { |
|
115 |
var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim'); |
|
116 |
return this.filter(function(_element) { |
|
117 |
return _rgxp.test(_element.description); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
118 |
}); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
119 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
120 |
|
| 858 | 121 |
IriSP.Model.List.prototype.searchByTextFields = function(_text) { |
122 |
var _rgxp = new RegExp('(' + _text.replace(/(\W)/gm,'\\$1') + ')','gim'); |
|
123 |
return this.filter(function(_element) { |
|
| 864 | 124 |
return _rgxp.test(_element.description) || _rgxp.test(_element.title); |
| 858 | 125 |
}); |
126 |
} |
|
127 |
||
128 |
IriSP.Model.List.prototype.addId = function(_id) { |
|
| 866 | 129 |
var _el = this.directory.getElement(_id) |
130 |
if (!this.hasId(_id) && typeof _el !== "undefined") { |
|
131 |
this.idIndex.push(_id); |
|
132 |
Array.prototype.push.call(this, _el); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
133 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
134 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
135 |
|
| 866 | 136 |
IriSP.Model.List.prototype.push = function(_el) { |
137 |
if (typeof this.directory.getElement(_el.id) === "undefined") { |
|
138 |
this.directory.addElement(_el); |
|
139 |
} |
|
140 |
this.idIndex.push(_el.id); |
|
141 |
Array.prototype.push.call(this, _el); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
142 |
} |
| 858 | 143 |
|
| 866 | 144 |
IriSP.Model.List.prototype.addIds = function(_array) { |
145 |
var _l = _array.length, |
|
146 |
_this = this; |
|
147 |
IriSP._(_array).forEach(function(_id) { |
|
148 |
_this.addId(_id); |
|
149 |
}); |
|
| 858 | 150 |
} |
151 |
||
| 866 | 152 |
IriSP.Model.List.prototype.addElements = function(_array) { |
153 |
var _l = _array.length, |
|
154 |
_this = this; |
|
155 |
IriSP._(_array).forEach(function(_el) { |
|
156 |
_this.push(_el); |
|
157 |
}); |
|
| 858 | 158 |
} |
159 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
160 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
161 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
162 |
IriSP.Model.Time = function(_milliseconds) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
163 |
this.milliseconds = parseInt(typeof _milliseconds !== "undefined" ? _milliseconds : 0); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
164 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
165 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
166 |
IriSP.Model.Time.prototype.setSeconds = function(_seconds) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
167 |
this.milliseconds = 1000 * _seconds; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
168 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
169 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
170 |
IriSP.Model.Time.prototype.getSeconds = function() { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
171 |
return Math.floor(this.milliseconds / 1000); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
172 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
173 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
174 |
IriSP.Model.Time.prototype.getHMS = function() { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
175 |
var _totalSeconds = Math.abs(this.getSeconds()); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
176 |
return { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
177 |
hours : Math.floor(_totalSeconds / 3600), |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
178 |
minutes : (Math.floor(_totalSeconds / 60) % 60), |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
179 |
seconds : _totalSeconds % 60 |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
180 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
181 |
} |
|
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 |
IriSP.Model.Time.prototype.toString = function() { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
184 |
function pad(_n) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
185 |
var _res = _n.toString(); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
186 |
while (_res.length < 2) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
187 |
_res = '0' + _res; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
188 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
189 |
return _res; |
|
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 |
var _hms = this.getHMS(), |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
192 |
_res = ''; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
193 |
if (_hms.hours) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
194 |
_res += pad(_hms.hours) + ':' |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
195 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
196 |
_res += pad(_hms.minutes) + ':' + pad(_hms.seconds); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
197 |
return _res; |
|
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 |
|
| 858 | 200 |
/* */ |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
201 |
|
| 860 | 202 |
IriSP.Model.Reference = function(_source, _idRef) { |
203 |
this.source = _source; |
|
| 858 | 204 |
if (typeof _idRef === "object") { |
205 |
this.isList = true; |
|
| 860 | 206 |
this.contents = new IriSP.Model.List(this.source.directory); |
| 866 | 207 |
this.contents.addIds(IriSP._(_idRef).map(function(_id) { |
| 860 | 208 |
return _source.getNamespaced(_id).fullname; |
209 |
})); |
|
| 858 | 210 |
} else { |
211 |
this.isList = false; |
|
| 860 | 212 |
this.contents = _source.getNamespaced(_idRef).fullname; |
| 858 | 213 |
} |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
214 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
215 |
|
| 858 | 216 |
IriSP.Model.Reference.prototype.getContents = function() { |
| 860 | 217 |
return (this.isList ? this.contents : this.source.directory.getElement(this.contents)); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
218 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
219 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
220 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
221 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
222 |
IriSP.Model.Element = function(_id, _source) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
223 |
this.elementType = 'element'; |
| 860 | 224 |
if (typeof _source !== "undefined") { |
225 |
if (typeof _id === "undefined" || !_id) { |
|
226 |
_id = IriSP.Model.getAI(); |
|
227 |
} |
|
| 858 | 228 |
this.source = _source; |
| 860 | 229 |
this.id = _source.getNamespaced(_id).fullname; |
| 858 | 230 |
this.title = ""; |
231 |
this.description = ""; |
|
232 |
this.source.directory.addElement(this); |
|
|
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 |
IriSP.Model.Element.prototype.toString = function() { |
| 858 | 237 |
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
|
238 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
239 |
|
| 858 | 240 |
IriSP.Model.Element.prototype.setReference = function(_elementType, _idRef) { |
| 860 | 241 |
this[_elementType] = new IriSP.Model.Reference(this.source, _idRef); |
| 854 | 242 |
} |
243 |
||
| 858 | 244 |
IriSP.Model.Element.prototype.getReference = function(_elementType) { |
245 |
if (typeof this[_elementType] !== "undefined") { |
|
246 |
return this[_elementType].getContents(); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
247 |
} |
|
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 |
|
| 864 | 250 |
IriSP.Model.Element.prototype.getRelated = function(_elementType) { |
251 |
var _this = this; |
|
252 |
return this.source.getList(_elementType).filter(function(_el) { |
|
253 |
var _ref = _el[_this.elementType]; |
|
254 |
if (_ref.isList) { |
|
255 |
return _ref.contents.hasId(_this.id); |
|
256 |
} |
|
257 |
else { |
|
258 |
return _ref.contents === _this.id; |
|
259 |
} |
|
260 |
}); |
|
261 |
} |
|
262 |
||
|
856
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.Media = function(_id, _source) { |
266 |
IriSP.Model.Element.call(this, _id, _source); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
267 |
this.elementType = 'media'; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
268 |
this.duration = new IriSP.Model.Time(); |
| 866 | 269 |
this.video = ''; |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
270 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
271 |
|
| 858 | 272 |
IriSP.Model.Media.prototype = new IriSP.Model.Element(); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
273 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
274 |
IriSP.Model.Media.prototype.setDuration = function(_durationMs) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
275 |
this.duration.milliseconds = _durationMs; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
276 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
277 |
|
| 864 | 278 |
IriSP.Model.Media.prototype.getAnnotations = function() { |
279 |
return this.getRelated("annotation"); |
|
280 |
} |
|
281 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
282 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
283 |
|
| 864 | 284 |
IriSP.Model.Tag = function(_id, _source) { |
285 |
IriSP.Model.Element.call(this, _id, _source); |
|
| 858 | 286 |
this.elementType = 'tag'; |
287 |
} |
|
288 |
||
289 |
IriSP.Model.Tag.prototype = new IriSP.Model.Element(); |
|
290 |
||
| 864 | 291 |
IriSP.Model.Tag.prototype.getAnnotations = function() { |
292 |
return this.getRelated("annotation"); |
|
293 |
} |
|
294 |
||
| 858 | 295 |
/* */ |
296 |
||
| 864 | 297 |
IriSP.Model.AnnotationType = function(_id, _source) { |
298 |
IriSP.Model.Element.call(this, _id, _source); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
299 |
this.elementType = 'annotationType'; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
300 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
301 |
|
| 858 | 302 |
IriSP.Model.AnnotationType.prototype = new IriSP.Model.Element(); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
303 |
|
| 864 | 304 |
IriSP.Model.AnnotationType.prototype.getAnnotations = function() { |
305 |
return this.getRelated("annotation"); |
|
306 |
} |
|
307 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
308 |
/* Annotation |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
309 |
* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
310 |
|
| 864 | 311 |
IriSP.Model.Annotation = function(_id, _source) { |
312 |
IriSP.Model.Element.call(this, _id, _source); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
313 |
this.elementType = 'annotation'; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
314 |
this.begin = new IriSP.Model.Time(); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
315 |
this.end = new IriSP.Model.Time(); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
316 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
317 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
318 |
IriSP.Model.Annotation.prototype = new IriSP.Model.Element(null); |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
319 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
320 |
IriSP.Model.Annotation.prototype.setBegin = function(_beginMs) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
321 |
this.begin.milliseconds = _beginMs; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
322 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
323 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
324 |
IriSP.Model.Annotation.prototype.setEnd = function(_beginMs) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
325 |
this.end.milliseconds = _beginMs; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
326 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
327 |
|
| 858 | 328 |
IriSP.Model.Annotation.prototype.setMedia = function(_idRef) { |
329 |
this.setReference("media", _idRef); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
330 |
} |
|
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 |
IriSP.Model.Annotation.prototype.getMedia = function() { |
| 858 | 333 |
return this.getReference("media"); |
|
856
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 |
|
| 858 | 336 |
IriSP.Model.Annotation.prototype.setAnnotationType = function(_idRef) { |
337 |
this.setReference("annotationType", _idRef); |
|
|
856
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 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
340 |
IriSP.Model.Annotation.prototype.getAnnotationType = function() { |
| 858 | 341 |
return this.getReference("annotationType"); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
342 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
343 |
|
| 858 | 344 |
IriSP.Model.Annotation.prototype.setTags = function(_idRefs) { |
345 |
this.setReference("tag", _idRefs); |
|
|
856
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 |
|
| 858 | 348 |
IriSP.Model.Annotation.prototype.getTags = function() { |
349 |
return this.getReference("tag"); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
350 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
351 |
|
| 858 | 352 |
/* */ |
353 |
||
| 860 | 354 |
IriSP.Model.Source = function(_config) { |
| 858 | 355 |
this.status = IriSP.Model.SOURCE_STATUS_EMPTY; |
| 860 | 356 |
if (typeof _config !== "undefined") { |
357 |
var _this = this; |
|
| 866 | 358 |
IriSP._(_config).forEach(function(_v, _k) { |
| 860 | 359 |
_this[_k] = _v; |
360 |
}) |
|
361 |
this.callbackQueue = []; |
|
362 |
this.contents = {}; |
|
363 |
if (typeof this.namespace === "undefined") { |
|
364 |
this.namespace = IriSP.Model.getAI(); |
|
365 |
} |
|
366 |
if (typeof this.namespaceUrl === "undefined") { |
|
367 |
this.namespaceUrl = (typeof this.url !== "undefined" ? this.url : this.namespaceUrl); |
|
368 |
} |
|
369 |
this.directory.namespaces[this.namespace] = this.namespaceUrl; |
|
370 |
this.get(); |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
IriSP.Model.Source.prototype.getNamespaced = function(_id) { |
|
375 |
var _tab = _id.split(':'); |
|
376 |
if (_tab.length > 1) { |
|
377 |
return { |
|
378 |
namespace : _tab[0], |
|
379 |
name : _tab[1], |
|
380 |
fullname : _id |
|
381 |
} |
|
382 |
} else { |
|
383 |
return { |
|
384 |
namespace : this.namespace, |
|
385 |
name : _id, |
|
386 |
fullname : this.namespace + ':' + _id |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
|
|
391 |
IriSP.Model.Source.prototype.unNamespace = function(_id) { |
|
392 |
return _id.replace(this.namespace + ':', ''); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
393 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
394 |
|
| 858 | 395 |
IriSP.Model.Source.prototype.addList = function(_listId, _contents) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
396 |
if (typeof this.contents[_listId] === "undefined") { |
| 860 | 397 |
this.contents[_listId] = new IriSP.Model.List(this.directory); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
398 |
} |
| 866 | 399 |
this.contents[_listId].addElements(_contents); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
400 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
401 |
|
| 858 | 402 |
IriSP.Model.Source.prototype.getList = function(_listId) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
403 |
if (typeof this.contents[_listId] === "undefined") { |
| 860 | 404 |
return this.directory.getGlobalList().filter(function(_e) { |
| 858 | 405 |
return (_e.elType === _listId); |
406 |
}); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
407 |
} else { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
408 |
return this.contents[_listId]; |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
409 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
410 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
411 |
|
| 866 | 412 |
IriSP.Model.Source.prototype.forEach = function(_callback) { |
| 860 | 413 |
var _this = this; |
| 866 | 414 |
IriSP._(this.contents).forEach(function(_value, _key) { |
| 860 | 415 |
_callback.call(_this, _value, _key); |
416 |
}) |
|
417 |
} |
|
418 |
||
| 866 | 419 |
IriSP.Model.Source.prototype.getElement = function(_elId) { |
420 |
return this.directory.getElement(_elId); |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
421 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
422 |
|
| 858 | 423 |
IriSP.Model.Source.prototype.setCurrentMediaId = function(_idRef) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
424 |
if (typeof _idRef !== "undefined") { |
| 866 | 425 |
this.currentMedia = this.getMedias().getElement(this.getNamespaced(_idRef).fullname); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
426 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
427 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
428 |
|
| 858 | 429 |
IriSP.Model.Source.prototype.setDefaultCurrentMedia = function() { |
| 866 | 430 |
if (typeof this.currentMedia === "undefined" && this.getMedias().length) { |
431 |
this.currentMedia = this.getMedias()[0]; |
|
| 854 | 432 |
} |
433 |
} |
|
434 |
||
| 864 | 435 |
IriSP.Model.Source.prototype.listNamespaces = function(_excludeSelf) { |
| 860 | 436 |
var _this = this, |
| 864 | 437 |
_nsls = [], |
438 |
_excludeSelf = (typeof _excludeSelf !== "undefined" && _excludeSelf); |
|
| 866 | 439 |
this.forEach(function(_list) { |
440 |
IriSP._(_list).forEach(function(_el) { |
|
441 |
var _ns = _el.id.replace(/:.*$/,''); |
|
| 864 | 442 |
if (IriSP._(_nsls).indexOf(_ns) === -1 && (!_excludeSelf || _ns !== _this.namespace)) { |
| 860 | 443 |
_nsls.push(_ns); |
444 |
} |
|
445 |
}) |
|
446 |
}); |
|
447 |
return _nsls; |
|
448 |
} |
|
449 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
450 |
IriSP.Model.Source.prototype.get = function() { |
| 858 | 451 |
this.status = IriSP.Model.SOURCE_STATUS_READY; |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
452 |
var _this = this; |
| 858 | 453 |
if (_this.callbackQueue.length) { |
| 866 | 454 |
IriSP._(_this.callbackQueue).forEach(function(_callback) { |
| 858 | 455 |
_callback.call(_this); |
456 |
}); |
|
457 |
} |
|
458 |
_this.callbackQueue = []; |
|
| 854 | 459 |
} |
460 |
||
| 860 | 461 |
IriSP.Model.Source.prototype.serialize = function() { |
462 |
return this.serializer.serialize(this); |
|
463 |
} |
|
464 |
||
| 866 | 465 |
IriSP.Model.Source.prototype.onLoad = function(_callback) { |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
466 |
if (this.status === IriSP.Model.SOURCE_STATUS_READY) { |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
467 |
callback.call(this); |
| 854 | 468 |
} else { |
469 |
this.callbackQueue.push(_callback); |
|
470 |
} |
|
471 |
} |
|
472 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
473 |
IriSP.Model.Source.prototype.getAnnotations = function() { |
| 858 | 474 |
return this.getList("annotation"); |
| 854 | 475 |
} |
476 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
477 |
IriSP.Model.Source.prototype.getMedias = function() { |
| 858 | 478 |
return this.getList("media"); |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
479 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
480 |
|
| 864 | 481 |
IriSP.Model.Source.prototype.getAnnotationTypes = function() { |
482 |
return this.getList("annotationType"); |
|
483 |
} |
|
484 |
||
485 |
IriSP.Model.Source.prototype.getAnnotationTypeByTitle = function(_title) { |
|
486 |
var _res = this.getAnnotationTypes().searchByTitle(_title); |
|
| 866 | 487 |
if (_res.length) { |
488 |
return _res[0]; |
|
| 864 | 489 |
} |
490 |
} |
|
491 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
492 |
IriSP.Model.Source.prototype.getDuration = function() { |
| 866 | 493 |
var _m = this.currentMedia; |
494 |
if (typeof _m !== "undefined") { |
|
495 |
return this.currentMedia.duration; |
|
496 |
} |
|
| 858 | 497 |
} |
498 |
||
499 |
/* */ |
|
500 |
||
| 860 | 501 |
IriSP.Model.RemoteSource = function(_config) { |
502 |
IriSP.Model.Source.call(this, _config); |
|
| 858 | 503 |
} |
504 |
||
505 |
IriSP.Model.RemoteSource.prototype = new IriSP.Model.Source(); |
|
506 |
||
507 |
IriSP.Model.RemoteSource.prototype.get = function() { |
|
508 |
this.status = IriSP.Model.SOURCE_STATUS_WAITING; |
|
509 |
var _this = this; |
|
510 |
IriSP.jQuery.getJSON(this.url, function(_result) { |
|
511 |
_this.serializer.deSerialize(_result, _this); |
|
512 |
if (_this.callbackQueue.length) { |
|
| 866 | 513 |
IriSP._(_this.callbackQueue).forEach(function(_callback) { |
| 858 | 514 |
_callback.call(_this); |
515 |
}); |
|
516 |
} |
|
517 |
_this.callbackQueue = []; |
|
518 |
_this.status = IriSP.Model.SOURCE_STATUS_READY; |
|
519 |
}); |
|
| 854 | 520 |
} |
521 |
||
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
522 |
/* */ |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
523 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
524 |
IriSP.Model.Directory = function() { |
| 858 | 525 |
this.remoteSources = {}; |
526 |
this.elements = {}; |
|
| 860 | 527 |
this.namespaces = {}; |
| 858 | 528 |
} |
529 |
||
530 |
IriSP.Model.Directory.prototype.remoteSource = function(_properties) { |
|
| 860 | 531 |
var _config = IriSP._({ directory: this }).extend(_properties); |
| 858 | 532 |
if (typeof this.remoteSources[_properties.url] === "undefined") { |
| 860 | 533 |
this.remoteSources[_properties.url] = new IriSP.Model.RemoteSource(_config); |
| 858 | 534 |
} |
535 |
return this.remoteSources[_properties.url]; |
|
| 854 | 536 |
} |
537 |
||
| 860 | 538 |
IriSP.Model.Directory.prototype.newLocalSource = function(_properties) { |
539 |
var _config = IriSP._({ directory: this }).extend(_properties), |
|
540 |
_res = new IriSP.Model.Source(_config); |
|
541 |
return _res; |
|
542 |
} |
|
543 |
||
| 858 | 544 |
IriSP.Model.Directory.prototype.getElement = function(_id) { |
545 |
return this.elements[_id]; |
|
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
546 |
} |
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
547 |
|
| 858 | 548 |
IriSP.Model.Directory.prototype.addElement = function(_element) { |
549 |
this.elements[_element.id] = _element; |
|
550 |
} |
|
551 |
||
552 |
IriSP.Model.Directory.prototype.getGlobalList = function() { |
|
553 |
var _res = new IriSP.Model.List(this); |
|
| 866 | 554 |
_res.addIds(IriSP._(this.elements).keys()); |
| 858 | 555 |
return _res; |
| 854 | 556 |
} |
|
856
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
557 |
|
|
41c574c807d1
basic implementation of model: media, annotation type, annotation
veltr
parents:
854
diff
changeset
|
558 |
/* */ |