File size: 8,530 Bytes
b1b3bae |
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 |
using DWSIM.Interfaces;
using DWSIM.Simulate365.Models;
using DWSIM.Simulate365.Services;
using DWSIM.UI.Web;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
namespace DWSIM.Simulate365.FormFactories
{
public class S365FilePickerForm : IFilePicker
{
private WebUIForm _webUIForm;
private readonly FilePickerService _filePickerService;
public string SuggestedDirectory { get; set; }
public string SuggestedFilename { get; set; }
private readonly UserService _userService;
#region Public events
public static event EventHandler FileOpenedFromDashboard;
public static event EventHandler<BeforeShowDialogEventArgs> BeforeShowSaveDialog;
public static event EventHandler<BeforeShowDialogEventArgs> BeforeShowOpenDialog;
#endregion
public S365FilePickerForm()
{
_filePickerService = new FilePickerService();
_filePickerService.S3365DashboardFileOpenStarted += FilePickerService_S3365DashboardFileOpenStarted;
_filePickerService.S365DashboardSaveFileClicked += FilePickerService_S365DashboardSaveFileClicked;
_filePickerService.S365DashboardFolderCreated += _filePickerService_S365DashboardFolderCreated;
_userService = UserService.GetInstance();
_userService.OnUserLoggedIn += OnUserLoggedInEvent;
}
private void OnUserLoggedInEvent(object sender, EventArgs e)
{
_webUIForm.Navigate(_webUIForm.InitialUrl);
}
private void _filePickerService_S365DashboardFolderCreated(object sender, EventArgs e)
{
_webUIForm.RealoadPage();
}
private void FilePickerService_S365DashboardSaveFileClicked(object sender, S365DashboardSaveFile e)
{
UsubscribeFromEvents();
// Close window
_webUIForm?.Close();
_webUIForm?.Dispose();
}
private void UsubscribeFromEvents()
{
_userService.OnUserLoggedIn -= OnUserLoggedInEvent;
}
private void FilePickerService_S3365DashboardFileOpenStarted(object sender, EventArgs e)
{
UsubscribeFromEvents();
// Close window
_webUIForm?.Close();
_webUIForm?.Dispose();
}
public S365File ShowSaveDialog(List<string> fileFormats = null)
{
// Invoke event handlers
var eventArgs = new BeforeShowDialogEventArgs();
BeforeShowSaveDialog?.Invoke(null, eventArgs);
if (eventArgs.Cancel)
return null;
var navigationPath = "filepicker/save";
var queryParams = new Dictionary<string, string>();
if (fileFormats != null && fileFormats.Count > 0)
{
queryParams.Add("extensions", string.Join("_", fileFormats));
}
if (!string.IsNullOrWhiteSpace(SuggestedDirectory))
{
queryParams.Add("directory", HttpUtility.UrlEncode(SuggestedDirectory));
}
if (!string.IsNullOrWhiteSpace(SuggestedFilename))
{
queryParams.Add("filename", HttpUtility.UrlEncode(SuggestedFilename));
}
var initialUrl = $"{navigationPath}";
if (queryParams.Any())
{
initialUrl = initialUrl + string.Join("", queryParams.Select(x =>
{
var param = $"{x.Key}={x.Value}";
return queryParams.First().Key == x.Key ? $"?{param}" : $"&{param}";
}).ToList());
}
string title = "Save file to Simulate 365 Dashboard";
_webUIForm = new WebUIForm(initialUrl, title, true)
{
Width = (int)(1300 * DWSIM.GlobalSettings.Settings.DpiScale),
Height = (int)(800 * DWSIM.GlobalSettings.Settings.DpiScale)
};
_webUIForm.SubscribeToInitializationCompleted(Browser_CoreWebView2InitializationCompleted);
_webUIForm.ShowDialog();
return _filePickerService.SelectedSaveFile != null ?
new S365File(null)
{
FileUniqueIdentifier = null,
Filename = _filePickerService.SelectedSaveFile.Filename,
ParentUniqueIdentifier = _filePickerService.SelectedSaveFile.ParentUniqueIdentifier,
FullPath = _filePickerService.SelectedSaveFile.SimulatePath,
ConflictAction = _filePickerService.SelectedSaveFile.ConflictAction
} : null;
}
public S365File ShowOpenDialog(List<string> fileFormats = null)
{
// Invoke event handlers
var eventArgs = new BeforeShowDialogEventArgs();
BeforeShowOpenDialog?.Invoke(null, eventArgs);
if (eventArgs.Cancel)
return null;
var navigationPath = "filepicker/open";
var queryParams = new Dictionary<string, string>();
if (fileFormats != null && fileFormats.Count > 0)
{
queryParams.Add("extensions", string.Join("_", fileFormats));
}
if (!string.IsNullOrWhiteSpace(SuggestedDirectory))
{
queryParams.Add("directory", HttpUtility.UrlEncode(SuggestedDirectory));
}
var initialUrl = $"{navigationPath}";
if (queryParams.Any())
{
initialUrl = initialUrl + string.Join("", queryParams.Select(x =>
{
var param = $"{x.Key}={x.Value}";
return queryParams.First().Key == x.Key ? $"?{param}" : $"&{param}";
}).ToList());
}
string title = "Open file from Simulate 365 Dashboard";
_webUIForm = new WebUIForm(initialUrl, title, true)
{
Width = (int)(1300 * DWSIM.GlobalSettings.Settings.DpiScale),
Height = (int)(800 * DWSIM.GlobalSettings.Settings.DpiScale)
};
_webUIForm.SubscribeToInitializationCompleted(Browser_CoreWebView2InitializationCompleted);
_webUIForm.ShowDialog();
return _filePickerService.SelectedOpenFile;
}
private void Browser_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
try
{
var webView = sender as WebView2;
if (webView.CoreWebView2 != null)
{
webView.CoreWebView2.AddHostObjectToScript("authService", new AuthService());
webView.CoreWebView2.AddHostObjectToScript("filePickerService", _filePickerService);
}
}
catch (Exception ex)
{
// throw;
}
}
#region FilePickerService
public IVirtualFile ShowOpenDialog(IEnumerable<IFilePickerAllowedType> allowedTypes)
{
List<string> fileFormats = null;
if (allowedTypes != null && allowedTypes.Count() > 0)
{
fileFormats = allowedTypes.SelectMany(t => t.AllowedExtensions.Select(e => ReplateLeadingStarDot(e))).Distinct().ToList();
}
var file = ShowOpenDialog(fileFormats);
FileOpenedFromDashboard?.Invoke(this, new EventArgs());
return file;
}
public IVirtualFile ShowSaveDialog(IEnumerable<IFilePickerAllowedType> allowedTypes)
{
List<string> fileFormats = null;
if (allowedTypes != null && allowedTypes.Count() > 0)
{
fileFormats = allowedTypes.SelectMany(t => t.AllowedExtensions.Select(e => ReplateLeadingStarDot(e))).Distinct().ToList();
}
var file = ShowSaveDialog(fileFormats);
return file;
}
private string ReplateLeadingStarDot(string input)
{
return Regex.Replace(input, @"^\*{0,1}\.", "");
}
#endregion
}
public class BeforeShowDialogEventArgs : EventArgs
{
public bool Cancel { get; set; }
}
}
|