<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.TranscribeEntry');

mirosubs.subtitle.TranscribeEntry = function(videoPlayer) {
    goog.ui.Component.call(this);
    this.videoPlayer_ = videoPlayer;
    this.endOfPPlayheadTime_ = null;
    this.repeatVideoMode_ = false;

    this.continuouslyTyping_ = false;
    this.continuousTypingTimer_ = new goog.Timer(
        mirosubs.subtitle.TranscribeEntry.P * 1000);
    this.typingPauseTimer_ = new goog.Timer(
        mirosubs.subtitle.TranscribeEntry.S * 1000);
};
goog.inherits(mirosubs.subtitle.TranscribeEntry, goog.ui.Component);
mirosubs.subtitle.TranscribeEntry.logger_ = 
    goog.debug.Logger.getLogger('mirosubs.subtitle.TranscribeEntry');

mirosubs.subtitle.TranscribeEntry.P = 4;
mirosubs.subtitle.TranscribeEntry.R = 3;
mirosubs.subtitle.TranscribeEntry.S = 1;

mirosubs.subtitle.TranscribeEntry.prototype.createDom = function() {
    mirosubs.subtitle.TranscribeEntry.superClass_.createDom.call(this);
    this.getElement().setAttribute('class', 'mirosubs-transcribeControls');
    this.addChild(this.labelInput_ = new goog.ui.LabelInput(
        'Type subtitle and press enter'), true);
    this.labelInput_.LABEL_CLASS_NAME = 'mirosubs-label-input-label';
    goog.dom.classes.add(this.labelInput_.getElement(), 'trans');
};
mirosubs.subtitle.TranscribeEntry.prototype.enterDocument = function() {
    mirosubs.subtitle.TranscribeEntry.superClass_.enterDocument.call(this);
    this.keyHandler_ = new goog.events.KeyHandler(this.labelInput_.getElement());
    this.getHandler().listen(this.keyHandler_,
                             goog.events.KeyHandler.EventType.KEY,
                             this.handleKey_);
    this.getHandler().listen(this.labelInput_.getElement(),
                             goog.events.EventType.KEYUP,
                             this.handleKeyUp_);
    this.getHandler().listen(this.typingPauseTimer_,
                             goog.Timer.TICK,
                             this.typingPauseTimerTick_);
    this.getHandler().listen(this.continuousTypingTimer_,
                             goog.Timer.TICK,
                             this.continuousTypingTimerTick_);
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        "P is set to " + mirosubs.subtitle.TranscribeEntry.P);
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        "R is set to " + mirosubs.subtitle.TranscribeEntry.R);
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        "S is set to " + mirosubs.subtitle.TranscribeEntry.S);
};
mirosubs.subtitle.TranscribeEntry.prototype.focus = function() {
    if (this.labelInput_.getValue() == '')
        this.labelInput_.focusAndSelect();
    else
        this.labelInput_.getElement().focus();
};
mirosubs.subtitle.TranscribeEntry.prototype.handleKey_ = function(event) {
    if (event.keyCode == goog.events.KeyCodes.ENTER) {
        event.preventDefault();
        this.addNewTitle_();
    }
    else if (event.keyCode != goog.events.KeyCodes.TAB && 
             this.repeatVideoMode_) {
        this.typingPauseTimer_.stop();
        this.typingPauseTimer_.start();
        if (!this.continuouslyTyping_) {
            mirosubs.subtitle.TranscribeEntry.logger_.info(
                "Continuous typing started.");
            this.continuousTypingTimer_.start();
            this.continuouslyTyping_ = true;
        }
    }
};
mirosubs.subtitle.TranscribeEntry.prototype.continuousTypingTimerTick_ = function() {
    // P seconds since continuous typing was started.
    this.continuousTypingTimer_.stop();
    this.videoPlayer_.pause();
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        ["Continuous typing has now progressed for P ",
         "seconds. Pausing video at playhead time ", 
         this.videoPlayer_.getPlayheadTime() + ''].join(''));
};
mirosubs.subtitle.TranscribeEntry.prototype.typingPauseTimerTick_ = function() {
    // S seconds since last keystroke!
    var pSecondsElapsed = !this.continuousTypingTimer_.enabled;
    var newPlayheadTime = this.videoPlayer_.getPlayheadTime() - 
        mirosubs.subtitle.TranscribeEntry.R;
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        ["Continuous typing ended.",
         pSecondsElapsed ? 
         (" Restarting video at playhead time " + newPlayheadTime) : 
         ""].join(''));
    this.continuouslyTyping_ = false;
    this.typingPauseTimer_.stop();
    this.continuousTypingTimer_.stop();
    if (pSecondsElapsed) {
        this.videoPlayer_.setPlayheadTime(newPlayheadTime);
        this.videoPlayer_.play();
    }
};
/**
 * Turns Repeat Video Mode on or off. When this mode is turned on, the video 
 * repeats during typing. The mode is off by default.
 * @param {boolean} mode True to turn Repeat Video Mode on, false to turn it off.
 */
