diff -r 27b248a13355 -r 8a6c9e3d0158 src/js/model.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/js/model.js Thu Aug 30 13:16:33 2012 +0200 @@ -0,0 +1,833 @@ +/* TODO: Separate Project-specific data from Source */ + +/* model.js is where data is stored in a standard form, whatever the serializer */ + +IriSP.Model = { + _SOURCE_STATUS_EMPTY : 0, + _SOURCE_STATUS_WAITING : 1, + _SOURCE_STATUS_READY : 2, + _ID_AUTO_INCREMENT : 0, + _ID_BASE : (function(_d) { + function pad(n){return n<10 ? '0'+n : n} + function fillrand(n) { + var _res = '' + for (var i=0; i _time; + }); + if (_list.length) { + return _list[0]; + } else { + return undefined; + } +} + +IriSP.Model.Mashup.prototype.getMediaAtTime = function(_time) { + var _annotation = this.getAnnotationAtTime(_time); + if (typeof _annotation !== "undefined") { + return _annotation.getMedia(); + } else { + return undefined; + } +} + +/* */ + +IriSP.Model.Source = function(_config) { + this.status = IriSP.Model._SOURCE_STATUS_EMPTY; + if (typeof _config !== "undefined") { + var _this = this; + IriSP._(_config).forEach(function(_v, _k) { + _this[_k] = _v; + }) + this.callbackQueue = []; + this.contents = {}; + this.get(); + } +} + +IriSP.Model.Source.prototype.addList = function(_listId, _contents) { + if (typeof this.contents[_listId] === "undefined") { + this.contents[_listId] = new IriSP.Model.List(this.directory); + } + this.contents[_listId].addElements(_contents); +} + +IriSP.Model.Source.prototype.getList = function(_listId, _global) { + _global = (typeof _global !== "undefined" && _global); + if (_global || typeof this.contents[_listId] === "undefined") { + return this.directory.getGlobalList().filter(function(_e) { + return (_e.elementType === _listId); + }); + } else { + return this.contents[_listId]; + } +} + +IriSP.Model.Source.prototype.forEach = function(_callback) { + var _this = this; + IriSP._(this.contents).forEach(function(_value, _key) { + _callback.call(_this, _value, _key); + }) +} + +IriSP.Model.Source.prototype.getElement = function(_elId) { + return this.directory.getElement(_elId); +} + +IriSP.Model.Source.prototype.get = function() { + this.status = IriSP.Model._SOURCE_STATUS_WAITING; + this.handleCallbacks(); +} + +/* We defer the callbacks calls so they execute after the queue is cleared */ +IriSP.Model.Source.prototype.deferCallback = function(_callback) { + var _this = this; + IriSP._.defer(function() { + _callback.call(_this); + }); +} + +IriSP.Model.Source.prototype.handleCallbacks = function() { + this.status = IriSP.Model._SOURCE_STATUS_READY; + while (this.callbackQueue.length) { + this.deferCallback(this.callbackQueue.splice(0,1)[0]); + } +} +IriSP.Model.Source.prototype.onLoad = function(_callback) { + if (this.status === IriSP.Model._SOURCE_STATUS_READY) { + this.deferCallback(_callback); + } else { + this.callbackQueue.push(_callback); + } +} + +IriSP.Model.Source.prototype.serialize = function() { + return this.serializer.serialize(this); +} + +IriSP.Model.Source.prototype.deSerialize = function(_data) { + this.serializer.deSerialize(_data, this); +} + +IriSP.Model.Source.prototype.getAnnotations = function(_global) { + _global = (typeof _global !== "undefined" && _global); + return this.getList("annotation", _global); +} + +IriSP.Model.Source.prototype.getMedias = function(_global) { + _global = (typeof _global !== "undefined" && _global); + return this.getList("media", _global); +} + +IriSP.Model.Source.prototype.getTags = function(_global) { + _global = (typeof _global !== "undefined" && _global); + return this.getList("tag", _global); +} + +IriSP.Model.Source.prototype.getMashups = function(_global) { + _global = (typeof _global !== "undefined" && _global); + return this.getList("mashup", _global); +} + +IriSP.Model.Source.prototype.getAnnotationTypes = function(_global) { + _global = (typeof _global !== "undefined" && _global); + return this.getList("annotationType", _global); +} + +IriSP.Model.Source.prototype.getAnnotationsByTypeTitle = function(_title, _global) { + _global = (typeof _global !== "undefined" && _global); + var _res = new IriSP.Model.List(this.directory), + _annTypes = this.getAnnotationTypes(_global).searchByTitle(_title); + _annTypes.forEach(function(_annType) { + _res.addElements(_annType.getAnnotations(_global)); + }) + return _res; +} + +IriSP.Model.Source.prototype.getDuration = function() { + var _m = this.currentMedia; + if (typeof _m !== "undefined") { + return this.currentMedia.duration; + } +} + +IriSP.Model.Source.prototype.merge = function(_source) { + var _this = this; + _source.forEach(function(_value, _key) { + _this.getList(_key).addElements(_value); + }); +} + +/* */ + +IriSP.Model.RemoteSource = function(_config) { + IriSP.Model.Source.call(this, _config); +} + +IriSP.Model.RemoteSource.prototype = new IriSP.Model.Source(); + +IriSP.Model.RemoteSource.prototype.get = function() { + this.status = IriSP.Model._SOURCE_STATUS_WAITING; + var _this = this; + this.serializer.loadData(this.url, function(_result) { + _this.deSerialize(_result); + _this.handleCallbacks(); + }); +} + +/* */ + +IriSP.Model.Directory = function() { + this.remoteSources = {}; + this.elements = {}; +} + +IriSP.Model.Directory.prototype.remoteSource = function(_properties) { + if (typeof _properties !== "object" || typeof _properties.url === "undefined") { + throw "Error : IriSP.Model.Directory.remoteSource(configuration): configuration.url is undefined"; + } + var _config = IriSP._({ directory: this }).extend(_properties); + if (typeof this.remoteSources[_properties.url] === "undefined") { + this.remoteSources[_properties.url] = new IriSP.Model.RemoteSource(_config); + } + return this.remoteSources[_properties.url]; +} + +IriSP.Model.Directory.prototype.newLocalSource = function(_properties) { + var _config = IriSP._({ directory: this }).extend(_properties), + _res = new IriSP.Model.Source(_config); + return _res; +} + +IriSP.Model.Directory.prototype.getElement = function(_id) { + return this.elements[_id]; +} + +IriSP.Model.Directory.prototype.addElement = function(_element) { + this.elements[_element.id] = _element; +} + +IriSP.Model.Directory.prototype.getGlobalList = function() { + var _res = new IriSP.Model.List(this); + _res.addIds(IriSP._(this.elements).keys()); + return _res; +} + +/* */