|
1 |
| -namespace Samples.ViewModels |
2 |
| -{ |
| 1 | +namespace Samples.ViewModels |
| 2 | +{ |
3 | 3 | using AngleSharp;
|
4 | 4 | using AngleSharp.Io;
|
5 |
| - using System; |
6 |
| - using System.Threading; |
7 |
| - using System.Threading.Tasks; |
8 |
| - |
9 |
| - sealed class MainViewModel : BaseViewModel |
10 |
| - { |
11 |
| - #region Fields |
12 |
| - |
13 |
| - private readonly ITabViewModel[] _views; |
14 |
| - private readonly IEventViewModel[] _logs; |
15 |
| - private readonly IBrowsingContext _context; |
16 |
| - |
17 |
| - private Task _current; |
18 |
| - private CancellationTokenSource _cts; |
19 |
| - private String _status; |
20 |
| - private String _address; |
21 |
| - |
22 |
| - #endregion |
23 |
| - |
24 |
| - #region Child View Models |
25 |
| - |
26 |
| - private readonly DOMViewModel _dom; |
27 |
| - private readonly ProfilerViewModel _profiler; |
28 |
| - private readonly QueryViewModel _query; |
29 |
| - private readonly ReplViewModel _repl; |
30 |
| - private readonly SettingsViewModel _settings; |
31 |
| - private readonly StatisticsViewModel _statistics; |
32 |
| - private readonly TreeViewModel _tree; |
33 |
| - private readonly SheetViewModel _sheets; |
34 |
| - private readonly ErrorsViewModel _errors; |
35 |
| - |
36 |
| - #endregion |
37 |
| - |
38 |
| - #region ctor |
39 |
| - |
40 |
| - public MainViewModel() |
41 |
| - { |
42 |
| - var config = Configuration.Default |
43 |
| - .WithCss() |
44 |
| - .WithCookies() |
45 |
| - .WithJs() |
46 |
| - .WithDefaultLoader(new LoaderOptions |
| 5 | + using System; |
| 6 | + using System.Threading; |
| 7 | + using System.Threading.Tasks; |
| 8 | + |
| 9 | + sealed class MainViewModel : BaseViewModel |
| 10 | + { |
| 11 | + #region Fields |
| 12 | + |
| 13 | + private readonly ITabViewModel[] _views; |
| 14 | + private readonly IEventViewModel[] _logs; |
| 15 | + private readonly IBrowsingContext _context; |
| 16 | + |
| 17 | + private Task _current; |
| 18 | + private CancellationTokenSource _cts; |
| 19 | + private String _status; |
| 20 | + private String _address; |
| 21 | + |
| 22 | + #endregion |
| 23 | + |
| 24 | + #region Child View Models |
| 25 | + |
| 26 | + private readonly DOMViewModel _dom; |
| 27 | + private readonly ProfilerViewModel _profiler; |
| 28 | + private readonly QueryViewModel _query; |
| 29 | + private readonly ReplViewModel _repl; |
| 30 | + private readonly SettingsViewModel _settings; |
| 31 | + private readonly StatisticsViewModel _statistics; |
| 32 | + private readonly TreeViewModel _tree; |
| 33 | + private readonly SheetViewModel _sheets; |
| 34 | + private readonly ErrorsViewModel _errors; |
| 35 | + |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region ctor |
| 39 | + |
| 40 | + public MainViewModel() |
| 41 | + { |
| 42 | + var config = Configuration.Default |
| 43 | + .WithCss() |
| 44 | + .WithCookies() |
| 45 | + .WithJs() |
| 46 | + .WithDefaultLoader(new LoaderOptions |
47 | 47 | {
|
48 |
| - IsNavigationDisabled = true, |
| 48 | + IsNavigationDisabled = false, |
49 | 49 | IsResourceLoadingEnabled = true,
|
50 |
| - }); |
51 |
| - _context = BrowsingContext.New(config); |
52 |
| - _profiler = new ProfilerViewModel(_context); |
53 |
| - _errors = new ErrorsViewModel(_context); |
54 |
| - _dom = new DOMViewModel(); |
55 |
| - _query = new QueryViewModel(); |
56 |
| - _repl = new ReplViewModel(); |
57 |
| - _settings = new SettingsViewModel(); |
58 |
| - _statistics = new StatisticsViewModel(); |
59 |
| - _tree = new TreeViewModel(); |
60 |
| - _sheets = new SheetViewModel(); |
61 |
| - _cts = new CancellationTokenSource(); |
62 |
| - _views = new ITabViewModel[] |
63 |
| - { |
64 |
| - _dom, |
65 |
| - _query, |
66 |
| - _repl, |
67 |
| - _statistics, |
68 |
| - _tree, |
69 |
| - _sheets |
70 |
| - }; |
71 |
| - _logs = new IEventViewModel[] |
72 |
| - { |
73 |
| - _profiler, |
74 |
| - _errors |
75 |
| - }; |
76 |
| - } |
77 |
| - |
78 |
| - #endregion |
79 |
| - |
80 |
| - #region Properties |
81 |
| - |
82 |
| - public DOMViewModel Dom |
83 |
| - { |
84 |
| - get { return _dom; } |
85 |
| - } |
86 |
| - |
87 |
| - public ErrorsViewModel Errors |
88 |
| - { |
89 |
| - get { return _errors; } |
90 |
| - } |
91 |
| - |
92 |
| - public ProfilerViewModel Profiler |
93 |
| - { |
94 |
| - get { return _profiler; } |
95 |
| - } |
96 |
| - |
97 |
| - public QueryViewModel Queries |
98 |
| - { |
99 |
| - get { return _query; } |
100 |
| - } |
101 |
| - |
102 |
| - public ReplViewModel Console |
103 |
| - { |
104 |
| - get { return _repl; } |
105 |
| - } |
106 |
| - |
107 |
| - public SettingsViewModel Settings |
108 |
| - { |
109 |
| - get { return _settings; } |
110 |
| - } |
111 |
| - |
112 |
| - public StatisticsViewModel Statistics |
113 |
| - { |
114 |
| - get { return _statistics; } |
115 |
| - } |
116 |
| - |
117 |
| - public TreeViewModel Tree |
118 |
| - { |
119 |
| - get { return _tree; } |
120 |
| - } |
121 |
| - |
122 |
| - public SheetViewModel Sheets |
123 |
| - { |
124 |
| - get { return _sheets; } |
125 |
| - } |
126 |
| - |
127 |
| - public String Address |
128 |
| - { |
129 |
| - get { return _address; } |
130 |
| - set |
131 |
| - { |
132 |
| - _address = value; |
133 |
| - RaisePropertyChanged(); |
134 |
| - } |
135 |
| - } |
136 |
| - |
137 |
| - public String Status |
138 |
| - { |
139 |
| - get { return _status; } |
140 |
| - set |
141 |
| - { |
142 |
| - _status = value; |
143 |
| - RaisePropertyChanged(); |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 |
| - #endregion |
148 |
| - |
149 |
| - #region Methods |
150 |
| - |
151 |
| - public void Go() |
152 |
| - { |
153 |
| - var url = CreateUrlFrom(_address); |
154 |
| - |
155 |
| - if (_current != null && !_current.IsCompleted) |
156 |
| - { |
157 |
| - _cts.Cancel(); |
158 |
| - _cts = new CancellationTokenSource(); |
159 |
| - } |
160 |
| - |
161 |
| - _profiler.Reset(); |
162 |
| - _current = LoadAsync(url, _cts.Token); |
163 |
| - } |
164 |
| - |
165 |
| - private async Task LoadAsync(Url url, CancellationToken cancel) |
166 |
| - { |
167 |
| - Status = String.Format("Loading {0} ...", url.Href); |
168 |
| - |
169 |
| - foreach (var log in _logs) |
170 |
| - { |
171 |
| - log.Reset(); |
172 |
| - } |
173 |
| - |
174 |
| - var document = await _context.OpenAsync(url, cancel); |
175 |
| - |
176 |
| - foreach (var view in _views) |
177 |
| - { |
178 |
| - view.Document = document; |
179 |
| - } |
180 |
| - |
181 |
| - Status = String.Format("Loaded {0}.", url.Href); |
182 |
| - } |
183 |
| - |
184 |
| - #endregion |
185 |
| - } |
186 |
| -} |
| 50 | + }); |
| 51 | + _context = BrowsingContext.New(config); |
| 52 | + _profiler = new ProfilerViewModel(_context); |
| 53 | + _errors = new ErrorsViewModel(_context); |
| 54 | + _dom = new DOMViewModel(); |
| 55 | + _query = new QueryViewModel(); |
| 56 | + _repl = new ReplViewModel(); |
| 57 | + _settings = new SettingsViewModel(); |
| 58 | + _statistics = new StatisticsViewModel(); |
| 59 | + _tree = new TreeViewModel(); |
| 60 | + _sheets = new SheetViewModel(); |
| 61 | + _cts = new CancellationTokenSource(); |
| 62 | + _views = new ITabViewModel[] |
| 63 | + { |
| 64 | + _dom, |
| 65 | + _query, |
| 66 | + _repl, |
| 67 | + _statistics, |
| 68 | + _tree, |
| 69 | + _sheets |
| 70 | + }; |
| 71 | + _logs = new IEventViewModel[] |
| 72 | + { |
| 73 | + _profiler, |
| 74 | + _errors |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + #endregion |
| 79 | + |
| 80 | + #region Properties |
| 81 | + |
| 82 | + public DOMViewModel Dom |
| 83 | + { |
| 84 | + get { return _dom; } |
| 85 | + } |
| 86 | + |
| 87 | + public ErrorsViewModel Errors |
| 88 | + { |
| 89 | + get { return _errors; } |
| 90 | + } |
| 91 | + |
| 92 | + public ProfilerViewModel Profiler |
| 93 | + { |
| 94 | + get { return _profiler; } |
| 95 | + } |
| 96 | + |
| 97 | + public QueryViewModel Queries |
| 98 | + { |
| 99 | + get { return _query; } |
| 100 | + } |
| 101 | + |
| 102 | + public ReplViewModel Console |
| 103 | + { |
| 104 | + get { return _repl; } |
| 105 | + } |
| 106 | + |
| 107 | + public SettingsViewModel Settings |
| 108 | + { |
| 109 | + get { return _settings; } |
| 110 | + } |
| 111 | + |
| 112 | + public StatisticsViewModel Statistics |
| 113 | + { |
| 114 | + get { return _statistics; } |
| 115 | + } |
| 116 | + |
| 117 | + public TreeViewModel Tree |
| 118 | + { |
| 119 | + get { return _tree; } |
| 120 | + } |
| 121 | + |
| 122 | + public SheetViewModel Sheets |
| 123 | + { |
| 124 | + get { return _sheets; } |
| 125 | + } |
| 126 | + |
| 127 | + public String Address |
| 128 | + { |
| 129 | + get { return _address; } |
| 130 | + set |
| 131 | + { |
| 132 | + _address = value; |
| 133 | + RaisePropertyChanged(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public String Status |
| 138 | + { |
| 139 | + get { return _status; } |
| 140 | + set |
| 141 | + { |
| 142 | + _status = value; |
| 143 | + RaisePropertyChanged(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + #endregion |
| 148 | + |
| 149 | + #region Methods |
| 150 | + |
| 151 | + public void Go() |
| 152 | + { |
| 153 | + var url = CreateUrlFrom(_address); |
| 154 | + |
| 155 | + if (_current != null && !_current.IsCompleted) |
| 156 | + { |
| 157 | + _cts.Cancel(); |
| 158 | + _cts = new CancellationTokenSource(); |
| 159 | + } |
| 160 | + |
| 161 | + _profiler.Reset(); |
| 162 | + _current = LoadAsync(url, _cts.Token); |
| 163 | + } |
| 164 | + |
| 165 | + private async Task LoadAsync(Url url, CancellationToken cancel) |
| 166 | + { |
| 167 | + Status = String.Format("Loading {0} ...", url.Href); |
| 168 | + |
| 169 | + foreach (var log in _logs) |
| 170 | + { |
| 171 | + log.Reset(); |
| 172 | + } |
| 173 | + |
| 174 | + var document = await _context.OpenAsync(url, cancel); |
| 175 | + |
| 176 | + foreach (var view in _views) |
| 177 | + { |
| 178 | + view.Document = document; |
| 179 | + } |
| 180 | + |
| 181 | + Status = String.Format("Loaded {0}.", url.Href); |
| 182 | + } |
| 183 | + |
| 184 | + #endregion |
| 185 | + } |
| 186 | +} |
0 commit comments