JQuery.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package h2d.comp;
  2. import h2d.css.Defs;
  3. private typedef Query = Array<CssClass>;
  4. @:access(h2d.comp.Component)
  5. @:keep
  6. class JQuery {
  7. var root : Component;
  8. var select : Array<Component>;
  9. public function new( root : Component, query : Dynamic ) {
  10. while( root.parentComponent != null )
  11. root = root.parentComponent;
  12. this.root = root;
  13. select = getSet(query);
  14. }
  15. public function getComponents() {
  16. return select;
  17. }
  18. public function toggleClass( cl : String, ?flag : Bool ) {
  19. for( s in select ) s.toggleClass(cl,flag);
  20. return this;
  21. }
  22. public function find( q : Dynamic ) {
  23. if( Std.is(q, Component) )
  24. return new JQuery(root, Lambda.has(select, q) ? null : q);
  25. if( Std.is(q, String) ) {
  26. var q = parseQuery(q);
  27. var out = [];
  28. for( s in select )
  29. lookupRec(s, q, out);
  30. return new JQuery(root, out);
  31. }
  32. throw "Invalid JQuery " + q;
  33. return null;
  34. }
  35. public function filter( q : Dynamic ) {
  36. if( Std.is(q, Component) )
  37. return new JQuery(root, Lambda.has(select, q) ? null : q);
  38. if( Std.is(q, String) ) {
  39. var q = parseQuery(q);
  40. return new JQuery(root, [for( s in select ) if( matchQuery(q, s) ) s]);
  41. }
  42. if( Std.is(q, JQuery) ) {
  43. var q : JQuery = q;
  44. return new JQuery(root, [for( s in select ) if( Lambda.has(q.select, s) ) s]);
  45. }
  46. throw "Invalid JQuery " + q;
  47. return null;
  48. }
  49. public function not( q : Dynamic ) {
  50. if( Std.is(q, Component) )
  51. return new JQuery(root, [for( s in select ) if( s != q ) s]);
  52. if( Std.is(q, String) ) {
  53. var q = parseQuery(q);
  54. return new JQuery(root, [for( s in select ) if( !matchQuery(q, s) ) s]);
  55. }
  56. if( Std.is(q, JQuery) ) {
  57. var q : JQuery = q;
  58. return new JQuery(root, [for( s in select ) if( !Lambda.has(q.select, s) ) s]);
  59. }
  60. throw "Invalid JQuery " + q;
  61. return null;
  62. }
  63. public function click( f : JQuery -> Void ) {
  64. for( c in select ) {
  65. var int = Std.instance(c, Interactive);
  66. if( int == null ) throw c + " is not interactive";
  67. int.onClick = function() f(new JQuery(root,c));
  68. }
  69. return this;
  70. }
  71. public function show() {
  72. for( s in select )
  73. s.getStyle(true).display = true;
  74. return this;
  75. }
  76. public function hide() {
  77. for( s in select )
  78. s.getStyle(true).display = false;
  79. return this;
  80. }
  81. public function toggle() {
  82. for( s in select ) {
  83. var s = s.getStyle(true);
  84. s.display = !s.display;
  85. }
  86. return this;
  87. }
  88. public function iterator() {
  89. var it = select.iterator();
  90. return {
  91. hasNext : it.hasNext,
  92. next : function() return new JQuery(root, it.next()),
  93. };
  94. }
  95. function _get_val() : Dynamic {
  96. var c = select[0];
  97. if( c == null ) return null;
  98. return switch( c.name ) {
  99. case "slider":
  100. cast(c, h2d.comp.Slider).value;
  101. case "checkbox":
  102. cast(c, h2d.comp.Checkbox).checked;
  103. case "input":
  104. cast(c, h2d.comp.Input).value;
  105. case "color":
  106. cast(c, h2d.comp.Color).value;
  107. case "itemlist":
  108. cast(c, h2d.comp.ItemList).selected;
  109. case "select":
  110. cast(c, h2d.comp.Select).value;
  111. default:
  112. null;
  113. }
  114. }
  115. function _set_val( v : Dynamic ) {
  116. for( c in select )
  117. switch( c.name ) {
  118. case "slider":
  119. cast(c, h2d.comp.Slider).value = v;
  120. case "checkbox":
  121. cast(c, h2d.comp.Checkbox).checked = v != null && v != false;
  122. case "input":
  123. cast(c, h2d.comp.Input).value = Std.string(v);
  124. case "color":
  125. cast(c, h2d.comp.Color).value = v;
  126. case "itemlist":
  127. cast(c, h2d.comp.ItemList).selected = v;
  128. case "select":
  129. cast(c, h2d.comp.Select).setValue(v);
  130. default:
  131. null;
  132. }
  133. return this;
  134. }
  135. function _get_text() {
  136. var c = select[0];
  137. if( c == null ) return "";
  138. return switch( c.name ) {
  139. case "button":
  140. cast(c, h2d.comp.Button).text;
  141. case "label":
  142. cast(c, h2d.comp.Label).text;
  143. default:
  144. "";
  145. }
  146. }
  147. function _set_text(v:String) {
  148. for( c in select )
  149. switch( c.name ) {
  150. case "button":
  151. cast(c, h2d.comp.Button).text = v;
  152. case "label":
  153. cast(c, h2d.comp.Label).text = v;
  154. default:
  155. }
  156. return this;
  157. }
  158. function _set_style(v:String) {
  159. var s = new h2d.css.Style();
  160. new h2d.css.Parser().parse(v, s);
  161. for( c in select )
  162. c.addStyle(s);
  163. return this;
  164. }
  165. function getSet( query : Dynamic ) {
  166. var set;
  167. if( query == null )
  168. set = [];
  169. else if( Std.is(query,Component) )
  170. set = [query];
  171. else if( Std.is(query, Array) ) {
  172. var a : Array<Dynamic> = query;
  173. for( v in a ) if( !Std.is(v, Component) ) throw "Invalid JQuery "+query;
  174. set = a;
  175. } else if( Std.is(query, String) )
  176. set = lookup(root, query);
  177. else
  178. throw "Invalid JQuery " + query;
  179. return set;
  180. }
  181. function lookup( root : Component, query : String ) {
  182. var set = [];
  183. lookupRec(root, parseQuery(query), set);
  184. return set;
  185. }
  186. function parseQuery(q) : Query {
  187. return new h2d.css.Parser().parseClasses(q);
  188. }
  189. function matchQuery(q:Query, comp:Component) {
  190. for( r in q )
  191. if( h2d.css.Engine.ruleMatch(r, comp) )
  192. return true;
  193. return false;
  194. }
  195. function lookupRec(comp:Component, q, set : Array<Component> ) {
  196. if( matchQuery(q, comp) )
  197. set.push(comp);
  198. for( s in comp.components )
  199. lookupRec(s, q, set);
  200. }
  201. }