event.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. define( [
  2. "./core",
  3. "./var/document",
  4. "./var/documentElement",
  5. "./var/isFunction",
  6. "./var/rnothtmlwhite",
  7. "./var/rcheckableType",
  8. "./var/slice",
  9. "./data/var/acceptData",
  10. "./data/var/dataPriv",
  11. "./core/nodeName",
  12. "./core/init",
  13. "./selector"
  14. ], function( jQuery, document, documentElement, isFunction, rnothtmlwhite,
  15. rcheckableType, slice, acceptData, dataPriv, nodeName ) {
  16. "use strict";
  17. var rtypenamespace = /^([^.]*)(?:\.(.+)|)/;
  18. function returnTrue() {
  19. return true;
  20. }
  21. function returnFalse() {
  22. return false;
  23. }
  24. // Support: IE <=9 - 11+
  25. // focus() and blur() are asynchronous, except when they are no-op.
  26. // So expect focus to be synchronous when the element is already active,
  27. // and blur to be synchronous when the element is not already active.
  28. // (focus and blur are always synchronous in other supported browsers,
  29. // this just defines when we can count on it).
  30. function expectSync( elem, type ) {
  31. return ( elem === safeActiveElement() ) === ( type === "focus" );
  32. }
  33. // Support: IE <=9 only
  34. // Accessing document.activeElement can throw unexpectedly
  35. // https://bugs.jquery.com/ticket/13393
  36. function safeActiveElement() {
  37. try {
  38. return document.activeElement;
  39. } catch ( err ) { }
  40. }
  41. function on( elem, types, selector, data, fn, one ) {
  42. var origFn, type;
  43. // Types can be a map of types/handlers
  44. if ( typeof types === "object" ) {
  45. // ( types-Object, selector, data )
  46. if ( typeof selector !== "string" ) {
  47. // ( types-Object, data )
  48. data = data || selector;
  49. selector = undefined;
  50. }
  51. for ( type in types ) {
  52. on( elem, type, selector, data, types[ type ], one );
  53. }
  54. return elem;
  55. }
  56. if ( data == null && fn == null ) {
  57. // ( types, fn )
  58. fn = selector;
  59. data = selector = undefined;
  60. } else if ( fn == null ) {
  61. if ( typeof selector === "string" ) {
  62. // ( types, selector, fn )
  63. fn = data;
  64. data = undefined;
  65. } else {
  66. // ( types, data, fn )
  67. fn = data;
  68. data = selector;
  69. selector = undefined;
  70. }
  71. }
  72. if ( fn === false ) {
  73. fn = returnFalse;
  74. } else if ( !fn ) {
  75. return elem;
  76. }
  77. if ( one === 1 ) {
  78. origFn = fn;
  79. fn = function( event ) {
  80. // Can use an empty set, since event contains the info
  81. jQuery().off( event );
  82. return origFn.apply( this, arguments );
  83. };
  84. // Use same guid so caller can remove using origFn
  85. fn.guid = origFn.guid || ( origFn.guid = jQuery.guid++ );
  86. }
  87. return elem.each( function() {
  88. jQuery.event.add( this, types, fn, data, selector );
  89. } );
  90. }
  91. /*
  92. * Helper functions for managing events -- not part of the public interface.
  93. * Props to Dean Edwards' addEvent library for many of the ideas.
  94. */
  95. jQuery.event = {
  96. global: {},
  97. add: function( elem, types, handler, data, selector ) {
  98. var handleObjIn, eventHandle, tmp,
  99. events, t, handleObj,
  100. special, handlers, type, namespaces, origType,
  101. elemData = dataPriv.get( elem );
  102. // Only attach events to objects that accept data
  103. if ( !acceptData( elem ) ) {
  104. return;
  105. }
  106. // Caller can pass in an object of custom data in lieu of the handler
  107. if ( handler.handler ) {
  108. handleObjIn = handler;
  109. handler = handleObjIn.handler;
  110. selector = handleObjIn.selector;
  111. }
  112. // Ensure that invalid selectors throw exceptions at attach time
  113. // Evaluate against documentElement in case elem is a non-element node (e.g., document)
  114. if ( selector ) {
  115. jQuery.find.matchesSelector( documentElement, selector );
  116. }
  117. // Make sure that the handler has a unique ID, used to find/remove it later
  118. if ( !handler.guid ) {
  119. handler.guid = jQuery.guid++;
  120. }
  121. // Init the element's event structure and main handler, if this is the first
  122. if ( !( events = elemData.events ) ) {
  123. events = elemData.events = Object.create( null );
  124. }
  125. if ( !( eventHandle = elemData.handle ) ) {
  126. eventHandle = elemData.handle = function( e ) {
  127. // Discard the second event of a jQuery.event.trigger() and
  128. // when an event is called after a page has unloaded
  129. return typeof jQuery !== "undefined" && jQuery.event.triggered !== e.type ?
  130. jQuery.event.dispatch.apply( elem, arguments ) : undefined;
  131. };
  132. }
  133. // Handle multiple events separated by a space
  134. types = ( types || "" ).match( rnothtmlwhite ) || [ "" ];
  135. t = types.length;
  136. while ( t-- ) {
  137. tmp = rtypenamespace.exec( types[ t ] ) || [];
  138. type = origType = tmp[ 1 ];
  139. namespaces = ( tmp[ 2 ] || "" ).split( "." ).sort();
  140. // There *must* be a type, no attaching namespace-only handlers
  141. if ( !type ) {
  142. continue;
  143. }
  144. // If event changes its type, use the special event handlers for the changed type
  145. special = jQuery.event.special[ type ] || {};
  146. // If selector defined, determine special event api type, otherwise given type
  147. type = ( selector ? special.delegateType : special.bindType ) || type;
  148. // Update special based on newly reset type
  149. special = jQuery.event.special[ type ] || {};
  150. // handleObj is passed to all event handlers
  151. handleObj = jQuery.extend( {
  152. type: type,
  153. origType: origType,
  154. data: data,
  155. handler: handler,
  156. guid: handler.guid,
  157. selector: selector,
  158. needsContext: selector && jQuery.expr.match.needsContext.test( selector ),
  159. namespace: namespaces.join( "." )
  160. }, handleObjIn );
  161. // Init the event handler queue if we're the first
  162. if ( !( handlers = events[ type ] ) ) {
  163. handlers = events[ type ] = [];
  164. handlers.delegateCount = 0;
  165. // Only use addEventListener if the special events handler returns false
  166. if ( !special.setup ||
  167. special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
  168. if ( elem.addEventListener ) {
  169. elem.addEventListener( type, eventHandle );
  170. }
  171. }
  172. }
  173. if ( special.add ) {
  174. special.add.call( elem, handleObj );
  175. if ( !handleObj.handler.guid ) {
  176. handleObj.handler.guid = handler.guid;
  177. }
  178. }
  179. // Add to the element's handler list, delegates in front
  180. if ( selector ) {
  181. handlers.splice( handlers.delegateCount++, 0, handleObj );
  182. } else {
  183. handlers.push( handleObj );
  184. }
  185. // Keep track of which events have ever been used, for event optimization
  186. jQuery.event.global[ type ] = true;
  187. }
  188. },
  189. // Detach an event or set of events from an element
  190. remove: function( elem, types, handler, selector, mappedTypes ) {
  191. var j, origCount, tmp,
  192. events, t, handleObj,
  193. special, handlers, type, namespaces, origType,
  194. elemData = dataPriv.hasData( elem ) && dataPriv.get( elem );
  195. if ( !elemData || !( events = elemData.events ) ) {
  196. return;
  197. }
  198. // Once for each type.namespace in types; type may be omitted
  199. types = ( types || "" ).match( rnothtmlwhite ) || [ "" ];
  200. t = types.length;
  201. while ( t-- ) {
  202. tmp = rtypenamespace.exec( types[ t ] ) || [];
  203. type = origType = tmp[ 1 ];
  204. namespaces = ( tmp[ 2 ] || "" ).split( "." ).sort();
  205. // Unbind all events (on this namespace, if provided) for the element
  206. if ( !type ) {
  207. for ( type in events ) {
  208. jQuery.event.remove( elem, type + types[ t ], handler, selector, true );
  209. }
  210. continue;
  211. }
  212. special = jQuery.event.special[ type ] || {};
  213. type = ( selector ? special.delegateType : special.bindType ) || type;
  214. handlers = events[ type ] || [];
  215. tmp = tmp[ 2 ] &&
  216. new RegExp( "(^|\\.)" + namespaces.join( "\\.(?:.*\\.|)" ) + "(\\.|$)" );
  217. // Remove matching events
  218. origCount = j = handlers.length;
  219. while ( j-- ) {
  220. handleObj = handlers[ j ];
  221. if ( ( mappedTypes || origType === handleObj.origType ) &&
  222. ( !handler || handler.guid === handleObj.guid ) &&
  223. ( !tmp || tmp.test( handleObj.namespace ) ) &&
  224. ( !selector || selector === handleObj.selector ||
  225. selector === "**" && handleObj.selector ) ) {
  226. handlers.splice( j, 1 );
  227. if ( handleObj.selector ) {
  228. handlers.delegateCount--;
  229. }
  230. if ( special.remove ) {
  231. special.remove.call( elem, handleObj );
  232. }
  233. }
  234. }
  235. // Remove generic event handler if we removed something and no more handlers exist
  236. // (avoids potential for endless recursion during removal of special event handlers)
  237. if ( origCount && !handlers.length ) {
  238. if ( !special.teardown ||
  239. special.teardown.call( elem, namespaces, elemData.handle ) === false ) {
  240. jQuery.removeEvent( elem, type, elemData.handle );
  241. }
  242. delete events[ type ];
  243. }
  244. }
  245. // Remove data and the expando if it's no longer used
  246. if ( jQuery.isEmptyObject( events ) ) {
  247. dataPriv.remove( elem, "handle events" );
  248. }
  249. },
  250. dispatch: function( nativeEvent ) {
  251. var i, j, ret, matched, handleObj, handlerQueue,
  252. args = new Array( arguments.length ),
  253. // Make a writable jQuery.Event from the native event object
  254. event = jQuery.event.fix( nativeEvent ),
  255. handlers = (
  256. dataPriv.get( this, "events" ) || Object.create( null )
  257. )[ event.type ] || [],
  258. special = jQuery.event.special[ event.type ] || {};
  259. // Use the fix-ed jQuery.Event rather than the (read-only) native event
  260. args[ 0 ] = event;
  261. for ( i = 1; i < arguments.length; i++ ) {
  262. args[ i ] = arguments[ i ];
  263. }
  264. event.delegateTarget = this;
  265. // Call the preDispatch hook for the mapped type, and let it bail if desired
  266. if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) {
  267. return;
  268. }
  269. // Determine handlers
  270. handlerQueue = jQuery.event.handlers.call( this, event, handlers );
  271. // Run delegates first; they may want to stop propagation beneath us
  272. i = 0;
  273. while ( ( matched = handlerQueue[ i++ ] ) && !event.isPropagationStopped() ) {
  274. event.currentTarget = matched.elem;
  275. j = 0;
  276. while ( ( handleObj = matched.handlers[ j++ ] ) &&
  277. !event.isImmediatePropagationStopped() ) {
  278. // If the event is namespaced, then each handler is only invoked if it is
  279. // specially universal or its namespaces are a superset of the event's.
  280. if ( !event.rnamespace || handleObj.namespace === false ||
  281. event.rnamespace.test( handleObj.namespace ) ) {
  282. event.handleObj = handleObj;
  283. event.data = handleObj.data;
  284. ret = ( ( jQuery.event.special[ handleObj.origType ] || {} ).handle ||
  285. handleObj.handler ).apply( matched.elem, args );
  286. if ( ret !== undefined ) {
  287. if ( ( event.result = ret ) === false ) {
  288. event.preventDefault();
  289. event.stopPropagation();
  290. }
  291. }
  292. }
  293. }
  294. }
  295. // Call the postDispatch hook for the mapped type
  296. if ( special.postDispatch ) {
  297. special.postDispatch.call( this, event );
  298. }
  299. return event.result;
  300. },
  301. handlers: function( event, handlers ) {
  302. var i, handleObj, sel, matchedHandlers, matchedSelectors,
  303. handlerQueue = [],
  304. delegateCount = handlers.delegateCount,
  305. cur = event.target;
  306. // Find delegate handlers
  307. if ( delegateCount &&
  308. // Support: IE <=9
  309. // Black-hole SVG <use> instance trees (trac-13180)
  310. cur.nodeType &&
  311. // Support: Firefox <=42
  312. // Suppress spec-violating clicks indicating a non-primary pointer button (trac-3861)
  313. // https://www.w3.org/TR/DOM-Level-3-Events/#event-type-click
  314. // Support: IE 11 only
  315. // ...but not arrow key "clicks" of radio inputs, which can have `button` -1 (gh-2343)
  316. !( event.type === "click" && event.button >= 1 ) ) {
  317. for ( ; cur !== this; cur = cur.parentNode || this ) {
  318. // Don't check non-elements (trac-13208)
  319. // Don't process clicks on disabled elements (trac-6911, trac-8165, trac-11382, trac-11764)
  320. if ( cur.nodeType === 1 && !( event.type === "click" && cur.disabled === true ) ) {
  321. matchedHandlers = [];
  322. matchedSelectors = {};
  323. for ( i = 0; i < delegateCount; i++ ) {
  324. handleObj = handlers[ i ];
  325. // Don't conflict with Object.prototype properties (trac-13203)
  326. sel = handleObj.selector + " ";
  327. if ( matchedSelectors[ sel ] === undefined ) {
  328. matchedSelectors[ sel ] = handleObj.needsContext ?
  329. jQuery( sel, this ).index( cur ) > -1 :
  330. jQuery.find( sel, this, null, [ cur ] ).length;
  331. }
  332. if ( matchedSelectors[ sel ] ) {
  333. matchedHandlers.push( handleObj );
  334. }
  335. }
  336. if ( matchedHandlers.length ) {
  337. handlerQueue.push( { elem: cur, handlers: matchedHandlers } );
  338. }
  339. }
  340. }
  341. }
  342. // Add the remaining (directly-bound) handlers
  343. cur = this;
  344. if ( delegateCount < handlers.length ) {
  345. handlerQueue.push( { elem: cur, handlers: handlers.slice( delegateCount ) } );
  346. }
  347. return handlerQueue;
  348. },
  349. addProp: function( name, hook ) {
  350. Object.defineProperty( jQuery.Event.prototype, name, {
  351. enumerable: true,
  352. configurable: true,
  353. get: isFunction( hook ) ?
  354. function() {
  355. if ( this.originalEvent ) {
  356. return hook( this.originalEvent );
  357. }
  358. } :
  359. function() {
  360. if ( this.originalEvent ) {
  361. return this.originalEvent[ name ];
  362. }
  363. },
  364. set: function( value ) {
  365. Object.defineProperty( this, name, {
  366. enumerable: true,
  367. configurable: true,
  368. writable: true,
  369. value: value
  370. } );
  371. }
  372. } );
  373. },
  374. fix: function( originalEvent ) {
  375. return originalEvent[ jQuery.expando ] ?
  376. originalEvent :
  377. new jQuery.Event( originalEvent );
  378. },
  379. special: {
  380. load: {
  381. // Prevent triggered image.load events from bubbling to window.load
  382. noBubble: true
  383. },
  384. click: {
  385. // Utilize native event to ensure correct state for checkable inputs
  386. setup: function( data ) {
  387. // For mutual compressibility with _default, replace `this` access with a local var.
  388. // `|| data` is dead code meant only to preserve the variable through minification.
  389. var el = this || data;
  390. // Claim the first handler
  391. if ( rcheckableType.test( el.type ) &&
  392. el.click && nodeName( el, "input" ) ) {
  393. // dataPriv.set( el, "click", ... )
  394. leverageNative( el, "click", returnTrue );
  395. }
  396. // Return false to allow normal processing in the caller
  397. return false;
  398. },
  399. trigger: function( data ) {
  400. // For mutual compressibility with _default, replace `this` access with a local var.
  401. // `|| data` is dead code meant only to preserve the variable through minification.
  402. var el = this || data;
  403. // Force setup before triggering a click
  404. if ( rcheckableType.test( el.type ) &&
  405. el.click && nodeName( el, "input" ) ) {
  406. leverageNative( el, "click" );
  407. }
  408. // Return non-false to allow normal event-path propagation
  409. return true;
  410. },
  411. // For cross-browser consistency, suppress native .click() on links
  412. // Also prevent it if we're currently inside a leveraged native-event stack
  413. _default: function( event ) {
  414. var target = event.target;
  415. return rcheckableType.test( target.type ) &&
  416. target.click && nodeName( target, "input" ) &&
  417. dataPriv.get( target, "click" ) ||
  418. nodeName( target, "a" );
  419. }
  420. },
  421. beforeunload: {
  422. postDispatch: function( event ) {
  423. // Support: Firefox 20+
  424. // Firefox doesn't alert if the returnValue field is not set.
  425. if ( event.result !== undefined && event.originalEvent ) {
  426. event.originalEvent.returnValue = event.result;
  427. }
  428. }
  429. }
  430. }
  431. };
  432. // Ensure the presence of an event listener that handles manually-triggered
  433. // synthetic events by interrupting progress until reinvoked in response to
  434. // *native* events that it fires directly, ensuring that state changes have
  435. // already occurred before other listeners are invoked.
  436. function leverageNative( el, type, expectSync ) {
  437. // Missing expectSync indicates a trigger call, which must force setup through jQuery.event.add
  438. if ( !expectSync ) {
  439. if ( dataPriv.get( el, type ) === undefined ) {
  440. jQuery.event.add( el, type, returnTrue );
  441. }
  442. return;
  443. }
  444. // Register the controller as a special universal handler for all event namespaces
  445. dataPriv.set( el, type, false );
  446. jQuery.event.add( el, type, {
  447. namespace: false,
  448. handler: function( event ) {
  449. var notAsync, result,
  450. saved = dataPriv.get( this, type );
  451. if ( ( event.isTrigger & 1 ) && this[ type ] ) {
  452. // Interrupt processing of the outer synthetic .trigger()ed event
  453. // Saved data should be false in such cases, but might be a leftover capture object
  454. // from an async native handler (gh-4350)
  455. if ( !saved.length ) {
  456. // Store arguments for use when handling the inner native event
  457. // There will always be at least one argument (an event object), so this array
  458. // will not be confused with a leftover capture object.
  459. saved = slice.call( arguments );
  460. dataPriv.set( this, type, saved );
  461. // Trigger the native event and capture its result
  462. // Support: IE <=9 - 11+
  463. // focus() and blur() are asynchronous
  464. notAsync = expectSync( this, type );
  465. this[ type ]();
  466. result = dataPriv.get( this, type );
  467. if ( saved !== result || notAsync ) {
  468. dataPriv.set( this, type, false );
  469. } else {
  470. result = {};
  471. }
  472. if ( saved !== result ) {
  473. // Cancel the outer synthetic event
  474. event.stopImmediatePropagation();
  475. event.preventDefault();
  476. // Support: Chrome 86+
  477. // In Chrome, if an element having a focusout handler is blurred by
  478. // clicking outside of it, it invokes the handler synchronously. If
  479. // that handler calls `.remove()` on the element, the data is cleared,
  480. // leaving `result` undefined. We need to guard against this.
  481. return result && result.value;
  482. }
  483. // If this is an inner synthetic event for an event with a bubbling surrogate
  484. // (focus or blur), assume that the surrogate already propagated from triggering the
  485. // native event and prevent that from happening again here.
  486. // This technically gets the ordering wrong w.r.t. to `.trigger()` (in which the
  487. // bubbling surrogate propagates *after* the non-bubbling base), but that seems
  488. // less bad than duplication.
  489. } else if ( ( jQuery.event.special[ type ] || {} ).delegateType ) {
  490. event.stopPropagation();
  491. }
  492. // If this is a native event triggered above, everything is now in order
  493. // Fire an inner synthetic event with the original arguments
  494. } else if ( saved.length ) {
  495. // ...and capture the result
  496. dataPriv.set( this, type, {
  497. value: jQuery.event.trigger(
  498. // Support: IE <=9 - 11+
  499. // Extend with the prototype to reset the above stopImmediatePropagation()
  500. jQuery.extend( saved[ 0 ], jQuery.Event.prototype ),
  501. saved.slice( 1 ),
  502. this
  503. )
  504. } );
  505. // Abort handling of the native event
  506. event.stopImmediatePropagation();
  507. }
  508. }
  509. } );
  510. }
  511. jQuery.removeEvent = function( elem, type, handle ) {
  512. // This "if" is needed for plain objects
  513. if ( elem.removeEventListener ) {
  514. elem.removeEventListener( type, handle );
  515. }
  516. };
  517. jQuery.Event = function( src, props ) {
  518. // Allow instantiation without the 'new' keyword
  519. if ( !( this instanceof jQuery.Event ) ) {
  520. return new jQuery.Event( src, props );
  521. }
  522. // Event object
  523. if ( src && src.type ) {
  524. this.originalEvent = src;
  525. this.type = src.type;
  526. // Events bubbling up the document may have been marked as prevented
  527. // by a handler lower down the tree; reflect the correct value.
  528. this.isDefaultPrevented = src.defaultPrevented ||
  529. src.defaultPrevented === undefined &&
  530. // Support: Android <=2.3 only
  531. src.returnValue === false ?
  532. returnTrue :
  533. returnFalse;
  534. // Create target properties
  535. // Support: Safari <=6 - 7 only
  536. // Target should not be a text node (trac-504, trac-13143)
  537. this.target = ( src.target && src.target.nodeType === 3 ) ?
  538. src.target.parentNode :
  539. src.target;
  540. this.currentTarget = src.currentTarget;
  541. this.relatedTarget = src.relatedTarget;
  542. // Event type
  543. } else {
  544. this.type = src;
  545. }
  546. // Put explicitly provided properties onto the event object
  547. if ( props ) {
  548. jQuery.extend( this, props );
  549. }
  550. // Create a timestamp if incoming event doesn't have one
  551. this.timeStamp = src && src.timeStamp || Date.now();
  552. // Mark it as fixed
  553. this[ jQuery.expando ] = true;
  554. };
  555. // jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
  556. // https://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
  557. jQuery.Event.prototype = {
  558. constructor: jQuery.Event,
  559. isDefaultPrevented: returnFalse,
  560. isPropagationStopped: returnFalse,
  561. isImmediatePropagationStopped: returnFalse,
  562. isSimulated: false,
  563. preventDefault: function() {
  564. var e = this.originalEvent;
  565. this.isDefaultPrevented = returnTrue;
  566. if ( e && !this.isSimulated ) {
  567. e.preventDefault();
  568. }
  569. },
  570. stopPropagation: function() {
  571. var e = this.originalEvent;
  572. this.isPropagationStopped = returnTrue;
  573. if ( e && !this.isSimulated ) {
  574. e.stopPropagation();
  575. }
  576. },
  577. stopImmediatePropagation: function() {
  578. var e = this.originalEvent;
  579. this.isImmediatePropagationStopped = returnTrue;
  580. if ( e && !this.isSimulated ) {
  581. e.stopImmediatePropagation();
  582. }
  583. this.stopPropagation();
  584. }
  585. };
  586. // Includes all common event props including KeyEvent and MouseEvent specific props
  587. jQuery.each( {
  588. altKey: true,
  589. bubbles: true,
  590. cancelable: true,
  591. changedTouches: true,
  592. ctrlKey: true,
  593. detail: true,
  594. eventPhase: true,
  595. metaKey: true,
  596. pageX: true,
  597. pageY: true,
  598. shiftKey: true,
  599. view: true,
  600. "char": true,
  601. code: true,
  602. charCode: true,
  603. key: true,
  604. keyCode: true,
  605. button: true,
  606. buttons: true,
  607. clientX: true,
  608. clientY: true,
  609. offsetX: true,
  610. offsetY: true,
  611. pointerId: true,
  612. pointerType: true,
  613. screenX: true,
  614. screenY: true,
  615. targetTouches: true,
  616. toElement: true,
  617. touches: true,
  618. which: true
  619. }, jQuery.event.addProp );
  620. jQuery.each( { focus: "focusin", blur: "focusout" }, function( type, delegateType ) {
  621. jQuery.event.special[ type ] = {
  622. // Utilize native event if possible so blur/focus sequence is correct
  623. setup: function() {
  624. // Claim the first handler
  625. // dataPriv.set( this, "focus", ... )
  626. // dataPriv.set( this, "blur", ... )
  627. leverageNative( this, type, expectSync );
  628. // Return false to allow normal processing in the caller
  629. return false;
  630. },
  631. trigger: function() {
  632. // Force setup before trigger
  633. leverageNative( this, type );
  634. // Return non-false to allow normal event-path propagation
  635. return true;
  636. },
  637. // Suppress native focus or blur if we're currently inside
  638. // a leveraged native-event stack
  639. _default: function( event ) {
  640. return dataPriv.get( event.target, type );
  641. },
  642. delegateType: delegateType
  643. };
  644. } );
  645. // Create mouseenter/leave events using mouseover/out and event-time checks
  646. // so that event delegation works in jQuery.
  647. // Do the same for pointerenter/pointerleave and pointerover/pointerout
  648. //
  649. // Support: Safari 7 only
  650. // Safari sends mouseenter too often; see:
  651. // https://bugs.chromium.org/p/chromium/issues/detail?id=470258
  652. // for the description of the bug (it existed in older Chrome versions as well).
  653. jQuery.each( {
  654. mouseenter: "mouseover",
  655. mouseleave: "mouseout",
  656. pointerenter: "pointerover",
  657. pointerleave: "pointerout"
  658. }, function( orig, fix ) {
  659. jQuery.event.special[ orig ] = {
  660. delegateType: fix,
  661. bindType: fix,
  662. handle: function( event ) {
  663. var ret,
  664. target = this,
  665. related = event.relatedTarget,
  666. handleObj = event.handleObj;
  667. // For mouseenter/leave call the handler if related is outside the target.
  668. // NB: No relatedTarget if the mouse left/entered the browser window
  669. if ( !related || ( related !== target && !jQuery.contains( target, related ) ) ) {
  670. event.type = handleObj.origType;
  671. ret = handleObj.handler.apply( this, arguments );
  672. event.type = fix;
  673. }
  674. return ret;
  675. }
  676. };
  677. } );
  678. jQuery.fn.extend( {
  679. on: function( types, selector, data, fn ) {
  680. return on( this, types, selector, data, fn );
  681. },
  682. one: function( types, selector, data, fn ) {
  683. return on( this, types, selector, data, fn, 1 );
  684. },
  685. off: function( types, selector, fn ) {
  686. var handleObj, type;
  687. if ( types && types.preventDefault && types.handleObj ) {
  688. // ( event ) dispatched jQuery.Event
  689. handleObj = types.handleObj;
  690. jQuery( types.delegateTarget ).off(
  691. handleObj.namespace ?
  692. handleObj.origType + "." + handleObj.namespace :
  693. handleObj.origType,
  694. handleObj.selector,
  695. handleObj.handler
  696. );
  697. return this;
  698. }
  699. if ( typeof types === "object" ) {
  700. // ( types-object [, selector] )
  701. for ( type in types ) {
  702. this.off( type, selector, types[ type ] );
  703. }
  704. return this;
  705. }
  706. if ( selector === false || typeof selector === "function" ) {
  707. // ( types [, fn] )
  708. fn = selector;
  709. selector = undefined;
  710. }
  711. if ( fn === false ) {
  712. fn = returnFalse;
  713. }
  714. return this.each( function() {
  715. jQuery.event.remove( this, types, fn, selector );
  716. } );
  717. }
  718. } );
  719. return jQuery;
  720. } );