Skip to content

Commit

Permalink
Add disabled argument to sortable-item
Browse files Browse the repository at this point in the history
  • Loading branch information
jacojoubert committed Jun 23, 2021
1 parent 4db5107 commit 888dc5c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ modifier
</li>
```

### Multiple Ember-Sortables renders simultaneously (Modifier version)
### Multiple Ember-Sortables renders simultaneously (Modifier version)

The modifier version uses a service behind the scenes for communication between the group and the items and to maintain state. It does this seemlessly when the elements are rendered on the screen. However, if there are two sortables rendered at the same time, either in the same component or different components, the state management does not know which items belong to which group.

Expand Down Expand Up @@ -326,7 +326,7 @@ Ensure that the same name is passed to both the group and the items, this would


### Disabling Drag (Experimental)
`sortable-item` (component and modifier) exposes an optional `isDraggingDisabled` flag that you can use to disable `drag` on the particular item.
`sortable-item` (component and modifier) exposes an optional `disabled` (previously `isDraggingDisabled`) flag that you can use to disable reordering for that particular item.
This flag is intended as an utility to make your life easier with 3 main benefits:
1. You can now specify which `sortable-item` are not intended to be draggable/sortable.
2. You do not have to duplicate the `sortable-item` UI just for the purpose of disabling the `sorting` behavior.
Expand All @@ -350,8 +350,8 @@ component
an ordered list, `ol`, by default.
- `sortable-item`
a list item, `li`, by default.
The modifier version can be attached to to any element that makes sense,

The modifier version can be attached to to any element that makes sense,

##### Keyboard Navigation
There are 4 modes during keyboard navigation:
Expand Down
12 changes: 10 additions & 2 deletions addon/components/sortable-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,15 @@ export default Component.extend({
spacing: 0,

/**
Removes the ability for the current item to be dragged
Removes the ability for the current item to be reordered
@property disabled
@type Boolean
@default false
*/
disabled: false,

/**
Deprecated. Removes the ability for the current item to be reordered
@property isDraggingDisabled
@type Boolean
@default false
Expand Down Expand Up @@ -267,7 +275,7 @@ export default Component.extend({
*/
_primeDrag(startEvent) {
// Prevent dragging if the sortable-item is destroying or is disabled.
if (this.isDestroying || this.get('isDraggingDisabled')) {
if (this.isDestroying || this.isDisabled) {
return;
}

Expand Down
28 changes: 9 additions & 19 deletions addon/modifiers/sortable-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const sortableItemWaiter = buildWaiter("sortable-item-waiter");
* Modifier to mark an element as an item to be reordered
*
* @param {Object} model The model that this item will represent
* @param {boolean} [isDraggingDisabled=false] Set to true to make this item not draggable
* @param {boolean} [disabled=false] Set to true to make this item not sortable
* @param {Function} [onDragStart] An optional callback for when dragging starts.
* @param {Function} [onDragStop] An optional callback for when dragging stops.
*
Expand Down Expand Up @@ -64,7 +64,7 @@ export default class SortableItemModifier extends Modifier {
direction;

@reads("sortableGroup.disabled")
disabled;
groupDisabled;

@service('ember-sortable@ember-sortable')
sortableService;
Expand Down Expand Up @@ -102,13 +102,13 @@ export default class SortableItemModifier extends Modifier {
}

/**
Removes the ability for the current item to be dragged
@property isDraggingDisabled
Removes the ability for the current item to be sorted
@property disabled
@type boolean
@default false
*/
get isDraggingDisabled() {
return this.args.named.isDraggingDisabled || false;
get isDisabled() {
return this.groupDisabled || this.args.named.disabled || this.args.named.isDraggingDisabled || false;
}

/**
Expand Down Expand Up @@ -220,10 +220,7 @@ export default class SortableItemModifier extends Modifier {
isBusy;

/**
Removes the ability for the current item to be dragged
@property isDraggingDisabled
@type boolean
@default false
@property disableCheckScrollBounds
*/
get disableCheckScrollBounds() {
return this.args.named.disableCheckScrollBounds != undefined ? this.args.named.disableCheckScrollBounds : isTesting;
Expand All @@ -242,8 +239,7 @@ export default class SortableItemModifier extends Modifier {

@action
keyDown(event) {
// Prevents keyboard sorting if group is disabled
if (this.disabled) { return; }
if (this.isDisabled) { return; }

// If the event is coming from within the item, we do not want to activate keyboard reorder mode.
if (event.target === this.handleElement || event.target === this.element) {
Expand Down Expand Up @@ -302,13 +298,7 @@ export default class SortableItemModifier extends Modifier {
* @private
*/
_primeDrag(startEvent) {
// Prevent dragging if the entire group is disabled
if (this.disabled) { return; }

// Prevent dragging if the sortable-item is disabled.
if (this.isDraggingDisabled) {
return;
}
if (this.isDisabled) { return; }

if (this.handleElement && !startEvent.target.closest(this.handle)) {
return;
Expand Down

0 comments on commit 888dc5c

Please sign in to comment.