forked from radzenhq/radzen-blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRadzenCarousel.razor.cs
367 lines (316 loc) · 10.8 KB
/
RadzenCarousel.razor.cs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace Radzen.Blazor
{
/// <summary>
/// RadzenCarousel component.
/// </summary>
/// <example>
/// <code>
/// <RadzenCarousel Change=@(args => Console.WriteLine($"Selected index is: {args}"))>
/// <Items>
/// <RadzenCarouselItem>
/// Details for Orders
/// </RadzenCarouselItem>
/// <RadzenCarousel>
/// Details for Employees
/// </RadzenCarouselItem>
/// </Items>
/// </RadzenCarousel>
/// </code>
/// </example>
public partial class RadzenCarousel : RadzenComponent
{
/// <summary>
/// Gets or sets the items.
/// </summary>
/// <value>The items.</value>
[Parameter]
public RenderFragment Items { get; set; }
internal List<RadzenCarouselItem> items = new List<RadzenCarouselItem>();
/// <summary>
/// Adds the item.
/// </summary>
/// <param name="item">The item.</param>
public void AddItem(RadzenCarouselItem item)
{
if (!items.Contains(item))
{
items.Add(item);
StateHasChanged();
}
}
/// <summary>
/// Removes the item.
/// </summary>
/// <param name="item">The item.</param>
public void RemoveItem(RadzenCarouselItem item)
{
if (items.Contains(item))
{
items.Remove(item);
if (!disposed)
{
try { InvokeAsync(StateHasChanged); } catch { }
}
}
}
/// <inheritdoc />
protected override string GetComponentCssClass()
{
return $"rz-carousel {(AllowNavigation ? "" : "rz-carousel-no-navigation")} {(PagerOverlay ? "rz-carousel-pager-overlay" : "")}".Trim();
}
/// <summary>
/// Navigates to specific index.
/// </summary>
public async Task Navigate(int index)
{
if (Auto)
{
await Reset();
}
await GoTo(index);
}
async Task Prev()
{
await Navigate(selectedIndex == 0 ? items.Count - 1 : selectedIndex - 1);
}
async Task Next()
{
await Navigate(selectedIndex == items.Count - 1 ? 0 : selectedIndex + 1);
}
async Task GoTo(int index)
{
if (index >= 0 && index <= items.Count - 1 && selectedIndex != index)
{
selectedIndex = index;
await SelectedIndexChanged.InvokeAsync(selectedIndex);
await Change.InvokeAsync(selectedIndex);
await JSRuntime.InvokeVoidAsync("Radzen.scrollCarouselItem", items[selectedIndex].element);
StateHasChanged();
}
}
/// <summary>
/// Stops the auto-cycle timer.
/// </summary>
public void Stop()
{
timer?.Change(Timeout.Infinite, Timeout.Infinite);
}
/// <summary>
/// Starts the auto-cycle timer.
/// </summary>
public void Start()
{
timer?.Change(TimeSpan.FromMilliseconds(Interval), TimeSpan.FromMilliseconds(Interval));
}
/// <summary>
/// Resets the auto-cycle timer.
/// </summary>
public async Task Reset()
{
Stop();
Start();
await Task.CompletedTask;
}
/// <summary>
/// Gets or sets the selected index.
/// </summary>
/// <value>The selected index.</value>
[Parameter]
public int SelectedIndex { get; set; }
private int selectedIndex;
/// <summary>
/// Gets or sets the selected index changed callback.
/// </summary>
/// <value>The selected index changed callback.</value>
[Parameter]
public EventCallback<int> SelectedIndexChanged { get; set; }
/// <summary>
/// Gets or sets the change callback.
/// </summary>
/// <value>The change callback.</value>
[Parameter]
public EventCallback<int> Change { get; set; }
/// <inheritdoc />
public override async Task SetParametersAsync(ParameterView parameters)
{
var shouldUpdate = false;
if (parameters.DidParameterChange(nameof(SelectedIndex), SelectedIndex))
{
selectedIndex = parameters.GetValueOrDefault<int>(nameof(SelectedIndex));
shouldUpdate = true;
}
if (parameters.DidParameterChange(nameof(Auto), Auto) ||
parameters.DidParameterChange(nameof(Interval), Interval))
{
if (parameters.GetValueOrDefault<bool>(nameof(Auto)))
{
await Reset();
}
else
{
Stop();
}
}
await base.SetParametersAsync(parameters);
if (shouldUpdate)
{
await JSRuntime.InvokeVoidAsync("Radzen.scrollCarouselItem", items[selectedIndex].element);
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="RadzenCarousel"/> cycle is automatic.
/// </summary>
/// <value><c>true</c> if cycle automatic; otherwise, <c>false</c>.</value>
[Parameter]
public bool Auto { get; set; } = true;
/// <summary>
/// Gets or sets the auto-cycle interval in milliseconds.
/// </summary>
[Parameter]
public double Interval { get; set; } = 4000;
/// <summary>
/// Gets or sets the pager position. Set to <c>PagerPosition.Bottom</c> by default.
/// </summary>
/// <value>The pager position.</value>
[Parameter]
public PagerPosition PagerPosition { get; set; } = PagerPosition.Bottom;
/// <summary>
/// Gets or sets a value indicating whether pager overlays the carousel items. Set to <c>true</c> by default.
/// </summary>
/// <value><c>true</c> if pager overlay is allowed; otherwise, <c>false</c>.</value>
[Parameter]
public bool PagerOverlay { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether paging is allowed. Set to <c>true</c> by default.
/// </summary>
/// <value><c>true</c> if paging is allowed; otherwise, <c>false</c>.</value>
[Parameter]
public bool AllowPaging { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether previous/next navigation is allowed. Set to <c>true</c> by default.
/// </summary>
/// <value><c>true</c> if previous/next navigation is allowed; otherwise, <c>false</c>.</value>
[Parameter]
public bool AllowNavigation { get; set; } = true;
/// <summary>
/// Gets or sets the buttons style
/// </summary>
/// <value>The buttons style.</value>
[Parameter]
public ButtonStyle ButtonStyle { get; set; } = ButtonStyle.Base;
/// <summary>
/// Gets or sets the design variant of the buttons.
/// </summary>
/// <value>The variant of the buttons.</value>
[Parameter]
public Variant ButtonVariant { get; set; } = Variant.Text;
/// <summary>
/// Gets or sets the color shade of the buttons.
/// </summary>
/// <value>The color shade of the buttons.</value>
[Parameter]
public Shade ButtonShade { get; set; } = Shade.Lighter;
/// <summary>
/// Gets or sets the buttons size.
/// </summary>
/// <value>The buttons size.</value>
[Parameter]
public ButtonSize ButtonSize { get; set; } = ButtonSize.Large;
/// <summary>
/// Gets or sets the next button text.
/// </summary>
/// <value>The next button text.</value>
[Parameter]
public string NextText { get; set; } = "";
/// <summary>
/// Gets or sets the previous button text.
/// </summary>
/// <value>The previous button text.</value>
[Parameter]
public string PrevText { get; set; } = "";
/// <summary>
/// Gets or sets the next button icon.
/// </summary>
/// <value>The next button icon.</value>
[Parameter]
public string NextIcon { get; set; } = "arrow_forward_ios";
/// <summary>
/// Gets or sets the previous button icon.
/// </summary>
/// <value>The previous button icon.</value>
[Parameter]
public string PrevIcon { get; set; } = "arrow_back_ios_new";
System.Threading.Timer timer;
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
var ts = TimeSpan.FromMilliseconds(Interval);
timer = new System.Threading.Timer(state => InvokeAsync(Next),
null, Auto ? ts : Timeout.InfiniteTimeSpan, ts);
}
}
/// <inheritdoc />
public override void Dispose()
{
base.Dispose();
if (timer != null)
{
timer.Dispose();
timer = null;
}
}
double? x;
double? y;
void OnTouchStart(TouchEventArgs args)
{
x = args.Touches[0].ClientX;
y = args.Touches[0].ClientY;
}
async Task OnTouchEnd(TouchEventArgs args)
{
if (x == null || y == null)
{
return;
}
var xDiff = x.Value - args.ChangedTouches[0].ClientX;
var yDiff = y.Value - args.ChangedTouches[0].ClientY;
if (Math.Abs(xDiff) < 100 && Math.Abs(yDiff) < 100)
{
x = null;
y = null;
return;
}
if (Math.Abs(xDiff) > Math.Abs(yDiff))
{
if (xDiff > 0)
{
await Next();
}
else
{
await Prev();
}
}
x = null;
y = null;
}
void OnTouchCancel(TouchEventArgs args)
{
x = null;
y = null;
}
}
}