JQuery.hx 15 KB

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