<commit_before>'use strict';

var ko = require('knockout');
var $ = require('jquery');
var Raven = require('raven-js');
var koHelpers = require('js/koHelpers');
var $osf = require('js/osfHelpers');

var MESSAGE_TIMEOUT = 5000;

/**
 * Knockout view model for the Forward node settings widget.
 */
var ViewModel = function(node, enabled, url, label) {

    var self = this;

    var forwardUrl = $osf.apiV2Url('nodes/' + node.id + '/addons/forward/');

    self.enabled = ko.observable(enabled);
    self.label = koHelpers.sanitizedObservable(label);
    self.url = ko.observable(url).extend({
        ensureHttp: true,
        url: true,
        required: true
    });

    // Flashed messages
    self.message = ko.observable('');
    self.messageClass = ko.observable('text-info');

    self.pendingRequest = ko.observable(false);

    ko.validation.addAnonymousRule(
        self.url,
        koHelpers.makeRegexValidator(
            new RegExp(nodeId, 'i'),
            'Components cannot link to themselves',
            false
        )
    );

    self.validators = ko.validatedObservable({
        url: self.url,
    });

    self.enabled.subscribe(function(newValue) {
        self.pendingRequest(true);
        if (!newValue) {
            $osf.ajaxJSON(
                'delete',
                forwardUrl,
                {'isCors': true}
            ).done(function(response) {
                self.pendingRequest(false);
            }).fail(function(xhr, status, error) {
                $osf.growl('Error', 'Unable to disable redirect link.');
                Raven.captureMessage('Error disabling redirect link.', {
                    extra: {
                        url: forwardUrl, status: status, error: error
                    }
                });
            });
        } else {
            $osf.ajaxJSON(
                'post',
                forwardUrl,
                {
                    'data': {
                        'data': {
                            'id': 'forward',
                            'type': 'node_addons',
                            'attributes': {}
                        }
                    },
                    'isCors': true
                }
            ).done(function(response) {
                self.pendingRequest(false);
            }).fail(function(xhr, status, error) {
                $osf.growl('Error', 'Unable to enable redirect link.');
                Raven.captureMessage('Error enabling redirect link.', {
                    extra: {
                        url: forwardUrl, status: status, error: error
                    }
                });
            });
        }
    });

    function onSubmitSuccess() {
        if (self.url() == null) {
            self.changeMessage(
                'Please fill in the required field.',
                'text-danger'
            );
        }
        else {
            self.changeMessage(
                'Successfully linked to <a href="' + self.url() + '">' + self.url() + '</a>.',
                'text-success',
                MESSAGE_TIMEOUT
            );
        }
    }

    function onSubmitError(xhr, status) {
        var re = /^(https?):\/{2}/;
        var b = self.url().replace(re, '');
        if (b == '') {
            self.changeMessage(
                'Please fill in the required field.',
                'text-danger'
            );
        }
        else {
            self.changeMessage(
                'Could not change redirect link settings. Please try again later.',
                'text-danger'
            );
            Raven.captureMessage('Error updating redirect link.', {
                extra: {
                    url: forwardUrl, status: status, error: error
                }
            });
        }
    }

    /**
     * Submit new settings.
     */
    self.submitSettings = function() {
        self.pendingRequest(true);
        $osf.ajaxJSON(
            'put',
            forwardUrl,
            {
                'data': {
                    'data': {
                        'id': 'forward',
                        'type': 'node_addons',
                        'attributes': {
                            'url': self.url(),
                            'label': self.label()
                        }
                    }
                },
                'isCors': true
            }
        ).done(function(response) {
            onSubmitSuccess()
            self.pendingRequest(false);
        }).fail(function(response) {
            onSubmitError()
        });
    };

    /** Change the flashed message. */
    self.changeMessage = function(text, css, timeout) {
        self.message(text);
        var cssClass = css || 'text-info';
        self.messageClass(cssClass);
        if (timeout) {
            // Reset message after timeout period
            setTimeout(function() {
                self.message('');
                self.messageClass('text-info');<commit_msg>Set pendingRequest to false on request failure.<commit_after>'use strict';

var ko = require('knockout');
var $ = require('jquery');
var Raven = require('raven-js');
var koHelpers = require('js/koHelpers');
var $osf = require('js/osfHelpers');

var MESSAGE_TIMEOUT = 5000;

/**
 * Knockout view model for the Forward node settings widget.
 */
var ViewModel = function(node, enabled, url, label) {

    var self = this;

    var forwardUrl = $osf.apiV2Url('nodes/' + node.id + '/addons/forward/');

    self.enabled = ko.observable(enabled);
    self.label = koHelpers.sanitizedObservable(label);
    self.url = ko.observable(url).extend({
        ensureHttp: true,
        url: true,
        required: true
    });

    // Flashed messages
    self.message = ko.observable('');
    self.messageClass = ko.observable('text-info');

    self.pendingRequest = ko.observable(false);

    ko.validation.addAnonymousRule(
        self.url,
        koHelpers.makeRegexValidator(
            new RegExp(nodeId, 'i'),
            'Components cannot link to themselves',
            false
        )
    );

    self.validators = ko.validatedObservable({
        url: self.url,
    });

    self.enabled.subscribe(function(newValue) {
        self.pendingRequest(true);
        if (!newValue) {
            $osf.ajaxJSON(
                'delete',
                forwardUrl,
                {'isCors': true}
            ).done(function(response) {
                self.pendingRequest(false);
            }).fail(function(xhr, status, error) {
                $osf.growl('Error', 'Unable to disable redirect link.');
                Raven.captureMessage('Error disabling redirect link.', {
                    extra: {
                        url: forwardUrl, status: status, error: error
                    }
                });
            });
        } else {
            $osf.ajaxJSON(
                'post',
                forwardUrl,
                {
                    'data': {
                        'data': {
                            'id': 'forward',
                            'type': 'node_addons',
                            'attributes': {}
                        }
                    },
                    'isCors': true
                }
            ).done(function(response) {
                self.pendingRequest(false);
            }).fail(function(xhr, status, error) {
                $osf.growl('Error', 'Unable to enable redirect link.');
                Raven.captureMessage('Error enabling redirect link.', {
                    extra: {
                        url: forwardUrl, status: status, error: error
                    }
                });
            });
        }
    });

    function onSubmitSuccess() {
        if (self.url() == null) {
            self.changeMessage(
                'Please fill in the required field.',
                'text-danger'
            );
        }
        else {
            self.changeMessage(
                'Successfully linked to <a href="' + self.url() + '">' + self.url() + '</a>.',
                'text-success',
                MESSAGE_TIMEOUT
            );
        }
    }

    function onSubmitError(xhr, status) {
        var re = /^(https?):\/{2}/;
        var b = self.url().replace(re, '');
        if (b == '') {
            self.changeMessage(
                'Please fill in the required field.',
                'text-danger'
            );
        }
        else {
            self.changeMessage(
                'Could not change redirect link settings. Please try again later.',
                'text-danger'
            );
            Raven.captureMessage('Error updating redirect link.', {
                extra: {
                    url: forwardUrl, status: status, error: error
                }
            });
        }
    }

    /**
     * Submit new settings.
     */
    self.submitSettings = function() {
        self.pendingRequest(true);
        $osf.ajaxJSON(
            'put',
            forwardUrl,
            {
                'data': {
                    'data': {
                        'id': 'forward',
                        'type': 'node_addons',
                        'attributes': {
                            'url': self.url(),
                            'label': self.label()
                        }
                    }
                },
                'isCors': true
            }
        ).done(function(response) {
            onSubmitSuccess()
            self.pendingRequest(false);
        }).fail(function(response) {
            onSubmitError()
            self.pendingRequest(false);
        });
    };

    /** Change the flashed message. */
    self.changeMessage = function(text, css, timeout) {
        self.message(text);
        var cssClass = css || 'text-info';
        self.messageClass(cssClass);
        if (timeout) {
            // Reset message after timeout period
            setTimeout(function() {
                self.message('');
                self.messageClass('text-info');
{'fraction_non_alphanumeric': 0.1239017571884984, 'fraction_numerical': 0.0011980830670926517, 'mean_word_length': 1.233942908117752, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5815912', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2492, 'n_words': 534}