-
Notifications
You must be signed in to change notification settings - Fork 0
/
uMap.pas
399 lines (334 loc) · 8.54 KB
/
uMap.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
unit uMap;
interface
uses
Classes, SHDocVw, DB, SysUtils, Registry;
const
cId: String = 'id';
cName: String = 'name';
cCount: String = 'cnt';
cLon: String = 'lon';
cLat: String = 'lat';
cPart: String = 'part';
cSide: String = 'side';
type TGeo = class(TObject)
private
fLon: Double;
fLat: Double;
public
property Lon: Double read fLon write fLon;
property Lat: Double read fLat write fLat;
public
function getString: String;
end;
type TDataCoord = class(TObject)
private
fId: Integer;
fName: String;
fCount: Integer;
fGeo: array of array of TGeo;
public
property Id: Integer read fId write fId;
property Name: String read fName write fName;
property Count: Integer read fCount write fCount;
public
procedure setGeo(const aSide: Integer; const aLat, aLon: Double);
function getString: String;
end;
type TMap = class(TObject)
private
fSrcHtmlFile: String;
fSrcName: String;
fHtmlName: String;
fDataSet: TDataSet;
fBrowser: TWebBrowser;
fDataCoord: array of TDataCoord;
fVersion: Integer;
private
function isAllParam: Boolean;
function setHtmlName(const aName: String): String;
function coordJSobjectGet: String;
function createHTML: Boolean;
procedure setRegistry;
procedure RemoveOldHTML;
public
property Actived: Boolean read isAllParam;
public
constructor Create(aHtmlName: String; aDataSet: TDataSet;
aBrowser: TWebBrowser; aVersion: Integer);
destructor Destroy; override;
function ShowData: Boolean;
function GetCode: TStringList;
end;
implementation
uses CustomerService;
{ TMap }
function DeleteFiles(const FileMask: string): Boolean;
var
SearchRec: TSearchRec;
begin
Result := FindFirst(ExpandFileName(FileMask), faAnyFile, SearchRec) = 0;
try
if Result then
repeat
if (SearchRec.Name[1] <> '.') and
(SearchRec.Attr and faVolumeID <> faVolumeID) and
(SearchRec.Attr and faDirectory <> faDirectory) then
begin
Result := DeleteFile(ExtractFilePath(FileMask) + SearchRec.Name);
if not Result then Break;
end;
until FindNext(SearchRec) <> 0;
finally
FindClose(SearchRec);
end;
end;
constructor TMap.Create(aHtmlName: String; aDataSet: TDataSet;
aBrowser: TWebBrowser; aVersion: Integer);
begin
Assert(aHtmlName > '', '{F1BB4F1E-BB4B-47B3-9C49-D9D1CDE63CAD}');
Assert(Assigned(aDataSet), '{01BF71C9-8B39-4854-B631-35315EFFB8AE}');
Assert(Assigned(aBrowser), '{01BF71C9-8B39-4854-B631-35315EFFB8AE}');
fSrcHtmlFile := '04DDBE6B-66C7-4CB1-988E-44847B0C4DCD.h';
fVersion := aVersion;
fHtmlName := setHtmlName(aHtmlName);
fDataSet := aDataSet;
fBrowser := aBrowser;
setRegistry;
FormatSettings.DecimalSeparator := '.';
end;
function TMap.createHTML: Boolean;
var
p, fn, s: String;
sl: TStrings;
begin
Result := False;
p := ExtractFilePath(ParamStr(0));
fn := p + fSrcHtmlFile;
if (not FileExists(fn)) then
Exit;
sl := TStringList.Create;
try
sl.LoadFromFile(p + fSrcHtmlFile);
sl.Text := StringReplace(sl.Text, '%%CoordData%%', coordJSobjectGet, [rfReplaceAll, rfIgnoreCase]);
sl.SaveToFile(p + fHtmlName, TEncoding.UTF8);
finally
FreeAndNil(sl);
end;
Result := True;
end;
destructor TMap.Destroy;
var
f: String;
begin
{
f := ExtractFilePath(ParamStr(0)) + fHtmlName;
if (FileExists(f)) then
DeleteFile(f);
}
inherited;
end;
function TMap.GetCode: TStringList;
var
lst: TStringList;
tbl, row: OleVariant;
i, j, r, c: Integer;
s: String;
begin
lst := TStringList.Create;
tbl := fBrowser.OleObject.Document.GetElementById('export_table');
r := tbl.rows.length;
//2 - ÷òî áû íå áðàòü ñòðîêó èòîãî
for i := 0 to r - 2 do
begin
row := tbl.rows.item(i).getElementsByTagName('TH');
if (row.length = 0) then
row := tbl.rows.item(i).getElementsByTagName('TD');
c := row.length;
s := '';
for j := 0 to c - 1 do
s := s + '~' + row.Item(j).innerText;
s := Copy(s, 2);
lst.Add(s);
end;
Result := lst;
end;
function TMap.coordJSobjectGet: String;
var
f_id, f_name, f_cnt, f_lon, f_lat, f_part, f_side: TField;
id, part, side: Integer;
cd: TDataCoord;
i, k: Integer;
begin
Result := '';
f_id := fDataSet.FindField(cId);
f_name := fDataSet.FindField(cName);
f_cnt := fDataSet.FindField(cCount);
f_lon := fDataSet.FindField(cLon);
f_lat := fDataSet.FindField(cLat);
f_part := fDataSet.FindField(cPart);
f_side := fDataSet.FindField(cSide);
id := -1000000;
part := -1000000;
side := -1000000;
if (fDataSet.Active = False) then
fDataSet.Open;
fDataSet.DisableControls;
try
fDataSet.First;
while not fDataSet.Eof do
begin
if ((id <> f_id.AsInteger) or (part <> f_part.AsInteger)) then
begin
id := f_id.AsInteger;
part := f_part.AsInteger;
cd := TDataCoord.Create;
cd.Id := id;
cd.Name := f_name.AsString;
cd.Count := f_cnt.AsInteger;
k := Length(fDataCoord);
SetLength(fDataCoord, k + 1);
fDataCoord[k] := cd;
end;
cd.setGeo(f_side.AsInteger, f_lat.AsFloat, f_lon.AsFloat);
fDataSet.Next;
end;
finally
fDataSet.EnableControls;
end;
Result := '';
for i := Low(fDataCoord) to High(fDataCoord) do
Result := Result + fDataCoord[i].getString + ',' + #13;
Result := 'var coord = [' + Result + '];';
end;
function TMap.isAllParam: Boolean;
var
f: String;
function isAssigned(const aFieldName: String): Boolean;
begin
Result := Assigned(fDataSet.FieldByName(aFieldName));
end;
begin
Result := False;
f := ExtractFilePath(ParamStr(0)) + fSrcHtmlFile;
if (not FileExists(f)) then
Exit;
if (not fDataSet.Active) then
fDataSet.Open;
if (not isAssigned(cId)) then
Exit;
if (not isAssigned(cName)) then
Exit;
if (not isAssigned(cCount)) then
Exit;
if (not isAssigned(cLat)) then
Exit;
if (not isAssigned(cLon)) then
Exit;
if (not isAssigned(cPart)) then
Exit;
if (not isAssigned(cSide)) then
Exit;
Result := True;
end;
procedure TMap.RemoveOldHTML;
var
m: String;
begin
m := ExtractFilePath(Paramstr(0)) + fSrcName + '*.html';
DeleteFiles(m);
end;
function TMap.setHtmlName(const aName: String): String;
const
cExt: String = '.html';
var
fn: String;
begin
fSrcName := aName;
fn := aName + '_' + IntToStr(fVersion);
Result := ChangeFileExt(fn, cExt);
end;
procedure TMap.setRegistry;
var
r: TRegistry;
key: String;
fn: String;
begin
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
//HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
//key := '\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION\';
key := '\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\';
fn := ExtractFileName(ParamStr(0));
r := TRegistry.Create;
try
//r.RootKey := $80000002; //HKEY_LOCAL_MACHINE;
r.RootKey := $80000001; //HKEY_CURRENT_USER;
if (r.KeyExists(key + fn)) then
Exit;
r.OpenKey(key, true);
r.WriteInteger(fn, 10001);
finally
FreeAndNil(r);
end;
end;
function TMap.ShowData: Boolean;
var
p: String;
begin
Result := False;
p := ExtractFilePath(ParamStr(0)) + fHtmlName;
if (not FileExists(p)) then
begin
RemoveOldHTML;
if (not Actived) then
Exit;
if (not createHTML) then
Exit;
end;
fBrowser.Navigate(p);
Result := True;
end;
{ TCoord }
function TGeo.getString: String;
begin
Result := '[' + FloatToStr(fLat) + ',' + FloatToStr(fLon) + ']';
end;
{ TDataCoord }
function TDataCoord.getString: String;
var
s, z: String;
i, j: Integer;
begin
Result := '';
z := '';
Result := 'id:' + IntToStr(fId) + ',';
Result := Result + 'name:''' + fName + ''',';
Result := Result + 'count:' + IntToStr(fCount) + ',';
//Ñîáèðàåì êîîðäèíàòû
for i := Low(fGeo) to High(fGeo) do
begin
s := '';
for j := Low(fGeo[i]) to High(fGeo[i]) do
begin
s := s + fGeo[i][j].getString + ',';
end;
z := z + '[' + s + '],';
end;
Result := Result + 'geo:[' + z + '],';
Result := Result + 'state:0';
Result := '{' + Result + '}';
end;
procedure TDataCoord.setGeo(const aSide: Integer; const aLat, aLon: Double);
var
c, d: Integer;
g: TGeo;
begin
g := TGeo.Create;
g.Lat := aLat;
g.Lon := aLon;
SetLength(fGeo, aSide);
d := aSide - 1;
c := Length(fGeo[d]);
SetLength(fGeo[d], c + 1);
fGeo[d][c] := g;
end;
end.