123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- 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; };
-
- /* Tabulator v4.6.3 (c) Oliver Folkerd */
-
- var Keybindings = function Keybindings(table) {
- this.table = table; //hold Tabulator object
- this.watchKeys = null;
- this.pressedKeys = null;
- this.keyupBinding = false;
- this.keydownBinding = false;
- };
-
- Keybindings.prototype.initialize = function () {
- var bindings = this.table.options.keybindings,
- mergedBindings = {};
-
- this.watchKeys = {};
- this.pressedKeys = [];
-
- if (bindings !== false) {
-
- for (var key in this.bindings) {
- mergedBindings[key] = this.bindings[key];
- }
-
- if (Object.keys(bindings).length) {
-
- for (var _key in bindings) {
- mergedBindings[_key] = bindings[_key];
- }
- }
-
- this.mapBindings(mergedBindings);
- this.bindEvents();
- }
- };
-
- Keybindings.prototype.mapBindings = function (bindings) {
- var _this = this;
-
- var self = this;
-
- var _loop = function _loop(key) {
-
- if (_this.actions[key]) {
-
- if (bindings[key]) {
-
- if (_typeof(bindings[key]) !== "object") {
- bindings[key] = [bindings[key]];
- }
-
- bindings[key].forEach(function (binding) {
- self.mapBinding(key, binding);
- });
- }
- } else {
- console.warn("Key Binding Error - no such action:", key);
- }
- };
-
- for (var key in bindings) {
- _loop(key);
- }
- };
-
- Keybindings.prototype.mapBinding = function (action, symbolsList) {
- var self = this;
-
- var binding = {
- action: this.actions[action],
- keys: [],
- ctrl: false,
- shift: false,
- meta: false
- };
-
- var symbols = symbolsList.toString().toLowerCase().split(" ").join("").split("+");
-
- symbols.forEach(function (symbol) {
- switch (symbol) {
- case "ctrl":
- binding.ctrl = true;
- break;
-
- case "shift":
- binding.shift = true;
- break;
-
- case "meta":
- binding.meta = true;
- break;
-
- default:
- symbol = parseInt(symbol);
- binding.keys.push(symbol);
-
- if (!self.watchKeys[symbol]) {
- self.watchKeys[symbol] = [];
- }
-
- self.watchKeys[symbol].push(binding);
- }
- });
- };
-
- Keybindings.prototype.bindEvents = function () {
- var self = this;
-
- this.keyupBinding = function (e) {
- var code = e.keyCode;
- var bindings = self.watchKeys[code];
-
- if (bindings) {
-
- self.pressedKeys.push(code);
-
- bindings.forEach(function (binding) {
- self.checkBinding(e, binding);
- });
- }
- };
-
- this.keydownBinding = function (e) {
- var code = e.keyCode;
- var bindings = self.watchKeys[code];
-
- if (bindings) {
-
- var index = self.pressedKeys.indexOf(code);
-
- if (index > -1) {
- self.pressedKeys.splice(index, 1);
- }
- }
- };
-
- this.table.element.addEventListener("keydown", this.keyupBinding);
-
- this.table.element.addEventListener("keyup", this.keydownBinding);
- };
-
- Keybindings.prototype.clearBindings = function () {
- if (this.keyupBinding) {
- this.table.element.removeEventListener("keydown", this.keyupBinding);
- }
-
- if (this.keydownBinding) {
- this.table.element.removeEventListener("keyup", this.keydownBinding);
- }
- };
-
- Keybindings.prototype.checkBinding = function (e, binding) {
- var self = this,
- match = true;
-
- if (e.ctrlKey == binding.ctrl && e.shiftKey == binding.shift && e.metaKey == binding.meta) {
- binding.keys.forEach(function (key) {
- var index = self.pressedKeys.indexOf(key);
-
- if (index == -1) {
- match = false;
- }
- });
-
- if (match) {
- binding.action.call(self, e);
- }
-
- return true;
- }
-
- return false;
- };
-
- //default bindings
- Keybindings.prototype.bindings = {
- navPrev: "shift + 9",
- navNext: 9,
- navUp: 38,
- navDown: 40,
- scrollPageUp: 33,
- scrollPageDown: 34,
- scrollToStart: 36,
- scrollToEnd: 35,
- undo: "ctrl + 90",
- redo: "ctrl + 89",
- copyToClipboard: "ctrl + 67"
- };
-
- //default actions
- Keybindings.prototype.actions = {
- keyBlock: function keyBlock(e) {
- e.stopPropagation();
- e.preventDefault();
- },
- scrollPageUp: function scrollPageUp(e) {
- var rowManager = this.table.rowManager,
- newPos = rowManager.scrollTop - rowManager.height,
- scrollMax = rowManager.element.scrollHeight;
-
- e.preventDefault();
-
- if (rowManager.displayRowsCount) {
- if (newPos >= 0) {
- rowManager.element.scrollTop = newPos;
- } else {
- rowManager.scrollToRow(rowManager.getDisplayRows()[0]);
- }
- }
-
- this.table.element.focus();
- },
- scrollPageDown: function scrollPageDown(e) {
- var rowManager = this.table.rowManager,
- newPos = rowManager.scrollTop + rowManager.height,
- scrollMax = rowManager.element.scrollHeight;
-
- e.preventDefault();
-
- if (rowManager.displayRowsCount) {
- if (newPos <= scrollMax) {
- rowManager.element.scrollTop = newPos;
- } else {
- rowManager.scrollToRow(rowManager.getDisplayRows()[rowManager.displayRowsCount - 1]);
- }
- }
-
- this.table.element.focus();
- },
- scrollToStart: function scrollToStart(e) {
- var rowManager = this.table.rowManager;
-
- e.preventDefault();
-
- if (rowManager.displayRowsCount) {
- rowManager.scrollToRow(rowManager.getDisplayRows()[0]);
- }
-
- this.table.element.focus();
- },
- scrollToEnd: function scrollToEnd(e) {
- var rowManager = this.table.rowManager;
-
- e.preventDefault();
-
- if (rowManager.displayRowsCount) {
- rowManager.scrollToRow(rowManager.getDisplayRows()[rowManager.displayRowsCount - 1]);
- }
-
- this.table.element.focus();
- },
- navPrev: function navPrev(e) {
- var cell = false;
-
- if (this.table.modExists("edit")) {
- cell = this.table.modules.edit.currentCell;
-
- if (cell) {
- e.preventDefault();
- cell.nav().prev();
- }
- }
- },
-
- navNext: function navNext(e) {
- var cell = false;
- var newRow = this.table.options.tabEndNewRow;
- var nav;
-
- if (this.table.modExists("edit")) {
- cell = this.table.modules.edit.currentCell;
-
- if (cell) {
- e.preventDefault();
-
- nav = cell.nav();
-
- if (!nav.next()) {
- if (newRow) {
-
- cell.getElement().firstChild.blur();
-
- if (newRow === true) {
- newRow = this.table.addRow({});
- } else {
- if (typeof newRow == "function") {
- newRow = this.table.addRow(newRow(cell.row.getComponent()));
- } else {
- newRow = this.table.addRow(newRow);
- }
- }
-
- newRow.then(function () {
- setTimeout(function () {
- nav.next();
- });
- });
- }
- }
- }
- }
- },
-
- navLeft: function navLeft(e) {
- var cell = false;
-
- if (this.table.modExists("edit")) {
- cell = this.table.modules.edit.currentCell;
-
- if (cell) {
- e.preventDefault();
- cell.nav().left();
- }
- }
- },
-
- navRight: function navRight(e) {
- var cell = false;
-
- if (this.table.modExists("edit")) {
- cell = this.table.modules.edit.currentCell;
-
- if (cell) {
- e.preventDefault();
- cell.nav().right();
- }
- }
- },
-
- navUp: function navUp(e) {
- var cell = false;
-
- if (this.table.modExists("edit")) {
- cell = this.table.modules.edit.currentCell;
-
- if (cell) {
- e.preventDefault();
- cell.nav().up();
- }
- }
- },
-
- navDown: function navDown(e) {
- var cell = false;
-
- if (this.table.modExists("edit")) {
- cell = this.table.modules.edit.currentCell;
-
- if (cell) {
- e.preventDefault();
- cell.nav().down();
- }
- }
- },
-
- undo: function undo(e) {
- var cell = false;
- if (this.table.options.history && this.table.modExists("history") && this.table.modExists("edit")) {
-
- cell = this.table.modules.edit.currentCell;
-
- if (!cell) {
- e.preventDefault();
- this.table.modules.history.undo();
- }
- }
- },
-
- redo: function redo(e) {
- var cell = false;
- if (this.table.options.history && this.table.modExists("history") && this.table.modExists("edit")) {
-
- cell = this.table.modules.edit.currentCell;
-
- if (!cell) {
- e.preventDefault();
- this.table.modules.history.redo();
- }
- }
- },
-
- copyToClipboard: function copyToClipboard(e) {
- if (!this.table.modules.edit.currentCell) {
- if (this.table.modExists("clipboard", true)) {
- this.table.modules.clipboard.copy(false, true);
- }
- }
- }
- };
-
- Tabulator.prototype.registerModule("keybindings", Keybindings);
|