Skip to content

Commit

Permalink
feat:(sortgrid) ng-sg-active class implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleš committed Oct 7, 2021
1 parent f224ffb commit 3e00db0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Alternative you can provide custom styles for the different classes listed bello
| ng-sg-placeholder | This class is added to the placeholder item which previews where the item is inserted |
| 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 |
| 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 |
| ng-sg-active | This class is added when dragging item| |

# Scrolling
The ng-sortgrid has a *autoScroll* flag which you can use to enable autoScroll. If you enable autoScroll the screen will start to scroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ describe('NgsgClassService', () => {
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-selected');
});

it('should add the active class', () => {
const addClassSpy = createSpy();
const element = {classList: {add: addClassSpy}} as any;
sut.addActiveClass(element);
expect(addClassSpy).toHaveBeenCalledWith('ng-sg-active');
});

it('should remove the active class', () => {
const removeClassSpy = createSpy();
const element = {classList: {remove: removeClassSpy}} as any;
sut.removeActiveClass(element);
expect(removeClassSpy).toHaveBeenCalledWith('ng-sg-active');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class NgsgClassService {
private SELECTED_DEFAULT_CLASS = 'ng-sg-selected';
private PLACEHOLDER_DEFAULT_CLASS = 'ng-sg-placeholder';
private DROPPED_DEFAULT_CLASS = 'ng-sg-dropped';
private ACTIVE_DEFAULT_CLASS = 'ng-sg-active';

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

public addActiveClass(element: Element): void {
element.classList.add(this.ACTIVE_DEFAULT_CLASS);
}

public removeActiveClass(element: Element): void {
element.classList.remove(this.ACTIVE_DEFAULT_CLASS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { NgsgSelectionService } from './ngsg-selection.service';
describe('NgsgSelectionService', () => {
const ngsgClassService = {
addSelectedClass: jest.fn(),
addActiveClass: jest.fn(),
removeSelectedClass: jest.fn(),
removeActiveClass: jest.fn(),
} as any;

const ngsgStore = {
Expand Down
9 changes: 7 additions & 2 deletions projects/ng-sortgrid/src/lib/ngsg-item.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe('NgsgItemDirective', () => {
} as any;
const ngsgEventService = new NgsgEventsService();
const scrollHelperService = {
scrollIfNecessary: () => {},
scrollIfNecessary: () => { },
} as any;
const classService = {
addActiveClass: jest.fn()
} as any;

beforeEach(() => {
Expand All @@ -40,7 +43,8 @@ describe('NgsgItemDirective', () => {
ngsgReflectService,
ngsgStore,
ngsgEventService,
scrollHelperService
scrollHelperService,
classService
);
});

Expand Down Expand Up @@ -69,6 +73,7 @@ describe('NgsgItemDirective', () => {
} as any;
sut.dragStart(event);
expect(ngsgSelectionService.selectElementIfNoSelection).toHaveBeenCalledWith(sortGroup, event.target);
expect(classService.addActiveClass).toHaveBeenCalledWith(event.target);
});

it('should init the sort for the current group', () => {
Expand Down
15 changes: 9 additions & 6 deletions projects/ng-sortgrid/src/lib/ngsg-item.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {NgsgOrderChange} from './shared/ngsg-order-change.model';
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';

const selector = '[ngSortgridItem]';

Expand All @@ -46,7 +47,8 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
private reflectService: NgsgReflectService,
private ngsgStore: NgsgStoreService,
private ngsgEventService: NgsgEventsService,
private scrollHelperService: ScrollHelperService
private scrollHelperService: ScrollHelperService,
private classService: NgsgClassService
) {
}

Expand All @@ -60,11 +62,11 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
takeUntil(this.destroy$),
takeWhile(() => this.autoScroll)
).subscribe(() => {
this.scrollHelperService.scrollIfNecessary(event, {
top: this.scrollPointTop,
bottom: this.scrollPointBottom
}, this.scrollSpeed);
}
this.scrollHelperService.scrollIfNecessary(event, {
top: this.scrollPointTop,
bottom: this.scrollPointBottom
}, this.scrollSpeed);
}
);
}

Expand Down Expand Up @@ -94,6 +96,7 @@ export class NgsgItemDirective implements OnInit, OnChanges, AfterViewInit, OnDe
return;
}
this.selectionService.selectElementIfNoSelection(this.ngSortGridGroup, event.target);
this.classService.addActiveClass(event.target);
this.sortService.initSort(this.ngSortGridGroup);
}

Expand Down
16 changes: 9 additions & 7 deletions projects/ng-sortgrid/src/lib/sort/sort/ngsg-sort.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ describe('NgsgSortService', () => {
addPlaceHolderClass: jest.fn(),
removePlaceHolderClass: jest.fn(),
addDroppedClass: jest.fn(),
addActiveClass: jest.fn(),
removeSelectedClass: jest.fn(),
removeDroppedClass: jest.fn(),
removeActiveClass: jest.fn(),
} as any;
const ngsgStore = {
getFirstSelectItem: jest.fn(),
Expand All @@ -21,13 +23,13 @@ describe('NgsgSortService', () => {
});

const createElement = (value, nextSibling) =>
({
value,
nextSibling,
parentNode: {
insertBefore: () => {},
},
} as any);
({
value,
nextSibling,
parentNode: {
insertBefore: () => {},
},
} as any);

it('should insert the first element in the middle if we drag it to the right', () => {
const group = 'test-group';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class NgsgSortService {
this.classService.removePlaceHolderClass(item);
this.classService.addDroppedClass(item);
this.classService.removeSelectedClass(item);
this.classService.removeActiveClass(item);
timer(500).subscribe(() => this.classService.removeDroppedClass(item));
}
}

0 comments on commit 3e00db0

Please sign in to comment.