Skip to content

Instantly share code, notes, and snippets.

View allenhwkim's full-sized avatar

Allen Kim allenhwkim

View GitHub Profile
import { Directive, ElementRef, AfterViewInit } from '@angular/core';
@Directive({
selector: '[trapFocus]'
})
export class TrapFocusDirective implements AfterViewInit {
constructor(private el: ElementRef) {}
ngAfterViewInit() {
this.trapFocus(this.el.nativeElement);
@allenhwkim
allenhwkim / enctypt-decrytp.pipe.ts
Created July 6, 2020 17:03
Angular: Encrypt/Decrypt
import { Pipe, PipeTransform } from '@angular/core';
import * as CryptoJS from 'crypto-js';
const key = 'TUc0emRqRXpkdw==';
@Pipe({name: 'encrypted'})
export class EncryptPipe implements PipeTransform {
transform(value: string) {
if (value) {
return CryptoJS.AES.encrypt(value, key).toString();
@allenhwkim
allenhwkim / enctypt-decrytp.pipe.ts
Created July 6, 2020 17:03
Angular: Encrypt/Decrypt
import { Pipe, PipeTransform } from '@angular/core';
import * as CryptoJS from 'crypto-js';
const key = 'TUc0emRqRXpkdw=='; // btoa(btoa(myKey));
@Pipe({name: 'encrypted'})
export class EncryptPipe implements PipeTransform {
transform(value: string) {
if (value) {
return CryptoJS.AES.encrypt(value, key).toString();
import { Component, Output, EventEmitter } from '@angular/core';
const DAY_MS = 60 * 60 * 24 * 1000;
@Component({
selector: 'my-calendar',
templateUrl: './calendar.component.html',
styleUrls: [ './calendar.component.scss' ]
})
export class CalendarComponent {
dates: Array<Date>;
@allenhwkim
allenhwkim / app.animations.ts
Last active July 2, 2020 04:51
Collection of Angular animations
import { trigger, transition, style, animate, query, stagger } from '@angular/animations';
export const fadeAnimation = trigger('fadeAnimation', [
transition(':enter', [
style({ opacity: 0 }), animate('300ms', style({ opacity: 1 }))]
),
transition(':leave',
[style({ opacity: 1 }), animate('300ms', style({ opacity: 0 }))]
)
]);
import {Directive, Component, Input, ViewContainerRef, TemplateRef, AfterViewInit} from '@angular/core';
@Directive({selector: '[inView]'})
export class InView implements AfterViewInit {
alreadyRendered: boolean; // cheking if visible already
constructor(
private vcRef: ViewContainerRef,
private tplRef: TemplateRef<any>
) {}
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef, ɵstringify as stringify} from '@angular/core';
@allenhwkim
allenhwkim / print-storage.js
Created April 18, 2020 06:14
print all sessionStorage and localStorage data
var i;
document.write('<h2>local storage</h2>');
for (i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);
document.write(`<li> <b>${key}</b>: ${value}</li>`);
}
document.write('<h2>session storage</h2>');
@allenhwkim
allenhwkim / get-position.js
Created December 22, 2019 00:50
Get Element Position Relative to Window
function getPosition (el) {
const rect = el.getBoundingClientRect();
const x = rect.left + window.scrollX;
const y = rect.top + window.scrollY;
return { x, y };
}
function getCenterPosition (el) {
const rect = el.getBoundingClientRect();
const x = rect.left + window.scrollX + (rect.width / 2);
@allenhwkim
allenhwkim / typescript-parser.js
Last active December 10, 2019 17:59
Typescript Parser from string
const ts = require('typescript');
class TypescriptParser {
constructor(code) {
const sourceFile = ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest);
this.rootNode = this.getRecursiveFrom(sourceFile, sourceFile);
}
getRecursiveFrom(node, sourceFile) {
const syntaxKind = ts.SyntaxKind[node.kind];