<commit_before>/**
 * Created by Mauriel on 2/9/2016.
 */

function label_ajax_submit() {
    var el = $(this);
    var dataFormID = el.attr("data-form-id");
    var formID = $("form[data-id='" + dataFormID + "']");
    var form = $(formID);
    var datastring = form.serialize();
    var url = form.attr('action');

    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        data: datastring,
        success: function (result) {
            var json_response = JSON.parse(result);
            if (json_response.status == "success") {

                var action = form.find("input[name='action']");
                if (json_response.action == "CREATE") {
                    action.val("DELETE");
                    $("#btnMyResources").removeClass("btn-resource-add");
                    $("#btnMyResources").addClass("btn-resource-remove");
                    $("#btnMyResources").attr("title", "Remove from my resources");
                }
                else {
                    action.val("CREATE");
                    $("#btnMyResources").addClass("btn-resource-add");
                    $("#btnMyResources").removeClass("btn-resource-remove");
                    $("#btnMyResources").attr("title", "Add to my resources");
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

        }
    });
    //don't submit the form
    return false;
}


function shareable_ajax_submit(event) {
    var form = $(this).closest("form");
    var datastring = form.serialize();
    var url = form.attr('action');
    var element = $(this);
    var action = $(this).closest("form").find("input[name='t']").val();
    element.attr("disabled", true);


    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        data: datastring,
        success: function () {
            element.attr("disabled", false);
            if (action == "make_not_shareable") {
                element.closest("form").find("input[name='t']").val("make_shareable");
            }
            else {
                element.closest("form").find("input[name='t']").val("make_not_shareable");
            }
        },
        error: function () {
            element.attr("disabled", false);
        }
    });
    //don't submit the form
    return false;
}

function unshare_resource_ajax_submit(form_id, check_for_prompt, remove_permission) {
    if (typeof check_for_prompt === 'undefined'){
        check_for_prompt = true;
    }
    if (typeof  remove_permission === 'undefined'){
        remove_permission = true;
    }
    if (check_for_prompt){
        if (!promptSelfRemovingAccess(form_id)){
            return;
        }
    }
    if (!remove_permission){
        return;
    }

    $form = $('#' + form_id);
    var datastring = $form.serialize();
    var url = $form.attr('action');
    setPointerEvents(false);
    $form.parent().closest("tr").addClass("loading");
    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        data: datastring,
        success: function (result) {
            var json_response = JSON.parse(result);
            if (json_response.status == "success") {
                if (json_response.hasOwnProperty('redirect_to')){
                    window.location.href = json_response.redirect_to;
                }
                $form.parent().closest("tr").remove();
                if ($(".access-table li.active[data-access-type='Is owner']").length == 1) {
                    $(".access-table li.active[data-access-type='Is owner']").closest("tr").addClass("hide-actions");
                }
                setPointerEvents(true);
            }
            else {
                $("#div-invite-people").find(".label-danger").remove(); // Remove previous alerts
                $("#div-invite-people").append("<span class='label label-danger'><strong>Error: </strong>" + json_response.message + "</span>");
                setPointerEvents(true);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $("#div-invite-people").find(".label-danger").remove(); // Remove previous alerts
            $("#div-invite-people").append("<span class='label label-danger'><strong>Error: </strong>" + errorThrown + "</span>");
            setPointerEvents(true);
        }
    });
    //don't submit the form
    return false;
}

