Menu

[r2017]: / trunk / Src / UHTMLDetailUtils.pas  Maximize  Restore  History

Download this file

325 lines (296 with data), 12.9 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
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
317
318
319
320
321
322
323
{
* UHTMLDetailUtils.pas
*
* Utility functions that assist in generating HTML code used in creating detail
* pane pages.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UHTMLDetailUtils.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UHTMLDetailUtils;
interface
uses
// Project
UHTMLUtils, UIStringList;
function TextLink(const URL, JSFn, Hint: string; Classes: IStringList;
const Text: string): string;
{Creates an <a>..</a> link surrounding some text.
@param URL [in] URL accessed by link. Should be url encoded. Setting URL to
'' creates a link to "javascript:void(0);".
@param JSFn [in] JavaScript code called in tag's onclick event. Setting to
JSFn to '' prevents onclick event being used. When set the onclick event
returns false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names for link's class attribute. May be
nil if no classes required.
@param Text [in] Plain text to be enclosed between tags. Any usupported
characters in the text are converted to HTML entities.
@return Complete <a> tag.
}
function JSLink(const JSFn, Hint: string; Classes: IStringList;
const Text: string): string;
{Creates an <a>..</a> link surrounding some text. The link triggers JavaScript
code and does not access a URL.
@param JSFn [in] JavaScript code called in tag's onclick event. Passing ''
prevents onclick event being used. When set the onclick event returns
false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names for link's class attribute. May be
nil if no classes required.
@param Text [in] Plain text to be enclosed between tags. Any usupported
characters in the text are converted to HTML entities.
@return Complete <a> tag.
}
function SnippetALink(const SnippetName: string;
const UserDefined: Boolean): string;
{Creates an <a>..</a> link that triggers external object's DisplaySnippet
method when clicked. The link does not access a URL. The link's text is the
name of the snippet. The external object's ShowHint method is called to
display a hint containing the snippet's name when the mouse passes over the
link.
@param SnippetName [in] Name of snippet to be displayed when link is
clicked.
@param UserDefined [in] Flag indicating if snippet is user defined.
@return Complete <a> tag.
}
function CategoryALink(const CatID, CatDesc: string): string;
{Creates an <a>..</a> link that triggers external object's DisplayCategory
method when clicked. The link does not access a URL. The link's text is the
name of the category. The external object's ShowHint method is called to
display a hint containing the category's name when the mouse passes over the
link.
@param CatID [in] ID of category to be displayed when link is clicked.
@param CatDesc [in] Description of category used in hint
@return Complete <a> tag.
}
function AOpenTag(const URL, JSFn, Hint: string;
Classes: IStringList): string;
{Creates an opening <a> tag with specified properties.
@param URL [in] URL accessed by link. Should be url encoded. Setting URL to
'' creates a link to "javascript:void(0);".
@param JSFn [in] JavaScript code called in the tag's onclick event. Setting
to '' prevents the onclick event being used. When set the onclick event
returns false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names used in tag's class attribute. May
be nil if no classes required.
@return Required opening <a> tag.
}
/// <summary>Creates HTML attrubites required to produce a roll-over hint with
/// given name.</summary>
function RollOverHintAttrs(const Hint: string): IHTMLAttributes;
implementation
uses
// Delphi
SysUtils, Controls,
// Project
UJavaScriptUtils;
/// <summary>Appends a false return statement to given JavaScript function
/// call and returns combined statement.</summary>
function JSFnWithFalseReturn(const JSFn: string): string;
begin
Result := JSFn + '; return false;';
end;
function AOpenTag(const URL, JSFn, Hint: string;
Classes: IStringList): string;
{Creates an opening <a> tag with specified properties.
@param URL [in] URL accessed by link. Should be url encoded. Setting URL to
'' creates a link to "javascript:void(0);".
@param JSFn [in] JavaScript code called in the tag's onclick event. Setting
to '' prevents the onclick event being used. When set the onclick event
returns false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names used in tag's class attribute. May
be nil if no classes required.
@return Required opening <a> tag.
}
const
cVoid = 'javascript:void(0);'; // used as nul href
var
Attrs: IHTMLAttributes; // tag's attributes
ShortHint: string; // short hint from Hint param
LongHint: string; // long hint from Hint param
begin
Attrs := THTMLAttributes.Create;
// Decode hint string into short and long hints (separated by '|' )
ShortHint := GetShortHint(Hint);
LongHint := GetLongHint(Hint);
// Determine href attribute from URL param
if URL <> '' then
Attrs.Add('href', URL)
else
Attrs.Add('href', cVoid);
// Set title attribute if short hint provided
if ShortHint <> '' then
Attrs.Add('title', ShortHint);
// Set class attribute if provided
if Assigned(Classes) and (Classes.Count > 0) then
Attrs.Add('class', Classes);
// Set onclick event if JavaScript function provided
if JSFn <> '' then
Attrs.Add('onclick', JSFnWithFalseReturn(JSFn));
// Set onmouseover and onmouseout to external ShowHint if long hint provided
if LongHint <> '' then
Attrs.Append(RollOverHintAttrs(LongHint));
// Build and return the tag
Result := MakeTag('a', ttOpen, Attrs);
end;
function RollOverHintAttrs(const Hint: string): IHTMLAttributes;
begin
Result := THTMLAttributes.Create;
if Hint = '' then
Exit;
Result.Add(
'onmouseover', JSFnWithFalseReturn(JSLiteralFunc('showHint', [Hint]))
);
Result.Add(
'onmouseout', JSFnWithFalseReturn(JSLiteralFunc('clearHint', []))
);
end;
function ALink(const URL, JSFn, Hint: string; Classes: IStringList;
const InnerHTML: string): string;
{Creates a hyper-link compound tag surrounding some inner HTML.
@param URL [in] URL accessed by link. Should be url encoded. Setting URL to
'' creates a link to "javascript:void(0);".
@param JSFn [in] JavaScript code called in the tag's onclick event. Setting
to '' prevents the onclick event being used. When set the onclick event
returns false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names used in tag's class attribute. May
be nil if no classes required.
@param InnerHTML [in] HTML to be included within link. Must be valid HTML
code.
@return Required HTML tag.
}
begin
Result := AOpenTag(URL, JSFn, Hint, Classes)
+ InnerHTML
+ MakeTag('a', ttClose)
end;
function TextLink(const URL, JSFn, Hint: string; Classes: IStringList;
const Text: string): string;
{Creates an <a>..</a> link surrounding some text.
@param URL [in] URL accessed by link. Should be url encoded. Setting URL to
'' creates a link to "javascript:void(0);".
@param JSFn [in] JavaScript code called in the tag's onclick event. Setting
to '' prevents the onclick event being used. When set the onclick event
returns false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names used in tag's class attribute. May
be nil if no classes required.
@param Text [in] Plain text to be enclosed between tags. Any usupported
characters in the text are converted to HTML entities.
@return Complete <a> tag.
}
begin
Result := ALink(URL, JSFn, Hint, Classes, MakeSafeHTMLText(Text));
end;
function JSLink(const JSFn, Hint: string; Classes: IStringList;
const Text: string): string;
{Creates an <a>..</a> link surrounding some text. The link triggers JavaScript
code and does not access a URL.
@param JSFn [in] JavaScript code called in the tag's onclick event. Setting
to '' prevents the onclick event being used. When set the onclick event
returns false.
@param Hint [in] Standard Delphi hint string, in form short-hint|long-hint.
Link's title attribute is set to short-hint while if long-hint is provided
the external object's ShowHint method is called with long-hint when mouse
passes over the link.
@param Classes [in] List of class names used in tag's class attribute. May
be nil if no classes required.
@param Text [in] Plain text to be enclosed between tags. Any usupported
characters in the text are converted to HTML entities.
@return Complete <a> tag.
}
begin
Result := TextLink('', JSFn, Hint, Classes, Text);
end;
function SnippetALink(const SnippetName: string;
const UserDefined: Boolean): string;
{Creates an <a>..</a> link that triggers external object's DisplaySnippet
method when clicked. The link does not access a URL. The link's text is the
name of the snippet. The external object's ShowHint method is called to
display a hint containing the snippet's name when the mouse passes over the
link.
@param SnippetName [in] Name of snippet to be displayed when link is
clicked.
@param UserDefined [in] Flag indicating if snippet is user defined.
@return Complete <a> tag.
}
resourcestring
// Long hint
sSnippetHint = 'Display "%s"'; // long hint
begin
// Create javascript link enclosing snippet name
Result := JSLink(
JSLiteralFunc('displaySnippet', [SnippetName, UserDefined]),
'|' + Format(sSnippetHint, [SnippetName]),
TIStringList.Create('snippet-link'),
SnippetName
);
end;
function CategoryALink(const CatID, CatDesc: string): string;
{Creates an <a>..</a> link that triggers external object's DisplayCategory
method when clicked. The link does not access a URL. The link's text is the
name of the category. The external object's ShowHint method is called to
display a hint containing the category's name when the mouse passes over the
link.
@param CatID [in] ID of category to be displayed when link is clicked.
@param CatDesc [in] Description of category used in hint
@return Complete <a> tag.
}
resourcestring
sCategoryHint = 'Display "%s" category'; // long hint
begin
// Create javascript link enclosing category name
Result := JSLink(
JSLiteralFunc('displayCategory', [CatID]),
'|' + Format(sCategoryHint, [CatDesc]),
TIStringList.Create('category-link'),
CatDesc
);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.