Skip to content

Commit

Permalink
[FEATURE] minResizeWidth + Event bubbling fix (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Oct 29, 2016
1 parent 6a376ee commit bc82287
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
8 changes: 8 additions & 0 deletions addon/classes/Column.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export default class Column extends Ember.Object.extend({
*/
align: 'left',

/**
* The minimum width (in px) that this column can be resized to.
* @property minResizeWidth
* @type {Number}
* @default 0
*/
minResizeWidth: 0,

/**
* An array of sub columns to be grouped together
* @property subColumns
Expand Down
3 changes: 2 additions & 1 deletion addon/components/columns/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const Column = Component.extend({
tagName: 'th',
classNames: ['lt-column'],
attributeBindings: ['style', 'colspan', 'rowspan'],
classNameBindings: ['align', 'isGroupColumn:lt-group-column', 'isHideable', 'isSortable', 'isSorted', 'isResizable', 'column.classNames'],
classNameBindings: ['align', 'isGroupColumn:lt-group-column', 'isHideable', 'isSortable', 'isSorted', 'isResizable', 'isResizing', 'column.classNames'],

isGroupColumn: computed.readOnly('column.isGroupColumn'),
isSortable: computed.readOnly('column.sortable'),
isSorted: computed.readOnly('column.sorted'),
isHideable: computed.readOnly('column.hideable'),
isResizable: computed.readOnly('column.resizable'),
isResizing: false,

style: computed('column.width', function() {
return cssStyleify(this.get('column').getProperties(['width']));
Expand Down
33 changes: 20 additions & 13 deletions addon/components/lt-column-resizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Ember from 'ember';
import layout from '../templates/components/lt-column-resizer';

const {
$
$,
computed
} = Ember;

export default Ember.Component.extend({
Expand All @@ -15,6 +16,10 @@ export default Ember.Component.extend({
startWidth: null,
startX: null,

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

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

Expand All @@ -41,28 +46,33 @@ export default Ember.Component.extend({
},

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

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

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

this.$().closest('.ember-light-table').addClass('is-resizing');
},

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

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

this.set('isResizing', false);
this.set('column.width', `${$column.width()}px`);
this.sendAction('columnResized', this.get('column.width'));
this.set('column.width', width);

this.sendAction('columnResized', width);
this.$().closest('.ember-light-table').removeClass('is-resizing');
}
},

Expand All @@ -72,19 +82,16 @@ export default Ember.Component.extend({
e.stopPropagation();

let resizeOnDrag = this.get('resizeOnDrag');
let $column = this._getColumn();
let $column = this.get('$column');
let minResizeWidth = this.get('column.minResizeWidth');
let { startX, startWidth } = this.getProperties(['startX', 'startWidth']);
let width = startWidth + (e.pageX - startX);
let width = Math.max(startWidth + (e.pageX - startX), minResizeWidth);

if (resizeOnDrag) {
this.set('column.width', `${width}px`);
} else {
$column.width(`${width}px`);
$column.outerWidth(`${width}px`);
}
}
},

_getColumn() {
return $(this.get('element')).parent('th');
}
});
8 changes: 8 additions & 0 deletions addon/styles/addon.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
user-select: none;
}

.ember-light-table .lt-column.is-resizing {
pointer-events: none;
}

.ember-light-table.is-resizing {
cursor: col-resize;
}

.ember-light-table .lt-column .lt-column-resizer {
width: 5px;
cursor: col-resize;
Expand Down
2 changes: 1 addition & 1 deletion addon/templates/components/columns/base.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
{{/if}}

{{#if isResizable}}
{{lt-column-resizer column=column resizeOnDrag=resizeOnDrag columnResized=(action 'columnResized')}}
{{lt-column-resizer column=column resizeOnDrag=resizeOnDrag isResizing=(mut isResizing) columnResized=(action 'columnResized')}}
{{/if}}
11 changes: 8 additions & 3 deletions tests/dummy/app/controllers/resizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,29 @@ export default TableController.extend({
label: 'First Name',
valuePath: 'firstName',
resizable: true,
minResizeWidth: 50,
width: '150px'
}, {
label: 'Last Name',
valuePath: 'lastName',
resizable: true,
minResizeWidth: 50,
width: '150px'
}, {
label: 'Address',
valuePath: 'address',
resizable: true
resizable: true,
minResizeWidth: 100
}, {
label: 'State',
valuePath: 'state',
resizable: true
resizable: true,
minResizeWidth: 100
}, {
label: 'Country',
valuePath: 'country',
resizable: true
resizable: true,
minResizeWidth: 100
}];
})
});

0 comments on commit bc82287

Please sign in to comment.