htmlwidgets.pp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2019-Now by Michael Van Canneyt, member of the
  4. Free Pascal development team
  5. WEB Widget Set : Basic bare HTML Widgets
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit htmlwidgets;
  13. {$mode objfpc}
  14. interface
  15. uses
  16. Classes, SysUtils, webwidget, js, web;
  17. Type
  18. TTextMode = (tmText,tmHTML);
  19. { TButtonWidget }
  20. TButtonWidget = Class(TWebWidget)
  21. private
  22. FText: String;
  23. FTextMode: TTextMode;
  24. procedure SetText(AValue: String);
  25. procedure SetTextMode(AValue: TTextMode);
  26. Protected
  27. procedure ApplyText(aElement: TJSHTMLElement);
  28. Procedure SetName(const NewName: TComponentName); override;
  29. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  30. Public
  31. Procedure Click;
  32. Function HTMLTag : String; override;
  33. Published
  34. Property Text : String Read FText Write SetText;
  35. Property TextMode : TTextMode Read FTextMode Write SetTextMode;
  36. end;
  37. { TViewPort }
  38. TViewPort = Class(TCustomWebWidget)
  39. Private
  40. Class var FInstance : TViewPort;
  41. Protected
  42. Class Function FixedParent : TJSHTMLElement; override;
  43. Class Function FixedElement : TJSHTMLElement; override;
  44. Function DoRenderHTML(aParent,aElement : TJSHTMLElement) :TJSHTMLElement; override;
  45. Public
  46. Constructor Create (aOwner: TComponent); override;
  47. Function HTMLTag : String; override;
  48. Class Function Instance : TViewPort;
  49. Property Element;
  50. end;
  51. { TWebPage }
  52. TWebPage = Class(TCustomWebWidget)
  53. private
  54. Protected
  55. Class Function DefaultParentElement: TJSHTMLElement; override;
  56. Class Function DefaultParent : TCustomWebWidget; override;
  57. Procedure DoUnRender(aParent : TJSHTMLElement) ; override;
  58. Public
  59. Constructor Create(AOwner : TComponent); override;
  60. Function HTMLTag : String; override;
  61. // Later on, allow IFrame;
  62. Published
  63. Property ParentID;
  64. Property ElementID;
  65. Property Classes;
  66. Property Styles;
  67. Property StyleRefresh;
  68. Property Visible;
  69. // Events
  70. Property BeforeRenderHTML;
  71. Property AfterRenderHTML;
  72. Property OnAbort;
  73. Property OnAnimationCancel;
  74. Property OnAnimationEnd;
  75. Property OnAnimationIteration;
  76. Property OnAnimationStart;
  77. Property OnAuxClick;
  78. Property OnBlur;
  79. Property OnCancel;
  80. Property OnCanPlay;
  81. Property OnCanPlayThrough;
  82. Property OnChange;
  83. Property OnClick;
  84. Property OnCompositionEnd;
  85. Property OnCompositionStart;
  86. Property OnCompositionUpdate;
  87. Property OnContextMenu;
  88. Property OnCopy;
  89. Property OnCut;
  90. Property OnCueChange;
  91. Property OnDblClick;
  92. Property OnDurationChange;
  93. Property OnEnded ;
  94. Property OnError ;
  95. Property OnFocus;
  96. Property OnFocusIn ;
  97. Property OnFocusOut ;
  98. Property OnGotPointerCapture;
  99. Property OnInput;
  100. Property OnInvalid;
  101. Property OnKeyDown;
  102. Property OnKeyPress;
  103. Property OnKeyUp;
  104. Property OnLoad;
  105. Property OnLoadedData;
  106. Property OnLoadedMetaData;
  107. Property OnLoadend;
  108. Property OnLoadStart;
  109. Property OnLostPointerCapture;
  110. Property OnMouseDown;
  111. Property OnMouseEnter;
  112. Property OnMouseLeave;
  113. Property OnMouseMove;
  114. Property OnMouseOut;
  115. Property OnMouseUp;
  116. Property OnOverFlow;
  117. Property OnPaste;
  118. Property OnPause;
  119. Property OnPlay;
  120. Property OnPointerCancel;
  121. Property OnPointerDown;
  122. Property OnPointerEnter;
  123. Property OnPointerLeave;
  124. Property OnPointerMove;
  125. Property OnPointerOut;
  126. Property OnPointerOver;
  127. Property OnPointerUp;
  128. Property OnReset;
  129. Property OnResize;
  130. Property OnScroll;
  131. Property OnSelect;
  132. Property OnSubmit;
  133. Property OnTouchStart;
  134. Property OnTransitionCancel;
  135. Property OnTransitionEnd;
  136. Property OnTransitionRun;
  137. Property OnTransitionStart;
  138. Property OnWheel;
  139. end;
  140. { TCustomInputWidget }
  141. TCustomInputWidget = Class(TWebWidget)
  142. private
  143. FValue : String;
  144. FValueName : String;
  145. FText : String;
  146. FReadOnly : Boolean;
  147. FRequired : Boolean;
  148. function GetReadOnly: Boolean;
  149. function GetRequired: Boolean;
  150. function GetText: String;
  151. function GetValue: String;
  152. function GetValueName: String;
  153. procedure SetReadonly(AValue: Boolean);
  154. procedure SetRequired(AValue: Boolean);
  155. procedure SetText(AValue: String);
  156. procedure SetValue(AValue: String);
  157. function GetInputElement: TJSHTMLInputElement;
  158. procedure SetValueName(AValue: String);
  159. Protected
  160. Procedure SetName(const NewName: TComponentName); override;
  161. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  162. Property InputElement : TJSHTMLInputElement Read GetInputElement;
  163. // Text to show (checkbox etc). Enable in descendents as needed
  164. Property Text : String Read GetText Write SetText;
  165. Public
  166. function InputType : String; virtual; abstract;
  167. Function HTMLTag : String; override;
  168. // Value as string
  169. Property Value : String Read GetValue Write SetValue;
  170. // Value Name to use when submitting using form.
  171. Property ValueName : String Read GetValueName Write SetValueName;
  172. Property ReadOnly : Boolean Read GetReadOnly Write SetReadonly;
  173. Property Required : Boolean Read GetRequired Write SetRequired;
  174. end;
  175. { TTextInputWidget }
  176. TInputTextType = (ittText,ittPassword,ittNumber,ittEmail,ittSearch,ittTelephone,ittURL,ittColor);
  177. TTextInputWidget = class(TCustomInputWidget)
  178. private
  179. FMaxLength : Integer;
  180. FMinLength : Integer;
  181. FTextType : TInputTextType;
  182. function GetAsNumber: NativeInt;
  183. function GetMaxLength: NativeInt;
  184. function GetMinLength: NativeInt;
  185. function GetTextType: TInputTextType;
  186. procedure SetAsNumber(AValue: NativeInt);
  187. procedure SetMaxLength(AValue: NativeInt);
  188. procedure SetMinLength(AValue: NativeInt);
  189. procedure SetTextType(AValue: TInputTextType);
  190. Protected
  191. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  192. Public
  193. Class Function AllowChildren : Boolean; override;
  194. function InputType : String; override;
  195. Published
  196. Property Value;
  197. Property ValueName;
  198. Property Required;
  199. Property TextType : TInputTextType Read GetTextType Write SetTextType;
  200. property AsNumber : NativeInt Read GetAsNumber Write SetAsNumber;
  201. Property MaxLength : NativeInt Read GetMaxLength Write SetMaxLength;
  202. Property MinLength : NativeInt Read GetMinLength Write SetMinLength;
  203. // Todo: List support
  204. end;
  205. { TButtonInputWidget }
  206. TInputButtonType = (ibtSubmit,ibtReset,ibtImage);
  207. TInputButtonTypes = set of TInputButtonType;
  208. TButtonInputWidget = class(TCustomInputWidget)
  209. private
  210. FButtonType: TInputButtonType;
  211. FSrc: String;
  212. procedure SetButtonType(AValue: TInputButtonType);
  213. procedure SetSrc(AValue: String);
  214. Public
  215. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  216. function InputType : String; override;
  217. Class Function AllowChildren : Boolean; override;
  218. Published
  219. Property ButtonType : TInputButtonType Read FButtonType Write SetButtonType;
  220. Property Value;
  221. Property ValueName;
  222. Property Src : String Read FSrc Write SetSrc;
  223. end;
  224. { TCheckableInputWidget }
  225. TCheckableInputWidget = class(TCustomInputWidget)
  226. private
  227. FChecked: Boolean;
  228. function GetChecked: Boolean;
  229. procedure SetChecked(AValue: Boolean);
  230. Protected
  231. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  232. Public
  233. Property Value;
  234. Property ValueName;
  235. Property Checked : Boolean Read GetChecked Write SetChecked;
  236. Property Text;
  237. end;
  238. { TRadioInputWidget }
  239. TRadioInputWidget = class(TCheckableInputWidget)
  240. private
  241. Public
  242. function InputType : String; override;
  243. Published
  244. Property Value;
  245. Property ValueName;
  246. Property Checked;
  247. Property Text;
  248. end;
  249. { TCheckboxInputWidget }
  250. TCheckboxInputWidget = class(TCheckableInputWidget)
  251. private
  252. Public
  253. function InputType : String; override;
  254. Published
  255. Property Value;
  256. Property ValueName;
  257. Property Checked;
  258. Property Text;
  259. end;
  260. { TDateInputWidget }
  261. TDateInputWidget = class(TCustomInputWidget)
  262. private
  263. FDate: TDateTime;
  264. function GetDate: TDateTime;
  265. procedure SetDate(AValue: TDateTime);
  266. Public
  267. function InputType : String; override;
  268. Class Function AllowChildren : Boolean; override;
  269. Published
  270. Property Required;
  271. Property ValueName;
  272. Property Date : TDateTime Read GetDate Write SetDate;
  273. end;
  274. { TFileInputWidget }
  275. TFileInfo = record
  276. Name : String;
  277. TimeStamp : TDateTime;
  278. FileType : String;
  279. Size : NativeInt;
  280. end;
  281. TFileInputWidget = class(TCustomInputWidget)
  282. private
  283. FMultiple: Boolean;
  284. function GetFileCount: Integer;
  285. function GetFileDate(aIndex : Integer): TDateTime;
  286. function GetFileInfo(aIndex : Integer): TFileInfo;
  287. function GetFileName(aIndex : Integer): String;
  288. function GetFileSize(aIndex : Integer): NativeInt;
  289. function GetFileType(aIndex : Integer): String;
  290. function GetMultiple: Boolean;
  291. procedure SetMultiple(AValue: Boolean);
  292. Protected
  293. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  294. Public
  295. Class Function AllowChildren : Boolean; override;
  296. function InputType : String; override;
  297. Property FileCount : Integer read GetFileCount;
  298. Property Files[aIndex : Integer] : String Read GetFileName;
  299. Property FileSizes[aIndex : Integer] : NativeInt Read GetFileSize;
  300. Property FileTypes[aIndex : Integer] : String Read GetFileType;
  301. Property FileDates[aIndex : Integer] : TDateTime Read GetFileDate;
  302. Property FileInfos[aIndex : Integer] : TFileInfo Read GetFileInfo;
  303. Published
  304. Property ValueName;
  305. Property Required;
  306. Property Multiple : Boolean Read GetMultiple Write SetMultiple;
  307. end;
  308. { THiddenInputWidget }
  309. THiddenInputWidget = class(TCustomInputWidget)
  310. Public
  311. Class Function AllowChildren : Boolean; override;
  312. function InputType : String; override;
  313. Published
  314. Property ValueName;
  315. Property Value;
  316. Property Required;
  317. end;
  318. { TTextAreaWidget }
  319. TTextAreaWrap = (tawSoft,tawHard,tawOff);
  320. TTextAreaWidget = Class(TWebWidget)
  321. private
  322. FLines: TStrings;
  323. FIgnoreChanges : Boolean;
  324. FMaxLength: Cardinal;
  325. FValueName : String;
  326. FRows,
  327. FColumns : Cardinal;
  328. FWrap: TTextAreaWrap;
  329. FRequired,
  330. FReadOnly : Boolean;
  331. procedure ApplyWrap(aElement: TJSHTMLTextAreaElement);
  332. procedure DoLineChanges(Sender: TObject);
  333. function GetColumns: Cardinal;
  334. function GetLines: TStrings;
  335. function GetReadOnly: Boolean;
  336. function GetRequired: Boolean;
  337. function GetRows: Cardinal;
  338. function GetText: String;
  339. function GetValueName: string;
  340. procedure SetColumns(AValue: Cardinal);
  341. procedure SetLines(AValue: TStrings);
  342. procedure SetMaxLength(AValue: Cardinal);
  343. procedure SetReadonly(AValue: Boolean);
  344. procedure SetRequired(AValue: Boolean);
  345. procedure SetRows(AValue: Cardinal);
  346. procedure SetText(AValue: String);
  347. procedure SetValueName(AValue: string);
  348. Function GetTextArea : TJSHTMLTextAreaElement;
  349. procedure SetWrap(AValue: TTextAreaWrap);
  350. Protected
  351. procedure ApplyLines(aElement: TJSHTMLTextAreaElement);
  352. procedure LinesFromHTML(aHTML : String);
  353. Procedure SetName(const NewName: TComponentName); override;
  354. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  355. Property TextArea :TJSHTMLTextAreaElement Read GetTextArea;
  356. Public
  357. Constructor Create(aOwner : TComponent); override;
  358. Destructor Destroy; override;
  359. Class Function AllowChildren : Boolean; override;
  360. Function HTMLTag : String; override;
  361. Property InnerHTML : String Read GetText Write SetText;
  362. Published
  363. Property ValueName : string Read GetValueName Write SetValueName;
  364. Property Rows : Cardinal Read GetRows Write SetRows;
  365. Property Columns : Cardinal Read GetColumns Write SetColumns;
  366. Property Lines : TStrings Read GetLines Write SetLines;
  367. Property MaxLength : Cardinal Read FMaxLength Write SetMaxLength;
  368. Property Wrap : TTextAreaWrap Read FWrap Write SetWrap;
  369. Property ReadOnly : Boolean Read GetReadOnly Write SetReadonly;
  370. Property Required : Boolean Read GetRequired Write SetRequired;
  371. end;
  372. { TImageWidget }
  373. TImageWidget = class(TWebWidget)
  374. private
  375. FHeight: Integer;
  376. FWidth: Integer;
  377. FSrc : String;
  378. function GetHeight: Integer;
  379. function GetImg: TJSHTMLImageElement;
  380. function GetSrc: String;
  381. function GetWidth: Integer;
  382. procedure SetHeight(AValue: Integer);
  383. procedure SetSrc(AValue: String);
  384. procedure SetWidth(AValue: Integer);
  385. Protected
  386. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  387. Property ImgElement : TJSHTMLImageElement Read GetImg;
  388. Public
  389. Function HTMLTag : String; override;
  390. Published
  391. Property Src : String Read GetSrc Write SetSrc;
  392. Property Width : Integer Read GetWidth Write SetWidth;
  393. Property Height : Integer Read GetHeight Write SetHeight;
  394. end;
  395. { TSelectWidget }
  396. TJSHTMLOptionElementArray = Array of TJSHTMLOptionElement;
  397. TCustomSelectWidget = Class;
  398. { TCustomSelectWidget }
  399. TCustomSelectWidget = class(TWebWidget)
  400. Private
  401. FSize,
  402. FSelectedIndex : Integer;
  403. FOptions : TJSHTMLOptionElementArray;
  404. FMultiple : Boolean;
  405. function GetMultiple: Boolean;
  406. function GetSelected(Index : Integer): Boolean;
  407. function GetSelectedIndex: Integer;
  408. function GetSelect: TJSHTMLSelectElement;
  409. function GetSelectionCount: Integer;
  410. function GetSelectionItem(aIndex : Integer): String;
  411. function GetSelectionValue(aIndex : Integer): String;
  412. function GetSize: Integer;
  413. procedure SetMultiple(AValue: Boolean);
  414. procedure SetSelected(Index : Integer; AValue: Boolean);
  415. procedure SetSelectedIndex(AValue: Integer);
  416. procedure SetSize(AValue: Integer);
  417. Protected
  418. Type
  419. { TSelectOptionEnumerator }
  420. TSelectOptionEnumerator = Class
  421. private
  422. FSelect: TCustomSelectWidget;
  423. public
  424. constructor Create(ASelect : TCustomSelectWidget); reintroduce; virtual;
  425. Function OptionText : String; virtual; abstract;
  426. Function HasValue : boolean; virtual;
  427. Function Value : string; virtual;
  428. function MoveNext: Boolean; virtual; abstract;
  429. Property Select: TCustomSelectWidget Read FSelect;
  430. end;
  431. Protected
  432. function GetItemCount: Integer; virtual;
  433. Function CreateOptionEnumerator : TSelectOptionEnumerator; virtual; abstract;
  434. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  435. Procedure BuildOptions(aSelect : TJSHTMLSelectElement); virtual;
  436. Property Options : TJSHTMLOptionElementArray Read Foptions;
  437. Property SelectElement : TJSHTMLSelectElement Read GetSelect;
  438. Protected
  439. // Can be made public/published
  440. // Items that are selected
  441. Property ItemCount : Integer Read GetItemCount;
  442. Property Selected[Index : Integer] : Boolean Read GetSelected Write SetSelected;
  443. Property SelectionCount : Integer Read GetSelectionCount;
  444. Property SelectionValue[aIndex : Integer] : String Read GetSelectionValue;
  445. Property SelectionItem[aIndex : Integer] : String Read GetSelectionItem;
  446. property SelectedIndex : Integer Read GetSelectedIndex Write SetSelectedindex;
  447. Property Multiple : Boolean Read GetMultiple Write SetMultiple;
  448. Property Size : Integer Read GetSize Write SetSize;
  449. Public
  450. Constructor Create(aOWner : TComponent); override;
  451. Function HTMLTag : String; override;
  452. end;
  453. TSelectWidget = class(TCustomSelectWidget)
  454. private
  455. FItems : TStrings;
  456. FValues : TStrings;
  457. function GetItems: TStrings;
  458. function GetValues: TStrings;
  459. procedure OptionsChanged(Sender: TObject);
  460. procedure setItems(AValue: TStrings);
  461. procedure setValues(AValue: TStrings);
  462. Protected
  463. Type
  464. { TStringsSelectOptionEnumerator }
  465. TStringsSelectOptionEnumerator = Class(TSelectOptionEnumerator)
  466. FCurrent : Integer;
  467. constructor Create(ASelect : TCustomSelectWidget); override;
  468. Function OptionText : String; override;
  469. Function HasValue : boolean; override;
  470. Function Value : string; override;
  471. function MoveNext: Boolean; override;
  472. end;
  473. Function CreateOptionEnumerator: TSelectOptionEnumerator; override;
  474. Public
  475. Constructor Create(aOWner : TComponent); override;
  476. Destructor Destroy; override;
  477. Property SelectionCount;
  478. Property SelectionValue;
  479. Property SelectionItem;
  480. Property Selected;
  481. Property Options;
  482. Property SelectElement;
  483. Property ItemCount;
  484. Published
  485. Property Items : TStrings Read GetItems Write setItems;
  486. Property Values : TStrings Read GetValues Write setValues;
  487. property SelectedIndex;
  488. Property Multiple;
  489. property size;
  490. end;
  491. { TLabelWidget }
  492. TLabelWidget = Class(TWebWidget)
  493. private
  494. FLabelFor: TWebWidget;
  495. FText: String;
  496. function GetLabelEl: TJSHTMLLabelElement;
  497. function GetText: String;
  498. procedure SetLabelFor(AValue: TWebWidget);
  499. procedure SetText(AValue: String);
  500. Protected
  501. procedure ApplyLabelFor(aLabelElement: TJSHTMLLabelElement);
  502. Procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  503. Procedure SetName(const NewName: TComponentName); override;
  504. Procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  505. Property LabelElement : TJSHTMLLabelElement Read GetLabelEl;
  506. Public
  507. Function HTMLTag : String; override;
  508. Property Text : String Read GetText Write SetText;
  509. Property LabelFor : TWebWidget Read FLabelFor Write SetLabelFor;
  510. end;
  511. TTextTag = (ttParagraph,ttBold,ttItalic,ttUnderline,ttStrikeThrough,ttSpan,ttQuote,ttBlockQuote,ttH1,ttH2,ttH3,ttH4,ttH5,ttH6,ttPre,ttRuby,ttArticle,ttAddress,ttAbbr,ttCustom);
  512. { TTextWidget }
  513. { TCustomTextWidget }
  514. TCustomTextWidget = Class(TCustomWebWidget)
  515. private
  516. FCustomTag: String;
  517. FEnvelopeTag: TTextTag;
  518. FTextMode: TTextMode;
  519. procedure SetCustomTag(AValue: String);
  520. procedure SetEnvelopeTag(AValue: TTextTag);
  521. procedure SetTextMode(AValue: TTextMode);
  522. Protected
  523. procedure ApplyWidgetSettings(aElement: TJSHTMLElement); override;
  524. procedure ApplyText(aElement : TJSHTMLElement); virtual;
  525. Function GetText : String; virtual; abstract;
  526. Public
  527. Function HTMLTag : String; override;
  528. Published
  529. Property CustomTag : String Read FCustomTag Write SetCustomTag;
  530. Property EnvelopeTag : TTextTag Read FEnvelopeTag Write SetEnvelopeTag;
  531. Property TextMode : TTextMode Read FTextMode Write SetTextMode;
  532. end;
  533. TTextWidget = Class(TCustomTextWidget)
  534. private
  535. FText : String;
  536. procedure SetText(AValue: String);
  537. Protected
  538. Function GetText : String; override;
  539. published
  540. Property Text : String Read FText Write SetText;
  541. end;
  542. { TTextLinesWidget }
  543. TTextLinesWidget = Class(TCustomTextWidget)
  544. private
  545. FLines : TStrings;
  546. FForceLineBreaks: Boolean;
  547. procedure DoLinesChanged(Sender: TObject);
  548. procedure SetLines(AValue: TStrings);
  549. procedure SetForceLineBreaks(AValue: Boolean);
  550. Protected
  551. Function GetText : String; override;
  552. procedure ApplyText(aElement : TJSHTMLElement); override;
  553. Public
  554. Constructor Create(aOwner : TComponent); override;
  555. Destructor Destroy; override;
  556. published
  557. Property Lines : TStrings Read FLines Write SetLines;
  558. // When forcelinebreaks is true a <br> will be appended to every line.
  559. // Note that for TextMode=tmText this means the lines will be rendered as-is, but there will still be a <br> between the lines
  560. Property ForceLineBreaks : Boolean Read FForceLineBreaks Write SetForceLineBreaks;
  561. end;
  562. { TCustomTableColumn }
  563. TColumnOption = (coHeader,coCaptionHeader);
  564. TColumnOptions = set of TColumnOption;
  565. TCustomTableColumn = Class(TCollectionItem)
  566. private
  567. FAlignment: TAlignment;
  568. FCaption: String;
  569. FClassNames: String;
  570. procedure SetAlignment(AValue: TAlignment);
  571. procedure SetCaption(AValue: String);
  572. procedure SetClassNames(AValue: String);
  573. Protected
  574. Function GetDisplayName: string; override;
  575. function GetCaption: String; virtual;
  576. Public
  577. Procedure Assign(Source : TPersistent); override;
  578. Property Alignment : TAlignment Read FAlignment Write SetAlignment;
  579. Property Caption : String Read GetCaption Write SetCaption;
  580. Property ClassNames : String Read FClassNames Write SetClassNames;
  581. end;
  582. { TCustomTableColumns }
  583. TCustomTableColumns = Class(TCollection)
  584. private
  585. function GetCustomColumn(Index : Integer): TCustomTableColumn;
  586. procedure SetCustomColumn(Index : Integer; AValue: TCustomTableColumn);
  587. Protected
  588. Property CustomColumns [Index : Integer] : TCustomTableColumn Read GetCustomColumn Write SetCustomColumn; default;
  589. Public
  590. Function Add(aCaption : String): TCustomTableColumn; overload;
  591. end;
  592. { TCustomTableWidget }
  593. TTableOption = (toHeader, // use THead tag
  594. toHeaderRow, // Create header row
  595. toBody, // use TBody tag
  596. toFooter, // use TFoot tag
  597. toFooterRow, // create footer row
  598. toRowID, // add ID to tr: -kind-row
  599. toCellID, // add ID to cell td: -kind-row-col
  600. toHeaderRowData, // Add rowno to <tr data-row> for header.
  601. toHeaderCellDataRow, // Add rowno to <th data-row> for header. Automatic if onheadercellclick is set.
  602. toHeaderCellDataCol, // Add colno to <th data-col> for header. Automatic if onheadercellclick is set.
  603. toBodyRowData, // Add rowno to <tr data-row> for body.
  604. toBodyCellDataRow, // Add rowno to <th data-row> for body. Automatic if oncellclick is set.
  605. toBodyCellDataCol, // Add colno to <th data-col> for body. Automatic if oncellclick is set.
  606. tofooterRowData, // Add rowno to <tr data-row> for footer
  607. tofooterCellDataRow, // Add rowno to <th data-row> for footer. Automatic if onfootercellclick is set.
  608. tofooterCellDataCol // Add colno to <th data-col> for footer. Automatic if onfootercellclick is set.
  609. );
  610. TTableOptions = Set of TTableOption;
  611. TRowKind = (rkHeader,rkBody,rkFooter);
  612. Type
  613. TCustomTableWidget = Class;
  614. // Constructed only once when rendering !
  615. { TTableWidgetCelldata }
  616. TTableWidgetCellData = Class
  617. private
  618. FAsHTML: Boolean;
  619. FClassNames: String;
  620. FCol: Integer;
  621. FColumn: TCustomTableColumn;
  622. FContent: TJSHTMLElement;
  623. FKind: TRowKind;
  624. FRow: Integer;
  625. FTable: TCustomTableWidget;
  626. FTableID: String;
  627. FTag: String;
  628. FText: String;
  629. Protected
  630. Procedure SetRowColKind(aRow,aCol : Integer; aKind : TRowKind); virtual;
  631. Procedure Reset; // do not reset row,col, column
  632. Public
  633. Constructor Create(aTable : TCustomTableWidget;aTableID : String); virtual;
  634. Property Table : TCustomTableWidget Read FTable;
  635. Property Column : TCustomTableColumn Read FColumn;
  636. Property Row : Integer Read FRow;
  637. Property Col : Integer Read FCol;
  638. Property Kind : TRowKind Read FKind;
  639. Property Tag : String Read FTag Write FTag;
  640. Property ClassNames : String Read FClassNames Write FClassNames;
  641. Property Text : String Read FText Write FText;
  642. Property AsHTML : Boolean Read FAsHTML Write FAsHTML;
  643. Property Content : TJSHTMLElement Read FContent Write FContent;
  644. Property TableID : String Read FTableID;
  645. end;
  646. TTableRowEnumerator = Class
  647. private
  648. FTable: TCustomTableWidget;
  649. FCurrent : Integer;
  650. public
  651. constructor Create(ATable : TCustomTableWidget); reintroduce; virtual;
  652. Procedure GetCellData(aCell : TTableWidgetCellData); virtual;
  653. function MoveNext: Boolean; virtual;
  654. property CurrentRow : Integer Read FCurrent;
  655. Property Table : TCustomTableWidget Read FTable;
  656. end;
  657. TTableRowCountEnumerator = Class (TTableRowEnumerator)
  658. private
  659. FRowCount: Integer;
  660. public
  661. constructor Create(ATable : TCustomTableWidget;aCount : Integer); reintroduce;
  662. function MoveNext: Boolean; override;
  663. Property RowCount : Integer read FRowCount;
  664. end;
  665. TOnCellDataEvent = Procedure (Sender : TObject; Enum : TTableRowEnumerator; aCell : TTableWidgetCellData) of object;
  666. TCustomTableWidget = Class(TCustomWebWidget)
  667. private
  668. FCaption: String;
  669. FColumns: TCustomTableColumns;
  670. FOnCellClick: THTMLNotifyEvent;
  671. FOnFooterCellClick: THTMLNotifyEvent;
  672. FOnFooterRowClick: THTMLNotifyEvent;
  673. FOnHeaderCellClick: THTMLNotifyEvent;
  674. FOnHeaderRowClick: THTMLNotifyEvent;
  675. FOnRowClick: THTMLNotifyEvent;
  676. FTableOptions: TTableOptions;
  677. FOnGetCellData : TOnCellDataEvent;
  678. procedure SetCaption(AValue: String);
  679. procedure SetColumns(AValue: TCustomTableColumns);
  680. procedure SetTableOptions(AValue: TTableOptions);
  681. Public
  682. Protected
  683. procedure AppendCaption(aCaptionElement: TJSHTMLElement); virtual;
  684. procedure RenderData(aElement: TJSHTMLElement); virtual;
  685. function DoCellClick(aEvent: TJSMouseEvent): boolean; virtual;
  686. function DoHeaderCellClick(aEvent: TJSMouseEvent): boolean;virtual;
  687. function DoFooterCellClick(aEvent: TJSMouseEvent): boolean;virtual;
  688. function DoRowClick(aEvent: TJSMouseEvent): boolean; virtual;
  689. function DoHeaderRowClick(aEvent: TJSMouseEvent): boolean;virtual;
  690. function DoFooterRowClick(aEvent: TJSMouseEvent): boolean;virtual;
  691. function CreateColumns: TCustomTableColumns; virtual;
  692. Function DefaultTableOptions: TTableOptions; virtual;
  693. Procedure CreateDefaultColumns; virtual;
  694. Function GetRowEnumerator(aKind : TRowKind) : TTableRowEnumerator; virtual;
  695. function RenderCell(aCell: TTableWidgetCellData): TJSHTMLElement; virtual;
  696. procedure RenderRow(aEnum : TTableRowEnumerator; aParent: TJSHTMLElement; aKind: TRowKind; aCell: TTableWidgetCellData); virtual;
  697. procedure RenderRows(aParent: TJSHTMLElement; aKind : TRowKind; aCell: TTableWidgetCellData); virtual;
  698. Procedure ApplyWidgetSettings(aElement : TJSHTMLElement); override;
  699. Function HTMLTag : String; override;
  700. Function CreateCellData(const aTableID : String) : TTableWidgetCellData; virtual;
  701. Function GetBodyRowEnumerator : TTableRowEnumerator; virtual; abstract;
  702. Protected
  703. // These can be made public/published
  704. Property CustomColumns : TCustomTableColumns Read FColumns Write SetColumns;
  705. Property TableOptions : TTableOptions read FTableOptions write SetTableOptions;
  706. Property Caption : String Read FCaption Write SetCaption;
  707. Property OnGetCellData : TOnCellDataEvent Read FOnGetCellData Write FOnGetCellData;
  708. Property OnCellClick : THTMLNotifyEvent Read FOnCellClick Write FOnCellClick;
  709. Property OnHeaderCellClick : THTMLNotifyEvent Read FOnHeaderCellClick Write FOnHeaderCellClick;
  710. Property OnFooterCellClick : THTMLNotifyEvent Read FOnFooterCellClick Write FOnFooterCellClick;
  711. Property OnRowClick : THTMLNotifyEvent Read FOnRowClick Write FOnRowClick;
  712. Property OnHeaderRowClick : THTMLNotifyEvent Read FOnHeaderRowClick Write FOnHeaderRowClick;
  713. Property OnFooterRowClick : THTMLNotifyEvent Read FOnFooterRowClick Write FOnFooterRowClick;
  714. Public
  715. Constructor Create(aOwner : TComponent); override;
  716. Destructor Destroy; override;
  717. Procedure RefreshBody;
  718. end;
  719. TTableWidget = Class(TCustomTableWidget)
  720. Public
  721. Property CustomColumns;
  722. Property TableOptions;
  723. Property Caption;
  724. Property OnGetCellData;
  725. Property OnCellClick;
  726. Property OnHeaderCellClick;
  727. Property OnFooterCellClick;
  728. Property OnRowClick;
  729. Property OnHeaderRowClick;
  730. Property OnFooterRowClick;
  731. end;
  732. Function ViewPort : TViewPort;
  733. Const
  734. TextTagNames : Array[TTextTag] of string
  735. = ('p','b','i','u','s','span','quote','blockquote','h1','h2','h3','h4','h5','h6','pre','ruby','article','address','abbr','');
  736. RowKindNames : Array[TRowKind] of string = ('header','body','footer');
  737. implementation
  738. uses DateUtils;
  739. resourcestring
  740. SErrInvalidIndex = 'Index %d not in valid range of [0..%d]';
  741. Function ViewPort : TViewPort;
  742. begin
  743. Result:=TViewPort.Instance;
  744. end;
  745. { TTableRowCountEnumerator }
  746. Const
  747. CellTags : Array[TRowKind] of string = ('th','td','td');
  748. { TSelectWidget.TStringsSelectOptionEnumerator }
  749. constructor TSelectWidget.TStringsSelectOptionEnumerator.Create(ASelect: TCustomSelectWidget);
  750. begin
  751. inherited Create(ASelect);
  752. FCurrent:=-1;
  753. end;
  754. function TSelectWidget.TStringsSelectOptionEnumerator.OptionText: String;
  755. begin
  756. Result:=TSelectWidget(Select).Items[FCurrent];
  757. end;
  758. function TSelectWidget.TStringsSelectOptionEnumerator.HasValue: boolean;
  759. begin
  760. Result:=FCurrent<TSelectWidget(Select).Values.Count;
  761. end;
  762. function TSelectWidget.TStringsSelectOptionEnumerator.Value: string;
  763. begin
  764. Result:=TSelectWidget(Select).Values[FCurrent];
  765. end;
  766. function TSelectWidget.TStringsSelectOptionEnumerator.MoveNext: Boolean;
  767. begin
  768. Result:=FCurrent<TSelectWidget(Select).Items.Count-1;
  769. if Result then
  770. Inc(FCurrent);
  771. end;
  772. { TCustomSelectWidget.TSelectOptionEnumerator }
  773. constructor TCustomSelectWidget.TSelectOptionEnumerator.Create(ASelect: TCustomSelectWidget);
  774. begin
  775. FSelect:=ASelect;
  776. end;
  777. function TCustomSelectWidget.TSelectOptionEnumerator.HasValue: boolean;
  778. begin
  779. Result:=False;
  780. end;
  781. function TCustomSelectWidget.TSelectOptionEnumerator.Value: string;
  782. begin
  783. Result:='';
  784. end;
  785. constructor TTableRowCountEnumerator.Create(ATable: TCustomTableWidget; aCount: Integer);
  786. begin
  787. Inherited Create(aTable);
  788. FRowCount:=aCount;
  789. end;
  790. function TTableRowCountEnumerator.MoveNext: Boolean;
  791. begin
  792. Result:=Inherited MoveNext and (CurrentRow<RowCount)
  793. end;
  794. { TTableWidgetCellData }
  795. procedure TTableWidgetCellData.SetRowColKind(aRow, aCol: Integer; aKind: TRowKind);
  796. begin
  797. if (aRow<>-1) then
  798. FRow:=aRow;
  799. if (aCol<>-1) then
  800. FCol:=aCol;
  801. FKind:=aKind;
  802. end;
  803. procedure TTableWidgetCellData.Reset;
  804. begin
  805. Ftag:='td';
  806. FClassNames:='';
  807. FText:='';
  808. FContent:=Nil;
  809. FAsHTML:=False;
  810. end;
  811. constructor TTableWidgetCellData.Create(aTable: TCustomTableWidget; aTableID: String);
  812. begin
  813. FTable:=aTable;
  814. FTableID:=aTableID;
  815. SetRowColKind(0,0,rkBody);
  816. end;
  817. { TCustomTableWidget }
  818. procedure TCustomTableWidget.SetColumns(AValue: TCustomTableColumns);
  819. begin
  820. if FColumns=AValue then Exit;
  821. FColumns.Assign(AValue);
  822. end;
  823. procedure TCustomTableWidget.SetCaption(AValue: String);
  824. begin
  825. if FCaption=AValue then Exit;
  826. FCaption:=AValue;
  827. if isRendered then Refresh;
  828. end;
  829. function TCustomTableWidget.DoCellClick(aEvent: TJSMouseEvent): boolean;
  830. begin
  831. If Assigned(FOnCellClick) then
  832. FOnCellClick(Self,aEvent);
  833. If Assigned(FOnRowClick) then
  834. FOnRowClick(Self,aEvent);
  835. Result:=False;
  836. // Writeln('On click for cell',aEvent.targetElement.innerText);
  837. end;
  838. function TCustomTableWidget.DoHeaderCellClick(aEvent: TJSMouseEvent): boolean;
  839. begin
  840. If Assigned(FOnHeaderCellClick) then
  841. FOnHeaderCellClick(Self,aEvent);
  842. If Assigned(FOnHeaderRowClick) then
  843. FOnHeaderRowClick(Self,aEvent);
  844. Result:=False;
  845. // Writeln('On click for header cell',aEvent.targetElement.innerText);
  846. end;
  847. function TCustomTableWidget.DoFooterCellClick(aEvent: TJSMouseEvent): boolean;
  848. begin
  849. If Assigned(FOnFooterCellClick) then
  850. FOnFooterCellClick(Self,aEvent);
  851. If Assigned(FOnFooterRowClick) then
  852. FOnFooterRowClick(Self,aEvent);
  853. Result:=False;
  854. // Writeln('On click for Footer cell',aEvent.targetElement.innerText);
  855. end;
  856. function TCustomTableWidget.DoRowClick(aEvent: TJSMouseEvent): boolean;
  857. begin
  858. If Assigned(FOnRowClick) then
  859. FOnRowClick(Self,aEvent);
  860. Result:=False;
  861. // Writeln('On click for Row',aEvent.targetElement.innerText);
  862. end;
  863. function TCustomTableWidget.DoHeaderRowClick(aEvent: TJSMouseEvent): boolean;
  864. begin
  865. If Assigned(FOnHeaderRowClick) then
  866. FOnHeaderRowClick(Self,aEvent);
  867. Result:=False;
  868. // Writeln('On click for Header Row',aEvent.targetElement.innerText);
  869. end;
  870. function TCustomTableWidget.DoFooterRowClick(aEvent: TJSMouseEvent): boolean;
  871. begin
  872. If Assigned(FOnFooterRowClick) then
  873. FOnFooterRowClick(Self,aEvent);
  874. Result:=False;
  875. // Writeln('On click for Footer Row',aEvent.targetElement.innerText);
  876. end;
  877. procedure TCustomTableWidget.SetTableOptions(AValue: TTableOptions);
  878. begin
  879. if FTableOptions=AValue then Exit;
  880. FTableOptions:=AValue;
  881. if IsRendered then
  882. Refresh;
  883. end;
  884. procedure TCustomTableWidget.AppendCaption(aCaptionElement: TJSHTMLElement);
  885. begin
  886. aCaptionElement.InnerHTML:=Caption;
  887. end;
  888. function TCustomTableWidget.GetRowEnumerator(aKind: TRowKind): TTableRowEnumerator;
  889. begin
  890. Case aKind of
  891. rkHeader : Result:=TTableRowCountEnumerator.Create(Self,1);
  892. rkFooter : Result:=TTableRowCountEnumerator.Create(Self,1);
  893. rkBody : Result:=GetBodyRowEnumerator;
  894. end;
  895. end;
  896. procedure TTableRowEnumerator.GetCellData(aCell: TTableWidgetCellData);
  897. Var
  898. K : TRowKind;
  899. begin
  900. K:=aCell.Kind;
  901. Case K of
  902. rkHeader:
  903. begin
  904. aCell.Tag:='th';
  905. aCell.Text:=ACell.Column.Caption;
  906. end;
  907. rkFooter,
  908. rkBody :
  909. begin
  910. aCell.Tag:='td';
  911. end;
  912. end;
  913. end;
  914. function TCustomTableWidget.HTMLTag: String;
  915. begin
  916. Result:='table';
  917. end;
  918. function TCustomTableWidget.RenderCell(aCell: TTableWidgetCellData): TJSHTMLElement;
  919. Const
  920. Aligns : Array[TAlignment] of string = ('left','right','center');
  921. RowChecks : Array[TRowKind] of TTableOption = (toHeaderCellDataRow,toBodyCellDataRow,toFooterCellDataRow);
  922. ColChecks : Array[TRowKind] of TTableOption = (toHeaderCellDataCol,toBodyCellDataCol,toFooterCellDataCol);
  923. Var
  924. C : TJSHtmlElement;
  925. cl : THTMLNotifyEvent;
  926. K : TRowKind;
  927. M : THTMLClickEventHandler;
  928. elID : string;
  929. begin
  930. K:=aCell.Kind;
  931. if toCellID in TableOptions then
  932. elID:=aCell.TableID+'-'+RowKindNames[K]+'-'+IntToStr(ACell.Row)+'-'+IntToStr(aCell.Col)
  933. else
  934. elID:='';
  935. C:=CreateElement(aCell.Tag,elID);
  936. if aCell.Content<>Nil then
  937. C.AppendChild(aCell.Content)
  938. else if aCell.AsHTML then
  939. C.innerHTML:=aCell.text
  940. else
  941. C.innerText:=aCell.text;
  942. C.className:=AddClasses(aCell.Column.ClassNames,aCell.ClassNames);
  943. if ACell.Column.Alignment<>taLeftJustify then
  944. C.Style.setProperty('text-align',Aligns[ACell.Column.Alignment]);
  945. Case K of
  946. rkBody :
  947. begin
  948. CL:=FOnCellClick;
  949. M:=@DoCellClick;
  950. end;
  951. rkHeader :
  952. begin
  953. CL:=FOnHeaderCellClick;
  954. M:=@DoHeaderCellClick;
  955. end;
  956. rkFooter :
  957. begin
  958. CL:=FOnFooterCellClick;
  959. M:=@DoFooterCellClick;
  960. end;
  961. else
  962. CL:=Nil;
  963. M:=nil;
  964. end;
  965. if Assigned(cl) or (RowChecks[K] in TableOptions) then
  966. begin
  967. C.dataset['row']:=ACell.Row;
  968. C.Dataset['kind']:=RowKindNames[K];
  969. end;
  970. if Assigned(cl) or (ColChecks[K] in TableOptions) then
  971. begin
  972. C.dataset['col']:=ACell.Col;
  973. C.Dataset['kind']:=RowKindNames[K];
  974. end;
  975. if Assigned(M) then
  976. C.OnClick:=M;
  977. Result:=C;
  978. end;
  979. procedure TCustomTableWidget.RenderRow(aEnum: TTableRowEnumerator; aParent: TJSHTMLElement; aKind: TRowKind; aCell: TTableWidgetCellData);
  980. Var
  981. I: integer;
  982. begin
  983. For I:=0 to CustomColumns.Count-1 do
  984. begin
  985. aCell.Reset;
  986. aCell.FColumn:=CustomColumns[i];
  987. aCell.SetRowColKind(-1,I,aKind);
  988. // Writeln(CellKinds[aKind],' cell before : ',aCell.Tag,' data : ',aCell.Text);
  989. aEnum.GetCellData(aCell);
  990. // Writeln(CellKinds[aKind],' cell after : ',aCell.Tag,' data : ',aCell.Text);
  991. if aCell.Tag='' then
  992. ACell.Tag:=CellTags[aKind];
  993. if Assigned(FOnGetCellData) then
  994. FOnGetCellData(Self,aEnum,aCell);
  995. aParent.appendChild(RenderCell(aCell));
  996. end;
  997. end;
  998. procedure TCustomTableWidget.RenderRows(aParent: TJSHTMLElement; aKind: TRowKind; aCell: TTableWidgetCellData);
  999. Const
  1000. TableRowChecks : Array[TRowKind] of TTableOption = (toHeaderRowData,toBodyRowData,toFooterRowData);
  1001. Var
  1002. RowEl : TJSHTMLElement;
  1003. Enum : TTableRowEnumerator;
  1004. M : THTMLClickEventHandler;
  1005. cl : THTMLNotifyEvent;
  1006. elid : String;
  1007. begin
  1008. Enum:=GetRowEnumerator(aKind);
  1009. if Enum=Nil then
  1010. Exit;
  1011. try
  1012. While Enum.MoveNext do
  1013. begin
  1014. if toRowID in TableOptions then
  1015. elID:=aCell.TableID+'-'+RowKindNames[aKind]+'-'+IntToStr(Enum.CurrentRow)
  1016. else
  1017. elID:='';
  1018. RowEl:=CreateElement('tr',elID);
  1019. aCell.SetRowColKind(Enum.CurrentRow,-1,aKind);
  1020. Case aKind of
  1021. rkBody :
  1022. begin
  1023. CL:=FOnRowClick;
  1024. M:=@DoRowClick;
  1025. end;
  1026. rkHeader :
  1027. begin
  1028. CL:=FOnHeaderRowClick;
  1029. M:=@DoHeaderRowClick;
  1030. end;
  1031. rkFooter :
  1032. begin
  1033. CL:=FOnFooterRowClick;
  1034. M:=@DoFooterRowClick;
  1035. end;
  1036. end;
  1037. if Assigned(CL) or (TableRowChecks[Akind] in TableOptions) then
  1038. begin
  1039. RowEl.dataset['row']:=Enum.CurrentRow;
  1040. RowEl.dataset['kind']:=RowKindNames[aKind];
  1041. end;
  1042. if Assigned(M) then
  1043. RowEl.OnClick:=M;
  1044. RenderRow(Enum,RowEl,aKind,aCell);
  1045. aParent.AppendChild(RowEl);
  1046. end;
  1047. finally
  1048. Enum.Free;
  1049. end;
  1050. end;
  1051. procedure TCustomTableWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1052. begin
  1053. inherited ApplyWidgetSettings(aElement);
  1054. RenderData(aElement);
  1055. end;
  1056. procedure TCustomTableWidget.RenderData(aElement: TJSHTMLElement);
  1057. Var
  1058. El : TJSHTMLElement;
  1059. aCell : TTableWidgetCellData;
  1060. begin
  1061. if (Caption<>'') then
  1062. begin
  1063. El:=CreateElement('caption',aElement.ID+'-caption');
  1064. AppendCaption(EL);
  1065. aElement.AppendChild(EL);
  1066. end;
  1067. aCell:=CreateCellData(aElement.ID);
  1068. If (CustomColumns.Count=0) then
  1069. CreateDefaultColumns;
  1070. if toHeaderRow in TableOptions then
  1071. begin
  1072. if toHeader in TableOptions then
  1073. begin
  1074. El:=CreateElement('thead',aElement.ID+'-head');
  1075. aElement.AppendChild(el);
  1076. end
  1077. else
  1078. El:=aElement;
  1079. aCell.SetRowColKind(-1,-1,rkHeader);
  1080. RenderRows(El,rkHeader,aCell);
  1081. end;
  1082. if toBody in TableOptions then
  1083. begin
  1084. El:=CreateElement('tbody',aElement.ID+'-body');
  1085. aElement.AppendChild(el);
  1086. end
  1087. else
  1088. El:=aElement;
  1089. aCell.SetRowColKind(-1,-1,rkBody);
  1090. RenderRows(El,rkBody,aCell);
  1091. if toFooterRow in TableOptions then
  1092. begin
  1093. if toFooter in TableOptions then
  1094. begin
  1095. El:=CreateElement('tFoot',aElement.ID+'-foot');
  1096. aElement.AppendChild(el);
  1097. end
  1098. else
  1099. El:=aElement;
  1100. aCell.SetRowColKind(-1,-1,rkFooter);
  1101. RenderRows(El,rkFooter,aCell);
  1102. end;
  1103. end;
  1104. function TCustomTableWidget.CreateCellData(const aTableID : String): TTableWidgetCellData;
  1105. begin
  1106. Result:=TTableWidgetCellData.Create(Self,aTableID);
  1107. end;
  1108. function TCustomTableWidget.CreateColumns: TCustomTableColumns;
  1109. begin
  1110. Result:=TCustomTableColumns.Create(TCustomTableColumn);
  1111. end;
  1112. function TCustomTableWidget.DefaultTableOptions: TTableOptions;
  1113. begin
  1114. Result:=[toHeader,toBody,toFooter,toHeaderRow]
  1115. end;
  1116. procedure TCustomTableWidget.CreateDefaultColumns;
  1117. begin
  1118. // Do nothing
  1119. end;
  1120. constructor TCustomTableWidget.Create(aOwner: TComponent);
  1121. begin
  1122. inherited Create(aOwner);
  1123. FTableOptions:=DefaultTableOptions;
  1124. FColumns:=CreateColumns;
  1125. end;
  1126. destructor TCustomTableWidget.Destroy;
  1127. begin
  1128. FreeAndNil(FColumns);
  1129. inherited Destroy;
  1130. end;
  1131. procedure TCustomTableWidget.RefreshBody;
  1132. begin
  1133. if Not Assigned(Element) then
  1134. Refresh
  1135. else
  1136. begin
  1137. Element.Innerhtml:='';
  1138. RenderData(Element);
  1139. end;
  1140. end;
  1141. { TCustomTableColumn }
  1142. procedure TCustomTableColumn.SetAlignment(AValue: TAlignment);
  1143. begin
  1144. if FAlignment=AValue then Exit;
  1145. FAlignment:=AValue;
  1146. end;
  1147. function TCustomTableColumn.GetCaption: String;
  1148. begin
  1149. Result:=FCaption;
  1150. end;
  1151. procedure TCustomTableColumn.SetCaption(AValue: String);
  1152. begin
  1153. if FCaption=AValue then Exit;
  1154. FCaption:=AValue;
  1155. end;
  1156. procedure TCustomTableColumn.SetClassNames(AValue: String);
  1157. begin
  1158. if FClassNames=AValue then Exit;
  1159. FClassNames:=AValue;
  1160. end;
  1161. function TCustomTableColumn.GetDisplayName: string;
  1162. begin
  1163. Result:=Caption;
  1164. end;
  1165. procedure TCustomTableColumn.Assign(Source: TPersistent);
  1166. Var
  1167. C : TCustomTableColumn;
  1168. begin
  1169. if Source is TCustomTableColumn then
  1170. begin
  1171. C:=Source as TCustomTableColumn;
  1172. FCaption:=C.FCaption;
  1173. FClassNames:=C.FClassNames;
  1174. FAlignment:=C.Alignment;
  1175. end
  1176. else
  1177. inherited Assign(Source);
  1178. end;
  1179. { TCustomTableColumns }
  1180. function TCustomTableColumns.GetCustomColumn(Index : Integer): TCustomTableColumn;
  1181. begin
  1182. Result:=TCustomTableColumn(Items[Index]);
  1183. end;
  1184. procedure TCustomTableColumns.SetCustomColumn(Index : Integer; AValue: TCustomTableColumn);
  1185. begin
  1186. Items[Index]:=aValue;
  1187. end;
  1188. function TCustomTableColumns.Add(aCaption: String): TCustomTableColumn;
  1189. begin
  1190. Result:=add as TCustomTableColumn;
  1191. Result.Caption:=aCaption;
  1192. end;
  1193. { TTableRowEnumerator }
  1194. constructor TTableRowEnumerator.Create(ATable: TCustomTableWidget);
  1195. begin
  1196. FTable:=aTable;
  1197. FCurrent:=-1;
  1198. end;
  1199. function TTableRowEnumerator.MoveNext: Boolean;
  1200. begin
  1201. Inc(FCurrent);
  1202. Result:=True;
  1203. end;
  1204. { TCustomTextWidget }
  1205. procedure TCustomTextWidget.SetEnvelopeTag(AValue: TTextTag);
  1206. begin
  1207. // Writeln('Setting text tag : ',aValue);
  1208. if FEnvelopeTag=AValue then Exit;
  1209. FEnvelopeTag:=AValue;
  1210. if (FEnvelopeTag=ttCustom) and (FCustomTag='') then
  1211. FCustomTag:='div';
  1212. if IsRendered then
  1213. Refresh;
  1214. end;
  1215. procedure TCustomTextWidget.SetCustomTag(AValue: String);
  1216. begin
  1217. if FCustomTag=AValue then Exit;
  1218. FCustomTag:=AValue;
  1219. if (FCustomTag<>'') then
  1220. FEnvelopeTag:=ttCustom;
  1221. if IsRendered then
  1222. Refresh;
  1223. end;
  1224. procedure TCustomTextWidget.SetTextMode(AValue: TTextMode);
  1225. begin
  1226. if FTextMode=AValue then Exit;
  1227. FTextMode:=AValue;
  1228. if IsRendered then
  1229. ApplyText(Element);
  1230. end;
  1231. procedure TCustomTextWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1232. begin
  1233. // Writeln('ApplyWidgetSettings: ',aElement.tagName);
  1234. inherited ApplyWidgetSettings(aElement);
  1235. ApplyText(aElement);
  1236. end;
  1237. procedure TCustomTextWidget.ApplyText(aElement: TJSHTMLElement);
  1238. begin
  1239. if FTextMode=tmText then
  1240. aElement.innerText:=GetText
  1241. else
  1242. aElement.innerHTML:=GetText;
  1243. end;
  1244. function TCustomTextWidget.HTMLTag: String;
  1245. begin
  1246. Result:=TextTagNames[FEnvelopeTag];
  1247. if Result='' then
  1248. Result:='div';
  1249. // Writeln('Getting element tag: ',Result);
  1250. end;
  1251. { TTextLinesWidget }
  1252. procedure TTextLinesWidget.SetLines(AValue: TStrings);
  1253. begin
  1254. if FLines=AValue then Exit;
  1255. FLines.Assign(AValue);
  1256. end;
  1257. procedure TTextLinesWidget.SetForceLineBreaks(AValue: Boolean);
  1258. begin
  1259. if FForceLineBreaks=AValue then Exit;
  1260. FForceLineBreaks:=AValue;
  1261. if IsRendered then
  1262. ApplyText(Element);
  1263. end;
  1264. procedure TTextLinesWidget.DoLinesChanged(Sender: TObject);
  1265. begin
  1266. if IsRendered then
  1267. ApplyText(Element);
  1268. end;
  1269. function TTextLinesWidget.GetText: String;
  1270. Var
  1271. I : integer;
  1272. begin
  1273. if (FTextMode=tmHTML) and ForceLineBreaks then
  1274. begin
  1275. Result:='';
  1276. For I:=0 to FLines.Count-1 do
  1277. Result:=Result+flines[i]+'<br/>';
  1278. end
  1279. else
  1280. Result:=FLines.Text;
  1281. end;
  1282. procedure TTextLinesWidget.ApplyText(aElement: TJSHTMLElement);
  1283. Var
  1284. I : integer;
  1285. begin
  1286. if (TextMode=tmHTML) or (Not ForceLineBreaks) then
  1287. inherited ApplyText(aElement)
  1288. else
  1289. begin
  1290. For I:=0 to FLines.Count-1 do
  1291. begin
  1292. aElement.AppendChild(Document.createTextNode(FLines[i]));
  1293. aElement.AppendChild(CreateElement('br',''));
  1294. end;
  1295. end;
  1296. end;
  1297. constructor TTextLinesWidget.Create(aOwner: TComponent);
  1298. begin
  1299. inherited Create(aOwner);
  1300. FLines:=TstringList.Create;
  1301. TstringList(FLines).OnChange:=@DoLinesChanged;
  1302. end;
  1303. destructor TTextLinesWidget.Destroy;
  1304. begin
  1305. FLines:=TstringList.Create;
  1306. inherited Destroy;
  1307. end;
  1308. { TTextWidget }
  1309. procedure TTextWidget.SetText(AValue: String);
  1310. begin
  1311. if FText=AValue then Exit;
  1312. FText:=AValue;
  1313. if IsRendered then
  1314. ApplyText(Element);
  1315. end;
  1316. function TTextWidget.GetText: String;
  1317. begin
  1318. Result:=FText;
  1319. end;
  1320. { TLabelWidget }
  1321. procedure TLabelWidget.ApplyLabelFor(aLabelElement : TJSHTMLLabelElement);
  1322. begin
  1323. if Assigned(FlabelFor) then
  1324. begin
  1325. FlabelFor.EnsureElement;
  1326. aLabelElement.for_:=FlabelFor.ElementID;
  1327. end
  1328. else
  1329. aLabelElement.for_:='';
  1330. end;
  1331. procedure TLabelWidget.SetLabelFor(AValue: TWebWidget);
  1332. begin
  1333. if (FLabelFor=AValue) then Exit;
  1334. if Assigned(FLabelFor) then
  1335. FLabelFor.RemoveFreeNotification(Self);
  1336. FLabelFor:=AValue;
  1337. if Assigned(FLabelFor) then
  1338. FLabelFor.FreeNotification(Self);
  1339. If IsRendered then
  1340. ApplyLabelFor(LabelElement);
  1341. end;
  1342. function TLabelWidget.GetText: String;
  1343. begin
  1344. if IsElementDirty then
  1345. FText:=Element.InnerText;
  1346. Result:=FText;
  1347. end;
  1348. function TLabelWidget.GetLabelEl: TJSHTMLLabelElement;
  1349. begin
  1350. Result:=TJSHTMLLabelElement(Element);
  1351. end;
  1352. procedure TLabelWidget.SetText(AValue: String);
  1353. begin
  1354. If Text=aValue then exit;
  1355. Ftext:=aValue;
  1356. If IsRendered then
  1357. Element.innerText:=aValue;
  1358. end;
  1359. procedure TLabelWidget.Notification(AComponent: TComponent; Operation: TOperation);
  1360. begin
  1361. inherited Notification(AComponent, Operation);
  1362. if (Operation=opRemove) and (aComponent=FLabelFor) then
  1363. FLabelFor:=Nil;
  1364. end;
  1365. procedure TLabelWidget.SetName(const NewName: TComponentName);
  1366. Var
  1367. Old : String;
  1368. begin
  1369. Old:=Name;
  1370. inherited SetName(NewName);
  1371. if (csDesigning in ComponentState) then
  1372. if Old=Text then
  1373. Text:=Old;
  1374. end;
  1375. procedure TLabelWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1376. var
  1377. lbl : TJSHTMLLabelElement absolute aElement;
  1378. begin
  1379. inherited ApplyWidgetSettings(aElement);
  1380. lbl.InnerText:=Text;
  1381. ApplyLabelFor(Lbl);
  1382. end;
  1383. function TLabelWidget.HTMLTag: String;
  1384. begin
  1385. Result:='label';
  1386. end;
  1387. { TSelectWidget }
  1388. function TCustomSelectWidget.GetSelectedIndex: Integer;
  1389. begin
  1390. if IsRendered then
  1391. FSelectedIndex:=SelectElement.selectedIndex;
  1392. Result:=FSelectedIndex
  1393. end;
  1394. function TCustomSelectWidget.GetMultiple: Boolean;
  1395. begin
  1396. if IsElementDirty then
  1397. FMultiple:=SelectElement.multiple;
  1398. Result:=FMultiple;
  1399. end;
  1400. function TCustomSelectWidget.GetItemCount: Integer;
  1401. begin
  1402. Result:=Length(Options);
  1403. end;
  1404. function TCustomSelectWidget.GetSelected(Index : Integer): Boolean;
  1405. begin
  1406. if (Index<0) or (Index>=Length(Foptions)) then
  1407. Raise EWidgets.CreateFmt(SErrInvalidIndex,[Index,Length(Foptions)-1]);
  1408. Result:=FOptions[Index].Selected
  1409. end;
  1410. function TCustomSelectWidget.GetSelect: TJSHTMLSelectElement;
  1411. begin
  1412. Result:=TJSHTMLSelectElement(Element);
  1413. end;
  1414. function TCustomSelectWidget.GetSelectionCount: Integer;
  1415. begin
  1416. Result:=SelectElement.selectedOptions.length;
  1417. end;
  1418. function TCustomSelectWidget.GetSelectionItem(aIndex : Integer): String;
  1419. begin
  1420. if (aIndex<0) or (aindex>=GetSelectionCount) then
  1421. Raise EWidgets.CreateFmt(SErrInvalidIndex,[aIndex,GetSelectionCount-1]);
  1422. Result:=TJSHTMLOptionElement(SelectElement.selectedOptions.item(aIndex)).innerText;
  1423. end;
  1424. function TCustomSelectWidget.GetSelectionValue(aIndex : Integer): String;
  1425. begin
  1426. if (aIndex<0) or (aindex>=GetSelectionCount) then
  1427. Raise EWidgets.CreateFmt(SErrInvalidIndex,[aIndex,GetSelectionCount-1]);
  1428. Result:=TJSHTMLOptionElement(SelectElement.selectedOptions.item(aIndex)).value;
  1429. end;
  1430. function TCustomSelectWidget.GetSize: Integer;
  1431. begin
  1432. if IsElementDirty then
  1433. FSize:=SelectElement.Size;
  1434. Result:=FSize;
  1435. end;
  1436. procedure TCustomSelectWidget.SetMultiple(AValue: Boolean);
  1437. begin
  1438. If (AValue=Multiple) then exit;
  1439. FMultiple:=aValue;
  1440. If IsRendered then
  1441. SelectElement.multiple:=FMultiple;
  1442. end;
  1443. procedure TCustomSelectWidget.SetSelected(Index : Integer; AValue: Boolean);
  1444. begin
  1445. if (Index<0) or (Index>=Length(Foptions)) then
  1446. Raise EWidgets.CreateFmt(SErrInvalidIndex,[Index,Length(Foptions)-1]);
  1447. FOptions[Index].Selected:=aValue;
  1448. end;
  1449. procedure TCustomSelectWidget.SetSelectedIndex(AValue: Integer);
  1450. begin
  1451. if (SelectedIndex=aValue) then
  1452. Exit;
  1453. FSelectedIndex:=aValue;
  1454. if IsRendered then
  1455. SelectElement.SelectedIndex:=FSelectedIndex;
  1456. if Assigned(OnChange) then
  1457. OnChange(Self,Nil);
  1458. end;
  1459. procedure TCustomSelectWidget.SetSize(AValue: Integer);
  1460. begin
  1461. If (AValue=Size) then exit;
  1462. FSize:=aValue;
  1463. If IsRendered then
  1464. SelectElement.Size:=FSize;
  1465. end;
  1466. procedure TCustomSelectWidget.BuildOptions(aSelect: TJSHTMLSelectElement);
  1467. Var
  1468. O : TJSHTMLOptionElement;
  1469. Idx : Integer;
  1470. enum : TSelectOptionEnumerator;
  1471. begin
  1472. // Clear
  1473. SetLength(FOptions,0);
  1474. aSelect.InnerHTML:='';
  1475. // Rebuild
  1476. Idx:=0;
  1477. enum:=CreateOptionEnumerator;
  1478. While enum.MoveNext do
  1479. begin
  1480. O:=TJSHTMLOptionElement(CreateElement('option',''));
  1481. O.innerText:=enum.OptionText;
  1482. if enum.HasValue then
  1483. O.value:=enum.Value;
  1484. if Idx=FSelectedIndex then
  1485. O.selected:=True;
  1486. aSelect.AppendChild(O);
  1487. Inc(Idx);
  1488. end;
  1489. SetLength(Foptions,Idx);
  1490. Dec(idx);
  1491. While Idx>=0 do
  1492. begin
  1493. FOptions[Idx]:=TJSHTMLOptionElement(aSelect.Children[Idx]);
  1494. dec(Idx);
  1495. end;
  1496. end;
  1497. constructor TCustomSelectWidget.Create(aOWner: TComponent);
  1498. begin
  1499. inherited Create(aOWner);
  1500. FSelectedIndex:=-1;
  1501. end;
  1502. procedure TCustomSelectWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1503. Var
  1504. el : TJSHTmlSelectElement absolute aElement;
  1505. begin
  1506. inherited ApplyWidgetSettings(aElement);
  1507. el.multiple:=Self.Multiple;
  1508. el.Size:=Self.Size;
  1509. BuildOptions(el);
  1510. // We need to force this.
  1511. if SelectedIndex=-1 then
  1512. el.selectedIndex:=-1;
  1513. end;
  1514. function TCustomSelectWidget.HTMLTag: String;
  1515. begin
  1516. Result:='select';
  1517. end;
  1518. { TSelectWidget }
  1519. function TSelectWidget.GetItems: TStrings;
  1520. begin
  1521. Result:=FItems;
  1522. end;
  1523. function TSelectWidget.GetValues: TStrings;
  1524. begin
  1525. Result:=FValues;
  1526. end;
  1527. procedure TSelectWidget.OptionsChanged(Sender: TObject);
  1528. begin
  1529. if IsRendered then
  1530. BuildOptions(SelectElement);
  1531. end;
  1532. procedure TSelectWidget.setItems(AValue: TStrings);
  1533. begin
  1534. If (AValue=FItems) then exit;
  1535. FItems.Assign(aValue);
  1536. end;
  1537. procedure TSelectWidget.setValues(AValue: TStrings);
  1538. begin
  1539. If (AValue=FValues) then exit;
  1540. FValues.Assign(aValue);
  1541. end;
  1542. function TSelectWidget.CreateOptionEnumerator: TSelectOptionEnumerator;
  1543. begin
  1544. Result:=TStringsSelectOptionEnumerator.Create(Self);
  1545. end;
  1546. constructor TSelectWidget.Create(aOWner: TComponent);
  1547. begin
  1548. inherited Create(aOWner);
  1549. FItems:=TStringList.Create;
  1550. TStringList(FItems).OnChange:=@OptionsChanged;
  1551. FValues:=TStringList.Create;
  1552. TStringList(FValues).OnChange:=@OptionsChanged;
  1553. end;
  1554. destructor TSelectWidget.Destroy;
  1555. begin
  1556. FreeAndNil(FItems);
  1557. FreeAndNil(FValues);
  1558. inherited Destroy;
  1559. end;
  1560. { TImageWidget }
  1561. function TImageWidget.GetHeight: Integer;
  1562. begin
  1563. if IsElementDirty then
  1564. FHeight:=ImgElement.Height;
  1565. Result:=Fheight;
  1566. end;
  1567. function TImageWidget.GetImg: TJSHTMLImageElement;
  1568. begin
  1569. Result:=TJSHTMLImageElement(Element);
  1570. end;
  1571. function TImageWidget.GetSrc: String;
  1572. begin
  1573. if IsElementDirty then
  1574. FSrc:=ImgElement.Src;
  1575. Result:=FSrc;
  1576. end;
  1577. function TImageWidget.GetWidth: Integer;
  1578. begin
  1579. if IsElementDirty then
  1580. FWidth:=ImgElement.Width;
  1581. Result:=FWidth;
  1582. end;
  1583. procedure TImageWidget.SetHeight(AValue: Integer);
  1584. begin
  1585. if AValue=Height then exit;
  1586. FHeight:=AValue;
  1587. If isrendered then
  1588. ImgElement.Height:=aValue;
  1589. end;
  1590. procedure TImageWidget.SetSrc(AValue: String);
  1591. begin
  1592. if AValue=Src then exit;
  1593. FSrc:=AValue;
  1594. If isrendered then
  1595. ImgElement.Src:=FSrc;
  1596. end;
  1597. procedure TImageWidget.SetWidth(AValue: Integer);
  1598. begin
  1599. if AValue=Width then exit;
  1600. FWidth:=AValue;
  1601. If isrendered then
  1602. ImgElement.Width:=aValue;
  1603. end;
  1604. procedure TImageWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1605. var
  1606. img : TJSHTMLImageElement absolute aElement;
  1607. begin
  1608. inherited ApplyWidgetSettings(aElement);
  1609. Img.Src:=FSrc;
  1610. Img.Height:=FHeight;
  1611. Img.Width:=FWidth;
  1612. end;
  1613. function TImageWidget.HTMLTag: String;
  1614. begin
  1615. Result:='img';
  1616. end;
  1617. { TTextAreaWidget }
  1618. procedure TTextAreaWidget.SetLines(AValue: TStrings);
  1619. begin
  1620. if FLines=AValue then Exit;
  1621. FLines.Assign(AValue);
  1622. end;
  1623. procedure TTextAreaWidget.SetMaxLength(AValue: Cardinal);
  1624. begin
  1625. if FMaxLength=AValue then Exit;
  1626. FMaxLength:=AValue;
  1627. if IsRendered then
  1628. TextArea.maxLength:=aValue;
  1629. end;
  1630. procedure TTextAreaWidget.SetReadonly(AValue: Boolean);
  1631. begin
  1632. If aValue=ReadOnly then exit;
  1633. FReadOnly:=aValue;
  1634. if IsRendered then
  1635. TextArea.Readonly:=FReadOnly;
  1636. end;
  1637. procedure TTextAreaWidget.SetRequired(AValue: Boolean);
  1638. begin
  1639. If aValue=Required then exit;
  1640. FRequired:=aValue;
  1641. if IsRendered then
  1642. TextArea.Required:=FRequired;
  1643. end;
  1644. function TTextAreaWidget.GetColumns: Cardinal;
  1645. begin
  1646. if IsElementDirty then
  1647. FColumns:=TextArea.Cols;
  1648. Result:=FColumns;
  1649. end;
  1650. procedure TTextAreaWidget.DoLineChanges(Sender: TObject);
  1651. begin
  1652. if isRendered and not FIgnoreChanges then
  1653. ApplyLines(TextArea);
  1654. end;
  1655. function TTextAreaWidget.GetLines: TStrings;
  1656. begin
  1657. // We may want to change this to something more efficient. Maybe handle onchange
  1658. // Note that if yo
  1659. if IsElementDirty then
  1660. begin
  1661. FIgnoreChanges:=True;
  1662. try
  1663. LinesFromHTML(Element.InnerHTml);
  1664. finally
  1665. FIgnoreChanges:=False;
  1666. end;
  1667. end;
  1668. Result:=FLines;
  1669. end;
  1670. function TTextAreaWidget.GetReadOnly: Boolean;
  1671. begin
  1672. if IsElementDirty then
  1673. FReadonly:=TextArea.readOnly;
  1674. Result:=FReadonly;
  1675. end;
  1676. function TTextAreaWidget.GetRequired: Boolean;
  1677. begin
  1678. if IsElementDirty then
  1679. FRequired:=TextArea.Required;
  1680. Result:=FRequired;
  1681. end;
  1682. function TTextAreaWidget.GetRows: Cardinal;
  1683. begin
  1684. if IsElementDirty then
  1685. FRows:=TextArea.Rows;
  1686. Result:=FRows;
  1687. end;
  1688. function TTextAreaWidget.GetText: String;
  1689. begin
  1690. if IsElementDirty then
  1691. Result:=Element.InnerHTML
  1692. else
  1693. Result:=FLines.Text;
  1694. end;
  1695. function TTextAreaWidget.GetValueName: string;
  1696. begin
  1697. if IsElementDirty then
  1698. FValueName:=Element.Name;
  1699. Result:=FValueName;
  1700. end;
  1701. procedure TTextAreaWidget.SetColumns(AValue: Cardinal);
  1702. begin
  1703. if AValue=FColumns then exit;
  1704. FColumns:=aValue;
  1705. if isRendered then
  1706. TextArea.cols:=aValue;
  1707. end;
  1708. procedure TTextAreaWidget.SetRows(AValue: Cardinal);
  1709. begin
  1710. if AValue=FRows then exit;
  1711. FRows:=aValue;
  1712. if isRendered then
  1713. TextArea.Rows:=aValue;
  1714. end;
  1715. procedure TTextAreaWidget.SetText(AValue: String);
  1716. begin
  1717. if isRendered then
  1718. element.InnerText:=aValue
  1719. else
  1720. LinesFromHTML(aValue);
  1721. end;
  1722. procedure TTextAreaWidget.SetValueName(AValue: string);
  1723. begin
  1724. if aValue=FValueName then exit;
  1725. FValueName:=aValue;
  1726. if IsRendered then
  1727. TextArea.Name:=aValue;
  1728. end;
  1729. procedure TTextAreaWidget.SetName(const NewName: TComponentName);
  1730. var
  1731. Old : String;
  1732. begin
  1733. Old:=Name;
  1734. inherited SetName(NewName);
  1735. if csDesigning in ComponentState then
  1736. begin
  1737. if (FLines.Count=0) then
  1738. FLines.Add(Name)
  1739. else if (FLines.Count=1) and (FLines[0]=Old) then
  1740. FLines[0]:=Name;
  1741. end;
  1742. end;
  1743. procedure TTextAreaWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1744. var
  1745. area : TJSHTMLTextAreaElement absolute aElement;
  1746. begin
  1747. inherited ApplyWidgetSettings(aElement);
  1748. if FMaxLength>0 then
  1749. area.maxlength:=FMaxLength;
  1750. if FColumns>0 then
  1751. area.cols:=FColumns;
  1752. if FRows>0 then
  1753. area.Rows:=FRows;
  1754. if FLines.Count>0 then
  1755. ApplyLines(area);
  1756. if FValueName<>'' then
  1757. area.Name:=FValueName;
  1758. area.Readonly:=FReadOnly;
  1759. area.Required:=FRequired;
  1760. ApplyWrap(area);
  1761. end;
  1762. constructor TTextAreaWidget.Create(aOwner: TComponent);
  1763. begin
  1764. inherited Create(aOwner);
  1765. FLines:=TStringList.Create;
  1766. TStringList(FLines).OnChange:=@DoLineChanges;
  1767. FColumns:=50;
  1768. FRows:=10;
  1769. end;
  1770. destructor TTextAreaWidget.Destroy;
  1771. begin
  1772. FreeAndNil(Flines);
  1773. inherited;
  1774. end;
  1775. class function TTextAreaWidget.AllowChildren: Boolean;
  1776. begin
  1777. Result:=False;
  1778. end;
  1779. function TTextAreaWidget.GetTextArea: TJSHTMLTextAreaElement;
  1780. begin
  1781. Result:=TJSHTMLTextAreaElement(Element);
  1782. end;
  1783. procedure TTextAreaWidget.ApplyWrap(aElement :TJSHTMLTextAreaElement);
  1784. Const
  1785. Wraps : Array[TTextAreaWrap] of string = ('soft','hard','off');
  1786. begin
  1787. aElement.wrap:=Wraps[FWrap];
  1788. end;
  1789. procedure TTextAreaWidget.ApplyLines(aElement: TJSHTMLTextAreaElement);
  1790. begin
  1791. aElement.innerHTML:=FLines.Text;
  1792. end;
  1793. procedure TTextAreaWidget.LinesFromHTML(aHTML: String);
  1794. begin
  1795. FLines.Text:= StringReplace(aHTML,'<br>',sLineBreak,[rfIgnoreCase,rfReplaceAll]);
  1796. end;
  1797. procedure TTextAreaWidget.SetWrap(AValue: TTextAreaWrap);
  1798. begin
  1799. if FWrap=AValue then Exit;
  1800. FWrap:=AValue;
  1801. if IsRendered then
  1802. ApplyWrap(TextArea)
  1803. end;
  1804. function TTextAreaWidget.HTMLTag: String;
  1805. begin
  1806. result:='textarea';
  1807. end;
  1808. { TCheckboxInputWidget }
  1809. function TCheckboxInputWidget.InputType: String;
  1810. begin
  1811. Result:='checkbox';
  1812. end;
  1813. { TRadioInputWidget }
  1814. function TRadioInputWidget.InputType: String;
  1815. begin
  1816. Result:='radio';
  1817. end;
  1818. { THiddenInputWidget }
  1819. class function THiddenInputWidget.AllowChildren: Boolean;
  1820. begin
  1821. Result:=False;
  1822. end;
  1823. function THiddenInputWidget.InputType: String;
  1824. begin
  1825. Result:='hidden';
  1826. end;
  1827. { TFileInputWidget }
  1828. procedure TFileInputWidget.SetMultiple(AValue: Boolean);
  1829. begin
  1830. if FMultiple=AValue then Exit;
  1831. FMultiple:=AValue;
  1832. if Isrendered then
  1833. InputElement.multiple:=FMultiple;
  1834. end;
  1835. function TFileInputWidget.GetMultiple: Boolean;
  1836. begin
  1837. if IsElementDirty then
  1838. FMultiple:=InputElement.multiple;
  1839. Result:=FMultiple;
  1840. end;
  1841. function TFileInputWidget.GetFileName(aIndex : Integer): String;
  1842. begin
  1843. Result:=InputElement.files.Files[aIndex].name;
  1844. end;
  1845. function TFileInputWidget.GetFileSize(aIndex : Integer): NativeInt;
  1846. begin
  1847. Result:=InputElement.files.Files[aIndex].Size;
  1848. end;
  1849. function TFileInputWidget.GetFileType(aIndex : Integer): String;
  1850. begin
  1851. Result:=InputElement.files.Files[aIndex]._Type;
  1852. end;
  1853. function TFileInputWidget.GetFileCount: Integer;
  1854. begin
  1855. Result:=InputElement.files.Length;
  1856. end;
  1857. function TFileInputWidget.GetFileDate(aIndex : Integer): TDateTime;
  1858. begin
  1859. Result:=JSDateToDateTime(InputElement.files.Files[aIndex].lastModifiedDate);
  1860. end;
  1861. function TFileInputWidget.GetFileInfo(aIndex : Integer): TFileInfo;
  1862. Var
  1863. f : TJSHTMLFile;
  1864. begin
  1865. F:=InputElement.files.Files[aIndex];
  1866. Result.Name:=F.name;
  1867. Result.Size:=F.size;
  1868. Result.FileType:=F._type;
  1869. Result.TimeStamp:= JSDateToDateTime(F.lastModifiedDate);
  1870. end;
  1871. procedure TFileInputWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1872. Var
  1873. Old : String;
  1874. begin
  1875. Old:=FValue;
  1876. FValue:='';
  1877. try
  1878. inherited ApplyWidgetSettings(aElement);
  1879. TJSHTMLInputElement(aElement).multiple:=FMultiple;
  1880. finally
  1881. FValue:=Old;
  1882. end;
  1883. end;
  1884. class function TFileInputWidget.AllowChildren: Boolean;
  1885. begin
  1886. Result:=False;
  1887. end;
  1888. function TFileInputWidget.InputType: String;
  1889. begin
  1890. Result:='file';
  1891. end;
  1892. { TDateInputWidget }
  1893. function TDateInputWidget.GetDate: TDateTime;
  1894. var
  1895. aDate : TDateTime;
  1896. begin
  1897. if IsElementDirty then
  1898. begin
  1899. aDate:=ScanDateTime('yyyy-mm-dd',Value);
  1900. if aDate<>0 then
  1901. FDate:=aDate;
  1902. end;
  1903. Result:=FDate;
  1904. end;
  1905. procedure TDateInputWidget.SetDate(AValue: TDateTime);
  1906. begin
  1907. FDate:=aValue;
  1908. Value:=FormatDateTime('yyyy-mm-dd',FDate);
  1909. end;
  1910. function TDateInputWidget.InputType: String;
  1911. begin
  1912. Result:='date';
  1913. end;
  1914. class function TDateInputWidget.AllowChildren: Boolean;
  1915. begin
  1916. Result:=False;
  1917. end;
  1918. { TCheckableInputWidget }
  1919. procedure TCheckableInputWidget.SetChecked(AValue: Boolean);
  1920. begin
  1921. // Get actual value
  1922. if Checked=AValue then Exit;
  1923. if isRendered then
  1924. InputElement.checked:=aValue;
  1925. FChecked:=AValue;
  1926. end;
  1927. procedure TCheckableInputWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1928. begin
  1929. inherited ApplyWidgetSettings(aElement);
  1930. TJSHTMLInputElement(aElement).Checked:=FChecked;
  1931. end;
  1932. function TCheckableInputWidget.GetChecked: Boolean;
  1933. begin
  1934. if IsElementDirty then
  1935. FChecked:=InputElement.Checked;
  1936. Result:=FChecked;
  1937. end;
  1938. { TButtonInputWidget }
  1939. procedure TButtonInputWidget.SetButtonType(AValue: TInputButtonType);
  1940. begin
  1941. if FButtonType=AValue then Exit;
  1942. FButtonType:=AValue;
  1943. if IsRendered then
  1944. Refresh;
  1945. end;
  1946. procedure TButtonInputWidget.SetSrc(AValue: String);
  1947. begin
  1948. if FSrc=AValue then Exit;
  1949. FSrc:=AValue;
  1950. if IsRendered and (ButtonType=ibtImage) then
  1951. Element.setAttribute('src',FSrc);
  1952. end;
  1953. procedure TButtonInputWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  1954. begin
  1955. inherited ApplyWidgetSettings(aElement);
  1956. if ButtonType=ibtImage then
  1957. aElement.setAttribute('src',FSrc);
  1958. end;
  1959. function TButtonInputWidget.InputType: String;
  1960. Const
  1961. Types : Array[TInputButtonType] of string = ('submit','reset','image');
  1962. begin
  1963. Result:=Types[FButtonType]
  1964. end;
  1965. class function TButtonInputWidget.AllowChildren: Boolean;
  1966. begin
  1967. Result:=False;
  1968. end;
  1969. { TTextInputWidget }
  1970. function TTextInputWidget.GetAsNumber: NativeInt;
  1971. begin
  1972. Result:=StrToIntDef(Value,0);
  1973. end;
  1974. function TTextInputWidget.GetMaxLength: NativeInt;
  1975. begin
  1976. if IsElementDirty then
  1977. FMaxLength:=InputElement.maxLength;
  1978. Result:=FMaxLength;
  1979. end;
  1980. function TTextInputWidget.GetMinLength: NativeInt;
  1981. begin
  1982. if IsElementDirty then
  1983. FMinLength:=InputElement.minLength;
  1984. Result:=FMinLength;
  1985. end;
  1986. function TTextInputWidget.GetTextType: TInputTextType;
  1987. begin
  1988. Result:=FTextType;
  1989. end;
  1990. procedure TTextInputWidget.SetAsNumber(AValue: NativeInt);
  1991. begin
  1992. Value:=IntToStr(aValue);
  1993. end;
  1994. procedure TTextInputWidget.SetMaxLength(AValue: NativeInt);
  1995. begin
  1996. if (aValue=FMaxLength) then exit;
  1997. FMaxLength:=aValue;
  1998. if IsRendered then
  1999. InputElement.maxLength:=FMaxLength;
  2000. end;
  2001. procedure TTextInputWidget.SetMinLength(AValue: NativeInt);
  2002. begin
  2003. if (aValue=FMinLength) then exit;
  2004. FMinLength:=aValue;
  2005. if IsRendered then
  2006. InputElement.minLength:=FMinLength;
  2007. end;
  2008. procedure TTextInputWidget.SetTextType(AValue: TInputTextType);
  2009. begin
  2010. if aValue=FTextType then exit;
  2011. FTextType:=aValue;
  2012. if IsRendered then
  2013. Refresh;
  2014. end;
  2015. procedure TTextInputWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  2016. var
  2017. inp : TJSHTMLInputElement absolute aElement;
  2018. begin
  2019. inherited ApplyWidgetSettings(aElement);
  2020. if FMaxLength<>0 then
  2021. inp.maxLength:=FMaxLength;
  2022. if FMinLength<>0 then
  2023. inp.minLength:=FMinLength;
  2024. end;
  2025. class function TTextInputWidget.AllowChildren: Boolean;
  2026. begin
  2027. Result:=False;
  2028. end;
  2029. function TTextInputWidget.InputType: String;
  2030. Const
  2031. Types : Array[TInputTextType] of string =
  2032. ('text','password','number','email','search','tel','url','color');
  2033. begin
  2034. Result:=Types[FTextType];
  2035. end;
  2036. { TWebPage }
  2037. constructor TWebPage.Create(AOwner: TComponent);
  2038. begin
  2039. inherited Create(AOwner);
  2040. Classes:='WebPage';
  2041. end;
  2042. class function TWebPage.DefaultParentElement: TJSHTMLElement;
  2043. begin
  2044. Result:=TViewport.Instance.Element;
  2045. end;
  2046. class function TWebPage.DefaultParent: TCustomWebWidget;
  2047. begin
  2048. Result:=TViewport.Instance;
  2049. end;
  2050. procedure TWebPage.DoUnRender(aParent: TJSHTMLElement);
  2051. begin
  2052. inherited DoUnRender(aParent);
  2053. end;
  2054. function TWebPage.HTMLTag: String;
  2055. begin
  2056. Result:='div';
  2057. end;
  2058. { TViewPort }
  2059. function TViewPort.HTMLTag: String;
  2060. begin
  2061. Result:='body';
  2062. end;
  2063. class function TViewPort.FixedParent: TJSHTMLElement;
  2064. begin
  2065. Result:=TJSHTMLElement(Document.documentElement);
  2066. end;
  2067. class function TViewPort.FixedElement: TJSHTMLElement;
  2068. begin
  2069. Result:=TJSHTMLElement(Document.Body);
  2070. end;
  2071. function TViewPort.DoRenderHTML(aParent, aElement: TJSHTMLElement): TJSHTMLElement;
  2072. begin
  2073. Result:=FixedElement;
  2074. end;
  2075. constructor TViewPort.Create(aOwner: TComponent);
  2076. begin
  2077. inherited Create(aOwner);
  2078. EnsureElement;
  2079. end;
  2080. class function TViewPort.Instance: TViewPort;
  2081. begin
  2082. if Finstance=Nil then
  2083. FInstance:=TViewPort.Create(Nil);
  2084. Result:=FInstance;
  2085. end;
  2086. { TButtonWidget }
  2087. { TButtonWidget }
  2088. procedure TButtonWidget.SetText(AValue: String);
  2089. begin
  2090. if FText=AValue then Exit;
  2091. FText:=AValue;
  2092. if IsRendered then
  2093. ApplyText(Element);
  2094. end;
  2095. procedure TButtonWidget.SetTextMode(AValue: TTextMode);
  2096. begin
  2097. if FTextMode=AValue then Exit;
  2098. FTextMode:=AValue;
  2099. if IsRendered then
  2100. ApplyText(Element)
  2101. end;
  2102. procedure TButtonWidget.SetName(const NewName: TComponentName);
  2103. Var
  2104. Old : String;
  2105. begin
  2106. Old:=Name;
  2107. inherited SetName(NewName);
  2108. if (FText=Old) and (csDesigning in ComponentState) then
  2109. FText:=NewName;
  2110. end;
  2111. function TButtonWidget.HTMLTag: String;
  2112. begin
  2113. Result:='button';
  2114. end;
  2115. procedure TButtonWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  2116. begin
  2117. Inherited;
  2118. ApplyText(aElement);
  2119. end;
  2120. Procedure TButtonWidget.ApplyText(aElement : TJSHTMLElement);
  2121. begin
  2122. if FTextMode=tmText then
  2123. aElement.InnerText:=FText
  2124. else
  2125. aElement.InnerHTML:=FText;
  2126. end;
  2127. procedure TButtonWidget.Click;
  2128. begin
  2129. DispatchEvent('click');
  2130. end;
  2131. { TCustomInputWidget }
  2132. function TCustomInputWidget.GetValue: String;
  2133. Var
  2134. Inp : TJSHTMLInputElement;
  2135. begin
  2136. Inp:=InputElement;
  2137. If Assigned(Inp) then
  2138. Result:=Inp.value
  2139. else
  2140. Result:=FValue
  2141. end;
  2142. function TCustomInputWidget.GetText: String;
  2143. Var
  2144. Inp : TJSHTMLElement;
  2145. begin
  2146. Inp:=Element;
  2147. If Assigned(Inp) then
  2148. Result:=Inp.InnerText
  2149. else
  2150. Result:=FText;
  2151. // Writeln('Getting text: ',Result,' inner : ',FText);
  2152. end;
  2153. function TCustomInputWidget.GetReadOnly: Boolean;
  2154. begin
  2155. if IsElementDirty then
  2156. FReadonly:=InputElement.readOnly;
  2157. Result:=FReadonly;
  2158. end;
  2159. function TCustomInputWidget.GetRequired: Boolean;
  2160. begin
  2161. if IsElementDirty then
  2162. FRequired:=InputElement.Required;
  2163. Result:=FRequired;
  2164. end;
  2165. function TCustomInputWidget.GetValueName: String;
  2166. Var
  2167. Inp : TJSHTMLInputElement;
  2168. begin
  2169. Inp:=InputElement;
  2170. If Assigned(Inp) then
  2171. Result:=Inp.Name
  2172. else
  2173. begin
  2174. Result:=FValueName;
  2175. if Result='' then
  2176. Result:=Name;
  2177. end;
  2178. end;
  2179. procedure TCustomInputWidget.SetReadonly(AValue: Boolean);
  2180. begin
  2181. If aValue=ReadOnly then exit;
  2182. FReadOnly:=aValue;
  2183. if IsRendered then
  2184. InputElement.Readonly:=FReadOnly;
  2185. end;
  2186. procedure TCustomInputWidget.SetRequired(AValue: Boolean);
  2187. begin
  2188. If aValue=Required then exit;
  2189. FRequired:=aValue;
  2190. if IsRendered then
  2191. InputElement.Required:=FRequired;
  2192. end;
  2193. procedure TCustomInputWidget.SetText(AValue: String);
  2194. Var
  2195. Inp : TJSHTMLElement;
  2196. begin
  2197. Writeln('Setting text: ',AValue,' previous : ',Text);
  2198. if aValue=Text then exit;
  2199. FText:=aValue;
  2200. Inp:=Element;
  2201. If Assigned(Inp) then
  2202. Inp.innerText:=aValue;
  2203. end;
  2204. procedure TCustomInputWidget.SetValue(AValue: String);
  2205. Var
  2206. Inp : TJSHTMLInputElement;
  2207. begin
  2208. if aValue=Value then exit;
  2209. FValue:=aValue;
  2210. Inp:=InputElement;
  2211. If Assigned(Inp) then
  2212. Inp.value:=aValue;
  2213. end;
  2214. procedure TCustomInputWidget.ApplyWidgetSettings(aElement: TJSHTMLElement);
  2215. var
  2216. Inp : TJSHTMLInputElement absolute aElement;
  2217. begin
  2218. Inherited;
  2219. if (ExternalElement) and (FValue='') then
  2220. FValue:=TJSHTMLInputElement(aElement).value
  2221. else
  2222. begin
  2223. Inp._type:=InputType;
  2224. Inp.name:=FValueName;
  2225. Inp.value:=FValue;
  2226. Inp.Required:=FRequired;
  2227. Inp.ReadOnly:=FReadOnly;
  2228. Writeln('Setting inner text to "',FText,'"');
  2229. Inp.innerHtml:=FText;
  2230. Writeln('Setting inner text is now "',Inp.innerText,'"');
  2231. end;
  2232. end;
  2233. function TCustomInputWidget.HTMLTag: String;
  2234. begin
  2235. Result:='input';
  2236. end;
  2237. function TCustomInputWidget.GetInputElement: TJSHTMLInputElement;
  2238. begin
  2239. Result:=TJSHTMLInputElement(Element);
  2240. end;
  2241. procedure TCustomInputWidget.SetValueName(AValue: String);
  2242. Var
  2243. Inp : TJSHTMLInputElement;
  2244. begin
  2245. if aValue=ValueName then exit;
  2246. FValueName:=aValue;
  2247. Inp:=InputElement;
  2248. If Assigned(Inp) then
  2249. Inp.name:=aValue;
  2250. end;
  2251. procedure TCustomInputWidget.SetName(const NewName: TComponentName);
  2252. Var
  2253. Old : String;
  2254. begin
  2255. Old:=Name;
  2256. inherited SetName(NewName);
  2257. if (Value=Old) then
  2258. Value:=NewName;
  2259. end;
  2260. end.