mirosubs.subtitle.TranscribeEntry.prototype.setRepeatVideoMode = function(mode) {
    this.repeatVideoMode_ = mode;
    this.continuouslyTyping_ = false;
    this.continuousTypingTimer_.stop();
    this.typingPauseTimer_.stop();
};
mirosubs.subtitle.TranscribeEntry.prototype.handleKeyUp_ = function(event) {
    this.videoPlayer_.showCaptionText(this.labelInput_.getValue());
    this.issueLengthWarning_(this.insertsBreakableChar_(event.keyCode));
};
mirosubs.subtitle.TranscribeEntry.prototype.addNewTitle_ = function() {
    var value = this.labelInput_.getValue();
    // FIXME: accessing private member of goog.ui.LabelInput
    this.labelInput_.label_ = '';
    this.labelInput_.setValue('');
    this.labelInput_.focusAndSelect();
    this.dispatchEvent(new mirosubs.subtitle.TranscribeEntry
                       .NewTitleEvent(value));
};
mirosubs.subtitle.TranscribeEntry.prototype.issueLengthWarning_ = function(breakable) {
    var MAX_CHARS = 100;
    var length = this.labelInput_.getValue().length;
    if (breakable && length > MAX_CHARS)
        this.addNewTitle_();
    else
        this.getElement().style.background = this.warningColor_(length, 50, MAX_CHARS);
};
mirosubs.subtitle.TranscribeEntry.prototype.warningColor_ = 
    function(length, firstChars, maxChars) {

    if (length < firstChars) 
        return "#ddd";

    length -= firstChars;
    var r = 15;
    var g = 16 - 16 * length / (maxChars - firstChars);
    var b = 12 - 12 * length / (maxChars - firstChars);
    return ["#", this.hex_(r), this.hex_(g), this.hex_(b)].join('');    <commit_msg>bug 13533: if autopause is on but the video is already paused, do not resume the video after typing stops<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.TranscribeEntry');

mirosubs.subtitle.TranscribeEntry = function(videoPlayer) {
    goog.ui.Component.call(this);
    this.videoPlayer_ = videoPlayer;
    this.endOfPPlayheadTime_ = null;
    this.repeatVideoMode_ = false;

    this.wasPlaying_ = false;
    this.continuouslyTyping_ = false;
    this.continuousTypingTimer_ = new goog.Timer(
        mirosubs.subtitle.TranscribeEntry.P * 1000);
    this.typingPauseTimer_ = new goog.Timer(
        mirosubs.subtitle.TranscribeEntry.S * 1000);
};
goog.inherits(mirosubs.subtitle.TranscribeEntry, goog.ui.Component);
mirosubs.subtitle.TranscribeEntry.logger_ =
    goog.debug.Logger.getLogger('mirosubs.subtitle.TranscribeEntry');

mirosubs.subtitle.TranscribeEntry.P = 4;
mirosubs.subtitle.TranscribeEntry.R = 3;
mirosubs.subtitle.TranscribeEntry.S = 1;

mirosubs.subtitle.TranscribeEntry.prototype.createDom = function() {
    mirosubs.subtitle.TranscribeEntry.superClass_.createDom.call(this);
    this.getElement().setAttribute('class', 'mirosubs-transcribeControls');
    this.addChild(this.labelInput_ = new goog.ui.LabelInput(
        'Type subtitle and press enter'), true);
    this.labelInput_.LABEL_CLASS_NAME = 'mirosubs-label-input-label';
    goog.dom.classes.add(this.labelInput_.getElement(), 'trans');
};
mirosubs.subtitle.TranscribeEntry.prototype.enterDocument = function() {
    mirosubs.subtitle.TranscribeEntry.superClass_.enterDocument.call(this);
    this.keyHandler_ = new goog.events.KeyHandler(this.labelInput_.getElement());
    this.getHandler().listen(this.keyHandler_,
                             goog.events.KeyHandler.EventType.KEY,
                             this.handleKey_);
    this.getHandler().listen(this.labelInput_.getElement(),
                             goog.events.EventType.KEYUP,
                             this.handleKeyUp_);
    this.getHandler().listen(this.typingPauseTimer_,
                             goog.Timer.TICK,
                             this.typingPauseTimerTick_);
    this.getHandler().listen(this.continuousTypingTimer_,
                             goog.Timer.TICK,
                             this.continuousTypingTimerTick_);
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        "P is set to " + mirosubs.subtitle.TranscribeEntry.P);
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        "R is set to " + mirosubs.subtitle.TranscribeEntry.R);
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        "S is set to " + mirosubs.subtitle.TranscribeEntry.S);
};
mirosubs.subtitle.TranscribeEntry.prototype.focus = function() {
    if (this.labelInput_.getValue() == '')
        this.labelInput_.focusAndSelect();
    else
        this.labelInput_.getElement().focus();
};
mirosubs.subtitle.TranscribeEntry.prototype.handleKey_ = function(event) {
    if (event.keyCode == goog.events.KeyCodes.ENTER) {
        event.preventDefault();
        this.addNewTitle_();
    }
    else if (event.keyCode != goog.events.KeyCodes.TAB &&
             this.repeatVideoMode_) {
        this.typingPauseTimer_.stop();
        this.typingPauseTimer_.start();
        if (!this.continuouslyTyping_) {
            mirosubs.subtitle.TranscribeEntry.logger_.info(
                "Continuous typing started.");
            this.continuousTypingTimer_.start();
            this.continuouslyTyping_ = true;
        }
    }
};
mirosubs.subtitle.TranscribeEntry.prototype.continuousTypingTimerTick_ = function() {
    // P seconds since continuous typing was started.
    this.continuousTypingTimer_.stop();
    this.wasPlaying_ = this.videoPlayer_.isPlaying();
    this.videoPlayer_.pause();
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        ["Continuous typing has now progressed for P ",
         "seconds. Pausing video at playhead time ",
         this.videoPlayer_.getPlayheadTime() + ''].join(''));
};
mirosubs.subtitle.TranscribeEntry.prototype.typingPauseTimerTick_ = function() {
    // S seconds since last keystroke!
    var pSecondsElapsed = !this.continuousTypingTimer_.enabled;
    var newPlayheadTime = this.videoPlayer_.getPlayheadTime() -
        mirosubs.subtitle.TranscribeEntry.R;
    mirosubs.subtitle.TranscribeEntry.logger_.info(
        ["Continuous typing ended.",
         pSecondsElapsed ?
         (" Restarting video at playhead time " + newPlayheadTime) :
         ""].join(''));
    this.continuouslyTyping_ = false;
    this.typingPauseTimer_.stop();
    this.continuousTypingTimer_.stop();
    if (pSecondsElapsed && this.wasPlaying_) {
        this.videoPlayer_.setPlayheadTime(newPlayheadTime);
        this.videoPlayer_.play();
    }
};
/**
 * Turns Repeat Video Mode on or off. When this mode is turned on, the video
 * repeats during typing. The mode is off by default.
 * @param {boolean} mode True to turn Repeat Video Mode on, false to turn it off.
 */
