<commit_before>/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var scriptIsDirty = false;
var fromTemplateLink = false;
var fromStepsAddLink = false;
var idpNumber = 0;
var reqPathAuth = 0;
var localAuthNumber = 0;
var scriptStringHeader = "function onInitialRequest(context) {";
var scriptStringContent = [];
var scriptStringFooter = "}";
var documentBeforeChange;

$("#createApp").click(function () {
    validateAppCreation();
});

var addTemplate = $("#addTemplate");
var myCodeMirror = CodeMirror.fromTextArea(scriptTextArea, {
    theme: "mdn-like",
    keyMap: "sublime",
    mode: "javascript",
    lineNumbers: true,
    lineWrapping: true,
    lineWiseCopyCut: true,
    pasteLinesPerSelection: true,
    extraKeys: {
        "Ctrl-Space": "autocomplete",
        "F11": function (myCodeMirror) {
            myCodeMirror.setOption("fullScreen", !myCodeMirror.getOption("fullScreen"));
        },
        "Esc": function (myCodeMirror) {
            if (myCodeMirror.getOption("fullScreen")) myCodeMirror.setOption("fullScreen", false);
        },
        "Shift-Ctrl-F": function (myCodeMirror) {
            CodeMirror.commands["selectAll"](myCodeMirror);
            autoFormatSelection(myCodeMirror);
        }
    },
    indentWithTabs: true,
    autoCloseBrackets: true,
    matchBrackets: true,
    gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
    foldGutter: true,
    lint: true,
    showCursorWhenSelecting: true,
    styleActiveLine: true,
});
var doc = myCodeMirror.getDoc();
var editorContent = doc.getValue();

function validateAppCreation() {
    var warnCount = 0;
    var errorCount = 0;
    myCodeMirror.operation(function () {
        JSHINT(myCodeMirror.getValue());
        for (var i = 0; i < JSHINT.errors.length; ++i) {
            var err = JSHINT.errors[i];
            if (!err) {
                continue;
            } else if (err.code.lastIndexOf("W", 0) === 0) {
                warnCount++;
            } else {
                errorCount++;
            }
        }
    });

    if (!scriptIsDirty) {
        if(checkEmptyStep()){
            CARBON.showConfirmationDialog('Some authentication steps do not have authenticators added. Update anyway?',
                checkStepSubmit, null);
        } else {
            submitFormWithDIsabledScript();
        }
    } else {
        var stepsInUI = getExecuteStepsInUI();
        var stepsInScript = getExecuteStepsInScript();

        if (stepsInUI.length < stepsInScript.length) {
            CARBON.showConfirmationDialog('Total number of steps are smaller than that of the Script.',
                submitFormWithDIsabledScript, null);
        } else if (stepsInUI.length > stepsInScript.length) {
            CARBON.showConfirmationDialog('Total number of steps are greater than that of the Script.',
                submitFormWithEnabledScript, null);
        } else if (stepsInUI.length == stepsInScript.length) {
            submitFormWithEnabledScript();
        }

        if (errorCount > 0) {
            CARBON.showConfirmationDialog('Save script with errors? (Script will not be evaluated and will only be' +
                ' saved)',
                submitFormWithDIsabledScript, null);
        }
        if (warnCount > 0) {
            CARBON.showConfirmationDialog('Save script with warnings? (Script will be saved and evaluated)',
                submitFormWithEnabledScript, null);
        }

    }
}

function checkEmptyStep() {
    var isEmptyStep = false;
    $.each($('.step_body'), function () {
        if($(this).has(".auth_table > tbody > tr").length == 0) {
            isEmptyStep = true;
            return false;
        }
    });
    return isEmptyStep;
}

function checkStepSubmit() {
    var stepElementWithAuth = $('.step_body .auth_table > tbody > tr');
    buildScriptString(stepElementWithAuth);
    submitFormWithDIsabledScript();
}

function submitFormWithDIsabledScript() {
    $("#enableScript").prop("checked", false);
    $("#configure-auth-flow-form").submit();
}

function submitFormWithEnabledScript() {
    $("#enableScript").prop("checked", true);
    $("#configure-auth-flow-form").submit();
}<commit_msg>Resolving comments- Add Authentication Step validation<commit_after>/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

