Recorder.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ux-event-Recorder'>/**
  19. </span> * @extends Ext.ux.event.Driver
  20. * Event recorder.
  21. */
  22. Ext.define('Ext.ux.event.Recorder', function () {
  23. function samePt (pt1, pt2) {
  24. return pt1.x == pt2.x &amp;&amp; pt1.y == pt2.y;
  25. }
  26. return {
  27. extend: 'Ext.ux.event.Driver',
  28. eventsToRecord: function () {
  29. var key = { kind: 'keyboard', modKeys: true, key: true, bubbles: true },
  30. mouse = { kind: 'mouse', button: true, modKeys: true, xy: true, bubbles: true };
  31. return {
  32. keydown: key,
  33. keypress: key,
  34. keyup: key,
  35. mousemove: mouse,
  36. mouseover: mouse,
  37. mouseout: mouse,
  38. click: mouse,
  39. //mousewheel: Ext.apply({ wheel: true }, mouse),
  40. mousedown: mouse,
  41. mouseup: mouse,
  42. scroll: { kind: 'misc', bubbles: false }
  43. };
  44. }(),
  45. ignoreIdRegEx: /ext-gen(?:\d+)/,
  46. constructor: function (config) {
  47. var me = this,
  48. events = config &amp;&amp; config.eventsToRecord;
  49. if (events) {
  50. me.eventsToRecord = Ext.apply(Ext.apply({}, me.eventsToRecord), // duplicate
  51. events); // and merge
  52. delete config.eventsToRecord; // don't smash
  53. }
  54. me.callParent(arguments);
  55. me.addEvents(
  56. <span id='Ext-ux-event-Recorder-event-add'> /**
  57. </span> * @event add
  58. * Fires when an event is added to the recording.
  59. * @param {Ext.ux.event.Recorder} this
  60. * @param {Object} eventDescriptor The event descriptor.
  61. */
  62. 'add',
  63. <span id='Ext-ux-event-Recorder-event-coalesce'> /**
  64. </span> * @event coalesce
  65. * Fires when an event is coalesced. This edits the tail of the recorded
  66. * event list.
  67. * @param {Ext.ux.event.Recorder} this
  68. * @param {Object} eventDescriptor The event descriptor that was coalesced.
  69. */
  70. 'coalesce'
  71. );
  72. me.clear();
  73. me.modKeys = [];
  74. me.attachTo = me.attachTo || window;
  75. },
  76. clear: function () {
  77. this.eventsRecorded = [];
  78. },
  79. coalesce: function (rec) {
  80. var me = this,
  81. events = me.eventsRecorded,
  82. length = events.length,
  83. tail = length &amp;&amp; events[length-1],
  84. tailPrev;
  85. if (!tail) {
  86. return false;
  87. }
  88. if (rec.type == 'mousemove') {
  89. if (tail.type == 'mousemove' &amp;&amp; rec.ts - tail.ts &lt; 200) {
  90. rec.ts = tail.ts;
  91. events[length-1] = rec;
  92. return true;
  93. }
  94. } else if (rec.type == 'click') {
  95. if (length &gt;= 2 &amp;&amp; tail.type == 'mouseup' &amp;&amp;
  96. (tailPrev = events[length-2]).type == 'mousedown') {
  97. if (rec.button == tail.button &amp;&amp; rec.button == tailPrev.button &amp;&amp;
  98. rec.target == tail.target &amp;&amp; rec.target == tailPrev.target &amp;&amp;
  99. samePt(rec, tail) &amp;&amp; samePt(rec, tailPrev) ) {
  100. events.pop(); // remove mouseup
  101. tailPrev.type = 'mduclick';
  102. return true;
  103. }
  104. }
  105. }
  106. return false;
  107. },
  108. getElementXPath: function (el) {
  109. var me = this,
  110. good = false,
  111. xpath = [],
  112. count,
  113. sibling,
  114. t,
  115. tag;
  116. for (t = el; t; t = t.parentNode) {
  117. if (t == me.attachTo.document.body) {
  118. xpath.unshift('~');
  119. good = true;
  120. break;
  121. }
  122. if (t.id &amp;&amp; !me.ignoreIdRegEx.test(t.id)) {
  123. xpath.unshift('#' + t.id);
  124. good = true;
  125. break;
  126. }
  127. for (count = 1, sibling = t; !!(sibling = sibling.previousSibling); ) {
  128. if (sibling.tagName == t.tagName) {
  129. ++count;
  130. }
  131. }
  132. tag = t.tagName.toLowerCase();
  133. if (count &lt; 2) {
  134. xpath.unshift(tag);
  135. } else {
  136. xpath.unshift(tag + '[' + count + ']');
  137. }
  138. }
  139. return good ? xpath.join('/') : null;
  140. },
  141. getRecordedEvents: function () {
  142. return this.eventsRecorded;
  143. },
  144. // DOMNodeInserted
  145. onDomInsert: function (event, target) {
  146. this.watchTree(target);
  147. },
  148. //DOMNodeRemoved
  149. onDomRemove: function (event, target) {
  150. this.unwatchTree(target);
  151. },
  152. onEvent: function (e) {
  153. var me = this,
  154. info = me.eventsToRecord[e.type],
  155. root,
  156. modKeys, elXY,
  157. rec = {
  158. type: e.type,
  159. ts: me.getTimestamp(),
  160. target: me.getElementXPath(e.target)
  161. },
  162. xy;
  163. if (!rec.target) {
  164. return;
  165. }
  166. root = e.target.ownerDocument;
  167. root = root.defaultView || root.parentWindow; // Standards || IE
  168. if (root !== me.attachTo) {
  169. return;
  170. }
  171. if (info.xy) {
  172. xy = e.getXY();
  173. if (rec.target) {
  174. elXY = Ext.fly(e.getTarget()).getXY();
  175. xy[0] -= elXY[0];
  176. xy[1] -= elXY[1];
  177. }
  178. rec.x = xy[0];
  179. rec.y = xy[1];
  180. }
  181. if (info.button) {
  182. rec.button = e.button;
  183. }
  184. if (info.wheel) {
  185. rec.wheel = e.getWheelDelta();
  186. }
  187. if (info.modKeys) {
  188. me.modKeys[0] = e.altKey ? 'A' : '';
  189. me.modKeys[1] = e.ctrlKey ? 'C' : '';
  190. me.modKeys[2] = e.metaKey ? 'M' : '';
  191. me.modKeys[3] = e.shiftKey ? 'S' : '';
  192. modKeys = me.modKeys.join('');
  193. if (modKeys) {
  194. rec.modKeys = modKeys;
  195. }
  196. }
  197. if (info.key) {
  198. rec.charCode = e.getCharCode();
  199. rec.keyCode = e.getKey();
  200. }
  201. if (me.coalesce(rec)) {
  202. me.fireEvent('coalesce', me, rec);
  203. } else {
  204. me.eventsRecorded.push(rec);
  205. me.fireEvent('add', me, rec);
  206. }
  207. },
  208. onStart: function () {
  209. var me = this,
  210. on = {
  211. DOMNodeInserted: me.onDomInsert,
  212. DOMNodeRemoved: me.onDomRemove,
  213. scope: me
  214. },
  215. nonBubbleEvents = (me.nonBubbleEvents = {}),
  216. ddm = me.attachTo.Ext.dd.DragDropManager,
  217. evproto = me.attachTo.Ext.EventObjectImpl.prototype;
  218. me.watchingNodes = {};
  219. Ext.Object.each(me.eventsToRecord, function (name, value) {
  220. if (value) {
  221. if (value.bubbles) {
  222. on[name] = me.onEvent;
  223. } else {
  224. nonBubbleEvents[name] = value;
  225. }
  226. }
  227. });
  228. me.ddmStopEvent = ddm.stopEvent;
  229. ddm.stopEvent = Ext.Function.createSequence(ddm.stopEvent, function (e) {
  230. me.onEvent(e);
  231. });
  232. me.evStopEvent = evproto.stopEvent;
  233. evproto.stopEvent = Ext.Function.createSequence(evproto.stopEvent, function () {
  234. me.onEvent(this);
  235. });
  236. var body = me.attachTo.Ext.getBody();
  237. body.on(on);
  238. me.watchTree(body.dom);
  239. },
  240. onStop: function () {
  241. var me = this,
  242. body = me.attachTo.Ext.getBody();
  243. Ext.Object.each(me.eventsToRecord, function (name, value) {
  244. if (value) {
  245. body.un(name, me.onEvent, me);
  246. }
  247. });
  248. me.attachTo.Ext.dd.DragDropManager.stopEvent = me.ddmStopEvent;
  249. me.attachTo.Ext.EventObjectImpl.prototype.stopEvent = me.evStopEvent;
  250. me.unwatchTree(body.dom);
  251. },
  252. watchTree: function (root) {
  253. if (root.nodeType != 1) {
  254. return; // only ELEMENT_NODE's please...
  255. }
  256. var me = this,
  257. id = (root.tagName == 'BODY') ? '$' : root.id,
  258. watchingNodes = me.watchingNodes;
  259. if (id &amp;&amp; !watchingNodes[id]) {
  260. var on = {
  261. scope: me
  262. };
  263. Ext.Object.each(me.nonBubbleEvents, function (name, value) {
  264. if (value) {
  265. on[name] = me.onEvent;
  266. }
  267. });
  268. me.attachTo.Ext.fly(root).on(on);
  269. watchingNodes[id] = true;
  270. console.log('watch '+root.tagName+'#'+id);
  271. }
  272. Ext.each(root.childNodes, me.watchTree, me);
  273. },
  274. unwatchTree: function (root) {
  275. if (root.nodeType != 1) {
  276. return; // only ELEMENT_NODE's please...
  277. }
  278. var me = this,
  279. id = (root.tagName == 'BODY') ? '$' : root.id,
  280. watchingNodes = me.watchingNodes;
  281. if (id &amp;&amp; !watchingNodes[id]) {
  282. Ext.Object.each(me.nonBubbleEvents, function (name, value) {
  283. me.attachTo.Ext.fly(root).un(name, me.onEvent, me);
  284. });
  285. delete watchingNodes[id];
  286. console.log('unwatch '+root.tagName+'#'+id);
  287. }
  288. Ext.each(root.childNodes, me.unwatchTree, me);
  289. }
  290. };
  291. }());
  292. </pre>
  293. </body>
  294. </html>