import $ from 'jquery'; function addTranscriptFunctions(AblePlayer) { AblePlayer.prototype.setupTranscript = function () { var deferred = new this.defer(); var promise = deferred.promise(); if (this.usingYouTubeCaptions || this.usingVimeoCaptions || this.hideTranscriptButton ) { // a transcript is not possible or is disabled. this.transcriptType = null; deferred.resolve(); } else { if (!this.transcriptType) { // previously set transcriptType to null since there are no elements // check again to see if captions have been collected from other sources (e.g., YouTube) if (this.captions.length) { // captions are possible! Use the default type (popup) // if other types ('external' and 'manual') were desired, transcriptType would not be null here this.transcriptType = "popup"; } } if (this.transcriptType) { if ( this.transcriptType === "popup" || this.transcriptType === "external" ) { this.injectTranscriptArea(); deferred.resolve(); } else if (this.transcriptType === "manual") { this.setupManualTranscript(); deferred.resolve(); } } else { // there is no transcript deferred.resolve(); } } return promise; }; AblePlayer.prototype.injectTranscriptArea = function () { var autoScrollField, $languageSelectWrapper, languageSelectField, i, $option; this.$transcriptArea = $("
", { class: "able-transcript-area", role: "dialog", "aria-label": this.translate( 'transcriptTitle', 'Transcript' ), }); this.$transcriptToolbar = $("
", { class: "able-window-toolbar able-" + this.toolbarIconColor + "-controls", }); this.$transcriptDiv = $("
", { class: "able-transcript", id: "transcript-" + this.mediaId }); // Transcript toolbar content // Add auto Scroll checkbox autoScrollField = this.createField({ wrapperClass: 'autoscroll-transcript', id: 'autoscroll-transcript-checkbox-' + this.mediaId, label: this.translate( 'autoScroll', 'Auto scroll' ), type: 'checkbox', labelPosition: 'before' }); this.$autoScrollTranscriptCheckbox = autoScrollField.field; this.$transcriptToolbar.append( autoScrollField.wrapper ); // Add field for selecting a transcript language // Only necessary if there is more than one language if (this.captions.length > 1) { languageSelectField = this.createField({ wrapperClass: 'transcript-language-select-wrapper', id: 'transcript-language-select-' + this.mediaId, label: this.translate( 'language', 'Language' ), type: 'select', labelPosition: 'before' }); $languageSelectWrapper = languageSelectField.wrapper; this.$transcriptLanguageSelect = languageSelectField.field; for (i = 0; i < this.captions.length; i++) { $option = $("", { value: this.captions[i]["language"], lang: this.captions[i]["language"], }).text(this.captions[i]["label"]); if (this.captions[i]["def"]) { $option.prop("selected", true); } this.$transcriptLanguageSelect.append($option); } } if ($languageSelectWrapper) { this.$transcriptToolbar.append($languageSelectWrapper); } this.$transcriptArea.append(this.$transcriptToolbar, this.$transcriptDiv); // If client has provided separate transcript location, put it there. // Otherwise append it to the body if (this.transcriptDivLocation) { this.$transcriptArea.removeAttr( 'role' ); this.$transcriptArea.removeAttr( 'aria-label' ); $("#" + this.transcriptDivLocation).append(this.$transcriptArea); } else { this.$ableWrapper.append(this.$transcriptArea); } // make it draggable (popup only; NOT external transcript) if (!this.transcriptDivLocation) { this.initDragDrop("transcript"); if (this.prefTranscript === 1) { // transcript is on. Go ahead and position it this.positionDraggableWindow( "transcript", this.getDefaultWidth("transcript") ); } } // If client has provided separate transcript location, override user's preference for hiding transcript if (!this.prefTranscript && !this.transcriptDivLocation) { this.$transcriptArea.hide(); } }; AblePlayer.prototype.addTranscriptAreaEvents = function () { var thisObj = this; this.$autoScrollTranscriptCheckbox.on( 'click', function () { thisObj.handleTranscriptLockToggle( thisObj.$autoScrollTranscriptCheckbox.prop("checked") ); }); this.$transcriptDiv.on( "mousewheel DOMMouseScroll click scroll", function (e) { // Propagation is stopped in transcript click handler, so clicks are on the scrollbar // or outside of a clickable span. if (!thisObj.scrollingTranscript) { thisObj.autoScrollTranscript = false; thisObj.refreshControls("transcript"); } thisObj.scrollingTranscript = false; } ); if (typeof this.$transcriptLanguageSelect !== "undefined") { this.$transcriptLanguageSelect.on('click', function (e) { // execute default behavior // prevent propagation of mouse event to toolbar or window e.stopPropagation(); }); this.$transcriptLanguageSelect.on("change", function () { var language = thisObj.$transcriptLanguageSelect.val(); thisObj.syncTrackLanguages("transcript", language); }); } }; AblePlayer.prototype.transcriptSrcHasRequiredParts = function () { // check the external transcript to be sure it has all required components // return true or false // in the process, define all the needed variables and properties if ($("#" + this.transcriptSrc).length) { this.$transcriptArea = $("#" + this.transcriptSrc); if (this.$transcriptArea.find(".able-window-toolbar").length) { this.$transcriptToolbar = this.$transcriptArea .find(".able-window-toolbar") .eq(0); if (this.$transcriptArea.find(".able-transcript").length) { this.$transcriptDiv = this.$transcriptArea .find(".able-transcript") .eq(0); if (this.$transcriptArea.find(".able-transcript-seekpoint").length) { this.$transcriptSeekpoints = this.$transcriptArea.find( ".able-transcript-seekpoint" ); return true; } } } } return false; }; AblePlayer.prototype.setupManualTranscript = function () { var autoScrollField; autoScrollField = this.createField({ id: 'autoscroll-transcript-checkbox-' + this.mediaId, label: this.translate( 'autoScroll', 'Auto scroll' ), type: 'checkbox', labelPosition: 'before' }); // Add an auto-scroll checkbox to the toolbar. this.$autoScrollTranscriptCheckbox = autoScrollField.field; this.$transcriptToolbar.append( autoScrollField.label, this.$autoScrollTranscriptCheckbox ); }; AblePlayer.prototype.updateTranscript = function () { if (!this.transcriptType) { return; } if (this.playerCreated && !this.$transcriptArea) { return; } if (this.transcriptType === "external" || this.transcriptType === "popup") { var chapters, captions, descriptions; // Language of transcript might be different than language of captions // But both are in sync by default if (this.transcriptLang) { captions = this.transcriptCaptions.cues; } else { if (this.transcriptCaptions) { this.transcriptLang = this.transcriptCaptions.language; captions = this.transcriptCaptions.cues; } else if (this.selectedCaptions) { this.transcriptLang = this.captionLang; captions = this.selectedCaptions.cues; } } // setup chapters if (this.transcriptChapters) { chapters = this.transcriptChapters.cues; } else if (this.chapters.length > 0) { // Try and match the caption language. if (this.transcriptLang) { for (var i = 0; i < this.chapters.length; i++) { if (this.chapters[i].language === this.transcriptLang) { chapters = this.chapters[i].cues; } } } if (typeof chapters === "undefined") { chapters = this.chapters[0].cues || []; } } // setup descriptions if (this.transcriptDescriptions) { descriptions = this.transcriptDescriptions.cues; } else if (this.descriptions.length > 0) { // Try and match the caption language. if (this.transcriptLang) { for (i = 0; i < this.descriptions.length; i++) { if (this.descriptions[i].language === this.transcriptLang) { descriptions = this.descriptions[i].cues; } } } if (!descriptions) { descriptions = this.descriptions[0].cues || []; } } var div = this.generateTranscript( chapters || [], captions || [], descriptions || [] ); this.$transcriptDiv.html(div); // reset transcript selected