1 /* init.js - initialization and configuration of Popcorn and the widgets |
1 /* init.js - initialization and configuration of Popcorn and the widgets |
2 exemple json configuration: |
2 exemple json configuration: |
3 |
3 |
|
4 */ |
|
5 |
|
6 /** do some magic to configure popcorn according to the options object passed. |
|
7 Works for html5, jwplayer and youtube videos |
4 */ |
8 */ |
5 |
9 IriSP.configurePopcorn = function(layoutManager, options) { |
6 /** |
|
7 set up the IriSP.__dataloader instance - |
|
8 we need it because we have to get the metadata |
|
9 about the video before that the widget have even |
|
10 loaded. |
|
11 */ |
|
12 IriSP.setupDataLoader = function() { |
|
13 /* we set it up separately because we need to |
|
14 get data at the very beginning, for instance when |
|
15 setting up the video */ |
|
16 IriSP.__dataloader = new IriSP.DataLoader(); |
|
17 }; |
|
18 |
|
19 /** do some magic to configure popcorn according to the options object passed. |
|
20 Works for html5, jwplayer and youtube videos |
|
21 */ |
|
22 IriSP.configurePopcorn = function (layoutManager, options) { |
|
23 var pop; |
10 var pop; |
24 var ret = layoutManager.createDiv(); |
11 var ret = layoutManager.createDiv(); |
25 var containerDiv = ret[0]; |
12 var containerDiv = ret[0]; |
26 var spacerDiv = ret[1]; |
13 var spacerDiv = ret[1]; |
27 |
14 |
28 /* insert one pixel of margin between the video and the first widget, using the |
15 /* insert one pixel of margin between the video and the first widget, using the |
29 spacer. |
16 spacer. |
30 */ |
17 */ |
31 IriSP.jQuery("#" + spacerDiv).css("height", "1px"); |
18 IriSP.jQuery("#" + spacerDiv).css("height", "1px"); |
32 |
19 |
33 switch(options.type) { |
20 switch(options.type) { |
34 /* |
21 /* |
35 todo : dynamically create the div/video tag which |
22 todo : dynamically create the div/video tag which |
36 will contain the video. |
23 will contain the video. |
37 */ |
24 */ |
38 case "html5": |
25 case "html5": |
39 var tmpId = Popcorn.guid("video"); |
26 var tmpId = Popcorn.guid("video"); |
40 IriSP.jQuery("#" + containerDiv).append("<video src='" + options.file + "' id='" + tmpId + "'></video>"); |
27 IriSP.jQuery("#" + containerDiv).append("<video src='" + options.video + "' id='" + tmpId + "'></video>"); |
41 |
28 |
42 if (options.hasOwnProperty("width")) |
29 if(options.hasOwnProperty("width")) |
43 IriSP.jQuery("#" + containerDiv).css("width", options.width); |
30 IriSP.jQuery("#" + containerDiv).css("width", options.width); |
44 |
31 |
45 if (options.hasOwnProperty("height")) |
32 if(options.hasOwnProperty("height")) |
46 IriSP.jQuery("#" + containerDiv).css("height", options.height); |
33 IriSP.jQuery("#" + containerDiv).css("height", options.height); |
47 |
34 pop = Popcorn("#" + tmpId); |
48 pop = Popcorn("#" + tmpId); |
35 break; |
49 break; |
36 |
|
37 case "jwplayer": |
|
38 var opts = IriSP.jQuery.extend({}, options); |
|
39 delete opts.container; |
|
40 delete opts.type; |
|
41 opts.file = opts.video; |
|
42 delete opts.video; |
|
43 |
|
44 if(!options.hasOwnProperty("flashplayer")) { |
|
45 opts.flashplayer = IriSP.jwplayer_swf_path; |
|
46 } |
|
47 |
|
48 if(!options.hasOwnProperty("controlbar.position")) { |
|
49 opts["controlbar.position"] = "none"; |
|
50 } |
|
51 pop = new IriSP.PopcornReplacement.jwplayer("#" + containerDiv, opts); |
|
52 break; |
|
53 |
|
54 case "youtube": |
|
55 var opts = IriSP.jQuery.extend({}, options); |
|
56 delete opts.container; |
|
57 opts.controls = 0; |
|
58 opts.autostart = false; |
|
59 // Popcorn.youtube wants us to specify the size of the player in the style attribute of its container div. |
|
60 IriSP.jQuery("#" + containerDiv).css({ |
|
61 width : opts.width + "px", |
|
62 height : opts.height + "px" |
|
63 }) |
|
64 pop = Popcorn.youtube("#" + containerDiv, opts.video, opts); |
|
65 break; |
|
66 |
|
67 case "dailymotion": |
|
68 pop = new IriSP.PopcornReplacement.dailymotion("#" + containerDiv, options); |
|
69 break; |
|
70 |
|
71 case "allocine": |
|
72 /* pass the options as-is to the allocine player and let it handle everything */ |
|
73 pop = new IriSP.PopcornReplacement.allocine("#" + containerDiv, options); |
|
74 break; |
|
75 |
|
76 default: |
|
77 pop = undefined; |
|
78 }; |
|
79 |
|
80 return pop; |
|
81 }; |
|
82 /** Configure the gui and instantiate the widgets passed as parameters |
|
83 @param guiOptions the gui object as seen in the examples. |
|
84 */ |
|
85 IriSP.configureWidgets = function(popcornInstance, layoutManager, guiOptions) { |
|
86 |
|
87 var serialFactory = new IriSP.SerializerFactory(IriSP.__dataloader); |
|
88 var params = { |
|
89 width : guiOptions.width, |
|
90 height : guiOptions.height |
|
91 }; |
|
92 |
|
93 var default_options = guiOptions.default_options; |
|
94 if(IriSP.null_or_undefined(default_options)) |
|
95 default_options = {}; |
|
96 |
|
97 var ret_widgets = []; |
|
98 var index; |
|
99 |
|
100 for( index = 0; index < guiOptions.widgets.length; index++) { |
|
101 var widget = IriSP.instantiateWidget(popcornInstance, serialFactory, layoutManager, guiOptions.widgets[index], default_options); |
|
102 |
|
103 ret_widgets.push(widget); |
|
104 }; |
|
105 |
|
106 return ret_widgets; |
|
107 }; |
|
108 /** configure modules. @see configureWidgets */ |
|
109 IriSP.configureModules = function(popcornInstance, modulesList) { |
|
110 /* if(IriSP.null_or_undefined(modulesList)) |
|
111 return; |
|
112 |
|
113 var serialFactory = new IriSP.SerializerFactory(IriSP.__dataloader); |
|
114 var ret_modules = []; |
|
115 var index; |
|
116 |
|
117 for( index = 0; index < modulesList.length; index++) { |
|
118 var moduleConfig = modulesList[index]; |
|
119 |
|
120 var serializer = serialFactory.getSerializer(moduleConfig.metadata); |
|
121 var module = new IriSP[moduleConfig.type](popcornInstance, moduleConfig, serializer); |
|
122 ret_modules.push(module); |
|
123 }; |
|
124 |
|
125 return ret_modules; */ |
|
126 }; |
|
127 /** instantiate a widget - only called by configureWidgets, never by the user. Handles widget |
|
128 dependencies. |
|
129 @param popcornInstance popcorn instance the widget will user |
|
130 @param serialFactory serializer factory to instantiate the widget with |
|
131 @param layoutManager layout manager |
|
132 @param widgetConfig configuration options for the widget |
|
133 @param defaultOptions a dictionnary with some options defined for every widget. |
|
134 */ |
|
135 IriSP.instantiateWidget = function(popcornInstance, serialFactory, layoutManager, widgetConfig, defaultOptions) { |
|
136 |
|
137 if(IriSP.null_or_undefined(defaultOptions)) |
|
138 defaultOptions = {}; |
|
139 widgetConfig = IriSP.underscore.defaults(widgetConfig, defaultOptions); |
|
140 |
|
141 var arr = IriSP.jQuery.extend({}, widgetConfig); |
|
142 |
|
143 /* create a div for those widgets who didn't already specify a container; */ |
|
144 if(!arr.hasOwnProperty("container")) { |
|
145 /* create div returns us a container for the widget and a spacer */ |
|
146 var ret = layoutManager.createDiv(widgetConfig.type); |
|
147 var container = ret[0]; |
|
148 var spacer = ret[1]; |
|
149 arr.container = container; |
|
150 arr.spacer = spacer; |
|
151 arr.layoutManager = layoutManager; |
|
152 } |
|
153 var serializer = serialFactory.getSerializer(widgetConfig.metadata); |
|
154 |
|
155 if( typeof serializer == "undefined") |
|
156 debugger; |
|
157 |
|
158 // instantiate the object passed as a string |
|
159 var widget = new IriSP[widgetConfig.type](popcornInstance, arr, serializer); |
|
160 |
|
161 if(widgetConfig.hasOwnProperty("requires")) { |
|
162 // also create the widgets this one depends on. |
|
163 // the dependency widget is available in the parent widget context as |
|
164 // this.WidgetName (for instance, this.TipWidget); |
|
165 |
|
166 var i = 0; |
|
167 for( i = 0; i < widgetConfig.requires.length; i++) { |
|
168 var widgetName = widgetConfig.requires[i]["type"], _configobj = IriSP.jQuery.extend({}, widgetConfig.requires[i]), _div = document.createElement('div'), _container = IriSP.guid(arr.container + '_' + widgetName + '_'); |
|
169 _configobj.container = _container; |
|
170 _div.id = _container; |
|
171 widget.selector.append(_div); |
|
172 widget[widgetName] = IriSP.instantiateWidget(popcornInstance, serialFactory, layoutManager, _configobj, defaultOptions); |
|
173 } |
|
174 } |
|
175 |
|
176 serializer.sync(IriSP.wrap(widget, function() { |
|
177 this.draw(); |
|
178 })); |
|
179 return widget; |
|
180 }; |
|
181 /** single point of entry for the metadataplayer */ |
|
182 IriSP.initPlayer = function(config, metadata_url, format) { |
|
183 document.getElementById(config.gui.container).innerHTML = IriSP.templToHTML(IriSP.loading_template, config.gui); |
|
184 IriSP.loadLibs(config, metadata_url, format, function() { |
|
185 |
|
186 var layoutManager = new IriSP.LayoutManager(config.gui); |
50 |
187 |
51 case "jwplayer": |
188 if (typeof IriSP._videoData !== "undefined" && typeof config.player.video === "undefined") { |
52 var opts = IriSP.jQuery.extend({}, options); |
189 var _media = IriSP._videoData.currentMedia; |
53 delete opts.container; |
190 if (typeof _media !== "undefined") { |
54 delete opts.type; |
191 config.player.video = _media.video; |
55 |
192 if (typeof _media.streamer !== "undefined") { |
56 |
193 config.player.streamer = _media.streamer; |
57 /* Try to guess options.file and options.streamer only if file and streamer |
194 config.player.video = _media.video.replace(_media.streamer,''); |
58 are not already defined in the configuration */ |
195 } |
59 if (options.provider === "rtmp" && !opts.hasOwnProperty("file") && !opts.hasOwnProperty("streamer")) { |
|
60 /* exit if we can't access the metadata */ |
|
61 if (typeof(IriSP.__jsonMetadata) === "undefined") { |
|
62 break; |
|
63 }; |
|
64 |
|
65 // the json format is totally illogical |
|
66 //opts.streamer = IriSP.__jsonMetadata["medias"][0]["meta"]["item"]["value"]; |
|
67 //var source = IriSP.__jsonMetadata["medias"][0]["href"]; |
|
68 |
|
69 // the source if a full url but jwplayer wants an url relative to the |
|
70 // streamer url, so we've got to remove the common part. |
|
71 //opts.file = source.slice(opts.streamer.length); |
|
72 |
|
73 /* sometimes we get served a file with a wrong path and streamer. |
|
74 as a streamer is of the form rtmp://domain/path/ and the media is |
|
75 the rest, we uglily do this : |
|
76 */ |
|
77 opts.file = ""; |
|
78 opts.streamer = ""; |
|
79 var fullPath = IriSP.get_aliased(IriSP.__jsonMetadata["medias"][0], ["href","url"]); |
|
80 |
|
81 if (fullPath === null) { |
|
82 console.log("no url or href field defined in the metadata."); |
|
83 } |
196 } |
84 |
197 |
85 var pathSplit = fullPath.split('/'); |
198 } |
86 |
199 |
87 for (var i = 0; i < pathSplit.length; i++) { |
200 var pop = IriSP.configurePopcorn(layoutManager, config.player); |
88 if (i < 4) { |
201 |
89 opts.streamer += pathSplit[i] + "/"; |
202 IriSP._widgets = IriSP.configureWidgets(pop, layoutManager, config.gui); |
90 } else { |
203 IriSP.jQuery('#Ldt-loader').detach(); |
91 opts.file += pathSplit[i]; |
204 }); |
92 /* omit the last slash if we're on the last element */ |
205 }; |
93 if (i < pathSplit.length - 1) |
|
94 opts.file += "/"; |
|
95 } |
|
96 } |
|
97 } else { |
|
98 /* other providers type, video for instance - |
|
99 pass everything as is */ |
|
100 } |
|
101 |
|
102 if (!options.hasOwnProperty("flashplayer")) { |
|
103 opts.flashplayer = IriSP.jwplayer_swf_path; |
|
104 } |
|
105 |
|
106 if (!options.hasOwnProperty("controlbar.position")) { |
|
107 opts["controlbar.position"] = "none"; |
|
108 } |
|
109 |
|
110 pop = new IriSP.PopcornReplacement.jwplayer("#" + containerDiv, opts); |
|
111 break; |
|
112 |
|
113 case "youtube": |
|
114 var opts = IriSP.jQuery.extend({}, options); |
|
115 delete opts.container; |
|
116 opts.controls = 0; |
|
117 opts.autostart = false; |
|
118 templ = "width: {{width}}px; height: {{height}}px;"; |
|
119 var str = Mustache.to_html(templ, {width: opts.width, height: opts.height}); |
|
120 // Popcorn.youtube wants us to specify the size of the player in the style attribute of its container div. |
|
121 IriSP.jQuery("#" + containerDiv).attr("style", str); |
|
122 |
|
123 pop = Popcorn.youtube("#" + containerDiv, opts.video, opts); |
|
124 break; |
|
125 |
|
126 case "dailymotion": |
|
127 pop = new IriSP.PopcornReplacement.dailymotion("#" + containerDiv, options); |
|
128 break; |
|
129 |
|
130 case "allocine": |
|
131 /* pass the options as-is to the allocine player and let it handle everything */ |
|
132 pop = new IriSP.PopcornReplacement.allocine("#" + containerDiv, options); |
|
133 break; |
|
134 |
|
135 default: |
|
136 pop = undefined; |
|
137 }; |
|
138 |
|
139 return pop; |
|
140 }; |
|
141 |
|
142 /** Configure the gui and instantiate the widgets passed as parameters |
|
143 @param guiOptions the gui object as seen in the examples. |
|
144 */ |
|
145 IriSP.configureWidgets = function (popcornInstance, layoutManager, guiOptions) { |
|
146 |
|
147 var serialFactory = new IriSP.SerializerFactory(IriSP.__dataloader); |
|
148 var params = {width: guiOptions.width, height: guiOptions.height}; |
|
149 |
|
150 var default_options = guiOptions.default_options; |
|
151 if (IriSP.null_or_undefined(default_options)) |
|
152 default_options = {}; |
|
153 |
|
154 var ret_widgets = []; |
|
155 var index; |
|
156 |
|
157 for (index = 0; index < guiOptions.widgets.length; index++) { |
|
158 var widget = IriSP.instantiateWidget(popcornInstance, serialFactory, layoutManager, guiOptions.widgets[index], default_options); |
|
159 |
|
160 ret_widgets.push(widget); |
|
161 }; |
|
162 |
|
163 return ret_widgets; |
|
164 }; |
|
165 |
|
166 /** configure modules. @see configureWidgets */ |
|
167 IriSP.configureModules = function (popcornInstance, modulesList) { |
|
168 if (IriSP.null_or_undefined(modulesList)) |
|
169 return; |
|
170 |
|
171 var serialFactory = new IriSP.SerializerFactory(IriSP.__dataloader); |
|
172 var ret_modules = []; |
|
173 var index; |
|
174 |
|
175 for (index = 0; index < modulesList.length; index++) { |
|
176 var moduleConfig = modulesList[index]; |
|
177 |
|
178 var serializer = serialFactory.getSerializer(moduleConfig.metadata); |
|
179 var module = new IriSP[moduleConfig.type](popcornInstance, moduleConfig, serializer); |
|
180 ret_modules.push(module); |
|
181 }; |
|
182 |
|
183 return ret_modules; |
|
184 }; |
|
185 |
|
186 /** instantiate a widget - only called by configureWidgets, never by the user. Handles widget |
|
187 dependencies. |
|
188 @param popcornInstance popcorn instance the widget will user |
|
189 @param serialFactory serializer factory to instantiate the widget with |
|
190 @param layoutManager layout manager |
|
191 @param widgetConfig configuration options for the widget |
|
192 @param defaultOptions a dictionnary with some options defined for every widget. |
|
193 */ |
|
194 IriSP.instantiateWidget = function(popcornInstance, serialFactory, layoutManager, widgetConfig, defaultOptions) { |
|
195 |
|
196 if (IriSP.null_or_undefined(defaultOptions)) |
|
197 defaultOptions = {}; |
|
198 |
|
199 widgetConfig = IriSP.underscore.defaults(widgetConfig, defaultOptions); |
|
200 |
|
201 var arr = IriSP.jQuery.extend({}, widgetConfig); |
|
202 |
|
203 /* create a div for those widgets who didn't already specify a container; */ |
|
204 if (!arr.hasOwnProperty("container")) { |
|
205 /* create div returns us a container for the widget and a spacer */ |
|
206 var ret = layoutManager.createDiv(widgetConfig.type); |
|
207 var container = ret[0]; |
|
208 var spacer = ret[1]; |
|
209 arr.container = container; |
|
210 arr.spacer = spacer; |
|
211 arr.layoutManager = layoutManager; |
|
212 } |
|
213 var serializer = serialFactory.getSerializer(widgetConfig.metadata); |
|
214 |
|
215 if (typeof serializer == "undefined") |
|
216 debugger; |
|
217 |
|
218 // instantiate the object passed as a string |
|
219 var widget = new IriSP[widgetConfig.type](popcornInstance, arr, serializer); |
|
220 |
|
221 if (widgetConfig.hasOwnProperty("requires")) { |
|
222 // also create the widgets this one depends on. |
|
223 // the dependency widget is available in the parent widget context as |
|
224 // this.WidgetName (for instance, this.TipWidget); |
|
225 |
|
226 var i = 0; |
|
227 for(i = 0; i < widgetConfig.requires.length; i++) { |
|
228 var widgetName = widgetConfig.requires[i]["type"], |
|
229 _configobj = IriSP.jQuery.extend({}, widgetConfig.requires[i]), |
|
230 _div = document.createElement('div'), |
|
231 _container = IriSP.guid(arr.container + '_' + widgetName + '_'); |
|
232 _configobj.container = _container; |
|
233 _div.id = _container; |
|
234 widget.selector.append(_div); |
|
235 widget[widgetName] = IriSP.instantiateWidget(popcornInstance, serialFactory, layoutManager, _configobj, defaultOptions); |
|
236 } |
|
237 } |
|
238 |
|
239 serializer.sync(IriSP.wrap(widget, function() { this.draw(); })); |
|
240 return widget; |
|
241 }; |
|
242 |
|
243 /** single point of entry for the metadataplayer */ |
|
244 IriSP.initPlayer = function(config, metadata_url) { |
|
245 document.getElementById(config.gui.container).innerHTML = IriSP.templToHTML(IriSP.loading_template, config.gui); |
|
246 IriSP.loadLibs(config, metadata_url, |
|
247 function() { |
|
248 |
|
249 var layoutManager = new IriSP.LayoutManager(config.gui); |
|
250 |
|
251 var pop = IriSP.configurePopcorn(layoutManager, config.player); |
|
252 |
|
253 IriSP._widgets = IriSP.configureWidgets(pop, layoutManager, config.gui); |
|
254 IriSP._modules = IriSP.configureModules(pop, config.modules); |
|
255 IriSP.jQuery('#Ldt-loader').detach(); |
|
256 }); |
|
257 }; |
|