mirror of https://github.com/abpframework/abp.git
8 changed files with 79 additions and 37 deletions
@ -1,28 +1,29 @@ |
|||
import { inject, PLATFORM_ID } from '@angular/core'; |
|||
import { isPlatformBrowser, DOCUMENT } from '@angular/common'; |
|||
|
|||
export class DomStrategy { |
|||
private readonly isBrowser: boolean; |
|||
|
|||
constructor( |
|||
public target: HTMLElement = document.head, |
|||
private targetFactory: () => HTMLElement, |
|||
public position: InsertPosition = 'beforeend', |
|||
) {} |
|||
) { |
|||
const platformId = inject(PLATFORM_ID); |
|||
this.isBrowser = isPlatformBrowser(platformId); |
|||
} |
|||
|
|||
insertElement<T extends HTMLElement>(element: T) { |
|||
this.target.insertAdjacentElement(this.position, element); |
|||
if (this.isBrowser) { |
|||
const target = this.targetFactory(); |
|||
target.insertAdjacentElement(this.position, element); |
|||
} |
|||
} |
|||
} |
|||
|
|||
export const DOM_STRATEGY = { |
|||
AfterElement(element: HTMLElement) { |
|||
return new DomStrategy(element, 'afterend'); |
|||
}, |
|||
AppendToBody() { |
|||
return new DomStrategy(document.body, 'beforeend'); |
|||
}, |
|||
AppendToHead() { |
|||
return new DomStrategy(document.head, 'beforeend'); |
|||
}, |
|||
BeforeElement(element: HTMLElement) { |
|||
return new DomStrategy(element, 'beforebegin'); |
|||
}, |
|||
PrependToHead() { |
|||
return new DomStrategy(document.head, 'afterbegin'); |
|||
}, |
|||
AfterElement: (el: HTMLElement) => new DomStrategy(() => el, 'afterend'), |
|||
BeforeElement: (el: HTMLElement) => new DomStrategy(() => el, 'beforebegin'), |
|||
AppendToBody: () => new DomStrategy(() => inject(DOCUMENT).body, 'beforeend'), |
|||
AppendToHead: () => new DomStrategy(() => inject(DOCUMENT).head, 'beforeend'), |
|||
PrependToHead: () => new DomStrategy(() => inject(DOCUMENT).head, 'afterbegin'), |
|||
}; |
|||
|
|||
@ -1,6 +1,29 @@ |
|||
import { OAuthStorage } from 'angular-oauth2-oidc'; |
|||
import { oAuthStorage } from './oauth-storage'; |
|||
import { inject, PLATFORM_ID } from '@angular/core'; |
|||
import { isPlatformBrowser } from '@angular/common'; |
|||
|
|||
export function storageFactory(): OAuthStorage { |
|||
return oAuthStorage; |
|||
export class MockStorage implements Storage { |
|||
private data = new Map<string, string>(); |
|||
get length() { |
|||
return this.data.size; |
|||
} |
|||
clear() { |
|||
this.data.clear(); |
|||
} |
|||
getItem(key: string) { |
|||
return this.data.get(key) || null; |
|||
} |
|||
key(index: number) { |
|||
return Array.from(this.data.keys())[index] || null; |
|||
} |
|||
removeItem(key: string) { |
|||
this.data.delete(key); |
|||
} |
|||
setItem(key: string, value: string) { |
|||
this.data.set(key, value); |
|||
} |
|||
} |
|||
|
|||
export function oAuthStorageFactory(): Storage { |
|||
const platformId = inject(PLATFORM_ID); |
|||
return isPlatformBrowser(platformId) ? localStorage : new MockStorage(); |
|||
} |
|||
|
|||
Loading…
Reference in new issue