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

feat(lib): add input for intersection mode #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions cypress/integration/dragging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
disableSelectOnDrag,
enableSelectMode,
getDesktopExample,
setDragIntersectionMode,
shouldBeInvisible,
shouldBeVisible,
toggleItem,
Expand Down Expand Up @@ -460,4 +461,25 @@ describe('Dragging', () => {
});
});
});

describe('intersection mode "included"', () => {
beforeEach(() => {
setDragIntersectionMode('included');
});

it('should only select items that have their bounding box fully included in the select box', () => {
getDesktopExample().within(() => {
cy.getSelectItem(0)
.dispatch('mousedown', { button: 0 })
.getSelectItem(11)
.dispatch('mousemove')
.as('end')
.getSelectBox()
.then(shouldBeVisible)
.shouldSelect([6, 7])
.get('@end')
.dispatch('mouseup');
});
});
});
});
9 changes: 9 additions & 0 deletions cypress/support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ export const enableSelectWithShortcut = () => {
return cy.get('[data-cy="selectWithShortcut"]').click();
};

export const setDragIntersectionMode = (mode: 'intersection' | 'included') => {
return cy
.get('[data-cy="intersectionMode"]')
.click()
.then(() => {
cy.get(`.cdk-overlay-container .mat-select-panel-wrap .mat-option-text:contains("${mode}")`).click();
});
};

export const selectAll = () => {
return getSelectAllButton().click();
};
Expand Down
15 changes: 13 additions & 2 deletions projects/ngx-drag-to-select/src/lib/select-container.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
getRelativeMousePosition,
getMousePosition,
hasMinimumSize,
boxIncluded,
} from './utils';
import { KeyboardEventsService } from './keyboard-events.service';

Expand Down Expand Up @@ -102,6 +103,10 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
@Input() disableRangeSelection = false;
@Input() selectMode = false;
@Input() selectWithShortcut = false;
/**
* Configure if an items' bounding box needs to intersect or to be fully included in the select box.
*/
@Input() intersectionMode: 'intersection' | 'included' = 'intersection';

@Input()
@HostBinding('class.dts-custom')
Expand Down Expand Up @@ -538,7 +543,10 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
}

private _normalSelectionMode(selectBox: BoundingBox, item: SelectItemDirective, event: Event) {
const inSelection = boxIntersects(selectBox, item.getBoundingClientRect());
const inSelection =
this.intersectionMode === 'intersection'
? boxIntersects(selectBox, item.getBoundingClientRect())
: boxIncluded(selectBox, item.getBoundingClientRect());

const shouldAdd = inSelection && !item.selected && !this.shortcuts.removeFromSelection(event);

Expand All @@ -554,7 +562,10 @@ export class SelectContainerComponent implements AfterViewInit, OnDestroy, After
}

private _extendedSelectionMode(selectBox, item: SelectItemDirective, event: Event) {
const inSelection = boxIntersects(selectBox, item.getBoundingClientRect());
const inSelection =
this.intersectionMode === 'intersection'
? boxIntersects(selectBox, item.getBoundingClientRect())
: boxIncluded(selectBox, item.getBoundingClientRect());

const shoudlAdd =
(inSelection && !item.selected && !this.shortcuts.removeFromSelection(event) && !this._tmpItems.has(item)) ||
Expand Down
9 changes: 9 additions & 0 deletions projects/ngx-drag-to-select/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export const boxIntersects = (boxA: BoundingBox, boxB: BoundingBox) => {
);
};

export const boxIncluded = (boxA: BoundingBox, boxB: BoundingBox) => {
return (
boxA.left <= boxB.left &&
boxA.left + boxA.width >= boxB.left + boxB.width &&
boxA.top <= boxB.top &&
boxA.top + boxA.height >= boxB.top + boxB.height
);
};

export const calculateBoundingClientRect = (element: HTMLElement): BoundingBox => {
return element.getBoundingClientRect();
};
Expand Down
16 changes: 13 additions & 3 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ <h3>
<mat-checkbox data-cy="selectMode" [(ngModel)]="selectMode">Select Mode</mat-checkbox>
<mat-checkbox data-cy="selectWithShortcut" [(ngModel)]="selectWithShortcut">Select with Shortcut</mat-checkbox>

<mat-form-field>
<mat-label>Intersection Mode</mat-label>
<mat-select data-cy="intersectionMode" [(ngModel)]="intersectionMode">
<mat-option value="intersection">intersection</mat-option>
<mat-option value="included">included</mat-option>
</mat-select>
</mat-form-field>

<div class="action-buttons">
<button data-cy="selectAll" mat-raised-button (click)="selectContainer.selectAll()">Select All</button>
<button data-cy="clearSelection" mat-raised-button (click)="selectContainer.clearSelection()">
Expand All @@ -49,12 +57,14 @@ <h3>
[selectOnDrag]="selectOnDrag"
[selectWithShortcut]="selectWithShortcut"
[dragOverItems]="dragOverItems"
[intersectionMode]="intersectionMode"
>
<mat-grid-list cols="4" rowHeight="200px" gutterSize="20px">
<mat-grid-tile
[dtsSelectItem]="document"
[dtsDisabled]="disableEvenItems && document.disabled"
*ngFor="let document of documents">
[dtsSelectItem]="document"
[dtsDisabled]="disableEvenItems && document.disabled"
*ngFor="let document of documents"
>
{{ document.name }}
</mat-grid-tile>
</mat-grid-list>
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class AppComponent implements OnInit {
selectWithShortcut = false;
dragOverItems = true;
disableEvenItems = false;
intersectionMode: 'intersection' | 'full' = 'intersection';

constructor(
private titleService: Title,
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MatChipsModule } from '@angular/material/chips';
import { MatTabsModule } from '@angular/material/tabs';
import { MatIconModule } from '@angular/material/icon';
import { MatButtonModule } from '@angular/material/button';
import { MatSelectModule } from '@angular/material/select';

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -36,6 +37,7 @@ const MATERIAL_MODULES = [
MatTabsModule,
MatIconModule,
MatButtonModule,
MatSelectModule,
];

@NgModule({
Expand Down