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

Remove jQuery usage #593

Merged
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
47 changes: 25 additions & 22 deletions addon/components/lt-column-resizer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import $ from 'jquery';
import Component from '@ember/component';
import { computed } from '@ember/object';
import layout from '../templates/components/lt-column-resizer';
import closest from '../utils/closest';

const TOP_LEVEL_CLASS = '.ember-light-table';

Expand All @@ -15,8 +15,8 @@ export default Component.extend({
startWidth: null,
startX: null,

$column: computed(function() {
return $(this.get('element')).parent('th');
colElement: computed(function() {
return this.get('element').parentNode;
}).volatile().readOnly(),

didInsertElement() {
Expand All @@ -25,15 +25,14 @@ export default Component.extend({
this.__mouseMove = this._mouseMove.bind(this);
this.__mouseUp = this._mouseUp.bind(this);

$(document).on('mousemove', this.__mouseMove);
$(document).on('mouseup', this.__mouseUp);
document.addEventListener('mousemove', this.__mouseMove);
document.addEventListener('mouseup', this.__mouseUp);
},

willDestroyElement() {
this._super(...arguments);

$(document).off('mousemove', this.__mouseMove);
$(document).off('mouseup', this.__mouseUp);
document.removeEventListener('mousemove', this.__mouseMove);
document.removeEventListener('mouseip', this.__mouseUp);
},

click(e) {
Expand All @@ -45,33 +44,35 @@ export default Component.extend({
},

mouseDown(e) {
let $column = this.get('$column');
let column = this.get('colElement');

e.preventDefault();
e.stopPropagation();

this.setProperties({
isResizing: true,
startWidth: $column.outerWidth(),
startWidth: column.offsetWidth,
startX: e.pageX
});

this.$().closest(TOP_LEVEL_CLASS).addClass('is-resizing');
let topLevel = closest(this.get('element'), TOP_LEVEL_CLASS);
topLevel.classList.add('is-resizing');
},

_mouseUp(e) {
if (this.get('isResizing')) {
e.preventDefault();
e.stopPropagation();

let $column = this.get('$column');
let width = `${$column.outerWidth()}px`;
let column = this.get('colElement');
let width = `${column.offsetWidth}px`;

this.set('isResizing', false);
this.set('column.width', width);

let topLevel = closest(this.get('element'), TOP_LEVEL_CLASS);
topLevel.classList.remove('is-resizing');
this.onColumnResized(width);
this.$().closest(TOP_LEVEL_CLASS).removeClass('is-resizing');
}
},

Expand All @@ -85,16 +86,18 @@ export default Component.extend({
let { startX, startWidth } = this.getProperties(['startX', 'startWidth']);
let width = `${Math.max(startWidth + (e.pageX - startX), minResizeWidth)}px`;

let $column = this.get('$column');
let $index = this.get('table.visibleColumns').indexOf(this.get('column')) + 1;
let $table = this.$().closest(TOP_LEVEL_CLASS);

$column.outerWidth(width);
$(`thead td.lt-scaffolding:nth-child(${$index})`, $table).outerWidth(width);
$(`tfoot td.lt-scaffolding:nth-child(${$index})`, $table).outerWidth(width);
let column = this.get('colElement');
let index = this.get('table.visibleColumns').indexOf(this.get('column')) + 1;
let table = closest(this.get('element'), TOP_LEVEL_CLASS);

column.width = width;
table.querySelector(`thead td.lt-scaffolding:nth-child(${index})`).style.width = width;
table.querySelector(`tfoot td.lt-scaffolding:nth-child(${index})`).style.width = width;
if (resizeOnDrag) {
$(`tbody td:nth-child(${$index})`, $table).outerWidth(width);
let cols = table.querySelectorAll(`tbody td:nth-child(${index})`);
cols.forEach((col) => {
col.style.width = width;
});
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion addon/components/lt-infinity.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default Component.extend(InViewportMixin, {
this._super(...arguments);

let scrollBuffer = this.get('scrollBuffer');
let width = this.$().width();
let width = this.element.offsetWidth;
let scrollableContent = this.get('scrollableContent');

this.setProperties({
Expand Down
20 changes: 20 additions & 0 deletions addon/utils/closest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

/**
* A polyfill for jQuery .closest() method
* @param { Object } el Dom element to start from
* @param { String } selector Selector to match
* @return { Object } The closest matching node or null
*/
const closest = (el, selector) => {
let parent;
while (el) {
parent = el.parentElement;
if (parent && parent.matches(selector)) {
return parent;
}
el = parent;
}
return null;
};

export default closest;
47 changes: 47 additions & 0 deletions tests/helpers/table-columns.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,50 @@ export const GroupedColumns = [{
valuePath: 'country'
}]
}];

export const ResizableColumns = [{
label: 'User Details',
sortable: false,
align: 'center',

subColumns: [{
label: 'Avatar',
valuePath: 'avatar',
width: '60px',
sortable: false,
cellComponent: 'user-avatar'
}, {
label: 'First',
resizable: true,
valuePath: 'firstName',
width: '150px',
minResizeWidth: 75
}, {
label: 'Last',
resizable: true,
valuePath: 'lastName',
width: '150px',
minResizeWidth: 75
}]
}, {
label: 'Contact Information',
sortable: false,
align: 'center',

subColumns: [{
label: 'Address',
resizable: true,
valuePath: 'address',
minResizeWidth: 100
}, {
label: 'State',
resizable: true,
valuePath: 'state',
minResizeWidth: 100
}, {
label: 'Country',
resizable: true,
valuePath: 'country',
minResizeWidth: 100
}]
}];
15 changes: 14 additions & 1 deletion tests/integration/components/light-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { module, test } from 'qunit';
import hbs from 'htmlbars-inline-precompile';
import setupMirageTest from 'ember-cli-mirage/test-support/setup-mirage';
import Table from 'ember-light-table';
import Columns from '../../helpers/table-columns';
import Columns, { ResizableColumns } from '../../helpers/table-columns';
import hasClass from '../../helpers/has-class';
import RowComponent from 'ember-light-table/components/lt-row';
import Component from '@ember/component';
Expand Down Expand Up @@ -260,4 +260,17 @@ module('Integration | Component | light table', function(hooks) {
await click(element);
}
});

test('dragging resizes columns', async function(assert) {
let table = new Table(ResizableColumns, this.server.createList('user', 10));
this.setProperties({ table });
await render(hbs `
{{#light-table table height='40vh' as |t|}}
{{t.head fixed=true}}
{{t.body}}
{{/light-table}}
`);
let ths = this.element.querySelectorAll('th.is-resizable');
assert.equal(ths.length, 5);
});
});