Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | 93x 93x 93x 93x 93x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 93x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 15x 15x 1x 1x 1x 1x 1x 1x 16x 16x 1x 3x 3x 1x 1x 2x 2x 3x 16x 16x 16x 14x 13x 13x 1x 3x 16x 16x 12x 12x 4x 1x 3x 8x 8x 8x 8x 2x 6x 6x 6x 8x 4x 4x 3x 3x 6x 6x 6x 6x 6x | import { IDictionary, Metadata } from '@zeedhi/core';
import merge from 'lodash.merge';
import { ComponentRender } from '../zd-component/component-render';
import { IFrame, IFrameEvents } from './interfaces';
const TWO_HOURS = 2 * 60 * 60 * 1000;
export class Frame extends ComponentRender implements IFrame {
public loading = false;
public local = false;
public metadata: object = {};
public override: object = {};
public params: object = {};
public overrideNamedProps: IDictionary = {};
public path = '';
public cache = false;
public JSONCache: boolean | string = false;
public declare events: IFrameEvents;
public cacheDuration: number = TWO_HOURS;
/**
* Defines the component height. Possible values for this property can be
* <samp>'auto', '100%', '400px' or 400</samp>
*/
public height: string | number = 'auto';
/**
* Defines the component min height. Possible values for this property can be
* <samp>'auto', '100%', '400px' or 400</samp>
*/
public minHeight: string | number = 'auto';
/**
* Defines the component max height. Possible values for this property can be
* <samp>'auto', '100%', '400px' or 400</samp>
*/
public maxHeight: string | number = 'none';
/**
* Defines the component width. Possible values for this property can be
* <samp>'auto', '100%', '400px' or 400</samp>
*/
public width: string | number = '100%';
/**
* Defines the component min width. Possible values for this property can be
* <samp>'auto', '100%', '400px' or 400</samp>
*/
public minWidth: string | number = 'auto';
/**
* Defines the component max height. Possible values for this property can be
* <samp>'auto', '100%', '400px' or 400</samp>
*/
public maxWidth: string | number = 'none';
public type = 'get';
private readonly headerName = 'sw-fetched-on';
private static readonly cacheName = 'zd-frame-cache';
protected resolveLoad?: (value: object) => void;
protected rejectLoad?: (reason?: any) => void;
constructor(props: IFrame) {
super(props);
this.path = this.getInitValue('path', props.path, this.path);
this.local = props.local === true;
this.override = props.override || this.override;
this.overrideNamedProps = props.overrideNamedProps || this.overrideNamedProps;
this.cache = props.cache || this.cache;
this.JSONCache = this.getInitValue('JSONCache', props.JSONCache, this.JSONCache);
this.cacheDuration = this.getInitValue('cacheDuration', props.cacheDuration, this.cacheDuration);
this.height = this.getInitValue('height', props.height, this.height);
this.minHeight = this.getInitValue('minHeight', props.minHeight, this.minHeight);
this.maxHeight = this.getInitValue('maxHeight', props.maxHeight, this.maxHeight);
this.width = this.getInitValue('width', props.width, this.width);
this.minWidth = this.getInitValue('minWidth', props.minWidth, this.minWidth);
this.maxWidth = this.getInitValue('maxWidth', props.maxWidth, this.maxWidth);
this.type = this.getInitValue('type', props.type, this.type);
this.params = this.getInitValue('params', props.params, this.params);
}
public onCreated(): void {
super.onCreated();
this.getMetadata();
}
public overrideNamedPropsFunc(metadata: object) {
let strMetadata = JSON.stringify(metadata);
// TODO: re-factory this forEach for better performance
Object.keys(this.overrideNamedProps).forEach((key) => {
strMetadata = strMetadata.replace(RegExp(`<<${key}>>`, 'g'), this.overrideNamedProps[key]);
});
return JSON.parse(strMetadata);
}
public async reload() {
this.loading = true;
this.getMetadata();
}
/**
* Triggered when frame is load.
* @param event
* @param element
*/
public afterLoad(event?: Event, element?: HTMLElement) {
if (this.resolveLoad) this.resolveLoad(this.metadata);
this.callEvent('onAfterLoad', {
event,
element,
component: this,
});
}
/**
* Deletes all frame cached requests
*/
public static async deleteCache() {
return caches.delete(Frame.cacheName);
}
public async waitLoading(): Promise<object> {
return new Promise((resolve, reject) => {
if (!this.loading) {
resolve(this.metadata);
return;
}
this.resolveLoad = resolve;
this.rejectLoad = reject;
});
}
protected notFoundError(e: unknown) {
if (this.rejectLoad) this.rejectLoad(e);
}
private async getMetadata() {
try {
this.loading = true;
const page = await this.requestPage();
if (typeof page !== 'object') throw new Error();
this.metadata = merge(page, this.override);
if (Object.keys(this.overrideNamedProps).length !== 0) {
this.metadata = this.overrideNamedPropsFunc(this.metadata);
}
} catch (e) {
this.notFoundError(e);
} finally {
this.loading = false;
this.afterLoad();
}
}
private async requestPage(): Promise<IDictionary> {
const { path } = this;
if (!this.cache) {
if (this.type === 'post') {
return Metadata.post(path, this.params);
}
return Metadata.get(path, this.local, this.JSONCache);
}
const url = Metadata.getMetadataUrl(path, this.local);
const cache = await caches.open(Frame.cacheName);
const cachedResponse: Response | undefined = (await cache.match(url)) as Response | undefined;
if (this.isValid(cachedResponse)) {
return cachedResponse.json();
}
const metadata = await Metadata.get(path, this.local);
this.putCache(cache, url, metadata);
return metadata;
}
/**
* Checks if cache response is valid (not expired yet)
* @param response
*/
private isValid(response?: Response): response is Response {
if (!response) return false;
const header = response.headers.get(this.headerName);
if (!header) return false;
const lastFetch = Number(header);
return lastFetch + this.cacheDuration > Date.now();
}
/**
* Puts the metadata in defined cache with defined url
* @param cache Cache object in which data will be stored
* @param url Metadata url
* @param metadata Metadata to be stored
*/
private putCache(cache: Cache, url: string, metadata: IDictionary) {
const time = String(Date.now());
const headers = new Headers({
[this.headerName]: time,
});
const stringData = JSON.stringify(metadata);
const responseObj: Response = new Response(stringData, { headers });
cache.put(url, responseObj);
}
}
|