src/js/init.js
changeset 998 9521347ede1d
parent 987 7b65bf78873a
child 1000 91023037f841
equal deleted inserted replaced
997:1615c7e4ef9d 998:9521347ede1d
     1 /* init.js - initialization and configuration of the widgets
     1 /* Initialization of the namespace */
     2 */
       
     3 
     2 
     4 if (typeof window.IriSP === "undefined") {
     3 if (typeof window.IriSP === "undefined") {
     5     window.IriSP = {};
     4     window.IriSP = {};
     6 }
     5 }
     7 
     6 
     8 /* The Metadataplayer Object, single point of entry, replaces IriSP.init_player */
     7 if (typeof IriSP.jQuery === "undefined" && typeof window.jQuery !== "undefined") {
     9 
     8     IriSP.jQuery = window.jQuery;
    10 IriSP.Metadataplayer = function(config) {
       
    11     IriSP.log("IriSP.Metadataplayer constructor");
       
    12     for (var key in IriSP.guiDefaults) {
       
    13         if (IriSP.guiDefaults.hasOwnProperty(key) && !config.hasOwnProperty(key)) {
       
    14             config[key] = IriSP.guiDefaults[key]
       
    15         }
       
    16     }
       
    17     var _container = document.getElementById(config.container);
       
    18     _container.innerHTML = '<h3 class="Ldt-Loader">Loading... Chargement...</h3>';
       
    19     this.sourceManager = new IriSP.Model.Directory();
       
    20     this.config = config;
       
    21     this.__events = {};
       
    22     this.loadLibs();
       
    23 }
     9 }
    24 
    10 
    25 IriSP.Metadataplayer.prototype.toString = function() {
    11 if (typeof IriSP._ === "undefined" && typeof window._ !== "undefined") {
    26     return 'Metadataplayer in #' + this.config.container;
    12     IriSP._ = window._;
    27 }
    13 }
    28 
       
    29 IriSP.Metadataplayer.prototype.on = function(_event, _callback) {
       
    30     if (typeof this.__events[_event] === "undefined") {
       
    31         this.__events[_event] = [];
       
    32     }
       
    33     this.__events[_event].push(_callback);
       
    34 }
       
    35 
       
    36 IriSP.Metadataplayer.prototype.trigger = function(_event, _data) {
       
    37     var _element = this;
       
    38     IriSP._(this.__events[_event]).each(function(_callback) {
       
    39         _callback.call(_element, _data);
       
    40     });
       
    41 }
       
    42 
       
    43 IriSP.Metadataplayer.prototype.loadLibs = function() {
       
    44     IriSP.log("IriSP.Metadataplayer.prototype.loadLibs");
       
    45     var $L = $LAB
       
    46         .script(IriSP.getLib("underscore"))
       
    47         .script(IriSP.getLib("Mustache"))
       
    48         .script(IriSP.getLib("jQuery"));
       
    49     
       
    50     if (typeof JSON == "undefined") {
       
    51         $L.script(IriSP.getLib("json"));
       
    52     }
       
    53     
       
    54     $L.wait()
       
    55         .script(IriSP.getLib("jQueryUI"));
       
    56 
       
    57     /* widget specific requirements */
       
    58     for(var _i = 0; _i < this.config.widgets.length; _i++) {
       
    59         var _t = this.config.widgets[_i].type;
       
    60         if (typeof IriSP.widgetsRequirements[_t] !== "undefined" && typeof IriSP.widgetsRequirements[_t].requires !== "undefined" ) {
       
    61             for (var _j = 0; _j < IriSP.widgetsRequirements[_t].requires.length; _j++) {
       
    62                 $L.script(IriSP.getLib(IriSP.widgetsRequirements[_t].requires[_j]));
       
    63             }
       
    64         }
       
    65     }
       
    66     
       
    67     var _this = this;
       
    68     
       
    69     $L.wait(function() {
       
    70         _this.onLibsLoaded();
       
    71     });
       
    72 }
       
    73 
       
    74 IriSP.Metadataplayer.prototype.onLibsLoaded = function() {
       
    75     IriSP.log("IriSP.Metadataplayer.prototype.onLibsLoaded");
       
    76     if (typeof IriSP.jQuery === "undefined" && typeof window.jQuery !== "undefined") {
       
    77         IriSP.jQuery = window.jQuery;
       
    78     }
       
    79     if (typeof IriSP._ === "undefined" && typeof window._ !== "undefined") {
       
    80         IriSP._ = window._;
       
    81     }
       
    82     IriSP.loadCss(IriSP.getLib("cssjQueryUI"));
       
    83     IriSP.loadCss(this.config.css);
       
    84     
       
    85     this.$ = IriSP.jQuery('#' + this.config.container);
       
    86     this.$.css({
       
    87         "width": this.config.width,
       
    88         "clear": "both"
       
    89     });
       
    90     if (typeof this.config.height !== "undefined") {
       
    91         this.$.css("height", this.config.height);
       
    92     }
       
    93       
       
    94     this.widgets = [];
       
    95     var _this = this;
       
    96     IriSP._(this.config.widgets).each(function(widgetconf, key) {
       
    97         _this.widgets.push(null);
       
    98         _this.loadWidget(widgetconf, function(widget) {
       
    99             _this.widgets[key] = widget;
       
   100             if (widget.isLoaded()) {
       
   101                 _this.trigger("widget-loaded");
       
   102             }
       
   103         });
       
   104     });
       
   105     this.$.find('.Ldt-Loader').detach();
       
   106     
       
   107     this.widgetsLoaded = false;
       
   108     
       
   109     this.on("widget-loaded", function() {
       
   110         if (_this.widgetsLoaded) {
       
   111             return;
       
   112         }
       
   113         var isloaded = !IriSP._(_this.widgets).any(function(w) {
       
   114             return !(w && w.isLoaded())
       
   115         });
       
   116         if (isloaded) {
       
   117             _this.widgetsLoaded = true;
       
   118             _this.trigger("widgets-loaded");
       
   119         }
       
   120     });   
       
   121 }
       
   122 
       
   123 IriSP.Metadataplayer.prototype.loadMetadata = function(_metadataInfo) {
       
   124     if (_metadataInfo.elementType === "source") {
       
   125         return _metadataInfo;
       
   126     }
       
   127     if (typeof _metadataInfo.serializer === "undefined" && typeof _metadataInfo.format !== "undefined") {
       
   128         _metadataInfo.serializer = IriSP.serializers[_metadataInfo.format];
       
   129     }
       
   130     if (typeof _metadataInfo.url !== "undefined" && typeof _metadataInfo.serializer !== "undefined") {
       
   131         return this.sourceManager.remoteSource(_metadataInfo);
       
   132     } else {
       
   133         return this.sourceManager.newLocalSource(_metadataInfo);
       
   134     }
       
   135 }
       
   136 
       
   137 IriSP.Metadataplayer.prototype.loadWidget = function(_widgetConfig, _callback) {
       
   138     /* Creating containers if needed */
       
   139     if (typeof _widgetConfig.container === "undefined") {
       
   140         var _divs = this.layoutDivs(_widgetConfig.type);
       
   141         _widgetConfig.container = _divs[0];
       
   142     }
       
   143     
       
   144     var _this = this;
       
   145     
       
   146     if (typeof IriSP.Widgets[_widgetConfig.type] !== "undefined") {
       
   147         IriSP._.defer(function() {
       
   148             _callback(new IriSP.Widgets[_widgetConfig.type](_this, _widgetConfig));
       
   149         });
       
   150     } else {
       
   151         /* Loading Widget CSS */
       
   152         if (typeof IriSP.widgetsRequirements[_widgetConfig.type] === "undefined" || typeof IriSP.widgetsRequirements[_widgetConfig.type].noCss === "undefined" || !IriSP.widgetsRequirements[_widgetConfig.type].noCss) {
       
   153             IriSP.loadCss(IriSP.widgetsDir + '/' + _widgetConfig.type + '.css');
       
   154         }
       
   155         /* Loading Widget JS    */
       
   156         $LAB.script(IriSP.widgetsDir + '/' + _widgetConfig.type + '.js').wait(function() {
       
   157             _callback(new IriSP.Widgets[_widgetConfig.type](_this, _widgetConfig));
       
   158         });
       
   159     }
       
   160 }
       
   161 
       
   162 /** create a subdiv with an unique id, and a spacer div as well.
       
   163     @param widgetName the name of the widget.
       
   164     @return an array of the form [createdivId, spacerdivId].
       
   165 */
       
   166 IriSP.Metadataplayer.prototype.layoutDivs = function(_name, _height) {
       
   167     if (typeof(_name) === "undefined") {
       
   168        _name = "";
       
   169     }
       
   170     var newDiv = IriSP._.uniqueId(this.config.container + "_widget_" + _name + "_"),
       
   171         spacerDiv = IriSP._.uniqueId("LdtPlayer_spacer_"),
       
   172         divHtml = IriSP.jQuery('<div>')
       
   173             .attr("id",newDiv)
       
   174             .css({
       
   175                 width: this.config.width + "px",
       
   176                 position: "relative",
       
   177                 clear: "both"
       
   178             }),
       
   179         spacerHtml = IriSP.jQuery('<div>')
       
   180             .attr("id",spacerDiv)
       
   181             .css({
       
   182                 width: this.config.width + "px",
       
   183                 height: this.config.spacer_div_height + "px",
       
   184                 position: "relative",
       
   185                 clear: "both"
       
   186             });
       
   187     if (typeof _height !== "undefined") {
       
   188         divHtml.css("height", _height);
       
   189     }
       
   190             
       
   191     this.$.append(divHtml);
       
   192     this.$.append(spacerHtml);
       
   193 
       
   194     return [newDiv, spacerDiv];
       
   195 };