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