<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('unisubs.widget.SubtitleDialogOpener');

/**
 * @constructor
 * @param {string} videoID
 * @param {string} videoURL This is used for creating the embed code 
 *     that appears in the widget.
 * @param {unisubs.player.MediaSource} videoSource
 * @param {function(boolean)=} opt_loadingFn
 * @param {function()=} opt_subOpenFn
 */
unisubs.widget.SubtitleDialogOpener = function(videoID, videoURL, videoSource, opt_loadingFn, opt_subOpenFn) {
    goog.events.EventTarget.call(this);
    this.videoID_ = videoID;
    this.videoURL_ = videoURL;
    this.videoSource_ = videoSource;
    this.loadingFn_ = opt_loadingFn;
    this.subOpenFn_ = opt_subOpenFn;
};
goog.inherits(unisubs.widget.SubtitleDialogOpener, goog.events.EventTarget);

unisubs.widget.SubtitleDialogOpener.prototype.showLoading_ = function(loading) {
    if (this.loadingFn_)
        this.loadingFn_(loading);
};
unisubs.widget.SubtitleDialogOpener.prototype.getResumeEditingRecord_ = function(openDialogArgs) {
    var resumeEditingRecord = unisubs.widget.ResumeEditingRecord.fetch();
    if (resumeEditingRecord && resumeEditingRecord.matches(
        this.videoID_, openDialogArgs))
        return resumeEditingRecord;
    else
        return null;
};
unisubs.widget.SubtitleDialogOpener.prototype.saveResumeEditingRecord_ = function(sessionPK, openDialogArgs) {
    var resumeEditingRecord = new unisubs.widget.ResumeEditingRecord(
        this.videoID_, sessionPK, openDialogArgs);
    resumeEditingRecord.save();
};

/**
 * Calls start_editing on server and then, if successful, opens the dialog.
 * @param {unisubs.widget.OpenDialogArgs} openDialogArgs
 * @param {function()=} opt_completeCallback
 */
