Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Chore: Refactoring Controls for mobile" #171

Merged
merged 4 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 16 additions & 30 deletions src/lib/Controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ class Controls {
this.controlsEl = this.controlsWrapperEl.appendChild(document.createElement('div'));
this.controlsEl.className = 'bp-controls';

// Bind event handlers
this.mousemoveHandler = throttle(this.mousemoveHandler.bind(this), CONTROLS_AUTO_HIDE_TIMEOUT_IN_MILLIS - 500);
this.moouseenterHandler = this.mouseenterHandler.bind(this);
this.mouseleaveHandler = this.mouseleaveHandler.bind(this);
this.focusinHandler = this.focusinHandler.bind(this);
this.focusoutHandler = this.focusoutHandler.bind(this);

this.bindControlListeners();
this.containerEl.addEventListener('mousemove', this.mousemoveHandler);
this.controlsEl.addEventListener('mouseenter', this.mouseenterHandler);
this.controlsEl.addEventListener('mouseleave', this.mouseleaveHandler);
this.controlsEl.addEventListener('focusin', this.focusinHandler);
this.controlsEl.addEventListener('focusout', this.focusoutHandler);
}

/**
Expand All @@ -50,14 +47,6 @@ class Controls {
});
}

bindControlListeners() {
this.containerEl.addEventListener('mousemove', this.mousemoveHandler);
this.controlsEl.addEventListener('mouseenter', this.mouseenterHandler);
this.controlsEl.addEventListener('mouseleave', this.mouseleaveHandler);
this.controlsEl.addEventListener('focusin', this.focusinHandler);
this.controlsEl.addEventListener('focusout', this.focusoutHandler);
}

/**
* Checks if the button is a preview controls button
*
Expand All @@ -66,10 +55,7 @@ class Controls {
* @return {boolean} true if element is a preview control button
*/
isPreviewControlButton(element) {
return (
!!element &&
(element.classList.contains('bp-controls-btn') || element.parentNode.classList.contains('bp-controls-btn'))
);
return !!element && element.classList.contains('bp-controls-btn');
}

/**
Expand Down Expand Up @@ -99,56 +85,56 @@ class Controls {
* @private
* @return {void}
*/
mousemoveHandler() {
mousemoveHandler = throttle(() => {
this.containerEl.classList.add(SHOW_PREVIEW_CONTROLS_CLASS);
this.resetTimeout();
}
}, CONTROLS_AUTO_HIDE_TIMEOUT_IN_MILLIS - 500);

/**
* Mouse enter handler
*
* @private
* @return {void}
*/
mouseenterHandler() {
mouseenterHandler = () => {
this.blockHiding = true;
}
};

/**
* Mouse leave handler
*
* @private
* @return {void}
*/
mouseleaveHandler() {
mouseleaveHandler = () => {
this.blockHiding = false;
}
};

/**
* Handles all focusin events for the module.
*
* @param {Event} event - A DOM-normalized event object.
* @return {void}
*/
focusinHandler(event) {
focusinHandler = (event) => {
// When we focus onto a preview control button, show controls
if (this.isPreviewControlButton(event.target)) {
this.containerEl.classList.add(SHOW_PREVIEW_CONTROLS_CLASS);
}
}
};

/**
* Handles all focusout events for the module.
*
* @param {Event} event - A DOM-normalized event object.
* @return {void}
*/
focusoutHandler(event) {
focusoutHandler = (event) => {
// When we focus out of a control button and aren't focusing onto another control button, hide the controls
if (this.isPreviewControlButton(event.target) && !this.isPreviewControlButton(event.relatedTarget)) {
this.containerEl.classList.remove(SHOW_PREVIEW_CONTROLS_CLASS);
}
}
};

