quickhelp.js
269 lines
| 8.9 KiB
| application/javascript
|
JavascriptLexer
|
r16141 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r5023 | |||
|
r17197 | define([ | ||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17197 | 'base/js/utils', | ||
'base/js/dialog', | ||||
|
r17202 | ], function(IPython, $, utils, dialog) { | ||
|
r12103 | "use strict"; | ||
|
r17198 | var platform = utils.platform; | ||
|
r5023 | |||
|
r17210 | var QuickHelp = function (options) { | ||
|
r19176 | /** | ||
* Constructor | ||||
* | ||||
* Parameters: | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* events: $(Events) instance | ||||
* keyboard_manager: KeyboardManager instance | ||||
* notebook: Notebook instance | ||||
*/ | ||||
|
r17210 | this.keyboard_manager = options.keyboard_manager; | ||
|
r17212 | this.notebook = options.notebook; | ||
|
r17210 | this.keyboard_manager.quick_help = this; | ||
this.events = options.events; | ||||
|
r5023 | }; | ||
|
r16045 | var cmd_ctrl = 'Ctrl-'; | ||
|
r16034 | var platform_specific; | ||
|
r16029 | |||
if (platform === 'MacOS') { | ||||
|
r16032 | // Mac OS X specific | ||
|
r16045 | cmd_ctrl = 'Cmd-'; | ||
|
r16034 | platform_specific = [ | ||
|
r16032 | { shortcut: "Cmd-Up", help:"go to cell start" }, | ||
{ shortcut: "Cmd-Down", help:"go to cell end" }, | ||||
|
r18390 | { shortcut: "Alt-Left", help:"go one word left" }, | ||
{ shortcut: "Alt-Right", help:"go one word right" }, | ||||
{ shortcut: "Alt-Backspace", help:"del word before" }, | ||||
{ shortcut: "Alt-Delete", help:"del word after" }, | ||||
|
r16034 | ]; | ||
|
r16032 | } else { | ||
// PC specific | ||||
|
r16034 | platform_specific = [ | ||
|
r16032 | { shortcut: "Ctrl-Home", help:"go to cell start" }, | ||
|
r16051 | { shortcut: "Ctrl-Up", help:"go to cell start" }, | ||
|
r16032 | { shortcut: "Ctrl-End", help:"go to cell end" }, | ||
{ shortcut: "Ctrl-Down", help:"go to cell end" }, | ||||
{ shortcut: "Ctrl-Left", help:"go one word left" }, | ||||
{ shortcut: "Ctrl-Right", help:"go one word right" }, | ||||
{ shortcut: "Ctrl-Backspace", help:"del word before" }, | ||||
{ shortcut: "Ctrl-Delete", help:"del word after" }, | ||||
|
r16034 | ]; | ||
|
r16029 | } | ||
|
r5023 | |||
|
r16032 | var cm_shortcuts = [ | ||
|
r16034 | { shortcut:"Tab", help:"code completion or indent" }, | ||
{ shortcut:"Shift-Tab", help:"tooltip" }, | ||||
|
r16045 | { shortcut: cmd_ctrl + "]", help:"indent" }, | ||
{ shortcut: cmd_ctrl + "[", help:"dedent" }, | ||||
{ shortcut: cmd_ctrl + "a", help:"select all" }, | ||||
{ shortcut: cmd_ctrl + "z", help:"undo" }, | ||||
{ shortcut: cmd_ctrl + "Shift-z", help:"redo" }, | ||||
{ shortcut: cmd_ctrl + "y", help:"redo" }, | ||||
|
r16032 | ].concat( platform_specific ); | ||
|
r19425 | |||
var mac_humanize_map = { | ||||
// all these are unicode, will probably display badly on anything except macs. | ||||
// these are the standard symbol that are used in MacOS native menus | ||||
|
r19426 | // cf http://apple.stackexchange.com/questions/55727/ | ||
// for htmlentities and/or unicode value | ||||
|
r19425 | 'cmd':'⌘', | ||
'shift':'⇧', | ||||
'alt':'⌥', | ||||
'up':'↑', | ||||
'down':'↓', | ||||
'left':'←', | ||||
'right':'→', | ||||
'eject':'⏏', | ||||
'tab':'⇥', | ||||
|
r19426 | 'backtab':'⇤', | ||
'capslock':'⇪', | ||||
|
r19425 | 'esc':'⎋', | ||
'ctrl':'⌃', | ||||
'enter':'↩', | ||||
'pageup':'⇞', | ||||
'pagedown':'⇟', | ||||
'home':'↖', | ||||
'end':'↘', | ||||
'altenter':'⌤', | ||||
'space':'␣', | ||||
|
r19426 | 'delete':'⌦', | ||
'backspace':'⌫', | ||||
|
r19425 | 'apple':'', | ||
}; | ||||
var default_humanize_map = { | ||||
'shift':'Shift', | ||||
'alt':'Alt', | ||||
'up':'Up', | ||||
'down':'Down', | ||||
'left':'Left', | ||||
'right':'Right', | ||||
'tab':'Tab', | ||||
'capslock':'Caps Lock', | ||||
'esc':'Esc', | ||||
'ctrl':'Ctrl', | ||||
'enter':'Enter', | ||||
'pageup':'Page Up', | ||||
'pagedown':'Page Down', | ||||
'home':'Home', | ||||
'end':'End', | ||||
'space':'Space', | ||||
|
r19427 | 'backspace':'Backspace', | ||
|
r19425 | }; | ||
var humanize_map; | ||||
if (platform === 'MacOS'){ | ||||
humanize_map = mac_humanize_map; | ||||
} else { | ||||
humanize_map = default_humanize_map; | ||||
} | ||||
|
r16032 | |||
|
r19422 | function humanize_key(key){ | ||
if (key.length === 1){ | ||||
key = key.toUpperCase(); | ||||
} | ||||
|
r19425 | return humanize_map[key.toLowerCase()]||key; | ||
|
r19422 | } | ||
function humanize_sequence(sequence){ | ||||
|
r19424 | var joinchar = ','; | ||
|
r19422 | var hum = _.map(sequence.replace(/meta/g, 'cmd').split(','), humanize_shortcut).join(joinchar); | ||
return hum; | ||||
} | ||||
function humanize_shortcut(shortcut){ | ||||
var joinchar = '-'; | ||||
if (platform === 'MacOS'){ | ||||
joinchar = ''; | ||||
} | ||||
var sh = _.map(shortcut.split('-'), humanize_key ).join(joinchar); | ||||
return sh; | ||||
} | ||||
|
r16032 | |||
|
r5858 | QuickHelp.prototype.show_keyboard_shortcuts = function () { | ||
|
r19176 | /** | ||
* toggles display of keyboard shortcut dialog | ||||
*/ | ||||
|
r5023 | var that = this; | ||
|
r15771 | if ( this.force_rebuild ) { | ||
|
r15844 | this.shortcut_dialog.remove(); | ||
|
r15771 | delete(this.shortcut_dialog); | ||
this.force_rebuild = false; | ||||
} | ||||
|
r5858 | if ( this.shortcut_dialog ){ | ||
// if dialog is already shown, close it | ||||
|
r10939 | $(this.shortcut_dialog).modal("toggle"); | ||
|
r5858 | return; | ||
} | ||||
|
r17203 | var command_shortcuts = this.keyboard_manager.command_shortcuts.help(); | ||
var edit_shortcuts = this.keyboard_manager.edit_shortcuts.help(); | ||||
|
r14037 | var help, shortcut; | ||
|
r14093 | var i, half, n; | ||
|
r14037 | var element = $('<div/>'); | ||
|
r14066 | // The documentation | ||
|
r16957 | var doc = $('<div/>').addClass('alert alert-warning'); | ||
|
r14066 | doc.append( | ||
|
r14634 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') | ||
|
r14066 | ).append( | ||
'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ | ||||
|
r14949 | 'allows you to type code/text into a cell and is indicated by a green cell '+ | ||
|
r14093 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ | ||
'and is indicated by a grey cell border.' | ||||
|
r15840 | ); | ||
|
r14066 | element.append(doc); | ||
|
r14037 | // Command mode | ||
|
r14096 | var cmd_div = this.build_command_help(); | ||
element.append(cmd_div); | ||||
// Edit mode | ||||
|
r16034 | var edit_div = this.build_edit_help(cm_shortcuts); | ||
|
r14096 | element.append(edit_div); | ||
|
r17202 | this.shortcut_dialog = dialog.modal({ | ||
|
r14096 | title : "Keyboard shortcuts", | ||
body : element, | ||||
destroy : false, | ||||
buttons : { | ||||
Close : {} | ||||
|
r17212 | }, | ||
notebook: this.notebook, | ||||
keyboard_manager: this.keyboard_manager, | ||||
|
r14096 | }); | ||
|
r16023 | this.shortcut_dialog.addClass("modal_stretch"); | ||
|
r15771 | |||
|
r17197 | this.events.on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); | ||
|
r14096 | }; | ||
QuickHelp.prototype.build_command_help = function () { | ||||
|
r17203 | var command_shortcuts = this.keyboard_manager.command_shortcuts.help(); | ||
|
r15801 | return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts); | ||
|
r15840 | }; | ||
|
r14096 | |||
|
r15876 | var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' }; | ||
|
r15782 | var prettify = function (s) { | ||
|
r15877 | s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key | ||
|
r15873 | var keys = s.split('-'); | ||
|
r15782 | var k, i; | ||
|
r16227 | for (i=0; i < keys.length; i++) { | ||
|
r15782 | k = keys[i]; | ||
|
r15801 | if ( k.length == 1 ) { | ||
keys[i] = "<code><strong>" + k + "</strong></code>"; | ||||
continue; // leave individual keys lower-cased | ||||
} | ||||
|
r18390 | if (k.indexOf(',') === -1){ | ||
keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) ); | ||||
} | ||||
|
r15801 | keys[i] = "<code><strong>" + keys[i] + "</strong></code>"; | ||
|
r15782 | } | ||
return keys.join('-'); | ||||
|
r15781 | |||
}; | ||||
|
r16034 | QuickHelp.prototype.build_edit_help = function (cm_shortcuts) { | ||
|
r17203 | var edit_shortcuts = this.keyboard_manager.edit_shortcuts.help(); | ||
|
r16279 | jQuery.merge(cm_shortcuts, edit_shortcuts); | ||
|
r16034 | return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', cm_shortcuts); | ||
|
r15840 | }; | ||
|
r15801 | |||
var build_one = function (s) { | ||||
|
r15840 | var help = s.help; | ||
|
r19422 | var shortcut = ''; | ||
if(s.shortcut){ | ||||
shortcut = prettify(humanize_sequence(s.shortcut)); | ||||
} | ||||
|
r15801 | return $('<div>').addClass('quickhelp'). | ||
append($('<span/>').addClass('shortcut_key').append($(shortcut))). | ||||
|
r15840 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)); | ||
|
r15801 | |||
|
r15840 | }; | ||
|
r15801 | |||
var build_div = function (title, shortcuts) { | ||||
var i, half, n; | ||||
var div = $('<div/>').append($(title)); | ||||
var sub_div = $('<div/>').addClass('hbox'); | ||||
|
r15899 | var col1 = $('<div/>').addClass('box-flex1'); | ||
var col2 = $('<div/>').addClass('box-flex1'); | ||||
|
r15801 | n = shortcuts.length; | ||
|
r14093 | half = ~~(n/2); // Truncate :) | ||
|
r15840 | for (i=0; i<half; i++) { col1.append( build_one(shortcuts[i]) ); } | ||
for (i=half; i<n; i++) { col2.append( build_one(shortcuts[i]) ); } | ||||
|
r15801 | sub_div.append(col1).append(col2); | ||
div.append(sub_div); | ||||
return div; | ||||
|
r15840 | }; | ||
|
r5023 | |||
|
r17197 | // Backwards compatability. | ||
|
r5066 | IPython.QuickHelp = QuickHelp; | ||
|
r5023 | |||
|
r17201 | return {'QuickHelp': QuickHelp}; | ||
|
r17197 | }); | ||