JQuery.hx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package js;
  2. typedef JqEvent = {
  3. var target : Dom.HtmlDom;
  4. var currentTarget : Dom.HtmlDom;
  5. var relatedTarget : Dom.HtmlDom;
  6. var type : String;
  7. var timeStamp : Int;
  8. //var data : Dynamic;
  9. //var namespace : String;
  10. //var result : Dynamic;
  11. // position
  12. var pageX : Int;
  13. var pageY : Int;
  14. // keys
  15. var which : Int;
  16. // propagation
  17. function isDefaultPrevented() : Bool;
  18. function isImmediatePropagationStopped() : Bool;
  19. function isPropationStopped() : Bool;
  20. function preventDefault() : Void;
  21. function stopImmediatePropagation() : Void;
  22. function stopPropagation() : Void;
  23. }
  24. extern class JQuery implements ArrayAccess<JQuery> {
  25. var context(default,null) : Dom.HtmlDom;
  26. var length(default, null) : Int;
  27. function new( queryOrHtml : String, ?context : JQuery ) : Void;
  28. // attributes
  29. function addClass( className : String ) : JQuery;
  30. function removeClass( className : String ) : JQuery;
  31. function hasClass( className : String ) : Bool;
  32. function toggleClass( className : String, ?addRemove : Bool ) : JQuery;
  33. function attr( name : String, ?value : String ) : String;
  34. function removeAttr( attr : String ) : JQuery;
  35. function css( prop : String, ?value : String ) : String;
  36. function html( ?fill : String ) : String; // first element only
  37. function val() : String; // for input elements
  38. function text( ?value : String ) : String;
  39. // Size & Position
  40. function width( ?value : Int ) : Int;
  41. function height( ?value : Int ) : Int;
  42. function innerWidth( ?value : Int ) : Int;
  43. function innerHeight( ?value : Int ) : Int;
  44. function outerWidth( ?value : Int ) : Int;
  45. function outerHeight( ?value : Int ) : Int;
  46. function scrollLeft( ?value : Int ) : Int;
  47. function scrollTop( ?value : Int ) : Int;
  48. function offset( ?value : { left : Int, top : Int } ) : { left : Int, top : Int };
  49. function offsetParent() : JQuery;
  50. function position( ?value : { left : Int, top : Int } ) : { left : Int, top : Int };
  51. // current group manipulation
  52. function add( selectorOrHTML : String, ?context : JQuery ) : JQuery;
  53. function andSelf() : JQuery;
  54. function children( ?selector : String ) : JQuery;
  55. function clone( ?withDataAndEvents : Bool ) : JQuery;
  56. function closest( selector : String, ?context : JQuery ) : JQuery;
  57. function contents() : JQuery;
  58. function each( f : Void -> Void ) : JQuery;
  59. function end() : JQuery;
  60. function eq( index : Int ) : JQuery;
  61. function filter( selector : String ) : JQuery;
  62. function find( selector : String ) : JQuery;
  63. function first() : JQuery;
  64. function index( ?selector : String ) : Int;
  65. function last( ?selector : String ) : JQuery;
  66. function has( selector : String ) : JQuery;
  67. function next( ?selector : String ) : JQuery;
  68. function nextAll( ?selector : String ) : JQuery;
  69. function nextUntil( ?selector : String ) : JQuery;
  70. function parent( ?selector : String ) : JQuery;
  71. function parents( ?selector : String ) : JQuery;
  72. function parentsUntil( ?selector : String ) : JQuery;
  73. function not( selector : String ) : JQuery;
  74. function prev( ?selector : String ) : JQuery;
  75. function prevAll( ?selector : String ) : JQuery;
  76. function prevUntil( ?selector : String ) : JQuery;
  77. function pushStack( elements : Array<Dom.HtmlDom> ) : JQuery;
  78. function siblings( ?selector : String ) : JQuery;
  79. function size() : Int;
  80. function slice( start : Int, ?end : Int ) : JQuery;
  81. function toArray() : Array<Dom.HtmlDom>;
  82. // DOM changes
  83. @:multitype function before( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  84. @:multitype function after( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  85. @:multitype function append( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  86. @:multitype function appendTo( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  87. function detach( ?selector : String ) : JQuery;
  88. function empty() : JQuery; // remove all texts
  89. @:multitype function insertBefore( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  90. @:multitype function insertAfter( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  91. @:multitype function prepend( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  92. @:multitype function prependTo( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  93. function remove( ?selector : String ) : JQuery;
  94. function replaceAll( selector : String ) : JQuery;
  95. @:multitype function replaceWith( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  96. function unwrap() : JQuery;
  97. @:multitype function wrap( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  98. @:multitype function wrapAll( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  99. @:multitype function wrapInner( ?html : String, ?j : JQuery, ?h : Dom.HtmlDom ) : JQuery;
  100. // animation
  101. function animate( properties : { }, ?duration : Int, ?callb : Void -> Void ) : JQuery;
  102. function delay( duration : Int, ?queueName : String ) : JQuery;
  103. function hide( ?duration : Int, ?call : Void -> Void ) : JQuery;
  104. function fadeIn( ?duration : Int, ?call : Void -> Void ) : JQuery;
  105. function fadeOut( ?duration : Int, ?call : Void -> Void ) : JQuery;
  106. function fadeTo( duration : Int, opacity : Float, ?call : Void -> Void ) : JQuery;
  107. function fadeToggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  108. function show( ?duration : Int, ?call : Void -> Void ) : JQuery;
  109. function sliceDown( ?duration : Int, ?call : Void -> Void ) : JQuery;
  110. function sliceToggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  111. function sliceUp( ?duration : Int, ?call : Void -> Void ) : JQuery;
  112. function stop( ?clearQueue : Bool, ?jumpToEnd : Bool ) : JQuery;
  113. function toggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  114. // Events
  115. function blur( ?callb : JqEvent -> Void ) : JQuery;
  116. function change( ?callb : JqEvent -> Void ) : JQuery;
  117. function click( ?callb : JqEvent -> Void ) : JQuery;
  118. function dblclick( ?callb : JqEvent -> Void ) : JQuery;
  119. function error( ?callb : JqEvent -> Void ) : JQuery;
  120. function focus( ?callb : JqEvent -> Void ) : JQuery;
  121. function focusin( ?callb : JqEvent -> Void ) : JQuery;
  122. function focusout( ?callb : JqEvent -> Void ) : JQuery;
  123. function hover( onOver : JqEvent -> Void, ?onOut : Void -> Void ) : JQuery;
  124. function keydown( ?callb : JqEvent -> Void ) : JQuery;
  125. function keypress( ?callb : JqEvent -> Void ) : JQuery;
  126. function keyup( ?callb : JqEvent -> Void ) : JQuery;
  127. function mousedown( ?callb : JqEvent -> Void ) : JQuery;
  128. function mouseenter( ?callb : JqEvent -> Void ) : JQuery;
  129. function mouseleave( ?callb : JqEvent -> Void ) : JQuery;
  130. function mouseout( ?callb : JqEvent -> Void ) : JQuery;
  131. function mouseover( ?callb : JqEvent -> Void ) : JQuery;
  132. function mouseup( ?callb : JqEvent -> Void ) : JQuery;
  133. function load( ?callb : JqEvent -> Void ) : JQuery;
  134. function ready( callb : JqEvent -> Void ) : JQuery;
  135. function resize( ?callb : JqEvent -> Void ) : JQuery;
  136. function scroll( ?callb : JqEvent -> Void ) : JQuery;
  137. function select( ?callb : JqEvent -> Void ) : JQuery;
  138. function submit( ?callb : JqEvent -> Void ) : JQuery;
  139. function unload( ?callb : JqEvent -> Void ) : JQuery;
  140. function bind( events : String, callb : JqEvent -> Void ) : JQuery;
  141. function delegate( selector : String, events : String, callb : JqEvent -> Void ) : JQuery;
  142. function die( ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  143. function one( events : String, callb : JqEvent -> Void ) : JQuery;
  144. function live( events : String, callb : JqEvent -> Void ) : JQuery;
  145. function trigger( events : String ) : JQuery;
  146. function triggerHandler( events : String ) : JQuery;
  147. function unbind( ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  148. function undelegate( ?selector : String, ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  149. // queue
  150. function clearQueue( ?queueName : String ) : JQuery;
  151. function dequeue( ?queueName : String ) : JQuery;
  152. function queue( ?queueName : String ) : { length : Int };
  153. // ajax
  154. // TODO
  155. // deferred
  156. // TODO
  157. // other tools
  158. function get() : Array<Dom.HtmlDom>;
  159. function is( selector : String ) : Bool;
  160. function data<T>( key : String, ?value : T ) : T;
  161. function removeData( ?key : String ) : JQuery;
  162. function serialize() : String;
  163. function serializeArray() : Array<{ name : String, value : String }>;
  164. //inline function map<T>( f : JQuery -> T ) : Array<T> {
  165. // return untyped this["map"](function() return f(cur)).get();
  166. //}
  167. // haxe-additions
  168. inline function noBubble( events : String ) : JQuery { return (cast this).bind(events, false); }
  169. inline function loadURL( url : String, ?callb : Void -> Void ) : JQuery { return (cast this).load(url,callb); }
  170. inline function toggleClick( ?first : Void -> Void, ?second : Void -> Void ) : JQuery { return (cast this).toggle(first, second); }
  171. inline static function of( d : Dom.HtmlDom ) : JQuery { return new js.JQuery(cast d); }
  172. /**
  173. Return the current JQuery element (in a callback), similar to $(this) in JS.
  174. **/
  175. static var cur(getCurrent, null) : JQuery;
  176. static var fx(default, null) : { off : Bool, interval : Int };
  177. static var browser(default, null) : { webkit : Bool, opera : Bool, msie : Bool, mozilla : Bool, version : String };
  178. static function contains( parent : Dom.HtmlDom, child : Dom.HtmlDom ) : Bool;
  179. static function noConflict( ?removeAll : Bool ) : Void;
  180. static function parseJSON( json : String ) : Dynamic;
  181. //static function parseXML
  182. //static function get, post
  183. //static function getJSON, getScript, globalEval, grep
  184. //static function is*, makeArray, map, merge, noop, now, param, proxy, sub, trim, type, unique
  185. private static inline function getCurrent() : JQuery {
  186. return untyped __js__("$(this)");
  187. }
  188. private static function __init__() : Void untyped {
  189. #if !noEmbedJS
  190. haxe.macro.Tools.includeFile("js/jquery-1.5.min.js");
  191. #end
  192. var q : Dynamic = window.jQuery;
  193. js.JQuery = q;
  194. q.fn.noBubble = q.fn.bind;
  195. q.fn.loadURL = q.fn.load;
  196. q.fn.toggleClick = q.fn.toggle;
  197. q.of = q;
  198. }
  199. }