<commit_before>﻿@using Microsoft.AspNetCore.Identity
@using DaxnetBlog.Web.Models;
@using DaxnetBlog.Web.Security

@inject SignInManager<User> SignInManager
@{
    var controller = ViewContext.RouteData.Values["controller"].ToString();
    var action = ViewContext.RouteData.Values["action"].ToString();

    var feature = (string)Model.Feature;
    var key = (string)Model.Key;
}
@if (SignInManager.IsSignedIn(User))
{
    <div class="row">
        <div class="col-md-12">
            <div id="errorMessage" class="alert alert-danger">
                <span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>
                <a class="close" data-hide="alert" aria-label="close">&times;</a>
                <span id="txtErrorMessage"></span>
            </div>
            <section>
                <form id="commentForm" asp-controller="Home" asp-action="Reply" asp-route-returnurl="@Url.Action(action, controller)" method="post" class="form-horizontal">
                    <input id="key" type="hidden" name="key" value="@key" />
                    <input id="feature" type="hidden" name="feature" value="@feature" />
                    <div class="form-group">
                        <textarea id="comments" name="comments"></textarea>
                    </div>
                    <div class="form-group">
                        <div class="form-inline">
                            <img asp-captcha="true" />
                            <label for="captcha">请输入左侧验证码：</label>
                            <input type="text" class="form-control" id="captcha" name="captcha" />
                            <button class="btn btn-primary" type="submit" id="submit-btn">发表评论</button>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
}
else
{

    <div class="alert alert-warning">
        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
        请<a href="@Url.Action("Login", "Account", new { returnUrl = Url.Action(action, controller, null, null, null, "gotoComments") })" class="alert-link">【点击此处】</a>登录后发表评论。
    </div>
}

<a name="anchor_comments" id="anchor_comments"></a>

<script src="~/ckeditor/ckeditor.js"></script>
<script language="javascript" type="text/javascript">

</script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        // Hides the alert instead of closing and remove the alert from DOM.
        $("[data-hide]").on("click", function () {
            $("." + $(this).attr("data-hide")).hide();
        });
        $('#errorMessage').hide();

        var editor = CKEDITOR.replace('comments', {
            customConfig: 'config.comment.js'
        });

        editor.on("instanceReady", function () {
            if (window.location.href.indexOf('gotoComments') > -1) {
                document.getElementById('anchor_comments').scrollIntoView(true);
                this.focus();
            }
        })

        $('#commentForm').submit(function (e) {
            e.preventDefault();
            editor.setReadOnly(true);
            $('#captcha').attr("disabled", "disabled");
            $('#submit-btn').attr("disabled", "disabled");
            var isCommentEmpty = !$.trim(editor.getData());
            if (isCommentEmpty) {
                $('#errorMessage').show();
                $('#txtErrorMessage').text('评论内容为空，请输入评论的内容。');
                editor.focus();
                return;
            }
            var captchaString = $('#captcha').val();
            var isCaptchaEmpty = !$.trim(captchaString);
            if (isCaptchaEmpty) {
                $('#errorMessage').show();
                $('#txtErrorMessage').text('请输入验证码。');
                $('#captcha').focus();
                return;
            }

            var encryptedString = $('#__captcha_image').val();
            $.ajax({
                type: "POST",
                url: '@Url.Action("VerifyCaptcha", "Home")',
                data: { captchaString: captchaString, encryptedString: encryptedString },
                success: function (result, status, xhr) {
                    if (!result) {
                        $('#errorMessage').show();
                        $('#txtErrorMessage').text('输入的验证码不正确。');
                        $('#captcha').focus();
                    } else {
                        var comments = editor.getData();
                        alert(comments);
                        @*$.ajax({
                            type: "POST",
                            url: '@Url.Action("Reply", "Home")',
                            data: { feature: feature, key: key, comments: comments },
                            success: function (r, s, x) {
                                alert(r);

                            }
                        });*@
                    }
                },
                error: function (xhr) {
                    $('#errorMessage').show();
                    $('#txtErrorMessage').text(xhr.statusText);
                },
                complete: function (xhr, status) {
                    editor.setReadOnly(false);
                    $('#captcha').removeAttr("disabled");
                    $('#submit-btn').removeAttr("disabled");
                }
            });
        });
    });
</script><commit_msg>Refactor the JS code.<commit_after>﻿@using Microsoft.AspNetCore.Identity
@using DaxnetBlog.Web.Models;
@using DaxnetBlog.Web.Security

