-
Notifications
You must be signed in to change notification settings - Fork 24
/
Cromis.MVC.View.HTML.pas
150 lines (126 loc) · 3.75 KB
/
Cromis.MVC.View.HTML.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
unit Cromis.MVC.View.HTML;
interface
uses
SysUtils, Classes,
Cromis.PageProducer,
// cromis units
Cromis.MVC.View, Cromis.MVC.Common, Cromis.MVC.Model;
type
TMVCViewHTML = class(TInterfacedObject, IMVCView)
private
FViewID: string;
FIsActive: Boolean;
FViewModel: IMVCModel;
FContentType: string;
FSessionData: ISessionData;
function GetSessionData: ISessionData;
procedure SetSessionData(const Value: ISessionData);
protected
procedure ProduceContentPage(const Data: IMVCData;
const FileName: string;
const OnContentTag: TContentTagEvent);
procedure DoOnViewActivated(const Data: IMVCData); virtual;
property ContentType: string read FContentType;
public
property SessionData: ISessionData read GetSessionData write SetSessionData;
procedure UpdateView(const Section, ID: string; const Data: IMVCData); virtual;
procedure Deactivate(const Data: IMVCData = nil); virtual;
procedure Activate(const Data: IMVCData = nil); virtual;
procedure SetViewModel(const Model: IMVCModel);
procedure SetViewID(const Value: string);
function ViewModel: IMVCModel;
function IsActive: Boolean;
function ViewID: string;
procedure Cleanup;
end;
implementation
{ TMVCViewPlain }
procedure TMVCViewHTML.Activate(const Data: IMVCData);
begin
FIsActive := True;
// notify of activation
DoOnViewActivated(Data);
end;
procedure TMVCViewHTML.Cleanup;
begin
FViewModel := nil;
end;
procedure TMVCViewHTML.Deactivate(const Data: IMVCData);
begin
FIsActive := False;
end;
procedure TMVCViewHTML.DoOnViewActivated(const Data: IMVCData);
begin
// nothing to do
end;
function TMVCViewHTML.GetSessionData: ISessionData;
begin
Result := FSessionData;
end;
function TMVCViewHTML.IsActive: Boolean;
begin
Result := FIsActive;
end;
procedure TMVCViewHTML.ProduceContentPage(const Data: IMVCData;
const FileName: string;
const OnContentTag: TContentTagEvent);
var
I: Integer;
Name: string;
FS: TFileStream;
PageProducer: TPageProducerEx;
SessionContent: ISessionContent;
begin
PageProducer := TPageProducerEx.Create;
try
SessionContent := SessionData.SessionContent;
// map MVCData parameters to page parameters
for I := 0 to Data.Parameters.Count - 1 do
begin
Name := Data.Parameters.Items[I].Name;
PageProducer.PageParams.Values[Name] := Data.Parameters.Items[I].AsString;
end;
FS := TFileStream.Create(SessionContent.HTMLRoot + FileName, fmOpenRead or fmShareDenyNone);
try
PageProducer.SourceStream := FS;
PageProducer.OnContentTag := OnContentTag;
PageProducer.ProduceContent(SessionContent.Data);
finally
FS.Free;
end;
finally
PageProducer.Free;
end;
end;
procedure TMVCViewHTML.SetSessionData(const Value: ISessionData);
begin
FSessionData := Value;
end;
procedure TMVCViewHTML.SetViewID(const Value: string);
begin
FViewID := Value;
end;
procedure TMVCViewHTML.SetViewModel(const Model: IMVCModel);
begin
FViewModel := Model;
end;
procedure TMVCViewHTML.UpdateView(const Section, ID: string; const Data: IMVCData);
var
UpdateMethod: TUpdateViewProc;
begin
TMethod(UpdateMethod).Code := Self.MethodAddress(Format('UpdateView_%s', [Section]));
if TMethod(UpdateMethod).Code <> nil then
begin
TMethod(UpdateMethod).Data := Self;
UpdateMethod(ID, Data);
end;
end;
function TMVCViewHTML.ViewID: string;
begin
Result := FViewID;
end;
function TMVCViewHTML.ViewModel: IMVCModel;
begin
Result := FViewModel;
end;
end.