diff --git a/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.css b/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.css
new file mode 100644
index 0000000..be5f0a6
--- /dev/null
+++ b/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.css
@@ -0,0 +1,30 @@
+.example-box {
+ width: 200px;
+ height: 200px;
+ padding: 10px;
+ box-sizing: border-box;
+ border: solid 1px #ccc;
+ color: rgba(0, 0, 0, 0.87);
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ text-align: center;
+ background: #fff;
+ border-radius: 4px;
+ position: relative;
+ z-index: 1;
+ transition: box-shadow 200ms cubic-bezier(0, 0, 0.2, 1);
+ box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2),
+ 0 2px 2px 0 rgba(0, 0, 0, 0.14),
+ 0 1px 5px 0 rgba(0, 0, 0, 0.12);
+}
+
+.example-handle {
+ position: absolute;
+ top: 10px;
+ right: 10px;
+ color: #ccc;
+ cursor: move;
+ width: 24px;
+ height: 24px;
+}
diff --git a/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.html b/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.html
new file mode 100644
index 0000000..7aa2dd7
--- /dev/null
+++ b/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.html
@@ -0,0 +1,19 @@
+
1. Drag the items around using handle
+
+
+
diff --git a/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.ts b/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.ts
new file mode 100644
index 0000000..b77b84f
--- /dev/null
+++ b/projects/ng-sortgrid-demo/src/app/introduction/examples/drag-handle/drag-handle.component.ts
@@ -0,0 +1,12 @@
+import {Component} from '@angular/core';
+
+@Component({
+ selector: 'ngsg-demo-drag-handle',
+ templateUrl: 'drag-handle.component.html',
+ styleUrls: ['./drag-handle.component.css']
+})
+export class DragHandleComponent {
+
+ public items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+}
+
diff --git a/projects/ng-sortgrid-demo/src/app/introduction/introduction.component.html b/projects/ng-sortgrid-demo/src/app/introduction/introduction.component.html
index d17adb4..0c89868 100644
--- a/projects/ng-sortgrid-demo/src/app/introduction/introduction.component.html
+++ b/projects/ng-sortgrid-demo/src/app/introduction/introduction.component.html
@@ -36,4 +36,10 @@
5. Scrolling
+
+
+
6. Customizing the drag area using a handle
+
+
+
diff --git a/projects/ng-sortgrid-demo/src/app/introduction/introduction.module.ts b/projects/ng-sortgrid-demo/src/app/introduction/introduction.module.ts
index b13d541..ada54ac 100644
--- a/projects/ng-sortgrid-demo/src/app/introduction/introduction.module.ts
+++ b/projects/ng-sortgrid-demo/src/app/introduction/introduction.module.ts
@@ -5,6 +5,7 @@ import {NgsgModule} from '../../../../ng-sortgrid/src/lib/ngsg.module';
import {SharedModule} from '../shared/shared.module';
import {AsyncPipeMemoryComponent} from './examples/async-pipe/async-pipe-memory.component';
+import { DragHandleComponent } from './examples/drag-handle/drag-handle.component';
import {GettingStartedMemoryComponent} from './examples/getting-started/getting-started-memory.component';
import {GroupsMemoryComponent} from './examples/groups/groups-memory.component';
import {ReactOnChangesMemoryComponent} from './examples/react-on-changes/react-on-changes-memory.component';
@@ -17,7 +18,8 @@ import {IntroductionRoutingModule} from './introduction.routing.module';
GettingStartedMemoryComponent,
ReactOnChangesMemoryComponent,
GroupsMemoryComponent,
- AsyncPipeMemoryComponent
+ AsyncPipeMemoryComponent,
+ DragHandleComponent
],
imports: [
CommonModule,
diff --git a/projects/ng-sortgrid-demo/src/assets/gs7.png b/projects/ng-sortgrid-demo/src/assets/gs7.png
new file mode 100644
index 0000000..9ca09e0
Binary files /dev/null and b/projects/ng-sortgrid-demo/src/assets/gs7.png differ
diff --git a/projects/ng-sortgrid/src/lib/ngsg-drag-handle.directive.spec.ts b/projects/ng-sortgrid/src/lib/ngsg-drag-handle.directive.spec.ts
new file mode 100644
index 0000000..d36eb44
--- /dev/null
+++ b/projects/ng-sortgrid/src/lib/ngsg-drag-handle.directive.spec.ts
@@ -0,0 +1,17 @@
+import { NgsgDragHandleDirective } from './ngsg-drag-handle.directive';
+
+describe('NgsgDragHandleDirective', () => {
+ let sut: NgsgDragHandleDirective;
+
+ const elementRef = { nativeElement: {} } as any;
+
+ beforeEach(() => {
+ sut = new NgsgDragHandleDirective(
+ elementRef
+ );
+ });
+
+ it('should create an instance', () => {
+ expect(sut).toBeTruthy();
+ });
+});
diff --git a/projects/ng-sortgrid/src/lib/ngsg-drag-handle.directive.ts b/projects/ng-sortgrid/src/lib/ngsg-drag-handle.directive.ts
new file mode 100644
index 0000000..8265440
--- /dev/null
+++ b/projects/ng-sortgrid/src/lib/ngsg-drag-handle.directive.ts
@@ -0,0 +1,9 @@
+import { Directive, ElementRef } from '@angular/core';
+
+@Directive({
+ selector: '[ngSortgridDragHandle]'
+})
+export class NgsgDragHandleDirective {
+
+ constructor(public el: ElementRef){}
+}
diff --git a/projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts b/projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts
index 55b5667..493f93f 100644
--- a/projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts
+++ b/projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts
@@ -48,9 +48,9 @@ describe('NgsgItemDirective', () => {
);
});
- it('should set the draggable attribute on the elment', () => {
+ it('should not set the draggable attribute on the elment', () => {
sut.ngAfterViewInit();
- expect((elementRef.nativeElement as any).draggable).toBeTruthy();
+ expect((elementRef.nativeElement as any).draggable).toBeFalsy();
});
it('should not set selectedElements if the event did not occur on the host', () => {
diff --git a/projects/ng-sortgrid/src/lib/ngsg-item.directive.ts b/projects/ng-sortgrid/src/lib/ngsg-item.directive.ts
index 96af658..e8965c7 100644
--- a/projects/ng-sortgrid/src/lib/ngsg-item.directive.ts
+++ b/projects/ng-sortgrid/src/lib/ngsg-item.directive.ts
@@ -1,5 +1,6 @@
import {
AfterViewInit,
+ ContentChild,
Directive,
ElementRef,
EventEmitter,
@@ -23,6 +24,7 @@ import {NgsgReflectService} from './sort/reflection/ngsg-reflect.service';
import {NgsgSortService} from './sort/sort/ngsg-sort.service';
import {NgsgStoreService} from './store/ngsg-store.service';
import { NgsgClassService } from './helpers/class/ngsg-class.service';
+import { NgsgDragHandleDirective } from './ngsg-drag-handle.directive';
const selector = '[ngSortgridItem]';
@@ -37,6 +39,9 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
@Output() sorted = new EventEmitter>();
+ @ContentChild(NgsgDragHandleDirective) handle: NgsgDragHandleDirective;
+
+ private handleElement: HTMLElement;
private selected = false;
private destroy$ = new Subject();
@@ -82,7 +87,14 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
}
ngAfterViewInit(): void {
- this.el.nativeElement.draggable = true;
+ this.handleElement = this.handle?.el?.nativeElement || this.el.nativeElement;
+
+ fromEvent(this.handleElement, 'mousedown').pipe(
+ takeUntil(this.destroy$)
+ ).subscribe(() => {
+ this.el.nativeElement.draggable = true;
+ }
+ );
}
ngOnDestroy(): void {
@@ -119,6 +131,7 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
@HostListener('dragend')
drop(): void {
+ this.el.nativeElement.draggable = false;
if (!this.ngsgStore.hasSelectedItems(this.ngSortGridGroup)) {
return;
}
diff --git a/projects/ng-sortgrid/src/lib/ngsg.module.ts b/projects/ng-sortgrid/src/lib/ngsg.module.ts
index 6b93a55..6158a45 100644
--- a/projects/ng-sortgrid/src/lib/ngsg.module.ts
+++ b/projects/ng-sortgrid/src/lib/ngsg.module.ts
@@ -1,9 +1,10 @@
import { NgModule } from '@angular/core';
import { NgsgItemDirective } from './ngsg-item.directive';
+import { NgsgDragHandleDirective } from './ngsg-drag-handle.directive';
@NgModule({
- declarations: [NgsgItemDirective],
- exports: [NgsgItemDirective]
+ declarations: [NgsgItemDirective, NgsgDragHandleDirective],
+ exports: [NgsgItemDirective, NgsgDragHandleDirective]
})
export class NgsgModule {}