Skip to content

Commit

Permalink
Adding back selected-parent class (#713)
Browse files Browse the repository at this point in the history
* Adding back in the `selected-parent` class on select of a component

* Cleaning up tests
  • Loading branch information
jonwinton committed Dec 27, 2016
1 parent 9ca5989 commit d8c7431
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
22 changes: 18 additions & 4 deletions services/components/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var _ = require('lodash'),
visibleComponents = require('./visible-components'),
hidden = 'kiln-hide',
selectorHeight = 56, // selector menus are 48px tall, offset is 8px
currentSelected;
currentSelected,
currentSelectedParent;

/**
* Get the closest component element from the DOM. Checks self and then parents.
Expand Down Expand Up @@ -108,7 +109,8 @@ function removePadding() {
* @param {MouseEvent} e
*/
function select(el) {
var component = getComponentEl(el);
var component = getComponentEl(el),
parent = getParentEl(component);

// only one component can be selected at a time
unselect();
Expand All @@ -120,21 +122,28 @@ function select(el) {
component.classList.add('kiln-suppress-animation');
}

// selected component gets .selected, parent gets .selected-parent
// selected component gets .selected
if (component && component.tagName !== 'HTML') {
component.classList.add('selected');
addPadding(component);
currentSelected = component;
}

// if there's a parent it gets .selected-parent
if (parent) {
parent.classList.add('selected-parent');
currentSelectedParent = parent;
}

window.kiln.trigger('select', component);
}

/**
* remove selection
*/
function unselect() {
var current = currentSelected || dom.find('.component-selector-wrapper.selected');
var current = currentSelected || dom.find('.component-selector-wrapper.selected'),
currentParent = current ? currentSelectedParent || getParentEl(current) : null;

if (current) {
current.classList.remove('kiln-suppress-animation'); // unsuppress initialFadeInOut animation
Expand All @@ -143,7 +152,12 @@ function unselect() {
window.kiln.trigger('unselect', current);
}

if (currentParent) {
currentParent.classList.remove('selected-parent');
}

currentSelected = null;
currentSelectedParent = null;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions services/components/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ describe(dirname, function () {
fn(el);
expect(el.classList.contains('selected')).to.equal(true);
});

it('adds .selected-parent class to parent component', function () {
var el = stubEditableElement(),
parent = stubComponent(),
component = stubComponent();

component.appendChild(el);
parent.appendChild(component);

fn(el);
expect(parent.classList.contains('selected-parent')).to.equal(true);
});
});

describe('unselect', function () {
Expand All @@ -70,6 +82,19 @@ describe(dirname, function () {
fn(); // unselect
expect(component.classList.contains('selected')).to.equal(false);
});

it('removed .selected-parent class from component parent', function () {
var el = stubEditableElement(),
component = stubComponent(),
parent = stubComponent();

component.appendChild(el);
parent.appendChild(component);

lib.select(el);
fn(); // unselect
expect(parent.classList.contains('selected-parent')).to.equal(false);
});
});

describe('when', function () {
Expand Down

0 comments on commit d8c7431

Please sign in to comment.