1 var Keybindings =
function(table){
4 this.pressedKeys = null;
5 this.keyupBinding =
false;
6 this.keydownBinding =
false;
9 Keybindings.prototype.initialize =
function(){
10 var bindings = this.table.options.keybindings,
14 this.pressedKeys = [];
16 if(bindings !==
false){
18 for(let key in this.bindings){
19 mergedBindings[key] = this.bindings[key];
22 if(Object.keys(bindings).length){
24 for(let key in bindings){
25 mergedBindings[key] = bindings[key];
29 this.mapBindings(mergedBindings);
34 Keybindings.prototype.mapBindings =
function(bindings){
37 for(let key in bindings){
39 if(this.actions[key]){
43 if(typeof bindings[key] !==
"object"){
44 bindings[key] = [bindings[key]];
47 bindings[key].forEach(
function(binding){
48 self.mapBinding(key, binding);
53 console.warn(
"Key Binding Error - no such action:", key);
58 Keybindings.prototype.mapBinding =
function(action, symbolsList){
62 action: this.actions[action],
68 var symbols = symbolsList.toString().toLowerCase().split(
" ").join(
"").split(
"+");
70 symbols.forEach(
function(symbol){
81 symbol = parseInt(symbol);
82 binding.keys.push(symbol);
84 if(!
self.watchKeys[symbol]){
85 self.watchKeys[symbol] = [];
88 self.watchKeys[symbol].push(binding);
93 Keybindings.prototype.bindEvents =
function(){
96 this.keyupBinding =
function(e){
98 var bindings =
self.watchKeys[code];
102 self.pressedKeys.push(code);
104 bindings.forEach(
function(binding){
105 self.checkBinding(e, binding);
110 this.keydownBinding =
function(e){
111 var code = e.keyCode;
112 var bindings =
self.watchKeys[code];
116 var index =
self.pressedKeys.indexOf(code);
119 self.pressedKeys.splice(index, 1);
124 this.table.element.addEventListener(
"keydown", this.keyupBinding);
126 this.table.element.addEventListener(
"keyup", this.keydownBinding);
129 Keybindings.prototype.clearBindings =
function(){
130 if(this.keyupBinding){
131 this.table.element.removeEventListener(
"keydown", this.keyupBinding);
134 if(this.keydownBinding){
135 this.table.element.removeEventListener(
"keyup", this.keydownBinding);
140 Keybindings.prototype.checkBinding =
function(e, binding){
144 if(e.ctrlKey == binding.ctrl && e.shiftKey == binding.shift){
145 binding.keys.forEach(
function(key){
146 var index =
self.pressedKeys.indexOf(key);
154 binding.action.call(
self, e);
164 Keybindings.prototype.bindings = {
175 copyToClipboard:
"ctrl + 67",
179 Keybindings.prototype.actions = {
180 keyBlock:
function(e){
184 scrollPageUp:
function(e){
185 var rowManager = this.table.rowManager,
186 newPos = rowManager.scrollTop - rowManager.height,
187 scrollMax = rowManager.element.scrollHeight;
191 if(rowManager.displayRowsCount){
193 rowManager.element.scrollTop = newPos;
195 rowManager.scrollToRow(rowManager.getDisplayRows()[0]);
199 this.table.element.focus();
201 scrollPageDown:
function(e){
202 var rowManager = this.table.rowManager,
203 newPos = rowManager.scrollTop + rowManager.height,
204 scrollMax = rowManager.element.scrollHeight;
208 if(rowManager.displayRowsCount){
209 if(newPos <= scrollMax){
210 rowManager.element.scrollTop = newPos;
212 rowManager.scrollToRow(rowManager.getDisplayRows()[rowManager.displayRowsCount - 1]);
216 this.table.element.focus();
219 scrollToStart:
function(e){
220 var rowManager = this.table.rowManager;
224 if(rowManager.displayRowsCount){
225 rowManager.scrollToRow(rowManager.getDisplayRows()[0]);
228 this.table.element.focus();
230 scrollToEnd:
function(e){
231 var rowManager = this.table.rowManager;
235 if(rowManager.displayRowsCount){
236 rowManager.scrollToRow(rowManager.getDisplayRows()[rowManager.displayRowsCount - 1]);
239 this.table.element.focus();
244 if(this.table.modExists(
"edit")){
245 cell = this.table.modules.edit.currentCell;
256 var newRow = this.table.options.tabEndNewRow;
259 if(this.table.modExists(
"edit")){
260 cell = this.table.modules.edit.currentCell;
270 newRow = this.table.addRow({})
272 if(typeof newRow ==
"function"){
273 newRow = this.table.addRow(newRow(cell.row.getComponent()))
275 newRow = this.table.addRow(newRow)
291 if(this.table.modExists(
"edit")){
292 cell = this.table.modules.edit.currentCell;
301 navRight:
function(e){
304 if(this.table.modExists(
"edit")){
305 cell = this.table.modules.edit.currentCell;
317 if(this.table.modExists(
"edit")){
318 cell = this.table.modules.edit.currentCell;
330 if(this.table.modExists(
"edit")){
331 cell = this.table.modules.edit.currentCell;
342 if(this.table.options.history &&
this.table.modExists(
"history") && this.table.modExists(
"edit")){
344 cell = this.table.modules.edit.currentCell;
348 this.table.modules.history.undo();
355 if(this.table.options.history &&
this.table.modExists(
"history") && this.table.modExists(
"edit")){
357 cell = this.table.modules.edit.currentCell;
361 this.table.modules.history.redo();
366 copyToClipboard:
function(e){
367 if(!this.table.modules.edit.currentCell){
368 if(this.table.modExists(
"clipboard",
true)){
369 this.table.modules.clipboard.copy(!this.table.options.selectable ||
this.table.options.selectable ==
"highlight" ?
"active" :
"selected", null, null, null,
true);
376 Tabulator.prototype.registerModule(
"keybindings", Keybindings);