| author | durandn |
| Fri, 18 Sep 2015 14:39:45 +0200 | |
| changeset 1049 | 4e8b3df6e5be |
| parent 1043 | 967ac73327dc |
| child 1063 | e9765601184d |
| permissions | -rw-r--r-- |
| 998 | 1 |
/* widgets-container/metadataplayer.js - initialization and configuration of the widgets |
2 |
*/ |
|
3 |
||
4 |
/* The Metadataplayer Object, single point of entry, replaces IriSP.init_player */ |
|
5 |
||
| 1000 | 6 |
(function(ns) { |
7 |
||
8 |
var formerJQuery, formerUnderscore, former$; |
|
9 |
||
10 |
var Metadataplayer = ns.Metadataplayer = function(config) { |
|
11 |
ns.log("IriSP.Metadataplayer constructor"); |
|
12 |
for (var key in ns.guiDefaults) { |
|
13 |
if (ns.guiDefaults.hasOwnProperty(key) && !config.hasOwnProperty(key)) { |
|
14 |
config[key] = ns.guiDefaults[key] |
|
| 998 | 15 |
} |
16 |
} |
|
17 |
var _container = document.getElementById(config.container); |
|
18 |
_container.innerHTML = '<h3 class="Ldt-Loader">Loading... Chargement...</h3>'; |
|
| 1000 | 19 |
this.sourceManager = new ns.Model.Directory(); |
| 998 | 20 |
this.config = config; |
21 |
this.__events = {}; |
|
22 |
this.loadLibs(); |
|
23 |
}; |
|
24 |
||
| 1000 | 25 |
Metadataplayer.prototype.toString = function() { |
| 998 | 26 |
return 'Metadataplayer in #' + this.config.container; |
27 |
}; |
|
28 |
||
| 1000 | 29 |
Metadataplayer.prototype.on = function(_event, _callback) { |
| 998 | 30 |
if (typeof this.__events[_event] === "undefined") { |
31 |
this.__events[_event] = []; |
|
32 |
} |
|
33 |
this.__events[_event].push(_callback); |
|
34 |
}; |
|
35 |
||
| 1000 | 36 |
Metadataplayer.prototype.trigger = function(_event, _data) { |
| 998 | 37 |
var _element = this; |
| 1000 | 38 |
ns._(this.__events[_event]).each(function(_callback) { |
| 998 | 39 |
_callback.call(_element, _data); |
40 |
}); |
|
41 |
}; |
|
42 |
||
| 1000 | 43 |
Metadataplayer.prototype.loadLibs = function() { |
44 |
ns.log("IriSP.Metadataplayer.prototype.loadLibs"); |
|
| 998 | 45 |
var $L = $LAB |
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
46 |
.queueScript(ns.getLib("Mustache")); |
| 1000 | 47 |
|
48 |
formerJQuery = !!window.jQuery; |
|
49 |
former$ = !!window.$; |
|
50 |
formerUnderscore = !!window._; |
|
| 998 | 51 |
|
| 1000 | 52 |
if (typeof ns.jQuery === "undefined") { |
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
53 |
$L.queueScript(ns.getLib("jQuery")); |
| 1000 | 54 |
} |
55 |
|
|
56 |
if (typeof ns._ === "undefined") { |
|
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
57 |
$L.queueScript(ns.getLib("underscore")); |
| 1000 | 58 |
} |
59 |
|
|
60 |
if (typeof window.JSON == "undefined") { |
|
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
61 |
$L.queueScript(ns.getLib("json")); |
| 998 | 62 |
} |
63 |
|
|
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
64 |
$L.queueWait().queueScript(ns.getLib("jQueryUI")).queueWait(); |
| 998 | 65 |
|
66 |
/* widget specific requirements */ |
|
67 |
for(var _i = 0; _i < this.config.widgets.length; _i++) { |
|
68 |
var _t = this.config.widgets[_i].type; |
|
| 1000 | 69 |
if (typeof ns.widgetsRequirements[_t] !== "undefined" && typeof ns.widgetsRequirements[_t].requires !== "undefined" ) { |
70 |
for (var _j = 0; _j < ns.widgetsRequirements[_t].requires.length; _j++) { |
|
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
71 |
$L.queueScript(ns.getLib(ns.widgetsRequirements[_t].requires[_j])); |
| 998 | 72 |
} |
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
var _this = this; |
|
77 |
|
|
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
78 |
$L.queueWait(function() { |
| 998 | 79 |
_this.onLibsLoaded(); |
80 |
}); |
|
|
1043
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
81 |
|
|
967ac73327dc
slighlty improve jquery ui loading
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
82 |
$L.runQueue(); |
| 998 | 83 |
}; |
84 |
||
| 1000 | 85 |
Metadataplayer.prototype.onLibsLoaded = function() { |
86 |
ns.log("IriSP.Metadataplayer.prototype.onLibsLoaded"); |
|
87 |
if (typeof ns.jQuery === "undefined" && typeof window.jQuery !== "undefined") { |
|
88 |
ns.jQuery = window.jQuery; |
|
89 |
if (former$ || formerJQuery) { |
|
90 |
window.jQuery.noConflict(formerJQuery); |
|
91 |
} |
|
| 998 | 92 |
} |
| 1000 | 93 |
if (typeof ns._ === "undefined" && typeof window._ !== "undefined") { |
94 |
ns._ = window._; |
|
95 |
if (formerUnderscore) { |
|
96 |
_.noConflict(); |
|
97 |
} |
|
| 998 | 98 |
} |
| 1000 | 99 |
ns.loadCss(ns.getLib("cssjQueryUI")); |
100 |
ns.loadCss(this.config.css); |
|
| 998 | 101 |
|
| 1000 | 102 |
this.$ = ns.jQuery('#' + this.config.container); |
| 998 | 103 |
this.$.css({ |
104 |
"width": this.config.width, |
|
105 |
"clear": "both" |
|
106 |
}); |
|
107 |
if (typeof this.config.height !== "undefined") { |
|
108 |
this.$.css("height", this.config.height); |
|
109 |
} |
|
110 |
|
|
111 |
this.widgets = []; |
|
112 |
var _this = this; |
|
| 1000 | 113 |
ns._(this.config.widgets).each(function(widgetconf, key) { |
| 998 | 114 |
_this.widgets.push(null); |
115 |
_this.loadWidget(widgetconf, function(widget) { |
|
116 |
_this.widgets[key] = widget; |
|
117 |
if (widget.isLoaded()) { |
|
118 |
_this.trigger("widget-loaded"); |
|
119 |
} |
|
120 |
}); |
|
121 |
}); |
|
122 |
this.$.find('.Ldt-Loader').detach(); |
|
123 |
|
|
124 |
this.widgetsLoaded = false; |
|
125 |
|
|
126 |
this.on("widget-loaded", function() { |
|
127 |
if (_this.widgetsLoaded) { |
|
128 |
return; |
|
129 |
} |
|
| 1000 | 130 |
var isloaded = !ns._(_this.widgets).any(function(w) { |
| 1013 | 131 |
return !(w && w.isLoaded()); |
| 998 | 132 |
}); |
133 |
if (isloaded) { |
|
134 |
_this.widgetsLoaded = true; |
|
135 |
_this.trigger("widgets-loaded"); |
|
136 |
} |
|
137 |
}); |
|
138 |
}; |
|
139 |
||
| 1000 | 140 |
Metadataplayer.prototype.loadMetadata = function(_metadataInfo) { |
| 998 | 141 |
if (_metadataInfo.elementType === "source") { |
142 |
return _metadataInfo; |
|
143 |
} |
|
144 |
if (typeof _metadataInfo.serializer === "undefined" && typeof _metadataInfo.format !== "undefined") { |
|
| 1000 | 145 |
_metadataInfo.serializer = ns.serializers[_metadataInfo.format]; |
| 998 | 146 |
} |
147 |
if (typeof _metadataInfo.url !== "undefined" && typeof _metadataInfo.serializer !== "undefined") { |
|
148 |
return this.sourceManager.remoteSource(_metadataInfo); |
|
149 |
} else { |
|
150 |
return this.sourceManager.newLocalSource(_metadataInfo); |
|
151 |
} |
|
152 |
}; |
|
153 |
||
| 1000 | 154 |
Metadataplayer.prototype.loadWidget = function(_widgetConfig, _callback) { |
| 998 | 155 |
/* Creating containers if needed */ |
156 |
if (typeof _widgetConfig.container === "undefined") { |
|
157 |
var _divs = this.layoutDivs(_widgetConfig.type); |
|
158 |
_widgetConfig.container = _divs[0]; |
|
159 |
} |
|
160 |
|
|
161 |
var _this = this; |
|
162 |
|
|
| 1000 | 163 |
if (typeof ns.Widgets[_widgetConfig.type] !== "undefined") { |
164 |
ns._.defer(function() { |
|
165 |
_callback(new ns.Widgets[_widgetConfig.type](_this, _widgetConfig)); |
|
| 998 | 166 |
}); |
167 |
} else { |
|
168 |
/* Loading Widget CSS */ |
|
| 1000 | 169 |
if (typeof ns.widgetsRequirements[_widgetConfig.type] === "undefined" || typeof ns.widgetsRequirements[_widgetConfig.type].noCss === "undefined" || !ns.widgetsRequirements[_widgetConfig.type].noCss) { |
170 |
ns.loadCss(ns.widgetsDir + '/' + _widgetConfig.type + '.css'); |
|
| 998 | 171 |
} |
172 |
/* Loading Widget JS */ |
|
| 1000 | 173 |
$LAB.script(ns.widgetsDir + '/' + _widgetConfig.type + '.js').wait(function() { |
174 |
_callback(new ns.Widgets[_widgetConfig.type](_this, _widgetConfig)); |
|
| 998 | 175 |
}); |
176 |
} |
|
177 |
}; |
|
178 |
||
179 |
/** create a subdiv with an unique id, and a spacer div as well. |
|
180 |
@param widgetName the name of the widget. |
|
181 |
@return an array of the form [createdivId, spacerdivId]. |
|
182 |
*/ |
|
| 1000 | 183 |
Metadataplayer.prototype.layoutDivs = function(_name, _height) { |
| 998 | 184 |
if (typeof(_name) === "undefined") { |
185 |
_name = ""; |
|
186 |
} |
|
| 1000 | 187 |
var newDiv = ns._.uniqueId(this.config.container + "_widget_" + _name + "_"), |
188 |
spacerDiv = ns._.uniqueId("LdtPlayer_spacer_"), |
|
189 |
divHtml = ns.jQuery('<div>') |
|
| 998 | 190 |
.attr("id",newDiv) |
191 |
.css({ |
|
192 |
width: this.config.width + "px", |
|
193 |
position: "relative", |
|
194 |
clear: "both" |
|
195 |
}), |
|
| 1000 | 196 |
spacerHtml = ns.jQuery('<div>') |
| 998 | 197 |
.attr("id",spacerDiv) |
198 |
.css({ |
|
199 |
width: this.config.width + "px", |
|
200 |
height: this.config.spacer_div_height + "px", |
|
201 |
position: "relative", |
|
202 |
clear: "both" |
|
203 |
}); |
|
204 |
if (typeof _height !== "undefined") { |
|
205 |
divHtml.css("height", _height); |
|
206 |
} |
|
207 |
|
|
208 |
this.$.append(divHtml); |
|
209 |
this.$.append(spacerHtml); |
|
210 |
||
211 |
return [newDiv, spacerDiv]; |
|
212 |
}; |
|
213 |
||
| 1000 | 214 |
})(IriSP); |
215 |
||
| 998 | 216 |
/* End of widgets-container/metadataplayer.js */ |