|
1 /* init.js - initialization and configuration of Popcorn and the widgets |
|
2 exemple json configuration: |
|
3 |
|
4 */ |
|
5 |
|
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; |
|
24 var ret = layoutManager.createDiv(); |
|
25 var containerDiv = ret[0]; |
|
26 var spacerDiv = ret[1]; |
|
27 |
|
28 /* insert one pixel of margin between the video and the first widget, using the |
|
29 spacer. |
|
30 */ |
|
31 IriSP.jQuery("#" + spacerDiv).css("height", "1px"); |
|
32 |
|
33 switch(options.type) { |
|
34 /* |
|
35 todo : dynamically create the div/video tag which |
|
36 will contain the video. |
|
37 */ |
|
38 case "html5": |
|
39 var tmpId = Popcorn.guid("video"); |
|
40 IriSP.jQuery("#" + containerDiv).append("<video src='" + options.file + "' id='" + tmpId + "'></video>"); |
|
41 |
|
42 if (options.hasOwnProperty("width")) |
|
43 IriSP.jQuery("#" + containerDiv).css("width", options.width); |
|
44 |
|
45 if (options.hasOwnProperty("height")) |
|
46 IriSP.jQuery("#" + containerDiv).css("height", options.height); |
|
47 |
|
48 pop = Popcorn("#" + tmpId); |
|
49 break; |
|
50 |
|
51 case "jwplayer": |
|
52 var opts = IriSP.jQuery.extend({}, options); |
|
53 delete opts.container; |
|
54 delete opts.type; |
|
55 |
|
56 |
|
57 /* Try to guess options.file and options.streamer only if file and streamer |
|
58 are not already defined in the configuration */ |
|
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 } |
|
84 |
|
85 var pathSplit = fullPath.split('/'); |
|
86 |
|
87 for (var i = 0; i < pathSplit.length; i++) { |
|
88 if (i < 4) { |
|
89 opts.streamer += pathSplit[i] + "/"; |
|
90 } else { |
|
91 opts.file += pathSplit[i]; |
|
92 /* omit the last slash if we're on the last element */ |
|
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 }; |