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 { |
export class DomStrategy { |
||||
|
private readonly isBrowser: boolean; |
||||
|
|
||||
constructor( |
constructor( |
||||
public target: HTMLElement = document.head, |
private targetFactory: () => HTMLElement, |
||||
public position: InsertPosition = 'beforeend', |
public position: InsertPosition = 'beforeend', |
||||
) {} |
) { |
||||
|
const platformId = inject(PLATFORM_ID); |
||||
|
this.isBrowser = isPlatformBrowser(platformId); |
||||
|
} |
||||
|
|
||||
insertElement<T extends HTMLElement>(element: T) { |
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 = { |
export const DOM_STRATEGY = { |
||||
AfterElement(element: HTMLElement) { |
AfterElement: (el: HTMLElement) => new DomStrategy(() => el, 'afterend'), |
||||
return new DomStrategy(element, 'afterend'); |
BeforeElement: (el: HTMLElement) => new DomStrategy(() => el, 'beforebegin'), |
||||
}, |
AppendToBody: () => new DomStrategy(() => inject(DOCUMENT).body, 'beforeend'), |
||||
AppendToBody() { |
AppendToHead: () => new DomStrategy(() => inject(DOCUMENT).head, 'beforeend'), |
||||
return new DomStrategy(document.body, 'beforeend'); |
PrependToHead: () => new DomStrategy(() => inject(DOCUMENT).head, 'afterbegin'), |
||||
}, |
|
||||
AppendToHead() { |
|
||||
return new DomStrategy(document.head, 'beforeend'); |
|
||||
}, |
|
||||
BeforeElement(element: HTMLElement) { |
|
||||
return new DomStrategy(element, 'beforebegin'); |
|
||||
}, |
|
||||
PrependToHead() { |
|
||||
return new DomStrategy(document.head, 'afterbegin'); |
|
||||
}, |
|
||||
}; |
}; |
||||
|
|||||
@ -1,6 +1,29 @@ |
|||||
import { OAuthStorage } from 'angular-oauth2-oidc'; |
import { inject, PLATFORM_ID } from '@angular/core'; |
||||
import { oAuthStorage } from './oauth-storage'; |
import { isPlatformBrowser } from '@angular/common'; |
||||
|
|
||||
export function storageFactory(): OAuthStorage { |
export class MockStorage implements Storage { |
||||
return oAuthStorage; |
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