Recorder.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. Ext.define('Ext.ux.event.Recorder', {
  10. extend: 'Ext.ux.event.Driver',
  11. eventsToRecord: function () {
  12. var //key = { modKeys: true, key: true },
  13. mouse = { button: true, modKeys: true, xy: true };
  14. return {
  15. //keydown: key,
  16. //keypress: key,
  17. //keyup: key,
  18. //mousemove: mouse,
  19. //mouseover: mouse,
  20. //mouseout: mouse,
  21. click: mouse,
  22. //mousewheel: Ext.apply({ wheel: true }, mouse),
  23. mousedown: mouse,
  24. mouseup: mouse
  25. };
  26. }(),
  27. ignoreIdRegEx: /ext-gen(?:\d+)/,
  28. constructor: function (config) {
  29. var me = this,
  30. events = config && config.eventsToRecord;
  31. if (events) {
  32. me.eventsToRecord = Ext.apply(Ext.apply({}, me.eventsToRecord), // duplicate
  33. events); // and merge
  34. delete config.eventsToRecord; // don't smash
  35. }
  36. me.callParent(arguments);
  37. me.clear();
  38. me.modKeys = [];
  39. },
  40. clear: function () {
  41. this.events = [];
  42. },
  43. coalesce: function (rec) {
  44. var me = this,
  45. events = me.events,
  46. length = events.length,
  47. tail = length && events[length-1];
  48. if (tail && tail.type == 'mousemove' && rec.type == 'mousemove') {
  49. if (rec.ts - tail.ts < 200) {
  50. events[length-1] = rec;
  51. return true;
  52. }
  53. }
  54. return false;
  55. },
  56. getElementXPath: function (el) {
  57. var me = this,
  58. good = false,
  59. xpath = [],
  60. count,
  61. sibling,
  62. t,
  63. tag;
  64. for (t = el; t; t = t.parentNode) {
  65. if (t == document.body) {
  66. xpath.unshift('~');
  67. good = true;
  68. break;
  69. }
  70. if (t.id && !me.ignoreIdRegEx.test(t.id)) {
  71. xpath.unshift('#' + t.id);
  72. good = true;
  73. break;
  74. }
  75. for (count = 1, sibling = t; !!(sibling = sibling.previousSibling); ) {
  76. if (sibling.tagName == t.tagName) {
  77. ++count;
  78. }
  79. }
  80. tag = t.tagName.toLowerCase();
  81. if (count < 2) {
  82. xpath.unshift(tag);
  83. } else {
  84. xpath.unshift(tag + '[' + count + ']');
  85. }
  86. }
  87. return good ? xpath.join('/') : null;
  88. },
  89. onEvent: function (e) {
  90. var me = this,
  91. info = me.eventsToRecord[e.type],
  92. modKeys,
  93. rec = {
  94. type: e.type,
  95. ts: me.getTimestamp(),
  96. target: me.getElementXPath(e.target)
  97. };
  98. if (!rec.target) {
  99. return;
  100. }
  101. if (info.xy) {
  102. rec.xy = e.getXY();
  103. }
  104. if (info.button) {
  105. rec.button = e.button;
  106. }
  107. if (info.wheel) {
  108. rec.wheel = e.getWheelDelta();
  109. }
  110. if (info.modKeys) {
  111. me.modKeys[0] = e.altKey ? 'A' : '';
  112. me.modKeys[1] = e.ctrlKey ? 'C' : '';
  113. me.modKeys[2] = e.metaKey ? 'M' : '';
  114. me.modKeys[3] = e.shiftKey ? 'S' : '';
  115. modKeys = me.modKeys.join('');
  116. if (modKeys) {
  117. rec.modKeys = modKeys;
  118. }
  119. }
  120. if (info.key) {
  121. rec.charCode = e.getCharCode();
  122. rec.keyCode = e.getKey();
  123. }
  124. if (!me.coalesce(rec)) {
  125. me.events.push(rec);
  126. }
  127. Ext.log('record: ' + Ext.encode(rec));
  128. },
  129. onStart: function () {
  130. var me = this,
  131. on = {
  132. scope: me
  133. },
  134. ddm = Ext.dd.DragDropManager,
  135. evproto = Ext.EventObjectImpl.prototype;
  136. Ext.Object.each(me.eventsToRecord, function (name, value) {
  137. if (value) {
  138. on[name] = me.onEvent;
  139. }
  140. });
  141. me.ddmStopEvent = ddm.stopEvent;
  142. ddm.stopEvent = Ext.Function.createSequence(ddm.stopEvent, function (e) {
  143. me.onEvent(e);
  144. });
  145. me.evStopEvent = evproto.stopEvent;
  146. evproto.stopEvent = Ext.Function.createSequence(evproto.stopEvent, function () {
  147. me.onEvent(this);
  148. });
  149. Ext.getBody().on(on);
  150. },
  151. onStop: function () {
  152. var me = this,
  153. body = Ext.getBody();
  154. Ext.Object.each(me.eventsToRecord, function (name, value) {
  155. if (value) {
  156. body.un(name, me.onEvent, me);
  157. }
  158. });
  159. Ext.dd.DragDropManager.stopEvent = me.ddmStopEvent;
  160. Ext.EventObjectImpl.prototype.stopEvent = me.evStopEvent;
  161. }
  162. });