src/widgets/Slideshare.js
branchnew-model
changeset 1020 198c2b79f5e1
parent 1019 3ab36f402b0c
equal deleted inserted replaced
1019:3ab36f402b0c 1020:198c2b79f5e1
     1 /* TODO: Add Slide synchronization */
     1 /* TODO: Add Slide synchronization */
     2 
     2 
     3 IriSP.Widgets.Slideshare = function(player, config) {
     3 IriSP.Widgets.Slideshare = function(player, config) {
     4     IriSP.Widgets.Widget.call(this, player, config);
     4     IriSP.Widgets.Widget.call(this, player, config);
     5 };
     5     this.lastSlide = {
       
     6         presentation: "",
       
     7         slide: 0
       
     8     }
       
     9     this.embedObject = null;
       
    10     this.oembedCache = {}
       
    11 }
     6 
    12 
     7 IriSP.Widgets.Slideshare.prototype = new IriSP.Widgets.Widget();
    13 IriSP.Widgets.Slideshare.prototype = new IriSP.Widgets.Widget();
     8 
    14 
     9 IriSP.Widgets.Slideshare.prototype.defaults = {
    15 IriSP.Widgets.Slideshare.prototype.defaults = {
    10     annotation_type: "slide",
    16     annotation_type: "slide",
    11     sync: true,
    17     sync: true,
    12 };
    18     embed_width: 400,
       
    19     embed_height: 300
       
    20 }
    13 
    21 
    14 IriSP.Widgets.Slideshare.prototype.messages = {
    22 IriSP.Widgets.Slideshare.prototype.messages = {
    15     fr: {
    23     fr: {
    16         slides_ : "Diapositives"
    24         slides_ : "Diapositives :"
    17     },
    25     },
    18     en: {
    26     en: {
    19         slides_ : "Slides"
    27         slides_ : "Slides:"
    20     }
    28     }
    21 };
    29 }
    22 
    30 
    23 IriSP.Widgets.Slideshare.prototype.template =
    31 IriSP.Widgets.Slideshare.prototype.template =
    24     '<div class="Ldt-SlideShare"><h2>{{l10n.slides_}}</h2><hr /><div class="Ldt-SlideShare-Container"></div></div>';
    32     '<div class="Ldt-SlideShare"><h2>{{l10n.slides_}}</h2><hr /><div class="Ldt-SlideShare-Container"></div></div>';
    25 
    33 
    26 IriSP.Widgets.Slideshare.prototype.draw = function() {
    34 IriSP.Widgets.Slideshare.prototype.draw = function() {
    27     
    35     var _hide = false;
    28     function insertSlideshare(_presentation, _slide) {
    36     if (typeof this.annotation_type !== "undefined" && this.annotation_type) {
    29         if (_lastEmbedded === _presentation) {
    37         var _annType = this.source.getAnnotationTypes().searchByTitle(this.annotation_type);
    30             if (_embedObject && typeof _embedObject.jumpTo === "function") {
    38         _hide = !_annType.length;
    31                 _embedObject.jumpTo(parseInt(_slide));
       
    32             }
       
    33         } else {
       
    34             _lastEmbedded = _presentation;
       
    35             var _id = IriSP.Model.getUID(),
       
    36                 _params = {
       
    37                     allowScriptAccess: "always"
       
    38                 },
       
    39                 _atts = {
       
    40                     id: _id
       
    41                 },
       
    42                 _flashvars = {
       
    43                     doc : _presentation,
       
    44                     startSlide : _slide
       
    45                 };
       
    46             $container.html('<div id="' + _id + '"></div>');
       
    47             swfobject.embedSWF(
       
    48                 "http://static.slidesharecdn.com/swf/ssplayer2.swf",
       
    49                 _id,
       
    50                 _this.embed_width,
       
    51                 _this.embed_height,
       
    52                 "8",
       
    53                 null,
       
    54                 _flashvars,
       
    55                 _params,
       
    56                 _atts
       
    57             );
       
    58             _embedObject = document.getElementById(_id);
       
    59         }
       
    60         $container.show();
       
    61     }
    39     }
    62     
    40     if (_hide) {
    63     var _annotations = this.getWidgetAnnotations();
       
    64     if (!_annotations.length) {
       
    65         this.$.hide();
    41         this.$.hide();
    66     } else {
    42     } else {
    67         this.renderTemplate();
    43         this.renderTemplate();
    68         var _lastPres = "",
    44         this.$container = this.$.find(".Ldt-SlideShare-Container");
    69             _embedObject = null,
    45         this.bindPopcorn("timeupdate","onTimeupdate");
    70             _oembedCache = {},
    46         this.onTimeupdate();
    71             _lastEmbedded = "",
    47     }
    72             _this = this,
    48 }
    73             $container = this.$.find(".Ldt-SlideShare-Container");
    49 
    74             
    50 IriSP.Widgets.Slideshare.prototype.onTimeupdate = function() {
    75         this.embed_width = this.embed_width || $container.innerWidth();
    51     var _list = this.getWidgetAnnotationsAtTime();
    76         this.embed_height = this.embed_height || Math.floor(this.embed_width * 3/4);
    52     if (_list.length) {
    77         
    53         var _description = _list[0].description,
    78         _annotations.forEach(function(_a) {
    54             _isurl = /^https?:\/\//.test(_description),
    79             _a.on("leave", function() {
    55             _presentation = _description.replace(/#.*$/,''),
    80                 $container.hide();
    56             _slidematch = _description.match(/(#|\?|&)id=(\d+)/),
    81                 _lastPres = "";
    57             _slide = parseInt(_slidematch && _slidematch.length > 2 ? _slidematch[2] : 1),
    82             });
    58             _this = this;
    83             _a.on("enter", function() {
    59         if (_presentation !== this.lastSlide.presentation) {
    84                 var _description = _a.description,
    60             if (_isurl) {
    85                     _isurl = /^https?:\/\//.test(_description),
    61                 if (typeof this.oembedCache[_presentation] === "undefined") {
    86                     _presentation = _description.replace(/#.*$/,''),
    62                     var _ajaxUrl = "http://www.slideshare.net/api/oembed/1?url="
    87                     _slidematch = _description.match(/(#|\?|&)id=(\d+)/),
    63                         + encodeURIComponent(_presentation)
    88                     _slide = parseInt(_slidematch && _slidematch.length > 2 ? _slidematch[2] : 1);
    64                         + "&format=jsonp&callback=?";
    89                 if (_presentation !== _lastPres) {
    65                     IriSP.jQuery.getJSON(_ajaxUrl, function(_oembedData) {
    90                     if (_isurl) {
    66                         var _presmatch = _oembedData.html.match(/doc=([a-z0-9\-_%]+)/i);
    91                         if (typeof _oembedCache[_presentation] === "undefined") {
    67                         if (_presmatch && _presmatch.length > 1) {
    92                             var _ajaxUrl = "http://www.slideshare.net/api/oembed/1?url="
    68                             _this.oembedCache[_presentation] =  _presmatch[1];
    93                                 + encodeURIComponent(_presentation)
    69                             _this.insertSlideshare(_presmatch[1], _slide);
    94                                 + "&format=jsonp&callback=?";
       
    95                             IriSP.jQuery.getJSON(_ajaxUrl, function(_oembedData) {
       
    96                                 var _presmatch = _oembedData.html.match(/doc=([a-z0-9\-_%]+)/i);
       
    97                                 if (_presmatch && _presmatch.length > 1) {
       
    98                                     _oembedCache[_presentation] =  _presmatch[1];
       
    99                                     insertSlideshare(_presmatch[1], _slide);
       
   100                                 }
       
   101                             });
       
   102                         } else {
       
   103                             insertSlideshare(_oembedCache[_presentation], _slide);
       
   104                         }
    70                         }
   105                     } else {
    71                     });
   106                         insertSlideshare(_presentation, _slide);
    72                 } else {
   107                     }
    73                     this.insertSlideshare(this.oembedCache[_presentation], _slide);
   108                 }
    74                 }
   109                 if (_this.sync && _embedObject && typeof _embedObject.jumpTo === "function") {
    75             } else {
   110                     _embedObject.jumpTo(parseInt(_slide));
    76                 this.insertSlideshare(_presentation, _slide);
   111                 }
    77             }
   112                 _lastPres = _presentation;
    78         }
   113                 
    79         if (_slide != this.lastSlide.slide && this.sync && this.embedObject && typeof this.embedObject.jumpTo === "function") {
   114             });
    80             this.embedObject.jumpTo(parseInt(_slide));
   115         });
    81         }
       
    82         this.lastSlide = {
       
    83             presentation: _presentation,
       
    84             slide: _slide
       
    85         }
       
    86     } else {
       
    87         if (this.lastSlide.presentation) {
       
    88             this.$container.hide();
       
    89             this.lastSlide = {
       
    90                 presentation: "",
       
    91                 slide: 0
       
    92             }
       
    93         }
   116     }
    94     }
   117 };
    95 }
       
    96 
       
    97 IriSP.Widgets.Slideshare.prototype.insertSlideshare = function(_presentation, _slide) {
       
    98     if (this.lastEmbedded === _presentation) {
       
    99         if (this.embedObject && typeof this.embedObject.jumpTo === "function") {
       
   100             this.embedObject.jumpTo(parseInt(_slide));
       
   101         }
       
   102     } else {
       
   103         this.lastEmbedded = _presentation;
       
   104         var _id = IriSP.Model.getUID(),
       
   105             _params = {
       
   106                 allowScriptAccess: "always"
       
   107             }
       
   108             _atts = {
       
   109                 id: _id
       
   110             },
       
   111             _flashvars = {
       
   112                 doc : _presentation,
       
   113                 startSlide : _slide
       
   114             };
       
   115         this.$container.html('<div id="' + _id + '"></div>');
       
   116         swfobject.embedSWF(
       
   117             "http://static.slidesharecdn.com/swf/ssplayer2.swf",
       
   118             _id,
       
   119             this.embed_width,
       
   120             this.embed_height,
       
   121             "8",
       
   122             null,
       
   123             _flashvars,
       
   124             _params,
       
   125             _atts
       
   126         );
       
   127         this.embedObject = document.getElementById(_id);
       
   128     }
       
   129     this.$container.show();
       
   130 }