/
Index.cshtml
219 lines (203 loc) · 11.9 KB
/
Index.cshtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
@using System.Globalization;
@using OrchardCore.ContentManagement.Metadata.Settings;
@using OrchardCore.Layers.Drivers
@model OrchardCore.Layers.ViewModels.LayersIndexViewModel
@inject IContentManager ContentManager
@inject OrchardCore.ContentManagement.Metadata.IContentDefinitionManager ContentDefinitionManager
@inject OrchardCore.ContentManagement.Display.IContentItemDisplayManager ContentItemDisplayManager
@{
var widgetContentTypes = ContentDefinitionManager.ListTypeDefinitions().Where(t => t.GetSettings<ContentTypeSettings>().Stereotype == "Widget");
}
<script asp-name="jquery-ui" at="Foot"></script>
<zone Name="Title"><h1>@RenderTitleSegments(T["Edit Widgets and Layers"])</h1></zone>
<form id="undo-form" asp-action="UpdatePosition" method="post" class="no-multisubmit">
<div id="layer-undo-message" class="alert alert-success alert-dismissible show d-none" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
@T["Widget successfully moved. <a href=\"javascript:console.log('submit');$('#undo-form').submit();\"><strong>Undo</strong></a>."]
</div>
<input id="layer-undo-message-contentItemId" type="hidden" name="contentItemId" value="" />
<input id="layer-undo-message-zone" type="hidden" name="zone" value="" />
<input id="layer-undo-message-position" type="hidden" name="position" value="" />
</form>
<div class="row">
<div class="col-md-8">
<h3>@T["Zones"]</h3>
<form asp-action="Index">
@if (Model.Zones == null || !Model.Zones.Any())
{
<p class="alert alert-warning">@T["Please <a href=\"{0}\">define the available zones</a> before adding Widgets", Url.Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = LayerSiteSettingsDisplayDriver.GroupId })]</p>
}
else
{
foreach (var zone in Model.Zones)
{
double maxPosition = 0;
<div class="card form-group">
<div class="card-header text-muted">
@zone
</div>
<div class="layer-zone card-body" data-zone="@zone">
@if (Model.Widgets.ContainsKey(zone) && Model.Widgets[zone].Any())
{
<div class="form-group">
<ul class="list-group zones">
@foreach (var contentItemSummary in Model.Widgets[zone])
{
await contentItemSummary.Tags.AddAsync(Html.Raw("<span class=\"badge ta-badge font-weight-normal\"><i class=\"fa fa-bookmark-o text-info\" aria-hidden=\"true\"></i> " + Html.Encode(contentItemSummary.ContentItem.Content.LayerMetadata.Layer) + "</span>"));
maxPosition = Math.Max(maxPosition, (double)contentItemSummary.ContentItem.Content.LayerMetadata.Position);
<text>
<li class="list-group-item">
<div class="properties cursor-move">
@await DisplayAsync(contentItemSummary)
</div>
<input type="hidden"
class="layer-metadata"
data-zone="@zone"
data-layer="@contentItemSummary.ContentItem.Content.LayerMetadata.Layer"
data-position="@contentItemSummary.ContentItem.Content.LayerMetadata.Position.ToString(CultureInfo.InvariantCulture)"
data-content-item-id="@contentItemSummary.ContentItem.ContentItemId" />
</li>
</text>
}
</ul>
</div>
}
@if (Model.Layers.Any())
{
@if (widgetContentTypes.Any())
{
<div class="btn-dropdown-wrapper w-100">
<div class="btn-group btn-dropdown">
<button type="button" class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
@T["Add Widget"]
</button>
<div class="dropdown-menu">
@foreach (var type in widgetContentTypes)
{
var contentItem = await ContentManager.NewAsync(type.Name);
var createRoute = (await ContentManager.PopulateAspectAsync<ContentItemMetadata>(contentItem)).CreateRouteValues;
createRoute["returnUrl"] = Context.Request.PathBase + Context.Request.Path;
createRoute["LayerMetadata.Zone"] = zone;
createRoute["LayerMetadata.Position"] = maxPosition + 1;
<a class="dropdown-item add-list-widget btn-sm"
data-widget-type="@type.Name"
data-zone="@zone"
href="@Url.Action((string)createRoute["Action"], (string)createRoute["controller"], createRoute)">@type.DisplayName</a>
}
</div>
</div>
</div>
}
else
{
<span>@T["No widgets found."]</span>
}
}
else
{
<span>@T["No layers found."]</span>
}
</div>
</div>
}
}
</form>
</div>
<div class="col-md-4 col-md-pull-right">
<h3>@T["Layers"]</h3>
<p>
<span class="hint">
@T["Select a layer to reveal its associated widgets."]
<a class="seedoc" href="@(OrchardCore.Admin.Constants.DocsUrl)reference/modules/Layers/#rules" target="_blank">@T["See documentation"]</a>
</span>
</p>
<div class="form-group">
<ul class="list-group">
@foreach (var layer in Model.Layers)
{
<li class="list-group-item">
<div class="properties">
<div class="d-flex flex-wrap">
<div class="custom-control custom-checkbox mr-auto flex-grow-1">
<input type="checkbox" class="custom-control-input layer-check" id="@layer.Name" value="@layer.Name">
<label class="custom-control-label" for="@layer.Name">@layer.Name</label>
</div>
<div>
<a class="btn btn-primary btn-sm" href="@Url.Action("Edit", "Admin", new { area = "OrchardCore.Layers", name = layer.Name })">@T["Edit"]</a>
<a class="btn btn-danger btn-sm" href="@Url.Action("Delete", "Admin", new { area = "OrchardCore.Layers", name = layer.Name })" data-url-af="RemoveUrl UnsafeUrl">@T["Delete"]</a>
</div>
</div>
<div>
<span class="hint">@layer.Description</span>
</div>
</div>
</li>
}
</ul>
</div>
@if (!Model.Layers.Any())
{
<p class="alert alert-warning">@T["Please <a href=\"{0}\">create a layer</a> before adding Widgets", Url.Action("Create", "Admin", new { area = "OrchardCore.Layers" })]</p>
}
<a class="button btn-sm" href="@Url.Action("Create", "Admin", new { area = "OrchardCore.Layers" })">@T["Add"]</a>
</div>
</div>
<script at="Foot">
$(".list-group.zones").sortable({
handle: ".properties",
cursor: "move",
connectWith: '.list-group',
stop: function (event, ui) {
var newZone = ui.item.closest('.layer-zone').data("zone");
var metadata = ui.item.find('.layer-metadata')
var currentPosition = Number(metadata.data("position"));
var item = ui.item.closest('.list-group-item');
var previous = item.prev('.list-group-item').find('.layer-metadata');
var next = item.next('.list-group-item').find('.layer-metadata');
var contentItemId = metadata.data("content-item-id");
var newPosition = currentPosition;
if (previous.length == 0 && next.length == 0) {
// we can keep the current position
}
else if (previous.length == 0 && next.length > 0) {
// first in the list
newPosition = Number(next.data("position") || 0) - 1;
}
else if (previous.length > 0 && next.length == 0) {
// last in the list
newPosition = Number(previous.data("position") || 0) + 1;
}
else {
newPosition = (Number(previous.data("position") || 0) + Number(next.data("position") || 0)) / 2;
}
var currentZone = metadata.data("zone");
var currentPosition = metadata.data("position");
metadata.data("zone", newZone);
metadata.data("position", newPosition);
var url = "@Url.Action("UpdatePosition", "Admin", new { area = "OrchardCore.Layers" })" + "?" + "contentItemId=" + encodeURIComponent(contentItemId) + "&position=" + encodeURIComponent(newPosition) + "&zone=" + encodeURIComponent(newZone);
var antiforgerytoken = $("[name='__RequestVerificationToken']").val();
$.post(url, { "__RequestVerificationToken": antiforgerytoken })
.done(function () {
console.log('done');
$('#layer-undo-message').removeClass("d-none");
$('#layer-undo-message-contentItemId').val(contentItemId);
$('#layer-undo-message-position').val(currentPosition);
$('#layer-undo-message-zone').val(currentZone);
})
.fail(function () {
console.log('failed');
$('#layer-undo-message').addClass("d-none");
})
;
}
});
$('.layer-check').on('click', function () {
$('.list-group-item').removeClass('list-group-item-success');
$('.layer-check:checked').each(function () {
var layer = $(this).val();
$('[data-layer="' + layer + '"]').closest('.list-group-item').addClass('list-group-item-success');
});
});
</script>