<commit_before>'use strict';

var $ = require('jquery');
var bootbox = require('bootbox');
var Raven = require('raven-js');
var ko = require('knockout');

var ProjectSettings = require('js/projectSettings.js');

var $osf = require('js/osfHelpers');
require('css/addonsettings.css');

var ctx = window.contextVars;


// Initialize treebeard grid for notifications
var ProjectNotifications = require('js/notificationsTreebeard.js');
var $notificationsMsg = $('#configureNotificationsMessage');
var notificationsURL = ctx.node.urls.api  + 'subscriptions/';
// Need check because notifications settings don't exist on registration's settings page
if ($('#grid').length) {
    $.ajax({
        url: notificationsURL,
        type: 'GET',
        dataType: 'json'
    }).done(function(response) {
        new ProjectNotifications(response);
    }).fail(function(xhr, status, error) {
        $notificationsMsg.addClass('text-danger');
        $notificationsMsg.text('Could not retrieve notification settings.');
        Raven.captureMessage('Could not GET notification settings.', {
            url: notificationsURL, status: status, error: error
        });
    });
}

//Initialize treebeard grid for wiki
var ProjectWiki = require('js/wikiSettingsTreebeard.js');
var wikiSettingsURL = ctx.node.urls.api  + 'wiki/settings/';
var $wikiMsg = $('#configureWikiMessage');

if ($('#wgrid').length) {
    $.ajax({
        url: wikiSettingsURL,
        type: 'GET',
        dataType: 'json'
    }).done(function(response) {
        new ProjectWiki(response);
    }).fail(function(xhr, status, error) {
        $wikiMsg.addClass('text-danger');
        $wikiMsg.text('Could not retrieve wiki settings.');
        Raven.captureMessage('Could not GET wiki settings.', {
            url: wikiSettingsURL, status: status, error: error
        });
    });
}


var institutionsViewModel = function() {
    ko.punches.enableAll();
    var self = this;
    self.primaryInstitution = ko.observable('None');
    self.institutionHref = ko.observable('');
    self.availableInstitutions = ko.observable();
    $osf.ajaxJSON(
        'GET',
        ctx.apiV2Prefix + 'users/' + ctx.currentUser.id + '/?embed=institutions',
        {isCors: true}
    ).done(function(response) {
        self.availableInstitutions(response.data.embeds.institutions.data);
    });
    $osf.ajaxJSON(
        'GET',
        ctx.apiV2Prefix + 'nodes/' + ctx.node.id + '/institution/',
        {isCors: true}
    ).done(function(response) {
        if (response.data.attributes){
            self.primaryInstitution(response.data.attributes.name);
            self.institutionHref(response.data.links.html);
        }
    }).fail(function(){});
    self.submitInst = function() {
        var inst = $('input[name=primaryInst]:checked', '#selectedInst').val();
        $osf.ajaxJSON(
            'PUT',
            ctx.apiV2Prefix + 'nodes/' + ctx.node.id + '/relationships/institution/',
            {
                'isCors': true,
                'data': {
                     'data': {'type': 'institutions', 'id': inst}
                },
                fields: {xhrFields: {withCredentials: true}}
            }
        ).done(function (response) {
            window.location.reload();
        }).fail(function (response) {
            $osf.growl('Error!');
        });
    };
    self.clearInst = function() {
        $osf.ajaxJSON(
            'PUT',<commit_msg>prevent 404 response on settings page when no instituion affiliated<commit_after>'use strict';

var $ = require('jquery');
var bootbox = require('bootbox');
var Raven = require('raven-js');
var ko = require('knockout');

var ProjectSettings = require('js/projectSettings.js');

var $osf = require('js/osfHelpers');
require('css/addonsettings.css');

var ctx = window.contextVars;


// Initialize treebeard grid for notifications
var ProjectNotifications = require('js/notificationsTreebeard.js');
var $notificationsMsg = $('#configureNotificationsMessage');
var notificationsURL = ctx.node.urls.api  + 'subscriptions/';
// Need check because notifications settings don't exist on registration's settings page
if ($('#grid').length) {
    $.ajax({
        url: notificationsURL,
        type: 'GET',
        dataType: 'json'
    }).done(function(response) {
        new ProjectNotifications(response);
    }).fail(function(xhr, status, error) {
        $notificationsMsg.addClass('text-danger');
        $notificationsMsg.text('Could not retrieve notification settings.');
        Raven.captureMessage('Could not GET notification settings.', {
            url: notificationsURL, status: status, error: error
        });
    });
}

//Initialize treebeard grid for wiki
var ProjectWiki = require('js/wikiSettingsTreebeard.js');
var wikiSettingsURL = ctx.node.urls.api  + 'wiki/settings/';
var $wikiMsg = $('#configureWikiMessage');

if ($('#wgrid').length) {
    $.ajax({
        url: wikiSettingsURL,
        type: 'GET',
        dataType: 'json'
    }).done(function(response) {
        new ProjectWiki(response);
    }).fail(function(xhr, status, error) {
        $wikiMsg.addClass('text-danger');
        $wikiMsg.text('Could not retrieve wiki settings.');
        Raven.captureMessage('Could not GET wiki settings.', {
            url: wikiSettingsURL, status: status, error: error
        });
    });
}


var institutionsViewModel = function() {
    ko.punches.enableAll();
    var self = this;
    self.primaryInstitution = ko.observable('None');
    self.institutionHref = ko.observable('');
    self.availableInstitutions = ko.observable();
    $osf.ajaxJSON(
        'GET',
        ctx.apiV2Prefix + 'users/' + ctx.currentUser.id + '/?embed=institutions',
        {isCors: true}
    ).done(function(response) {
        self.availableInstitutions(response.data.embeds.institutions.data);
    });
    $osf.ajaxJSON(
        'GET',
        ctx.apiV2Prefix + 'nodes/' + ctx.node.id + '/?embed=primary_institution',
        {isCors: true}
    ).done(function(response) {
        if (response.data.embeds.primary_institution.data){
            self.primaryInstitution(response.data.embeds.primary_institution.data.attributes.name);
            self.institutionHref(response.data.embeds.primary_institution.data.links.html);
        }
    }).fail(function(){});
    self.submitInst = function() {
        var inst = $('input[name=primaryInst]:checked', '#selectedInst').val();
        $osf.ajaxJSON(
            'PUT',
            ctx.apiV2Prefix + 'nodes/' + ctx.node.id + '/relationships/institution/',
            {
                'isCors': true,
                'data': {
                     'data': {'type': 'institutions', 'id': inst}
                },
                fields: {xhrFields: {withCredentials: true}}
            }
        ).done(function (response) {
            window.location.reload();
        }).fail(function (response) {
            $osf.growl('Error!');
        });
    };
    self.clearInst = function() {
        $osf.ajaxJSON(
            'PUT',
{'fraction_non_alphanumeric': 0.15122090532628873, 'fraction_numerical': 0.0012851635013565615, 'mean_word_length': 2.7354666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1078866', 'n_tokens_mistral': 2052, 'n_tokens_neox': 2009, 'n_words': 406}