@data-fair/frame 0.7.0 → 0.8.1
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 +2 -2
- package/lib/DFrameElement.d.ts +2 -2
- package/lib/DFrameElement.js +29 -10
- package/package.json +1 -1
package/dist/d-frame.min.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var x=Object.defineProperty;var H=(l,c,f)=>c in l?x(l,c,{enumerable:!0,configurable:!0,writable:!0,value:f}):l[c]=f;var n=(l,c,f)=>H(l,typeof c!="symbol"?c+"":c,f);(function(){"use strict";function l(e){return e[0]==="df-child"&&e[1]==="init"}function c(e){return e[0]==="df-child"&&e[1]==="height"}function f(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[0]==="df-child"&&e[1]==="ready"}function w(e){return e.endsWith("/")?e:e===""?"/":e.slice(0,e.lastIndexOf("/")+1)}function E(e,a){if(a.origin!==e.origin)return a.href;if(a.pathname===e.pathname)return"";const t=w(e.pathname);return a.pathname.startsWith(t)?a.pathname.replace(t,"./"):a.pathname}const A=e=>e.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&");function v(e){const a=[];for(const t of e.split(",")){const[s="*",i]=t.split(":"),r="^"+A(s).replace(/\\\*/g,"(.*)")+"$";a.push({regexp:new RegExp(r),append:i})}return a}function S(e,a,t,s){if(!t&&!s)return e.href;const i=new URL(a);if(t&&i.searchParams.forEach((r,d)=>{for(const h of t){if(h.append&&!d.startsWith(h.append)||d===s)continue;const o=h.append?d.replace(h.append,""):d;if(o.match(h.regexp)){e.searchParams.set(o,r);break}}}),s!==null){const r=i.searchParams.get(s);r&&(e.pathname=new URL(r,e).pathname)}return e.href}function C(e,a,t,s,i){const r=new URL(t),d=new URL(a);if(s&&(r.searchParams.forEach((h,o)=>{for(const u of s){if(u.append&&!o.startsWith(u.append))continue;const g=u.append?o.replace(u.append,""):o;if(g.match(u.regexp)){d.searchParams.get(g)===null&&r.searchParams.delete(o);break}}}),d.searchParams.forEach((h,o)=>{for(const u of s){if(!o.match(u.regexp)||(e==null?void 0:e.searchParams.get(o))===h)continue;const g=u.append?u.append+o:o;r.searchParams.set(g,h);break}})),i!==null&&e){const h=E(e,d);h?r.searchParams.set(i,h):r.searchParams.delete(i)}return r}function P(e){return e.vIframe===!0&&e.uiNotification!==void 0}function R(e){if(typeof e=="string")return{type:"default",title:e};if(e.error&&!e.errorMsg){const a=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:a}:{type:"error",title:a}}return e.type==="error"?{type:e.clientError?"warning":"error",title:e.msg,detail:e.errorMsg}:{type:e.type??"default",title:e.msg}}class M{stateChange(a,t){a==="replace"&&window.history.replaceState(null,"",t),a==="push"&&window.history.pushState(null,"",t)}}const p=document.createElement("template");p.innerHTML=`<style>
|
|
2
2
|
:host {
|
|
3
3
|
display: block;
|
|
4
4
|
}
|
|
@@ -11,5 +11,5 @@ var H=Object.defineProperty;var x=(l,d,f)=>d in l?H(l,d,{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"
|
|
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 z extends HTMLElement{constructor(){super();n(this,"adapter");n(this,"connected",!1);n(this,"initialSrc");n(this,"srcUrl");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",{});this.randomId=Math.random().toString(36).slice(-6);const t=p.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(r=>{(this.width===void 0||Math.abs(this.width-r[0].contentRect.width)>2)&&(this.width=r[0].contentRect.width,this.updateAspectRatioHeight())}).observe(this),this.adapter=new M,window.addEventListener("message",r=>this.onMessage(r))}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 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 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(l(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}]),this.init()),c(i)&&(this.resizedHeight=i[2],this.resize==="yes"&&!this.ready&&!this.readyMessage&&(this.ready=!0,this.dispatchEvent(new CustomEvent("ready"))),this.updateStyle()),f(i)){if(this.parsedSyncParams||this.syncPath){const r=C(this.srcUrl,i[3],window.location.href,this.parsedSyncParams,this.syncPath);r.href!==window.location.href&&this.adapter.stateChange(i[2],r)}this.stateChangeEvents&&this.dispatchEvent(new CustomEvent("state-change",{detail:[i[2],i[3]]}))}m(i)&&this.dispatchEvent(new CustomEvent("message",{detail:i[2]})),b(i)&&this.dispatchEvent(new CustomEvent("notif",{detail:i[2]})),y(i)&&(this.ready=!0,this.dispatchEvent(new CustomEvent("ready")),this.updateStyle())}else P(i)&&this.dispatchEvent(new CustomEvent("notif",{detail:R(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;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.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 r=this.getAttribute(i);if(r!==null){const d=i.slice(7);s[d]=r}}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])}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=v(this.syncParams||"*")),this.updateStyle(),this.updateIframeExtraAttrs(),this.updateSrc()}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",z)})();
|
|
15
15
|
//# sourceMappingURL=d-frame.min.js.map
|
package/lib/DFrameElement.d.ts
CHANGED
|
@@ -8,8 +8,6 @@ export default class DFrameElement extends HTMLElement {
|
|
|
8
8
|
set id(value: string);
|
|
9
9
|
get debug(): boolean;
|
|
10
10
|
set debug(value: boolean);
|
|
11
|
-
get lazy(): boolean;
|
|
12
|
-
set lazy(value: boolean);
|
|
13
11
|
get src(): string;
|
|
14
12
|
set src(value: string);
|
|
15
13
|
get aspectRatio(): string | null;
|
|
@@ -40,6 +38,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
40
38
|
private randomId;
|
|
41
39
|
private iframeLoaded;
|
|
42
40
|
private ready;
|
|
41
|
+
private iframeExtraAttrs;
|
|
43
42
|
get actualAspectRatio(): number;
|
|
44
43
|
constructor();
|
|
45
44
|
onMessage(e: MessageEvent): void;
|
|
@@ -48,6 +47,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
48
47
|
updateSrc(): void;
|
|
49
48
|
updateStyle(): void;
|
|
50
49
|
updateAspectRatioHeight(): void;
|
|
50
|
+
updateIframeExtraAttrs(): void;
|
|
51
51
|
log(level: 'debug' | 'info', ...args: any[]): void;
|
|
52
52
|
connectedCallback(): void;
|
|
53
53
|
disconnectedCallback(): void;
|
package/lib/DFrameElement.js
CHANGED
|
@@ -24,7 +24,7 @@ template.innerHTML = `<style>
|
|
|
24
24
|
height: 100%;
|
|
25
25
|
border: none;
|
|
26
26
|
}
|
|
27
|
-
</style><div class="d-frame-wrapper"><slot name="loading"><div style="min-height:150px;"></div></slot><iframe class="d-frame-iframe"
|
|
27
|
+
</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>`;
|
|
28
28
|
export default class DFrameElement extends HTMLElement {
|
|
29
29
|
get id() { return this.getAttribute('id') ?? this.randomId; }
|
|
30
30
|
set id(value) {
|
|
@@ -37,13 +37,6 @@ export default class DFrameElement extends HTMLElement {
|
|
|
37
37
|
else
|
|
38
38
|
this.removeAttribute('debug');
|
|
39
39
|
}
|
|
40
|
-
get lazy() { return this.hasAttribute('lazy'); }
|
|
41
|
-
set lazy(value) {
|
|
42
|
-
if (value)
|
|
43
|
-
this.setAttribute('lazy', '');
|
|
44
|
-
else
|
|
45
|
-
this.removeAttribute('lazy');
|
|
46
|
-
}
|
|
47
40
|
get src() { return this.getAttribute('src'); }
|
|
48
41
|
set src(value) { this.setAttribute('src', value); }
|
|
49
42
|
get aspectRatio() {
|
|
@@ -122,6 +115,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
122
115
|
randomId;
|
|
123
116
|
iframeLoaded = false;
|
|
124
117
|
ready = false;
|
|
118
|
+
iframeExtraAttrs = {};
|
|
125
119
|
get actualAspectRatio() {
|
|
126
120
|
if (this.aspectRatio !== 'auto')
|
|
127
121
|
return Number(this.aspectRatio);
|
|
@@ -140,8 +134,6 @@ export default class DFrameElement extends HTMLElement {
|
|
|
140
134
|
this.wrapperElement = root.childNodes[1];
|
|
141
135
|
this.slotElement = this.wrapperElement.childNodes[0];
|
|
142
136
|
this.iframeElement = this.wrapperElement.childNodes[1];
|
|
143
|
-
this.iframeElement.setAttribute('scrolling', this.resize === 'yes' ? 'no' : 'auto');
|
|
144
|
-
this.iframeElement.setAttribute('loading', this.lazy ? 'lazy' : 'eager');
|
|
145
137
|
this.iframeElement.addEventListener('load', () => {
|
|
146
138
|
if (this.iframeElement.getAttribute('src')) {
|
|
147
139
|
this.log('debug', 'iframe loaded');
|
|
@@ -300,6 +292,30 @@ export default class DFrameElement extends HTMLElement {
|
|
|
300
292
|
this.aspectRatioHeight = Math.ceil(this.width / this.actualAspectRatio);
|
|
301
293
|
this.updateStyle();
|
|
302
294
|
}
|
|
295
|
+
updateIframeExtraAttrs() {
|
|
296
|
+
const previousAttrs = this.iframeExtraAttrs;
|
|
297
|
+
const newAttrs = {};
|
|
298
|
+
for (const name of this.getAttributeNames()) {
|
|
299
|
+
if (name.startsWith('iframe-')) {
|
|
300
|
+
const value = this.getAttribute(name);
|
|
301
|
+
if (value !== null) {
|
|
302
|
+
const attrName = name.slice(7);
|
|
303
|
+
newAttrs[attrName] = value;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
this.log('debug', 'update iframe extra attributes', previousAttrs, newAttrs);
|
|
308
|
+
for (const key of Object.keys(previousAttrs)) {
|
|
309
|
+
if (!(key in newAttrs)) {
|
|
310
|
+
this.iframeElement.removeAttribute(key);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
for (const key of Object.keys(newAttrs)) {
|
|
314
|
+
if (newAttrs[key] !== previousAttrs[key]) {
|
|
315
|
+
this.iframeElement.setAttribute(key, newAttrs[key]);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
303
319
|
log(level, ...args) {
|
|
304
320
|
if (level === 'debug' && !this.debug)
|
|
305
321
|
return;
|
|
@@ -330,6 +346,7 @@ export default class DFrameElement extends HTMLElement {
|
|
|
330
346
|
if (this.syncParams !== null)
|
|
331
347
|
this.parsedSyncParams = parseSyncParams(this.syncParams || '*');
|
|
332
348
|
this.updateStyle();
|
|
349
|
+
this.updateIframeExtraAttrs();
|
|
333
350
|
this.updateSrc();
|
|
334
351
|
}
|
|
335
352
|
disconnectedCallback() {
|
|
@@ -343,6 +360,8 @@ export default class DFrameElement extends HTMLElement {
|
|
|
343
360
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
344
361
|
if (name === 'aspect-ratio')
|
|
345
362
|
this.updateAspectRatioHeight();
|
|
363
|
+
if (name.startsWith('iframe-'))
|
|
364
|
+
this.updateIframeExtraAttrs();
|
|
346
365
|
if (!this.connected)
|
|
347
366
|
return;
|
|
348
367
|
this.log('debug', 'attribute change', name, oldValue, newValue);
|