/**
* Adds buttons to controls
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Controls.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
transition: opacity .5s;
}

.box-show-preview-controls .bp-controls {
.box-show-preview-controls .bp-controls,
.bp-controls:hover,
.bp-controls:focus {
opacity: 1;
}

Expand Down
70 changes: 0 additions & 70 deletions src/lib/MobileControls.js

This file was deleted.

39 changes: 8 additions & 31 deletions src/lib/__tests__/Controls-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ let controls;
let clock;

const sandbox = sinon.sandbox.create();
let stubs;

const SHOW_PREVIEW_CONTROLS_CLASS = 'box-show-preview-controls';

Expand All @@ -18,7 +17,6 @@ describe('lib/Controls', () => {
beforeEach(() => {
fixture.load('__tests__/Controls-test.html');
controls = new Controls(document.getElementById('test-controls-container'));
stubs = {};
});

afterEach(() => {
Expand All @@ -30,7 +28,6 @@ describe('lib/Controls', () => {
}

controls = null;
stubs = null;
});

describe('constructor()', () => {
Expand All @@ -47,15 +44,15 @@ describe('lib/Controls', () => {

describe('destroy()', () => {
it('should remove the correct event listeners', () => {
stubs.containerElEventListener = sandbox.stub(controls.containerEl, 'removeEventListener');
stubs.controlsElEventListener = sandbox.stub(controls.controlsEl, 'removeEventListener');
const containerElEventListener = sandbox.stub(controls.containerEl, 'removeEventListener');
const controlsElEventListener = sandbox.stub(controls.controlsEl, 'removeEventListener');

controls.destroy();
expect(stubs.containerElEventListener).to.be.calledWith('mousemove', controls.mousemoveHandler);
expect(stubs.controlsElEventListener).to.be.calledWith('mouseenter', controls.mouseenterHandler);
expect(stubs.controlsElEventListener).to.be.calledWith('mouseleave', controls.mouseleaveHandler);
expect(stubs.controlsElEventListener).to.be.calledWith('focusin', controls.focusinHandler);
expect(stubs.controlsElEventListener).to.be.calledWith('focusout', controls.focusoutHandler);
expect(containerElEventListener).to.be.calledWith('mousemove', controls.mousemoveHandler);
expect(controlsElEventListener).to.be.calledWith('mouseenter', controls.mouseenterHandler);
expect(controlsElEventListener).to.be.calledWith('mouseleave', controls.mouseleaveHandler);
expect(controlsElEventListener).to.be.calledWith('focusin', controls.focusinHandler);
expect(controlsElEventListener).to.be.calledWith('focusout', controls.focusoutHandler);
});

it('should remove click listeners for any button references', () => {
Expand All @@ -75,35 +72,15 @@ describe('lib/Controls', () => {
});
});

describe('bindControlListeners()', () => {
it('should add the correct event listeners', () => {
stubs.addContainerElListener = sandbox.stub(controls.containerEl, 'addEventListener');
stubs.addControlsElListener = sandbox.stub(controls.controlsEl, 'addEventListener');

controls.bindControlListeners();
expect(stubs.addContainerElListener).to.be.calledWith('mousemove', controls.mousemoveHandler);
expect(stubs.addControlsElListener).to.be.calledWith('mouseenter', controls.mouseenterHandler);
expect(stubs.addControlsElListener).to.be.calledWith('mouseleave', controls.mouseleaveHandler);
expect(stubs.addControlsElListener).to.be.calledWith('focusin');
expect(stubs.addControlsElListener).to.be.calledWith('focusout');
});
});

describe('isPreviewControlButton()', () => {
it('should determine whether the element is a preview control button', () => {
let element = null;
let parent = null;
expect(controls.isPreviewControlButton(element)).to.be.false;

parent = document.createElement('div');
element = parent.appendChild(document.createElement('div'));
element = document.createElement('div');
element.className = 'bp-controls-btn';
expect(controls.isPreviewControlButton(element)).to.be.true;

parent.className = 'bp-controls-btn';
expect(controls.isPreviewControlButton(element)).to.be.true;

parent.className = '';
element.className = '';
expect(controls.isPreviewControlButton(element)).to.be.false;
});
Expand Down
1 change: 0 additions & 1 deletion src/lib/__tests__/MobileControls-test.html

This file was deleted.

Loading