Skip to content

Commit

Permalink
feat: 🎸 parse public methods for directives
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Jul 16, 2024
1 parent 88ac642 commit bace09b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/bin/ng-parsel.bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ program
parse(mergeOptionalConfigWithDefaults(configObject.config));
} else {
console.log(chalk.cyan(`ng-parsel: no configuration found. CLI arguments will be used.`));
options.src = './test-spa';

parse(mergeOptionalConfigWithDefaults(options));
}

Expand Down
2 changes: 2 additions & 0 deletions src/parser/directive/directive.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NgParselOutput } from '../shared/model/types.model.js';
import { NgParselFieldDecorator } from '../shared/model/decorator.model.js';
import { NgParselMethod } from '../shared/model/method.model.js';

export interface NgParselDirective extends NgParselOutput {
className: string;
Expand All @@ -8,4 +9,5 @@ export interface NgParselDirective extends NgParselOutput {
implementation: string;
inputs: NgParselFieldDecorator[];
outputs: NgParselFieldDecorator[];
methodsPublicExplicit: NgParselMethod[];
}
2 changes: 2 additions & 0 deletions src/parser/directive/directive.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('DirectiveParser', () => {
},
],
implementation,
methodsPublicExplicit: [],
};
jest.spyOn(fs, 'readFileSync').mockReturnValue(implementation);

Expand Down Expand Up @@ -93,6 +94,7 @@ describe('DirectiveParser', () => {
},
],
implementation,
methodsPublicExplicit: [],
};
jest.spyOn(fs, 'readFileSync').mockReturnValue(implementation);

Expand Down
2 changes: 2 additions & 0 deletions src/parser/directive/directive.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getDecoratorProperties } from '../shared/parser/decorator.parser.js';
import { parseInputsAndOutputs } from '../shared/parser/field-decorator.parser.js';

import { NgParselDirective } from './directive.model.js';
import { parseExplicitPublicMethods } from '../shared/parser/method.parser.js';

export function parseDirective(ast: ts.SourceFile, directiveFilePath: string): NgParselDirective {
const directiveDecorators = getDecoratorProperties(ast);
Expand All @@ -24,5 +25,6 @@ export function parseDirective(ast: ts.SourceFile, directiveFilePath: string): N
implementation: directiveImplementation,
inputs: inputsAndOutputs.inputs,
outputs: inputsAndOutputs.outputs,
methodsPublicExplicit: parseExplicitPublicMethods(ast),
};
}
32 changes: 20 additions & 12 deletions test-spa/src/app/tooltip.directive.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
import {Directive, ElementRef, HostListener, Input, OnDestroy} from "@angular/core";
import { Directive, ElementRef, HostListener, Input, OnDestroy } from '@angular/core';

@Directive({
selector: '[tooltip]'
selector: '[tooltip]',
})
export class TooltipDirective implements OnDestroy {

@Input() tooltip = ''; // The text for the tooltip to display
@Input() delay? = 190; // Optional delay input, in ms

private myPopup: any;
private timer: any;

constructor(private el: ElementRef) { }
constructor(private el: ElementRef) {}

ngOnDestroy(): void {
if (this.myPopup) { this.myPopup.remove() }
if (this.myPopup) {
this.myPopup.remove();
}
}

@HostListener('mouseenter') onMouseEnter() {
this.timer = setTimeout(() => {
let x = this.el.nativeElement.getBoundingClientRect().left + this.el.nativeElement.offsetWidth / 2; // Get the middle of the element
let y = this.el.nativeElement.getBoundingClientRect().top + this.el.nativeElement.offsetHeight + 6; // Get the bottom of the element, plus a little extra
this.createTooltipPopup(x, y);
}, this.delay)
}, this.delay);
}

@HostListener('mousedown') dragstart() {
console.log('Drag start');
if (this.timer) clearTimeout(this.timer);
if (this.myPopup) { this.myPopup.remove() }
if (this.myPopup) {
this.myPopup.remove();
}
}

@HostListener('mouseleave') onMouseLeave() {
if (this.timer) clearTimeout(this.timer);
if (this.myPopup) { this.myPopup.remove() }
if (this.myPopup) {
this.myPopup.remove();
}
}

public mySuperMethod(): string {
return 'This method is awesome';
}

private createTooltipPopup(x: number, y: number) {
let popup = document.createElement('div');
popup.innerHTML = this.tooltip;
popup.setAttribute("class", "tooltip-container");
popup.style.top = y.toString() + "px";
popup.style.left = x.toString() + "px";
popup.setAttribute('class', 'tooltip-container');
popup.style.top = y.toString() + 'px';
popup.style.left = x.toString() + 'px';
document.body.appendChild(popup);
this.myPopup = popup;
}

}

0 comments on commit bace09b

Please sign in to comment.