Skip to content

Commit 3a73fdc

Browse files
committed
Add a dialog to show all available commands for key mappings under the options page.
1 parent 954a842 commit 3a73fdc

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

background_page.html

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
/*
152152
* Retrieves the help dialog HTML template from a file, and populates it with the latest keybindings.
153153
*/
154-
function helpDialogHtml() {
154+
function helpDialogHtml(showUnboundCommands, showCommandNames, customTitle) {
155155
var commandsToKey = {};
156156
for (var key in keyToCommandRegistry) {
157157
var command = keyToCommandRegistry[key].command;
@@ -160,21 +160,32 @@
160160
var dialogHtml = fetchFileContents("helpDialog.html");
161161
for (var group in commandGroups)
162162
dialogHtml = dialogHtml.replace("{{" + group + "}}",
163-
helpDialogHtmlForCommandGroup(group, commandsToKey, availableCommands));
163+
helpDialogHtmlForCommandGroup(group, commandsToKey, availableCommands,
164+
showUnboundCommands, showCommandNames));
164165
dialogHtml = dialogHtml.replace("{{version}}", currentVersion);
166+
dialogHtml = dialogHtml.replace("{{title}}", customTitle || "Help");
165167
return dialogHtml;
166168
}
167169

168170
/*
169171
* Generates HTML for a given set of commands. commandGroups are defined in commands.js
170172
*/
171-
function helpDialogHtmlForCommandGroup(group, commandsToKey, availableCommands) {
173+
function helpDialogHtmlForCommandGroup(group, commandsToKey, availableCommands,
174+
showUnboundCommands, showCommandNames) {
172175
var html = [];
173176
for (var i = 0; i < commandGroups[group].length; i++) {
174177
var command = commandGroups[group][i];
175-
if (commandsToKey[command])
176-
html.push("<tr><td>", escapeHtml(commandsToKey[command].join(", ")),
177-
"</td><td>:</td><td>",availableCommands[command].description, "</td></tr>");
178+
bindings = (commandsToKey[command] || [""]).join(", ")
179+
if (showUnboundCommands || commandsToKey[command])
180+
{
181+
html.push("<tr><td>", escapeHtml(bindings),
182+
"</td><td>:</td><td>", availableCommands[command].description);
183+
184+
if (showCommandNames)
185+
html.push("<span class='commandName'>(" + command + ")</span>");
186+
187+
html.push("</td></tr>");
188+
}
178189
}
179190
return html.join("\n");
180191
}

helpDialog.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
font-weight:bold;
5151
padding-top:3px;
5252
}
53+
#vimiumHelpDialog .commandName {
54+
font-family:"courier new";
55+
}
5356
#vimiumHelpDialog .closeButton {
5457
position:absolute;
5558
right:10px;
@@ -72,7 +75,7 @@
7275
<!-- Note that the template placeholders (e.g. "pageNavigation") will be filled in by the background
7376
page with the up-to-date key bindings when the dialog is shown. -->
7477
<a class="closeButton" href="#">x</a>
75-
<div id="vimiumTitle">Vim<span style="color:#2f508e">ium</span> Help</div>
78+
<div id="vimiumTitle"><span style="color:#2f508e">Vim</span>ium {{title}}</div>
7679
<div class="vimiumColumn">
7780
<table>
7881
<tr><td></td><td></td><td class="vimiumHelpSectionTitle">Navigating the page</td></tr>

options.html

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<script type="text/javascript">
6767
$ = function(id) { return document.getElementById(id); };
6868
isShowingHelpDialog = false;
69+
isShowingCommandListing = false;
6970

7071
var defaultSettings = chrome.extension.getBackgroundPage().defaultSettings;
7172

@@ -82,6 +83,7 @@
8283
for (var i = 0; i < editableFields.length; i++)
8384
$(editableFields[i]).addEventListener("keyup", onOptionKeyup, false);
8485
$("advancedOptions").addEventListener("click", openAdvancedOptions, false);
86+
$("showCommands").addEventListener("click", showCommandListing, false);
8587
document.addEventListener("keydown", onKeydown, true);
8688
}
8789

@@ -135,7 +137,7 @@
135137
}
136138

137139
function showHelpDialog() {
138-
if (isShowingHelpDialog)
140+
if (isShowingHelpDialog || isShowingCommandListing)
139141
return false;
140142
html = chrome.extension.getBackgroundPage().helpDialogHtml();
141143
isShowingHelpDialog = true;
@@ -155,6 +157,27 @@
155157
helpDialog.parentNode.removeChild(helpDialog);
156158
}
157159

160+
function showCommandListing() {
161+
if (isShowingCommandListing || isShowingHelpDialog)
162+
return false;
163+
html = chrome.extension.getBackgroundPage().helpDialogHtml(true, true, "Command Listing");
164+
isShowingCommandListing = true;
165+
var container = document.createElement("div");
166+
container.id = "vimiumCommandListingContainer";
167+
container.innerHTML = html;
168+
container.getElementsByClassName("closeButton")[0].addEventListener("click", hideCommandListing, false);
169+
document.body.appendChild(container);
170+
var dialog = document.getElementById("vimiumHelpDialog");
171+
dialog.style.top = Math.max((window.innerHeight - dialog.clientHeight) / 2.0, 20) + "px";
172+
}
173+
174+
function hideCommandListing() {
175+
isShowingCommandListing = false;
176+
var commandListing = document.getElementById("vimiumCommandListingContainer");
177+
if (commandListing)
178+
commandListing.parentNode.removeChild(commandListing);
179+
}
180+
158181
function onKeydown(event) {
159182
var keyChar = getKeyChar(event);
160183
var isFormField = ["INPUT", "TEXTAREA"].indexOf(event.target.tagName) >= 0;
@@ -213,6 +236,7 @@ <h1>Vimium - Options</h1>
213236
" this is a comment<br/>
214237
# this is also a comment<br/>
215238
</div>
239+
<a href="#" id="showCommands">Show available commands.</a>
216240
</div>
217241
</div>
218242
<textarea id="keyMappings" type="text"></textarea>

0 commit comments

Comments
 (0)