src/js/widgets.js
changeset 998 9521347ede1d
parent 997 1615c7e4ef9d
child 999 7130fcc8ce9c
equal deleted inserted replaced
997:1615c7e4ef9d 998:9521347ede1d
     1 /* Definition of an ancestor for the Widget classes */
       
     2 
       
     3 if (typeof IriSP.Widgets === "undefined") {
       
     4     IriSP.Widgets = {}
       
     5 }
       
     6 
       
     7 /**
       
     8  * @class IriSP.Widget is an "abstract" class. It's mostly used to define some properties common to every widget.
       
     9  *
       
    10  *  Note that widget constructors are never called directly by the user. Instead, the widgets are instantiated by functions
       
    11  *  defined in init.js
       
    12  *
       
    13  * @constructor
       
    14  * @param player - a reference to the player widget
       
    15  * @param config - configuration options for the widget
       
    16  */
       
    17 
       
    18 
       
    19 IriSP.Widgets.Widget = function(player, config) {
       
    20     
       
    21     if( typeof player === "undefined") {
       
    22         /* Probably an abstract call of the class when
       
    23          * individual widgets set their prototype */
       
    24         return;
       
    25     }
       
    26     
       
    27     this.__subwidgets = [];
       
    28     
       
    29     /* Setting all the configuration options */
       
    30     var _type = config.type,
       
    31         _config = IriSP._.defaults({}, config, player.config.default_options, this.defaults),
       
    32         _this = this;
       
    33     
       
    34     IriSP._(_config).forEach(function(_value, _key) {
       
    35        _this[_key] = _value;
       
    36     });
       
    37     
       
    38     this.$ = IriSP.jQuery('#' + this.container);
       
    39     
       
    40     if (typeof this.width === "undefined") {
       
    41         this.width = this.$.width();
       
    42     } else {
       
    43         this.$.css("width", this.width);
       
    44     }
       
    45     
       
    46     if (typeof this.height !== "undefined") {
       
    47         this.$.css("height", this.height);
       
    48     }
       
    49     
       
    50     /* Setting this.player at the end in case it's been overriden
       
    51      * by a configuration option of the same name :-(
       
    52      */
       
    53     this.player = player;
       
    54     
       
    55     /* Adding classes and html attributes */
       
    56     this.$.addClass("Ldt-TraceMe Ldt-Widget").attr("widget-type", _type);
       
    57     
       
    58     this.l10n = (
       
    59         typeof this.messages[IriSP.language] !== "undefined"
       
    60         ? this.messages[IriSP.language]
       
    61         : (
       
    62             IriSP.language.length > 2 && typeof this.messages[IriSP.language.substr(0,2)] !== "undefined"
       
    63             ? this.messages[IriSP.language.substr(0,2)]
       
    64             : this.messages["en"]
       
    65         )
       
    66     );
       
    67     
       
    68     /* Loading Metadata if required */
       
    69     
       
    70     if (this.metadata) {
       
    71         /* Getting metadata */
       
    72         this.source = player.loadMetadata(this.metadata);
       
    73         
       
    74         /* Call draw when loaded */
       
    75         this.source.onLoad(function() {
       
    76             if (_this.media_id) {
       
    77                 _this.media = this.getElement(_this.media_id);
       
    78             } else {
       
    79                 var _mediaopts = {
       
    80                     is_mashup: _this.is_mashup || false
       
    81                 }
       
    82                 _this.media = this.getCurrentMedia(_mediaopts);
       
    83             }
       
    84             
       
    85             _this.draw();
       
    86             player.trigger("widget-loaded");
       
    87         });
       
    88     } else {
       
    89         this.draw();
       
    90     }
       
    91     
       
    92     
       
    93 };
       
    94 
       
    95 IriSP.Widgets.Widget.prototype.defaults = {}
       
    96 
       
    97 IriSP.Widgets.Widget.prototype.template = '';
       
    98 
       
    99 IriSP.Widgets.Widget.prototype.messages = {"en":{}};
       
   100 
       
   101 IriSP.Widgets.Widget.prototype.templateToHtml = function(_template) {
       
   102     return Mustache.to_html(_template, this);
       
   103 }
       
   104 
       
   105 IriSP.Widgets.Widget.prototype.renderTemplate = function() {
       
   106     this.$.append(this.templateToHtml(this.template));
       
   107 }
       
   108 
       
   109 IriSP.Widgets.Widget.prototype.functionWrapper = function(_name) {
       
   110     var _this = this,
       
   111         _function = this[_name];
       
   112     if (typeof _function !== "undefined") {
       
   113         return function() {
       
   114             return _function.apply(_this, Array.prototype.slice.call(arguments, 0));
       
   115         }
       
   116     } else {
       
   117         console.log("Error, Unknown function IriSP.Widgets." + this.type + "." + _name)
       
   118     }
       
   119 }
       
   120 
       
   121 IriSP.Widgets.Widget.prototype.getFunctionOrName = function(_functionOrName) {
       
   122     switch (typeof _functionOrName) {
       
   123         case "function":
       
   124             return _functionOrName;
       
   125         case "string":
       
   126             return this.functionWrapper(_functionOrName);
       
   127         default:
       
   128             return undefined;
       
   129     }
       
   130 }
       
   131 
       
   132 IriSP.Widgets.Widget.prototype.onMdpEvent = function(_eventName, _functionOrName) {
       
   133     this.player.on(_eventName, this.getFunctionOrName(_functionOrName));
       
   134 }
       
   135 
       
   136 IriSP.Widgets.Widget.prototype.onMediaEvent = function(_eventName, _functionOrName) {
       
   137     this.media.on(_eventName, this.getFunctionOrName(_functionOrName));
       
   138 }
       
   139 
       
   140 IriSP.Widgets.Widget.prototype.getWidgetAnnotations = function() {
       
   141     if (typeof this.annotation_type === "undefined") {
       
   142         return this.media.getAnnotations();
       
   143     }
       
   144     if (this.annotation_type.elementType === "annotationType") {
       
   145         return this.annotation_type.getAnnotations();
       
   146     }
       
   147     return this.media.getAnnotationsByTypeTitle(this.annotation_type);
       
   148 }
       
   149 
       
   150 IriSP.Widgets.Widget.prototype.getWidgetAnnotationsAtTime = function() {
       
   151     var _time = this.media.getCurrentTime();
       
   152     return this.getWidgetAnnotations().filter(function(_annotation) {
       
   153         return _annotation.begin <= _time && _annotation.end > _time;
       
   154     });
       
   155 }
       
   156 
       
   157 IriSP.Widgets.Widget.prototype.isLoaded = function() {
       
   158     var isloaded = !IriSP._(this.__subwidgets).any(function(w) {
       
   159         return !(w && w.isLoaded());
       
   160     });
       
   161     return isloaded;
       
   162 }
       
   163 
       
   164 IriSP.Widgets.Widget.prototype.insertSubwidget = function(_selector, _widgetoptions, _propname) {
       
   165     var _id = _selector.attr("id"),
       
   166         _this = this,
       
   167         _type = _widgetoptions.type,
       
   168         $L = $LAB,
       
   169         key = this.__subwidgets.length;
       
   170     this.__subwidgets.push(null);
       
   171     if (typeof _id == "undefined") {
       
   172         _id = IriSP._.uniqueId(this.container + '_sub_widget_' + _widgetoptions.type);
       
   173         _selector.attr("id", _id);
       
   174     }
       
   175     _widgetoptions.container = _id;
       
   176     if (typeof IriSP.widgetsRequirements[_type] !== "undefined" && typeof IriSP.widgetsRequirements[_type].requires !== "undefined" ) {
       
   177         for (var _j = 0; _j < IriSP.widgetsRequirements[_type].requires.length; _j++) {
       
   178             $L.script(IriSP.getLib(IriSP.widgetsRequirements[_type].requires[_j]));
       
   179         }
       
   180     }
       
   181     $L.wait(function() {
       
   182         _this.player.loadWidget(_widgetoptions, function(_widget) {
       
   183             if (_propname) {
       
   184                 _this[_propname] = _widget;
       
   185             }
       
   186             _this.__subwidgets[key] = _widget;
       
   187         });
       
   188     });
       
   189 }
       
   190 
       
   191 /**
       
   192  * This method responsible of drawing a widget on screen.
       
   193  */
       
   194 IriSP.Widgets.Widget.prototype.draw = function() {
       
   195     /* implemented by "sub-classes" */
       
   196 };