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