<commit_before>﻿@model Orchard.MediaLibrary.ViewModels.MediaLibraryPickerFieldViewModel
@using Orchard.ContentManagement
@using Orchard.Utility.Extensions;
@using Orchard.MediaLibrary.Settings;

@{
    Script.Require("jQueryUI_Sortable").AtFoot();
    Style.Include("media-library-picker-admin.css");

    Script.Require("jQueryColorBox");
    Style.Require("jQueryColorBox");
    
    var settings = Model.Field.PartFieldDefinition.Settings.GetModel<MediaLibraryPickerFieldSettings>();
    var contentManager = WorkContext.Resolve<IContentManager>();
}

<fieldset>
    <label @if(settings.Required) { <text>class="required"</text> }>@Model.Field.DisplayName</label>
    <div id="save-message-@Html.FieldIdFor(m => m.Field.Ids)" class="message message-Warning media-library-picker-message">@T("You need to save your changes.")</div>
    <div id="media-library-picker-@Html.FieldIdFor(m => m.Field.Ids)" class="items media-library-picker" summary="@Model.Field.DisplayName">
        <ul>
            @foreach (var contentItem in Model.ContentItems) {
                <li>
                    <div data-id="@contentItem.Id" data-fieldid="@Html.FieldIdFor(m => m.Field.Ids)" class="media-library-picker-item">
                        <div class="thumbnail">
                            @Display(contentManager.BuildDisplay(contentItem, "Thumbnail"))
                            <div class="overlay">
                                <h3>@Html.ItemDisplayText(contentItem)</h3>                         
                            </div>

                        </div>
                    </div>
                    <span data-id="@contentItem.Id" class="media-library-picker-remove button grey">@T("Remove")</span>
                </li>
            }
        </ul>
    </div>

    <div>
        <span id="btn-@Html.FieldIdFor(m => m.Field.Ids)" class="button">@T("Add")</span>
    </div>
    
    @Html.HiddenFor(m => m.SelectedIds)
    <span class="hint">@settings.Hint</span>
</fieldset>

