[Pas2js] WebForm Template issue

warleyalex warleyalex at yahoo.com.br
Tue Apr 3 05:35:10 CEST 2018


When you create a pas2js web-browser based project, the wizard creates the
main project.lpr file with a start main HTML page. Now, the idea is create
fully functional project with multiple "WebForms". 

Assuming "WebForm" is basically a lazarus "form unit". However, it should
also have an associated HTML page! you can use F12 to switch between the
code editor and form designer for the form of the associated Unit.
 
When I click File -> New WebForm, to add a second form (Form2) to the
project. The form template generates Form2 associated Unit (Unit2) and add
"Unit2" to the project uses clause. In addiction, the form template "should"
also create an associate HTML page "Unit2.html"

Unit1.pas + Unit1.lfm + Unit1.html => for the main form
Unit2.pas + Unit2.lfm + Unit2.html => for the second form

So, the idea is the same easy-to-use centralized place for add forms into
the current project. You go to
File -> New... -> TWebForm to add forms to the active project.

I have made some changes into the custom form project (to mimic the webform
template), using the following code. It successfully creates and register
the form unit to the project, but unfortunately I couldn't find a way to
"create and add the associated HTML file" when you add the webform, the
webform template should create this HTML file. 

Anyone can shed some lights on this?

---------------
unit webforms;

{$mode objfpc}{$H+}

interface

uses
  LazIDEIntf,
  Classes, SysUtils, Forms;

Type

  { TCustomFormDescr }

  TCustomFormDescr = Class
  private
    FAuthor: String;
    FCaption: String;
    FCategory: String;
    FDescription: String;
    FFormClass: TFormClass;
    FLazPackage: String;
    FUnitName: String;
  public
    Constructor Create(AFormClass : TFormClass; const APackage: string);
    Constructor Create(AFormClass : TFormClass; Const
ACaption,ADescription,AUnit,APackage : String);
    Property FormClass : TFormClass Read FFormClass Write FFormClass;
    Property Caption : String Read FCaption Write FCaption;
    Property Description : String Read FDescription Write FDescription;
    Property UnitName : String Read FUnitName Write FUnitName;
    Property Category : String Read FCategory Write FCategory;
    Property Author : String Read FAuthor Write FAuthor;
    Property LazPackage : String Read FLazPackage Write FLazPackage;
  end;

Procedure RegisterCustomForm(Descr : TCustomFormDescr);
Procedure RegisterCustomForm(AFormClass : TFormClass; const APackage:
string);
Procedure RegisterCustomForm(AFormClass : TFormClass; Const AUnitName,
APackage : String);

Procedure Register;

implementation

uses ProjectIntf,NewItemIntf,contnrs;

Const
  SAppFrameWork = 'WebForm';
  SInstanceOf   = 'Create a new Web Form';

{ TCustomFormDescr }

constructor TCustomFormDescr.Create(AFormClass: TFormClass;
  const APackage: string);

Var
  N,U : String;

begin
  N:=AFormClass.ClassName;
  U:=N;
  If (Upcase(U[1])='T') then
    Delete(U,1,1);
  Create(AFormClass,N,Format(SInstanceOf,[N]),U,APackage);
end;

constructor TCustomFormDescr.Create(AFormClass: TFormClass;
  const ACaption, ADescription, AUnit, APackage: String);
begin
  FFormClass:=AFormClass;
  FCaption:=ACaption;
  FDescription:=ADescription;
  FUnitName:=AUnit;
  FCategory:=SAppFrameWork;
  FLazPackage:=APackage;
end;

Type
  { TCustomFormFileDescriptor }
  TCustomFormFileDescriptor = Class(TFileDescPascalUnitWithResource)
  private
    FFormDescr: TCustomFormDescr;
  Public
    Constructor Create(ADescr : TCustomFormDescr); overload;
    Property FormDescr : TCustomFormDescr Read FFormDescr;
    Function GetLocalizedName : String; override;
    Function GetLocalizedDescription : String; override;
    Function GetInterfaceUsesSection : String; override;
    function GetInterfaceSource(const Filename,
      SourceName, ResourceName: string): string; override;
    function GetImplementationSource(const Filename,
  SourceName, ResourceName: string): string; override;
  end;

{ TCustomFormFileDescriptor }

constructor TCustomFormFileDescriptor.Create(ADescr: TCustomFormDescr);
begin
  Inherited Create;
  FFormDescr:=ADescr;
  ResourceClass:=FFormDescr.FFormClass;
  Name:=FFormDescr.Caption;
  RequiredPackages:=ADescr.LazPackage;
