Built files from Bizgaze WebServer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

keybindings.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  3. var Keybindings = function Keybindings(table) {
  4. this.table = table; //hold Tabulator object
  5. this.watchKeys = null;
  6. this.pressedKeys = null;
  7. this.keyupBinding = false;
  8. this.keydownBinding = false;
  9. };
  10. Keybindings.prototype.initialize = function () {
  11. var bindings = this.table.options.keybindings,
  12. mergedBindings = {};
  13. this.watchKeys = {};
  14. this.pressedKeys = [];
  15. if (bindings !== false) {
  16. for (var key in this.bindings) {
  17. mergedBindings[key] = this.bindings[key];
  18. }
  19. if (Object.keys(bindings).length) {
  20. for (var _key in bindings) {
  21. mergedBindings[_key] = bindings[_key];
  22. }
  23. }
  24. this.mapBindings(mergedBindings);
  25. this.bindEvents();
  26. }
  27. };
  28. Keybindings.prototype.mapBindings = function (bindings) {
  29. var _this = this;
  30. var self = this;
  31. var _loop = function _loop(key) {
  32. if (_this.actions[key]) {
  33. if (bindings[key]) {
  34. if (_typeof(bindings[key]) !== "object") {
  35. bindings[key] = [bindings[key]];
  36. }
  37. bindings[key].forEach(function (binding) {
  38. self.mapBinding(key, binding);
  39. });
  40. }
  41. } else {
  42. console.warn("Key Binding Error - no such action:", key);
  43. }
  44. };
  45. for (var key in bindings) {
  46. _loop(key);
  47. }
  48. };
  49. Keybindings.prototype.mapBinding = function (action, symbolsList) {
  50. var self = this;
  51. var binding = {
  52. action: this.actions[action],
  53. keys: [],
  54. ctrl: false,
  55. shift: false,
  56. meta: false
  57. };
  58. var symbols = symbolsList.toString().toLowerCase().split(" ").join("").split("+");
  59. symbols.forEach(function (symbol) {
  60. switch (symbol) {
  61. case "ctrl":
  62. binding.ctrl = true;
  63. break;
  64. case "shift":
  65. binding.shift = true;
  66. break;
  67. case "meta":
  68. binding.meta = true;
  69. break;
  70. default:
  71. symbol = parseInt(symbol);
  72. binding.keys.push(symbol);
  73. if (!self.watchKeys[symbol]) {
  74. self.watchKeys[symbol] = [];
  75. }
  76. self.watchKeys[symbol].push(binding);
  77. }
  78. });
  79. };
  80. Keybindings.prototype.bindEvents = function () {
  81. var self = this;
  82. this.keyupBinding = function (e) {
  83. var code = e.keyCode;
  84. var bindings = self.watchKeys[code];
  85. if (bindings) {
  86. self.pressedKeys.push(code);
  87. bindings.forEach(function (binding) {
  88. self.checkBinding(e, binding);
  89. });
  90. }
  91. };
  92. this.keydownBinding = function (e) {
  93. var code = e.keyCode;
  94. var bindings = self.watchKeys[code];
  95. if (bindings) {
  96. var index = self.pressedKeys.indexOf(code);
  97. if (index > -1) {
  98. self.pressedKeys.splice(index, 1);
  99. }
  100. }
  101. };
  102. this.table.element.addEventListener("keydown", this.keyupBinding);
  103. this.table.element.addEventListener("keyup", this.keydownBinding);
  104. };
  105. Keybindings.prototype.clearBindings = function () {
  106. if (this.keyupBinding) {
  107. this.table.element.removeEventListener("keydown", this.keyupBinding);
  108. }
  109. if (this.keydownBinding) {
  110. this.table.element.removeEventListener("keyup", this.keydownBinding);
  111. }
  112. };
  113. Keybindings.prototype.checkBinding = function (e, binding) {
  114. var self = this,
  115. match = true;
  116. if (e.ctrlKey == binding.ctrl && e.shiftKey == binding.shift && e.metaKey == binding.meta) {
  117. binding.keys.forEach(function (key) {
  118. var index = self.pressedKeys.indexOf(key);
  119. if (index == -1) {
  120. match = false;
  121. }
  122. });
  123. if (match) {
  124. binding.action.call(self, e);
  125. }
  126. return true;
  127. }
  128. return false;
  129. };
  130. //default bindings
  131. Keybindings.prototype.bindings = {
  132. navPrev: "shift + 9",
  133. navNext: 9,
  134. navUp: 38,
  135. navDown: 40,
  136. scrollPageUp: 33,
  137. scrollPageDown: 34,
  138. scrollToStart: 36,
  139. scrollToEnd: 35,
  140. undo: "ctrl + 90",
  141. redo: "ctrl + 89",
  142. copyToClipboard: "ctrl + 67"
  143. };
  144. //default actions
  145. Keybindings.prototype.actions = {
  146. keyBlock: function keyBlock(e) {
  147. e.stopPropagation();
  148. e.preventDefault();
  149. },
  150. scrollPageUp: function scrollPageUp(e) {
  151. var rowManager = this.table.rowManager,
  152. newPos = rowManager.scrollTop - rowManager.height,
  153. scrollMax = rowManager.element.scrollHeight;
  154. e.preventDefault();
  155. if (rowManager.displayRowsCount) {
  156. if (newPos >= 0) {
  157. rowManager.element.scrollTop = newPos;
  158. } else {
  159. rowManager.scrollToRow(rowManager.getDisplayRows()[0]);
  160. }
  161. }
  162. this.table.element.focus();
  163. },
  164. scrollPageDown: function scrollPageDown(e) {
  165. var rowManager = this.table.rowManager,
  166. newPos = rowManager.scrollTop + rowManager.height,
  167. scrollMax = rowManager.element.scrollHeight;
  168. e.preventDefault();
  169. if (rowManager.displayRowsCount) {
  170. if (newPos <= scrollMax) {
  171. rowManager.element.scrollTop = newPos;
  172. } else {
  173. rowManager.scrollToRow(rowManager.getDisplayRows()[rowManager.displayRowsCount - 1]);
  174. }
  175. }
  176. this.table.element.focus();
  177. },
  178. scrollToStart: function scrollToStart(e) {
  179. var rowManager = this.table.rowManager;
  180. e.preventDefault();
  181. if (rowManager.displayRowsCount) {
  182. rowManager.scrollToRow(rowManager.getDisplayRows()[0]);
  183. }
  184. this.table.element.focus();
  185. },
  186. scrollToEnd: function scrollToEnd(e) {
  187. var rowManager = this.table.rowManager;
  188. e.preventDefault();
  189. if (rowManager.displayRowsCount) {
  190. rowManager.scrollToRow(rowManager.getDisplayRows()[rowManager.displayRowsCount - 1]);
  191. }
  192. this.table.element.focus();
  193. },
  194. navPrev: function navPrev(e) {
  195. var cell = false;
  196. if (this.table.modExists("edit")) {
  197. cell = this.table.modules.edit.currentCell;
  198. if (cell) {
  199. e.preventDefault();
  200. cell.nav().prev();
  201. }
  202. }
  203. },
  204. navNext: function navNext(e) {
  205. var cell = false;
  206. var newRow = this.table.options.tabEndNewRow;
  207. var nav;
  208. if (this.table.modExists("edit")) {
  209. cell = this.table.modules.edit.currentCell;
  210. if (cell) {
  211. e.preventDefault();
  212. nav = cell.nav();
  213. if (!nav.next()) {
  214. if (newRow) {
  215. cell.getElement().firstChild.blur();
  216. if (newRow === true) {
  217. newRow = this.table.addRow({});
  218. } else {
  219. if (typeof newRow == "function") {
  220. newRow = this.table.addRow(newRow(cell.row.getComponent()));
  221. } else {
  222. newRow = this.table.addRow(newRow);
  223. }
  224. }
  225. newRow.then(function () {
  226. setTimeout(function () {
  227. nav.next();
  228. });
  229. });
  230. }
  231. }
  232. }
  233. }
  234. },
  235. navLeft: function navLeft(e) {
  236. var cell = false;
  237. if (this.table.modExists("edit")) {
  238. cell = this.table.modules.edit.currentCell;
  239. if (cell) {
  240. e.preventDefault();
  241. cell.nav().left();
  242. }
  243. }
  244. },
  245. navRight: function navRight(e) {
  246. var cell = false;
  247. if (this.table.modExists("edit")) {
  248. cell = this.table.modules.edit.currentCell;
  249. if (cell) {
  250. e.preventDefault();
  251. cell.nav().right();
  252. }
  253. }
  254. },
  255. navUp: function navUp(e) {
  256. var cell = false;
  257. if (this.table.modExists("edit")) {
  258. cell = this.table.modules.edit.currentCell;
  259. if (cell) {
  260. e.preventDefault();
  261. cell.nav().up();
  262. }
  263. }
  264. },
  265. navDown: function navDown(e) {
  266. var cell = false;
  267. if (this.table.modExists("edit")) {
  268. cell = this.table.modules.edit.currentCell;
  269. if (cell) {
  270. e.preventDefault();
  271. cell.nav().down();
  272. }
  273. }
  274. },
  275. undo: function undo(e) {
  276. var cell = false;
  277. if (this.table.options.history && this.table.modExists("history") && this.table.modExists("edit")) {
  278. cell = this.table.modules.edit.currentCell;
  279. if (!cell) {
  280. e.preventDefault();
  281. this.table.modules.history.undo();
  282. }
  283. }
  284. },
  285. redo: function redo(e) {
  286. var cell = false;
  287. if (this.table.options.history && this.table.modExists("history") && this.table.modExists("edit")) {
  288. cell = this.table.modules.edit.currentCell;
  289. if (!cell) {
  290. e.preventDefault();
  291. this.table.modules.history.redo();
  292. }
  293. }
  294. },
  295. copyToClipboard: function copyToClipboard(e) {
  296. if (!this.table.modules.edit.currentCell) {
  297. if (this.table.modExists("clipboard", true)) {
  298. this.table.modules.clipboard.copy(false, true);
  299. }
  300. }
  301. }
  302. };
  303. Tabulator.prototype.registerModule("keybindings", Keybindings);