<commit_before>﻿/// <reference path="globals.ts"/>
define(["require", "exports", "Framework/Signum.Web/Signum/Scripts/Entities"], function(require, exports, Entities) {
    exports.validationSummary = "validaton-summary";
    exports.globalErrorsKey = "sfGlobalErrors";
    exports.globalValidationSummary = "sfGlobalValidationSummary";

    function validate(valOptions) {
        SF.log("validate");

        var options = $.extend({
            prefix: "",
            controllerUrl: SF.Urls.validate,
            requestExtraJsonData: null,
            ajaxError: null,
            errorSummaryId: null
        }, valOptions);

        return SF.ajaxPost({
            url: options.controllerUrl,
            async: false,
            data: constructRequestData(options)
        }).then(function (result) {
            var validatorResult = {
                modelState: result.ModelState,
                isValid: isValid(result.ModelState),
                newToStr: result[Entities.Keys.toStr],
                newLink: result[Entities.Keys.link]
            };
            exports.showErrors(options, validatorResult.modelState);
            return validatorResult;
        });
    }
    exports.validate = validate;

    function constructRequestData(valOptions) {
        SF.log("Validator constructRequestData");

        var formValues = exports.getFormValues(valOptions.prefix, "prefix");

        if (valOptions.rootType)
            formValues["rootType"] = valOptions.rootType;

        if (valOptions.propertyRoute)
            formValues["propertyRoute"] = valOptions.propertyRoute;

        return $.extend(formValues, valOptions.requestExtraJsonData);
    }

    function getFormValues(prefix, prefixRequestKey) {
        var result;
        if (!prefix) {
            result = cleanFormInputs($("form :input")).serializeObject();
        } else {
            var mainControl = prefix.child("divMainControl").get();

            result = cleanFormInputs(mainControl.find(":input")).serializeObject();

            result[prefix.child(Entities.Keys.runtimeInfo)] = mainControl.data("runtimeinfo");

            result = $.extend(result, exports.getFormBasics());
        }

        if (prefixRequestKey)
            result[prefixRequestKey] = prefix;

        return result;
    }
    exports.getFormValues = getFormValues;

    function getFormValuesLite(prefix, prefixRequestKey) {
        var result = exports.getFormBasics();

        result[prefix.child(Entities.Keys.runtimeInfo)] = prefix ? prefix.child("divMainControl").get().data("runtimeinfo") : prefix.child(Entities.Keys.runtimeInfo).get().val();

        if (prefixRequestKey)
            result[prefixRequestKey] = prefix;

        return result;
    }
    exports.getFormValuesLite = getFormValuesLite;

    function getFormValuesHtml(entityHtml, prefixRequestKey) {
        var mainControl = entityHtml.getChild("divMainControl");

        var result = cleanFormInputs(mainControl.find(":input")).serializeObject();

        result[entityHtml.prefix.child(Entities.Keys.runtimeInfo)] = mainControl.data("runtimeinfo");

        if (prefixRequestKey)
            result[prefixRequestKey] = entityHtml.prefix;

        return $.extend(result, exports.getFormBasics());
    }
    exports.getFormValuesHtml = getFormValuesHtml;

    function getFormBasics() {
        return $('#' + Entities.Keys.tabId + ", input:hidden[name=" + Entities.Keys.antiForgeryToken + "]").serializeObject();
    }
    exports.getFormBasics = getFormBasics;

    function cleanFormInputs(form) {
        return form.not(".sf-search-control :input");
    }

    function isModelState(result) {
        return typeof result == "Object" && typeof result.ModelState != "undefined";
    }
    exports.isModelState = isModelState;

    function showErrors(valOptions, modelState) {
        valOptions = $.extend({
            prefix: "",
            showInlineErrors: true,
            fixedInlineErrorText: "*",
            errorSummaryId: null,
            showPathErrors: false
        }, valOptions);

        SF.log("Validator showErrors");

        //Remove previous errors
        $('.' + exports.hasError).removeClass(exports.hasError);
        $('.' + exports.validationSummary).remove();

        var allErrors = [];

        var prefix;
        for (prefix in modelState) {
            if (modelState.hasOwnProperty(prefix)) {
                var errorsArray = modelState[prefix];
                allErrors.push(errorsArray);
                if (prefix != exports.globalErrorsKey) {
                    exports.setHasError($('#' + prefix));
                    setPathErrors(valOptions, prefix, errorsArray);
                } else {
                    setPathErrors(valOptions, valOptions.prefix, errorsArray);
                }
            }
        }

        if (allErrors.length) {
            SF.log("(Errors Validator showErrors): " + allErrors.join(''));
            return false;
        }
        return true;
    }
    exports.showErrors = showErrors;

    exports.hasError = "has-error";
    function cleanHasError($element) {
        exports.errorElement($element).removeClass(exports.hasError);
    }
    exports.cleanHasError = cleanHasError;

    function setHasError($element) {
        exports.errorElement($element).addClass(exports.hasError);
    }
    exports.setHasError = setHasError;

    function errorElement($element) {
        var formGroup = $element.closest(".form-group");
        if (formGroup.length)
            return formGroup;

        return $element;
    }
    exports.errorElement = errorElement;

    //This will mark all the path with the error class, and it will also set summary error entries for the controls more inner than the current one
    function setPathErrors(valOptions, prefix, errorsArray) {
        var partialErrors = errorsArray.map(function (a) {
            return "<li>" + a + "</li>";
        }).join('');

        exports.getPathPrefixes(prefix).forEach(function (currPrefix) {

        var summary = valOptions["errorSummaryId"] ? $('#' + valOptions["errorSummaryId"]) : currPrefix.child(exports.globalValidationSummary).tryGet();


            if (summary.length) {
                var ul = summary.children("ul." + exports.validationSummary);
                if (!ul.length)
                    ul = $('<ul class="' + exports.validationSummary + ' alert alert-danger"></ul>').appendTo(summary);

                ul.append(partialErrors);
            }
            if (currPrefix.length < valOptions.prefix.length) {
                var element = $('#' + currPrefix);

                if (element.length > 0 && !element.hasClass("SF-avoid-child-errors"))
                    exports.setHasError(element);<commit_msg>fix validator<commit_after>﻿/// <reference path="globals.ts"/>
define(["require", "exports", "Framework/Signum.Web/Signum/Scripts/Entities"], function(require, exports, Entities) {
    exports.validationSummary = "validaton-summary";
    exports.globalErrorsKey = "sfGlobalErrors";
    exports.globalValidationSummary = "sfGlobalValidationSummary";

    function validate(valOptions) {
        SF.log("validate");

        var options = $.extend({
            prefix: "",
            controllerUrl: SF.Urls.validate,
            requestExtraJsonData: null,
            ajaxError: null,
            errorSummaryId: null
        }, valOptions);

        return SF.ajaxPost({
            url: options.controllerUrl,
            async: false,
            data: constructRequestData(options)
        }).then(function (result) {
            var validatorResult = {
                modelState: result.ModelState,
                isValid: isValid(result.ModelState),
                newToStr: result[Entities.Keys.toStr],
                newLink: result[Entities.Keys.link]
            };
            exports.showErrors(options, validatorResult.modelState);
            return validatorResult;
        });
    }
    exports.validate = validate;

    function constructRequestData(valOptions) {
        SF.log("Validator constructRequestData");

        var formValues = exports.getFormValues(valOptions.prefix, "prefix");

        if (valOptions.rootType)
            formValues["rootType"] = valOptions.rootType;

        if (valOptions.propertyRoute)
            formValues["propertyRoute"] = valOptions.propertyRoute;

        return $.extend(formValues, valOptions.requestExtraJsonData);
    }

    function getFormValues(prefix, prefixRequestKey) {
        var result;
        if (!prefix) {
            result = cleanFormInputs($("form :input")).serializeObject();
        } else {
            var mainControl = prefix.child("divMainControl").get();

            result = cleanFormInputs(mainControl.find(":input")).serializeObject();

            result[prefix.child(Entities.Keys.runtimeInfo)] = mainControl.data("runtimeinfo");

            result = $.extend(result, exports.getFormBasics());
        }

        if (prefixRequestKey)
            result[prefixRequestKey] = prefix;

        return result;
    }
    exports.getFormValues = getFormValues;

    function getFormValuesLite(prefix, prefixRequestKey) {
        var result = exports.getFormBasics();

        result[prefix.child(Entities.Keys.runtimeInfo)] = prefix ? prefix.child("divMainControl").get().data("runtimeinfo") : prefix.child(Entities.Keys.runtimeInfo).get().val();

        if (prefixRequestKey)
            result[prefixRequestKey] = prefix;

        return result;
    }
    exports.getFormValuesLite = getFormValuesLite;

    function getFormValuesHtml(entityHtml, prefixRequestKey) {
        var mainControl = entityHtml.getChild("divMainControl");

        var result = cleanFormInputs(mainControl.find(":input")).serializeObject();

        result[entityHtml.prefix.child(Entities.Keys.runtimeInfo)] = mainControl.data("runtimeinfo");

        if (prefixRequestKey)
            result[prefixRequestKey] = entityHtml.prefix;

        return $.extend(result, exports.getFormBasics());
    }
    exports.getFormValuesHtml = getFormValuesHtml;

    function getFormBasics() {
        return $('#' + Entities.Keys.tabId + ", input:hidden[name=" + Entities.Keys.antiForgeryToken + "]").serializeObject();
    }
    exports.getFormBasics = getFormBasics;

    function cleanFormInputs(form) {
        return form.not(".sf-search-control :input");
    }

    function isModelState(result) {
        return typeof result == "Object" && typeof result.ModelState != "undefined";
    }
    exports.isModelState = isModelState;

    function showErrors(valOptions, modelState) {
        valOptions = $.extend({
            prefix: "",
            showInlineErrors: true,
            fixedInlineErrorText: "*",
            errorSummaryId: null,
            showPathErrors: false
        }, valOptions);

        SF.log("Validator showErrors");

        //Remove previous errors
        $('.' + exports.hasError).removeClass(exports.hasError);
        $('.' + exports.validationSummary).remove();

        var allErrors = [];

        var prefix;
        for (prefix in modelState) {
            if (modelState.hasOwnProperty(prefix)) {
                var errorsArray = modelState[prefix];
                allErrors.push(errorsArray);
                if (prefix != exports.globalErrorsKey) {
                    exports.setHasError($('#' + prefix));
                    setPathErrors(valOptions, prefix, errorsArray);
                } else {
                    setPathErrors(valOptions, valOptions.prefix, errorsArray);
                }
            }
        }

        if (allErrors.length) {
            SF.log("(Errors Validator showErrors): " + allErrors.join(''));
            return false;
        }
        return true;
    }
    exports.showErrors = showErrors;

    exports.hasError = "has-error";
    function cleanHasError($element) {
        exports.errorElement($element).removeClass(exports.hasError);
    }
    exports.cleanHasError = cleanHasError;

    function setHasError($element) {
        exports.errorElement($element).addClass(exports.hasError);
    }
    exports.setHasError = setHasError;

    function errorElement($element) {
        var formGroup = $element.closest(".form-group");
        if (formGroup.length)
            return formGroup;

        return $element;
    }
    exports.errorElement = errorElement;

    //This will mark all the path with the error class, and it will also set summary error entries for the controls more inner than the current one
    function setPathErrors(valOptions, prefix, errorsArray) {
        var partialErrors = errorsArray.map(function (a) {
            return "<li>" + a + "</li>";
        }).join('');

        exports.getPathPrefixes(prefix).forEach(function (currPrefix) {
            var summary = valOptions["errorSummaryId"] ? $('#' + valOptions["errorSummaryId"]) : currPrefix.child(exports.globalValidationSummary).tryGet();

            if (summary.length) {
                var ul = summary.children("ul." + exports.validationSummary);
                if (!ul.length)
                    ul = $('<ul class="' + exports.validationSummary + ' alert alert-danger"></ul>').appendTo(summary);

                ul.append(partialErrors);
            }
            if (currPrefix.length < valOptions.prefix.length) {
                var element = $('#' + currPrefix);

                if (element.length > 0 && !element.hasClass("SF-avoid-child-errors"))
                    exports.setHasError(element);
{'fraction_non_alphanumeric': 0.11898058971699511, 'fraction_numerical': 0.0001481700992739665, 'mean_word_length': 2.4174683544303797, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 15, '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': '3853800', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3434, 'n_words': 650}