src/widgets/MultiSegments.js
author durandn
Tue, 01 Sep 2015 15:31:46 +0200
changeset 1046 eb77616c245f
parent 1033 c20df1c080e6
child 1068 7623f9af9272
permissions -rw-r--r--
Updated LatestAnnotation, CurrentSegmentInfobox, AnnotationController and AnnotationList widgets to use timerange to find the current segment when the corresponding option is used + CurrentSegmentInfobox now has an option for editing the current segment + LatestAnnotation now has a "Copy and edit" button that allows to use the text from the currently displayed annotation into the CreateAnnotation widget textarea

IriSP.Widgets.MultiSegments = function(player, config) {
    IriSP.Widgets.Widget.call(this, player, config);
};

IriSP.Widgets.MultiSegments.prototype = new IriSP.Widgets.Widget();

IriSP.Widgets.MultiSegments.prototype.defaults = {
    annotation_show_arrow: true,
    annotation_start_minimized: false,
    annotation_show_annotation_type: true,
    show_all: false
};

IriSP.Widgets.MultiSegments.prototype.draw = function() {
    var _this = this,
        lines = [],
        currentLine = null,
        segmentsopts = {},
        annotationopts = {};
    IriSP._(this).each(function(_v,_k) {
        if (/^segments_/.test(_k)) {
            segmentsopts[_k.replace(/^segments_/,"")] = _v;
        }
        if (/^annotation_/.test(_k)) {
            annotationopts[_k.replace(/^annotation_/,"")] = _v;
        }
    });
    this.source.getAnnotationTypes().forEach(function(_anntype) {
        var segments = _anntype.getAnnotations().filter(function(_ann) {
            return _ann.getDuration() > 0;
        });
        if (segments.length) {
            
            var visible = false;
            
            var line = {
                segmentWidget: IriSP.jQuery("<div>"),
                annotationWidget: IriSP.jQuery("<div>"),
                hasSegmentsNow: function() {
                    var _time = _this.media.getCurrentTime();
                    return !!segments.filter(function(_annotation) {
                        return _annotation.begin <= _time && _annotation.end > _time;
                    }).length;
                },
                hide: function() {
                    if (visible) {
                        visible = false;
                        this.annotationWidget.slideUp();
                    }
                },
                show: function() {
                    if (!visible) {
                        visible = true;
                        this.annotationWidget.slideDown();
                    }
                }
            };
                
                
            line.segmentWidget
                .addClass("Ldt-MultiSegments-Segment")
                .appendTo(_this.$);
                
            if (!_this.show_all) {
                line.segmentWidget.mouseenter(function() {
                    if (line.hasSegmentsNow()) {
                        currentLine = line;
                        checkVisibilities();
                    }
                });
            }
            
            line.annotationWidget
                .addClass("Ldt-MultiSegments-Annotation")
                .appendTo(_this.$)
                .hide();
                
            _this.insertSubwidget(
                line.segmentWidget,
                IriSP._({
                    type: "Segments",
                    annotation_type: _anntype,
                    width: _this.width
                }).extend(segmentsopts)
            );
            
            _this.insertSubwidget(
                line.annotationWidget,
                IriSP._({
                    type: "Annotation",
                    annotation_type: _anntype,
                    width: _this.width
                }).extend(annotationopts)
            );
            
            lines.push(line);
        }
    });
    
    // open line on segment click
    IriSP.jQuery(document).on("click",".Ldt-Segments-Segment",function(e){
    	if (!_this.show_all && currentLine && !currentLine.hasSegmentsNow()) {
            currentLine = undefined;
        }
        IriSP._(lines).each(function(line) {
        	if(IriSP.jQuery(e.target).parent().parent()[0]==line.segmentWidget[0]){
        		currentLine = line;
        		line.show();
        	} else {
                line.hide();
            }
        });
    });
    
    //var _annotationWidgets = _this.$.find(".Ldt-MultiSegments-Annotation");
    
    function checkVisibilities(_time) {
        /*if (!_this.show_all && currentLine && !currentLine.hasSegmentsNow()) {
            currentLine = undefined;
        }
        IriSP._(lines).each(function(line) {
            if (line.hasSegmentsNow()) {
                if (!_this.show_all && !currentLine) {
                    currentLine = line;
                }
                if (_this.show_all || line === currentLine) {
                    line.show();
                } else {
                    line.hide();
                }
            } else {
                line.hide();
            }
        });*/
    }
};