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 |
|
4 */ |
3 */ |
5 |
4 |
6 /** do some magic to configure popcorn according to the options object passed. |
5 /* The Metadataplayer Object, single point of entry, replaces IriSP.init_player */ |
7 Works for html5, jwplayer and youtube videos |
6 |
8 */ |
7 IriSP.Metadataplayer = function(config, video_metadata) { |
9 IriSP.configurePopcorn = function(layoutManager, options) { |
8 IriSP._.defaults(config.gui, IriSP.guiDefaults); |
10 var pop; |
9 var _container = document.getElementById(config.gui.container); |
11 var ret = layoutManager.createDiv(); |
10 _container.innerHTML = IriSP.templToHTML(IriSP.loading_template, config.gui); |
12 var containerDiv = ret[0]; |
11 this.video_metadata = video_metadata; |
13 var spacerDiv = ret[1]; |
12 this.sourceManager = new IriSP.Model.Directory(); |
14 |
13 this.config = config; |
15 /* insert one pixel of margin between the video and the first widget, using the |
14 this.loadLibs(); |
16 spacer. |
15 } |
|
16 |
|
17 IriSP.Metadataplayer.prototype.toString = function() { |
|
18 return 'A Metadataplayer in DIV #' + this.config.gui.container; |
|
19 } |
|
20 |
|
21 IriSP.Metadataplayer.prototype.loadLibs = function() { |
|
22 console.log("Loading Libs"); |
|
23 // Localize jQuery variable |
|
24 IriSP.jQuery = null; |
|
25 var $L = $LAB.script(IriSP.getLib("jQuery")).script(IriSP.getLib("swfObject")).wait().script(IriSP.getLib("jQueryUI")); |
|
26 |
|
27 if(this.config.player.type === "jwplayer" || this.config.player.type === "allocine" || this.config.player.type === "dailymotion") { |
|
28 // load our popcorn.js lookalike |
|
29 $L.script(IriSP.getLib("jwplayer")); |
|
30 } else { |
|
31 // load the real popcorn |
|
32 $L.script(IriSP.getLib("popcorn")).script(IriSP.getLib("popcorn.code")); |
|
33 // load plugins if necessary |
|
34 if(this.config.player.type === "youtube") { |
|
35 $L.script(IriSP.getLib("popcorn.youtube")); |
|
36 } |
|
37 if(this.config.player.type === "vimeo"){ |
|
38 $L.script(IriSP.getLib("popcorn.vimeo")); |
|
39 } |
|
40 } |
|
41 |
|
42 /* widget specific requirements */ |
|
43 for(var _i = 0; _i < this.config.gui.widgets.length; _i++) { |
|
44 if(this.config.gui.widgets[_i].type === "PolemicWidget" || this.config.gui.widgets[_i].type === "StackGraphWidget" || this.config.gui.widgets[_i].type === "SparklineWidget") { |
|
45 $L.script(IriSP.getLib("raphael")); |
|
46 } |
|
47 if(this.config.gui.widgets[_i].type === "TraceWidget") { |
|
48 $L.script(IriSP.getLib("tracemanager")) |
|
49 } |
|
50 } |
|
51 |
|
52 var _this = this; |
|
53 |
|
54 $L.wait(function() { |
|
55 console.log("jQuery is loaded"); |
|
56 IriSP.jQuery = window.jQuery.noConflict(true); |
|
57 |
|
58 var css_link_jquery = IriSP.jQuery("<link>", { |
|
59 rel : "stylesheet", |
|
60 type : "text/css", |
|
61 href : IriSP.getLib("cssjQueryUI") |
|
62 }); |
|
63 var css_link_custom = IriSP.jQuery("<link>", { |
|
64 rel : "stylesheet", |
|
65 type : "text/css", |
|
66 href : _this.config.gui.css |
|
67 }); |
|
68 console.log('Appending CSS'); |
|
69 |
|
70 css_link_jquery.appendTo('head'); |
|
71 css_link_custom.appendTo('head'); |
|
72 |
|
73 console.log(_this); |
|
74 _this.onLibsLoaded(); |
|
75 |
|
76 }); |
|
77 } |
|
78 |
|
79 IriSP.Metadataplayer.prototype.loadMetadata = function(_metadataInfo) { |
|
80 if (typeof _metadataInfo.serializer === "undefined" && typeof _metadataInfo.format !== "undefined") { |
|
81 _metadataInfo.serializer = IriSP.serializers[_metadataInfo.format]; |
|
82 } |
|
83 if (typeof _metadataInfo.url === "undefined" && typeof _metadataInfo.src !== "undefined") { |
|
84 _metadataInfo.url = _metadataInfo.src; |
|
85 } |
|
86 console.log(_metadataInfo); |
|
87 if (typeof _metadataInfo.url !== "undefined" && typeof _metadataInfo.serializer !== "undefined") { |
|
88 return this.sourceManager.remoteSource(_metadataInfo); |
|
89 } else { |
|
90 return this.sourceManager.newLocalSource(_metadataInfo); |
|
91 } |
|
92 } |
|
93 |
|
94 IriSP.Metadataplayer.prototype.onLibsLoaded = function() { |
|
95 console.log("Libs Loaded"); |
|
96 this.videoData = this.loadMetadata(this.video_metadata); |
|
97 console.log(this.videoData); |
|
98 this.$ = IriSP.jQuery('#' + this.config.gui.container); |
|
99 this.$.css({ |
|
100 "width": this.config.gui.width, |
|
101 "clear": "both" |
|
102 }); |
|
103 if (typeof this.config.gui.height !== "undefined") { |
|
104 this.$.css("height", this.config.gui.height); |
|
105 } |
|
106 |
|
107 var _this = this; |
|
108 console.log("calling OnLoad"); |
|
109 this.videoData.onLoad(function() { |
|
110 _this.onVideoDataLoaded(); |
|
111 }); |
|
112 } |
|
113 |
|
114 IriSP.Metadataplayer.prototype.onVideoDataLoaded = function() { |
|
115 console.log("Video Data Loaded"); |
|
116 if (typeof this.videoData !== "undefined" && typeof this.config.player.video === "undefined") { |
|
117 var _media = this.videoData.currentMedia; |
|
118 if (typeof _media !== "undefined") { |
|
119 config.player.video = _media.video; |
|
120 if (typeof _media.streamer !== "undefined") { |
|
121 config.player.streamer = _media.streamer; |
|
122 config.player.video = _media.video.replace(_media.streamer,''); |
|
123 } |
|
124 } |
|
125 |
|
126 } |
|
127 this.configurePopcorn(config.player); |
|
128 this.widgets = []; |
|
129 for(var i = 0; i < this.config.gui.widgets.length; i++) { |
|
130 this.widgets.push(new IriSP.Widgets[_config.type](this, this.config.gui.widgets[i])); |
|
131 }; |
|
132 this.$('.Ldt-loader').detach(); |
|
133 } |
|
134 |
|
135 IriSP.Metadataplayer.prototype.configurePopcorn = function() { |
|
136 var pop, |
|
137 ret = this.layoutDivs(), |
|
138 containerDiv = ret[0], |
|
139 spacerDiv = ret[1]; |
|
140 |
|
141 /* insert one pixel of margin between the video and the first widget, |
|
142 * using the spacer. |
17 */ |
143 */ |
18 IriSP.jQuery("#" + spacerDiv).css("height", "1px"); |
144 IriSP.jQuery("#" + spacerDiv).css("height", Math.max(1, this.config.gui.spacer_div_height) + "px"); |
19 |
145 |
20 switch(options.type) { |
146 switch(options.type) { |
21 /* |
147 /* |
22 todo : dynamically create the div/video tag which |
148 todo : dynamically create the div/video tag which |
23 will contain the video. |
149 will contain the video. |
70 |
196 |
71 case "allocine": |
197 case "allocine": |
72 /* pass the options as-is to the allocine player and let it handle everything */ |
198 /* pass the options as-is to the allocine player and let it handle everything */ |
73 pop = new IriSP.PopcornReplacement.allocine("#" + containerDiv, options); |
199 pop = new IriSP.PopcornReplacement.allocine("#" + containerDiv, options); |
74 break; |
200 break; |
75 |
201 |
76 default: |
202 default: |
77 pop = undefined; |
203 pop = undefined; |
78 }; |
204 }; |
79 |
205 |
80 return pop; |
206 this.popcorn = pop; |
|
207 } |
|
208 |
|
209 /** create a subdiv with an unique id, and a spacer div as well. |
|
210 @param widgetName the name of the widget. |
|
211 @return an array of the form [createdivId, spacerdivId]. |
|
212 */ |
|
213 IriSP.Metadataplayer.prototype.layoutDivs = function(_name) { |
|
214 if (typeof(_name) === "undefined") { |
|
215 _name = ""; |
|
216 } |
|
217 var newDiv = IriSP.guid(this.container + "_widget_" + _name + "_"), |
|
218 spacerDiv = IriSP.guid("LdtPlayer_spacer_"), |
|
219 divTempl = "<div id='{{id}}' style='width: {{width}}px; position: relative; clear: both;'></div>", |
|
220 spacerTempl = "<div id='{{spacer_id}}' style='width: {{width}}px; position: relative; height: {{spacer_div_height}}px;'></div>", |
|
221 divHtml = Mustache.to_html( divTempl, |
|
222 { |
|
223 id: newDiv, |
|
224 width: this.config.gui.width |
|
225 }), |
|
226 spacerHtml = Mustache.to_html( spacerTempl, |
|
227 { |
|
228 spacer_id: spacerDiv, |
|
229 width: this.config.gui.width, |
|
230 spacer_div_height: this.config.gui.height |
|
231 }); |
|
232 |
|
233 this.$.append(divCode); |
|
234 this.$.append(spacerCode); |
|
235 |
|
236 return [newDiv, spacerDiv]; |
81 }; |
237 }; |
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); |
|
187 |
|
188 if (typeof IriSP._videoData !== "undefined" && typeof config.player.video === "undefined") { |
|
189 var _media = IriSP._videoData.currentMedia; |
|
190 if (typeof _media !== "undefined") { |
|
191 config.player.video = _media.video; |
|
192 if (typeof _media.streamer !== "undefined") { |
|
193 config.player.streamer = _media.streamer; |
|
194 config.player.video = _media.video.replace(_media.streamer,''); |
|
195 } |
|
196 } |
|
197 |
|
198 } |
|
199 |
|
200 var pop = IriSP.configurePopcorn(layoutManager, config.player); |
|
201 |
|
202 IriSP._widgets = IriSP.configureWidgets(pop, layoutManager, config.gui); |
|
203 IriSP.jQuery('#Ldt-loader').detach(); |
|
204 }); |
|
205 }; |
|