Menu

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

Download this file

155 lines (134 with data), 5.5 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2005-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Utility functions that assist in generating HTML code used in creating detail
* pane pages.
}
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.
}
/// <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 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 := AOpenTag(URL, JSFn, Hint, Classes)
+ MakeSafeHTMLText(Text)
+ MakeTag('a', ttClose);
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.