-
Notifications
You must be signed in to change notification settings - Fork 221
feat: support custom gutter template #360
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { InjectionToken } from '@angular/core' | ||
|
|
||
| /** | ||
| * Identifies the gutter by number through DI | ||
| * to allow SplitGutterDragHandleDirective and SplitGutterExcludeFromDragDirective to know | ||
| * the gutter template context without inputs | ||
| */ | ||
| export const GUTTER_NUM_TOKEN = new InjectionToken<number>('Gutter num') |
22 changes: 22 additions & 0 deletions
22
projects/angular-split/src/lib/gutter/split-gutter-drag-handle.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Directive, OnInit, OnDestroy, Inject, ElementRef } from '@angular/core' | ||
| import { SplitGutterDirective } from './split-gutter.directive' | ||
| import { GUTTER_NUM_TOKEN } from './gutter-num-token' | ||
|
|
||
| @Directive({ | ||
| selector: '[asSplitGutterDragHandle]', | ||
| }) | ||
| export class SplitGutterDragHandleDirective implements OnInit, OnDestroy { | ||
| constructor( | ||
| @Inject(GUTTER_NUM_TOKEN) private gutterNum: number, | ||
| private elementRef: ElementRef<HTMLElement>, | ||
| private gutterDir: SplitGutterDirective, | ||
| ) {} | ||
|
|
||
| ngOnInit(): void { | ||
| this.gutterDir.addToMap(this.gutterDir.gutterToHandleElementMap, this.gutterNum, this.elementRef) | ||
| } | ||
|
|
||
| ngOnDestroy(): void { | ||
| this.gutterDir.removedFromMap(this.gutterDir.gutterToHandleElementMap, this.gutterNum, this.elementRef) | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
projects/angular-split/src/lib/gutter/split-gutter-dynamic-injector.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { Injector, Directive, Input, ViewContainerRef, TemplateRef } from '@angular/core' | ||
| import { GUTTER_NUM_TOKEN } from './gutter-num-token' | ||
|
|
||
| interface SplitGutterDynamicInjectorTemplateContext { | ||
| $implicit: Injector | ||
| } | ||
|
|
||
| /** | ||
| * This directive allows creating a dynamic injector inside ngFor | ||
| * with dynamic gutter num and expose the injector for ngTemplateOutlet usage | ||
| */ | ||
| @Directive({ | ||
| selector: '[asSplitGutterDynamicInjector]', | ||
| }) | ||
| export class SplitGutterDynamicInjectorDirective { | ||
| @Input('asSplitGutterDynamicInjector') | ||
| public set gutterNum(value: number) { | ||
| this.vcr.clear() | ||
|
|
||
| const injector = Injector.create({ | ||
| providers: [ | ||
| { | ||
| provide: GUTTER_NUM_TOKEN, | ||
| useValue: value, | ||
| }, | ||
| ], | ||
| parent: this.vcr.injector, | ||
| }) | ||
|
|
||
| this.vcr.createEmbeddedView(this.templateRef, { $implicit: injector }) | ||
| } | ||
|
|
||
| constructor( | ||
| private vcr: ViewContainerRef, | ||
| private templateRef: TemplateRef<SplitGutterDynamicInjectorTemplateContext>, | ||
| ) {} | ||
|
|
||
| static ngTemplateContextGuard( | ||
| dir: SplitGutterDynamicInjectorDirective, | ||
| ctx: unknown, | ||
| ): ctx is SplitGutterDynamicInjectorTemplateContext { | ||
| return true | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
projects/angular-split/src/lib/gutter/split-gutter-exclude-from-drag.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Directive, OnInit, OnDestroy, Inject, ElementRef } from '@angular/core' | ||
| import { SplitGutterDirective } from './split-gutter.directive' | ||
| import { GUTTER_NUM_TOKEN } from './gutter-num-token' | ||
|
|
||
| @Directive({ | ||
| selector: '[asSplitGutterExcludeFromDrag]', | ||
| }) | ||
| export class SplitGutterExcludeFromDragDirective implements OnInit, OnDestroy { | ||
| constructor( | ||
| @Inject(GUTTER_NUM_TOKEN) private gutterNum: number, | ||
| private elementRef: ElementRef<HTMLElement>, | ||
| private gutterDir: SplitGutterDirective, | ||
| ) {} | ||
|
|
||
| ngOnInit(): void { | ||
| this.gutterDir.addToMap(this.gutterDir.gutterToExcludeDragElementMap, this.gutterNum, this.elementRef) | ||
| } | ||
|
|
||
| ngOnDestroy(): void { | ||
| this.gutterDir.removedFromMap(this.gutterDir.gutterToExcludeDragElementMap, this.gutterNum, this.elementRef) | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
projects/angular-split/src/lib/gutter/split-gutter.directive.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { Directive, ElementRef, TemplateRef } from '@angular/core' | ||
| import { IArea } from '../interface' | ||
|
|
||
| export interface SplitGutterTemplateContext { | ||
| /** | ||
| * The area before the gutter. | ||
| * In RTL the right area and in LTR the left area | ||
| */ | ||
| areaBefore: IArea | ||
| /** | ||
| * The area after the gutter. | ||
| * In RTL the left area and in LTR the right area | ||
| */ | ||
| areaAfter: IArea | ||
| /** | ||
| * The absolute number of the gutter based on direction (RTL and LTR). | ||
| * First gutter is 1, second is 2, etc... | ||
| */ | ||
| gutterNum: number | ||
| /** | ||
| * Whether this is the first gutter. | ||
| * In RTL the most right area and in LTR the most left area | ||
| */ | ||
| first: boolean | ||
| /** | ||
| * Whether this is the last gutter. | ||
| * In RTL the most left area and in LTR the most right area | ||
| */ | ||
| last: boolean | ||
| /** | ||
| * Whether the gutter is being dragged now | ||
| */ | ||
| isDragged: boolean | ||
| } | ||
|
|
||
| @Directive({ | ||
| selector: '[asSplitGutter]', | ||
| }) | ||
| export class SplitGutterDirective { | ||
| /** | ||
| * The map holds reference to the drag handle elements inside instances | ||
| * of the provided template. | ||
| */ | ||
| public gutterToHandleElementMap = new Map<number, ElementRef<HTMLElement>[]>() | ||
| /** | ||
| * The map holds reference to the excluded drag elements inside instances | ||
| * of the provided template. | ||
| */ | ||
| public gutterToExcludeDragElementMap = new Map<number, ElementRef<HTMLElement>[]>() | ||
|
|
||
| constructor(public template: TemplateRef<SplitGutterTemplateContext>) {} | ||
|
|
||
| public canStartDragging(originElement: HTMLElement, gutterNum: number) { | ||
| if (this.gutterToExcludeDragElementMap.has(gutterNum)) { | ||
| const isInsideExclude = this.gutterToExcludeDragElementMap | ||
| .get(gutterNum) | ||
| .some((gutterExcludeElement) => gutterExcludeElement.nativeElement.contains(originElement)) | ||
|
|
||
| if (isInsideExclude) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| if (this.gutterToHandleElementMap.has(gutterNum)) { | ||
| return this.gutterToHandleElementMap | ||
| .get(gutterNum) | ||
| .some((gutterHandleElement) => gutterHandleElement.nativeElement.contains(originElement)) | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| public addToMap(map: Map<number, ElementRef<HTMLElement>[]>, gutterNum: number, elementRef: ElementRef<HTMLElement>) { | ||
| if (map.has(gutterNum)) { | ||
| map.get(gutterNum).push(elementRef) | ||
| } else { | ||
| map.set(gutterNum, [elementRef]) | ||
| } | ||
| } | ||
|
|
||
| public removedFromMap( | ||
| map: Map<number, ElementRef<HTMLElement>[]>, | ||
| gutterNum: number, | ||
| elementRef: ElementRef<HTMLElement>, | ||
| ) { | ||
| const elements = map.get(gutterNum) | ||
| elements.splice(elements.indexOf(elementRef), 1) | ||
|
|
||
| if (elements.length === 0) { | ||
| map.delete(gutterNum) | ||
| } | ||
| } | ||
|
|
||
| static ngTemplateContextGuard(dir: SplitGutterDirective, ctx: unknown): ctx is SplitGutterTemplateContext { | ||
| return true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.