<commit_before>// Universal Subtitles, universalsubtitles.org
//
// Copyright (C) 2010 Participatory Culture Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see
// http://www.gnu.org/licenses/agpl-3.0.html.

goog.provide('mirosubs.subtitle.Dialog');

/**
 *
 * @param {mirosubs.subtitle.ServerModel} serverModel
 * @param {Array.<Object.<string, *>>} existingCaptions existing captions in
 *     json object format.
 */
mirosubs.subtitle.Dialog = function(videoSource, serverModel,
                                    existingCaptions) {
    mirosubs.Dialog.call(this, videoSource);
    this.serverModel_ = serverModel;
    var uw = this.unitOfWork_ = new mirosubs.UnitOfWork();
    this.captionSet_ =
        new mirosubs.subtitle.EditableCaptionSet(existingCaptions, uw);
    this.captionManager_ =
        new mirosubs.CaptionManager(
            this.getVideoPlayerInternal(), this.captionSet_);
    this.serverModel_ = serverModel;
    this.serverModel_.init(uw, goog.bind(this.showLoginNag_, this));
    /**
     * @type {?boolean} True iff we pass into FINISHED state.
     */
    this.saved_ = false;
    /**
     *
     * @type {?mirosubs.subtitle.Dialog.State_}
     */
    this.state_ = null;
    this.currentSubtitlePanel_ = null;
    this.rightPanelListener_ = new goog.events.EventHandler(this);
    this.doneButtonEnabled_ = true;
    this.addingTranslations_ = false;
};
goog.inherits(mirosubs.subtitle.Dialog, mirosubs.Dialog);

/**
 *
 * @enum
 */
