src/widgets/Slideshare.js
author ymh <ymh.work@gmail.com>
Fri, 18 Oct 2024 10:24:57 +0200
changeset 1074 231ea5ea7de4
parent 1072 ac1eacb3aa33
permissions -rw-r--r--
change http to https for default thumb

/* TODO: Add Slide synchronization */
import slideshareStyles from "./Slideshare.module.css";
import jQuery from "jquery";

const Slideshare = function(ns) {
    return class extends ns.Widgets.Widget {
        constructor(player, config) {
    super(player, config);
};

static defaults = {
    annotation_type: "slide",
    sync: true,
};

static messages =  {
    fr: {
        slides_ : "Diapositives"
    },
    en: {
        slides_ : "Slides"
    }
};

static template =
    '<div class="Ldt-SlideShare"><h2>{{l10n.slides_}}</h2><hr /><div class="Ldt-SlideShare-Container"></div></div>';

draw() {
    
    function insertSlideshare(_presentation, _slide) {
        if (_lastEmbedded === _presentation) {
            if (_embedObject && typeof _embedObject.jumpTo === "function") {
                _embedObject.jumpTo(parseInt(_slide));
            }
        } else {
            _lastEmbedded = _presentation;
            var _id = ns.Model.getUID(),
                _params = {
                    allowScriptAccess: "always"
                },
                _atts = {
                    id: _id
                },
                _flashvars = {
                    doc : _presentation,
                    startSlide : _slide
                };
            $container.html('<div id="' + _id + '"></div>');
            swfobject.embedSWF(
                "http://static.slidesharecdn.com/swf/ssplayer2.swf",
                _id,
                _this.embed_width,
                _this.embed_height,
                "8",
                null,
                _flashvars,
                _params,
                _atts
            );
            _embedObject = document.getElementById(_id);
        }
        $container.show();
    }
    
    var _annotations = this.getWidgetAnnotations();
    if (!_annotations.length) {
        this.$.hide();
    } else {
        this.renderTemplate();
        var _lastPres = "",
            _embedObject = null,
            _oembedCache = {},
            _lastEmbedded = "",
            _this = this,
            $container = this.$.find(".Ldt-SlideShare-Container");
            
        this.embed_width = this.embed_width || $container.innerWidth();
        this.embed_height = this.embed_height || Math.floor(this.embed_width * 3/4);
        
        _annotations.forEach(function(_a) {
            _a.on("leave", function() {
                $container.hide();
                _lastPres = "";
            });
            _a.on("enter", function() {
                var _description = _a.description,
                    _isurl = /^https?:\/\//.test(_description),
                    _presentation = _description.replace(/#.*$/,''),
                    _slidematch = _description.match(/(#|\?|&)id=(\d+)/),
                    _slide = parseInt(_slidematch && _slidematch.length > 2 ? _slidematch[2] : 1);
                if (_presentation !== _lastPres) {
                    if (_isurl) {
                        if (typeof _oembedCache[_presentation] === "undefined") {
                            var _ajaxUrl = "http://www.slideshare.net/api/oembed/1?url="
                                + encodeURIComponent(_presentation)
                                + "&format=jsonp&callback=?";
                            jQuery.getJSON(_ajaxUrl, function(_oembedData) {
                                var _presmatch = _oembedData.html.match(/doc=([a-z0-9\-_%]+)/i);
                                if (_presmatch && _presmatch.length > 1) {
                                    _oembedCache[_presentation] =  _presmatch[1];
                                    insertSlideshare(_presmatch[1], _slide);
                                }
                            });
                        } else {
                            insertSlideshare(_oembedCache[_presentation], _slide);
                        }
                    } else {
                        insertSlideshare(_presentation, _slide);
                    }
                }
                if (_this.sync && _embedObject && typeof _embedObject.jumpTo === "function") {
                    _embedObject.jumpTo(parseInt(_slide));
                }
                _lastPres = _presentation;
                
            });
        });
    }
};
    }}

export { Slideshare, slideshareStyles };