1 module awebview.gui.widgets.radio; 2 3 import awebview.gui.html; 4 import std.exception; 5 6 interface IRadio 7 { 8 @property 9 bool isChecked(); 10 11 @property 12 void isChecked(bool bChecked); 13 } 14 15 16 abstract class HTMLRadio : HTMLElement, IRadio 17 { 18 this(string id, bool doCreateObject) 19 { 20 super(id, doCreateObject); 21 } 22 23 24 override 25 @property 26 bool isChecked(){ return this["checked"].get!bool; } 27 28 override 29 @property 30 void isChecked(bool bChecked){ this["checked"] = bChecked; } 31 } 32 33 34 class InputRadio(alias attrs = null) 35 : TemplateHTMLElement!(HTMLRadio, `<input type="radio" id="%[id%]" value="%[id%]">`) 36 { 37 this(string id) 38 { 39 super(id, true); 40 } 41 } 42 43 44 class HTMLRadioGroup : HTMLElement 45 { 46 this(string id, HTMLRadio[] rs) 47 { 48 super(id, false); 49 _rs ~= rs.dup; 50 51 foreach(e; _rs) 52 e.staticProps["name"] = this.id; 53 } 54 55 56 @property 57 inout(HTMLRadio)[] elements() inout 58 { 59 return _rs; 60 } 61 62 63 @property 64 HTMLRadio checked() 65 { 66 foreach(e; _rs) 67 if(e.isChecked) 68 return e; 69 70 return null; 71 } 72 73 74 @property 75 void checked(string id) 76 { 77 foreach(i, e; _rs) 78 if(e.id == id){ 79 e.isChecked = true; 80 return; 81 } 82 83 enforce(0); 84 } 85 86 87 private: 88 HTMLRadio[] _rs; 89 }