|
76 | 76 | highlightSourceLines(null);
|
77 | 77 | $(window).on('hashchange', highlightSourceLines);
|
78 | 78 |
|
79 |
| - // Helper function for Keyboard events, |
80 |
| - // Get's the char from the keypress event |
| 79 | + // Gets the human-readable string for the virtual-key code of the |
| 80 | + // given KeyboardEvent, ev. |
81 | 81 | //
|
82 |
| - // This method is used because e.wich === x is not |
83 |
| - // compatible with non-english keyboard layouts |
| 82 | + // This function is meant as a polyfill for KeyboardEvent#key, |
| 83 | + // since it is not supported in Trident. We also test for |
| 84 | + // KeyboardEvent#keyCode because the handleShortcut handler is |
| 85 | + // also registered for the keydown event, because Blink doesn't fire |
| 86 | + // keypress on hitting the Escape key. |
84 | 87 | //
|
85 |
| - // Note: event.type must be keypress ! |
86 |
| - function getChar(event) { |
87 |
| - if (event.which == null) { |
88 |
| - return String.fromCharCode(event.keyCode) // IE |
89 |
| - } else if (event.which!=0 && event.charCode!=0) { |
90 |
| - return String.fromCharCode(event.which) // the rest |
91 |
| - } else { |
92 |
| - return null // special key |
93 |
| - } |
| 88 | + // So I guess you could say things are getting pretty interoperable. |
| 89 | + function getVirtualKey(ev) { |
| 90 | + if ("key" in ev && typeof ev.key != "undefined") |
| 91 | + return ev.key; |
| 92 | + |
| 93 | + var c = ev.charCode || ev.keyCode; |
| 94 | + if (c == 27) |
| 95 | + return "Escape"; |
| 96 | + return String.fromCharCode(c); |
94 | 97 | }
|
95 | 98 |
|
96 |
| - $(document).on('keypress', function handleKeyboardShortcut(e) { |
97 |
| - if (document.activeElement.tagName === 'INPUT') { |
| 99 | + function handleShortcut(ev) { |
| 100 | + if (document.activeElement.tagName == "INPUT") |
98 | 101 | return;
|
99 |
| - } |
100 | 102 |
|
101 |
| - if (getChar(e) === '?') { |
102 |
| - if (e.shiftKey && $('#help').hasClass('hidden')) { |
103 |
| - e.preventDefault(); |
104 |
| - $('#help').removeClass('hidden'); |
| 103 | + switch (getVirtualKey(ev)) { |
| 104 | + case "Escape": |
| 105 | + if (!$("#help").hasClass("hidden")) { |
| 106 | + ev.preventDefault(); |
| 107 | + $("#help").addClass("hidden"); |
| 108 | + } else if (!$("#search").hasClass("hidden")) { |
| 109 | + ev.preventDefault(); |
| 110 | + $("#search").addClass("hidden"); |
| 111 | + $("#main").removeClass("hidden"); |
105 | 112 | }
|
106 |
| - } else if (getChar(e) === 's' || getChar(e) === 'S') { |
107 |
| - e.preventDefault(); |
108 |
| - $('.search-input').focus(); |
109 |
| - } |
110 |
| - }).on('keydown', function(e) { |
111 |
| - // The escape key event has to be captured with the keydown event. |
112 |
| - // Because keypressed has no keycode for the escape key |
113 |
| - // (and other special keys in general)... |
114 |
| - if (document.activeElement.tagName === 'INPUT') { |
115 |
| - return; |
116 |
| - } |
117 |
| - |
118 |
| - if (e.keyCode === 27) { // escape key |
119 |
| - if (!$('#help').hasClass('hidden')) { |
120 |
| - e.preventDefault(); |
121 |
| - $('#help').addClass('hidden'); |
122 |
| - } else if (!$('#search').hasClass('hidden')) { |
123 |
| - e.preventDefault(); |
124 |
| - $('#search').addClass('hidden'); |
125 |
| - $('#main').removeClass('hidden'); |
| 113 | + break; |
| 114 | + |
| 115 | + case "s": |
| 116 | + case "S": |
| 117 | + ev.preventDefault(); |
| 118 | + $(".search-input").focus(); |
| 119 | + break; |
| 120 | + |
| 121 | + case "?": |
| 122 | + if (ev.shiftKey && $("#help").hasClass("hidden")) { |
| 123 | + ev.preventDefault(); |
| 124 | + $("#help").removeClass("hidden"); |
126 | 125 | }
|
| 126 | + break; |
127 | 127 | }
|
128 |
| - }).on('click', function(e) { |
129 |
| - if (!$(e.target).closest('#help').length) { |
130 |
| - $('#help').addClass('hidden'); |
| 128 | + } |
| 129 | + |
| 130 | + $(document).on("keypress", handleShortcut); |
| 131 | + $(document).on("keydown", handleShortcut); |
| 132 | + $(document).on("click", function(ev) { |
| 133 | + if (!$(ev.target).closest("#help").length) { |
| 134 | + $("#help").addClass("hidden"); |
131 | 135 | }
|
132 | 136 | });
|
133 | 137 |
|
134 |
| - |
135 | 138 | $('.version-selector').on('change', function() {
|
136 | 139 | var i, match,
|
137 | 140 | url = document.location.href,
|
|
150 | 153 |
|
151 | 154 | document.location.href = url;
|
152 | 155 | });
|
| 156 | + |
153 | 157 | /**
|
154 | 158 | * A function to compute the Levenshtein distance between two strings
|
155 | 159 | * Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported
|
|
0 commit comments