mirosubs.subtitle.Dialog.State_ = {
    TRANSCRIBE: 0,
    SYNC: 1,
    REVIEW: 2,
    FINISHED: 3
};
mirosubs.subtitle.Dialog.prototype.captionReached_ = function(event) {
    var c = event.caption;
    this.getVideoPlayerInternal().showCaptionText(c ? c.getText() : '');
};
mirosubs.subtitle.Dialog.prototype.createDom = function() {
    mirosubs.subtitle.Dialog.superClass_.createDom.call(this);
    this.setState_(mirosubs.subtitle.Dialog.State_.TRANSCRIBE);
};
mirosubs.subtitle.Dialog.prototype.enterDocument = function() {
    mirosubs.subtitle.Dialog.superClass_.enterDocument.call(this);
    this.getHandler().
        listen(
            document,
            goog.events.EventType.KEYDOWN,
            this.handleKeyDown_).
        listen(
            this.captionManager_,
            mirosubs.CaptionManager.CAPTION,
            this.captionReached_);
};
mirosubs.subtitle.Dialog.prototype.setState_ = function(state) {
    this.state_ = state;

    var nextSubPanel = this.makeCurrentStateSubtitlePanel_();
    var captionPanel = this.getCaptioningAreaInternal();
    captionPanel.removeChildren(true);
    captionPanel.addChild(nextSubPanel, true);

    var rightPanel = nextSubPanel.getRightPanel();
    this.setRightPanelInternal(rightPanel);

    this.getTimelinePanelInternal().removeChildren(true);

    this.disposeCurrentPanels_();
    this.currentSubtitlePanel_ = nextSubPanel;

    var et = mirosubs.RightPanel.EventType;
    this.rightPanelListener_.listen(
        rightPanel, et.LEGENDKEY, this.handleLegendKeyPress_);
    this.rightPanelListener_.listen(
        rightPanel, et.DONE, this.handleDoneKeyPress_);
    var s = mirosubs.subtitle.Dialog.State_;
    if (state == s.SYNC || state == s.REVIEW) {
        rightPanel.showBackLink(
            state == s.SYNC ? "Back to Transcribe" : "Back to Sync");
        this.rightPanelListener_.listen(
            rightPanel, et.BACK, this.handleBackKeyPress_);
        this.timelineSubtitleSet_ =
            new mirosubs.timeline.SubtitleSet(
                this.captionSet_, this.getVideoPlayerInternal());
        this.getTimelinePanelInternal().addChild(
            new mirosubs.timeline.Timeline(
                1, this.timelineSubtitleSet_,
                this.getVideoPlayerInternal()), true);
    }

    var videoPlayer = this.getVideoPlayerInternal();
    if (this.isInDocument()) {
        videoPlayer.setPlayheadTime(0);
        videoPlayer.pause();
    }
};
mirosubs.subtitle.Dialog.prototype.setFinishedState_ = function() {
    this.saved_ = true;
    var sharePanel = new mirosubs.subtitle.SharePanel(
        this.serverModel_);
    this.setRightPanelInternal(sharePanel);
    this.getTimelinePanelInternal().removeChildren(true);
    this.getCaptioningAreaInternal().removeChildren(true);
    var bottomContainer = this.getBottomPanelContainerInternal();
    var bottomFinishedPanel = new mirosubs.subtitle.BottomFinishedPanel(this);
    bottomContainer.addChild(bottomFinishedPanel, true);
};
mirosubs.subtitle.Dialog.prototype.handleKeyDown_ = function(event) {
    var s = mirosubs.subtitle.Dialog.State_;
    if (event.keyCode == goog.events.KeyCodes.CTRL)
        this.ctrlClicked_();
    if (event.keyCode == goog.events.KeyCodes.TAB) {
        //TODO: this violates accessibility guidelines. Use another key instead of TAB!
        this.togglePause_();
        event.preventDefault();
    }
    if (event.keyCode == goog.events.KeyCodes.SPACE &&
        (this.state_ == s.SYNC || this.state_ == s.REVIEW) &&
        !this.currentSubtitlePanel_.currentlyEditingSubtitle()) {
        this.togglePause_();
        event.preventDefault();
    }
};
mirosubs.subtitle.Dialog.prototype.handleBackKeyPress_ = function(event) {
    var s = mirosubs.subtitle.Dialog.State_;
    if (this.state_ == s.SYNC)
        this.setState_(s.TRANSCRIBE);
    else if (this.state_ == s.REVIEW)<commit_msg>bug 13569: stop video and set playhead time to 0 when finishing the review process<commit_after>// Universal Subtitles, universalsubtitles.org
//
// Copyright (C) 2010 Participatory Culture Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see
// http://www.gnu.org/licenses/agpl-3.0.html.

goog.provide('mirosubs.subtitle.Dialog');

/**
 *
 * @param {mirosubs.subtitle.ServerModel} serverModel
 * @param {Array.<Object.<string, *>>} existingCaptions existing captions in
 *     json object format.
 */
mirosubs.subtitle.Dialog = function(videoSource, serverModel,
                                    existingCaptions) {
    mirosubs.Dialog.call(this, videoSource);
    this.serverModel_ = serverModel;
    var uw = this.unitOfWork_ = new mirosubs.UnitOfWork();
    this.captionSet_ =
        new mirosubs.subtitle.EditableCaptionSet(existingCaptions, uw);
    this.captionManager_ =
        new mirosubs.CaptionManager(
            this.getVideoPlayerInternal(), this.captionSet_);
    this.serverModel_ = serverModel;
    this.serverModel_.init(uw, goog.bind(this.showLoginNag_, this));
    /**
     * @type {?boolean} True iff we pass into FINISHED state.
     */
    this.saved_ = false;
    /**
     *
     * @type {?mirosubs.subtitle.Dialog.State_}
     */
    this.state_ = null;
    this.currentSubtitlePanel_ = null;
    this.rightPanelListener_ = new goog.events.EventHandler(this);
    this.doneButtonEnabled_ = true;
    this.addingTranslations_ = false;
};
goog.inherits(mirosubs.subtitle.Dialog, mirosubs.Dialog);

/**
 *
 * @enum
 */
mirosubs.subtitle.Dialog.State_ = {
    TRANSCRIBE: 0,
    SYNC: 1,
    REVIEW: 2,
    FINISHED: 3
};
mirosubs.subtitle.Dialog.prototype.captionReached_ = function(event) {
    var c = event.caption;
    this.getVideoPlayerInternal().showCaptionText(c ? c.getText() : '');
};
mirosubs.subtitle.Dialog.prototype.createDom = function() {
    mirosubs.subtitle.Dialog.superClass_.createDom.call(this);
    this.setState_(mirosubs.subtitle.Dialog.State_.TRANSCRIBE);
};
mirosubs.subtitle.Dialog.prototype.enterDocument = function() {
    mirosubs.subtitle.Dialog.superClass_.enterDocument.call(this);
    this.getHandler().
        listen(
            document,
            goog.events.EventType.KEYDOWN,
            this.handleKeyDown_).
        listen(
            this.captionManager_,
            mirosubs.CaptionManager.CAPTION,
            this.captionReached_);
};
mirosubs.subtitle.Dialog.prototype.setState_ = function(state) {
    this.state_ = state;

    var nextSubPanel = this.makeCurrentStateSubtitlePanel_();
    var captionPanel = this.getCaptioningAreaInternal();
    captionPanel.removeChildren(true);
    captionPanel.addChild(nextSubPanel, true);

    var rightPanel = nextSubPanel.getRightPanel();
    this.setRightPanelInternal(rightPanel);

    this.getTimelinePanelInternal().removeChildren(true);

    this.disposeCurrentPanels_();
    this.currentSubtitlePanel_ = nextSubPanel;

    var et = mirosubs.RightPanel.EventType;
    this.rightPanelListener_.listen(
        rightPanel, et.LEGENDKEY, this.handleLegendKeyPress_);
    this.rightPanelListener_.listen(
        rightPanel, et.DONE, this.handleDoneKeyPress_);
    var s = mirosubs.subtitle.Dialog.State_;
    if (state == s.SYNC || state == s.REVIEW) {
        rightPanel.showBackLink(
            state == s.SYNC ? "Back to Transcribe" : "Back to Sync");
        this.rightPanelListener_.listen(
            rightPanel, et.BACK, this.handleBackKeyPress_);
        this.timelineSubtitleSet_ =
            new mirosubs.timeline.SubtitleSet(
                this.captionSet_, this.getVideoPlayerInternal());
        this.getTimelinePanelInternal().addChild(
            new mirosubs.timeline.Timeline(
                1, this.timelineSubtitleSet_,
                this.getVideoPlayerInternal()), true);
    }

    var videoPlayer = this.getVideoPlayerInternal();
    if (this.isInDocument()) {
        videoPlayer.setPlayheadTime(0);
        videoPlayer.pause();
    }
};
mirosubs.subtitle.Dialog.prototype.setFinishedState_ = function() {
    this.saved_ = true;
    var sharePanel = new mirosubs.subtitle.SharePanel(
        this.serverModel_);
    this.setRightPanelInternal(sharePanel);
    this.getTimelinePanelInternal().removeChildren(true);
    this.getCaptioningAreaInternal().removeChildren(true);
    var bottomContainer = this.getBottomPanelContainerInternal();
    var bottomFinishedPanel = new mirosubs.subtitle.BottomFinishedPanel(this);
    bottomContainer.addChild(bottomFinishedPanel, true);

    var videoPlayer = this.getVideoPlayerInternal();
    if (this.isInDocument()) {
        videoPlayer.setPlayheadTime(0);
        videoPlayer.pause();
    }
};
mirosubs.subtitle.Dialog.prototype.handleKeyDown_ = function(event) {
    var s = mirosubs.subtitle.Dialog.State_;
    if (event.keyCode == goog.events.KeyCodes.CTRL)
        this.ctrlClicked_();
    if (event.keyCode == goog.events.KeyCodes.TAB) {
        //TODO: this violates accessibility guidelines. Use another key instead of TAB!
        this.togglePause_();
        event.preventDefault();
    }
    if (event.keyCode == goog.events.KeyCodes.SPACE &&
        (this.state_ == s.SYNC || this.state_ == s.REVIEW) &&
        !this.currentSubtitlePanel_.currentlyEditingSubtitle()) {
        this.togglePause_();
        event.preventDefault();
    }
};
mirosubs.subtitle.Dialog.prototype.handleBackKeyPress_ = function(event) {
    var s = mirosubs.subtitle.Dialog.State_;
    if (this.state_ == s.SYNC)
        this.setState_(s.TRANSCRIBE);
    else if (this.state_ == s.REVIEW)
{'fraction_non_alphanumeric': 0.11175257731958763, 'fraction_numerical': 0.0027216494845360823, 'mean_word_length': 3.213342599027102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2628626', 'n_tokens_mistral': 3704, 'n_tokens_neox': 3662, 'n_words': 724}