* Additional event

This commit is contained in:
Michaël Van Canneyt 2023-03-12 20:42:58 +01:00
parent fd444710d8
commit 561f965d98

View File

@ -5,6 +5,7 @@ interface
uses sysutils, classes, web, Rtl.HTMLEventNames, Rtl.HTMLActions, db; uses sysutils, classes, web, Rtl.HTMLEventNames, Rtl.HTMLActions, db;
Type Type
TDBCustomHTMLElementAction = class;
TDBCustomHTMLInputElementAction = Class; TDBCustomHTMLInputElementAction = Class;
TDBCustomHTMLButtonElementAction = Class; TDBCustomHTMLButtonElementAction = Class;
@ -14,7 +15,7 @@ Type
private private
FField: TField; FField: TField;
FFieldName: string; FFieldName: string;
FAction : TDBCustomHTMLInputElementAction; FAction : TDBCustomHTMLElementAction;
procedure SetFieldName(AValue: string); procedure SetFieldName(AValue: string);
protected protected
procedure DatasetChanged; override; procedure DatasetChanged; override;
@ -25,12 +26,12 @@ Type
procedure UpdateData; override; procedure UpdateData; override;
procedure FocusControl(aField: JSValue); Override; procedure FocusControl(aField: JSValue); Override;
public public
constructor Create(aAction : TDBCustomHTMLInputElementAction); constructor Create(aAction : TDBCustomHTMLElementAction);
function Edit: Boolean; override; function Edit: Boolean; override;
Function CanModify : Boolean; Function CanModify : Boolean;
Procedure Bind; Procedure Bind;
Procedure UnBind; Procedure UnBind;
property Action: TDBCustomHTMLInputElementAction read FAction; property Action: TDBCustomHTMLElementAction read FAction;
property Field: TField read FField; property Field: TField read FField;
property FieldName: string read FFieldName write SetFieldName; property FieldName: string read FFieldName write SetFieldName;
end; end;
@ -38,37 +39,81 @@ Type
{ TDBCustomHTMLInputElementAction } { TDBCustomHTMLInputElementAction }
TDBCustomHTMLInputElementAction = class(THTMLCustomElementAction) { TDBCustomHTMLElementAction }
TFieldTextData = Record
Field : TField;
Value : String;
end;
TGetFieldTextEvent = procedure(Sender : TObject; var aData : TFieldTextData) of object;
TDBCustomHTMLElementAction = class(THTMLCustomElementAction)
Private Private
FLink : THTMLActionDataLink; FLink : THTMLActionDataLink;
FOnEndEditing: TNotifyEvent; FOnEndEditing: TNotifyEvent;
FOnLayoutChanged: TNotifyEvent; FOnLayoutChanged: TNotifyEvent;
FOnStartEditing: TNotifyEvent; FOnStartEditing: TNotifyEvent;
FOnGetFieldText: TGetFieldTextEvent;
function GetDataSource: TDatasource; function GetDataSource: TDatasource;
function GetField: TField; function GetField: TField;
function GetFieldName: String; function GetFieldName: String;
procedure SetDatasource(AValue: TDatasource); procedure SetDatasource(AValue: TDatasource);
procedure SetFieldName(AValue: String); procedure SetFieldName(AValue: String);
function TransformFieldText(F: TField; const Value: String): String;
Protected Protected
procedure DoKeyDown(aEvent: TJSEvent); virtual; procedure DoKeyDown(aEvent: TJSEvent); virtual;
Procedure ActiveChanged; virtual; Procedure ActiveChanged; virtual;
Procedure StartEditing; virtual; Procedure StartEditing; virtual;
Procedure EndEditing; virtual; Procedure EndEditing; virtual;
Procedure LayoutChanged; virtual; Procedure LayoutChanged; virtual;
procedure CheckMaxLength; virtual;
Property Link : THTMLActionDataLink Read FLink; Property Link : THTMLActionDataLink Read FLink;
Public Public
Constructor Create(aOwner : TComponent); override; Constructor Create(aOwner : TComponent); override;
Destructor Destroy; override; Destructor Destroy; override;
procedure CheckMaxLength;
Procedure ElementToDataset; virtual; Procedure ElementToDataset; virtual;
Procedure DatasetToElement; virtual; Procedure DatasetToElement; virtual;
Procedure BindEvents(aEl : TJSElement); override;
Property Field : TField Read GetField; Property Field : TField Read GetField;
Property Datasource : TDatasource Read GetDataSource Write SetDatasource; Property Datasource : TDatasource Read GetDataSource Write SetDatasource;
Property FieldName : String Read GetFieldName Write SetFieldName; Property FieldName : String Read GetFieldName Write SetFieldName;
Property OnStartEditing : TNotifyEvent Read FOnStartEditing Write FOnStartEditing; Property OnStartEditing : TNotifyEvent Read FOnStartEditing Write FOnStartEditing;
Property OnEndEditing : TNotifyEvent Read FOnEndEditing Write FOnEndEditing; Property OnEndEditing : TNotifyEvent Read FOnEndEditing Write FOnEndEditing;
Property OnLayoutChanged : TNotifyEvent Read FOnLayoutChanged Write FOnLayoutChanged; Property OnLayoutChanged : TNotifyEvent Read FOnLayoutChanged Write FOnLayoutChanged;
Property OnGetFieldText : TGetFieldTextEvent Read FOnGetFieldText Write FOnGetFieldText;
end;
TDBHTMLElementAction = class(TDBCustomHTMLElementAction)
Published
Property Events;
Property CustomEvents;
Property ElementID;
Property CSSSelector;
Property PreventDefault;
Property StopPropagation;
Property OnExecute;
Property BeforeBind;
Property AfterBind;
Property Datasource;
Property FieldName;
Property OnStartEditing;
Property OnEndEditing;
Property OnLayoutChanged;
Property OnGetFieldText;
end;
TDBCustomHTMLInputElementAction = class(TDBCustomHTMLElementAction)
Private
procedure DoKeyDown(aEvent: TJSEvent); virtual;
Procedure ActiveChanged; override;
Procedure StartEditing; override;
Procedure EndEditing; override;
Procedure LayoutChanged; override;
Property Link : THTMLActionDataLink Read FLink;
Public
procedure CheckMaxLength; override;
Procedure ElementToDataset; override;
Procedure DatasetToElement; override;
Procedure BindEvents(aEl : TJSElement); override;
end; end;
TDBHTMLInputElementAction = class(TDBCustomHTMLInputElementAction) TDBHTMLInputElementAction = class(TDBCustomHTMLInputElementAction)
@ -87,6 +132,7 @@ Type
Property OnStartEditing; Property OnStartEditing;
Property OnEndEditing; Property OnEndEditing;
Property OnLayoutChanged; Property OnLayoutChanged;
Property OnGetFieldText;
end; end;
{ TButtonActionDataLink } { TButtonActionDataLink }
@ -312,35 +358,40 @@ begin
CheckButtonState; CheckButtonState;
end; end;
{ TDBCustomHTMLInputElementAction } { TDBCustomHTMLElementAction }
function TDBCustomHTMLInputElementAction.GetDataSource: TDatasource; function TDBCustomHTMLElementAction.GetDataSource: TDatasource;
begin begin
Result:=Link.DataSource; Result:=Link.DataSource;
end; end;
function TDBCustomHTMLInputElementAction.GetField: TField; function TDBCustomHTMLElementAction.GetField: TField;
begin begin
Result:=Link.Field; Result:=Link.Field;
end; end;
function TDBCustomHTMLInputElementAction.GetFieldName: String; function TDBCustomHTMLElementAction.GetFieldName: String;
begin begin
Result:=Link.FieldName; Result:=Link.FieldName;
end; end;
procedure TDBCustomHTMLInputElementAction.SetDatasource(AValue: TDatasource); procedure TDBCustomHTMLElementAction.SetDatasource(AValue: TDatasource);
begin begin
if aValue=Link.DataSource then exit; if aValue=Link.DataSource then exit;
Link.Datasource:=aValue; Link.Datasource:=aValue;
end; end;
procedure TDBCustomHTMLInputElementAction.SetFieldName(AValue: String); procedure TDBCustomHTMLElementAction.SetFieldName(AValue: String);
begin begin
Link.FieldName:=aValue; Link.FieldName:=aValue;
end; end;
procedure TDBCustomHTMLInputElementAction.ActiveChanged; procedure TDBCustomHTMLElementAction.DoKeyDown(aEvent: TJSEvent);
begin
end;
procedure TDBCustomHTMLElementAction.ActiveChanged;
begin begin
if Link.Active then if Link.Active then
begin begin
@ -355,93 +406,86 @@ begin
EndEditing; EndEditing;
end; end;
procedure TDBCustomHTMLInputElementAction.StartEditing; procedure TDBCustomHTMLElementAction.StartEditing;
begin begin
if Element is TJSHTMLInputElement then
TJSHTMLInputElement(Element).readOnly:=Link.ReadOnly;
if Assigned(FOnStartEditing) then if Assigned(FOnStartEditing) then
FOnStartEditing(Self); FOnStartEditing(Self);
end; end;
procedure TDBCustomHTMLInputElementAction.EndEditing;
procedure TDBCustomHTMLElementAction.EndEditing;
begin begin
if Element is TJSHTMLInputElement then
TJSHTMLInputElement(Element).readOnly:=Link.ReadOnly;
if Assigned(FOnEndEditing) then if Assigned(FOnEndEditing) then
FOnEndEditing(Self); FOnEndEditing(Self);
end; end;
procedure TDBCustomHTMLInputElementAction.LayoutChanged; procedure TDBCustomHTMLElementAction.LayoutChanged;
begin begin
If Assigned(FOnLayoutChanged) then If Assigned(FOnLayoutChanged) then
FOnLayoutChanged(Self); FOnLayoutChanged(Self);
end; end;
constructor TDBCustomHTMLInputElementAction.Create(aOwner: TComponent); constructor TDBCustomHTMLElementAction.Create(aOwner: TComponent);
begin begin
inherited Create(aOwner); inherited Create(aOwner);
FLink:=THTMLActionDataLink.Create(Self); FLink:=THTMLActionDataLink.Create(Self);
end; end;
destructor TDBCustomHTMLInputElementAction.Destroy; destructor TDBCustomHTMLElementAction.Destroy;
begin begin
FreeAndNil(FLink); FreeAndNil(FLink);
inherited Destroy; inherited Destroy;
end; end;
procedure TDBCustomHTMLInputElementAction.ElementToDataset; procedure TDBCustomHTMLElementAction.ElementToDataset;
Var
F : TField;
E : TJSHTMLElement;
EI : TJSHTMLInputElement absolute E;
begin begin
F:=Field; // Do nothing.
E:=Element;
if Not Assigned(F) then
exit;
if E is TJSHTMLInputElement then
begin
if (EI._type='checkbox') then
F.AsBoolean:=EI.Checked
else if SameText(EI._type,'date')then
Field.AsDateTime:=ExtractDate(EI.value)
else if SameText(EI._type,'time')then
Field.AsDateTime:=ExtractTime(EI.value)
else
F.AsString:=Value;
end
else
F.AsString:=Value;
end; end;
procedure TDBCustomHTMLInputElementAction.DatasetToElement; procedure TDBCustomHTMLElementAction.CheckMaxLength;
begin
// Do nothing
end;
Function TDBCustomHTMLElementAction.TransformFieldText(F : TField; const Value: String) : String;
Var
D : TFieldTextData;
begin
if not Assigned(FOnGetFieldText) then
Result:=Value
else
begin
D.Value:=Value;
D.Field:=F;
FOnGetFieldText(Self,D);
Result:=D.Value;
end;
end;
procedure TDBCustomHTMLElementAction.DatasetToElement;
Var Var
F : TField; F : TField;
E : TJSHTMLElement; E : TJSHTMLElement;
EI : TJSHTMLInputElement absolute E;
begin begin
F:=Field; F:=Field;
E:=Element; E:=Element;
if Not Assigned(F) then if Not (Assigned(F) and assigned(E)) then
Exit; Exit;
if E is TJSHTMLInputElement then Value:=TransFormFieldText(Field,F.AsString);
begin end;
if (EI._type='checkbox') then
EI.Checked:=F.AsBoolean
else if SameText(EI._type,'date') then procedure TDBCustomHTMLInputElementAction.BindEvents(aEl: TJSElement);
EI.Value:=FormatHTMLDate(F.AsDateTime) begin
else if SameText(EI._type,'time') then inherited BindEvents(aEl);
EI.Value:=FormatHTMLTime(F.AsDateTime) aEl.addEventListener(sEventKeyDown,@DoKeyDown);
else
Value:=Field.AsString;
end
else
Self.Value:=Field.AsString;
end; end;
procedure TDBCustomHTMLInputElementAction.DoKeyDown(aEvent : TJSEvent); procedure TDBCustomHTMLInputElementAction.DoKeyDown(aEvent : TJSEvent);
@ -457,12 +501,32 @@ begin
end; end;
end; end;
procedure TDBCustomHTMLInputElementAction.BindEvents(aEl: TJSElement); procedure TDBCustomHTMLInputElementAction.ActiveChanged;
begin begin
inherited BindEvents(aEl); inherited ActiveChanged;
aEl.addEventListener(sEventKeyDown,@DoKeyDown);
end; end;
procedure TDBCustomHTMLInputElementAction.StartEditing;
begin
if Element is TJSHTMLInputElement then
TJSHTMLInputElement(Element).readOnly:=Link.ReadOnly;
Inherited;
end;
procedure TDBCustomHTMLInputElementAction.EndEditing;
begin
Inherited;
if Element is TJSHTMLInputElement then
TJSHTMLInputElement(Element).readOnly:=Link.ReadOnly;
end;
procedure TDBCustomHTMLInputElementAction.LayoutChanged;
begin
inherited LayoutChanged;
end;
{ THTMLActionDataLink } { THTMLActionDataLink }
procedure THTMLActionDataLink.SetFieldName(AValue: string); procedure THTMLActionDataLink.SetFieldName(AValue: string);
@ -517,7 +581,7 @@ begin
Action.FocusControl; Action.FocusControl;
end; end;
constructor THTMLActionDataLink.Create(aAction: TDBCustomHTMLInputElementAction); constructor THTMLActionDataLink.Create(aAction: TDBCustomHTMLElementAction);
begin begin
Inherited Create; Inherited Create;
FAction:=aAction; FAction:=aAction;
@ -552,6 +616,60 @@ begin
iel.maxLength:=Field.Size; iel.maxLength:=Field.Size;
end; end;
procedure TDBCustomHTMLInputElementAction.ElementToDataset;
Var
F : TField;
E : TJSHTMLElement;
EI : TJSHTMLInputElement absolute E;
begin
F:=Field;
E:=Element;
if Not Assigned(F) then
exit;
if E is TJSHTMLInputElement then
begin
if (EI._type='checkbox') then
F.AsBoolean:=EI.Checked
else if SameText(EI._type,'date')then
Field.AsDateTime:=ExtractDate(EI.value)
else if SameText(EI._type,'time')then
Field.AsDateTime:=ExtractTime(EI.value)
else
F.AsString:=Value;
end
else
F.AsString:=Value;
end;
procedure TDBCustomHTMLInputElementAction.DatasetToElement;
Var
F : TField;
E : TJSHTMLElement;
EI : TJSHTMLInputElement absolute E;
begin
F:=Field;
E:=Element;
if Not (Assigned(F) and Assigned(E)) then
Exit;
Self.Value:=TransFormFieldText(Field,F.AsString);
if E is TJSHTMLInputElement then
begin
if (EI._type='checkbox') then
EI.Checked:=F.AsBoolean
else if SameText(EI._type,'date') then
EI.Value:=TransFormFieldText(F,FormatHTMLDate(F.AsDateTime))
else if SameText(EI._type,'time') then
EI.Value:=TransFormFieldText(F,FormatHTMLTime(F.AsDateTime))
else
Value:=TransFormFieldText(F,F.AsString);
end
else
Inherited
end;
procedure THTMLActionDataLink.Bind; procedure THTMLActionDataLink.Bind;
begin begin