var scriptIsDirty = false;
var fromTemplateLink = false;
var fromStepsAddLink = false;
var idpNumber = 0;
var reqPathAuth = 0;
var localAuthNumber = 0;
var scriptStringHeader = "function onInitialRequest(context) {";
var scriptStringContent = [];
var scriptStringFooter = "}";
var documentBeforeChange;

$("#createApp").click(function () {
    validateAppCreation();
});

var addTemplate = $("#addTemplate");
var myCodeMirror = CodeMirror.fromTextArea(scriptTextArea, {
    theme: "mdn-like",
    keyMap: "sublime",
    mode: "javascript",
    lineNumbers: true,
    lineWrapping: true,
    lineWiseCopyCut: true,
    pasteLinesPerSelection: true,
    extraKeys: {
        "Ctrl-Space": "autocomplete",
        "F11": function (myCodeMirror) {
            myCodeMirror.setOption("fullScreen", !myCodeMirror.getOption("fullScreen"));
        },
        "Esc": function (myCodeMirror) {
            if (myCodeMirror.getOption("fullScreen")) myCodeMirror.setOption("fullScreen", false);
        },
        "Shift-Ctrl-F": function (myCodeMirror) {
            CodeMirror.commands["selectAll"](myCodeMirror);
            autoFormatSelection(myCodeMirror);
        }
    },
    indentWithTabs: true,
    autoCloseBrackets: true,
    matchBrackets: true,
    gutters: ["CodeMirror-lint-markers", "CodeMirror-linenumbers", "CodeMirror-foldgutter"],
    foldGutter: true,
    lint: true,
    showCursorWhenSelecting: true,
    styleActiveLine: true,
});
var doc = myCodeMirror.getDoc();
var editorContent = doc.getValue();

function validateAppCreation() {
    var warnCount = 0;
    var errorCount = 0;
    myCodeMirror.operation(function () {
        JSHINT(myCodeMirror.getValue());
        for (var i = 0; i < JSHINT.errors.length; ++i) {
            var err = JSHINT.errors[i];
            if (!err) {
                continue;
            } else if (err.code.lastIndexOf("W", 0) === 0) {
                warnCount++;
            } else {
                errorCount++;
            }
        }
    });

    if(checkEmptyStep()){
        CARBON.showErrorDialog('Some authentication steps do not have authenticators. Add' +
            ' missing authenticators or delete the empty step.',
            null, null);
    } else {
        if (!scriptIsDirty) {
            submitFormWithDisabledScript();
        } else {
            var stepsInUI = getExecuteStepsInUI();
            var stepsInScript = getExecuteStepsInScript();

            if (stepsInUI.length < stepsInScript.length) {
                CARBON.showConfirmationDialog('Total number of steps are smaller than that of the Script.',
                    submitFormWithDisabledScript, null);
            } else if (stepsInUI.length > stepsInScript.length) {
                CARBON.showConfirmationDialog('Total number of steps are greater than that of the Script.',
                    submitFormWithEnabledScript, null);
            } else if (stepsInUI.length == stepsInScript.length) {
                submitFormWithEnabledScript();
            }

            if (errorCount > 0) {
                CARBON.showConfirmationDialog('Save script with errors? (Script will not be evaluated and will only be' +
                    ' saved)',
                    submitFormWithDisabledScript, null);
            }
            if (warnCount > 0) {
                CARBON.showConfirmationDialog('Save script with warnings? (Script will be saved and evaluated)',
                    submitFormWithEnabledScript, null);
            }
        }
    }
}

function checkEmptyStep() {
    var isEmptyStep = false;
    $.each($('.step_body'), function () {
        if($(this).has(".auth_table > tbody > tr").length == 0) {
            isEmptyStep = true;
            return false;
        }
    });
    return isEmptyStep;
}

function submitFormWithDisabledScript() {
    $("#enableScript").prop("checked", false);
    $("#configure-auth-flow-form").submit();
}

function submitFormWithEnabledScript() {
    $("#enableScript").prop("checked", true);
    $("#configure-auth-flow-form").submit();
}
{'fraction_non_alphanumeric': 0.115372589890568, 'fraction_numerical': 0.0050026055237102655, 'mean_word_length': 2.6766283524904213, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3640224', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2629, 'n_words': 704}