@data-fair/frame 0.1.0 → 0.2.0
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/README.md +2 -2
- package/dist/d-frame-content.min.js +1 -1
- package/dist/d-frame.min.js +2 -2
- package/package.json +1 -1
- package/src/DFrameContent.d.ts +14 -11
- package/src/DFrameContent.js +8 -0
- package/src/DFrameElement.js +7 -1
- package/src/messages.d.ts +13 -4
- package/src/messages.js +6 -0
package/README.md
CHANGED
|
@@ -4,8 +4,8 @@ An iframe wrapper for powerful web app integrations.
|
|
|
4
4
|
Lightweight, vanilla JS, no dependency.
|
|
5
5
|
|
|
6
6
|
Supports:
|
|
7
|
+
- dynamic resizing
|
|
7
8
|
- navigation syncing
|
|
8
|
-
-
|
|
9
|
-
- dynamic resize
|
|
9
|
+
- notification passing
|
|
10
10
|
|
|
11
11
|
See [the documentation](https://data-fair.github.io/frame/latest/).
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var DFrameContent=function(){"use strict";var u=Object.defineProperty;var
|
|
1
|
+
var DFrameContent=function(){"use strict";var u=Object.defineProperty;var l=(n,i,s)=>i in n?u(n,i,{enumerable:!0,configurable:!0,writable:!0,value:s}):n[i]=s;var a=(n,i,s)=>l(n,typeof i!="symbol"?i+"":i,s);function n(r){return r[0]==="df-parent"&&r[1]==="init"}function i(r){return r[0]==="df-parent"&&r[1]==="updateSrc"}const s=["animationstart","webkitAnimationStart","animationiteration","webkitAnimationIteration","animationend","webkitAnimationEnd","input","mouseup","mousedown","orientationchange","afterprint","beforeprint","readystatechange","touchstart","touchend","touchcancel","transitionstart","webkitTransitionStart","MSTransitionStart","oTransitionStart","otransitionstart","transitioniteration","webkitTransitionIteration","MSTransitionIteration","oTransitionIteration","otransitioniteration","transitionend","webkitTransitionEnd","MSTransitionEnd","oTransitionEnd","otransitionend","resize"];class c{constructor(t){a(this,"initialized",!1);a(this,"options");a(this,"pendingCheckHeight",!1);a(this,"debug",!1);a(this,"throttledCheckHeight");a(this,"lastHeight",0);this.options=t??{};const e=()=>{this.checkHeight(),this.pendingCheckHeight=!1};this.throttledCheckHeight=()=>{this.pendingCheckHeight||(this.pendingCheckHeight=!0,requestAnimationFrame(e))},!(typeof window>"u")&&(window.addEventListener("message",o=>this.onMessage(o)),this.postMessageToParent(["df-child","init"]))}log(t,...e){t==="debug"&&!this.debug||(t==="debug"&&console.debug("d-frame-content",...e),t==="info"&&console.info("d-frame-content",...e))}onMessage(t){if(t.source===window.parent&&Array.isArray(t.data)){const e=t.data;n(e)&&(this.debug=!!e[2].debug,e[2].resize!=="no"&&this.initResize(),(e[2].syncParams||e[2].syncPath)&&this.initSyncParams(),this.initialized=!0),i(e)&&(this.options.updateSrc?this.options.updateSrc(e[2].startsWith("/")?window.location.origin+e[2]:e[2]):window.location.href=e[2])}}postMessageToParent(t){window.parent.postMessage(t)}sendNotif(t){this.postMessageToParent(["df-child","notif",t])}sendMessage(t){this.postMessageToParent(["df-child","custom",t])}initResize(){this.log("debug","initResize"),this.checkHeight(),this.createMutationObserver(),this.createWindowEventListeners()}initSyncParams(){this.log("debug","initSyncParams");const t=window.history.replaceState,e=(...h)=>{const d=t.apply(window.history,h);return this.postMessageToParent(["df-child","stateChange","push",window.location.href]),d};window.history.pushState=e;const o=(...h)=>{const d=t.apply(window.history,h);return this.postMessageToParent(["df-child","stateChange","replace",window.location.href]),d};window.history.replaceState=o}createMutationObserver(){const t=document.querySelector("body");if(!t)throw new Error("DFrameContentManager was initialized before the HTML body");new window.MutationObserver(this.throttledCheckHeight).observe(t,{attributes:!1,attributeOldValue:!1,characterData:!1,characterDataOldValue:!1,childList:!0,subtree:!0})}createWindowEventListeners(){for(const t of s)window.addEventListener(t,this.throttledCheckHeight,{passive:!0})}checkHeight(){const t=document.querySelectorAll("[data-iframe-height]");let e=0;for(const o of t){const h=o.getAttribute("data-iframe-height"),d=o.getBoundingClientRect().bottom+parseFloat(getComputedStyle(o).getPropertyValue("margin-bottom"))+(h?parseFloat(h):0);d>e&&(e=d)}e!==this.lastHeight&&(this.postMessageToParent(["df-child","height",e]),this.lastHeight=e)}}return new c}();
|
|
2
2
|
//# sourceMappingURL=d-frame-content.min.js.map
|
package/dist/d-frame.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var C=Object.defineProperty;var H=(u,c,p)=>c in u?C(u,c,{enumerable:!0,configurable:!0,writable:!0,value:p}):u[c]=p;var o=(u,c,p)=>H(u,typeof c!="symbol"?c+"":c,p);(function(){"use strict";function u(e){return e[0]==="df-child"&&e[1]==="init"}function c(e){return e[0]==="df-child"&&e[1]==="height"}function p(e){return e[0]==="df-child"&&e[1]==="stateChange"}function m(e){return e[0]==="df-child"&&e[1]==="custom"}function b(e){return e[0]==="df-child"&&e[1]==="notif"}function y(e){return e.endsWith("/")?e:e===""?"/":e.slice(0,e.lastIndexOf("/")+1)}function w(e,r){if(r.origin!==e.origin)return r.href;if(r.pathname===e.pathname)return"";const t=y(e.pathname);return r.pathname.startsWith(t)?r.pathname.replace(t,"./"):r.pathname}const A=e=>e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&");function E(e){const r=[];for(const t of e.split(",")){const[s="*",i]=t.split(":"),a="^"+A(s).replace(/\\\*/g,"(.*)")+"$";r.push({regexp:new RegExp(a),append:i})}return r}function S(e,r,t,s){if(!t&&!s)return e.href;const i=new URL(r);if(t&&i.searchParams.forEach((a,d)=>{for(const n of t){if(n.append&&!d.startsWith(n.append)||d===s)continue;const h=n.append?d.replace(n.append,""):d;if(h.match(n.regexp)){e.searchParams.set(h,a);break}}}),s!==null){const a=i.searchParams.get(s);a&&(e.pathname=new URL(a,e).pathname)}return e.href}function P(e,r,t,s,i){const a=new URL(t),d=new URL(r);if(s&&(a.searchParams.forEach((n,h)=>{for(const l of s){if(l.append&&!h.startsWith(l.append))continue;const f=l.append?h.replace(l.append,""):h;if(f.match(l.regexp)){d.searchParams.get(f)===null&&a.searchParams.delete(h);break}}}),d.searchParams.forEach((n,h)=>{for(const l of s){if(!h.match(l.regexp)||(e==null?void 0:e.searchParams.get(h))===n)continue;const f=l.append?l.append+h:h;a.searchParams.set(f,n);break}})),i!==null&&e){const n=w(e,d);n?a.searchParams.set(i,n):a.searchParams.delete(i)}return a}class z{stateChange(r,t){r==="replace"&&window.history.replaceState(null,"",t),r==="push"&&window.history.pushState(null,"",t)}}const g=document.createElement("template");g.innerHTML=`<style>
|
|
2
2
|
:host {
|
|
3
3
|
display: block;
|
|
4
4
|
}
|
|
@@ -8,5 +8,5 @@ var E=Object.defineProperty;var R=(u,c,p)=>c in u?E(u,c,{enumerable:!0,configura
|
|
|
8
8
|
border: none;
|
|
9
9
|
max-height: 100%;
|
|
10
10
|
}
|
|
11
|
-
</style><slot name="loader"></slot><iframe class="d-frame-iframe" frameborder="0">`;class
|
|
11
|
+
</style><slot name="loader"></slot><iframe class="d-frame-iframe" frameborder="0">`;class R extends HTMLElement{constructor(){super();o(this,"adapter");o(this,"connected",!1);o(this,"initialSrc");o(this,"srcUrl");o(this,"parsedSyncParams");o(this,"iframeElement");o(this,"slotElement");o(this,"width");o(this,"aspectRatioHeight");o(this,"resizedHeight");const t=g.content.cloneNode(!0);this.slotElement=t.childNodes[1],this.resize!=="yes"&&(this.slotElement.style.display="none"),this.iframeElement=t.childNodes[2],this.iframeElement.setAttribute("scrolling",this.resize==="yes"?"no":"auto"),this.iframeElement.setAttribute("loading",this.lazy?"lazy":"eager"),this.attachShadow({mode:"open"}).appendChild(t),new ResizeObserver(a=>{this.width!==a[0].contentRect.width&&(this.width=a[0].contentRect.width,this.updateAspectRatioHeight())}).observe(this),this.adapter=new z,window.addEventListener("message",a=>this.onMessage(a))}get debug(){return this.hasAttribute("debug")}set debug(t){t?this.setAttribute("debug",""):this.removeAttribute("debug")}get lazy(){return this.hasAttribute("lazy")}set lazy(t){t?this.setAttribute("lazy",""):this.removeAttribute("lazy")}get src(){return this.getAttribute("src")}set src(t){this.setAttribute("src",t)}get aspectRatio(){return this.getAttribute("aspect-ratio")??"auto"}set aspectRatio(t){this.setAttribute("aspect-ratio",t)}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 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)&&Array.isArray(t.data)){const i=t.data;if(this.log("debug","received message from child",i),u(i)&&(this.postMessageToChild(["df-parent","init",{debug:this.debug,resize:this.resize,syncParams:this.syncParams!==null,syncPath:this.syncPath!==null}]),this.init()),c(i)&&(this.resizedHeight=i[2],this.updateStyle()),p(i)&&(this.parsedSyncParams||this.syncPath)){const a=P(this.srcUrl,i[3],window.location.href,this.parsedSyncParams,this.syncPath);a.href!==window.location.href&&this.adapter.stateChange(i[2],a)}m(i)&&this.dispatchEvent(new CustomEvent("message",{detail:i[2]})),b(i)&&this.dispatchEvent(new CustomEvent("notif",{detail:i[2]}))}}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;this.srcUrl=new URL(this.src.startsWith("/")?window.location.origin+this.src:this.src);const t=S(this.srcUrl,window.location.href,this.parsedSyncParams,this.syncPath);this.initialSrc?this.postMessageToChild(["df-parent","updateSrc",t]):this.iframeElement.setAttribute("src",t)}updateStyle(){let t="";(this.resize==="yes"||this.resize==="auto"&&this.resizedHeight)&&(this.iframeElement.setAttribute("scrolling","no"),this.resizedHeight?(this.slotElement.style.display="none",t+=`height:${this.resizedHeight}px;`):this.height?(this.slotElement.style.display="none",t+=`height:${this.height};`):this.aspectRatio!=="auto"?(this.slotElement.style.display="none",t+=`height:${this.aspectRatioHeight}px;`):(this.slotElement.style.display="block",t+="height:0;")),(this.resize==="no"||this.resize==="auto"&&!this.resizedHeight)&&(this.iframeElement.setAttribute("scrolling","auto"),this.height?t+=`height:${this.height};`:t+=`height:${this.aspectRatioHeight}px;`),this.iframeElement.setAttribute("style",t)}updateAspectRatioHeight(){this.width&&(this.resizedHeight||this.height||(this.aspectRatioHeight=this.width/this.actualAspectRatio,this.updateStyle()))}log(t,...s){t==="debug"&&!this.debug||(t==="debug"&&console.debug("d-frame",...s),t==="info"&&console.info("d-frame",...s))}connectedCallback(){if(!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=E(this.syncParams||"*")),this.updateStyle(),this.updateSrc()}static get observedAttributes(){return["src","aspect-ratio","height","sync-params","sync-path"]}attributeChangedCallback(t,s,i){this.log("debug","attribute change",t,s,i),t==="aspect-ratio"&&this.updateAspectRatioHeight(),t==="src"&&this.updateSrc(),t==="height"&&this.updateStyle(),t==="sync-params"&&this.updateSrc(),t==="sync-path"&&this.updateSrc()}}customElements.define("d-frame",R)})();
|
|
12
12
|
//# sourceMappingURL=d-frame.min.js.map
|
package/package.json
CHANGED
package/src/DFrameContent.d.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type Notif } from './messages.js';
|
|
2
2
|
export type DFrameContentOptions = {
|
|
3
3
|
updateSrc?: (src: string) => void;
|
|
4
4
|
};
|
|
5
5
|
export default class DFrameContent {
|
|
6
|
-
|
|
6
|
+
initialized: boolean;
|
|
7
|
+
options: DFrameContentOptions;
|
|
7
8
|
private pendingCheckHeight;
|
|
8
|
-
|
|
9
|
+
debug: boolean;
|
|
9
10
|
private throttledCheckHeight;
|
|
10
11
|
private lastHeight;
|
|
11
12
|
constructor(options?: DFrameContentOptions);
|
|
12
|
-
log
|
|
13
|
-
onMessage
|
|
14
|
-
postMessageToParent
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
private log;
|
|
14
|
+
private onMessage;
|
|
15
|
+
private postMessageToParent;
|
|
16
|
+
sendNotif(notif: Notif): void;
|
|
17
|
+
sendMessage(msg: any): void;
|
|
18
|
+
private initResize;
|
|
19
|
+
private initSyncParams;
|
|
20
|
+
private createMutationObserver;
|
|
21
|
+
private createWindowEventListeners;
|
|
22
|
+
private checkHeight;
|
|
20
23
|
}
|
package/src/DFrameContent.js
CHANGED
|
@@ -31,6 +31,7 @@ const windowEventTypes = [
|
|
|
31
31
|
'otransitionend', 'resize'
|
|
32
32
|
];
|
|
33
33
|
export default class DFrameContent {
|
|
34
|
+
initialized = false;
|
|
34
35
|
options;
|
|
35
36
|
pendingCheckHeight = false;
|
|
36
37
|
debug = false;
|
|
@@ -71,6 +72,7 @@ export default class DFrameContent {
|
|
|
71
72
|
this.initResize();
|
|
72
73
|
if (message[2].syncParams || message[2].syncPath)
|
|
73
74
|
this.initSyncParams();
|
|
75
|
+
this.initialized = true;
|
|
74
76
|
}
|
|
75
77
|
if (isUpdateSrcMessage(message)) {
|
|
76
78
|
if (this.options.updateSrc) {
|
|
@@ -85,6 +87,12 @@ export default class DFrameContent {
|
|
|
85
87
|
postMessageToParent(message) {
|
|
86
88
|
window.parent.postMessage(message);
|
|
87
89
|
}
|
|
90
|
+
sendNotif(notif) {
|
|
91
|
+
this.postMessageToParent(['df-child', 'notif', notif]);
|
|
92
|
+
}
|
|
93
|
+
sendMessage(msg) {
|
|
94
|
+
this.postMessageToParent(['df-child', 'custom', msg]);
|
|
95
|
+
}
|
|
88
96
|
initResize() {
|
|
89
97
|
this.log('debug', 'initResize');
|
|
90
98
|
this.checkHeight();
|
package/src/DFrameElement.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// this is a good doc about writing web components https://web.dev/articles/custom-elements-best-practices
|
|
2
|
-
import { isHeightMessage, isInitChildMessage, isStateChangeMessage } from './messages.js';
|
|
2
|
+
import { isCustomMessage, isHeightMessage, isInitChildMessage, isNotifMessage, isStateChangeMessage } from './messages.js';
|
|
3
3
|
import { parseSyncParams, getChildSrc, getParentUrl } from './utils/sync-params.js';
|
|
4
4
|
class WindowStateChangeAdapter {
|
|
5
5
|
stateChange(action, newUrl) {
|
|
@@ -143,6 +143,12 @@ export default class DFrameElement extends HTMLElement {
|
|
|
143
143
|
this.adapter.stateChange(message[2], newParentHUrl);
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
|
+
if (isCustomMessage(message)) {
|
|
147
|
+
this.dispatchEvent(new CustomEvent('message', { detail: message[2] }));
|
|
148
|
+
}
|
|
149
|
+
if (isNotifMessage(message)) {
|
|
150
|
+
this.dispatchEvent(new CustomEvent('notif', { detail: message[2] }));
|
|
151
|
+
}
|
|
146
152
|
}
|
|
147
153
|
}
|
|
148
154
|
init() {
|
package/src/messages.d.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
export type InitChildMessage = ['df-child', 'init'];
|
|
2
2
|
export type HeightMessage = ['df-child', 'height', number];
|
|
3
3
|
export type StateChangeMessage = ['df-child', 'stateChange', 'replace' | 'push', string];
|
|
4
|
-
export type
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
4
|
+
export type CustomMessage = ['df-child', 'custom', any];
|
|
5
|
+
export type NotifMessage = ['df-child', 'notif', Notif];
|
|
6
|
+
export type ChildMessage = InitChildMessage | HeightMessage | StateChangeMessage | CustomMessage | NotifMessage;
|
|
7
|
+
export type Notif = {
|
|
8
|
+
type: 'default' | 'info' | 'success' | 'warning' | 'error';
|
|
9
|
+
title: string;
|
|
10
|
+
detail?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function isInitChildMessage(message: any): message is InitChildMessage;
|
|
13
|
+
export declare function isHeightMessage(message: any): message is HeightMessage;
|
|
14
|
+
export declare function isStateChangeMessage(message: any): message is StateChangeMessage;
|
|
15
|
+
export declare function isCustomMessage(message: any): message is CustomMessage;
|
|
16
|
+
export declare function isNotifMessage(message: any): message is NotifMessage;
|
|
8
17
|
export type InitParentMessage = ['df-parent', 'init', {
|
|
9
18
|
debug?: boolean;
|
|
10
19
|
resize: 'yes' | 'no' | 'auto';
|
package/src/messages.js
CHANGED
|
@@ -7,6 +7,12 @@ export function isHeightMessage(message) {
|
|
|
7
7
|
export function isStateChangeMessage(message) {
|
|
8
8
|
return message[0] === 'df-child' && message[1] === 'stateChange';
|
|
9
9
|
}
|
|
10
|
+
export function isCustomMessage(message) {
|
|
11
|
+
return message[0] === 'df-child' && message[1] === 'custom';
|
|
12
|
+
}
|
|
13
|
+
export function isNotifMessage(message) {
|
|
14
|
+
return message[0] === 'df-child' && message[1] === 'notif';
|
|
15
|
+
}
|
|
10
16
|
export function isInitParentMessage(message) {
|
|
11
17
|
return message[0] === 'df-parent' && message[1] === 'init';
|
|
12
18
|
}
|