Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

7.4 KB
Raw Blame
@using Grand.Business.Core.Interfaces.Storage
@using Grand.SharedKernel.Extensions
@model string
@inject IDownloadService downloadService
@{
    <link rel="stylesheet" type="text/css" asp-src="@(Grand.SharedUIResources.Constants.WwwRoot)/administration/fineuploader/fineuploader-4.2.2.min.css" />
    <script asp-location="Footer" asp-src="@(Grand.SharedUIResources.Constants.WwwRoot)/administration/fineuploader/jquery.fineuploader-4.2.2.min.js"></script>
    var area = ViewContext.RouteData.Values["area"]?.ToString();
    var randomNumber = CommonHelper.GenerateRandomInteger();
    var clientId = "download" + randomNumber;
    var download = await downloadService.GetDownloadById(Model);

    var disableUrl = Convert.ToBoolean(ViewData["Grand.DownloadEditor.DisableUrl"] ?? false);
}

<script>
    $(document).ready(function() {
        $('#cbUseDownloadURL@(randomNumber)').click(toggleDownloadRecordType@(randomNumber));

        $('#saveDownloadUrl@(randomNumber)').click(function() {
            var downloadUrl = $("#downloadurl@(randomNumber)").val();
            $('#saveDownloadUrl@(randomNumber)').attr('disabled', true);
            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("SaveDownloadUrl", "Download", new { area }))",
                data: addAntiForgeryToken({ "downloadUrl": downloadUrl, DownloadType: '@ViewData["DownloadType"]', ReferenceId: '@ViewData["ReferenceId"]' }),
                success: function (data) {
                    if (data.success) {
                        $('#pnlDownloadURLResult@(randomNumber)').fadeIn("slow").delay(1000).fadeOut("slow");
                        $("#@(clientId + "value") input").val(data.downloadId);
                        $('#saveDownloadUrl@(randomNumber)').attr('disabled', false);
                    }
                    else {
                        alert(data.error);
                        $('#saveDownloadUrl@(randomNumber)').attr('disabled', false);
                    }
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert('Failed to save download object.');
                    $('#saveDownloadUrl@(randomNumber)').attr('disabled', false);
                }
            });
        });

        toggleDownloadRecordType@(randomNumber)();
    });

    function toggleDownloadRecordType@(randomNumber)() {
        if ($('#cbUseDownloadURL@(randomNumber)').is(':checked')) {
            $('#pnlDownloadURL@(randomNumber)').show();
            $('#pnlDownloadFile@(randomNumber)').hide();
        } else {
            $('#pnlDownloadURL@(randomNumber)').hide();
            $('#pnlDownloadFile@(randomNumber)').show();
        }
    }

</script>
<div id="@(clientId + "value")">
    <input asp-for="@Model" type="hidden"/>
</div>
@* The toggle, the url row and the upload row keep every id the script above and the uploader
   below look up (cbUseDownloadURL, pnlDownloadURL, pnlDownloadFile, saveDownloadUrl, ...).
   The two elements the script reveals with jQuery - the "saved" note and the remove button -
   keep an inline display:none, because [hidden] and .d-none are !important and would win
   over fadeIn() and show(). *@
<div class="grand-download grand-fields">
    @if (!disableUrl)
    {
        <div class="form-check form-switch">
            <input type="checkbox" class="form-check-input" role="switch" name="cbUseDownloadURL@(randomNumber)" id="cbUseDownloadURL@(randomNumber)"
                   @if (download is { UseDownloadUrl: true })
                   {
                       <text> checked="checked" </text>
                   }/>
            <label class="form-check-label" for="cbUseDownloadURL@(randomNumber)">@Loc["Admin.Download.UseDownloadURL"]</label>
        </div>
    }
    <div class="grand-field" id="pnlDownloadURL@(randomNumber)">
        <label class="grand-field__label" for="downloadurl@(randomNumber)">@Loc["Admin.Download.DownloadURL"]</label>
        <div class="grand-field__control input-group">
            <input type="text" class="form-control" name="downloadurl@(randomNumber)" id="downloadurl@(randomNumber)"
                   @if (download != null && !string.IsNullOrEmpty(download.DownloadUrl))
                   {
                       <text> value="@(download.DownloadUrl)" </text>
                   }/>
            <input type="button" id="saveDownloadUrl@(randomNumber)" class="btn btn-outline-secondary" value="@Loc["Admin.Download.SaveDownloadURL"]"/>
        </div>
        <div id="pnlDownloadURLResult@(randomNumber)" class="grand-field__hint text-success" style="display:none;">@Loc["Admin.Download.DownloadURLSaved"]</div>
    </div>
    <div class="grand-field" id="pnlDownloadFile@(randomNumber)">
        <span class="grand-field__label">@Loc["Admin.Download.UploadFile"]</span>
        <div>
            @*fine uploader container*@
            <div id="@clientId"></div>
            <div id="@(clientId)-qq-template" style="display: none;">
                <partial name="Partials/QqUploader" />
            </div>
            <script>

                $(document).ready(function () {
                    $("#@clientId").fineUploader({
                        request: {
                            endpoint: '@(Url.Action("AsyncUpload", "Download", new { area }))',
                            params: {
                                DownloadType: '@ViewData["DownloadType"]',
                                ReferenceId: '@ViewData["ReferenceId"]'
                            },
                            inputName: "file",
                            customHeaders: {
                                'X-CSRF-TOKEN': $('input[name="__RequestVerificationToken"]').val()
                            }
                        },
                        template: "@(clientId)-qq-template",
                        multiple: false
                    }).on("complete", function (event, id, name, responseJSON, xhr) {
                        $("#@(clientId + "downloadurl")").html("<a href='" + responseJSON.downloadUrl + "' class=\"btn btn-outline-secondary btn-sm\">@Loc["Admin.Download.DownloadUploadedFile"]</a>");
                        $("#@(clientId + "value") input").val(responseJSON.downloadId);
                        $("#@(clientId + "remove")").show();
                    });


                    $("#@(clientId + "remove")").click(function (e) {
                        $("#@(clientId + "downloadurl")").html("");
                        $("#@(clientId + "value") input").val("");
                        $(this).hide();
                    });
                });

            </script>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <div id="@(clientId + "downloadurl")">
                @if (download != null)
                {
                    <a href="@(Url.Action("DownloadFile", "Download", new { downloadGuid = download.DownloadGuid, area }))" class="btn btn-outline-secondary btn-sm">@Loc["Admin.Download.DownloadUploadedFile"]</a>
                }
            </div>
            @if (download != null)
            {
                <span id="@(clientId + "remove")" class="btn btn-outline-danger btn-sm">@Loc["Admin.Download.RemoveDownload"]</span>
            }
            else
            {
                <span id="@(clientId + "remove")" class="btn btn-outline-danger btn-sm" style="display:none;">@Loc["Admin.Download.RemoveDownload"]</span>
            }
        </div>
    </div>
</div>