added support for dependency widgets (i.e : a widget can now depend on another, popcorn-port
authorhamidouk
Wed, 02 Nov 2011 12:33:20 +0100
branchpopcorn-port
changeset 171 158f0193ec54
parent 170 5150ae56e0a6
child 172 3ffa0e0c8803
added support for dependency widgets (i.e : a widget can now depend on another, and the latter will be instantiated and available to the former).
src/js/init.js
unittests/tests/init.js
--- a/src/js/init.js	Wed Nov 02 10:58:34 2011 +0100
+++ b/src/js/init.js	Wed Nov 02 12:33:20 2011 +0100
@@ -42,21 +42,36 @@
   var index;
   
   for (index = 0; index < guiOptions.widgets.length; index++) {    
-    var widget = guiOptions.widgets[index];
-    var container = layoutManager.createDiv();
-        
-    var arr = IriSP.jQuery.extend({}, widget);
-    arr.container = container;
-
-    var serializer = serialFactory.getSerializer(widget.metadata);    
-
-    // instantiate the object passed as a string
-    var widget = new IriSP[widget.type](popcornInstance, arr, serializer);
-    
-    serializer.sync(IriSP.wrap(widget, function() { this.draw(); }));
+    var widgetConfig = guiOptions.widgets[index];
+    var widget = IriSP.instantiateWidget(popcornInstance, serialFactory, layoutManager, widgetConfig);
     ret_widgets.push(widget);
    
   };
 
   return ret_widgets;
+};
+
+IriSP.instantiateWidget = function(popcornInstance, serialFactory, layoutManager, widgetConfig) {
+    var container = layoutManager.createDiv();        
+    var arr = IriSP.jQuery.extend({}, widget);
+    arr.container = container;
+
+    var serializer = serialFactory.getSerializer(widgetConfig.metadata);    
+
+    // instantiate the object passed as a string
+    var widget = new IriSP[widgetConfig.type](popcornInstance, arr, serializer);    
+    
+    if (widgetConfig.hasOwnProperty("requires")) {
+      // also create the widgets this one depends on.
+      // the dependency widget is available in the parent widget context as
+      // this.WidgetName (for instance, this.TipWidget);
+      
+      for(var j in widgetConfig.requires) {
+        var widgetName = widgetConfig.requires[j]["type"];
+        widget[widgetName] = IriSP.instantiateWidget(popcornInstance, serialFactory, layoutManager, widgetConfig.requires[j]);
+      }
+    }
+    
+    serializer.sync(IriSP.wrap(widget, function() { this.draw(); }));
+    return widget;
 };
\ No newline at end of file
--- a/unittests/tests/init.js	Wed Nov 02 10:58:34 2011 +0100
+++ b/unittests/tests/init.js	Wed Nov 02 12:33:20 2011 +0100
@@ -69,6 +69,55 @@
     equal(elem.attr("src"), popcornOptions.file, "the src attribute is set correctly");    
   });
   
+  test("test the instantiation of a single widget without dependencies", function() {
+    
+    var dt = new IriSP.DataLoader();
+    var serialFactory = new IriSP.SerializerFactory(dt);
+ 
+    var layoutManager = new IriSP.LayoutManager({container: "LdtPlayer", width: 327, height: 542});
+    var pop = IriSP.configurePopcorn(layoutManager, this.popcornOptions);
+    var conf = {type: "PlayerWidget",
+               mode: "radio",
+               metadata:{
+                format:'cinelab',
+                src:'test.json',
+                type:'dummy'}
+              };
+              
+    var res = IriSP.instantiateWidget(pop, serialFactory, layoutManager, conf);    
+    ok(res instanceof IriSP.PlayerWidget, "the returned widget is of the correct instance");
+  });  
+  
+  test("test the instantiation of a single widget with one dependency", function() {
+    var dt = new IriSP.DataLoader();
+    var serialFactory = new IriSP.SerializerFactory(dt);
+ 
+    var layoutManager = new IriSP.LayoutManager({container: "LdtPlayer", width: 327, height: 542});    
+    
+    var pop = IriSP.configurePopcorn(layoutManager, this.popcornOptions);
+    var conf = {type: "PlayerWidget",
+               mode: "radio",
+               metadata:{
+                format:'cinelab',
+                src:'test.json',
+                type:'dummy'},
+                requires: [
+                {type: "PlayerWidget",
+                  mode: "radio",
+                  metadata:{
+                    format:'cinelab',
+                    src:'test.json',
+                    type:'dummy'
+                } }]
+              };
+    
+    
+    var res = IriSP.instantiateWidget(pop, serialFactory, layoutManager, conf);
+    
+    ok(res instanceof IriSP.PlayerWidget, "the returned widget is of the correct instance");
+    ok(res.PlayerWidget instanceof IriSP.PlayerWidget, "the dependency widget is accessible from the parent");
+  });
+  
   test("test the instantiation of a bunch of widgets", function() {
   
     var layoutManager = new IriSP.LayoutManager({container: "LdtPlayer", width: 327, height: 542});
@@ -81,4 +130,6 @@
     ok(widgets[2] instanceof IriSP.AnnotationsWidget, "third widget is an annotation widget");
     equal(IriSP.jQuery("#" + this.widgetOptions.container).length, 1, "a new dom element has been created");
   });
+  
+
 }
\ No newline at end of file