event.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. This file is part of the DITA Open Toolkit project hosted on
  3. Sourceforge.net. See the accompanying license.txt file for
  4. applicable licenses.
  5. Copyright (c) 2006, Yahoo! Inc. All rights reserved.
  6. Code licensed under the BSD License:
  7. http://developer.yahoo.net/yui/license.txt
  8. version: 0.10.0
  9. */
  10. /**
  11. * The CustomEvent class lets you define events for your application
  12. * that can be subscribed to by one or more independent component.
  13. *
  14. * @param {String} type The type of event, which is passed to the callback
  15. * when the event fires
  16. * @param {Object} oScope The context the event will fire from. "this" will
  17. * refer to this object in the callback. Default value:
  18. * the window object. The listener can override this.
  19. * @constructor
  20. */
  21. YAHOO.util.CustomEvent = function(type, oScope) {
  22. /**
  23. * The type of event, returned to subscribers when the event fires
  24. * @type string
  25. */
  26. this.type = type;
  27. /**
  28. * The scope the the event will fire from by default. Defaults to the window
  29. * obj
  30. * @type object
  31. */
  32. this.scope = oScope || window;
  33. /**
  34. * The subscribers to this event
  35. * @type Subscriber[]
  36. */
  37. this.subscribers = [];
  38. // Register with the event utility for automatic cleanup. Made optional
  39. // so that CustomEvent can be used independently of pe.event
  40. if (YAHOO.util.Event) {
  41. YAHOO.util.Event.regCE(this);
  42. }
  43. };
  44. YAHOO.util.CustomEvent.prototype = {
  45. /**
  46. * Subscribes the caller to this event
  47. * @param {Function} fn The function to execute
  48. * @param {Object} obj An object to be passed along when the event fires
  49. * @param {boolean} bOverride If true, the obj passed in becomes the execution
  50. * scope of the listener
  51. */
  52. subscribe: function(fn, obj, bOverride) {
  53. this.subscribers.push( new YAHOO.util.Subscriber(fn, obj, bOverride) );
  54. },
  55. /**
  56. * Unsubscribes the caller from this event
  57. * @param {Function} fn The function to execute
  58. * @param {Object} obj An object to be passed along when the event fires
  59. * @return {boolean} True if the subscriber was found and detached.
  60. */
  61. unsubscribe: function(fn, obj) {
  62. var found = false;
  63. for (var i=0, len=this.subscribers.length; i<len; ++i) {
  64. var s = this.subscribers[i];
  65. if (s && s.contains(fn, obj)) {
  66. this._delete(i);
  67. found = true;
  68. }
  69. }
  70. return found;
  71. },
  72. /**
  73. * Notifies the subscribers. The callback functions will be executed
  74. * from the scope specified when the event was created, and with the following
  75. * parameters:
  76. * <pre>
  77. * - The type of event
  78. * - All of the arguments fire() was executed with as an array
  79. * - The custom object (if any) that was passed into the subscribe() method
  80. * </pre>
  81. *
  82. * @param {Array} an arbitrary set of parameters to pass to the handler
  83. */
  84. fire: function() {
  85. for (var i=0, len=this.subscribers.length; i<len; ++i) {
  86. var s = this.subscribers[i];
  87. if (s) {
  88. var scope = (s.override) ? s.obj : this.scope;
  89. s.fn.call(scope, this.type, arguments, s.obj);
  90. }
  91. }
  92. },
  93. /**
  94. * Removes all listeners
  95. */
  96. unsubscribeAll: function() {
  97. for (var i=0, len=this.subscribers.length; i<len; ++i) {
  98. this._delete(i);
  99. }
  100. },
  101. /**
  102. * @private
  103. */
  104. _delete: function(index) {
  105. var s = this.subscribers[index];
  106. if (s) {
  107. delete s.fn;
  108. delete s.obj;
  109. }
  110. delete this.subscribers[index];
  111. }
  112. };
  113. /////////////////////////////////////////////////////////////////////
  114. /**
  115. * @class Stores the subscriber information to be used when the event fires.
  116. * @param {Function} fn The function to execute
  117. * @param {Object} obj An object to be passed along when the event fires
  118. * @param {boolean} bOverride If true, the obj passed in becomes the execution
  119. * scope of the listener
  120. * @constructor
  121. */
  122. YAHOO.util.Subscriber = function(fn, obj, bOverride) {
  123. /**
  124. * The callback that will be execute when the event fires
  125. * @type function
  126. */
  127. this.fn = fn;
  128. /**
  129. * An optional custom object that will passed to the callback when
  130. * the event fires
  131. * @type object
  132. */
  133. this.obj = obj || null;
  134. /**
  135. * The default execution scope for the event listener is defined when the
  136. * event is created (usually the object which contains the event).
  137. * By setting override to true, the execution scope becomes the custom
  138. * object passed in by the subscriber
  139. * @type boolean
  140. */
  141. this.override = (bOverride);
  142. };
  143. /**
  144. * Returns true if the fn and obj match this objects properties.
  145. * Used by the unsubscribe method to match the right subscriber.
  146. *
  147. * @param {Function} fn the function to execute
  148. * @param {Object} obj an object to be passed along when the event fires
  149. * @return {boolean} true if the supplied arguments match this
  150. * subscriber's signature.
  151. */
  152. YAHOO.util.Subscriber.prototype.contains = function(fn, obj) {
  153. return (this.fn == fn && this.obj == obj);
  154. };
  155. /* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
  156. // Only load this library once. If it is loaded a second time, existing
  157. // events cannot be detached.
  158. if (!YAHOO.util.Event) {
  159. /**
  160. * @class
  161. * The event utility provides functions to add and remove event listeners,
  162. * event cleansing. It also tries to automatically remove listeners it
  163. * registers during the unload event.
  164. * @constructor
  165. */
  166. YAHOO.util.Event = function() {
  167. /**
  168. * True after the onload event has fired
  169. * @type boolean
  170. * @private
  171. */
  172. var loadComplete = false;
  173. /**
  174. * Cache of wrapped listeners
  175. * @type array
  176. * @private
  177. */
  178. var listeners = [];
  179. /**
  180. * Listeners that will be attached during the onload event
  181. * @type array
  182. * @private
  183. */
  184. var delayedListeners = [];
  185. /**
  186. * User-defined unload function that will be fired before all events
  187. * are detached
  188. * @type array
  189. * @private
  190. */
  191. var unloadListeners = [];
  192. /**
  193. * Cache of the custom events that have been defined. Used for
  194. * automatic cleanup
  195. * @type array
  196. * @private
  197. */
  198. var customEvents = [];
  199. /**
  200. * Cache of DOM0 event handlers to work around issues with DOM2 events
  201. * in Safari
  202. * @private
  203. */
  204. var legacyEvents = [];
  205. /**
  206. * Listener stack for DOM0 events
  207. * @private
  208. */
  209. var legacyHandlers = [];
  210. /**
  211. * The number of times to poll after window.onload. This number is
  212. * increased if additional late-bound handlers are requested after
  213. * the page load.
  214. * @private
  215. */
  216. var retryCount = 0;
  217. /**
  218. * onAvailable listeners
  219. * @private
  220. */
  221. var onAvailStack = [];
  222. /**
  223. * Lookup table for legacy events
  224. * @private
  225. */
  226. var legacyMap = [];
  227. /**
  228. * Counter for auto id generation
  229. * @private
  230. */
  231. var counter = 0;
  232. return { // PREPROCESS
  233. /**
  234. * The number of times we should look for elements that are not
  235. * in the DOM at the time the event is requested after the document
  236. * has been loaded. The default is 200@50 ms, so it will poll
  237. * for 10 seconds or until all outstanding handlers are bound
  238. * (whichever comes first).
  239. * @type int
  240. */
  241. POLL_RETRYS: 200,
  242. /**
  243. * The poll interval in milliseconds
  244. * @type int
  245. */
  246. POLL_INTERVAL: 50,
  247. /**
  248. * Element to bind, int constant
  249. * @type int
  250. */
  251. EL: 0,
  252. /**
  253. * Type of event, int constant
  254. * @type int
  255. */
  256. TYPE: 1,
  257. /**
  258. * Function to execute, int constant
  259. * @type int
  260. */
  261. FN: 2,
  262. /**
  263. * Function wrapped for scope correction and cleanup, int constant
  264. * @type int
  265. */
  266. WFN: 3,
  267. /**
  268. * Object passed in by the user that will be returned as a
  269. * parameter to the callback, int constant
  270. * @type int
  271. */
  272. SCOPE: 3,
  273. /**
  274. * Adjusted scope, either the element we are registering the event
  275. * on or the custom object passed in by the listener, int constant
  276. * @type int
  277. */
  278. ADJ_SCOPE: 4,
  279. /**
  280. * Safari detection is necessary to work around the preventDefault
  281. * bug that makes it so you can't cancel a href click from the
  282. * handler. There is not a capabilities check we can use here.
  283. * @private
  284. */
  285. isSafari: (/Safari|Konqueror|KHTML/gi).test(navigator.userAgent),
  286. /**
  287. * IE detection needed to properly calculate pageX and pageY.
  288. * capabilities checking didn't seem to work because another
  289. * browser that does not provide the properties have the values
  290. * calculated in a different manner than IE.
  291. * @private
  292. */
  293. isIE: (!this.isSafari && !navigator.userAgent.match(/opera/gi) &&
  294. navigator.userAgent.match(/msie/gi)),
  295. /**
  296. * @private
  297. */
  298. addDelayedListener: function(el, sType, fn, oScope, bOverride) {
  299. delayedListeners[delayedListeners.length] =
  300. [el, sType, fn, oScope, bOverride];
  301. // If this happens after the inital page load, we need to
  302. // reset the poll counter so that we continue to search for
  303. // the element for a fixed period of time.
  304. if (loadComplete) {
  305. retryCount = this.POLL_RETRYS;
  306. this.startTimeout(0);
  307. // this._tryPreloadAttach();
  308. }
  309. },
  310. /**
  311. * @private
  312. */
  313. startTimeout: function(interval) {
  314. var i = (interval || interval === 0) ? interval : this.POLL_INTERVAL;
  315. var self = this;
  316. var callback = function() { self._tryPreloadAttach(); };
  317. this.timeout = setTimeout(callback, i);
  318. },
  319. /**
  320. * Executes the supplied callback when the item with the supplied
  321. * id is found. This is meant to be used to execute behavior as
  322. * soon as possible as the page loads. If you use this after the
  323. * initial page load it will poll for a fixed time for the element.
  324. * The number of times it will poll and the frequency are
  325. * configurable. By default it will poll for 10 seconds.
  326. * @param {string} p_id the id of the element to look for.
  327. * @param {function} p_fn what to execute when the element is found.
  328. * @param {object} p_obj an optional object to be passed back as
  329. * a parameter to p_fn.
  330. * @param {boolean} p_override If set to true, p_fn will execute
  331. * in the scope of p_obj
  332. *
  333. */
  334. onAvailable: function(p_id, p_fn, p_obj, p_override) {
  335. onAvailStack.push( { id: p_id,
  336. fn: p_fn,
  337. obj: p_obj,
  338. override: p_override } );
  339. retryCount = this.POLL_RETRYS;
  340. this.startTimeout(0);
  341. // this._tryPreloadAttach();
  342. },
  343. /**
  344. * Appends an event handler
  345. *
  346. * @param {Object} el The html element to assign the
  347. * event to
  348. * @param {String} sType The type of event to append
  349. * @param {Function} fn The method the event invokes
  350. * @param {Object} oScope An arbitrary object that will be
  351. * passed as a parameter to the handler
  352. * @param {boolean} bOverride If true, the obj passed in becomes
  353. * the execution scope of the listener
  354. * @return {boolean} True if the action was successful or defered,
  355. * false if one or more of the elements
  356. * could not have the event bound to it.
  357. */
  358. addListener: function(el, sType, fn, oScope, bOverride) {
  359. if (!fn || !fn.call) {
  360. return false;
  361. }
  362. // The el argument can be an array of elements or element ids.
  363. if ( this._isValidCollection(el)) {
  364. var ok = true;
  365. for (var i=0,len=el.length; i<len; ++i) {
  366. ok = ( this.on(el[i],
  367. sType,
  368. fn,
  369. oScope,
  370. bOverride) && ok );
  371. }
  372. return ok;
  373. } else if (typeof el == "string") {
  374. var oEl = this.getEl(el);
  375. // If the el argument is a string, we assume it is
  376. // actually the id of the element. If the page is loaded
  377. // we convert el to the actual element, otherwise we
  378. // defer attaching the event until onload event fires
  379. // check to see if we need to delay hooking up the event
  380. // until after the page loads.
  381. if (loadComplete && oEl) {
  382. el = oEl;
  383. } else {
  384. // defer adding the event until onload fires
  385. this.addDelayedListener(el,
  386. sType,
  387. fn,
  388. oScope,
  389. bOverride);
  390. return true;
  391. }
  392. }
  393. // Element should be an html element or an array if we get
  394. // here.
  395. if (!el) {
  396. return false;
  397. }
  398. // we need to make sure we fire registered unload events
  399. // prior to automatically unhooking them. So we hang on to
  400. // these instead of attaching them to the window and fire the
  401. // handles explicitly during our one unload event.
  402. if ("unload" == sType && oScope !== this) {
  403. unloadListeners[unloadListeners.length] =
  404. [el, sType, fn, oScope, bOverride];
  405. return true;
  406. }
  407. // if the user chooses to override the scope, we use the custom
  408. // object passed in, otherwise the executing scope will be the
  409. // HTML element that the event is registered on
  410. var scope = (bOverride) ? oScope : el;
  411. // wrap the function so we can return the oScope object when
  412. // the event fires;
  413. var wrappedFn = function(e) {
  414. return fn.call(scope, YAHOO.util.Event.getEvent(e),
  415. oScope);
  416. };
  417. var li = [el, sType, fn, wrappedFn, scope];
  418. var index = listeners.length;
  419. // cache the listener so we can try to automatically unload
  420. listeners[index] = li;
  421. if (this.useLegacyEvent(el, sType)) {
  422. var legacyIndex = this.getLegacyIndex(el, sType);
  423. if (legacyIndex == -1) {
  424. legacyIndex = legacyEvents.length;
  425. legacyMap[el.id + sType] = legacyIndex;
  426. // cache the signature for the DOM0 event, and
  427. // include the existing handler for the event, if any
  428. legacyEvents[legacyIndex] =
  429. [el, sType, el["on" + sType]];
  430. legacyHandlers[legacyIndex] = [];
  431. el["on" + sType] =
  432. function(e) {
  433. YAHOO.util.Event.fireLegacyEvent(
  434. YAHOO.util.Event.getEvent(e), legacyIndex);
  435. };
  436. }
  437. // add a reference to the wrapped listener to our custom
  438. // stack of events
  439. legacyHandlers[legacyIndex].push(index);
  440. // DOM2 Event model
  441. } else if (el.addEventListener) {
  442. el.addEventListener(sType, wrappedFn, false);
  443. // Internet Explorer abstraction
  444. } else if (el.attachEvent) {
  445. el.attachEvent("on" + sType, wrappedFn);
  446. }
  447. return true;
  448. },
  449. /**
  450. * Shorthand for YAHOO.util.Event.addListener
  451. * @type function
  452. */
  453. // on: this.addListener,
  454. /**
  455. * When using legacy events, the handler is routed to this object
  456. * so we can fire our custom listener stack.
  457. * @private
  458. */
  459. fireLegacyEvent: function(e, legacyIndex) {
  460. var ok = true;
  461. var le = legacyHandlers[legacyIndex];
  462. for (var i=0,len=le.length; i<len; ++i) {
  463. var index = le[i];
  464. if (index) {
  465. var li = listeners[index];
  466. if ( li && li[this.WFN] ) {
  467. var scope = li[this.ADJ_SCOPE];
  468. var ret = li[this.WFN].call(scope, e);
  469. ok = (ok && ret);
  470. } else {
  471. // This listener was removed, so delete it from
  472. // the array
  473. delete le[i];
  474. }
  475. }
  476. }
  477. return ok;
  478. },
  479. /**
  480. * Returns the legacy event index that matches the supplied
  481. * signature
  482. * @private
  483. */
  484. getLegacyIndex: function(el, sType) {
  485. /*
  486. for (var i=0,len=legacyEvents.length; i<len; ++i) {
  487. var le = legacyEvents[i];
  488. if (le && le[0] === el && le[1] === sType) {
  489. return i;
  490. }
  491. }
  492. return -1;
  493. */
  494. var key = this.generateId(el) + sType;
  495. if (typeof legacyMap[key] == "undefined") {
  496. return -1;
  497. } else {
  498. return legacyMap[key];
  499. }
  500. },
  501. /**
  502. * Logic that determines when we should automatically use legacy
  503. * events instead of DOM2 events.
  504. * @private
  505. */
  506. useLegacyEvent: function(el, sType) {
  507. if (!el.addEventListener && !el.attachEvent) {
  508. return true;
  509. } else if (this.isSafari) {
  510. if ("click" == sType || "dblclick" == sType) {
  511. return true;
  512. }
  513. }
  514. return false;
  515. },
  516. /**
  517. * Removes an event handler
  518. *
  519. * @param {Object} el the html element or the id of the element to
  520. * assign the event to.
  521. * @param {String} sType the type of event to remove
  522. * @param {Function} fn the method the event invokes
  523. * @return {boolean} true if the unbind was successful, false
  524. * otherwise
  525. */
  526. removeListener: function(el, sType, fn, index) {
  527. if (!fn || !fn.call) {
  528. return false;
  529. }
  530. // The el argument can be a string
  531. if (typeof el == "string") {
  532. el = this.getEl(el);
  533. // The el argument can be an array of elements or element ids.
  534. } else if ( this._isValidCollection(el)) {
  535. var ok = true;
  536. for (var i=0,len=el.length; i<len; ++i) {
  537. ok = ( this.removeListener(el[i], sType, fn) && ok );
  538. }
  539. return ok;
  540. }
  541. if ("unload" == sType) {
  542. for (i=0, len=unloadListeners.length; i<len; i++) {
  543. var li = unloadListeners[i];
  544. if (li &&
  545. li[0] == el &&
  546. li[1] == sType &&
  547. li[2] == fn) {
  548. delete unloadListeners[i];
  549. return true;
  550. }
  551. }
  552. return false;
  553. }
  554. var cacheItem = null;
  555. if ("undefined" == typeof index) {
  556. index = this._getCacheIndex(el, sType, fn);
  557. }
  558. if (index >= 0) {
  559. cacheItem = listeners[index];
  560. }
  561. if (!el || !cacheItem) {
  562. return false;
  563. }
  564. if (el.removeEventListener) {
  565. el.removeEventListener(sType, cacheItem[this.WFN], false);
  566. } else if (el.detachEvent) {
  567. el.detachEvent("on" + sType, cacheItem[this.WFN]);
  568. }
  569. // removed the wrapped handler
  570. delete listeners[index][this.WFN];
  571. delete listeners[index][this.FN];
  572. delete listeners[index];
  573. return true;
  574. },
  575. /**
  576. * Returns the event's target element
  577. * @param {Event} ev the event
  578. * @param {boolean} resolveTextNode when set to true the target's
  579. * parent will be returned if the target is a
  580. * text node
  581. * @return {HTMLElement} the event's target
  582. */
  583. getTarget: function(ev, resolveTextNode) {
  584. var t = ev.target || ev.srcElement;
  585. if (resolveTextNode && t && "#text" == t.nodeName) {
  586. return t.parentNode;
  587. } else {
  588. return t;
  589. }
  590. },
  591. /**
  592. * Returns the event's pageX
  593. * @param {Event} ev the event
  594. * @return {int} the event's pageX
  595. */
  596. getPageX: function(ev) {
  597. var x = ev.pageX;
  598. if (!x && 0 !== x) {
  599. x = ev.clientX || 0;
  600. if ( this.isIE ) {
  601. x += this._getScrollLeft();
  602. }
  603. }
  604. return x;
  605. },
  606. /**
  607. * Returns the event's pageY
  608. * @param {Event} ev the event
  609. * @return {int} the event's pageY
  610. */
  611. getPageY: function(ev) {
  612. var y = ev.pageY;
  613. if (!y && 0 !== y) {
  614. y = ev.clientY || 0;
  615. if ( this.isIE ) {
  616. y += this._getScrollTop();
  617. }
  618. }
  619. return y;
  620. },
  621. /**
  622. * Returns the pageX and pageY properties as an indexed array.
  623. * @type int[]
  624. */
  625. getXY: function(ev) {
  626. return [this.getPageX(ev), this.getPageY(ev)];
  627. },
  628. /**
  629. * Returns the event's related target
  630. * @param {Event} ev the event
  631. * @return {HTMLElement} the event's relatedTarget
  632. */
  633. getRelatedTarget: function(ev) {
  634. var t = ev.relatedTarget;
  635. if (!t) {
  636. if (ev.type == "mouseout") {
  637. t = ev.toElement;
  638. } else if (ev.type == "mouseover") {
  639. t = ev.fromElement;
  640. }
  641. }
  642. return t;
  643. },
  644. /**
  645. * Returns the time of the event. If the time is not included, the
  646. * event is modified using the current time.
  647. * @param {Event} ev the event
  648. * @return {Date} the time of the event
  649. */
  650. getTime: function(ev) {
  651. if (!ev.time) {
  652. var t = new Date().getTime();
  653. try {
  654. ev.time = t;
  655. } catch(e) {
  656. // can't set the time property
  657. return t;
  658. }
  659. }
  660. return ev.time;
  661. },
  662. /**
  663. * Convenience method for stopPropagation + preventDefault
  664. * @param {Event} ev the event
  665. */
  666. stopEvent: function(ev) {
  667. this.stopPropagation(ev);
  668. this.preventDefault(ev);
  669. },
  670. /**
  671. * Stops event propagation
  672. * @param {Event} ev the event
  673. */
  674. stopPropagation: function(ev) {
  675. if (ev.stopPropagation) {
  676. ev.stopPropagation();
  677. } else {
  678. ev.cancelBubble = true;
  679. }
  680. },
  681. /**
  682. * Prevents the default behavior of the event
  683. * @param {Event} ev the event
  684. */
  685. preventDefault: function(ev) {
  686. if (ev.preventDefault) {
  687. ev.preventDefault();
  688. } else {
  689. ev.returnValue = false;
  690. }
  691. },
  692. /**
  693. * Finds the event in the window object, the caller's arguments, or
  694. * in the arguments of another method in the callstack. This is
  695. * executed automatically for events registered through the event
  696. * manager, so the implementer should not normally need to execute
  697. * this function at all.
  698. * @param {Event} the event parameter from the handler
  699. * @return {Event} the event
  700. */
  701. getEvent: function(e) {
  702. var ev = e || window.event;
  703. if (!ev) {
  704. var c = this.getEvent.caller;
  705. while (c) {
  706. ev = c.arguments[0];
  707. if (ev && Event == ev.constructor) {
  708. break;
  709. }
  710. c = c.caller;
  711. }
  712. }
  713. return ev;
  714. },
  715. /**
  716. * Returns the charcode for an event
  717. * @param {Event} ev the event
  718. * @return {int} the event's charCode
  719. */
  720. getCharCode: function(ev) {
  721. return ev.charCode || ((ev.type == "keypress") ? ev.keyCode : 0);
  722. },
  723. /**
  724. * @private
  725. * Locating the saved event handler data by function ref
  726. */
  727. _getCacheIndex: function(el, sType, fn) {
  728. for (var i=0,len=listeners.length; i<len; ++i) {
  729. var li = listeners[i];
  730. if ( li &&
  731. li[this.FN] == fn &&
  732. li[this.EL] == el &&
  733. li[this.TYPE] == sType ) {
  734. return i;
  735. }
  736. }
  737. return -1;
  738. },
  739. /**
  740. * Generates an unique ID for the element if it does not already
  741. * have one.
  742. * @param el the element
  743. * @return {string} the id of the element
  744. */
  745. generateId: function(el) {
  746. var id = el.id;
  747. if (!id) {
  748. id = "yuievtautoid-" + (counter++);
  749. el.id = id;
  750. }
  751. return id;
  752. },
  753. /**
  754. * We want to be able to use getElementsByTagName as a collection
  755. * to attach a group of events to. Unfortunately, different
  756. * browsers return different types of collections. This function
  757. * tests to determine if the object is array-like. It will also
  758. * fail if the object is an array, but is empty.
  759. * @param o the object to test
  760. * @return {boolean} true if the object is array-like and populated
  761. * @private
  762. */
  763. _isValidCollection: function(o) {
  764. return ( o && // o is something
  765. o.length && // o is indexed
  766. typeof o != "string" && // o is not a string
  767. !o.tagName && // o is not an HTML element
  768. !o.alert && // o is not a window
  769. typeof o[0] != "undefined" );
  770. },
  771. /**
  772. * @private
  773. * DOM element cache
  774. */
  775. elCache: {},
  776. /**
  777. * We cache elements bound by id because when the unload event
  778. * fires, we can no longer use document.getElementById
  779. * @private
  780. */
  781. getEl: function(id) {
  782. return document.getElementById(id);
  783. },
  784. /**
  785. * Clears the element cache
  786. * @deprecated
  787. * @private
  788. */
  789. clearCache: function() { },
  790. /**
  791. * Called by CustomEvent instances to provide a handle to the
  792. * event * that can be removed later on. Should be package
  793. * protected.
  794. * @private
  795. */
  796. regCE: function(ce) {
  797. customEvents.push(ce);
  798. },
  799. /**
  800. * @private
  801. * hook up any deferred listeners
  802. */
  803. _load: function(e) {
  804. loadComplete = true;
  805. },
  806. /**
  807. * Polling function that runs before the onload event fires,
  808. * attempting * to attach to DOM Nodes as soon as they are
  809. * available
  810. * @private
  811. */
  812. _tryPreloadAttach: function() {
  813. if (this.locked) {
  814. return false;
  815. }
  816. this.locked = true;
  817. // keep trying until after the page is loaded. We need to
  818. // check the page load state prior to trying to bind the
  819. // elements so that we can be certain all elements have been
  820. // tested appropriately
  821. var tryAgain = !loadComplete;
  822. if (!tryAgain) {
  823. tryAgain = (retryCount > 0);
  824. }
  825. // Delayed listeners
  826. var stillDelayed = [];
  827. for (var i=0,len=delayedListeners.length; i<len; ++i) {
  828. var d = delayedListeners[i];
  829. // There may be a race condition here, so we need to
  830. // verify the array element is usable.
  831. if (d) {
  832. // el will be null if document.getElementById did not
  833. // work
  834. var el = this.getEl(d[this.EL]);
  835. if (el) {
  836. this.on(el, d[this.TYPE], d[this.FN],
  837. d[this.SCOPE], d[this.ADJ_SCOPE]);
  838. delete delayedListeners[i];
  839. } else {
  840. stillDelayed.push(d);
  841. }
  842. }
  843. }
  844. delayedListeners = stillDelayed;
  845. // onAvailable
  846. notAvail = [];
  847. for (i=0,len=onAvailStack.length; i<len ; ++i) {
  848. var item = onAvailStack[i];
  849. if (item) {
  850. el = this.getEl(item.id);
  851. if (el) {
  852. var scope = (item.override) ? item.obj : el;
  853. item.fn.call(scope, item.obj);
  854. delete onAvailStack[i];
  855. } else {
  856. notAvail.push(item);
  857. }
  858. }
  859. }
  860. retryCount = (stillDelayed.length === 0 &&
  861. notAvail.length === 0) ? 0 : retryCount - 1;
  862. if (tryAgain) {
  863. this.startTimeout();
  864. }
  865. this.locked = false;
  866. },
  867. /**
  868. * Removes all listeners registered by pe.event. Called
  869. * automatically during the unload event.
  870. * @private
  871. */
  872. _unload: function(e, me) {
  873. for (var i=0,len=unloadListeners.length; i<len; ++i) {
  874. var l = unloadListeners[i];
  875. if (l) {
  876. var scope = (l[this.ADJ_SCOPE]) ? l[this.SCOPE]: window;
  877. l[this.FN].call(scope, this.getEvent(e), l[this.SCOPE] );
  878. }
  879. }
  880. if (listeners && listeners.length > 0) {
  881. for (i=0,len=listeners.length; i<len ; ++i) {
  882. l = listeners[i];
  883. if (l) {
  884. this.removeListener(l[this.EL], l[this.TYPE],
  885. l[this.FN], i);
  886. }
  887. }
  888. this.clearCache();
  889. }
  890. for (i=0,len=customEvents.length; i<len; ++i) {
  891. customEvents[i].unsubscribeAll();
  892. delete customEvents[i];
  893. }
  894. for (i=0,len=legacyEvents.length; i<len; ++i) {
  895. // dereference the element
  896. delete legacyEvents[i][0];
  897. // delete the array item
  898. delete legacyEvents[i];
  899. }
  900. },
  901. /**
  902. * Returns scrollLeft
  903. * @private
  904. */
  905. _getScrollLeft: function() {
  906. return this._getScroll()[1];
  907. },
  908. /**
  909. * Returns scrollTop
  910. * @private
  911. */
  912. _getScrollTop: function() {
  913. return this._getScroll()[0];
  914. },
  915. /**
  916. * Returns the scrollTop and scrollLeft. Used to calculate the
  917. * pageX and pageY in Internet Explorer
  918. * @private
  919. */
  920. _getScroll: function() {
  921. var dd = document.documentElement; db = document.body;
  922. if (dd && dd.scrollTop) {
  923. return [dd.scrollTop, dd.scrollLeft];
  924. } else if (db) {
  925. return [db.scrollTop, db.scrollLeft];
  926. } else {
  927. return [0, 0];
  928. }
  929. }
  930. };
  931. } ();
  932. /**
  933. * @private
  934. */
  935. YAHOO.util.Event.on = YAHOO.util.Event.addListener;
  936. if (document && document.body) {
  937. YAHOO.util.Event._load();
  938. } else {
  939. YAHOO.util.Event.on(window, "load", YAHOO.util.Event._load,
  940. YAHOO.util.Event, true);
  941. }
  942. YAHOO.util.Event.on(window, "unload", YAHOO.util.Event._unload,
  943. YAHOO.util.Event, true);
  944. YAHOO.util.Event._tryPreloadAttach();
  945. }