[Pas2js] External class with custom event

Luca Olivetti luca at ventoso.org
Mon May 22 12:14:51 CEST 2023


El 22/5/23 a les 10:49, Michael Van Canneyt via Pas2js ha escrit:

> You can reuse a external name.

OK, but I think there is no need to (see below)


> 
> The chatGPT suggestion is not quite correct.

Well, yes, the original wrapper for the class was quite buggy and 
incomplete, but it put me on the right track. Besides, it found all the 
methods of the Choices class by just giving it the github url.

Anyway, this is what I came up with:

   TChoices = class external name 'Choices'
   public
     constructor new(element: TJSObject; options: JSValue);
     procedure init;
     procedure destroy;
     procedure clear;
     function getValue(pick: Boolean = False): JSValue;
     procedure setValue(value: JSValue);
     function isActiveElement: Boolean;
     procedure highlightItem(item: TJSNode; runEvent: Boolean = True);
     procedure unhighlightItem(item: TJSNode; runEvent: Boolean = True);
     procedure highlightAll(runEvent: Boolean = True);
     procedure unhighlightAll(runEvent: Boolean = True);
     procedure removeActiveItemsByValue(value: JSValue; runEvent: 
Boolean = True);
     procedure removeActiveItems(runEvent: Boolean = True);
     procedure enable;
     procedure disable;
     procedure clearStore;
     procedure clearInput(runEvent: Boolean = True);
     procedure setChoices(choices: array of JSValue; value: JSValue = 
nil; label_: JSValue = nil; replaceChoices: Boolean = False);
     procedure setChoiceByValue(value: JSValue; runEvent: Boolean = True);
     procedure clearChoices;
   end;


   TChoice = class (TJsObject)
   private
     FActive: boolean; external name 'active';
     FDisabled: boolean; external name 'disabled';
     FElementId: string; external name 'elementId';
     FgroupId: integer; external name 'groupId';
     Fid: integer; external name 'id';
     FKeyCode: integer; external name 'keyCode';
     FLabel: string; external name 'label';
     FPlaceholder: boolean; external name 'placeholder';
     FScore:integer; external name 'score';
     FSelected:boolean; external name 'selected';
     FValue:string; external name 'value';
   public
     property Active:boolean read FActive;
     property Disabled:boolean read FDisabled;
     property ElementId:string read FElementId;
     property GroupId:integer read FGroupId;
     property Id:integer read FId;
     property KeyCode:integer read FKeyCode;
     property Label_:string read FLabel;
     property Placeholder:boolean read FPlaceHolder;
     property Score:integer read FScore;
     property Value:string read FValue;
   end;

   TChoiceEvent = class(TJsEvent)
   private
     FDetail : TJSObject; external name 'detail';
   public
     property Detail:TJSObject read FDetail;
   end;

   TChoiceEventDetail = class(TJsObject)
   private
     FChoice: TChoice; external name 'choice';
   public
     property Choice:TChoice read FChoice;
   end;



procedure TMyApplication.ShowSelection(event: TJSEvent);
var
   choice: TChoice;
begin
   choice:=TChoiceEventDetail(TChoiceEvent(event).Detail).Choice;
   console.log(choice);

   console.log(choice.active);
   console.log(choice.disabled);
   console.log(choice.elementid);
   console.log(choice.groupid);
   console.log(choice.KeyCode);
   console.log(choice.id);
   console.log(choice.Label_);
   console.log(choice.placeholder);
   console.log(choice.score);
   console.log(choice.value);
end;


It seems to work OK

Bye
-- 
Luca


More information about the Pas2js mailing list