Skip to content
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
14 changes: 14 additions & 0 deletions addon/components/grid-stack-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ export default class extends Component {
}
}

/**
* @method didUpdateAttrs
* @override
*/
didUpdateAttrs() {
super.didUpdateAttrs(...arguments);

let parent = this.parentContainer;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let parent = this.parentContainer;
const parent = this.parentContainer;

if (parent && parent.gridStack) {
let { height, width, x, y } = this.options;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let { height, width, x, y } = this.options;
const { height, width, x, y } = this.options;

parent.gridStack.update(this.element, x, y, width, height);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do this for height, width, x, y do we need to keep attributeBindings for height, width, x, y?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there is any benefit in adding an exclusion list. It would be nice to remove the attributeBindings completely, since they are not supported in glimmer components.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the benefit would be not updating the DOM multiple times (with update() & with attributeBindings) when grid-stack-item args update. It also would be nice to have a single path to set x, y, width, height (and all args).

It would be nice to remove the attributeBindings

Totally agree 👍

I spoke with a gridstack maintainer on slack, looks like direct attribute updates are not officially supported. I filed an issue for this enhancement. Another issue is that not all item options are supported in the current update() method. I filed an issue for that too :).

Given, the need for a larger refactor, I think we can merge this and clean up the flow separately.

Thanks for your work on this 👍

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fix the GridStack.update() to take all options (easy) but not sure I will get to (or should) track DOM changes to update internal state.

}
}

/**
* @method didInsertElement
* @override
Expand Down
2 changes: 1 addition & 1 deletion addon/components/grid-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class GridStackComponent extends Component {
}

GRID_STACK_EVENTS.forEach(eventName => {
let action = get(this, `attrs.on${capitalize(eventName)}`);
let action = get(this, `on${capitalize(eventName)}`);

if (action) {
this.gridStack.on(eventName, function() {
Expand Down
56 changes: 56 additions & 0 deletions tests/integration/components/grid-stack-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,60 @@ module('Integration | Component | grid stack', function(hooks) {
.dom('.grid-stack .grid-stack-item.ui-draggable.ui-resizable.ui-draggable-disabled.ui-resizable-disabled')
.exists({ count: 1 }, 'grid-stack-item noResize and noMove properties are applied');
});

test('gridstack is notified when items properties change', async function(assert) {
assert.expect(1);

this.set('onChange', () => assert.ok(true, '`onChange` was called'));
this.set('width', 6);

await render(hbs`
<GridStack @onChange={{this.onChange}}>
<GridStackItem @options={{hash x=0 y=0 width=this.width height=1}}>
Test
</GridStackItem>
</GridStack>
`);

this.set('width', 12);
});
Comment thread
balajimanoharan marked this conversation as resolved.

test('gridstack updates the layout when items properties change', async function(assert) {
this.set('items', [
{ id: 0, options: { x: 0, y: 0, width: 5, height: 1 } },
{ id: 1, options: { x: 6, y: 0, width: 3, height: 1 } },
{ id: 2, options: { x: 0, y: 1, width: 4, height: 1 } },
{ id: 3, options: { x: 6, y: 1, width: 2, height: 1 } }
]);

await render(hbs`
<GridStack>
{{#each this.items as |item|}}
<GridStackItem data-id={{item.id}} @options={{item.options}}>
{{item.id}}
</GridStackItem>
{{/each}}
</GridStack>
`);

this.items.forEach(({id, options}) => {
assert.dom(`[data-id="${id}"]`).hasAttribute('data-gs-x', `${options.x}`, 'Initial grid-stack-item layout is correct');
assert.dom(`[data-id="${id}"]`).hasAttribute('data-gs-y', `${options.y}`, 'Initial grid-stack-item layout is correct');
});

// Update the position of item 1
this.set('items.1.options', { x: 2, y: 1, width: 12, height: 1 });

assert.dom(`[data-id="0"]`).hasAttribute('data-gs-x', '0', 'Updating a grid-stack-item leaves unaffected items the same');
assert.dom(`[data-id="0"]`).hasAttribute('data-gs-y', '0', 'Updating a grid-stack-item leaves unaffected items the same');

assert.dom(`[data-id="1"]`).hasAttribute('data-gs-x', '2', 'Updating a grid-stack-item updates moves the item');
assert.dom(`[data-id="1"]`).hasAttribute('data-gs-y', '1', 'Updating a grid-stack-item updates moves the item');

assert.dom(`[data-id="2"]`).hasAttribute('data-gs-x', '0', 'Updating a grid-stack-item moves conflicting items to a different row');
assert.dom(`[data-id="2"]`).hasAttribute('data-gs-y', '2', 'Updating a grid-stack-item moves conflicting items to a different row');

assert.dom(`[data-id="3"]`).hasAttribute('data-gs-x', '6', 'Updating a grid-stack-item moves conflicting items to a different row');
assert.dom(`[data-id="3"]`).hasAttribute('data-gs-y', '2', 'Updating a grid-stack-item moves conflicting items to a different row');
});
});