src/js/widgets.js
branchnew-model
changeset 875 43629caa77bc
parent 874 38b65761a7d5
child 876 03967b6ada7c
--- a/src/js/widgets.js	Fri Apr 20 19:13:11 2012 +0200
+++ b/src/js/widgets.js	Mon Apr 23 19:11:08 2012 +0200
@@ -1,4 +1,8 @@
-/* the widget classes and definitions */
+/* Definition of an ancestor for the Widget classes */
+
+if (typeof IriSP.Widgets === "undefined") {
+    IriSP.Widgets = {}
+}
 
 /**
  * @class IriSP.Widget is an "abstract" class. It's mostly used to define some properties common to every widget.
@@ -10,7 +14,9 @@
  * @param player - a reference to the player widget
  * @param config - configuration options for the widget
  */
-IriSP.Widget = function(player, config) {
+
+
+IriSP.Widgets.Widget = function(player, config) {
 
     if( typeof player === "undefined") {
         /* Probably an abstract call of the class when
@@ -20,16 +26,9 @@
     
     /* Setting all the configuration options */
     var _type = config.type,
-        _config = IriSP._.defaults({}, config, player.config.gui.default_options, IriSP.widgetsDefaults[_type]),
+        _config = IriSP._.defaults({}, config, player.config.gui.default_options, this.defaults),
         _this = this;
     
-    /* Creating containers if needed */
-    if (typeof _config.container === "undefined") {
-        var _divs = player.layoutDivs(_type);
-        _config.container = _divs[0];
-        _config.spacer = _divs[1];
-    }
-    
     IriSP._(_config).forEach(function(_value, _key) {
        _this[_key] = _value;
     });
@@ -52,28 +51,40 @@
     });
    
     /* Adding classes and html attributes */
-    console.log(this.container);
     this.$ = IriSP.jQuery('#' + this.container);
-    this.$.addClass("Ldt-TraceMe").addClass("Ldt-Widget").attr("widget-type", _type);
+    this.$.addClass("Ldt-TraceMe Ldt-Widget").attr("widget-type", _type);
     
     /* Does the widget require other widgets ? */
     if (typeof this.requires !== "undefined") {
         for (var _i = 0; _i < this.requires.length; _i++) {
             var _subconfig = this.requires[_i];
-            if (typeof IriSP[_subconfig.type] !== "undefined") {
-                _subconfig.container = IriSP._.uniqueId(this.container + '_' + _subconfig.type + '_');
-                this.$.append(IriSP.jQuery('<div>').attr("id",_subconfig.container));
-                console.log(this.$.html());
-                this[_subconfig.type] = new IriSP[_subconfig.type](player, _subconfig);
-            } else {
-                console.log("Error, Call to Undefined Widget Type : "+_subconfig.type);
-            }
+            _subconfig.container = IriSP._.uniqueId(this.container + '_' + _subconfig.type + '_');
+            this.$.append(IriSP.jQuery('<div>').attr("id",_subconfig.container));
+            this.player.loadWidget(_subconfig, function(_widget) {
+                _this[_subconfig.type.replace(/^./,function(_s){return _s.toLowerCase();})] = _widget
+            });
         }
     }
     
+    this.l10n = (typeof this.messages[IriSP.language] !== "undefined" ? this.messages[IriSP.language] : this.messages["en"]);
+    
 };
 
-IriSP.Widget.prototype.functionWrapper = function(_name) {
+IriSP.Widgets.Widget.prototype.defaults = {}
+
+IriSP.Widgets.Widget.prototype.template = '';
+
+IriSP.Widgets.Widget.prototype.messages = {"en":{}};
+
+IriSP.Widgets.Widget.prototype.templateToHtml = function(_template) {
+    return Mustache.to_html(_template, this);
+}
+
+IriSP.Widgets.Widget.prototype.renderTemplate = function() {
+    this.$.append(this.templateToHtml(this.template));
+}
+
+IriSP.Widgets.Widget.prototype.functionWrapper = function(_name) {
     var _this = this,
         _function = this[_name];
     if (typeof _function !== "undefined") {
@@ -85,19 +96,19 @@
     }
 }
 
-IriSP.Widget.prototype.bindPopcorn = function(_popcornEvent, _functionName) {
+IriSP.Widgets.Widget.prototype.bindPopcorn = function(_popcornEvent, _functionName) {
     this.player.popcorn.listen(_popcornEvent, this.functionWrapper(_functionName))
 }
 
 /**
  * This method responsible of drawing a widget on screen.
  */
-IriSP.Widget.prototype.draw = function() {
+IriSP.Widgets.Widget.prototype.draw = function() {
     /* implemented by "sub-classes" */
 };
 /**
  * Optional method if you want your widget to support redraws.
  */
-IriSP.Widget.prototype.redraw = function() {
+IriSP.Widgets.Widget.prototype.redraw = function() {
     /* implemented by "sub-classes" */
 };