-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDW.VCL.Select2.pas
245 lines (206 loc) · 6.54 KB
/
DW.VCL.Select2.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
unit DW.VCL.Select2;
interface
uses
Classes, System.SysUtils, StrUtils, DB, DW.VCL.Input, DWElementTag;
type
TDWSelect2Item = class(TCollectionItem)
private
FKey: string;
FDisplayText: string;
procedure SetDisplayText(const Value: string);
procedure SetKey(const Value: string);
published
property Key: string read FKey write SetKey;
property DisplayText: string read FDisplayText write SetDisplayText;
end;
TDWSelect2Items = class(TOwnedCollection)
private
function GetItem(Index: Integer): TDWSelect2Item;
procedure SetItem(Index: Integer; const Value: TDWSelect2Item);
public
property Items[Index: Integer]: TDWSelect2Item read GetItem write SetItem;
function Add: TDWSelect2Item;
end;
TAsyncSearchEvent = procedure(Sender: TObject; SeachTerm: TStrings) of object;
TDWSelect2 = class(TDWSelect)
private
FOnAsyncSearch: TAsyncSearchEvent;
// to update script options when Component options are changed
procedure UpdateOptions;
// this event we return a json with the options that the Select2 request
procedure DoOnAsyncSearch(aParams: TStrings; var aReply: string);
procedure SetOnAsyncSearch(const Value: TAsyncSearchEvent);
protected
procedure OnItemsChange(ASender: TObject); override;
procedure InternalRenderHTML(var AHTMLTag: TDWElementTag); override;
procedure InternalRenderStyle(AStyle: TStringList); override;
procedure InternalRenderScript(const AHTMLName: string; AScript: TStringList); override;
procedure InternalRenderAsync(const AHTMLName: string); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function RenderAsync: TDWElementXHTMLTag; override;
function RenderHTML: TDWElementTag; override;
published
property ScriptInsideTag default False;
property OnAsyncSearch: TAsyncSearchEvent read FOnAsyncSearch write SetOnAsyncSearch;
end;
implementation
uses DWUtils, DW.VCL.FluidForm, DWGlobal;
{ TIWBSSelect2 }
constructor TDWSelect2.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
inherited ScriptInsideTag := False;
// UpdateOptions;
end;
destructor TDWSelect2.Destroy;
begin
inherited;
end;
procedure TDWSelect2.DoOnAsyncSearch(aParams: TStrings; var aReply: string);
var
data: string;
line: string;
i: Integer;
begin
if Assigned(FOnAsyncSearch) then
FOnAsyncSearch(Self, aParams);
data := '[';
for i := 0 to Items.Count - 1 do
begin
line := '{"id":"' + InttoStr(i) + '","text":"' + Items.Names[i] + '","value":"' +
Items.ValueFromIndex[i] + '"}';
if i > 0 then
data := data + ',';
data := data + line;
end;
data := data + ']';
aReply := ('{"items": ' + data + '}');
end;
procedure TDWSelect2.InternalRenderAsync(const AHTMLName: string);
var
LSelectedIdx: string;
i: Integer;
begin
inherited;
{ if (FText <> FOldText) then begin
LSelectedIdx := '';
if MultiSelect then
begin
for i := 0 to Length(FItemsSelected)-1 do
if FItemsSelected[i] then begin
if LSelectedIdx <> '' then
LSelectedIdx := LSelectedIdx + ',';
LSelectedIdx := LSelectedIdx + IntToStr(i);
end;
end
else if FItemIndex >= 0 then
LSelectedIdx := IntToStr(FItemIndex);
IWBSExecuteAsyncJScript(AApplication, '$("#'+AHTMLName+'").val(['+LSelectedIdx+']);', False, True);
FOldText := FText;
end; }
end;
procedure TDWSelect2.InternalRenderHTML(var AHTMLTag: TDWElementTag);
begin
inherited;
AHTMLTag.AddStringParam('style', ActiveStyle);
end;
procedure TDWSelect2.InternalRenderScript(const AHTMLName: string; AScript: TStringList);
begin
inherited;
AScript.Add('$(''#{%htmlname%}'').select2({%options%});');
end;
procedure TDWSelect2.InternalRenderStyle(AStyle: TStringList);
begin
inherited;
if ParentContainer is TDWFluidForm then
AStyle.Values['width'] := InttoStr(Width) + 'px';
end;
(* function TIWBSSelect2.IsScriptStored: Boolean;
begin
{ TODO 1 -oDELCIO -cIMPROVEMENT : MOVE TO InternalRenderScripts }
Result := Script.Text <> '$(''#{%htmlname%}'').select2({%options%});';
end; *)
procedure TDWSelect2.OnItemsChange(ASender: TObject);
begin
inherited;
UpdateOptions;
end;
function TDWSelect2.RenderAsync: TDWElementXHTMLTag;
begin
UpdateOptions;
Result := inherited RenderAsync;
end;
function TDWSelect2.RenderHTML: TDWElementTag;
begin
UpdateOptions;
Result := inherited RenderHTML;
end;
procedure TDWSelect2.SetOnAsyncSearch(const Value: TAsyncSearchEvent);
begin
FOnAsyncSearch := Value;
end;
procedure TDWSelect2.UpdateOptions;
var
OptTxt: TStrings;
LRestCallback: string;
begin
OptTxt := TStringList.Create;
try
OptTxt.NameValueSeparator := ':';
OptTxt.Delimiter := ',';
OptTxt.QuoteChar := ' ';
OptTxt.StrictDelimiter := True;
LRestCallback := DWApplication.RegisterRestCallBack(Self, HTMLName + '.dataurl',
DoOnAsyncSearch);
OptTxt.Values['ajax'] := '{url: "' + LRestCallback + '",' + 'delay: 200,' + 'dataType: "json",'
+ 'processResults: function (data) { ' + 'return { results: data.items ' + '};}}';
OptTxt.Values['tags'] := 'true';
OptTxt.Values['placeholder'] := '"Selecione Uma Opção"';
OptTxt.Values['allowclear'] := 'true';
OptTxt.Values['width'] := '"style"'; // To Work with IWBSFluidForm
ScriptParams.Values['options'] := '{' + OptTxt.DelimitedText + '}';
{ if CustomRestEvents.Count = 0 then
CustomRestEvents.Add;
CustomRestEvents[0].EventName := 'dataurl';
CustomRestEvents[0].OnRestEvent := DoOnAsyncSearch; }
finally
OptTxt.Free;
end;
end;
{ TIWBSSelect2Item }
procedure TDWSelect2Item.SetDisplayText(const Value: string);
begin
FDisplayText := Value
end;
procedure TDWSelect2Item.SetKey(const Value: string);
begin
FKey := Value;
end;
{ TIWBSSelect2Items }
function TDWSelect2Items.Add: TDWSelect2Item;
begin
Result := TDWSelect2Item(inherited Add);
end;
function TDWSelect2Items.GetItem(Index: Integer): TDWSelect2Item;
begin
Result := TDWSelect2Item(inherited GetItem(Index));
end;
procedure TDWSelect2Items.SetItem(Index: Integer; const Value: TDWSelect2Item);
begin
inherited SetItem(Index, Value);
end;
initialization
// Enable CSS and JS for Select2 Plugin
if DebugHook <> 0 then
begin
IWBSAddGlobalLinkFile('/<dwlibpath>/select2/css/select2.css');
IWBSAddGlobalLinkFile('/<dwlibpath>/select2/js/select2.full.js');
end
else
begin
IWBSAddGlobalLinkFile('/<dwlibpath>/select2/css/select2.min.css');
IWBSAddGlobalLinkFile('/<dwlibpath>/select2/js/select2.full.min.js');
end;
end.