Skip to content

Commit

Permalink
Change (deprecated) KeyboardEvent.keyCode -> KeyboardEvent.key (amppr…
Browse files Browse the repository at this point in the history
…oject#18530)

* Change KeyboardEvent.keyCode -> KeyboardEvent.key

* Linter issues

* Change KeyCodes enum to be strings

* which fix
  • Loading branch information
nainar committed Oct 4, 2018
1 parent 42b8f9a commit 36fe72c
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 111 deletions.
16 changes: 8 additions & 8 deletions extensions/amp-accordion/0.1/amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {ActionTrust} from '../../../src/action-constants';
import {Animation} from '../../../src/animation';
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Layout} from '../../../src/layout';
import {Services} from '../../../src/services';
import {bezierCurve} from '../../../src/curve';
Expand Down Expand Up @@ -487,14 +487,14 @@ class AmpAccordion extends AMP.BaseElement {
if (event.defaultPrevented) {
return;
}
const {keyCode} = event;
switch (keyCode) {
case KeyCodes.UP_ARROW: /* fallthrough */
case KeyCodes.DOWN_ARROW:
const {key} = event;
switch (key) {
case Keys.UP_ARROW: /* fallthrough */
case Keys.DOWN_ARROW:
this.navigationKeyDownHandler_(event);
return;
case KeyCodes.ENTER: /* fallthrough */
case KeyCodes.SPACE:
case Keys.ENTER: /* fallthrough */
case Keys.SPACE:
if (event.target == event.currentTarget) {
// Only activate if header element was activated directly.
// Do not respond to key presses on its children.
Expand All @@ -516,7 +516,7 @@ class AmpAccordion extends AMP.BaseElement {
if (index !== -1) {
event.preventDefault();
// Up and down are the same regardless of locale direction.
const diff = event.keyCode == KeyCodes.UP_ARROW ? -1 : 1;
const diff = event.key == Keys.UP_ARROW ? -1 : 1;
// If user navigates one past the beginning or end, wrap around.
let newFocusIndex = (index + diff) % this.headers_.length;
if (newFocusIndex < 0) {
Expand Down
12 changes: 6 additions & 6 deletions extensions/amp-accordion/0.1/test/test-amp-accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import '../amp-accordion';
import {KeyCodes} from '../../../../src/utils/key-codes';
import {Keys} from '../../../../src/utils/key-codes';
import {poll} from '../../../../testing/iframe';
import {tryFocus} from '../../../../src/dom';

Expand Down Expand Up @@ -369,7 +369,7 @@ describes.realWin('amp-accordion', {
const headerElements = doc.querySelectorAll(
'section > *:first-child');
const keyDownEvent = {
keyCode: KeyCodes.SPACE,
key: Keys.SPACE,
target: headerElements[0],
currentTarget: headerElements[0],
preventDefault: sandbox.spy(),
Expand All @@ -391,7 +391,7 @@ describes.realWin('amp-accordion', {
const child = doc.createElement('div');
headerElements[0].appendChild(child);
const keyDownEvent = {
keyCode: KeyCodes.ENTER,
key: Keys.ENTER,
target: child,
currentTarget: headerElements[0],
preventDefault: sandbox.spy(),
Expand All @@ -411,7 +411,7 @@ describes.realWin('amp-accordion', {
const headerElements = doc.querySelectorAll(
'section > *:first-child');
const keyDownEvent = {
keyCode: KeyCodes.ENTER,
key: Keys.ENTER,
target: headerElements[1],
currentTarget: headerElements[1],
preventDefault: sandbox.spy(),
Expand All @@ -435,7 +435,7 @@ describes.realWin('amp-accordion', {
expect(doc.activeElement)
.to.equal(headerElements[0]);
const upArrowEvent = {
keyCode: KeyCodes.UP_ARROW,
key: Keys.UP_ARROW,
target: headerElements[0],
currentTarget: headerElements[0],
preventDefault: sandbox.spy(),
Expand All @@ -444,7 +444,7 @@ describes.realWin('amp-accordion', {
expect(doc.activeElement)
.to.equal(headerElements[headerElements.length - 1]);
const downArrowEvent = {
keyCode: KeyCodes.DOWN_ARROW,
key: Keys.DOWN_ARROW,
target: headerElements[headerElements.length - 1],
currentTarget: headerElements[headerElements.length - 1],
preventDefault: sandbox.spy(),
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-carousel/0.1/base-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Services} from '../../../src/services';

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ export class BaseCarousel extends AMP.BaseElement {
button.classList.add(className);
button.setAttribute('role', 'button');
button.onkeydown = event => {
if (event.keyCode == KeyCodes.ENTER || event.keyCode == KeyCodes.SPACE) {
if (event.key == Keys.ENTER || event.key == Keys.SPACE) {
if (!event.defaultPrevented) {
event.preventDefault();
onInteraction();
Expand Down
8 changes: 4 additions & 4 deletions extensions/amp-date-picker/0.1/amp-date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {DEFAULT_FORMAT, DEFAULT_LOCALE, FORMAT_STRINGS} from './constants';
import {DatesList} from './dates-list';
import {Deferred} from '../../../src/utils/promise';
import {FiniteStateMachine} from '../../../src/finite-state-machine';
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Layout, isLayoutSizeDefined} from '../../../src/layout';
import {Services} from '../../../src/services';
import {batchFetchJsonFor} from '../../../src/batched-json';
Expand Down Expand Up @@ -948,7 +948,7 @@ export class AmpDatePicker extends AMP.BaseElement {
* @private
*/
handleDocumentKeydown_(e) {
if (e.keyCode == KeyCodes.ESCAPE &&
if (e.key == Keys.ESCAPE &&
this.mode_ == DatePickerMode.OVERLAY &&
this.element.contains(this.document_.activeElement)) {
this.transitionTo_(DatePickerState.OVERLAY_CLOSED);
Expand All @@ -967,7 +967,7 @@ export class AmpDatePicker extends AMP.BaseElement {
return;
}

if (e.keyCode == KeyCodes.DOWN_ARROW) {
if (e.key == Keys.DOWN_ARROW) {
this.updateDateFieldFocus_(target);
this.transitionTo_(DatePickerState.OVERLAY_OPEN_PICKER);
if (this.mode_ === DatePickerMode.STATIC) {
Expand All @@ -978,7 +978,7 @@ export class AmpDatePicker extends AMP.BaseElement {
}
}
e.preventDefault();
} else if (e.keyCode == KeyCodes.ESCAPE) {
} else if (e.key == Keys.ESCAPE) {
this.transitionTo_(DatePickerState.OVERLAY_CLOSED);
} else {
this.transitionTo_(DatePickerState.OVERLAY_OPEN_INPUT);
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-image-lightbox/0.1/amp-image-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
TapzoomRecognizer,
} from '../../../src/gesture-recognizers';
import {Gestures} from '../../../src/gesture';
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Services} from '../../../src/services';
import {bezierCurve} from '../../../src/curve';
import {continueMotion} from '../../../src/motion';
Expand Down Expand Up @@ -849,7 +849,7 @@ class AmpImageLightbox extends AMP.BaseElement {
* @private
*/
closeOnEscape_(event) {
if (event.keyCode == KeyCodes.ESCAPE) {
if (event.key == Keys.ESCAPE) {
this.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as lolex from 'lolex';
import {
ImageViewer,
} from '../amp-image-lightbox';
import {KeyCodes} from '../../../../src/utils/key-codes';
import {Keys} from '../../../../src/utils/key-codes';
import {Services} from '../../../../src/services';
import {parseSrcset} from '../../../../src/srcset';

Expand Down Expand Up @@ -190,7 +190,7 @@ describes.realWin('amp-image-lightbox component', {
ampImage.setAttribute('width', '100');
ampImage.setAttribute('height', '100');
impl.activate({caller: ampImage});
impl.closeOnEscape_({keyCode: KeyCodes.ESCAPE});
impl.closeOnEscape_({key: Keys.ESCAPE});
expect(setupCloseSpy).to.be.calledOnce;

// Regression test: ensure escape event listener is bound properly
Expand Down
14 changes: 7 additions & 7 deletions extensions/amp-lightbox-gallery/0.1/amp-lightbox-gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
VIDEO_TAGS,
} from './service/lightbox-manager-impl';
import {Gestures} from '../../../src/gesture';
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Services} from '../../../src/services';
import {SwipeYRecognizer} from '../../../src/gesture-recognizers';
import {bezierCurve} from '../../../src/curve';
Expand Down Expand Up @@ -1268,19 +1268,19 @@ export class AmpLightboxGallery extends AMP.BaseElement {
if (!this.isActive_) {
return;
}
const {keyCode} = event;
switch (keyCode) {
case KeyCodes.ESCAPE:
const {key} = event;
switch (key) {
case Keys.ESCAPE:
this.close_();
break;
case KeyCodes.LEFT_ARROW:
case Keys.LEFT_ARROW:
this.maybeSlideCarousel_(/*direction*/ -1);
break;
case KeyCodes.RIGHT_ARROW:
case Keys.RIGHT_ARROW:
this.maybeSlideCarousel_(/*direction*/ 1);
break;
default:
// Keycode not registered. Do nothing.
// Key not registered. Do nothing.
}
}

Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-lightbox/0.1/amp-lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {ActionTrust} from '../../../src/action-constants';
import {AmpEvents} from '../../../src/amp-events';
import {CSS} from '../../../build/amp-lightbox-0.1.css';
import {Gestures} from '../../../src/gesture';
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Services} from '../../../src/services';
import {SwipeXYRecognizer} from '../../../src/gesture-recognizers';
import {
Expand Down Expand Up @@ -395,7 +395,7 @@ class AmpLightbox extends AMP.BaseElement {
* @private
*/
closeOnEscape_(event) {
if (event.keyCode == KeyCodes.ESCAPE) {
if (event.key == Keys.ESCAPE) {
this.close();
}
}
Expand Down
32 changes: 16 additions & 16 deletions extensions/amp-selector/0.1/amp-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import {ActionTrust} from '../../../src/action-constants';
import {AmpEvents} from '../../../src/amp-events';
import {CSS} from '../../../build/amp-selector-0.1.css';
import {KeyCodes} from '../../../src/utils/key-codes';
import {Keys} from '../../../src/utils/key-codes';
import {Services} from '../../../src/services';
import {areEqualOrdered} from '../../../src/utils/array';
import {closestBySelector, isRTL, tryFocus} from '../../../src/dom';
Expand Down Expand Up @@ -431,18 +431,18 @@ export class AmpSelector extends AMP.BaseElement {
if (this.element.hasAttribute('disabled')) {
return;
}
const {keyCode} = event;
switch (keyCode) {
case KeyCodes.LEFT_ARROW: /* fallthrough */
case KeyCodes.UP_ARROW: /* fallthrough */
case KeyCodes.RIGHT_ARROW: /* fallthrough */
case KeyCodes.DOWN_ARROW:
const {key} = event;
switch (key) {
case Keys.LEFT_ARROW: /* fallthrough */
case Keys.UP_ARROW: /* fallthrough */
case Keys.RIGHT_ARROW: /* fallthrough */
case Keys.DOWN_ARROW:
if (this.kbSelectMode_ != KEYBOARD_SELECT_MODES.NONE) {
this.navigationKeyDownHandler_(event);
}
return;
case KeyCodes.ENTER: /* fallthrough */
case KeyCodes.SPACE:
case Keys.ENTER: /* fallthrough */
case Keys.SPACE:
this.selectionKeyDownHandler_(event);
return;
}
Expand All @@ -456,20 +456,20 @@ export class AmpSelector extends AMP.BaseElement {
navigationKeyDownHandler_(event) {
const doc = this.win.document;
let dir = 0;
switch (event.keyCode) {
case KeyCodes.LEFT_ARROW:
switch (event.key) {
case Keys.LEFT_ARROW:
// Left is considered 'previous' in LTR and 'next' in RTL.
dir = isRTL(doc) ? 1 : -1;
break;
case KeyCodes.UP_ARROW:
case Keys.UP_ARROW:
// Up is considered 'previous' in both LTR and RTL.
dir = -1;
break;
case KeyCodes.RIGHT_ARROW:
case Keys.RIGHT_ARROW:
// Right is considered 'next' in LTR and 'previous' in RTL.
dir = isRTL(doc) ? -1 : 1;
break;
case KeyCodes.DOWN_ARROW:
case Keys.DOWN_ARROW:
// Down is considered 'next' in both LTR and RTL.
dir = 1;
break;
Expand Down Expand Up @@ -506,8 +506,8 @@ export class AmpSelector extends AMP.BaseElement {
* @param {!Event} event
*/
selectionKeyDownHandler_(event) {
const {keyCode} = event;
if (keyCode == KeyCodes.SPACE || keyCode == KeyCodes.ENTER) {
const {key} = event;
if (key == Keys.SPACE || key == Keys.ENTER) {
if (this.options_.includes(event.target)) {
event.preventDefault();
const el = dev().assertElement(event.target);
Expand Down

0 comments on commit 36fe72c

Please sign in to comment.