src/js/init.js
branchpopcorn-port
changeset 171 158f0193ec54
parent 158 1813e6e4f0d6
child 175 b0cb7132accb
--- 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