quickhelp.js
128 lines
| 4.6 KiB
| application/javascript
|
JavascriptLexer
Fernando Perez
|
r5023 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-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. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
MinRK
|
r5066 | // QuickHelp button | ||
Fernando Perez
|
r5023 | //============================================================================ | ||
var IPython = (function (IPython) { | ||||
Matthias BUSSONNIER
|
r12103 | "use strict"; | ||
Fernando Perez
|
r5023 | |||
MinRK
|
r5066 | var QuickHelp = function (selector) { | ||
Fernando Perez
|
r5023 | }; | ||
Brian Granger
|
r5858 | QuickHelp.prototype.show_keyboard_shortcuts = function () { | ||
// toggles display of keyboard shortcut dialog | ||||
Fernando Perez
|
r5023 | var that = this; | ||
Paul Ivanov
|
r15771 | if ( this.force_rebuild ) { | ||
Paul Ivanov
|
r15844 | this.shortcut_dialog.remove(); | ||
Paul Ivanov
|
r15771 | delete(this.shortcut_dialog); | ||
this.force_rebuild = false; | ||||
} | ||||
Brian Granger
|
r5858 | if ( this.shortcut_dialog ){ | ||
// if dialog is already shown, close it | ||||
MinRK
|
r10939 | $(this.shortcut_dialog).modal("toggle"); | ||
Brian Granger
|
r5858 | return; | ||
} | ||||
Brian E. Granger
|
r14037 | var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); | ||
var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); | ||||
var help, shortcut; | ||||
Brian E. Granger
|
r14093 | var i, half, n; | ||
Brian E. Granger
|
r14037 | var element = $('<div/>'); | ||
Brian E. Granger
|
r14066 | // The documentation | ||
var doc = $('<div/>').addClass('alert'); | ||||
doc.append( | ||||
Matthias BUSSONNIER
|
r14634 | $('<button/>').addClass('close').attr('data-dismiss','alert').html('×') | ||
Brian E. Granger
|
r14066 | ).append( | ||
'The IPython Notebook has two different keyboard input modes. <b>Edit mode</b> '+ | ||||
Preston Holmes
|
r14949 | 'allows you to type code/text into a cell and is indicated by a green cell '+ | ||
Brian E. Granger
|
r14093 | 'border. <b>Command mode</b> binds the keyboard to notebook level actions '+ | ||
'and is indicated by a grey cell border.' | ||||
Paul Ivanov
|
r15840 | ); | ||
Brian E. Granger
|
r14066 | element.append(doc); | ||
Brian E. Granger
|
r14037 | // Command mode | ||
Brian E. Granger
|
r14096 | var cmd_div = this.build_command_help(); | ||
element.append(cmd_div); | ||||
// Edit mode | ||||
var edit_div = this.build_edit_help(); | ||||
element.append(edit_div); | ||||
this.shortcut_dialog = IPython.dialog.modal({ | ||||
title : "Keyboard shortcuts", | ||||
body : element, | ||||
destroy : false, | ||||
buttons : { | ||||
Close : {} | ||||
} | ||||
}); | ||||
Paul Ivanov
|
r15771 | |||
$([IPython.events]).on('rebuild.QuickHelp', function() { that.force_rebuild = true;}); | ||||
Brian E. Granger
|
r14096 | }; | ||
QuickHelp.prototype.build_command_help = function () { | ||||
var command_shortcuts = IPython.keyboard_manager.command_shortcuts.help(); | ||||
Paul Ivanov
|
r15801 | return build_div('<h4>Command Mode (press <code>Esc</code> to enable)</h4>', command_shortcuts); | ||
Paul Ivanov
|
r15840 | }; | ||
Brian E. Granger
|
r14096 | |||
Paul Ivanov
|
r15876 | var special_case = { pageup: "PageUp", pagedown: "Page Down", 'minus': '-' }; | ||
Paul Ivanov
|
r15782 | var prettify = function (s) { | ||
Paul Ivanov
|
r15877 | s = s.replace(/-$/, 'minus'); // catch shortcuts using '-' key | ||
Paul Ivanov
|
r15873 | var keys = s.split('-'); | ||
Paul Ivanov
|
r15782 | var k, i; | ||
for (i in keys) { | ||||
k = keys[i]; | ||||
Paul Ivanov
|
r15801 | if ( k.length == 1 ) { | ||
keys[i] = "<code><strong>" + k + "</strong></code>"; | ||||
continue; // leave individual keys lower-cased | ||||
} | ||||
Paul Ivanov
|
r15782 | keys[i] = ( special_case[k] ? special_case[k] : k.charAt(0).toUpperCase() + k.slice(1) ); | ||
Paul Ivanov
|
r15801 | keys[i] = "<code><strong>" + keys[i] + "</strong></code>"; | ||
Paul Ivanov
|
r15782 | } | ||
return keys.join('-'); | ||||
Paul Ivanov
|
r15781 | |||
}; | ||||
Brian E. Granger
|
r14096 | QuickHelp.prototype.build_edit_help = function () { | ||
var edit_shortcuts = IPython.keyboard_manager.edit_shortcuts.help(); | ||||
Brian E. Granger
|
r14037 | // Edit mode | ||
Paul Ivanov
|
r15801 | return build_div('<h4>Edit Mode (press <code>Enter</code> to enable)</h4>', edit_shortcuts); | ||
Paul Ivanov
|
r15840 | }; | ||
Paul Ivanov
|
r15801 | |||
var build_one = function (s) { | ||||
Paul Ivanov
|
r15840 | var help = s.help; | ||
var shortcut = prettify(s.shortcut); | ||||
Paul Ivanov
|
r15801 | return $('<div>').addClass('quickhelp'). | ||
append($('<span/>').addClass('shortcut_key').append($(shortcut))). | ||||
Paul Ivanov
|
r15840 | append($('<span/>').addClass('shortcut_descr').text(' : ' + help)); | ||
Paul Ivanov
|
r15801 | |||
Paul Ivanov
|
r15840 | }; | ||
Paul Ivanov
|
r15801 | |||
var build_div = function (title, shortcuts) { | ||||
var i, half, n; | ||||
var div = $('<div/>').append($(title)); | ||||
var sub_div = $('<div/>').addClass('hbox'); | ||||
MinRK
|
r15899 | var col1 = $('<div/>').addClass('box-flex1'); | ||
var col2 = $('<div/>').addClass('box-flex1'); | ||||
Paul Ivanov
|
r15801 | n = shortcuts.length; | ||
Brian E. Granger
|
r14093 | half = ~~(n/2); // Truncate :) | ||
Paul Ivanov
|
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]) ); } | ||||
Paul Ivanov
|
r15801 | sub_div.append(col1).append(col2); | ||
div.append(sub_div); | ||||
return div; | ||||
Paul Ivanov
|
r15840 | }; | ||
Fernando Perez
|
r5023 | |||
// Set module variables | ||||
MinRK
|
r5066 | IPython.QuickHelp = QuickHelp; | ||
Fernando Perez
|
r5023 | |||
return IPython; | ||||
}(IPython)); | ||||