JQuery.hx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js;
  23. import js.html.DOMWindow;
  24. import js.html.Element;
  25. typedef JqEvent = {
  26. var target : Element;
  27. var currentTarget : Element;
  28. var relatedTarget : Element;
  29. var delegateTarget : Element;
  30. var type : String;
  31. var timeStamp : Int;
  32. //var data : Dynamic;
  33. //var namespace : String;
  34. //var result : Dynamic;
  35. // position
  36. var pageX : Int;
  37. var pageY : Int;
  38. var wheelDelta : Int;
  39. // keys
  40. var keyCode : Int;
  41. var charCode : Int;
  42. var shiftKey : Bool;
  43. var ctrlKey : Bool;
  44. var altKey : Bool;
  45. var metaKey : Bool;
  46. var which : Int;
  47. // propagation
  48. function isDefaultPrevented() : Bool;
  49. function isImmediatePropagationStopped() : Bool;
  50. function isPropationStopped() : Bool;
  51. function preventDefault() : Void;
  52. function stopImmediatePropagation() : Void;
  53. function stopPropagation() : Void;
  54. }
  55. @:initPackage
  56. extern class JQuery implements ArrayAccess<Element> {
  57. var context(default,null) : Element;
  58. var length(default, null) : Int;
  59. @:overload(function(j:JQuery):Void{})
  60. @:overload(function(j:DOMWindow):Void{})
  61. @:overload(function(j:Element):Void{})
  62. function new( html : String ) : Void;
  63. // attributes
  64. function addClass( className : String ) : JQuery;
  65. function removeClass( ?className : String ) : JQuery;
  66. function hasClass( className : String ) : Bool;
  67. function toggleClass( className : String, ?addRemove : Bool ) : JQuery;
  68. @:overload(function(name:String,value:String):JQuery{})
  69. function attr( name : String ) : String;
  70. function removeAttr( attr : String ) : JQuery;
  71. @:overload(function(name:String,value:Dynamic):JQuery{})
  72. function prop( name : String ) : Dynamic;
  73. @:overload(function(prop:String,value:String):JQuery{})
  74. @:overload(function(map:{}):JQuery{})
  75. function css( prop : String ) : String;
  76. @:overload(function(html:String):JQuery{})
  77. @:overload(function(html:JQuery):JQuery{})
  78. function html() : String;
  79. @:overload(function(value:String):JQuery{})
  80. function val() : String;
  81. @:overload(function(text:String):JQuery{})
  82. function text() : String;
  83. // Size & Position
  84. @:overload(function(value:Int):JQuery{})
  85. function width() : Int;
  86. @:overload(function(value:Int):JQuery{})
  87. function height() : Int;
  88. @:overload(function(value:Int):JQuery{})
  89. function innerWidth() : Int;
  90. @:overload(function(value:Int):JQuery{})
  91. function innerHeight() : Int;
  92. function outerWidth( ?includeMargin : Bool ) : Int;
  93. function outerHeight( ?includeMargin : Bool ) : Int;
  94. @:overload(function(value:Int):JQuery{})
  95. function scrollLeft() : Int;
  96. @:overload(function(value:Int):JQuery{})
  97. function scrollTop() : Int;
  98. @:overload(function(value: { left : Int, top : Int }):JQuery{})
  99. function offset() : { left : Int, top : Int };
  100. function offsetParent() : JQuery;
  101. @:overload(function(value: { left : Int, top : Int }):JQuery{})
  102. function position() : { left : Int, top : Int };
  103. // current group manipulation
  104. @:overload(function(value:JQuery):JQuery{})
  105. @:overload(function(value:Element):JQuery{})
  106. @:overload(function(value:Array<Element>):JQuery{})
  107. function add( selector : String, ?context : JQuery ) : JQuery;
  108. function andSelf() : JQuery;
  109. function children( ?selector : String ) : JQuery;
  110. function clone( ?withDataAndEvents : Bool ) : JQuery;
  111. function closest( selector : String, ?context : JQuery ) : JQuery;
  112. function contents() : JQuery;
  113. @:overload(function( f : Int -> Element -> Void ):JQuery{})
  114. function each( f : Void -> Void ) : JQuery;
  115. function end() : JQuery;
  116. function eq( index : Int ) : JQuery;
  117. function filter( selector : String ) : JQuery;
  118. function find( selector : String ) : JQuery;
  119. function first() : JQuery;
  120. function index( ?selector : String ) : Int;
  121. function last( ?selector : String ) : JQuery;
  122. function has( selector : String ) : JQuery;
  123. function next( ?selector : String ) : JQuery;
  124. function nextAll( ?selector : String ) : JQuery;
  125. function nextUntil( ?selector : String ) : JQuery;
  126. function parent( ?selector : String ) : JQuery;
  127. function parents( ?selector : String ) : JQuery;
  128. function parentsUntil( ?selector : String ) : JQuery;
  129. @:overload(function(value:Element):JQuery{})
  130. function not( selector : String ) : JQuery;
  131. function prev( ?selector : String ) : JQuery;
  132. function prevAll( ?selector : String ) : JQuery;
  133. function prevUntil( ?selector : String ) : JQuery;
  134. function pushStack( elements : Array<Element> ) : JQuery;
  135. function siblings( ?selector : String ) : JQuery;
  136. function size() : Int;
  137. function slice( start : Int, ?end : Int ) : JQuery;
  138. function toArray() : Array<Element>;
  139. // DOM changes
  140. @:overload(function(value:JQuery):JQuery{})
  141. @:overload(function(value:Element):JQuery{})
  142. function before( html : String ) : JQuery;
  143. @:overload(function(value:JQuery):JQuery{})
  144. @:overload(function(value:Element):JQuery{})
  145. function after( html : String ) : JQuery;
  146. @:overload(function(value:JQuery):JQuery{})
  147. @:overload(function(value:Element):JQuery{})
  148. function append( html : String ) : JQuery;
  149. @:overload(function(value:JQuery):JQuery{})
  150. @:overload(function(value:Element):JQuery{})
  151. function appendTo( html : String ) : JQuery;
  152. function detach( ?selector : String ) : JQuery;
  153. function empty() : JQuery; // remove all texts
  154. @:overload(function(value:JQuery):JQuery{})
  155. @:overload(function(value:Element):JQuery{})
  156. function insertBefore( html : String ) : JQuery;
  157. @:overload(function(value:JQuery):JQuery{})
  158. @:overload(function(value:Element):JQuery{})
  159. function insertAfter( html : String ) : JQuery;
  160. @:overload(function(value:JQuery):JQuery{})
  161. @:overload(function(value:Element):JQuery{})
  162. function prepend( html : String ) : JQuery;
  163. @:overload(function(value:JQuery):JQuery{})
  164. @:overload(function(value:Element):JQuery{})
  165. function prependTo( html : String ) : JQuery;
  166. function remove( ?selector : String ) : JQuery;
  167. function replaceAll( selector : String ) : JQuery;
  168. @:overload(function(value:JQuery):JQuery{})
  169. @:overload(function(value:Element):JQuery{})
  170. function replaceWith( html : String ) : JQuery;
  171. function unwrap() : JQuery;
  172. @:overload(function(value:JQuery):JQuery{})
  173. @:overload(function(value:Element):JQuery{})
  174. function wrap( html : String ) : JQuery;
  175. @:overload(function(value:JQuery):JQuery{})
  176. @:overload(function(value:Element):JQuery{})
  177. function wrapAll( html : String ) : JQuery;
  178. @:overload(function(value:JQuery):JQuery{})
  179. @:overload(function(value:Element):JQuery{})
  180. function wrapInner( html : String ) : JQuery;
  181. // animation
  182. @:overload(function(properties:{},?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  183. function animate( properties : { }, ?duration : Int, ?callb : Void -> Void ) : JQuery;
  184. function delay( duration : Int, ?queueName : String ) : JQuery;
  185. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  186. function hide( ?duration : Int, ?call : Void -> Void ) : JQuery;
  187. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  188. function fadeIn( ?duration : Int, ?call : Void -> Void ) : JQuery;
  189. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  190. function fadeOut( ?duration : Int, ?call : Void -> Void ) : JQuery;
  191. @:overload(function(duration:Int,opacity:Float,?easing:String,?call:Void->Void) : JQuery{})
  192. function fadeTo( duration : Int, opacity : Float, ?call : Void -> Void ) : JQuery;
  193. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  194. function fadeToggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  195. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  196. function show( ?duration : Int, ?call : Void -> Void ) : JQuery;
  197. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  198. function slideDown( ?duration : Int, ?call : Void -> Void ) : JQuery;
  199. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  200. function slideToggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  201. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  202. function slideUp( ?duration : Int, ?call : Void -> Void ) : JQuery;
  203. function stop( ?clearQueue : Bool, ?jumpToEnd : Bool ) : JQuery;
  204. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  205. function toggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  206. // Events
  207. function blur( ?callb : JqEvent -> Void ) : JQuery;
  208. function change( ?callb : JqEvent -> Void ) : JQuery;
  209. @:overload(function(callb:Void->Void):JQuery { } )
  210. @:overload(function(callb:JQuery.JqEvent->Void):JQuery{})
  211. @:overload(function(callb:Void->Bool):JQuery{})
  212. function click( ?callb : JqEvent -> Void ) : JQuery;
  213. function dblclick( ?callb : JqEvent -> Void ) : JQuery;
  214. function error( ?callb : JqEvent -> Void ) : JQuery;
  215. function focus( ?callb : JqEvent -> Void ) : JQuery;
  216. function focusin( ?callb : JqEvent -> Void ) : JQuery;
  217. function focusout( ?callb : JqEvent -> Void ) : JQuery;
  218. function hover( onOver : JqEvent -> Void, ?onOut : Void -> Void ) : JQuery;
  219. @:overload(function( callb : JQuery.JqEvent -> Bool ) : JQuery {})
  220. function keydown( ?callb : JqEvent -> Void ) : JQuery;
  221. @:overload(function( callb : JQuery.JqEvent -> Bool ) : JQuery {})
  222. function keypress( ?callb : JqEvent -> Void ) : JQuery;
  223. @:overload(function( callb : JQuery.JqEvent -> Bool ) : JQuery {})
  224. function keyup( ?callb : JqEvent -> Void ) : JQuery;
  225. function mousedown( ?callb : JqEvent -> Void ) : JQuery;
  226. function mouseenter( ?callb : JqEvent -> Void ) : JQuery;
  227. function mouseleave( ?callb : JqEvent -> Void ) : JQuery;
  228. function mouseout( ?callb : JqEvent -> Void ) : JQuery;
  229. function mouseover( ?callb : JqEvent -> Void ) : JQuery;
  230. function mousemove( ?callb : JqEvent -> Void ) : JQuery;
  231. function mouseup( ?callb : JqEvent -> Void ) : JQuery;
  232. // AJAX overloads
  233. @:overload(function( url:String, ?data : {}, ?callb : String -> String -> Void ) : JQuery {})
  234. @:overload(function( url:String, ?data : {}, ?callb : String -> Void ) : JQuery {})
  235. @:overload(function( url:String, ?data : {}, ?callb : Void -> Void ) : JQuery {})
  236. function load( ?callb : JqEvent -> Void ) : JQuery;
  237. function ready( callb : JqEvent -> Void ) : JQuery;
  238. function resize( ?callb : JqEvent -> Void ) : JQuery;
  239. function scroll( ?callb : JqEvent -> Void ) : JQuery;
  240. function select( ?callb : JqEvent -> Void ) : JQuery;
  241. function submit( ?callb : JqEvent -> Void ) : JQuery;
  242. function unload( ?callb : JqEvent -> Void ) : JQuery;
  243. function bind( events : String, callb : JqEvent -> Void ) : JQuery;
  244. function delegate( selector : String, events : String, callb : JqEvent -> Void ) : JQuery;
  245. function die( ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  246. function one( events : String, callb : JqEvent -> Void ) : JQuery;
  247. function live( events : String, callb : JqEvent -> Void ) : JQuery;
  248. function trigger( events : String ) : JQuery;
  249. function triggerHandler( events : String ) : JQuery;
  250. function unbind( ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  251. function undelegate( ?selector : String, ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  252. // queue
  253. function clearQueue( ?queueName : String ) : JQuery;
  254. function dequeue( ?queueName : String ) : JQuery;
  255. function queue( ?queueName : String, ?callb : (Void -> Void) -> Void ) : { length : Int };
  256. // ajax
  257. // TODO
  258. // deferred
  259. // TODO
  260. // other tools
  261. @:overload(function(index:Int):Element{})
  262. function get() : Array<Element>;
  263. @:overload(function(j:JQuery):Bool{})
  264. function is( selector : String ) : Bool;
  265. function data<T>( key : String, ?value : T ) : T;
  266. function removeData( ?key : String ) : JQuery;
  267. function serialize() : String;
  268. function serializeArray() : Array<{ name : String, value : String }>;
  269. //inline function map<T>( f : JQuery -> T ) : Array<T> {
  270. // return untyped this["map"](function() return f(cur)).get();
  271. //}
  272. // haXe addition
  273. @:runtime inline function iterator() : Iterator<JQuery> {
  274. return untyped __define_feature__('js.JQuery.iterator', this["iterator"])();
  275. }
  276. /**
  277. Return the current JQuery element (in a callback), similar to $(this) in JS.
  278. **/
  279. static var cur(get, null) : JQuery;
  280. static var fx(default, null) : { off : Bool, interval : Int };
  281. static var browser(default, null) : { webkit : Bool, opera : Bool, msie : Bool, mozilla : Bool, version : String };
  282. static function contains( parent : Element, child : Element ) : Bool;
  283. static function noConflict( ?removeAll : Bool ) : Void;
  284. static function parseJSON( json : String ) : Dynamic;
  285. static function globalEval( js : String ) : Void;
  286. //static function parseXML
  287. //static function get, post
  288. //static function getJSON, getScript, grep
  289. //static function is*, makeArray, map, merge, noop, now, param, proxy, sub, trim, type, unique
  290. private static inline function get_cur() : JQuery {
  291. return untyped __js__("$(this)");
  292. }
  293. private static function __init__() : Void untyped {
  294. #if embed_js
  295. if( untyped __js__("typeof($) == 'undefined'") )
  296. haxe.macro.Compiler.includeFile("js/jquery-latest.min.js");
  297. #end
  298. var q : Dynamic = window.jQuery;
  299. js.JQuery = q;
  300. __feature__('js.JQuery.iterator',
  301. q.fn.iterator = function() return { pos : 0, j : __this__, hasNext : function() return __this__.pos < __this__.j.length, next : function() return $(__this__.j[__this__.pos++]) }
  302. );
  303. }
  304. }