@using (Script.Foot()) {
<script type="text/javascript">
//<![CDATA[

    (function($) {

        var required = @(settings.Required ? "true" : "false");
        var multiple = @(settings.Multiple ? "true" : "false");
        var addButton = $('#btn-@Html.FieldIdFor(m => m.Field.Ids)');
        var @Html.FieldIdFor(m => m.Field.Ids)_Template = '<li><div data-id="{contentItemId}" data-fieldid="@Html.FieldIdFor(m => m.Field.Ids)" class="media-library-picker-item"><div class="thumbnail">{thumbnail}<div class="overlay"><h3>{title}</h3></div><div></div><span data-id="{contentItemId}" class="media-library-picker-remove button grey">@T("Remove")</span></li>';
        
        var refreshIds = function() {
            var id = $('#@Html.FieldIdFor(m => m.SelectedIds)');
            id.val('');
            $("div[data-fieldid = @Html.FieldIdFor(m => m.Field.Ids)]").each(function() {
                id.val(id.val() + "," + $(this).attr("data-id"));
            });

            var itemsCount = $("div[data-fieldid = @Html.FieldIdFor(m => m.Field.Ids)]").length;
            
            if(!multiple && itemsCount > 0) {
                addButton.hide();    
            }
            else {
                addButton.show();
            }
        };

        refreshIds();
        
        addButton.click(function() {
            var url = '@HttpUtility.JavaScriptStringEncode(Url.Action("Index", "Admin", new {area = "Orchard.MediaLibrary", dialog = true}))';

            $.colorbox({
                href: url,
                iframe: true,
                reposition: true,
                width: "100%",
                height: "100%",
                onLoad: function () {
                    // hide the scrollbars from the main window
                    $('html, body').css('overflow', 'hidden');
                    $('#cboxClose').remove();
                },
                onClosed: function () {
                    $('html, body').css('overflow', '');

                    var selectedData = $.colorbox.selectedData;
                    for (var i = 0; i < selectedData.length; i++) {
                        
                        var template = @Html.FieldIdFor(m => m.Field.Ids)_Template.replace(/\{contentItemId\}/g, selectedData[i].id).replace(/\{thumbnail\}/g, selectedData[i].thumbnail).replace(/\{title\}/g, selectedData[i].title);
                        var content = $(template);
                        $('#media-library-picker-@Html.FieldIdFor(m => m.Field.Ids) ul').append(content);
                    }
                    
                    refreshIds();

                    if (selectedData.length) {
                        $('#save-message-@Html.FieldIdFor(m => m.Field.Ids)').show();
                    }
                }
            });
            
        });

        $(document).on("click",'#media-library-picker-@Html.FieldIdFor(m => m.Field.Ids) .media-library-picker-remove',function() {
            $(this).closest('li').remove();
            refreshIds();
            $('#save-message-@Html.FieldIdFor(m => m.Field.Ids)').show();
        });
            
        $("#media-library-picker-@Html.FieldIdFor(m => m.Field.Ids) ul").sortable({
            handle: '.thumbnail',
            stop: function(event, ui) {
                refreshIds();
                $('#save-message-@Html.FieldIdFor(m => m.Field.Ids)').show();<commit_msg>Handling the case when closing MediaLibraryPickerField dialog without having selected anything to prevent a null reference javascript error.<commit_after>﻿@model Orchard.MediaLibrary.ViewModels.MediaLibraryPickerFieldViewModel
@using Orchard.ContentManagement
@using Orchard.Utility.Extensions;
@using Orchard.MediaLibrary.Settings;

@{
    Script.Require("jQueryUI_Sortable").AtFoot();
    Style.Include("media-library-picker-admin.css");

    Script.Require("jQueryColorBox");
    Style.Require("jQueryColorBox");
    
    var settings = Model.Field.PartFieldDefinition.Settings.GetModel<MediaLibraryPickerFieldSettings>();
    var contentManager = WorkContext.Resolve<IContentManager>();
}

<fieldset>
    <label @if(settings.Required) { <text>class="required"</text> }>@Model.Field.DisplayName</label>
    <div id="save-message-@Html.FieldIdFor(m => m.Field.Ids)" class="message message-Warning media-library-picker-message">@T("You need to save your changes.")</div>
    <div id="media-library-picker-@Html.FieldIdFor(m => m.Field.Ids)" class="items media-library-picker" summary="@Model.Field.DisplayName">
        <ul>
            @foreach (var contentItem in Model.ContentItems) {
                <li>
                    <div data-id="@contentItem.Id" data-fieldid="@Html.FieldIdFor(m => m.Field.Ids)" class="media-library-picker-item">
                        <div class="thumbnail">
                            @Display(contentManager.BuildDisplay(contentItem, "Thumbnail"))
                            <div class="overlay">
                                <h3>@Html.ItemDisplayText(contentItem)</h3>                         
                            </div>

                        </div>
                    </div>
                    <span data-id="@contentItem.Id" class="media-library-picker-remove button grey">@T("Remove")</span>
                </li>
            }
        </ul>
    </div>

    <div>
        <span id="btn-@Html.FieldIdFor(m => m.Field.Ids)" class="button">@T("Add")</span>
    </div>
    
    @Html.HiddenFor(m => m.SelectedIds)
    <span class="hint">@settings.Hint</span>
</fieldset>

@using (Script.Foot()) {
<script type="text/javascript">
//<![CDATA[

    (function($) {

        var required = @(settings.Required ? "true" : "false");
        var multiple = @(settings.Multiple ? "true" : "false");
        var addButton = $('#btn-@Html.FieldIdFor(m => m.Field.Ids)');
        var @Html.FieldIdFor(m => m.Field.Ids)_Template = '<li><div data-id="{contentItemId}" data-fieldid="@Html.FieldIdFor(m => m.Field.Ids)" class="media-library-picker-item"><div class="thumbnail">{thumbnail}<div class="overlay"><h3>{title}</h3></div><div></div><span data-id="{contentItemId}" class="media-library-picker-remove button grey">@T("Remove")</span></li>';
        
        var refreshIds = function() {
            var id = $('#@Html.FieldIdFor(m => m.SelectedIds)');
            id.val('');
            $("div[data-fieldid = @Html.FieldIdFor(m => m.Field.Ids)]").each(function() {
                id.val(id.val() + "," + $(this).attr("data-id"));
            });

            var itemsCount = $("div[data-fieldid = @Html.FieldIdFor(m => m.Field.Ids)]").length;
            
            if(!multiple && itemsCount > 0) {
                addButton.hide();    
            }
            else {
                addButton.show();
            }
        };

        refreshIds();
        
        addButton.click(function() {
            var url = '@HttpUtility.JavaScriptStringEncode(Url.Action("Index", "Admin", new {area = "Orchard.MediaLibrary", dialog = true}))';

            $.colorbox({
                href: url,
                iframe: true,
                reposition: true,
                width: "100%",
                height: "100%",
                onLoad: function () {
                    // hide the scrollbars from the main window
                    $('html, body').css('overflow', 'hidden');
                    $('#cboxClose').remove();
                },
                onClosed: function () {
                    $('html, body').css('overflow', '');

                    var selectedData = $.colorbox.selectedData;
                    
                    if (selectedData == null) // Dialog cancelled, do nothing
                        return;

                    for (var i = 0; i < selectedData.length; i++) {
                        
                        var template = @Html.FieldIdFor(m => m.Field.Ids)_Template.replace(/\{contentItemId\}/g, selectedData[i].id).replace(/\{thumbnail\}/g, selectedData[i].thumbnail).replace(/\{title\}/g, selectedData[i].title);
                        var content = $(template);
                        $('#media-library-picker-@Html.FieldIdFor(m => m.Field.Ids) ul').append(content);
                    }
                    
                    refreshIds();

                    if (selectedData.length) {
                        $('#save-message-@Html.FieldIdFor(m => m.Field.Ids)').show();
                    }
                }
            });
            
        });

        $(document).on("click",'#media-library-picker-@Html.FieldIdFor(m => m.Field.Ids) .media-library-picker-remove',function() {
            $(this).closest('li').remove();
            refreshIds();
            $('#save-message-@Html.FieldIdFor(m => m.Field.Ids)').show();
        });
            
        $("#media-library-picker-@Html.FieldIdFor(m => m.Field.Ids) ul").sortable({
            handle: '.thumbnail',
            stop: function(event, ui) {
                refreshIds();
                $('#save-message-@Html.FieldIdFor(m => m.Field.Ids)').show();
{'fraction_non_alphanumeric': 0.16867797840486345, 'fraction_numerical': 0.0021776608293258325, 'mean_word_length': 1.893672880021003, 'pattern_counts': {'":': 0, '<': 97, '<?xml version=': 0, '>': 131, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, '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': '3962529', 'n_tokens_mistral': 3167, 'n_tokens_neox': 3032, 'n_words': 472}