-
Notifications
You must be signed in to change notification settings - Fork 10
/
DW.VCL.InputMoney.pas
228 lines (200 loc) · 7.49 KB
/
DW.VCL.InputMoney.pas
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
unit DW.VCL.InputMoney;
// Created by Delcio 06/10/2016 22:44:56
interface
uses Classes, DW.VCL.CustomInput, DWElementTag, DW.VCL.InputForm;
type
TDWInputMoney = class(TDWCustomTextInput)
private
FAutoComplete: Boolean;
FSymbol: string;
FThousandsSeparator: string;
FDecimalSeparator: string;
FAllowZero: Boolean;
FPrecision: Byte;
FAllowNegative: Boolean;
procedure SetAutoComplete(const Value: Boolean);
procedure SetDecimalSeparator(const Value: string);
procedure SetSymbol(const Value: string);
procedure SetThousandsSeparator(const Value: string);
procedure SetAllowZero(const Value: Boolean);
procedure SetPrecision(const Value: Byte);
procedure SetAllowNegative(const Value: Boolean);
function GetAsCurrency: Currency;
procedure SetAsCurrency(const Value: Currency);
protected
procedure InternalRenderAsync(const AHTMLName: string); override;
procedure InternalRenderHTML(var AHTMLTag: TDWElementTag); override;
procedure InternalSetValue(const ASubmitValue: string; var ATextValue: string;
var ASetFieldValue: Boolean); override;
procedure InternalRenderScript(const AHTMLName: string; AScript: TStringList); override;
public
constructor Create(AOwner: TComponent); override;
published
property AsCurrency: Currency read GetAsCurrency write SetAsCurrency;
// FAutocomplete for websites it is good, but for applications it not intended
property AutoComplete: Boolean read FAutoComplete write SetAutoComplete default False;
// The currençy symbol
property Symbol: string read FSymbol write SetSymbol;
// the Decimal Separator
property DecimalSeparator: string read FDecimalSeparator write SetDecimalSeparator;
// the thousand separator
property ThousandsSeparator: string read FThousandsSeparator write SetThousandsSeparator;
// the number of digits after decimal separator
property Precision: Byte read FPrecision write SetPrecision;
// if true show $ 0,00 instead of blank
property AllowZero: Boolean read FAllowZero write SetAllowZero default True;
// if true allow negative values
property AllowNegative: Boolean read FAllowNegative write SetAllowNegative default False;
end;
implementation
uses
System.SysUtils, System.StrUtils, DWUtils, DW.VCL.Common, DWGlobal, DWTypes;
var
LFormatSettings: TFormatSettings;
{ TIWBSInputMoney }
constructor TDWInputMoney.Create(AOwner: TComponent);
begin
inherited;
FAutoComplete := False;
FSymbol := 'R$';
FDecimalSeparator := ',';
FThousandsSeparator := '.';
FAllowZero := True;
FPrecision := 2;
FAllowNegative := False;
end;
function TDWInputMoney.GetAsCurrency: Currency;
begin
if FText = '' then
Result := 0
else
Result := StrToCurr(FText, LFormatSettings)
end;
procedure TDWInputMoney.InternalRenderAsync(const AHTMLName: string);
var
LValue: string;
begin
inherited;
LValue := StringReplace(FText, FormatSettings.DecimalSeparator, '.',
[rfReplaceAll, rfIgnoreCase]);
if LValue <> '' then
DWApplication.CallBackResp.AddScriptToExecute('$("#' + HTMLName + '").maskMoney(''mask'',' +
LValue + ');', False);
end;
procedure TDWInputMoney.InternalRenderHTML(var AHTMLTag: TDWElementTag);
begin
inherited;
if FIsStatic then
begin
AHTMLTag := TDWElementTag.CreateHTMLTag('p');
try
AHTMLTag.AddClassParam(ActiveCss);
AHTMLTag.AddStringParam('id', HTMLName);
AHTMLTag.AddStringParam('style', ActiveStyle);
AHTMLTag.Contents.AddText(TDWBSCommon.TextToHTML(FText));
except
FreeAndNil(AHTMLTag);
raise;
end;
end
else
begin
AHTMLTag := TDWElementTag.CreateHTMLTag('input');
try
AHTMLTag.AddClassParam(ActiveCss);
AHTMLTag.AddStringParam('id', HTMLName);
AHTMLTag.AddStringParam('name', HTMLName);
AHTMLTag.AddStringParam('type', 'text');
AHTMLTag.AddStringParam('data-prefix', FSymbol);
AHTMLTag.AddStringParam('data-thousands', FThousandsSeparator);
AHTMLTag.AddStringParam('data-decimal', FDecimalSeparator);
AHTMLTag.AddIntegerParam('data-precision', FPrecision);
AHTMLTag.AddBooleanParam('data-allow-zero', FAllowZero);
AHTMLTag.AddBooleanParam('data-allow-negative', FAllowNegative);
// data-symbol="R$ " data-thousands="." data-decimal="," //
if ShowHint and (Hint <> '') then
AHTMLTag.AddStringParam('title', Hint);
if AutoFocus then
AHTMLTag.Add('autofocus');
if IsReadOnly then
AHTMLTag.Add('readonly');
if IsDisabled then
AHTMLTag.Add('disabled');
if MaxLength > 0 then
AHTMLTag.AddIntegerParam('maxlength', MaxLength);
AHTMLTag.AddStringParam('value', TDWBSCommon.TextToHTML(FText));
if Required then
AHTMLTag.Add('required');
if PlaceHolder <> '' then
AHTMLTag.AddStringParam('placeholder', TDWBSCommon.TextToHTML(PlaceHolder));
if TabIndex <> 0 then
AHTMLTag.AddStringParam('tabindex', IntToStr(TabIndex));
AHTMLTag.AddStringParam('autocomplete', IfThen(FAutoComplete, 'on', 'off'));
if (Validator <> nil) and (not(csDesigning in ComponentState)) then
Validator.RenderValidation(AHTMLTag);
AHTMLTag.AddStringParam('style', ActiveStyle);
except
FreeAndNil(AHTMLTag);
raise;
end;
end;
if not(Parent is TDWInputGroup) and (InputType <> bsitHidden) then
AHTMLTag := DWCreateInputFormGroup(Self, Parent, AHTMLTag, Caption, HTMLName);
end;
procedure TDWInputMoney.InternalSetValue(const ASubmitValue: string; var ATextValue: string;
var ASetFieldValue: Boolean);
begin
ATextValue := StringReplace(ASubmitValue, FSymbol, '', [rfIgnoreCase, rfReplaceAll]);
ATextValue := StringReplace(ATextValue, FThousandsSeparator, '', [rfIgnoreCase, rfReplaceAll]);
ATextValue := StringReplace(ATextValue, FDecimalSeparator, LFormatSettings.DecimalSeparator,
[rfIgnoreCase, rfReplaceAll]);
end;
procedure TDWInputMoney.SetAllowNegative(const Value: Boolean);
begin
FAllowNegative := Value;
end;
procedure TDWInputMoney.SetAllowZero(const Value: Boolean);
begin
FAllowZero := Value;
end;
procedure TDWInputMoney.SetAsCurrency(const Value: Currency);
begin
Text := CurrToStr(Value, LFormatSettings)
end;
procedure TDWInputMoney.SetAutoComplete(const Value: Boolean);
begin
FAutoComplete := Value;
end;
procedure TDWInputMoney.SetDecimalSeparator(const Value: string);
begin
FDecimalSeparator := Value;
end;
procedure TDWInputMoney.SetPrecision(const Value: Byte);
begin
FPrecision := Value;
end;
procedure TDWInputMoney.SetSymbol(const Value: string);
begin
FSymbol := Value;
end;
procedure TDWInputMoney.SetThousandsSeparator(const Value: string);
begin
FThousandsSeparator := Value;
end;
procedure TDWInputMoney.InternalRenderScript(const AHTMLName: string; AScript: TStringList);
begin
inherited;
AScript.Add('$(''#{%htmlname%}'').maskMoney();');
end;
initialization
LFormatSettings := TFormatSettings.Create('en-US'); // locale de us
LFormatSettings.DateSeparator := '-';
LFormatSettings.LongDateFormat := 'yyyy-mm-dd';
LFormatSettings.ShortDateFormat := LFormatSettings.LongDateFormat;
LFormatSettings.LongTimeFormat := 'hh:nn:ss';
LFormatSettings.ShortTimeFormat := LFormatSettings.LongTimeFormat;
if DebugHook <> 0 then
IWBSAddGlobalLinkFile('/<dwlibpath>/maskMoney/jquery.maskMoney.js')
else
IWBSAddGlobalLinkFile('/<dwlibpath>/maskMoney/jquery.maskMoney.min.js');
end.