-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathWebPageInteractor.cs
More file actions
196 lines (183 loc) · 6.17 KB
/
Copy pathWebPageInteractor.cs
File metadata and controls
196 lines (183 loc) · 6.17 KB
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
// Copyright (c) 2015 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using Gecko;
using SIL.FieldWorks.Common.FwUtils;
using SIL.FieldWorks.Common.Widgets;
using SIL.FieldWorks.FdoUi;
using SIL.LCModel;
using XCore;
using static SIL.FieldWorks.XWorks.GeneratedHtmlViewer;
namespace SIL.FieldWorks.LexText.Controls
{
public class WebPageInteractor
{
private readonly HtmlControl m_htmlControl;
private readonly Mediator m_mediator;
private readonly LcmCache m_cache;
private readonly FwTextBox m_tbWordForm;
public WebPageInteractor(HtmlControl htmlControl, Mediator mediator, LcmCache cache, FwTextBox tbWordForm)
{
m_htmlControl = htmlControl;
m_mediator = mediator;
m_cache = cache;
m_tbWordForm = tbWordForm;
m_htmlControl.Browser.DomClick += HandleDomClick;
}
protected bool TryGetHvo(GeckoElement element, out int hvo)
{
while (element != null)
{
switch (element.TagName.ToLowerInvariant())
{
case "table":
case "span":
case "th":
case "td":
string id = element.GetAttribute("id");
if (!string.IsNullOrEmpty(id))
return int.TryParse(id, out hvo);
break;
}
element = element.ParentElement;
}
hvo = 0;
return false;
}
protected void HandleDomClick(object sender, DomMouseEventArgs e)
{
if (sender == null || e == null || e.Target == null)
return;
GeckoElement elem = e.Target.CastToGeckoElement();
int hvo;
if (TryGetHvo(elem, out hvo))
JumpToToolBasedOnHvo(hvo);
if (elem.TagName.Equals("input", StringComparison.InvariantCultureIgnoreCase)
&& elem.GetAttribute("type").Equals("button", StringComparison.InvariantCultureIgnoreCase))
{
switch (elem.GetAttribute("name"))
{
case "ShowWordGrammarDetail":
ShowWordGrammarDetail(elem.GetAttribute("id"));
break;
case "TryWordGrammarAgain":
TryWordGrammarAgain(elem.GetAttribute("id"));
break;
case "GoToPreviousWordGrammarPage":
GoToPreviousWordGrammarPage();
break;
}
}
if (elem.TagName.Equals("IMG", StringComparison.InvariantCultureIgnoreCase)
&& m_htmlControl.ParentForm != null && m_htmlControl.ParentForm.OwnedForms.Length > 0)
{
// Try a Word tracing items:
// close any instance of the find dialog when a section is opened or closed because the matches are
// no longer valid or needed.
FindDialog findDlg = (FindDialog)m_htmlControl.ParentForm.OwnedForms[0];
findDlg?.Close();
}
}
/// <summary>
/// Set the current parser to use when tracing
/// </summary>
public XAmpleWordGrammarDebugger WordGrammarDebugger { get; set; }
/// <summary>
/// Have the main FLEx window jump to the appropriate item
/// </summary>
/// <param name="hvo">item whose parent will indcate where to jump to</param>
public void JumpToToolBasedOnHvo(int hvo)
{
if (hvo == 0)
return;
string sTool = null;
int parentClassId = 0;
ICmObject cmo = m_cache.ServiceLocator.GetObject(hvo);
switch (cmo.ClassID)
{
case MoFormTags.kClassId: // fall through
case MoAffixAllomorphTags.kClassId: // fall through
case MoStemAllomorphTags.kClassId: // fall through
case MoInflAffMsaTags.kClassId: // fall through
case MoDerivAffMsaTags.kClassId: // fall through
case MoUnclassifiedAffixMsaTags.kClassId: // fall through
case MoStemMsaTags.kClassId: // fall through
case MoMorphSynAnalysisTags.kClassId: // fall through
case MoAffixProcessTags.kClassId:
sTool = "lexiconEdit";
parentClassId = LexEntryTags.kClassId;
break;
case MoInflAffixSlotTags.kClassId: // fall through
case MoInflAffixTemplateTags.kClassId: // fall through
case PartOfSpeechTags.kClassId:
sTool = "posEdit";
parentClassId = PartOfSpeechTags.kClassId;
break;
// still need to test compound rule ones
case MoCoordinateCompoundTags.kClassId: // fall through
case MoEndoCompoundTags.kClassId: // fall through
case MoExoCompoundTags.kClassId:
sTool = "compoundRuleAdvancedEdit";
parentClassId = cmo.ClassID;
break;
case PhRegularRuleTags.kClassId: // fall through
case PhMetathesisRuleTags.kClassId:
sTool = "PhonologicalRuleEdit";
parentClassId = cmo.ClassID;
break;
case PhPhonemeTags.kClassId:
sTool = "phonemeEdit";
parentClassId = cmo.ClassID;
break;
}
if (parentClassId <= 0)
return; // do nothing
cmo = CmObjectUi.GetSelfOrParentOfClass(cmo, parentClassId);
if (cmo == null)
return; // do nothing
m_mediator.PostMessage("FollowLink", new FwLinkArgs(sTool, cmo.Guid));
}
/// <summary>
/// Show the first pass of the Word Grammar Debugger
/// </summary>
/// <param name="sNodeId">The node id in the XAmple trace to use</param>
public void ShowWordGrammarDetail(string sNodeId)
{
string sForm = AdjustForm(m_tbWordForm.Text);
m_htmlControl.URL = WordGrammarDebugger.SetUpWordGrammarDebuggerPage(sNodeId, sForm, m_htmlControl.URL);
}
/// <summary>
/// Try another pass in the Word Grammar Debugger
/// </summary>
/// <param name="sNodeId">the node id of the step to try</param>
public void TryWordGrammarAgain(string sNodeId)
{
string sForm = AdjustForm(m_tbWordForm.Text);
m_htmlControl.URL = WordGrammarDebugger.PerformAnotherWordGrammarDebuggerStepPage(sNodeId, sForm, m_htmlControl.URL);
}
/// <summary>
/// Back up a page in the Word Grammar Debugger
/// </summary>
/// <remarks>
/// We cannot merely use the history mechanism of the html control
/// because we need to keep track of the xml page source file as well as the html page.
/// This info is kept in the WordGrammarStack.
/// </remarks>
public void GoToPreviousWordGrammarPage()
{
m_htmlControl.URL = WordGrammarDebugger.PopWordGrammarStack();
}
/// <summary>
/// Modify the content of the form to use entities when needed
/// </summary>
/// <param name="sForm">form to adjust</param>
/// <returns>adjusted form</returns>
protected string AdjustForm(string sForm)
{
string sResult1 = sForm.Replace("&", "&");
string sResult2 = sResult1.Replace("<", "<");
return sResult2;
}
}
}