mirosubs.subtitle.TranscribeEntry.prototype.setRepeatVideoMode = function(mode) {
    this.repeatVideoMode_ = mode;
    this.continuouslyTyping_ = false;
    this.continuousTypingTimer_.stop();
    this.typingPauseTimer_.stop();
};
mirosubs.subtitle.TranscribeEntry.prototype.handleKeyUp_ = function(event) {
    this.videoPlayer_.showCaptionText(this.labelInput_.getValue());
    this.issueLengthWarning_(this.insertsBreakableChar_(event.keyCode));
};
mirosubs.subtitle.TranscribeEntry.prototype.addNewTitle_ = function() {
    var value = this.labelInput_.getValue();
    // FIXME: accessing private member of goog.ui.LabelInput
    this.labelInput_.label_ = '';
    this.labelInput_.setValue('');
    this.labelInput_.focusAndSelect();
    this.dispatchEvent(new mirosubs.subtitle.TranscribeEntry
                       .NewTitleEvent(value));
};
mirosubs.subtitle.TranscribeEntry.prototype.issueLengthWarning_ = function(breakable) {
    var MAX_CHARS = 100;
    var length = this.labelInput_.getValue().length;
    if (breakable && length > MAX_CHARS)
        this.addNewTitle_();
    else
        this.getElement().style.background = this.warningColor_(length, 50, MAX_CHARS);
};
mirosubs.subtitle.TranscribeEntry.prototype.warningColor_ =
    function(length, firstChars, maxChars) {

    if (length < firstChars)
        return "#ddd";

    length -= firstChars;
    var r = 15;
    var g = 16 - 16 * length / (maxChars - firstChars);
    var b = 12 - 12 * length / (maxChars - firstChars);
    return ["#", this.hex_(r), this.hex_(g), this.hex_(b)].join('');
{'fraction_non_alphanumeric': 0.11277624309392265, 'fraction_numerical': 0.004903314917127072, 'mean_word_length': 3.2108170979936026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '402676', 'n_tokens_mistral': 4483, 'n_tokens_neox': 4268, 'n_words': 838}