Skip to content

Commit 3e00db0

Browse files
author
Aleš
committed
feat:(sortgrid) ng-sg-active class implementation
1 parent f224ffb commit 3e00db0

File tree

8 files changed

+51
-15
lines changed

8 files changed

+51
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Alternative you can provide custom styles for the different classes listed bello
8787
| ng-sg-placeholder | This class is added to the placeholder item which previews where the item is inserted |
8888
| ng-sg-dropped | This class is added as soon after you drop an item. The class will be on the item for 500 milliseconds before it gets removed |
8989
| ng-sg-selected | This class is added when you press the CMD or the Ctrl Key and Click on an item. It indicates which items are selected for the multi drag&drop |
90+
| ng-sg-active | This class is added when dragging item| |
9091

9192
# Scrolling
9293
The ng-sortgrid has a *autoScroll* flag which you can use to enable autoScroll. If you enable autoScroll the screen will start to scroll

projects/ng-sortgrid/src/lib/helpers/class/ngsg-class.service.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,17 @@ describe('NgsgClassService', () => {
4949
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-selected');
5050
});
5151

52+
it('should add the active class', () => {
53+
const addClassSpy = createSpy();
54+
const element = {classList: {add: addClassSpy}} as any;
55+
sut.addActiveClass(element);
56+
expect(addClassSpy).toHaveBeenCalledWith('ng-sg-active');
57+
});
58+
59+
it('should remove the active class', () => {
60+
const removeClassSpy = createSpy();
61+
const element = {classList: {remove: removeClassSpy}} as any;
62+
sut.removeActiveClass(element);
63+
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-active');
64+
});
5265
});

projects/ng-sortgrid/src/lib/helpers/class/ngsg-class.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export class NgsgClassService {
77
private SELECTED_DEFAULT_CLASS = 'ng-sg-selected';
88
private PLACEHOLDER_DEFAULT_CLASS = 'ng-sg-placeholder';
99
private DROPPED_DEFAULT_CLASS = 'ng-sg-dropped';
10+
private ACTIVE_DEFAULT_CLASS = 'ng-sg-active';
1011

1112
public addPlaceHolderClass(element: Element): void {
1213
element.classList.add(this.PLACEHOLDER_DEFAULT_CLASS);
@@ -32,4 +33,12 @@ export class NgsgClassService {
3233
element.classList.remove(this.SELECTED_DEFAULT_CLASS);
3334
}
3435

36+
public addActiveClass(element: Element): void {
37+
element.classList.add(this.ACTIVE_DEFAULT_CLASS);
38+
}
39+
40+
public removeActiveClass(element: Element): void {
41+
element.classList.remove(this.ACTIVE_DEFAULT_CLASS);
42+
}
43+
3544
}

projects/ng-sortgrid/src/lib/mutliselect/ngsg-selection.service.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { NgsgSelectionService } from './ngsg-selection.service';
55
describe('NgsgSelectionService', () => {
66
const ngsgClassService = {
77
addSelectedClass: jest.fn(),
8+
addActiveClass: jest.fn(),
89
removeSelectedClass: jest.fn(),
10+
removeActiveClass: jest.fn(),
911
} as any;
1012

1113
const ngsgStore = {

projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ describe('NgsgItemDirective', () => {
2929
} as any;
3030
const ngsgEventService = new NgsgEventsService();
3131
const scrollHelperService = {
32-
scrollIfNecessary: () => {},
32+
scrollIfNecessary: () => { },
33+
} as any;
34+
const classService = {
35+
addActiveClass: jest.fn()
3336
} as any;
3437

3538
beforeEach(() => {
@@ -40,7 +43,8 @@ describe('NgsgItemDirective', () => {
4043
ngsgReflectService,
4144
ngsgStore,
4245
ngsgEventService,
43-
scrollHelperService
46+
scrollHelperService,
47+
classService
4448
);
4549
});
4650

@@ -69,6 +73,7 @@ describe('NgsgItemDirective', () => {
6973
} as any;
7074
sut.dragStart(event);
7175
expect(ngsgSelectionService.selectElementIfNoSelection).toHaveBeenCalledWith(sortGroup, event.target);
76+
expect(classService.addActiveClass).toHaveBeenCalledWith(event.target);
7277
});
7378

7479
it('should init the sort for the current group', () => {

projects/ng-sortgrid/src/lib/ngsg-item.directive.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {NgsgOrderChange} from './shared/ngsg-order-change.model';
2222
import {NgsgReflectService} from './sort/reflection/ngsg-reflect.service';
2323
import {NgsgSortService} from './sort/sort/ngsg-sort.service';
2424
import {NgsgStoreService} from './store/ngsg-store.service';
25+
import { NgsgClassService } from './helpers/class/ngsg-class.service';
2526

2627
const selector = '[ngSortgridItem]';
2728

@@ -46,7 +47,8 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
4647
private reflectService: NgsgReflectService,
4748
private ngsgStore: NgsgStoreService,
4849
private ngsgEventService: NgsgEventsService,
49-
private scrollHelperService: ScrollHelperService
50+
private scrollHelperService: ScrollHelperService,
51+
private classService: NgsgClassService
5052
) {
5153
}
5254

@@ -60,11 +62,11 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
6062
takeUntil(this.destroy$),
6163
takeWhile(() => this.autoScroll)
6264
).subscribe(() => {
63-
this.scrollHelperService.scrollIfNecessary(event, {
64-
top: this.scrollPointTop,
65-
bottom: this.scrollPointBottom
66-
}, this.scrollSpeed);
67-
}
65+
this.scrollHelperService.scrollIfNecessary(event, {
66+
top: this.scrollPointTop,
67+
bottom: this.scrollPointBottom
68+
}, this.scrollSpeed);
69+
}
6870
);
6971
}
7072

@@ -94,6 +96,7 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
9496
return;
9597
}
9698
this.selectionService.selectElementIfNoSelection(this.ngSortGridGroup, event.target);
99+
this.classService.addActiveClass(event.target);
97100
this.sortService.initSort(this.ngSortGridGroup);
98101
}
99102

projects/ng-sortgrid/src/lib/sort/sort/ngsg-sort.service.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ describe('NgsgSortService', () => {
88
addPlaceHolderClass: jest.fn(),
99
removePlaceHolderClass: jest.fn(),
1010
addDroppedClass: jest.fn(),
11+
addActiveClass: jest.fn(),
1112
removeSelectedClass: jest.fn(),
1213
removeDroppedClass: jest.fn(),
14+
removeActiveClass: jest.fn(),
1315
} as any;
1416
const ngsgStore = {
1517
getFirstSelectItem: jest.fn(),
@@ -21,13 +23,13 @@ describe('NgsgSortService', () => {
2123
});
2224

2325
const createElement = (value, nextSibling) =>
24-
({
25-
value,
26-
nextSibling,
27-
parentNode: {
28-
insertBefore: () => {},
29-
},
30-
} as any);
26+
({
27+
value,
28+
nextSibling,
29+
parentNode: {
30+
insertBefore: () => {},
31+
},
32+
} as any);
3133

3234
it('should insert the first element in the middle if we drag it to the right', () => {
3335
const group = 'test-group';

projects/ng-sortgrid/src/lib/sort/sort/ngsg-sort.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export class NgsgSortService {
5858
this.classService.removePlaceHolderClass(item);
5959
this.classService.addDroppedClass(item);
6060
this.classService.removeSelectedClass(item);
61+
this.classService.removeActiveClass(item);
6162
timer(500).subscribe(() => this.classService.removeDroppedClass(item));
6263
}
6364
}

0 commit comments

Comments
 (0)