@inject SignInManager<User> SignInManager
@{
    var controller = ViewContext.RouteData.Values["controller"].ToString();
    var action = ViewContext.RouteData.Values["action"].ToString();

    var feature = (string)Model.Feature;
    var key = (string)Model.Key;
}
@if (SignInManager.IsSignedIn(User))
{
    <div class="row">
        <div class="col-md-12">
            <div id="errorMessage" class="alert alert-danger">
                <span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>
                <a class="close" data-hide="alert" aria-label="close">&times;</a>
                <span id="txtErrorMessage"></span>
            </div>
            <section>
                <form id="commentForm" asp-controller="Home" asp-action="Reply" asp-route-returnurl="@Url.Action(action, controller)" method="post" class="form-horizontal">
                    <input id="key" type="hidden" name="key" value="@key" />
                    <input id="feature" type="hidden" name="feature" value="@feature" />
                    <div class="form-group">
                        <textarea id="comments" name="comments"></textarea>
                    </div>
                    <div class="form-group">
                        <div class="form-inline">
                            <img asp-captcha="true" />
                            <label for="captcha">请输入左侧验证码：</label>
                            <input type="text" class="form-control" id="captcha" name="captcha" />
                            <button class="btn btn-primary" type="submit" id="submit-btn">发表评论</button>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
}
else
{

    <div class="alert alert-warning">
        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
        请<a href="@Url.Action("Login", "Account", new { returnUrl = Url.Action(action, controller, null, null, null, "gotoComments") })" class="alert-link">【点击此处】</a>登录后发表评论。
    </div>
}

<a name="anchor_comments" id="anchor_comments"></a>

<script src="~/ckeditor/ckeditor.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        // Hides the alert instead of closing and remove the alert from DOM.
        $("[data-hide]").on("click", function () {
            $("." + $(this).attr("data-hide")).hide();
        });

        // Hides the error message by default
        $('#errorMessage').hide();

        // Render the comments editor (ckeditor)
        var editor = CKEDITOR.replace('comments', {
            customConfig: 'config.comment.js'
        });

        editor.on("instanceReady", function () {
            if (window.location.href.indexOf('gotoComments') > -1) {
                document.getElementById('anchor_comments').scrollIntoView(true);
                this.focus();
            }
        })

        // Overrides the submit behavior of the comment form
        $('#commentForm').submit(function (e) {
            e.preventDefault();

            var isCommentEmpty = !$.trim(editor.getData());
            if (isCommentEmpty) {
                $('#errorMessage').show();
                $('#txtErrorMessage').text('评论内容为空，请输入评论的内容。');
                editor.focus();
                return;
            }
            var captchaString = $('#captcha').val();
            var isCaptchaEmpty = !$.trim(captchaString);
            if (isCaptchaEmpty) {
                $('#errorMessage').show();
                $('#txtErrorMessage').text('请输入验证码。');
                $('#captcha').focus();
                return;
            }

            var encryptedString = $('#__captcha_image').val();
            $.ajax({
                type: "POST",
                url: '@Url.Action("VerifyCaptcha", "Home")',
                data: { captchaString: captchaString, encryptedString: encryptedString },
                beforeSend: function (xhr, settings) {
                    editor.setReadOnly(true);
                    $('#captcha').attr("disabled", "disabled");
                    $('#submit-btn').attr("disabled", "disabled");
                }
            })
            .done(function (result, status, xhr) {
                if (!result) {
                    $('#errorMessage').show();
                    $('#txtErrorMessage').text('输入的验证码不正确。');
                    $('#captcha').focus();
                } else {
                    var comments = editor.getData();
                    alert(comments);
                    $.ajax({
                        type: "POST",
                        url: '@Url.Action("Reply", "Home")',
                        data: { feature: feature, key: key, comments: comments },
                        success: function (r, s, x) {
                            alert(r);

                        }
                    });
                }
            })
            .fail(function (xhr) {
                $('#errorMessage').show();
                $('#txtErrorMessage').text(xhr.statusText);
            })
            .always(function (xhr, status) {
                editor.setReadOnly(false);
                $('#captcha').removeAttr("disabled");
                $('#submit-btn').removeAttr("disabled");
            });
        });
    });
</script>
{'fraction_non_alphanumeric': 0.16065331733874688, 'fraction_numerical': 0.0005536587616499031, 'mean_word_length': 1.6421257922964407, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 97, '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': '5324017', 'n_tokens_mistral': 2930, 'n_tokens_neox': 2852, 'n_words': 504}