@data-fair/frame 0.9.0 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/d-frame.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var L=Object.defineProperty;var I=(f,u,g)=>u in f?L(f,u,{enumerable:!0,configurable:!0,writable:!0,value:g}):f[u]=g;var n=(f,u,g)=>I(f,typeof u!="symbol"?u+"":u,g);(function(){"use strict";function f(e){return e[0]==="df-global"&&e[1]==="mouse"}function u(e){return e[0]==="df-child"&&e[1]==="init"}function g(e){return e[0]==="df-child"&&e[1]==="height"}function b(e){return e[0]==="df-child"&&e[1]==="stateChange"}function y(e){return e[0]==="df-child"&&e[1]==="custom"}function E(e){return e[0]==="df-child"&&e[1]==="notif"}function v(e){return e[0]==="df-child"&&e[1]==="ready"}function w(e){return e.endsWith("/")?e:e===""?"/":e.slice(0,e.lastIndexOf("/")+1)}function A(e,r){if(r.origin!==e.origin)return r.href;if(r.pathname===e.pathname)return"";const t=w(e.pathname);return r.pathname.startsWith(t)?r.pathname.replace(t,"./"):r.pathname}const S=e=>e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&");function M(e){const r=[];for(const t of e.split(",")){const[s="*",i]=t.split(":"),a="^"+S(s).replace(/\\\*/g,"(.*)")+"$";r.push({regexp:new RegExp(a),append:i})}return r}function C(e,r,t,s){if(!t&&!s)return e;const i=new URL(e),a=new URL(r);if(t&&a.searchParams.forEach((h,l)=>{for(const o of t){if(o.append&&!l.startsWith(o.append)||l===s)continue;const d=o.append?l.replace(o.append,""):l;if(d.match(o.regexp)){i.searchParams.set(d,h);break}}}),s!==null){const h=a.searchParams.get(s);h&&(i.pathname=new URL(h,e).pathname)}return i.href}function P(e,r,t,s,i){const a=new URL(e),h=new URL(t),l=new URL(r);if(s&&(h.searchParams.forEach((o,d)=>{for(const c of s){if(c.append&&!d.startsWith(c.append))continue;const p=c.append?d.replace(c.append,""):d;if(p.match(c.regexp)){l.searchParams.get(p)===null&&h.searchParams.delete(d);break}}}),l.searchParams.forEach((o,d)=>{for(const c of s){if(!d.match(c.regexp)||(a==null?void 0:a.searchParams.get(d))===o)continue;const p=c.append?c.append+d:d;h.searchParams.set(p,o);break}})),i!==null&&a){const o=A(a,l);o?h.searchParams.set(i,o):h.searchParams.delete(i)}return h}function R(e){return e.vIframe===!0&&e.uiNotification!==void 0}function z(e){if(typeof e=="string")return{type:"default",title:e};if(e.error&&!e.errorMsg){const r=e.error.response&&(e.error.response.data||e.error.response.status)||e.error.message||e.error;return e.msg?{type:"error",title:e.msg,detail:r}:{type:"error",title:r}}return e.type==="error"?{type:e.clientError?"warning":"error",title:e.msg,detail:e.errorMsg}:{type:e.type??"default",title:e.msg}}class x{stateChange(r,t){r==="replace"&&window.history.replaceState(null,"",t),r==="push"&&window.history.pushState(null,"",t)}onStateChange(r){window.addEventListener("popstate",r)}}const m=document.createElement("template");m.innerHTML=`<style>
|
|
2
2
|
:host {
|
|
3
3
|
display: block;
|
|
4
4
|
}
|
|
@@ -11,5 +11,5 @@ var H=Object.defineProperty;var L=(l,u,f)=>u in l?H(l,u,{enumerable:!0,configura
|
|
|
11
11
|
height: 100%;
|
|
12
12
|
border: none;
|
|
13
13
|
}
|
|
14
|
-
</style><div class="d-frame-wrapper"><slot name="loading"><div style="min-height:150px;"></div></slot><iframe class="d-frame-iframe" style="visibility:hidden;position:absolute;height:0px;"></iframe></div>`;class
|
|
14
|
+
</style><div class="d-frame-wrapper"><slot name="loading"><div style="min-height:150px;"></div></slot><iframe class="d-frame-iframe" style="visibility:hidden;position:absolute;height:0px;"></iframe></div>`;class H extends HTMLElement{constructor(){super();n(this,"adapter");n(this,"connected",!1);n(this,"initialSrc");n(this,"parsedSyncParams");n(this,"wrapperElement");n(this,"slotElement");n(this,"iframeElement");n(this,"width");n(this,"aspectRatioHeight");n(this,"resizedHeight");n(this,"randomId");n(this,"iframeLoaded",!1);n(this,"ready",!1);n(this,"iframeExtraAttrs",{});n(this,"lastUpdateSrc",null);n(this,"currentSyncedMouseEvents",new Set);this.randomId=Math.random().toString(36).slice(-6);const t=m.content.cloneNode(!0);this.wrapperElement=t.childNodes[1],this.slotElement=this.wrapperElement.childNodes[0],this.iframeElement=this.wrapperElement.childNodes[1],this.iframeElement.addEventListener("load",()=>{this.iframeElement.getAttribute("src")&&(this.log("debug","iframe loaded"),this.iframeLoaded=!0,this.dispatchEvent(new CustomEvent("iframe-load")),this.resize!=="yes"&&!this.readyMessage&&(this.ready=!0,this.dispatchEvent(new CustomEvent("ready")),this.updateStyle()))}),this.attachShadow({mode:"open"}).appendChild(t),new ResizeObserver(a=>{(this.width===void 0||Math.abs(this.width-a[0].contentRect.width)>2)&&(this.width=a[0].contentRect.width,this.updateAspectRatioHeight())}).observe(this),this.adapter=new x,window.addEventListener("message",a=>this.onMessage(a))}get id(){return this.getAttribute("id")??this.randomId}set id(t){this.setAttribute("id",t)}get debug(){return this.hasAttribute("debug")}set debug(t){t?this.setAttribute("debug",""):this.removeAttribute("debug")}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t)}get fullSrc(){return this.src.startsWith("/")?window.location.origin+this.src:this.src}get aspectRatio(){const t=this.getAttribute("aspect-ratio");return t===""?"auto":t}set aspectRatio(t){t!==null?this.setAttribute("aspect-ratio",t):this.removeAttribute("aspect-ratio")}get height(){return this.getAttribute("height")}set height(t){t?this.setAttribute("height",t):this.removeAttribute("height")}set resize(t){this.setAttribute("resize",t)}get resize(){const t=this.getAttribute("resize");return t===null?"auto":t===""?"yes":t}get syncParams(){return this.getAttribute("sync-params")}set syncParams(t){t!==null?this.setAttribute("sync-params",t):this.removeAttribute("sync-params")}get syncPath(){const t=this.getAttribute("sync-path");return t===null?null:t||"p"}set syncPath(t){t!==null?this.setAttribute("sync-params",t):this.removeAttribute("sync-params")}get stateChangeEvents(){return this.hasAttribute("state-change-events")}set stateChangeEvents(t){t?this.setAttribute("state-change-events",""):this.removeAttribute("state-change-events")}get readyMessage(){return this.hasAttribute("ready-message")}set readyMessage(t){t?this.setAttribute("ready-message",""):this.removeAttribute("ready-message")}get mouseEvents(){const t=this.getAttribute("mouse-events");return t===null?null:t===""?["click","dblclick","mousedown","mouseup"]:t.split(",")}set mouseEvents(t){t===null?this.removeAttribute("mouse-events"):this.setAttribute("mouse-events",t.join(","))}get actualAspectRatio(){return this.aspectRatio!=="auto"?Number(this.aspectRatio):!this.width||this.width<500?1:this.width<800?4/3:this.width<1200?16/9:21/9}onMessage(t){var s;if(t.source===((s=this.iframeElement)==null?void 0:s.contentWindow)){const i=t.data;if(this.log("debug","received message from child",i),Array.isArray(t.data)){if(u(i)&&(this.postMessageToChild(["df-parent","init",{id:this.id,debug:this.debug,resize:this.resize,syncParams:this.syncParams!==null,syncPath:this.syncPath!==null,stateChangeEvents:this.stateChangeEvents,mouseEvents:this.mouseEvents}]),this.init()),g(i)&&(this.resizedHeight=i[2],this.resize==="yes"&&!this.ready&&!this.readyMessage&&(this.ready=!0,this.dispatchEvent(new CustomEvent("ready"))),this.updateStyle()),b(i)){if(this.parsedSyncParams||this.syncPath){const a=P(this.fullSrc,i[3],window.location.href,this.parsedSyncParams,this.syncPath);a.href!==window.location.href&&this.adapter.stateChange(i[2],a)}this.stateChangeEvents&&this.dispatchEvent(new CustomEvent("state-change",{detail:[i[2],i[3]]}))}y(i)&&this.dispatchEvent(new CustomEvent("message",{detail:i[2]})),E(i)&&this.dispatchEvent(new CustomEvent("notif",{detail:i[2]})),v(i)&&(this.ready=!0,this.dispatchEvent(new CustomEvent("ready")),this.updateStyle()),f(i)&&this.applySyncedMouseEvent(i)}else R(i)&&this.dispatchEvent(new CustomEvent("notif",{detail:z(i.uiNotification)}))}}init(){this.initialSrc=this.src}postMessageToChild(t){var s,i;this.log("debug","send message to child",t),(i=(s=this.iframeElement)==null?void 0:s.contentWindow)==null||i.postMessage(t)}updateSrc(){if(!this.connected)return;const t=C(this.fullSrc,window.location.href,this.parsedSyncParams,this.syncPath);t!==this.lastUpdateSrc&&(this.lastUpdateSrc=t,this.initialSrc?this.postMessageToChild(["df-parent","updateSrc",t]):(this.log("debug","set src attribute of iframe: "+t),this.iframeElement.setAttribute("src",t)))}updateStyle(){if(!this.connected)return;let t;this.resizedHeight?(this.log("debug","height of iframe based on resize message: "+this.resizedHeight),t=this.resizedHeight+"px"):this.height?(this.log("debug","height of iframe based on direct attribute: "+this.height),t=this.height):this.aspectRatio!==null&&(this.log("debug","height of iframe based on aspect ratio: "+this.aspectRatioHeight),t=this.aspectRatioHeight+"px"),t!==void 0?(this.log("debug","set height iframe wrapper: "+t),this.wrapperElement.setAttribute("style",`height:${t};min-height:0;`)):this.wrapperElement.setAttribute("style",""),this.resize==="yes"||this.resize==="auto"&&this.resizedHeight?this.iframeElement.getAttribute("scrolling")!=="no"&&(this.log("debug","set scrolling attribute of iframe: no"),this.iframeElement.setAttribute("scrolling","no")):this.iframeElement.getAttribute("scrolling")!=="auto"&&(this.log("debug","set scrolling attribute of iframe: auto"),this.iframeElement.setAttribute("scrolling","auto")),this.ready&&this.iframeElement.getAttribute("style")!==""&&(this.log("debug","toggle loading slot and iframe visibility"),this.iframeElement.setAttribute("style",""),this.slotElement.setAttribute("style","display:none;"))}updateAspectRatioHeight(){this.width&&this.aspectRatio!==null&&(this.resizedHeight||this.height||(this.aspectRatioHeight=Math.ceil(this.width/this.actualAspectRatio),this.updateStyle()))}updateIframeExtraAttrs(){const t=this.iframeExtraAttrs,s={};for(const i of this.getAttributeNames())if(i.startsWith("iframe-")){const a=this.getAttribute(i);if(a!==null){const h=i.slice(7);s[h]=a}}this.log("debug","update iframe extra attributes",t,s);for(const i of Object.keys(t))i in s||this.iframeElement.removeAttribute(i);for(const i of Object.keys(s))s[i]!==t[i]&&this.iframeElement.setAttribute(i,s[i])}initMouseEvents(t){this.log("debug","initMouseEvents");for(const s of t)document.addEventListener(s,i=>{i instanceof MouseEvent&&this.transmitSyncedMouseEvent(["df-global","mouse",s,{altKey:i.altKey,ctrlKey:i.ctrlKey,shiftKey:i.shiftKey,metaKey:i.metaKey}])},!0)}transmitSyncedMouseEvent(t){this.currentSyncedMouseEvents.has(t[2])?this.currentSyncedMouseEvents.delete(t[2]):this.postMessageToChild(t)}applySyncedMouseEvent(t){this.currentSyncedMouseEvents.add(t[2]);const s=new MouseEvent(t[2],{bubbles:!0,cancelable:!0,view:window,...t[3]});this.dispatchEvent(s)}log(t,...s){t==="debug"&&!this.debug||(t==="debug"&&console.timeLog(`d-frame:${this.id}`,...s),t==="info"&&console.info(`d-frame:${this.id}`,...s))}connectedCallback(){if(!this.debug){const t=window.localStorage.getItem("debug");if(t){const s=t.split(",");(s.includes("d-frame")||s.includes("d-frame:"+this.id))&&(this.debug=!0)}}if(this.debug&&console.time(`d-frame:${this.id}`),!this.hasAttribute("src"))throw new Error("src is a required attribute");this.log("debug","connected"),this.connected=!0,this.adapter.onStateChange&&this.adapter.onStateChange(()=>{this.updateSrc()}),this.syncParams!==null&&(this.parsedSyncParams=M(this.syncParams||"*")),this.updateStyle(),this.updateIframeExtraAttrs(),this.updateSrc(),this.mouseEvents&&this.initMouseEvents(this.mouseEvents)}disconnectedCallback(){this.log("debug","disconnected"),this.debug&&console.timeEnd(`d-frame:${this.id}`)}static get observedAttributes(){return["src","aspect-ratio","height","sync-params","sync-path"]}attributeChangedCallback(t,s,i){t==="aspect-ratio"&&this.updateAspectRatioHeight(),t.startsWith("iframe-")&&this.updateIframeExtraAttrs(),this.connected&&(this.log("debug","attribute change",t,s,i),t==="src"&&this.updateSrc(),t==="height"&&this.updateStyle(),t==="sync-params"&&this.updateSrc(),t==="sync-path"&&this.updateSrc())}}customElements.get("d-frame")||customElements.define("d-frame",H)})();
|
|
15
15
|
//# sourceMappingURL=d-frame.min.js.map
|
package/lib/DFrameElement.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ParentMessage } from './messages.js';
|
|
2
2
|
export interface StateChangeAdapter {
|
|
3
3
|
stateChange(action: 'push' | 'replace', newUrl: URL): void;
|
|
4
|
-
onStateChange
|
|
4
|
+
onStateChange(callback: () => void): void;
|
|
5
5
|
}
|
|
6
6
|
export default class DFrameElement extends HTMLElement {
|
|
7
7
|
get id(): string;
|
|
@@ -10,6 +10,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
10
10
|
set debug(value: boolean);
|
|
11
11
|
get src(): string;
|
|
12
12
|
set src(value: string);
|
|
13
|
+
get fullSrc(): string;
|
|
13
14
|
get aspectRatio(): string | null;
|
|
14
15
|
set aspectRatio(value: string | null);
|
|
15
16
|
get height(): string | null;
|
|
@@ -29,7 +30,6 @@ export default class DFrameElement extends HTMLElement {
|
|
|
29
30
|
adapter: StateChangeAdapter;
|
|
30
31
|
private connected;
|
|
31
32
|
private initialSrc;
|
|
32
|
-
private srcUrl;
|
|
33
33
|
private parsedSyncParams;
|
|
34
34
|
private wrapperElement;
|
|
35
35
|
private slotElement;
|
|
@@ -46,6 +46,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
46
46
|
onMessage(e: MessageEvent): void;
|
|
47
47
|
init(): void;
|
|
48
48
|
postMessageToChild(message: ParentMessage): void;
|
|
49
|
+
private lastUpdateSrc;
|
|
49
50
|
updateSrc(): void;
|
|
50
51
|
updateStyle(): void;
|
|
51
52
|
updateAspectRatioHeight(): void;
|
package/lib/DFrameElement.js
CHANGED
|
@@ -9,6 +9,14 @@ class WindowStateChangeAdapter {
|
|
|
9
9
|
if (action === 'push')
|
|
10
10
|
window.history.pushState(null, '', newUrl);
|
|
11
11
|
}
|
|
12
|
+
onStateChange(callback) {
|
|
13
|
+
// this is only partial, for a full implementation we would have to monkeypatch window.history
|
|
14
|
+
// I chose to do it in the child but not here
|
|
15
|
+
// therefore support of dynamic changes performed through the history api requires an adapter
|
|
16
|
+
// the idea being that the child makes itself wholly compatible with d-frame and changes its navigation logic to fit it
|
|
17
|
+
// but the parent should be left as untouched as possible and uses d-frame as a black box (custom element)
|
|
18
|
+
window.addEventListener('popstate', callback);
|
|
19
|
+
}
|
|
12
20
|
}
|
|
13
21
|
const template = document.createElement('template');
|
|
14
22
|
template.innerHTML = `<style>
|
|
@@ -39,6 +47,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
39
47
|
}
|
|
40
48
|
get src() { return this.getAttribute('src'); }
|
|
41
49
|
set src(value) { this.setAttribute('src', value); }
|
|
50
|
+
get fullSrc() { return this.src.startsWith('/') ? (window.location.origin + this.src) : this.src; }
|
|
42
51
|
get aspectRatio() {
|
|
43
52
|
const value = this.getAttribute('aspect-ratio');
|
|
44
53
|
if (value === '')
|
|
@@ -118,7 +127,6 @@ export default class DFrameElement extends HTMLElement {
|
|
|
118
127
|
/* internal state */
|
|
119
128
|
connected = false;
|
|
120
129
|
initialSrc;
|
|
121
|
-
srcUrl;
|
|
122
130
|
parsedSyncParams;
|
|
123
131
|
wrapperElement;
|
|
124
132
|
slotElement;
|
|
@@ -201,7 +209,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
201
209
|
}
|
|
202
210
|
if (isStateChangeMessage(message)) {
|
|
203
211
|
if ((this.parsedSyncParams || this.syncPath)) {
|
|
204
|
-
const newParentHUrl = getParentUrl(this.
|
|
212
|
+
const newParentHUrl = getParentUrl(this.fullSrc, message[3], window.location.href, this.parsedSyncParams, this.syncPath);
|
|
205
213
|
if (newParentHUrl.href !== window.location.href) {
|
|
206
214
|
this.adapter.stateChange(message[2], newParentHUrl);
|
|
207
215
|
}
|
|
@@ -238,17 +246,20 @@ export default class DFrameElement extends HTMLElement {
|
|
|
238
246
|
this.log('debug', 'send message to child', message);
|
|
239
247
|
this.iframeElement?.contentWindow?.postMessage(message);
|
|
240
248
|
}
|
|
249
|
+
lastUpdateSrc = null;
|
|
241
250
|
updateSrc() {
|
|
242
251
|
if (!this.connected)
|
|
243
252
|
return;
|
|
244
|
-
|
|
245
|
-
|
|
253
|
+
const iframeSrc = getChildSrc(this.fullSrc, window.location.href, this.parsedSyncParams, this.syncPath);
|
|
254
|
+
if (iframeSrc === this.lastUpdateSrc)
|
|
255
|
+
return;
|
|
256
|
+
this.lastUpdateSrc = iframeSrc;
|
|
246
257
|
if (this.initialSrc) {
|
|
247
|
-
this.postMessageToChild(['df-parent', 'updateSrc',
|
|
258
|
+
this.postMessageToChild(['df-parent', 'updateSrc', iframeSrc]);
|
|
248
259
|
}
|
|
249
260
|
else {
|
|
250
|
-
this.log('debug', 'set src attribute of iframe: ' +
|
|
251
|
-
this.iframeElement.setAttribute('src',
|
|
261
|
+
this.log('debug', 'set src attribute of iframe: ' + iframeSrc);
|
|
262
|
+
this.iframeElement.setAttribute('src', iframeSrc);
|
|
252
263
|
}
|
|
253
264
|
}
|
|
254
265
|
updateStyle() {
|
|
@@ -3,5 +3,5 @@ export type ParsedSyncParams = {
|
|
|
3
3
|
append?: string;
|
|
4
4
|
}[];
|
|
5
5
|
export declare function parseSyncParams(syncParamsStr: string): ParsedSyncParams;
|
|
6
|
-
export declare function getChildSrc(
|
|
7
|
-
export declare function getParentUrl(
|
|
6
|
+
export declare function getChildSrc(fullSrc: string, parentHref: string, syncParams: ParsedSyncParams | undefined, syncPath: null | string): string;
|
|
7
|
+
export declare function getParentUrl(fullSrc: string, childHref: string, currentParentHref: string, syncParams: ParsedSyncParams | undefined, syncPath: null | string): URL;
|
package/lib/utils/sync-params.js
CHANGED
|
@@ -12,9 +12,10 @@ export function parseSyncParams(syncParamsStr) {
|
|
|
12
12
|
}
|
|
13
13
|
return syncParams;
|
|
14
14
|
}
|
|
15
|
-
export function getChildSrc(
|
|
15
|
+
export function getChildSrc(fullSrc, parentHref, syncParams, syncPath) {
|
|
16
16
|
if (!syncParams && !syncPath)
|
|
17
|
-
return
|
|
17
|
+
return fullSrc;
|
|
18
|
+
const childUrl = new URL(fullSrc);
|
|
18
19
|
const parentUrl = new URL(parentHref);
|
|
19
20
|
if (syncParams) {
|
|
20
21
|
parentUrl.searchParams.forEach((value, key) => {
|
|
@@ -26,7 +27,7 @@ export function getChildSrc(srcUrl, parentHref, syncParams, syncPath) {
|
|
|
26
27
|
const childKey = syncParam.append ? key.replace(syncParam.append, '') : key;
|
|
27
28
|
if (!childKey.match(syncParam.regexp))
|
|
28
29
|
continue;
|
|
29
|
-
|
|
30
|
+
childUrl.searchParams.set(childKey, value);
|
|
30
31
|
break;
|
|
31
32
|
}
|
|
32
33
|
});
|
|
@@ -34,12 +35,13 @@ export function getChildSrc(srcUrl, parentHref, syncParams, syncPath) {
|
|
|
34
35
|
if (syncPath !== null) {
|
|
35
36
|
const path = parentUrl.searchParams.get(syncPath);
|
|
36
37
|
if (path) {
|
|
37
|
-
|
|
38
|
+
childUrl.pathname = new URL(path, fullSrc).pathname;
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
|
-
return
|
|
41
|
+
return childUrl.href;
|
|
41
42
|
}
|
|
42
|
-
export function getParentUrl(
|
|
43
|
+
export function getParentUrl(fullSrc, childHref, currentParentHref, syncParams, syncPath) {
|
|
44
|
+
const srcUrl = new URL(fullSrc);
|
|
43
45
|
const parentUrl = new URL(currentParentHref);
|
|
44
46
|
const childUrl = new URL(childHref);
|
|
45
47
|
if (syncParams) {
|