JQuery.hx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 isPropagationStopped() : Bool;
  51. function preventDefault() : Void;
  52. function stopImmediatePropagation() : Void;
  53. function stopPropagation() : Void;
  54. }
  55. extern class JQueryHelper {
  56. @:overload(function(j:JQuery):JQuery{})
  57. @:overload(function(j:DOMWindow):JQuery{})
  58. @:overload(function(j:Element):JQuery{})
  59. public static inline function J( html : String ) : JQuery {
  60. return new JQuery(html);
  61. }
  62. public static var JTHIS(get, null) : JQuery;
  63. static inline function get_JTHIS() : JQuery {
  64. return untyped __js__("$(this)");
  65. }
  66. }
  67. @:initPackage
  68. extern class JQuery implements ArrayAccess<Element> {
  69. var context(default,null) : Element;
  70. var length(default, null) : Int;
  71. @:overload(function(j:JQuery):Void{})
  72. @:overload(function(j:DOMWindow):Void{})
  73. @:overload(function(j:Element):Void{})
  74. function new( html : String ) : Void;
  75. // attributes
  76. function addClass( className : String ) : JQuery;
  77. function removeClass( ?className : String ) : JQuery;
  78. function hasClass( className : String ) : Bool;
  79. function toggleClass( className : String, ?addRemove : Bool ) : JQuery;
  80. @:overload(function(name:String,value:String):JQuery{})
  81. function attr( name : String ) : String;
  82. function removeAttr( attr : String ) : JQuery;
  83. @:overload(function(name:String,value:Dynamic):JQuery{})
  84. function prop( name : String ) : Dynamic;
  85. @:overload(function(prop:String,value:String):JQuery{})
  86. @:overload(function(map:{}):JQuery{})
  87. function css( prop : String ) : String;
  88. @:overload(function(html:String):JQuery{})
  89. @:overload(function(html:JQuery):JQuery{})
  90. function html() : String;
  91. @:overload(function(value:String):JQuery{})
  92. function val() : String;
  93. @:overload(function(text:String):JQuery{})
  94. function text() : String;
  95. // Size & Position
  96. @:overload(function(value:Int):JQuery{})
  97. function width() : Int;
  98. @:overload(function(value:Int):JQuery{})
  99. function height() : Int;
  100. @:overload(function(value:Int):JQuery{})
  101. function innerWidth() : Int;
  102. @:overload(function(value:Int):JQuery{})
  103. function innerHeight() : Int;
  104. function outerWidth( ?includeMargin : Bool ) : Int;
  105. function outerHeight( ?includeMargin : Bool ) : Int;
  106. @:overload(function(value:Int):JQuery{})
  107. function scrollLeft() : Int;
  108. @:overload(function(value:Int):JQuery{})
  109. function scrollTop() : Int;
  110. @:overload(function(value: { left : Int, top : Int }):JQuery{})
  111. function offset() : { left : Int, top : Int };
  112. function offsetParent() : JQuery;
  113. @:overload(function(value: { left : Int, top : Int }):JQuery{})
  114. function position() : { left : Int, top : Int };
  115. // current group manipulation
  116. @:overload(function(value:JQuery):JQuery{})
  117. @:overload(function(value:Element):JQuery{})
  118. @:overload(function(value:Array<Element>):JQuery{})
  119. function add( selector : String, ?context : JQuery ) : JQuery;
  120. function andSelf() : JQuery;
  121. function children( ?selector : String ) : JQuery;
  122. function clone( ?withDataAndEvents : Bool ) : JQuery;
  123. function closest( selector : String, ?context : JQuery ) : JQuery;
  124. function contents() : JQuery;
  125. @:overload(function( f : Int -> Element -> Void ):JQuery{})
  126. function each( f : Void -> Void ) : JQuery;
  127. function end() : JQuery;
  128. function eq( index : Int ) : JQuery;
  129. function filter( selector : String ) : JQuery;
  130. function find( selector : String ) : JQuery;
  131. function first() : JQuery;
  132. function index( ?selector : String ) : Int;
  133. function last( ?selector : String ) : JQuery;
  134. function has( selector : String ) : JQuery;
  135. function next( ?selector : String ) : JQuery;
  136. function nextAll( ?selector : String ) : JQuery;
  137. function nextUntil( ?selector : String ) : JQuery;
  138. function parent( ?selector : String ) : JQuery;
  139. function parents( ?selector : String ) : JQuery;
  140. function parentsUntil( ?selector : String ) : JQuery;
  141. @:overload(function(value:Element):JQuery{})
  142. function not( selector : String ) : JQuery;
  143. function prev( ?selector : String ) : JQuery;
  144. function prevAll( ?selector : String ) : JQuery;
  145. function prevUntil( ?selector : String ) : JQuery;
  146. function pushStack( elements : Array<Element> ) : JQuery;
  147. function siblings( ?selector : String ) : JQuery;
  148. function size() : Int;
  149. function slice( start : Int, ?end : Int ) : JQuery;
  150. function toArray() : Array<Element>;
  151. // DOM changes
  152. @:overload(function(value:JQuery):JQuery{})
  153. @:overload(function(value:Element):JQuery{})
  154. function before( html : String ) : JQuery;
  155. @:overload(function(value:JQuery):JQuery{})
  156. @:overload(function(value:Element):JQuery{})
  157. function after( html : String ) : JQuery;
  158. @:overload(function(value:JQuery):JQuery{})
  159. @:overload(function(value:Element):JQuery{})
  160. function append( html : String ) : JQuery;
  161. @:overload(function(value:JQuery):JQuery{})
  162. @:overload(function(value:Element):JQuery{})
  163. function appendTo( html : String ) : JQuery;
  164. function detach( ?selector : String ) : JQuery;
  165. function empty() : JQuery; // remove all texts
  166. @:overload(function(value:JQuery):JQuery{})
  167. @:overload(function(value:Element):JQuery{})
  168. function insertBefore( html : String ) : JQuery;
  169. @:overload(function(value:JQuery):JQuery{})
  170. @:overload(function(value:Element):JQuery{})
  171. function insertAfter( html : String ) : JQuery;
  172. @:overload(function(value:JQuery):JQuery{})
  173. @:overload(function(value:Element):JQuery{})
  174. function prepend( html : String ) : JQuery;
  175. @:overload(function(value:JQuery):JQuery{})
  176. @:overload(function(value:Element):JQuery{})
  177. function prependTo( html : String ) : JQuery;
  178. function remove( ?selector : String ) : JQuery;
  179. function replaceAll( selector : String ) : JQuery;
  180. @:overload(function(value:JQuery):JQuery{})
  181. @:overload(function(value:Element):JQuery{})
  182. function replaceWith( html : String ) : JQuery;
  183. function unwrap() : JQuery;
  184. @:overload(function(value:JQuery):JQuery{})
  185. @:overload(function(value:Element):JQuery{})
  186. function wrap( html : String ) : JQuery;
  187. @:overload(function(value:JQuery):JQuery{})
  188. @:overload(function(value:Element):JQuery{})
  189. function wrapAll( html : String ) : JQuery;
  190. @:overload(function(value:JQuery):JQuery{})
  191. @:overload(function(value:Element):JQuery{})
  192. function wrapInner( html : String ) : JQuery;
  193. // animation
  194. @:overload(function(properties:{},?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  195. function animate( properties : { }, ?duration : Int, ?callb : Void -> Void ) : JQuery;
  196. function delay( duration : Int, ?queueName : String ) : JQuery;
  197. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  198. function hide( ?duration : Int, ?call : Void -> Void ) : JQuery;
  199. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  200. function fadeIn( ?duration : Int, ?call : Void -> Void ) : JQuery;
  201. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  202. function fadeOut( ?duration : Int, ?call : Void -> Void ) : JQuery;
  203. @:overload(function(duration:Int,opacity:Float,?easing:String,?call:Void->Void) : JQuery{})
  204. function fadeTo( duration : Int, opacity : Float, ?call : Void -> Void ) : JQuery;
  205. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  206. function fadeToggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  207. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  208. function show( ?duration : Int, ?call : Void -> Void ) : JQuery;
  209. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  210. function slideDown( ?duration : Int, ?call : Void -> Void ) : JQuery;
  211. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  212. function slideToggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  213. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  214. function slideUp( ?duration : Int, ?call : Void -> Void ) : JQuery;
  215. function stop( ?clearQueue : Bool, ?jumpToEnd : Bool ) : JQuery;
  216. @:overload(function(?duration:Int,?easing:String,?call:Void->Void) : JQuery{})
  217. function toggle( ?duration : Int, ?call : Void -> Void ) : JQuery;
  218. // Events
  219. function blur( ?callb : JqEvent -> Void ) : JQuery;
  220. function change( ?callb : JqEvent -> Void ) : JQuery;
  221. @:overload(function(callb:Void->Void):JQuery { } )
  222. @:overload(function(callb:JQuery.JqEvent->Void):JQuery{})
  223. @:overload(function(callb:Void->Bool):JQuery{})
  224. function click( ?callb : JqEvent -> Void ) : JQuery;
  225. function dblclick( ?callb : JqEvent -> Void ) : JQuery;
  226. function error( ?callb : JqEvent -> Void ) : JQuery;
  227. function focus( ?callb : JqEvent -> Void ) : JQuery;
  228. function focusin( ?callb : JqEvent -> Void ) : JQuery;
  229. function focusout( ?callb : JqEvent -> Void ) : JQuery;
  230. @:overload(function(onInOut:JqEvent->Void):JQuery{})
  231. function hover( onIn : JqEvent -> Void, ?onOut : JqEvent -> Void ) : JQuery;
  232. @:overload(function( callb : JQuery.JqEvent -> Bool ) : JQuery {})
  233. function keydown( ?callb : JqEvent -> Void ) : JQuery;
  234. @:overload(function( callb : JQuery.JqEvent -> Bool ) : JQuery {})
  235. function keypress( ?callb : JqEvent -> Void ) : JQuery;
  236. @:overload(function( callb : JQuery.JqEvent -> Bool ) : JQuery {})
  237. function keyup( ?callb : JqEvent -> Void ) : JQuery;
  238. function mousedown( ?callb : JqEvent -> Void ) : JQuery;
  239. function mouseenter( ?callb : JqEvent -> Void ) : JQuery;
  240. function mouseleave( ?callb : JqEvent -> Void ) : JQuery;
  241. function mouseout( ?callb : JqEvent -> Void ) : JQuery;
  242. function mouseover( ?callb : JqEvent -> Void ) : JQuery;
  243. function mousemove( ?callb : JqEvent -> Void ) : JQuery;
  244. function mouseup( ?callb : JqEvent -> Void ) : JQuery;
  245. // AJAX overloads
  246. @:overload(function( url:String, ?data : {}, ?callb : String -> String -> Void ) : JQuery {})
  247. @:overload(function( url:String, ?data : {}, ?callb : String -> Void ) : JQuery {})
  248. @:overload(function( url:String, ?data : {}, ?callb : Void -> Void ) : JQuery {})
  249. function load( ?callb : JqEvent -> Void ) : JQuery;
  250. function ready( callb : JqEvent -> Void ) : JQuery;
  251. function resize( ?callb : JqEvent -> Void ) : JQuery;
  252. function scroll( ?callb : JqEvent -> Void ) : JQuery;
  253. function select( ?callb : JqEvent -> Void ) : JQuery;
  254. function submit( ?callb : JqEvent -> Void ) : JQuery;
  255. function unload( ?callb : JqEvent -> Void ) : JQuery;
  256. function bind( events : String, callb : JqEvent -> Void ) : JQuery;
  257. function delegate( selector : String, events : String, callb : JqEvent -> Void ) : JQuery;
  258. function die( ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  259. function one( events : String, callb : JqEvent -> Void ) : JQuery;
  260. function live( events : String, callb : JqEvent -> Void ) : JQuery;
  261. function trigger( events : String ) : JQuery;
  262. function triggerHandler( events : String ) : JQuery;
  263. function unbind( ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  264. function undelegate( ?selector : String, ?events : String, ?callb : JqEvent -> Void ) : JQuery;
  265. // JQuery 1.7+
  266. @:overload(function(events:Dynamic<JqEvent->Void>):JQuery{})
  267. function on( events : String, callb : JqEvent -> Void ) : JQuery;
  268. // queue
  269. function clearQueue( ?queueName : String ) : JQuery;
  270. function dequeue( ?queueName : String ) : JQuery;
  271. function queue( ?queueName : String, ?callb : (Void -> Void) -> Void ) : { length : Int };
  272. // ajax
  273. // TODO
  274. // deferred
  275. // TODO
  276. // other tools
  277. @:overload(function(index:Int):Element{})
  278. function get() : Array<Element>;
  279. @:overload(function(j:JQuery):Bool{})
  280. function is( selector : String ) : Bool;
  281. @:overload(function() : Dynamic {})
  282. @:overload(function( key : String ) : Dynamic {})
  283. function data( key : String, value : Dynamic ) : JQuery;
  284. function removeData( ?key : String ) : JQuery;
  285. function serialize() : String;
  286. function serializeArray() : Array<{ name : String, value : String }>;
  287. //inline function map<T>( f : JQuery -> T ) : Array<T> {
  288. // return untyped this["map"](function() return f(cur)).get();
  289. //}
  290. // Haxe addition
  291. @:runtime inline function iterator() : Iterator<JQuery> {
  292. return untyped __define_feature__('js.JQuery.iterator', this["iterator"])();
  293. }
  294. /**
  295. Return the current JQuery element (in a callback), similar to $(this) in JS.
  296. **/
  297. static var cur(get, null) : JQuery;
  298. static var fx(default, null) : { off : Bool, interval : Int };
  299. static var browser(default, null) : { webkit : Bool, opera : Bool, msie : Bool, mozilla : Bool, version : String };
  300. static function contains( parent : Element, child : Element ) : Bool;
  301. static function noConflict( ?removeAll : Bool ) : Void;
  302. static function parseJSON( json : String ) : Dynamic;
  303. static function globalEval( js : String ) : Void;
  304. //static function parseXML
  305. //static function get, post
  306. //static function getJSON, getScript, grep
  307. //static function is*, makeArray, map, merge, noop, now, param, proxy, sub, trim, type, unique
  308. private static inline function get_cur() : JQuery {
  309. return untyped $(__js__("this"));
  310. }
  311. private static function __init__() : Void untyped {
  312. #if embed_js
  313. if( untyped __js__("typeof($) == 'undefined'") )
  314. haxe.macro.Compiler.includeFile("js/jquery-latest.min.js");
  315. #end
  316. var q : Dynamic = (untyped js.Browser.window).jQuery;
  317. js.JQuery = q;
  318. __feature__('js.JQuery.iterator',
  319. q.fn.iterator = function() return { pos : 0, j : __this__, hasNext : function() return __this__.pos < __this__.j.length, next : function() return $(__this__.j[__this__.pos++]) }
  320. );
  321. }
  322. }