src/js/widgets.js
branchnew-model
changeset 875 43629caa77bc
parent 874 38b65761a7d5
child 876 03967b6ada7c
equal deleted inserted replaced
874:38b65761a7d5 875:43629caa77bc
     1 /* the widget classes and definitions */
     1 /* Definition of an ancestor for the Widget classes */
       
     2 
       
     3 if (typeof IriSP.Widgets === "undefined") {
       
     4     IriSP.Widgets = {}
       
     5 }
     2 
     6 
     3 /**
     7 /**
     4  * @class IriSP.Widget is an "abstract" class. It's mostly used to define some properties common to every widget.
     8  * @class IriSP.Widget is an "abstract" class. It's mostly used to define some properties common to every widget.
     5  *
     9  *
     6  *  Note that widget constructors are never called directly by the user. Instead, the widgets are instantiated by functions
    10  *  Note that widget constructors are never called directly by the user. Instead, the widgets are instantiated by functions
     8  *
    12  *
     9  * @constructor
    13  * @constructor
    10  * @param player - a reference to the player widget
    14  * @param player - a reference to the player widget
    11  * @param config - configuration options for the widget
    15  * @param config - configuration options for the widget
    12  */
    16  */
    13 IriSP.Widget = function(player, config) {
    17 
       
    18 
       
    19 IriSP.Widgets.Widget = function(player, config) {
    14 
    20 
    15     if( typeof player === "undefined") {
    21     if( typeof player === "undefined") {
    16         /* Probably an abstract call of the class when
    22         /* Probably an abstract call of the class when
    17          * individual widgets set their prototype */
    23          * individual widgets set their prototype */
    18         return;
    24         return;
    19     }
    25     }
    20     
    26     
    21     /* Setting all the configuration options */
    27     /* Setting all the configuration options */
    22     var _type = config.type,
    28     var _type = config.type,
    23         _config = IriSP._.defaults({}, config, player.config.gui.default_options, IriSP.widgetsDefaults[_type]),
    29         _config = IriSP._.defaults({}, config, player.config.gui.default_options, this.defaults),
    24         _this = this;
    30         _this = this;
    25     
       
    26     /* Creating containers if needed */
       
    27     if (typeof _config.container === "undefined") {
       
    28         var _divs = player.layoutDivs(_type);
       
    29         _config.container = _divs[0];
       
    30         _config.spacer = _divs[1];
       
    31     }
       
    32     
    31     
    33     IriSP._(_config).forEach(function(_value, _key) {
    32     IriSP._(_config).forEach(function(_value, _key) {
    34        _this[_key] = _value;
    33        _this[_key] = _value;
    35     });
    34     });
    36     
    35     
    50     this.source.onLoad(function() {
    49     this.source.onLoad(function() {
    51         _this.draw();
    50         _this.draw();
    52     });
    51     });
    53    
    52    
    54     /* Adding classes and html attributes */
    53     /* Adding classes and html attributes */
    55     console.log(this.container);
       
    56     this.$ = IriSP.jQuery('#' + this.container);
    54     this.$ = IriSP.jQuery('#' + this.container);
    57     this.$.addClass("Ldt-TraceMe").addClass("Ldt-Widget").attr("widget-type", _type);
    55     this.$.addClass("Ldt-TraceMe Ldt-Widget").attr("widget-type", _type);
    58     
    56     
    59     /* Does the widget require other widgets ? */
    57     /* Does the widget require other widgets ? */
    60     if (typeof this.requires !== "undefined") {
    58     if (typeof this.requires !== "undefined") {
    61         for (var _i = 0; _i < this.requires.length; _i++) {
    59         for (var _i = 0; _i < this.requires.length; _i++) {
    62             var _subconfig = this.requires[_i];
    60             var _subconfig = this.requires[_i];
    63             if (typeof IriSP[_subconfig.type] !== "undefined") {
    61             _subconfig.container = IriSP._.uniqueId(this.container + '_' + _subconfig.type + '_');
    64                 _subconfig.container = IriSP._.uniqueId(this.container + '_' + _subconfig.type + '_');
    62             this.$.append(IriSP.jQuery('<div>').attr("id",_subconfig.container));
    65                 this.$.append(IriSP.jQuery('<div>').attr("id",_subconfig.container));
    63             this.player.loadWidget(_subconfig, function(_widget) {
    66                 console.log(this.$.html());
    64                 _this[_subconfig.type.replace(/^./,function(_s){return _s.toLowerCase();})] = _widget
    67                 this[_subconfig.type] = new IriSP[_subconfig.type](player, _subconfig);
    65             });
    68             } else {
       
    69                 console.log("Error, Call to Undefined Widget Type : "+_subconfig.type);
       
    70             }
       
    71         }
    66         }
    72     }
    67     }
    73     
    68     
       
    69     this.l10n = (typeof this.messages[IriSP.language] !== "undefined" ? this.messages[IriSP.language] : this.messages["en"]);
       
    70     
    74 };
    71 };
    75 
    72 
    76 IriSP.Widget.prototype.functionWrapper = function(_name) {
    73 IriSP.Widgets.Widget.prototype.defaults = {}
       
    74 
       
    75 IriSP.Widgets.Widget.prototype.template = '';
       
    76 
       
    77 IriSP.Widgets.Widget.prototype.messages = {"en":{}};
       
    78 
       
    79 IriSP.Widgets.Widget.prototype.templateToHtml = function(_template) {
       
    80     return Mustache.to_html(_template, this);
       
    81 }
       
    82 
       
    83 IriSP.Widgets.Widget.prototype.renderTemplate = function() {
       
    84     this.$.append(this.templateToHtml(this.template));
       
    85 }
       
    86 
       
    87 IriSP.Widgets.Widget.prototype.functionWrapper = function(_name) {
    77     var _this = this,
    88     var _this = this,
    78         _function = this[_name];
    89         _function = this[_name];
    79     if (typeof _function !== "undefined") {
    90     if (typeof _function !== "undefined") {
    80         return function() {
    91         return function() {
    81             return _function.apply(_this, Array.prototype.slice.call(arguments, 0));
    92             return _function.apply(_this, Array.prototype.slice.call(arguments, 0));
    83     } else {
    94     } else {
    84         console.log("Error, Unknown function IriSP." + this.type + "." + _name)
    95         console.log("Error, Unknown function IriSP." + this.type + "." + _name)
    85     }
    96     }
    86 }
    97 }
    87 
    98 
    88 IriSP.Widget.prototype.bindPopcorn = function(_popcornEvent, _functionName) {
    99 IriSP.Widgets.Widget.prototype.bindPopcorn = function(_popcornEvent, _functionName) {
    89     this.player.popcorn.listen(_popcornEvent, this.functionWrapper(_functionName))
   100     this.player.popcorn.listen(_popcornEvent, this.functionWrapper(_functionName))
    90 }
   101 }
    91 
   102 
    92 /**
   103 /**
    93  * This method responsible of drawing a widget on screen.
   104  * This method responsible of drawing a widget on screen.
    94  */
   105  */
    95 IriSP.Widget.prototype.draw = function() {
   106 IriSP.Widgets.Widget.prototype.draw = function() {
    96     /* implemented by "sub-classes" */
   107     /* implemented by "sub-classes" */
    97 };
   108 };
    98 /**
   109 /**
    99  * Optional method if you want your widget to support redraws.
   110  * Optional method if you want your widget to support redraws.
   100  */
   111  */
   101 IriSP.Widget.prototype.redraw = function() {
   112 IriSP.Widgets.Widget.prototype.redraw = function() {
   102     /* implemented by "sub-classes" */
   113     /* implemented by "sub-classes" */
   103 };
   114 };