Player.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**
  2. * This class manages the playback of an array of "event descriptors". For details on the
  3. * contents of an "event descriptor", see {@link Ext.ux.event.Recorder}. The events recorded by the
  4. * {@link Ext.ux.event.Recorder} class are designed to serve as input for this class.
  5. *
  6. * The simplest use of this class is to instantiate it with an {@link #eventQueue} and call
  7. * {@link #method-start}. Like so:
  8. *
  9. * var player = Ext.create('Ext.ux.event.Player', {
  10. * eventQueue: [ ... ],
  11. * speed: 2, // play at 2x speed
  12. * listeners: {
  13. * stop: function () {
  14. * player = null; // all done
  15. * }
  16. * }
  17. * });
  18. *
  19. * player.start();
  20. *
  21. * A more complex use would be to incorporate keyframe generation after playing certain
  22. * events.
  23. *
  24. * var player = Ext.create('Ext.ux.event.Player', {
  25. * eventQueue: [ ... ],
  26. * keyFrameEvents: {
  27. * click: true
  28. * },
  29. * listeners: {
  30. * stop: function () {
  31. * // play has completed... probably time for another keyframe...
  32. * player = null;
  33. * },
  34. * keyframe: onKeyFrame
  35. * }
  36. * });
  37. *
  38. * player.start();
  39. *
  40. * If a keyframe can be handled immediately (synchronously), the listener would be:
  41. *
  42. * function onKeyFrame () {
  43. * handleKeyFrame();
  44. * }
  45. *
  46. * If the keyframe event is always handled asynchronously, then the event listener is only
  47. * a bit more:
  48. *
  49. * function onKeyFrame (p, eventDescriptor) {
  50. * eventDescriptor.defer(); // pause event playback...
  51. *
  52. * handleKeyFrame(function () {
  53. * eventDescriptor.finish(); // ...resume event playback
  54. * });
  55. * }
  56. *
  57. * Finally, if the keyframe could be either handled synchronously or asynchronously (perhaps
  58. * differently by browser), a slightly more complex listener is required.
  59. *
  60. * function onKeyFrame (p, eventDescriptor) {
  61. * var async;
  62. *
  63. * handleKeyFrame(function () {
  64. * // either this callback is being called immediately by handleKeyFrame (in
  65. * // which case async is undefined) or it is being called later (in which case
  66. * // async will be true).
  67. *
  68. * if (async) {
  69. * eventDescriptor.finish();
  70. * } else {
  71. * async = false;
  72. * }
  73. * });
  74. *
  75. * // either the callback was called (and async is now false) or it was not
  76. * // called (and async remains undefined).
  77. *
  78. * if (async !== false) {
  79. * eventDescriptor.defer();
  80. * async = true; // let the callback know that we have gone async
  81. * }
  82. * }
  83. */
  84. Ext.define('Ext.ux.event.Player', {
  85. extend: 'Ext.ux.event.Driver',
  86. /**
  87. * @cfg {Array} eventQueue The event queue to playback. This must be provided before
  88. * the {@link #method-start} method is called.
  89. */
  90. /**
  91. * @cfg {Object} keyFrameEvents An object that describes the events that should generate
  92. * keyframe events. For example, `{ click: true }` would generate keyframe events after
  93. * each `click` event.
  94. */
  95. keyFrameEvents: {
  96. click: true
  97. },
  98. /**
  99. * @cfg {Boolean} pauseForAnimations True to pause event playback during animations, false
  100. * to ignore animations. Default is true.
  101. */
  102. pauseForAnimations: true,
  103. /**
  104. * @cfg {Number} speed The playback speed multiplier. Default is 1.0 (to playback at the
  105. * recorded speed). A value of 2 would playback at 2x speed.
  106. */
  107. speed: 1.0,
  108. stallTime: 0,
  109. tagPathRegEx: /(\w+)(?:\[(\d+)\])?/,
  110. constructor: function (config) {
  111. var me = this;
  112. me.callParent(arguments);
  113. me.addEvents(
  114. /**
  115. * @event beforeplay
  116. * Fires before an event is played.
  117. * @param {Ext.ux.event.Player} this
  118. * @param {Object} eventDescriptor The event descriptor about to be played.
  119. */
  120. 'beforeplay',
  121. /**
  122. * @event keyframe
  123. * Fires when this player reaches a keyframe. Typically, this is after events
  124. * like `click` are injected and any resulting animations have been completed.
  125. * @param {Ext.ux.event.Player} this
  126. * @param {Object} eventDescriptor The keyframe event descriptor.
  127. */
  128. 'keyframe'
  129. );
  130. me.eventObject = new Ext.EventObjectImpl();
  131. me.timerFn = function () {
  132. me.onTick();
  133. };
  134. me.attachTo = me.attachTo || window;
  135. },
  136. /**
  137. * Returns the element given is XPath-like description.
  138. * @param {String} xpath The XPath-like description of the element.
  139. * @return {HTMLElement}
  140. */
  141. getElementFromXPath: function (xpath) {
  142. var me = this,
  143. parts = xpath.split('/'),
  144. regex = me.tagPathRegEx,
  145. i, n, m, count, tag, child,
  146. el = me.attachTo.document;
  147. el = (parts[0] == '~') ? el.body
  148. : el.getElementById(parts[0].substring(1)); // remove '#'
  149. for (i = 1, n = parts.length; el && i < n; ++i) {
  150. m = regex.exec(parts[i]);
  151. count = m[2] ? parseInt(m[2], 10) : 1;
  152. tag = m[1].toUpperCase();
  153. for (child = el.firstChild; child; child = child.nextSibling) {
  154. if (child.tagName == tag) {
  155. if (count == 1) {
  156. break;
  157. }
  158. --count;
  159. }
  160. }
  161. el = child;
  162. }
  163. return el;
  164. },
  165. getTimeIndex: function () {
  166. var t = this.getTimestamp() - this.stallTime;
  167. return t * this.speed;
  168. },
  169. makeToken: function (eventDescriptor, signal) {
  170. var me = this,
  171. t0;
  172. eventDescriptor[signal] = true;
  173. eventDescriptor.defer = function () {
  174. eventDescriptor[signal] = false;
  175. t0 = me.getTime();
  176. };
  177. eventDescriptor.finish = function () {
  178. eventDescriptor[signal] = true;
  179. me.stallTime += me.getTime() - t0;
  180. me.schedule();
  181. };
  182. },
  183. /**
  184. * This method is called after an event has been played to prepare for the next event.
  185. * @param {Object} eventDescriptor The descriptor of the event just played.
  186. */
  187. nextEvent: function (eventDescriptor) {
  188. var me = this,
  189. index = ++me.queueIndex;
  190. // keyframe events are inserted after a keyFrameEvent is played.
  191. if (me.keyFrameEvents[eventDescriptor.type]) {
  192. Ext.Array.insert(me.eventQueue, index, [
  193. { keyframe: true, ts: eventDescriptor.ts }
  194. ]);
  195. }
  196. },
  197. /**
  198. * This method returns the event descriptor at the front of the queue. This does not
  199. * dequeue the event. Repeated calls return the same object (until {@link #nextEvent}
  200. * is called).
  201. */
  202. peekEvent: function () {
  203. var me = this,
  204. queue = me.eventQueue,
  205. index = me.queueIndex,
  206. eventDescriptor = queue[index],
  207. type = eventDescriptor && eventDescriptor.type,
  208. tmp;
  209. if (type == 'mduclick') {
  210. tmp = [
  211. Ext.applyIf({ type: 'mousedown' }, eventDescriptor),
  212. Ext.applyIf({ type: 'mouseup' }, eventDescriptor),
  213. Ext.applyIf({ type: 'click' }, eventDescriptor)
  214. ];
  215. me.replaceEvent(index, tmp);
  216. }
  217. return queue[index] || null;
  218. },
  219. replaceEvent: function (index, events) {
  220. for (var t, i = 0, n = events.length; i < n; ++i) {
  221. if (i) {
  222. t = events[i-1];
  223. delete t.afterplay;
  224. delete t.screenshot;
  225. delete events[i].beforeplay;
  226. }
  227. }
  228. Ext.Array.replace(this.eventQueue, index, 1, events);
  229. },
  230. /**
  231. * This method dequeues and injects events until it has arrived at the time index. If
  232. * no events are ready (based on the time index), this method does nothing.
  233. * @return {Boolean} True if there is more to do; false if not (at least for now).
  234. */
  235. processEvents: function () {
  236. var me = this,
  237. animations = me.pauseForAnimations && me.attachTo.Ext.fx.Manager.items,
  238. eventDescriptor;
  239. while ((eventDescriptor = me.peekEvent()) !== null) {
  240. if (animations && animations.getCount()) {
  241. return true;
  242. }
  243. if (eventDescriptor.keyframe) {
  244. if (!me.processKeyFrame(eventDescriptor)) {
  245. return false;
  246. }
  247. me.nextEvent(eventDescriptor);
  248. } else if (eventDescriptor.ts <= me.getTimeIndex() &&
  249. me.fireEvent('beforeplay', me, eventDescriptor) !== false &&
  250. me.playEvent(eventDescriptor)) {
  251. if(window.__x && eventDescriptor.screenshot) {
  252. __x.poll.sendSyncRequest({cmd: 'screenshot'});
  253. }
  254. me.nextEvent(eventDescriptor);
  255. } else {
  256. return true;
  257. }
  258. }
  259. me.stop();
  260. return false;
  261. },
  262. /**
  263. * This method is called when a keyframe is reached. This will fire the keyframe event.
  264. * If the keyframe has been handled, true is returned. Otherwise, false is returned.
  265. * @param {Object} The event descriptor of the keyframe.
  266. * @return {Boolean} True if the keyframe was handled, false if not.
  267. */
  268. processKeyFrame: function (eventDescriptor) {
  269. var me = this;
  270. // only fire keyframe event (and setup the eventDescriptor) once...
  271. if (!eventDescriptor.defer) {
  272. me.makeToken(eventDescriptor, 'done');
  273. me.fireEvent('keyframe', me, eventDescriptor);
  274. }
  275. return eventDescriptor.done;
  276. },
  277. /**
  278. * Called to inject the given event on the specified target.
  279. * @param {HTMLElement} target The target of the event.
  280. * @param {Ext.EventObject} The event to inject.
  281. */
  282. injectEvent: function (target, event) {
  283. event.injectEvent(target);
  284. },
  285. playEvent: function (eventDescriptor) {
  286. var me = this,
  287. target = me.getElementFromXPath(eventDescriptor.target),
  288. event;
  289. if (!target) {
  290. // not present (yet)... wait for element present...
  291. // TODO - need a timeout here
  292. return false;
  293. }
  294. if (!me.playEventHook(eventDescriptor, 'beforeplay')) {
  295. return false;
  296. }
  297. if (!eventDescriptor.injected) {
  298. eventDescriptor.injected = true;
  299. event = me.translateEvent(eventDescriptor, target);
  300. me.injectEvent(target, event);
  301. }
  302. return me.playEventHook(eventDescriptor, 'afterplay');
  303. },
  304. playEventHook: function (eventDescriptor, hookName) {
  305. var me = this,
  306. doneName = hookName + '.done',
  307. firedName = hookName + '.fired',
  308. hook = eventDescriptor[hookName];
  309. if (hook && !eventDescriptor[doneName]) {
  310. if (!eventDescriptor[firedName]) {
  311. eventDescriptor[firedName] = true;
  312. me.makeToken(eventDescriptor, doneName);
  313. me.eventScope[hook](eventDescriptor);
  314. }
  315. return false;
  316. }
  317. return true;
  318. },
  319. schedule: function () {
  320. var me = this;
  321. if (!me.timer) {
  322. me.timer = setTimeout(me.timerFn, 10);
  323. }
  324. },
  325. translateEvent: function (eventDescriptor, target) {
  326. var me = this,
  327. event = me.eventObject,
  328. modKeys = eventDescriptor.modKeys || '',
  329. xy;
  330. if ('x' in eventDescriptor) {
  331. event.xy = xy = Ext.fly(target).getXY();
  332. xy[0] += eventDescriptor.x;
  333. xy[1] += eventDescriptor.y;
  334. }
  335. if ('wheel' in eventDescriptor) {
  336. // see getWheelDelta
  337. }
  338. event.type = eventDescriptor.type;
  339. event.button = eventDescriptor.button;
  340. event.altKey = modKeys.indexOf('A') > 0;
  341. event.ctrlKey = modKeys.indexOf('C') > 0;
  342. event.metaKey = modKeys.indexOf('M') > 0;
  343. event.shiftKey = modKeys.indexOf('S') > 0;
  344. return event;
  345. },
  346. //---------------------------------
  347. // Driver overrides
  348. onStart: function () {
  349. var me = this;
  350. me.queueIndex = 0;
  351. me.schedule();
  352. },
  353. onStop: function () {
  354. var me = this;
  355. if (me.timer) {
  356. clearTimeout(me.timer);
  357. me.timer = null;
  358. }
  359. if (window.__x) {
  360. __x.poll.sendSyncRequest({cmd: 'finish'});
  361. }
  362. },
  363. //---------------------------------
  364. onTick: function () {
  365. var me = this;
  366. me.timer = null;
  367. if (me.processEvents()) {
  368. me.schedule();
  369. }
  370. }
  371. });