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