-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathSampleService.cs
316 lines (299 loc) · 13.3 KB
/
SampleService.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
#region Copyright Syncfusion® Inc. 2001-2025.
// Copyright Syncfusion® Inc. 2001-2025. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System.Linq;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System;
using Microsoft.JSInterop;
namespace Blazor_MAUI_Demos.Shared
{
/// <summary>
/// The injectable service class used to handle common functionalities all over the application.
/// </summary>
public class SampleService
{
/// <summary>
/// Specifies the app is rendering in device or not.
/// </summary>
public bool IsDevice { get; set; }
/// <summary>
/// Specifies spinner component reference.
/// </summary>
public SpinnerComponent Spinner { get; set; }
/// <summary>
/// Specifies the current component name.
/// </summary>
public string ComponentName { get; set; }
/// <summary>
/// Specifies the current sample details.
/// </summary>
public Sample SampleInfo { get; set; }
/// <summary>
/// Specifies the meta data component reference.
/// </summary>
public SampleMetaData MetaData { get; set; }
/// <summary>
/// Specifies the very first sample url.
/// </summary>
public string FirstSampleUrl { get; set; }
/// <summary>
/// Specifies the last sample url.
/// </summary>
public string LastSampleUrl { get; set; }
/// <summary>
/// Specifies the current sample url.
/// </summary>
public string CurrentSampleUrl { get; set; }
/// <summary>
/// Specifies the documentation link.
/// </summary>
public string DocumentLink { get; set; }
/// <summary>
/// Specifies the image url starts path.
/// </summary>
public string ImagePath { get; set; }
/// <summary>
/// Specifies the showcase image url starts path.
/// </summary>
public string ShowCaseImagePath { get; set; }
/// <summary>
/// Specifies the home page loaded or not.
/// </summary>
public bool IsHomeLoaded { get; set; }
/// <summary>
/// Specifies the demo page loaded or not.
/// </summary>
public bool IsDemoLoaded { get; set; }
/// <summary>
/// Specifies the demo page js loaded or not.
/// </summary>
public bool SampleJSLoaded { get; set; }
/// <summary>
/// Specifies the Demos Type whether its WASM or Server.
/// </summary>
public string DemoType { get; set; }
/// <summary>
/// Specifies the Common base script path..
/// </summary>
public string CommonScriptPath { get; set; }
/// <summary>
/// Specifies the Common base script path..
/// </summary>
public string SBScriptPath { get; set; }
/// <summary>
/// Specifies the diagram component script path.
/// </summary>
public string DiagramScriptPath { get; set; }
/// <summary>
/// Specifies the pdfviewer2 script path.
/// </summary>
public string PdfScriptPath2 { get; set; }
/// <summary>
/// Specifies the document viewer script path.
/// </summary>
public string ViewerScriptPath { get; set; }
/// <summary>
/// Specifies the document editor script path.
/// </summary>
public string DocScriptPath { get; set; }
/// <summary>
/// Specifies the pdfviewer Script loaded or not.
/// </summary>
public bool IsPdfScriptLoaded { get; set; }
/// <summary>
/// Specifies the document editor script loaded or not.
/// </summary>
public bool IsDocScriptLoaded { get; set; }
/// <summary>
/// Specifies the diagram interop script loaded or not.
/// </summary>
public bool IsDiagramScriptLoaded { get; set; }
/// <summary>
/// Specifies the Blazor Samples Common Static Web Assets location based on the project.
/// </summary>
public string WebAssetsPath { get; set; }
/// <summary>
/// Specifies the current sample is PDF Viewer.
/// </summary>
public bool IsPdfViewerSample { get; set; }
/// <summary>
/// Specifies the current sample is Document Editor/Word Editor.
/// </summary>
public bool IsWordEditorSample { get; set; }
/// <summary>
/// Specifies the current sample is DiagramComponent.
/// </summary>
public bool IsDiagramSample { get; set; }
public static string AssetsPath = "./";
public SampleService()
{
DemoType = "MAUI Blazor Demos";
WebAssetsPath = AssetsPath;
#if DEBUG || STAGING
ImagePath = WebAssetsPath + "images/common/";
ShowCaseImagePath = WebAssetsPath + "images/showcase/";
PdfScriptPath2 = "_content/Syncfusion.Blazor.SfPdfViewer/scripts";
DocScriptPath = "_content/Syncfusion.Blazor.WordProcessor/scripts";
CommonScriptPath = "_content/Syncfusion.Blazor.Core/scripts";
DiagramScriptPath = WebAssetsPath + "scripts/diagram/interop.js";
SBScriptPath = WebAssetsPath + "scripts/common/highlight.min.js";
ViewerScriptPath = WebAssetsPath + "scripts/pdfviewer/interop.js";
#else
ImagePath = "https://cdn.syncfusion.com/blazor/images/demos/";
ShowCaseImagePath = "https://cdn.syncfusion.com/blazor/images/showcase/";
PdfScriptPath2 = "https://cdn.syncfusion.com/blazor/27.1.48";
DocScriptPath = "https://cdn.syncfusion.com/blazor/27.1.48";
CommonScriptPath = "https://cdn.syncfusion.com/blazor/27.1.48";
DiagramScriptPath = WebAssetsPath + "scripts/diagram/interop.min.js";
SBScriptPath = WebAssetsPath + "scripts/common/demos.min.js";
ViewerScriptPath = WebAssetsPath + "scripts/pdfviewer/interop.min.js";
#endif
}
public async void SwicthToDemo(string id, string url, IJSRuntime JsRuntime, NavigationManager UriHelper)
{
if (!UriHelper.BaseUri.Contains("localhost")){
#if DEBUG || STAGING
url = id == "server" ? url : url.Replace("/net7/","/wasm/net7/");
#else
url = id == "server" ? url : url.Replace("/demos/","/wasm/demos/");
#endif
}
if (id != "server")
{
if (!UriHelper.BaseUri.Contains("localhost"))
{
await JsRuntime.InvokeVoidAsync("open", url, "_blank");
}
}
else
{
UriHelper.NavigateTo(url, true);
}
}
// Updates the SampleInfo and ComponentName based on current loaded uri.
internal void Update(NavigationManager urlHelper)
{
string updatedUrl = urlHelper.ToBaseRelativePath(urlHelper.Uri);
if (updatedUrl.Contains("?"))
{
updatedUrl = updatedUrl.Substring(0, updatedUrl.IndexOf("?"));
}
if (updatedUrl != string.Empty)
{
if (updatedUrl.LastIndexOf("/") == updatedUrl.Length - 1)
{
updatedUrl = updatedUrl.Substring(0, updatedUrl.LastIndexOf("/"));
}
updatedUrl = updatedUrl.Replace("//", "/");
string[] splittedUrl = updatedUrl.Split("/");
if (splittedUrl.Length >= 2)
{
try
{
string categoryName = splittedUrl[splittedUrl.Length - 2];
categoryName = categoryName.Replace("-", string.Empty);
SampleList controlInfo;
var sampleName = splittedUrl[splittedUrl.Length - 1];
if (categoryName == "buttons" && sampleName != "default-functionalities" && sampleName != "keyboard-navigation" || categoryName == "colorpicker" && sampleName == "inline")
{
controlInfo = SampleBrowser.SampleList.First<SampleList>(control => control.DemoPath.ToLower().Contains(sampleName));
}
else
{
controlInfo = SampleBrowser.SampleList.First<SampleList>(control => control.ControllerName.ToLower().Equals(categoryName));
}
this.ComponentName = controlInfo.Name;
var sampleInfo = controlInfo.Samples.Where(control => control.Url.ToLower() == updatedUrl).ToList();
if (sampleInfo.Count > 0)
{
this.SampleInfo = sampleInfo.First();
}
else
{
sampleName = sampleName.Replace("-", string.Empty);
this.SampleInfo = controlInfo.Samples.Where(control => control.FileName.ToLower() == (sampleName + ".razor")).First();
}
this.CurrentSampleUrl = this.SampleInfo.Url;
}
catch
{
}
}
// Navigate a Sample using Component Name
else if (splittedUrl.Length < 2 && splittedUrl.Length > 0)
{
string categoryName = splittedUrl[splittedUrl.Length - 1];
categoryName = categoryName.Replace("-", string.Empty);
var controlInfo = SampleBrowser.SampleList.First<SampleList>(control => control.ControllerName.ToLower().Equals(categoryName));
if (controlInfo.Samples.Count > 0)
{
this.SampleInfo = controlInfo.Samples.First();
}
this.ComponentName = controlInfo.Name;
this.CurrentSampleUrl = this.SampleInfo.Url;
var newUri = urlHelper.GetUriWithQueryParameters(SampleInfo.Url.ToLower(), new Dictionary<string, object>
{
["theme"] = "fluent"
});
urlHelper.NavigateTo(newUri);
}
}
}
// Updates the documentation link based on current loaded uri.
internal void UpdateFooter(string currentUrl)
{
currentUrl = Uri.UnescapeDataString(currentUrl);
var queryString = System.Web.HttpUtility.ParseQueryString(new Uri(currentUrl).Query);
// Handled condition to avoid a Culture switching navigation problem.
if (queryString.Get("redirectUri") != null)
{
currentUrl = queryString.Get("redirectUri");
currentUrl = currentUrl.Split('?')[currentUrl.Split('?').Length - 2];
}
else
{
currentUrl = queryString.Count != 0 ? currentUrl.Replace(queryString.ToString(), "") : currentUrl;
}
currentUrl = ((currentUrl.LastIndexOf('/') != -1) || (currentUrl.LastIndexOf('?') != -1) || (currentUrl.LastIndexOf('&') != -1)) ? currentUrl.Remove(currentUrl.Length - 1) : currentUrl;
currentUrl = currentUrl.EndsWith("/") ? currentUrl.TrimEnd('/') : currentUrl;
var splittedUrl = currentUrl.Split("/");
var ComponentName = splittedUrl[splittedUrl.Length - 2];
var ComponentInfo = SampleBrowser.SampleList.First<SampleList>(control => control.ControllerName.ToLower().Equals(ComponentName.Replace("-", string.Empty)));
if (ComponentInfo.Category.Equals("File Formats"))
{
this.DocumentLink = "https://help.syncfusion.com/" + ComponentInfo.Category.ToLower().Replace(" ", "-") + "/" + ComponentName + "/overview";
}
else
{
var DocLink = string.IsNullOrEmpty(ComponentInfo.CustomDocLink) ? ComponentName + "/getting-started" : ComponentInfo.CustomDocLink;
this.DocumentLink = "https://blazor.syncfusion.com/documentation/" + DocLink;
}
}
// Method for checking current Sample is PDF / Word / Diagram
public void checkSampleType(string currentUrl)
{
if(currentUrl.IndexOf("pdf-") != -1 || currentUrl.IndexOf("powerpoint") != -1)
{
IsPdfViewerSample = true;
}
else if (currentUrl.IndexOf("document-") != -1)
{
IsWordEditorSample = true;
}
else if (currentUrl.IndexOf("diagramcomponent") != -1)
{
IsDiagramSample = true;
}
}
// To handle external link navigation in the iOS app
public async Task HandleClickAsync(string url)
{
await Browser.Default.OpenAsync(url, BrowserLaunchMode.SystemPreferred);
}
}
}