Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

event-custom-complex.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('event-custom-complex', function(Y) {
  9. /**
  10. * Adds event facades, preventable default behavior, and bubbling.
  11. * events.
  12. * @module event-custom
  13. * @submodule event-custom-complex
  14. */
  15. var FACADE,
  16. FACADE_KEYS,
  17. key,
  18. EMPTY = {},
  19. CEProto = Y.CustomEvent.prototype,
  20. ETProto = Y.EventTarget.prototype,
  21. mixFacadeProps = function(facade, payload) {
  22. var p;
  23. for (p in payload) {
  24. if (!(FACADE_KEYS.hasOwnProperty(p))) {
  25. facade[p] = payload[p];
  26. }
  27. }
  28. };
  29. /**
  30. * Wraps and protects a custom event for use when emitFacade is set to true.
  31. * Requires the event-custom-complex module
  32. * @class EventFacade
  33. * @param e {Event} the custom event
  34. * @param currentTarget {HTMLElement} the element the listener was attached to
  35. */
  36. Y.EventFacade = function(e, currentTarget) {
  37. e = e || EMPTY;
  38. this._event = e;
  39. /**
  40. * The arguments passed to fire
  41. * @property details
  42. * @type Array
  43. */
  44. this.details = e.details;
  45. /**
  46. * The event type, this can be overridden by the fire() payload
  47. * @property type
  48. * @type string
  49. */
  50. this.type = e.type;
  51. /**
  52. * The real event type
  53. * @property _type
  54. * @type string
  55. * @private
  56. */
  57. this._type = e.type;
  58. //////////////////////////////////////////////////////
  59. /**
  60. * Node reference for the targeted eventtarget
  61. * @property target
  62. * @type Node
  63. */
  64. this.target = e.target;
  65. /**
  66. * Node reference for the element that the listener was attached to.
  67. * @property currentTarget
  68. * @type Node
  69. */
  70. this.currentTarget = currentTarget;
  71. /**
  72. * Node reference to the relatedTarget
  73. * @property relatedTarget
  74. * @type Node
  75. */
  76. this.relatedTarget = e.relatedTarget;
  77. };
  78. Y.mix(Y.EventFacade.prototype, {
  79. /**
  80. * Stops the propagation to the next bubble target
  81. * @method stopPropagation
  82. */
  83. stopPropagation: function() {
  84. this._event.stopPropagation();
  85. this.stopped = 1;
  86. },
  87. /**
  88. * Stops the propagation to the next bubble target and
  89. * prevents any additional listeners from being exectued
  90. * on the current target.
  91. * @method stopImmediatePropagation
  92. */
  93. stopImmediatePropagation: function() {
  94. this._event.stopImmediatePropagation();
  95. this.stopped = 2;
  96. },
  97. /**
  98. * Prevents the event's default behavior
  99. * @method preventDefault
  100. */
  101. preventDefault: function() {
  102. this._event.preventDefault();
  103. this.prevented = 1;
  104. },
  105. /**
  106. * Stops the event propagation and prevents the default
  107. * event behavior.
  108. * @method halt
  109. * @param immediate {boolean} if true additional listeners
  110. * on the current target will not be executed
  111. */
  112. halt: function(immediate) {
  113. this._event.halt(immediate);
  114. this.prevented = 1;
  115. this.stopped = (immediate) ? 2 : 1;
  116. }
  117. });
  118. CEProto.fireComplex = function(args) {
  119. var es, ef, q, queue, ce, ret, events, subs, postponed,
  120. self = this, host = self.host || self, next, oldbubble;
  121. if (self.stack) {
  122. // queue this event if the current item in the queue bubbles
  123. if (self.queuable && self.type != self.stack.next.type) {
  124. self.log('queue ' + self.type);
  125. self.stack.queue.push([self, args]);
  126. return true;
  127. }
  128. }
  129. es = self.stack || {
  130. // id of the first event in the stack
  131. id: self.id,
  132. next: self,
  133. silent: self.silent,
  134. stopped: 0,
  135. prevented: 0,
  136. bubbling: null,
  137. type: self.type,
  138. // defaultFnQueue: new Y.Queue(),
  139. afterQueue: new Y.Queue(),
  140. defaultTargetOnly: self.defaultTargetOnly,
  141. queue: []
  142. };
  143. subs = self.getSubs();
  144. self.stopped = (self.type !== es.type) ? 0 : es.stopped;
  145. self.prevented = (self.type !== es.type) ? 0 : es.prevented;
  146. self.target = self.target || host;
  147. if (self.stoppedFn) {
  148. events = new Y.EventTarget({
  149. fireOnce: true,
  150. context: host
  151. });
  152. self.events = events;
  153. events.on('stopped', self.stoppedFn);
  154. }
  155. self.currentTarget = host;
  156. self.details = args.slice(); // original arguments in the details
  157. // self.log("Firing " + self + ", " + "args: " + args);
  158. self.log("Firing " + self.type);
  159. self._facade = null; // kill facade to eliminate stale properties
  160. ef = self._getFacade(args);
  161. if (Y.Lang.isObject(args[0])) {
  162. args[0] = ef;
  163. } else {
  164. args.unshift(ef);
  165. }
  166. if (subs[0]) {
  167. self._procSubs(subs[0], args, ef);
  168. }
  169. // bubble if this is hosted in an event target and propagation has not been stopped
  170. if (self.bubbles && host.bubble && !self.stopped) {
  171. oldbubble = es.bubbling;
  172. es.bubbling = self.type;
  173. if (es.type != self.type) {
  174. es.stopped = 0;
  175. es.prevented = 0;
  176. }
  177. ret = host.bubble(self, args, null, es);
  178. self.stopped = Math.max(self.stopped, es.stopped);
  179. self.prevented = Math.max(self.prevented, es.prevented);
  180. es.bubbling = oldbubble;
  181. }
  182. if (self.prevented) {
  183. if (self.preventedFn) {
  184. self.preventedFn.apply(host, args);
  185. }
  186. } else if (self.defaultFn &&
  187. ((!self.defaultTargetOnly && !es.defaultTargetOnly) ||
  188. host === ef.target)) {
  189. self.defaultFn.apply(host, args);
  190. }
  191. // broadcast listeners are fired as discreet events on the
  192. // YUI instance and potentially the YUI global.
  193. self._broadcast(args);
  194. // Queue the after
  195. if (subs[1] && !self.prevented && self.stopped < 2) {
  196. if (es.id === self.id || self.type != host._yuievt.bubbling) {
  197. self._procSubs(subs[1], args, ef);
  198. while ((next = es.afterQueue.last())) {
  199. next();
  200. }
  201. } else {
  202. postponed = subs[1];
  203. if (es.execDefaultCnt) {
  204. postponed = Y.merge(postponed);
  205. Y.each(postponed, function(s) {
  206. s.postponed = true;
  207. });
  208. }
  209. es.afterQueue.add(function() {
  210. self._procSubs(postponed, args, ef);
  211. });
  212. }
  213. }
  214. self.target = null;
  215. if (es.id === self.id) {
  216. queue = es.queue;
  217. while (queue.length) {
  218. q = queue.pop();
  219. ce = q[0];
  220. // set up stack to allow the next item to be processed
  221. es.next = ce;
  222. ce.fire.apply(ce, q[1]);
  223. }
  224. self.stack = null;
  225. }
  226. ret = !(self.stopped);
  227. if (self.type != host._yuievt.bubbling) {
  228. es.stopped = 0;
  229. es.prevented = 0;
  230. self.stopped = 0;
  231. self.prevented = 0;
  232. }
  233. // Kill the cached facade to free up memory.
  234. // Otherwise we have the facade from the last fire, sitting around forever.
  235. self._facade = null;
  236. return ret;
  237. };
  238. CEProto._getFacade = function() {
  239. var ef = this._facade, o,
  240. args = this.details;
  241. if (!ef) {
  242. ef = new Y.EventFacade(this, this.currentTarget);
  243. }
  244. // if the first argument is an object literal, apply the
  245. // properties to the event facade
  246. o = args && args[0];
  247. if (Y.Lang.isObject(o, true)) {
  248. // protect the event facade properties
  249. mixFacadeProps(ef, o);
  250. // Allow the event type to be faked
  251. // http://yuilibrary.com/projects/yui3/ticket/2528376
  252. ef.type = o.type || ef.type;
  253. }
  254. // update the details field with the arguments
  255. // ef.type = this.type;
  256. ef.details = this.details;
  257. // use the original target when the event bubbled to this target
  258. ef.target = this.originalTarget || this.target;
  259. ef.currentTarget = this.currentTarget;
  260. ef.stopped = 0;
  261. ef.prevented = 0;
  262. this._facade = ef;
  263. return this._facade;
  264. };
  265. /**
  266. * Stop propagation to bubble targets
  267. * @for CustomEvent
  268. * @method stopPropagation
  269. */
  270. CEProto.stopPropagation = function() {
  271. this.stopped = 1;
  272. if (this.stack) {
  273. this.stack.stopped = 1;
  274. }
  275. if (this.events) {
  276. this.events.fire('stopped', this);
  277. }
  278. };
  279. /**
  280. * Stops propagation to bubble targets, and prevents any remaining
  281. * subscribers on the current target from executing.
  282. * @method stopImmediatePropagation
  283. */
  284. CEProto.stopImmediatePropagation = function() {
  285. this.stopped = 2;
  286. if (this.stack) {
  287. this.stack.stopped = 2;
  288. }
  289. if (this.events) {
  290. this.events.fire('stopped', this);
  291. }
  292. };
  293. /**
  294. * Prevents the execution of this event's defaultFn
  295. * @method preventDefault
  296. */
  297. CEProto.preventDefault = function() {
  298. if (this.preventable) {
  299. this.prevented = 1;
  300. if (this.stack) {
  301. this.stack.prevented = 1;
  302. }
  303. }
  304. };
  305. /**
  306. * Stops the event propagation and prevents the default
  307. * event behavior.
  308. * @method halt
  309. * @param immediate {boolean} if true additional listeners
  310. * on the current target will not be executed
  311. */
  312. CEProto.halt = function(immediate) {
  313. if (immediate) {
  314. this.stopImmediatePropagation();
  315. } else {
  316. this.stopPropagation();
  317. }
  318. this.preventDefault();
  319. };
  320. /**
  321. * Registers another EventTarget as a bubble target. Bubble order
  322. * is determined by the order registered. Multiple targets can
  323. * be specified.
  324. *
  325. * Events can only bubble if emitFacade is true.
  326. *
  327. * Included in the event-custom-complex submodule.
  328. *
  329. * @method addTarget
  330. * @param o {EventTarget} the target to add
  331. * @for EventTarget
  332. */
  333. ETProto.addTarget = function(o) {
  334. this._yuievt.targets[Y.stamp(o)] = o;
  335. this._yuievt.hasTargets = true;
  336. };
  337. /**
  338. * Returns an array of bubble targets for this object.
  339. * @method getTargets
  340. * @return EventTarget[]
  341. */
  342. ETProto.getTargets = function() {
  343. return Y.Object.values(this._yuievt.targets);
  344. };
  345. /**
  346. * Removes a bubble target
  347. * @method removeTarget
  348. * @param o {EventTarget} the target to remove
  349. * @for EventTarget
  350. */
  351. ETProto.removeTarget = function(o) {
  352. delete this._yuievt.targets[Y.stamp(o)];
  353. };
  354. /**
  355. * Propagate an event. Requires the event-custom-complex module.
  356. * @method bubble
  357. * @param evt {CustomEvent} the custom event to propagate
  358. * @return {boolean} the aggregated return value from Event.Custom.fire
  359. * @for EventTarget
  360. */
  361. ETProto.bubble = function(evt, args, target, es) {
  362. var targs = this._yuievt.targets, ret = true,
  363. t, type = evt && evt.type, ce, i, bc, ce2,
  364. originalTarget = target || (evt && evt.target) || this,
  365. oldbubble;
  366. if (!evt || ((!evt.stopped) && targs)) {
  367. for (i in targs) {
  368. if (targs.hasOwnProperty(i)) {
  369. t = targs[i];
  370. ce = t.getEvent(type, true);
  371. ce2 = t.getSibling(type, ce);
  372. if (ce2 && !ce) {
  373. ce = t.publish(type);
  374. }
  375. oldbubble = t._yuievt.bubbling;
  376. t._yuievt.bubbling = type;
  377. // if this event was not published on the bubble target,
  378. // continue propagating the event.
  379. if (!ce) {
  380. if (t._yuievt.hasTargets) {
  381. t.bubble(evt, args, originalTarget, es);
  382. }
  383. } else {
  384. ce.sibling = ce2;
  385. // set the original target to that the target payload on the
  386. // facade is correct.
  387. ce.target = originalTarget;
  388. ce.originalTarget = originalTarget;
  389. ce.currentTarget = t;
  390. bc = ce.broadcast;
  391. ce.broadcast = false;
  392. // default publish may not have emitFacade true -- that
  393. // shouldn't be what the implementer meant To Do
  394. ce.emitFacade = true;
  395. ce.stack = es;
  396. ret = ret && ce.fire.apply(ce, args || evt.details || []);
  397. ce.broadcast = bc;
  398. ce.originalTarget = null;
  399. // stopPropagation() was called
  400. if (ce.stopped) {
  401. break;
  402. }
  403. }
  404. t._yuievt.bubbling = oldbubble;
  405. }
  406. }
  407. }
  408. return ret;
  409. };
  410. FACADE = new Y.EventFacade();
  411. FACADE_KEYS = {};
  412. // Flatten whitelist
  413. for (key in FACADE) {
  414. FACADE_KEYS[key] = true;
  415. }
  416. }, '3.4.0' ,{requires:['event-custom-base']});