function promptSelfRemovingAccess(form_id){
    $form = $('#' + form_id);
    var url = $form.attr('action');
    // check if we are unsharing a user or a group
    var isUserUnsharing = false;
    if(url.indexOf("unshare-resource-with-user") > 0){
        isUserUnsharing = true;
    }
    if(!isUserUnsharing){
        return true;
    }
    var formIDParts = form_id.split('-');
    var userID = parseInt(formIDParts[formIDParts.length -1]);
    var currentUserID = parseInt($("#current-user-id").val());
    if (currentUserID != userID){
        // no need to prompt for confirmation since self is not unsharing
        return true;
    }

    // close the manage access panel (modal)
    $("#manage-access .btn-primary").click();

    // display remove access confirmation dialog
    $("#dialog-confirm-delete-self-access").dialog({
        resizable: false,
        height: "auto",
        width: 500,
        modal: true,
        dialogClass: 'noclose',
        buttons: {
            Cancel: function () {
                $(this).dialog("close");
                // show manage access control panel again
                $("#manage-access").modal('show');
                unshare_resource_ajax_submit(form_id, false, false);
            },
            "Remove": function () {
                $(this).dialog("close");
                unshare_resource_ajax_submit(form_id, false, true);
            }
        },
        open: function () {
            $(this).closest(".ui-dialog")
                .find(".ui-dialog-buttonset button:first") // the first button
                .addClass("btn btn-default");

            $(this).closest(".ui-dialog")<commit_msg>[#1146] made the modal not draggable<commit_after>/**
 * Created by Mauriel on 2/9/2016.
 */

function label_ajax_submit() {
    var el = $(this);
    var dataFormID = el.attr("data-form-id");
    var formID = $("form[data-id='" + dataFormID + "']");
    var form = $(formID);
    var datastring = form.serialize();
    var url = form.attr('action');

    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        data: datastring,
        success: function (result) {
            var json_response = JSON.parse(result);
            if (json_response.status == "success") {

                var action = form.find("input[name='action']");
                if (json_response.action == "CREATE") {
                    action.val("DELETE");
                    $("#btnMyResources").removeClass("btn-resource-add");
                    $("#btnMyResources").addClass("btn-resource-remove");
                    $("#btnMyResources").attr("title", "Remove from my resources");
                }
                else {
                    action.val("CREATE");
                    $("#btnMyResources").addClass("btn-resource-add");
                    $("#btnMyResources").removeClass("btn-resource-remove");
                    $("#btnMyResources").attr("title", "Add to my resources");
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

        }
    });
    //don't submit the form
    return false;
}


function shareable_ajax_submit(event) {
    var form = $(this).closest("form");
    var datastring = form.serialize();
    var url = form.attr('action');
    var element = $(this);
    var action = $(this).closest("form").find("input[name='t']").val();
    element.attr("disabled", true);


    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        data: datastring,
        success: function () {
            element.attr("disabled", false);
            if (action == "make_not_shareable") {
                element.closest("form").find("input[name='t']").val("make_shareable");
            }
            else {
                element.closest("form").find("input[name='t']").val("make_not_shareable");
            }
        },
        error: function () {
            element.attr("disabled", false);
        }
    });
    //don't submit the form
    return false;
}

function unshare_resource_ajax_submit(form_id, check_for_prompt, remove_permission) {
    if (typeof check_for_prompt === 'undefined'){
        check_for_prompt = true;
    }
    if (typeof  remove_permission === 'undefined'){
        remove_permission = true;
    }
    if (check_for_prompt){
        if (!promptSelfRemovingAccess(form_id)){
            return;
        }
    }
    if (!remove_permission){
        return;
    }

    $form = $('#' + form_id);
    var datastring = $form.serialize();
    var url = $form.attr('action');
    setPointerEvents(false);
    $form.parent().closest("tr").addClass("loading");
    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        data: datastring,
        success: function (result) {
            var json_response = JSON.parse(result);
            if (json_response.status == "success") {
                if (json_response.hasOwnProperty('redirect_to')){
                    window.location.href = json_response.redirect_to;
                }
                $form.parent().closest("tr").remove();
                if ($(".access-table li.active[data-access-type='Is owner']").length == 1) {
                    $(".access-table li.active[data-access-type='Is owner']").closest("tr").addClass("hide-actions");
                }
                setPointerEvents(true);
            }
            else {
                $("#div-invite-people").find(".label-danger").remove(); // Remove previous alerts
                $("#div-invite-people").append("<span class='label label-danger'><strong>Error: </strong>" + json_response.message + "</span>");
                setPointerEvents(true);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $("#div-invite-people").find(".label-danger").remove(); // Remove previous alerts
            $("#div-invite-people").append("<span class='label label-danger'><strong>Error: </strong>" + errorThrown + "</span>");
            setPointerEvents(true);
        }
    });
    //don't submit the form
    return false;
}

function promptSelfRemovingAccess(form_id){
    $form = $('#' + form_id);
    var url = $form.attr('action');
    // check if we are unsharing a user or a group
    var isUserUnsharing = false;
    if(url.indexOf("unshare-resource-with-user") > 0){
        isUserUnsharing = true;
    }
    if(!isUserUnsharing){
        return true;
    }
    var formIDParts = form_id.split('-');
    var userID = parseInt(formIDParts[formIDParts.length -1]);
    var currentUserID = parseInt($("#current-user-id").val());
    if (currentUserID != userID){
        // no need to prompt for confirmation since self is not unsharing
        return true;
    }

    // close the manage access panel (modal)
    $("#manage-access .btn-primary").click();

    // display remove access confirmation dialog
    $("#dialog-confirm-delete-self-access").dialog({
        resizable: false,
        draggable: false,
        height: "auto",
        width: 500,
        modal: true,
        dialogClass: 'noclose',
        buttons: {
            Cancel: function () {
                $(this).dialog("close");
                // show manage access control panel again
                $("#manage-access").modal('show');
                unshare_resource_ajax_submit(form_id, false, false);
            },
            "Remove": function () {
                $(this).dialog("close");
                unshare_resource_ajax_submit(form_id, false, true);
            }
        },
        open: function () {
            $(this).closest(".ui-dialog")
                .find(".ui-dialog-buttonset button:first") // the first button
                .addClass("btn btn-default");

            $(this).closest(".ui-dialog")
{'fraction_non_alphanumeric': 0.15793774001143884, 'fraction_numerical': 0.0022877686085464497, 'mean_word_length': 1.9868228404099562, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 21, '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': '511256', 'n_tokens_mistral': 3466, 'n_tokens_neox': 3348, 'n_words': 674}