Skip to content

Instantly share code, notes, and snippets.

View maximLyakhov's full-sized avatar
📐
angularing

Maxime Lyakhov maximLyakhov

📐
angularing
View GitHub Profile
@maximLyakhov
maximLyakhov / view-route.map.ts
Created May 24, 2023 12:15
Angular Router: function to map module path recursively
export function createViewRoute(
originalRoute: Route,
parentPath: string
): unknown {
const guardString = defaultTo('');
const guardArr = defaultTo([]);
type ViewRoute = Route & {_loadedRoutes: Routes} & {fullPath: string};
const castRoute = originalRoute as ViewRoute;
const fullPath =
parentPath +
@maximLyakhov
maximLyakhov / in-view.directive.spec.ts
Created May 19, 2023 10:48
Angular Directive: Renders template only if parent block is on screen.
import {ChangeDetectorRef, TemplateRef, ViewContainerRef} from '@angular/core';
import {InViewDirective} from '@ui/directives';
const mockVcr = {
element: {
nativeElement: document.createElement('span'),
},
clear: () => {},
createEmbeddedView: () => {},
} as unknown as ViewContainerRef;
@maximLyakhov
maximLyakhov / component.ts
Created April 24, 2023 12:27
Angular Router: Open In new Tab
class Component {
public navigate({row: {id}, type}: RowAction<Item>): void {
if (type.includes('ctrl')) {
const url = this.router.serializeUrl(this.router.createUrlTree([id]));
window.open(url, '_blank'); // new tab
return;
}
this.zone.run(() => this.router.navigate([id])).then();
}
}
@maximLyakhov
maximLyakhov / cm.directive.ts
Created April 6, 2023 12:59
angular context menu directive
import {coerceCssPixelValue} from '@angular/cdk/coercion';
import {
GlobalPositionStrategy,
Overlay,
OverlayPositionBuilder,
OverlayRef,
} from '@angular/cdk/overlay';
import {ComponentPortal} from '@angular/cdk/portal';
import {
Directive,
@maximLyakhov
maximLyakhov / interserction-observer.directive.ts
Created April 5, 2023 20:41
angular intersection observer directive
import {coerceElement} from '@angular/cdk/coercion';
import {
Directive,
ElementRef,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
} from '@angular/core';
@maximLyakhov
maximLyakhov / index.css
Created November 9, 2022 14:44
Change cursor icon
body {
cursor: url('./assets/svg/cross.svg'), auto;
}
@maximLyakhov
maximLyakhov / index.html
Created October 21, 2022 19:09
HTML is OP
<form action="/subscribe" method="post">
<fieldset>
<legend>Subscribe to the Newsletter</legend>
<input type="email" name="email">
<button>Ok</button>
</fieldset>
</form>
@maximLyakhov
maximLyakhov / component.ts
Last active October 20, 2022 12:49
Mark invalid controls
class Component {
create(form) {
if (form.valid) {
this.fr.post(form.value)
.subscribe(
(response) => {
this.router.navigate(['flight', response.id]);
},
(e) => {
@maximLyakhov
maximLyakhov / effects.ts
Created October 7, 2022 08:53
NgRx basic caching
export class Effects {
private cache = new Map();
public loadCountries$ = createEffect(() => {
return this.actions$.pipe(
ofType(actions.countriesload),
concatMap(() =>
this.cache.has(CACHE_KEYS.COUNTRIES)
? of(actions.countriessuccess(this.cache.get(CACHE_KEYS.COUNTRIES)))
: this.countriesService.getCollection().pipe(
@maximLyakhov
maximLyakhov / ssh-gen.sh
Created May 30, 2022 12:30
generate ssh
#generate key
cd ~/.ssh && ssh-keygen
#copy to clipboard
cat id_rsa.pub | pbcopy // macOS
cat id_rsa.pub | xclip // Linux
cat id_rsa.pub | clip // Win
Get-Content id_rsa.pub | Set-Clipboard // PowerShell
#add your key to your account via the website