unisubs.widget.SubtitleDialogOpener.prototype.openDialog = function( openDialogArgs, opt_completeCallback) {
    if (this.disallow_()) {
        return;
    }
    var that = this;
    this.showLoading_(true);
    var resumeEditingRecord = this.getResumeEditingRecord_(openDialogArgs);
    if (resumeEditingRecord && resumeEditingRecord.getSavedSubtitles()) {
        this.resumeEditing_(
            resumeEditingRecord.getSavedSubtitles(),
            openDialogArgs,
            opt_completeCallback);
    }
    else {
        this.startEditing_(openDialogArgs, opt_completeCallback);
    }
};
unisubs.widget.SubtitleDialogOpener.prototype.startEditing_ = function(openDialogArgs, opt_completeCallback) {
    var args = {
        'video_id': this.videoID_,
        'language_code': openDialogArgs.LANGUAGE,
        'subtitle_language_pk': openDialogArgs.SUBLANGUAGE_PK || null,
        'base_language_pk': openDialogArgs.BASELANGUAGE_PK || null,
        'original_language_code': openDialogArgs.ORIGINAL_LANGUAGE || null,
        'mode': unisubs.mode || null };
    var that = this;
    unisubs.Rpc.call(
        'start_editing', args,
        function(result) {
            that.saveResumeEditingRecord_(
                result['session_pk'], openDialogArgs);
            if (opt_completeCallback)
                opt_completeCallback();
            that.startEditingResponseHandler_(result, false);
        });
};
unisubs.widget.SubtitleDialogOpener.prototype.resumeEditing = function() { 
    var resumeEditingRecord = unisubs.widget.ResumeEditingRecord.fetch();
    this.resumeEditing_(
        resumeEditingRecord.getSavedSubtitles(),
        resumeEditingRecord.getOpenDialogArgs());
};
unisubs.widget.SubtitleDialogOpener.prototype.resumeEditing_ = function(savedSubtitles, openDialogArgs, opt_completeCallback) {
    var that = this;
    unisubs.Rpc.call(
        'resume_editing', 
        { 'session_pk': savedSubtitles.SESSION_PK },
        function(result) {
            if (result['response'] == 'ok') {
                // FIXME: ouch, kinda hacky.
                result['subtitles']['subtitles'] = 
                    savedSubtitles.CAPTION_SET.makeJsonSubs();
                result['subtitles']['title'] = 
                    savedSubtitles.CAPTION_SET.title;
                that.startEditingResponseHandler_(
                    result, true, <commit_msg>Make sure restoring from saved session won't overwrite actual title and description when those are null<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('unisubs.widget.SubtitleDialogOpener');

/**
 * @constructor
 * @param {string} videoID
 * @param {string} videoURL This is used for creating the embed code 
 *     that appears in the widget.
 * @param {unisubs.player.MediaSource} videoSource
 * @param {function(boolean)=} opt_loadingFn
 * @param {function()=} opt_subOpenFn
 */
unisubs.widget.SubtitleDialogOpener = function(videoID, videoURL, videoSource, opt_loadingFn, opt_subOpenFn) {
    goog.events.EventTarget.call(this);
    this.videoID_ = videoID;
    this.videoURL_ = videoURL;
    this.videoSource_ = videoSource;
    this.loadingFn_ = opt_loadingFn;
    this.subOpenFn_ = opt_subOpenFn;
};
goog.inherits(unisubs.widget.SubtitleDialogOpener, goog.events.EventTarget);

unisubs.widget.SubtitleDialogOpener.prototype.showLoading_ = function(loading) {
    if (this.loadingFn_)
        this.loadingFn_(loading);
};
unisubs.widget.SubtitleDialogOpener.prototype.getResumeEditingRecord_ = function(openDialogArgs) {
    var resumeEditingRecord = unisubs.widget.ResumeEditingRecord.fetch();
    if (resumeEditingRecord && resumeEditingRecord.matches(
        this.videoID_, openDialogArgs))
        return resumeEditingRecord;
    else
        return null;
};
unisubs.widget.SubtitleDialogOpener.prototype.saveResumeEditingRecord_ = function(sessionPK, openDialogArgs) {
    var resumeEditingRecord = new unisubs.widget.ResumeEditingRecord(
        this.videoID_, sessionPK, openDialogArgs);
    resumeEditingRecord.save();
};

/**
 * Calls start_editing on server and then, if successful, opens the dialog.
 * @param {unisubs.widget.OpenDialogArgs} openDialogArgs
 * @param {function()=} opt_completeCallback
 */
unisubs.widget.SubtitleDialogOpener.prototype.openDialog = function( openDialogArgs, opt_completeCallback) {
    if (this.disallow_()) {
        return;
    }
    var that = this;
    this.showLoading_(true);
    var resumeEditingRecord = this.getResumeEditingRecord_(openDialogArgs);
    if (resumeEditingRecord && resumeEditingRecord.getSavedSubtitles()) {
        this.resumeEditing_(
            resumeEditingRecord.getSavedSubtitles(),
            openDialogArgs,
            opt_completeCallback);
    }
    else {
        this.startEditing_(openDialogArgs, opt_completeCallback);
    }
};
unisubs.widget.SubtitleDialogOpener.prototype.startEditing_ = function(openDialogArgs, opt_completeCallback) {
    var args = {
        'video_id': this.videoID_,
        'language_code': openDialogArgs.LANGUAGE,
        'subtitle_language_pk': openDialogArgs.SUBLANGUAGE_PK || null,
        'base_language_pk': openDialogArgs.BASELANGUAGE_PK || null,
        'original_language_code': openDialogArgs.ORIGINAL_LANGUAGE || null,
        'mode': unisubs.mode || null };
    var that = this;
    unisubs.Rpc.call(
        'start_editing', args,
        function(result) {
            that.saveResumeEditingRecord_(
                result['session_pk'], openDialogArgs);
            if (opt_completeCallback)
                opt_completeCallback();
            that.startEditingResponseHandler_(result, false);
        });
};
unisubs.widget.SubtitleDialogOpener.prototype.resumeEditing = function() { 
    var resumeEditingRecord = unisubs.widget.ResumeEditingRecord.fetch();
    this.resumeEditing_(
        resumeEditingRecord.getSavedSubtitles(),
        resumeEditingRecord.getOpenDialogArgs());
};
unisubs.widget.SubtitleDialogOpener.prototype.resumeEditing_ = function(savedSubtitles, openDialogArgs, opt_completeCallback) {
    var that = this;
    unisubs.Rpc.call(
        'resume_editing', 
        { 'session_pk': savedSubtitles.SESSION_PK },
        function(result) {
            if (result['response'] == 'ok') {
                // FIXME: ouch, kinda hacky.
                result['subtitles']['subtitles'] = 
                    savedSubtitles.CAPTION_SET.makeJsonSubs();
                if (savedSubtitles.CAPTION_SET.title && savedSubtitles.CAPTION_SET.title.length){
                    result['subtitles']['title'] = 
                        savedSubtitles.CAPTION_SET.title;
                }
                if (savedSubtitles.CAPTION_SET.description && savedSubtitles.CAPTION_SET.description.length){
                    result['subtitles']['description'] = 
                        savedSubtitles.CAPTION_SET.description;
                }

                that.startEditingResponseHandler_(
                    result, true, 
{'fraction_non_alphanumeric': 0.09859982375403897, 'fraction_numerical': 0.0013708019191226869, 'mean_word_length': 3.3151668779045207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2543319', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2947, 'n_words': 629}