keyboardmanager.js
770 lines
| 24.4 KiB
| application/javascript
|
JavascriptLexer
|
r14020 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// Keyboard management | ||||
//============================================================================ | ||||
var IPython = (function (IPython) { | ||||
"use strict"; | ||||
|
r14036 | // Setup global keycodes and inverse keycodes. | ||
// See http://unixpapa.com/js/key.html for a complete description. The short of | ||||
// it is that there are different keycode sets. Firefox uses the "Mozilla keycodes" | ||||
// and Webkit/IE use the "IE keycodes". These keycode sets are mostly the same | ||||
// but have minor differences. | ||||
// These apply to Firefox, (Webkit and IE) | ||||
var _keycodes = { | ||||
'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, | ||||
'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 'r': 82, | ||||
's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90, | ||||
'1 !': 49, '2 @': 50, '3 #': 51, '4 $': 52, '5 %': 53, '6 ^': 54, | ||||
'7 &': 55, '8 *': 56, '9 (': 57, '0 )': 48, | ||||
'[ {': 219, '] }': 221, '` ~': 192, ', <': 188, '. >': 190, '/ ?': 191, | ||||
|
r14037 | '\\ |': 220, '\' "': 222, | ||
|
r14036 | 'numpad0': 96, 'numpad1': 97, 'numpad2': 98, 'numpad3': 99, 'numpad4': 100, | ||
'numpad5': 101, 'numpad6': 102, 'numpad7': 103, 'numpad8': 104, 'numpad9': 105, | ||||
'multiply': 106, 'add': 107, 'subtract': 109, 'decimal': 110, 'divide': 111, | ||||
'f1': 112, 'f2': 113, 'f3': 114, 'f4': 115, 'f5': 116, 'f6': 117, 'f7': 118, | ||||
'f8': 119, 'f9': 120, 'f11': 122, 'f12': 123, 'f13': 124, 'f14': 125, 'f15': 126, | ||||
'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18, | ||||
'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34, | ||||
'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40, | ||||
'insert': 45, 'delete': 46, 'numlock': 144, | ||||
}; | ||||
// These apply to Firefox and Opera | ||||
var _mozilla_keycodes = { | ||||
|
r14076 | '; :': 59, '= +': 61, '- _': 173, 'meta': 224 | ||
|
r14036 | } | ||
// This apply to Webkit and IE | ||||
var _ie_keycodes = { | ||||
|
r14037 | '; :': 186, '= +': 187, '- _': 189, | ||
|
r14036 | } | ||
var browser = IPython.utils.browser[0]; | ||||
|
r14816 | var platform = IPython.utils.platform; | ||
|
r14036 | |||
if (browser === 'Firefox' || browser === 'Opera') { | ||||
$.extend(_keycodes, _mozilla_keycodes); | ||||
} else if (browser === 'Safari' || browser === 'Chrome' || browser === 'MSIE') { | ||||
$.extend(_keycodes, _ie_keycodes); | ||||
} | ||||
var keycodes = {}; | ||||
var inv_keycodes = {}; | ||||
for (var name in _keycodes) { | ||||
var names = name.split(' '); | ||||
if (names.length === 1) { | ||||
var n = names[0] | ||||
keycodes[n] = _keycodes[n] | ||||
inv_keycodes[_keycodes[n]] = n | ||||
} else { | ||||
var primary = names[0]; | ||||
var secondary = names[1]; | ||||
keycodes[primary] = _keycodes[name] | ||||
keycodes[secondary] = _keycodes[name] | ||||
inv_keycodes[_keycodes[name]] = primary | ||||
} | ||||
} | ||||
// Default keyboard shortcuts | ||||
var default_common_shortcuts = { | ||||
'shift' : { | ||||
help : '', | ||||
|
r14093 | help_index : '', | ||
|
r14036 | handler : function (event) { | ||
// ignore shift keydown | ||||
return true; | ||||
} | ||||
}, | ||||
'shift+enter' : { | ||||
|
r14946 | help : 'run cell, select below', | ||
|
r14093 | help_index : 'ba', | ||
|
r14036 | handler : function (event) { | ||
|
r14945 | IPython.notebook.execute_cell_and_select_below(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
|
r14093 | 'ctrl+enter' : { | ||
|
r14946 | help : 'run cell', | ||
|
r14093 | help_index : 'bb', | ||
|
r14036 | handler : function (event) { | ||
|
r14945 | IPython.notebook.execute_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
|
r14093 | 'alt+enter' : { | ||
help : 'run cell, insert below', | ||||
help_index : 'bc', | ||||
|
r14036 | handler : function (event) { | ||
|
r14093 | IPython.notebook.execute_cell_and_insert_below(); | ||
|
r14036 | return false; | ||
} | ||||
} | ||||
} | ||||
|
r14816 | if (platform === 'MacOS') { | ||
default_common_shortcuts['cmd+s'] = | ||||
{ | ||||
help : 'save notebook', | ||||
help_index : 'fb', | ||||
handler : function (event) { | ||||
IPython.notebook.save_checkpoint(); | ||||
event.preventDefault(); | ||||
return false; | ||||
} | ||||
}; | ||||
} else { | ||||
default_common_shortcuts['ctrl+s'] = | ||||
{ | ||||
help : 'save notebook', | ||||
help_index : 'fb', | ||||
handler : function (event) { | ||||
IPython.notebook.save_checkpoint(); | ||||
event.preventDefault(); | ||||
return false; | ||||
} | ||||
}; | ||||
} | ||||
|
r14036 | // Edit mode defaults | ||
var default_edit_shortcuts = { | ||||
'esc' : { | ||||
help : 'command mode', | ||||
|
r14093 | help_index : 'aa', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.command_mode(); | ||||
|
r14073 | IPython.notebook.focus_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'ctrl+m' : { | ||||
help : 'command mode', | ||||
|
r14093 | help_index : 'ab', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.command_mode(); | ||||
|
r14073 | IPython.notebook.focus_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'up' : { | ||||
|
r14099 | help : '', | ||
help_index : '', | ||||
|
r14036 | handler : function (event) { | ||
var cell = IPython.notebook.get_selected_cell(); | ||||
if (cell && cell.at_top()) { | ||||
event.preventDefault(); | ||||
IPython.notebook.command_mode() | ||||
IPython.notebook.select_prev(); | ||||
IPython.notebook.edit_mode(); | ||||
return false; | ||||
}; | ||||
} | ||||
}, | ||||
'down' : { | ||||
|
r14099 | help : '', | ||
help_index : '', | ||||
|
r14036 | handler : function (event) { | ||
var cell = IPython.notebook.get_selected_cell(); | ||||
if (cell && cell.at_bottom()) { | ||||
event.preventDefault(); | ||||
IPython.notebook.command_mode() | ||||
IPython.notebook.select_next(); | ||||
IPython.notebook.edit_mode(); | ||||
return false; | ||||
}; | ||||
} | ||||
}, | ||||
|
r14057 | 'alt+-' : { | ||
help : 'split cell', | ||||
|
r14093 | help_index : 'ea', | ||
|
r14057 | handler : function (event) { | ||
IPython.notebook.split_cell(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14076 | 'alt+subtract' : { | ||
|
r14093 | help : '', | ||
help_index : 'eb', | ||||
|
r14076 | handler : function (event) { | ||
IPython.notebook.split_cell(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14816 | 'tab' : { | ||
help : 'indent or complete', | ||||
help_index : 'ec', | ||||
}, | ||||
'shift+tab' : { | ||||
help : 'tooltip', | ||||
help_index : 'ed', | ||||
}, | ||||
} | ||||
if (platform === 'MacOS') { | ||||
default_edit_shortcuts['cmd+/'] = | ||||
{ | ||||
help : 'toggle comment', | ||||
help_index : 'ee' | ||||
}; | ||||
default_edit_shortcuts['cmd+]'] = | ||||
{ | ||||
help : 'indent', | ||||
help_index : 'ef' | ||||
}; | ||||
default_edit_shortcuts['cmd+['] = | ||||
{ | ||||
help : 'dedent', | ||||
help_index : 'eg' | ||||
}; | ||||
} else { | ||||
default_edit_shortcuts['ctrl+/'] = | ||||
{ | ||||
help : 'toggle comment', | ||||
help_index : 'ee' | ||||
}; | ||||
default_edit_shortcuts['ctrl+]'] = | ||||
{ | ||||
help : 'indent', | ||||
help_index : 'ef' | ||||
}; | ||||
default_edit_shortcuts['ctrl+['] = | ||||
{ | ||||
help : 'dedent', | ||||
help_index : 'eg' | ||||
}; | ||||
|
r14036 | } | ||
// Command mode defaults | ||||
var default_command_shortcuts = { | ||||
'enter' : { | ||||
help : 'edit mode', | ||||
|
r14093 | help_index : 'aa', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.edit_mode(); | ||||
return false; | ||||
} | ||||
}, | ||||
'up' : { | ||||
help : 'select previous cell', | ||||
|
r14093 | help_index : 'da', | ||
|
r14036 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== 0 && index !== null) { | ||||
IPython.notebook.select_prev(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
'down' : { | ||||
help : 'select next cell', | ||||
|
r14093 | help_index : 'db', | ||
|
r14036 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== (IPython.notebook.ncells()-1) && index !== null) { | ||||
IPython.notebook.select_next(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14038 | 'k' : { | ||
help : 'select previous cell', | ||||
|
r14093 | help_index : 'dc', | ||
|
r14038 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== 0 && index !== null) { | ||||
IPython.notebook.select_prev(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
'j' : { | ||||
help : 'select next cell', | ||||
|
r14093 | help_index : 'dd', | ||
|
r14038 | handler : function (event) { | ||
var index = IPython.notebook.get_selected_index(); | ||||
if (index !== (IPython.notebook.ncells()-1) && index !== null) { | ||||
IPython.notebook.select_next(); | ||||
var cell = IPython.notebook.get_selected_cell(); | ||||
cell.focus_cell(); | ||||
}; | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14036 | 'x' : { | ||
help : 'cut cell', | ||||
|
r14093 | help_index : 'ee', | ||
|
r14036 | handler : function (event) { | ||
|
r14037 | IPython.notebook.cut_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'c' : { | ||||
help : 'copy cell', | ||||
|
r14093 | help_index : 'ef', | ||
|
r14036 | handler : function (event) { | ||
|
r14037 | IPython.notebook.copy_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
|
r14810 | 'shift+v' : { | ||
help : 'paste cell above', | ||||
help_index : 'eg', | ||||
handler : function (event) { | ||||
IPython.notebook.paste_cell_above(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14036 | 'v' : { | ||
help : 'paste cell below', | ||||
|
r14810 | help_index : 'eh', | ||
|
r14036 | handler : function (event) { | ||
|
r14037 | IPython.notebook.paste_cell_below(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'd' : { | ||||
help : 'delete cell (press twice)', | ||||
|
r14810 | help_index : 'ej', | ||
count: 2, | ||||
|
r14036 | handler : function (event) { | ||
|
r14810 | IPython.notebook.delete_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'a' : { | ||||
help : 'insert cell above', | ||||
|
r14093 | help_index : 'ec', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.insert_cell_above('code'); | ||||
IPython.notebook.select_prev(); | ||||
|
r14075 | IPython.notebook.focus_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'b' : { | ||||
help : 'insert cell below', | ||||
|
r14093 | help_index : 'ed', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.insert_cell_below('code'); | ||||
IPython.notebook.select_next(); | ||||
|
r14075 | IPython.notebook.focus_cell(); | ||
|
r14036 | return false; | ||
} | ||||
}, | ||||
'y' : { | ||||
help : 'to code', | ||||
|
r14093 | help_index : 'ca', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_code(); | ||||
return false; | ||||
} | ||||
}, | ||||
'm' : { | ||||
help : 'to markdown', | ||||
|
r14093 | help_index : 'cb', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_markdown(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14810 | 'r' : { | ||
|
r14036 | help : 'to raw', | ||
|
r14093 | help_index : 'cc', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_raw(); | ||||
return false; | ||||
} | ||||
}, | ||||
'1' : { | ||||
help : 'to heading 1', | ||||
|
r14093 | help_index : 'cd', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 1); | ||||
return false; | ||||
} | ||||
}, | ||||
'2' : { | ||||
help : 'to heading 2', | ||||
|
r14093 | help_index : 'ce', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 2); | ||||
return false; | ||||
} | ||||
}, | ||||
'3' : { | ||||
help : 'to heading 3', | ||||
|
r14093 | help_index : 'cf', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 3); | ||||
return false; | ||||
} | ||||
}, | ||||
'4' : { | ||||
help : 'to heading 4', | ||||
|
r14093 | help_index : 'cg', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 4); | ||||
return false; | ||||
} | ||||
}, | ||||
'5' : { | ||||
help : 'to heading 5', | ||||
|
r14093 | help_index : 'ch', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 5); | ||||
return false; | ||||
} | ||||
}, | ||||
'6' : { | ||||
help : 'to heading 6', | ||||
|
r14093 | help_index : 'ci', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.to_heading(undefined, 6); | ||||
return false; | ||||
} | ||||
}, | ||||
'o' : { | ||||
help : 'toggle output', | ||||
|
r14093 | help_index : 'gb', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.toggle_output(); | ||||
return false; | ||||
} | ||||
}, | ||||
'shift+o' : { | ||||
|
r14871 | help : 'toggle output scrolling', | ||
|
r14093 | help_index : 'gc', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.toggle_output_scroll(); | ||||
return false; | ||||
} | ||||
}, | ||||
's' : { | ||||
help : 'save notebook', | ||||
|
r14093 | help_index : 'fa', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.save_checkpoint(); | ||||
return false; | ||||
} | ||||
}, | ||||
'ctrl+j' : { | ||||
help : 'move cell down', | ||||
|
r14093 | help_index : 'eb', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.move_cell_down(); | ||||
return false; | ||||
} | ||||
}, | ||||
'ctrl+k' : { | ||||
help : 'move cell up', | ||||
|
r14093 | help_index : 'ea', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.move_cell_up(); | ||||
return false; | ||||
} | ||||
}, | ||||
'l' : { | ||||
help : 'toggle line numbers', | ||||
|
r14093 | help_index : 'ga', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.cell_toggle_line_numbers(); | ||||
return false; | ||||
} | ||||
}, | ||||
'i' : { | ||||
|
r14810 | help : 'interrupt kernel (press twice)', | ||
|
r14093 | help_index : 'ha', | ||
|
r14810 | count: 2, | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.kernel.interrupt(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14810 | '0' : { | ||
help : 'restart kernel (press twice)', | ||||
|
r14093 | help_index : 'hb', | ||
|
r14810 | count: 2, | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.restart_kernel(); | ||||
return false; | ||||
} | ||||
}, | ||||
'h' : { | ||||
help : 'keyboard shortcuts', | ||||
|
r14093 | help_index : 'gd', | ||
|
r14036 | handler : function (event) { | ||
IPython.quick_help.show_keyboard_shortcuts(); | ||||
return false; | ||||
} | ||||
}, | ||||
'z' : { | ||||
help : 'undo last delete', | ||||
|
r14810 | help_index : 'ei', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.undelete_cell(); | ||||
return false; | ||||
} | ||||
}, | ||||
'shift+=' : { | ||||
help : 'merge cell below', | ||||
|
r14810 | help_index : 'ek', | ||
|
r14036 | handler : function (event) { | ||
IPython.notebook.merge_cell_below(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14816 | 'shift+m' : { | ||
help : 'merge cell below', | ||||
help_index : 'ek', | ||||
handler : function (event) { | ||||
IPython.notebook.merge_cell_below(); | ||||
return false; | ||||
} | ||||
}, | ||||
|
r14036 | } | ||
// Shortcut manager class | ||||
|
r14810 | var ShortcutManager = function (delay) { | ||
|
r14036 | this._shortcuts = {} | ||
|
r14810 | this._counts = {} | ||
this.delay = delay || 800; // delay in milliseconds | ||||
|
r14036 | } | ||
|
r14037 | ShortcutManager.prototype.help = function () { | ||
var help = []; | ||||
for (var shortcut in this._shortcuts) { | ||||
|
r14093 | var help_string = this._shortcuts[shortcut]['help']; | ||
var help_index = this._shortcuts[shortcut]['help_index']; | ||||
if (help_string) { | ||||
|
r14816 | if (platform === 'MacOS') { | ||
shortcut = shortcut.replace('meta', 'cmd'); | ||||
} | ||||
|
r14093 | help.push({ | ||
shortcut: shortcut, | ||||
help: help_string, | ||||
help_index: help_index} | ||||
); | ||||
} | ||||
|
r14037 | } | ||
|
r14093 | help.sort(function (a, b) { | ||
if (a.help_index > b.help_index) | ||||
return 1; | ||||
if (a.help_index < b.help_index) | ||||
return -1; | ||||
return 0; | ||||
}); | ||||
|
r14037 | return help; | ||
} | ||||
|
r14090 | ShortcutManager.prototype.normalize_key = function (key) { | ||
|
r14036 | return inv_keycodes[keycodes[key]]; | ||
} | ||||
|
r14090 | ShortcutManager.prototype.normalize_shortcut = function (shortcut) { | ||
|
r14036 | // Sort a sequence of + separated modifiers into the order alt+ctrl+meta+shift | ||
|
r14816 | shortcut = shortcut.replace('cmd', 'meta').toLowerCase(); | ||
|
r14036 | var values = shortcut.split("+"); | ||
if (values.length === 1) { | ||||
|
r14090 | return this.normalize_key(values[0]) | ||
|
r14036 | } else { | ||
var modifiers = values.slice(0,-1); | ||||
|
r14090 | var key = this.normalize_key(values[values.length-1]); | ||
|
r14036 | modifiers.sort(); | ||
return modifiers.join('+') + '+' + key; | ||||
} | ||||
} | ||||
ShortcutManager.prototype.event_to_shortcut = function (event) { | ||||
// Convert a jQuery keyboard event to a strong based keyboard shortcut | ||||
var shortcut = ''; | ||||
var key = inv_keycodes[event.which] | ||||
if (event.altKey && key !== 'alt') {shortcut += 'alt+';} | ||||
if (event.ctrlKey && key !== 'ctrl') {shortcut += 'ctrl+';} | ||||
if (event.metaKey && key !== 'meta') {shortcut += 'meta+';} | ||||
if (event.shiftKey && key !== 'shift') {shortcut += 'shift+';} | ||||
shortcut += key; | ||||
return shortcut | ||||
} | ||||
ShortcutManager.prototype.clear_shortcuts = function () { | ||||
this._shortcuts = {}; | ||||
} | ||||
ShortcutManager.prototype.add_shortcut = function (shortcut, data) { | ||||
|
r14093 | if (typeof(data) === 'function') { | ||
|
r14099 | data = {help: '', help_index: '', handler: data} | ||
} | ||||
|
r14100 | data.help_index = data.help_index || ''; | ||
data.help = data.help || ''; | ||||
|
r14810 | data.count = data.count || 1; | ||
|
r14099 | if (data.help_index === '') { | ||
|
r14100 | data.help_index = 'zz'; | ||
|
r14093 | } | ||
|
r14090 | shortcut = this.normalize_shortcut(shortcut); | ||
|
r14810 | this._counts[shortcut] = 0; | ||
|
r14036 | this._shortcuts[shortcut] = data; | ||
} | ||||
ShortcutManager.prototype.add_shortcuts = function (data) { | ||||
for (var shortcut in data) { | ||||
this.add_shortcut(shortcut, data[shortcut]); | ||||
} | ||||
} | ||||
ShortcutManager.prototype.remove_shortcut = function (shortcut) { | ||||
|
r14090 | shortcut = this.normalize_shortcut(shortcut); | ||
|
r14810 | delete this._counts[shortcut]; | ||
|
r14036 | delete this._shortcuts[shortcut]; | ||
} | ||||
|
r14825 | ShortcutManager.prototype.count_handler = function (shortcut, event, data) { | ||
|
r14810 | var that = this; | ||
var c = this._counts; | ||||
|
r14825 | if (c[shortcut] === data.count-1) { | ||
c[shortcut] = 0; | ||||
return data.handler(event); | ||||
} else { | ||||
c[shortcut] = c[shortcut] + 1; | ||||
|
r14810 | setTimeout(function () { | ||
c[shortcut] = 0; | ||||
}, that.delay); | ||||
} | ||||
return false; | ||||
} | ||||
|
r14036 | ShortcutManager.prototype.call_handler = function (event) { | ||
var shortcut = this.event_to_shortcut(event); | ||||
var data = this._shortcuts[shortcut]; | ||||
|
r14810 | if (data) { | ||
|
r14036 | var handler = data['handler']; | ||
|
r14810 | if (handler) { | ||
if (data.count === 1) { | ||||
return handler(event); | ||||
} else if (data.count > 1) { | ||||
|
r14825 | return this.count_handler(shortcut, event, data); | ||
|
r14810 | } | ||
|
r14036 | } | ||
} | ||||
return true; | ||||
} | ||||
// Main keyboard manager for the notebook | ||||
|
r14020 | |||
var KeyboardManager = function () { | ||||
|
r14021 | this.mode = 'command'; | ||
|
r14023 | this.enabled = true; | ||
|
r14020 | this.bind_events(); | ||
|
r14036 | this.command_shortcuts = new ShortcutManager(); | ||
this.command_shortcuts.add_shortcuts(default_common_shortcuts); | ||||
this.command_shortcuts.add_shortcuts(default_command_shortcuts); | ||||
this.edit_shortcuts = new ShortcutManager(); | ||||
this.edit_shortcuts.add_shortcuts(default_common_shortcuts); | ||||
this.edit_shortcuts.add_shortcuts(default_edit_shortcuts); | ||||
|
r14020 | }; | ||
KeyboardManager.prototype.bind_events = function () { | ||||
var that = this; | ||||
$(document).keydown(function (event) { | ||||
return that.handle_keydown(event); | ||||
}); | ||||
}; | ||||
KeyboardManager.prototype.handle_keydown = function (event) { | ||||
var notebook = IPython.notebook; | ||||
|
r14089 | |||
|
r14036 | if (event.which === keycodes['esc']) { | ||
|
r14020 | // Intercept escape at highest level to avoid closing | ||
// websocket connection with firefox | ||||
event.preventDefault(); | ||||
} | ||||
|
r14023 | if (!this.enabled) { | ||
|
r14036 | if (event.which === keycodes['esc']) { | ||
// ESC | ||||
notebook.command_mode(); | ||||
return false; | ||||
} | ||||
|
r14023 | return true; | ||
|
r14020 | } | ||
if (this.mode === 'edit') { | ||||
|
r14036 | return this.edit_shortcuts.call_handler(event); | ||
} else if (this.mode === 'command') { | ||||
return this.command_shortcuts.call_handler(event); | ||||
|
r14020 | } | ||
return true; | ||||
} | ||||
KeyboardManager.prototype.edit_mode = function () { | ||||
this.last_mode = this.mode; | ||||
this.mode = 'edit'; | ||||
} | ||||
KeyboardManager.prototype.command_mode = function () { | ||||
this.last_mode = this.mode; | ||||
this.mode = 'command'; | ||||
} | ||||
|
r14024 | KeyboardManager.prototype.enable = function () { | ||
|
r14030 | this.enabled = true; | ||
|
r14024 | } | ||
KeyboardManager.prototype.disable = function () { | ||||
|
r14030 | this.enabled = false; | ||
|
r14024 | } | ||
|
r14036 | KeyboardManager.prototype.register_events = function (e) { | ||
var that = this; | ||||
e.on('focusin', function () { | ||||
that.command_mode(); | ||||
that.disable(); | ||||
}); | ||||
e.on('focusout', function () { | ||||
that.command_mode(); | ||||
that.enable(); | ||||
|
r14046 | }); | ||
// There are times (raw_input) where we remove the element from the DOM before | ||||
// focusout is called. In this case we bind to the remove event of jQueryUI, | ||||
// which gets triggered upon removal. | ||||
e.on('remove', function () { | ||||
that.command_mode(); | ||||
that.enable(); | ||||
}); | ||||
|
r14036 | } | ||
|
r14024 | |||
|
r14036 | IPython.keycodes = keycodes; | ||
IPython.inv_keycodes = inv_keycodes; | ||||
IPython.default_common_shortcuts = default_common_shortcuts; | ||||
IPython.default_edit_shortcuts = default_edit_shortcuts; | ||||
IPython.default_command_shortcuts = default_command_shortcuts; | ||||
IPython.ShortcutManager = ShortcutManager; | ||||
|
r14020 | IPython.KeyboardManager = KeyboardManager; | ||
return IPython; | ||||
}(IPython)); | ||||