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.

4.3 KB
Raw Blame
@using Grand.Business.Core.Interfaces.Common.Security
@using Grand.Domain.Permissions
@using Grand.SharedKernel.Extensions

@model string
@inject IPermissionService permissionService
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery antiforgery
@{
    var allowFileman = await permissionService.Authorize(StandardPermission.HtmlEditorManagePictures);
    var random = CommonHelper.GenerateRandomInteger();
    //the editor is not always rendered inside a form, so the token comes from IAntiforgery
    //(which also issues the cookie) rather than from a __RequestVerificationToken input
    var requestToken = antiforgery.GetAndStoreTokens(Context).RequestToken;
}
<div id="s-@(random)-summernote" class="summernote" data-name="null" data-file="null" data-format="null">
    @Html.Raw(ViewData.TemplateInfo.FormattedModelValue)
</div>
@Html.TextArea(string.Empty, /* Name suffix */
    ViewData.TemplateInfo.FormattedModelValue /* Initial value */
)
<script>
    function elfinderDialog@(random)(context) { // <------------------ +context
        var fm = $('<div/>').dialogelfinder({
            url: '@Url.Action("Connector", "ElFinder")',
            customHeaders: {
                'X-CSRF-TOKEN': '@requestToken'
            },
            baseUrl: '@(Grand.SharedUIResources.Constants.WwwRoot)/administration/elfinder/',
            lang: 'en',
            width: 840,
            height: 450,
            destroyOnClose: true,
            uiOptions: { // UI buttons available to the user
                toolbar: [
                    ['home', 'up'],
                    ['mkdir', 'mkfile', 'upload'],
                    ['open', 'download'],
                    ['copy', 'cut', 'paste'],
                    ['rm'],
                    ['duplicate', 'rename', 'edit'],
                    ['view', 'sort']
                ]
            },
            requestType: 'post',
            getFileCallback: function (file, fm) {
                var fulltype = file.mime;
                if (fulltype.includes("video")) {
                    var node = document.createElement('iframe');
                    node.src = file.url;
                    $('#s-@(random)-summernote').summernote('editor.insertNode', node);
                }
                if (fulltype.includes("image")) {
                    $('#s-@(random)-summernote').summernote('editor.insertImage', file.url);
                }
                if (!fulltype.includes("image") && !fulltype.includes("video")) {
                    $('#s-@(random)-summernote').summernote('editor.createLink', {
                        text: file.name,
                        url: file.url,
                        isNewWindow: false
                    });
                }
            },
            commandsOptions: {
                getfile: {
                    oncomplete: 'close',
                    folders: false
                }
            }
        }).dialogelfinder('instance');
    }
</script>
<script>
    $(document).ready(function () {
         var FileManager = function (context) {
            var ui = $.summernote.ui;
            // create button
            var button = ui.button({
                contents: '<i class="note-icon-picture"/>',
                tooltip: '@Loc["Admin.FileManager.Picture"]',
                click: function () {
                    elfinderDialog@(random)();
                }
            });
            return button.render();   // return button as jquery object
        }

        $('#s-@(random)-summernote').summernote({
            toolbar: [
                ['style', ['style']],
                ['font', ['bold', 'underline', 'clear']],
                ['fontname', ['fontname']],
                ['color', ['color']],
                ['para', ['ul', 'ol', 'paragraph']],
                ['table', ['table']],
                ['insert', ['link', 'image', 'elfinder', 'hr']],
                ['view', ['fullscreen', 'codeview', 'help']],
            ],
            buttons: {
                @if (allowFileman)
                {
                    <text> elfinder: FileManager, </text>
                }
            },
            callbacks: {
                    onChange: function (contents) {
                            var textarea = this.nextSibling.nextElementSibling;
                            textarea.value = contents;
                        },
            },
        });
    });
</script>