-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guider.cs
138 lines (127 loc) · 5.15 KB
/
Guider.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Polarizelab.Blazor.GuidePopup
{
public class Guider : IGuider
{
private Queue<GuideStep> GuideLines { get; set; }
private GuiderSetting Setting;
private string Id { get; } = Guid.NewGuid().ToString();
private readonly IJSRuntime _jSRuntime;
public Guider(IJSRuntime jSRuntime)
{
GuideLines = new Queue<GuideStep>();
Setting = new GuiderSetting();
_jSRuntime = jSRuntime;
}
public Guider(IJSRuntime jSRuntime, Action<GuiderSetting> options) : this(jSRuntime)
{
options(Setting);
}
public event EventHandler OnClosed;
public ValueTask<object> Show(ElementReference element, string content, GuidePosition guidePosition = GuidePosition.Right)
{
return _jSRuntime.InvokeAsync<object>("guiderJsFunctions.showWithElementRef", Setting, Id, element, content, guidePosition, DotNetObjectReference.Create(this));
}
public ValueTask<object> Show (string elementId, string content, GuidePosition guidePosition = GuidePosition.Right)
{
return _jSRuntime.InvokeAsync<object>("guiderJsFunctions.showWithElementId", Setting, Id, elementId, content, guidePosition, DotNetObjectReference.Create(this));
}
public ValueTask<object> Show (double x, double y, string content, GuidePosition guidePosition = GuidePosition.Right)
{
return _jSRuntime.InvokeAsync<object>("guiderJsFunctions.showWithXY", Setting, Id, x, y, content, guidePosition, DotNetObjectReference.Create (this));
}
[JSInvokable]
public void InvokeClosed()
{
OnClosed?.Invoke(this, null);
}
private IGuider Make(GuideStep guideStep)
{
GuideLines.Enqueue(guideStep);
return this;
}
public async Task Start()
{
while (GuideLines.Count != 0)
{
bool closed = false;
GuideStep step = GuideLines.Dequeue();
this.OnClosed += (s, e) =>
{
closed = true;
};
await ShowStep(step);
while (!closed)
await Task.Delay(100);
}
}
private ValueTask<object> ShowStep(GuideStep guideStep)
{
if (guideStep.GuideType == GuideType.Id)
return Show(guideStep.ElementId, guideStep.Content, guideStep.GuidePosition);
if (guideStep.GuideType == GuideType.Ref)
return Show(guideStep.ElementRef, guideStep.Content, guideStep.GuidePosition);
return Show(guideStep.X, guideStep.Y, guideStep.Content, guideStep.GuidePosition);
}
public IGuider Make(ElementReference element, string content, GuidePosition guidePosition = GuidePosition.Right)
{
return Make(new GuideStep(element, content, guidePosition));
}
public IGuider Make(string elementId, string content, GuidePosition guidePosition = GuidePosition.Right)
{
return Make(new GuideStep(elementId, content, guidePosition));
}
public IGuider Make(double x, double y, string content, GuidePosition guidePosition = GuidePosition.Right)
{
return Make(new GuideStep(x, y, content, guidePosition));
}
public async Task ShowAll()
{
if (GuideLines.Count == 0)
return;
await _jSRuntime.InvokeAsync<object>("guiderJsFunctions.showMany", Setting, Id, GuideLines.ToArray(), DotNetObjectReference.Create (this));
}
}
public class GuideStep
{
private GuideStep(string content, GuidePosition guidePosition)
{
this.Content = content;
this.GuidePosition = guidePosition;
}
public GuideStep(string elementId, string content, GuidePosition guidePosition = GuidePosition.Right) : this(content, guidePosition)
{
this.ElementId = elementId;
this.GuideType = GuideType.Id;
}
public GuideStep(ElementReference elementRef, string content, GuidePosition guidePosition = GuidePosition.Right) : this(content, guidePosition)
{
this.ElementRef = elementRef;
this.GuideType = GuideType.Ref;
}
public GuideStep(double x, double y, string content, GuidePosition guidePosition = GuidePosition.Right) : this(content, guidePosition)
{
this.X = x;
this.Y = y;
this.GuideType = GuideType.Coordination;
}
public GuideType GuideType { get; set; }
public string Content { get; set; }
public GuidePosition GuidePosition { get; set; }
public string ElementId { get; set; }
public ElementReference ElementRef { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
public enum GuideType
{
Ref,
Id,
Coordination
}
}