end;

function TCustomFormFileDescriptor.GetLocalizedName: String;
begin
  Result:=FFormDescr.Caption;
end;

function TCustomFormFileDescriptor.GetLocalizedDescription: String;
begin
  Result:=FFormDescr.Description;
  If (FFormDescr.Author<>'') then
    Result:=Result+LineEnding+'By '+FFormDescr.Author;
end;

function TCustomFormFileDescriptor.GetInterfaceUsesSection: String;
begin
  Result:=inherited GetInterfaceUsesSection;
  Result:='SysUtils, Classes,'+LineEnding
          +'{$IFDEF Pas2JS}'+LineEnding
          +'  JS, Web, WEBLib.Forms, WEBLIB.Controls, WEBLib.Graphics,
WEBLib.StdCtrls,'+LineEnding
          +'  WEBLib.ExtCtrls, WEBLib.Dialogs'+LineEnding
          +'{$ELSE}'+LineEnding
          +'  WebForms, Controls, Graphics, StdCtrls, ExtCtrls,
Dialogs'+LineEnding
          +'{$ENDIF}'+LineEnding
          +'  { you can add units after this }'
end;

function TCustomFormFileDescriptor.GetInterfaceSource(const Filename,
  SourceName, ResourceName: string): string;
const
  LE = LineEnding;
begin
  Result:=
     'type'+LE
    +'  T'+ResourceName+' = class('+ResourceClass.ClassName+')'+LE
    +'  private'+LE
    +'    { Private declarations }'+LE
    +'  protected'+LE
    +'    { Protected declarations }'+LE
    +'    procedure LoadDFMValues; override;'+LE
    +'  public'+LE
    +'    { Public declarations }'+LE
    +'  end;'+LE
    +LE;

  if DeclareClassVariable then
    Result := Result +
     'var'+LE
    +'  '+ResourceName+': T'+ResourceName+';'+LE
    +LE;
end;

function TCustomFormFileDescriptor.GetImplementationSource(const Filename,
  SourceName, ResourceName: string): string;
Var
  Src : TStrings;
begin
  Src:=TStringList.Create;
  try
    Result:=inherited GetImplementationSource(Filename, SourceName,
ResourceName);
    With Src do
      begin
      Add(Result);
      Add('Procedure T'+ResourceName+'.LoadDFMValues;');
      Add('begin');
      Add('inherited;');
      Add('  {$I '+ResourceName+'.lfm.inc}');
      Add('end;');
      Add('');
      Add('');
      Add('initialization');
      Add('{$IFNDEF Pas2JS}');
      Add('  RegisterClass(T'+ResourceName+');');
      Add('{$ENDIF}');
      Result:=Text;
      end;
  finally
    Src.Free;
  end;
end;

Var
  CustomFormList : TObjectList;

Procedure RegisterCustomForm(Descr : TCustomFormDescr);

begin
  CustomFormList.Add(Descr);
end;

Procedure RegisterCustomForm(AFormClass : TFormClass; const APackage:
string);
begin
  RegisterCustomForm(TCustomFormDescr.Create(AFormClass,APackage));
end;

Procedure RegisterCustomForm(AFormClass : TFormClass; Const AUnitName,
APackage : String);
Var
  D : TCustomFormDescr;
begin
  D:=TCustomFormDescr.Create(AFormClass,APackage);
  D.UnitName:=AUnitName; 
  RegisterCustomForm(D);
end;

Procedure Register;
var
  L : TStringList;
  I : Integer;
  D : TCustomFormDescr;
begin
  L:=TStringList.Create;
  Try
    L.Sorted:=True;
    L.Duplicates:=dupIgnore;
    For I:=0 to CustomFormList.Count-1 do
      L.Add(TCustomFormDescr(CustomFormList[i]).Category);
    For I:=0 to L.Count-1 do
    begin
      RegisterNewItemCategory(TNewIDEItemCategory.Create(L[i]));
    end;
  Finally
    L.Free;
  end;
  For I:=0 to CustomFormList.Count-1 do
    begin
    D:=TCustomFormDescr(CustomFormList[i]);
   
RegisterProjectFileDescriptor(TCustomFormFileDescriptor.Create(D),D.Category);
    end;
end;

Procedure InitCustomForms;

begin
  CustomFormList:=TObjectList.Create;
end;

Procedure DoneCustomForms;

begin
  FreeAndNil(CustomFormList);
end;

Initialization
  InitCustomForms;
Finalization
  DoneCustomForms;
end.
---------------




--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list