@contello/extension 7.8.2 → 8.0.0-next.1741104987

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/client.d.ts CHANGED
@@ -1,20 +1,34 @@
1
1
  import { ContelloClientChildMethods, ContelloClientParentMethods } from './methods';
2
2
  import { ExtensionChannel } from './channel';
3
3
  import { ContelloDialogRef, ContelloDialogOptions } from './dialog-ref';
4
+ type ContelloEntityDetailParams = {
5
+ mode: 'create';
6
+ } | {
7
+ mode: 'edit';
8
+ id: string;
9
+ } | {
10
+ mode: 'clone';
11
+ id: string;
12
+ };
4
13
  export declare class ContelloClient<D, O extends ContelloClientChildMethods, R extends ContelloClientParentMethods> {
5
14
  protected channel: ExtensionChannel<O, R>;
6
- protected applicationId: string;
15
+ protected projectId: string;
7
16
  private resizeObserver?;
8
17
  private targetOrigin;
9
18
  data?: D;
10
19
  private dialogs;
11
- constructor(targetOrigin: string, channelId: string, applicationId: string, debug: boolean);
20
+ constructor(targetOrigin: string, channelId: string, projectId: string, debug: boolean);
12
21
  connect(): Promise<void>;
13
22
  ready(): Promise<R["ready"][1]>;
14
23
  getAuthToken(): Promise<string>;
15
- createApplicationUrl(): string;
24
+ createProjectUrl(): string;
25
+ private createEntityEntryUrl;
16
26
  createSingletonEntityUrl(referenceName: string): string;
17
- createEntityUrl(referenceName: string, entityId?: string): string;
27
+ createEntityDetailUrl(referenceName: string, params: ContelloEntityDetailParams): string;
28
+ /**
29
+ * @deprecated Use createEntityDetailUrl instead
30
+ */
31
+ createEntityUrl(referenceName: string, entityId: string): string;
18
32
  createExtensionUrl(referenceName: string, params?: {
19
33
  path?: string[];
20
34
  query?: {
@@ -28,3 +42,4 @@ export declare class ContelloClient<D, O extends ContelloClientChildMethods, R e
28
42
  private getWindowHeight;
29
43
  private getDialogController;
30
44
  }
45
+ export {};
package/dist/index.js CHANGED
@@ -1,21 +1,21 @@
1
- var w = Object.defineProperty;
2
- var I = (l, e, t) => e in l ? w(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
- var a = (l, e, t) => (I(l, typeof e != "symbol" ? e + "" : e, t), t);
4
- let m = 0, y = 0;
1
+ var p = Object.defineProperty;
2
+ var I = (l, e, t) => e in l ? p(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
+ var i = (l, e, t) => (I(l, typeof e != "symbol" ? e + "" : e, t), t);
4
+ let y = 0, m = 0;
5
5
  class v {
6
6
  constructor(e) {
7
- a(this, "handlers", /* @__PURE__ */ new Map());
8
- a(this, "listeners", /* @__PURE__ */ new Map());
9
- a(this, "targetWindow");
10
- a(this, "channelId");
11
- a(this, "targetOrigin");
12
- a(this, "isParent");
13
- a(this, "handler", (e) => {
7
+ i(this, "handlers", /* @__PURE__ */ new Map());
8
+ i(this, "listeners", /* @__PURE__ */ new Map());
9
+ i(this, "targetWindow");
10
+ i(this, "channelId");
11
+ i(this, "targetOrigin");
12
+ i(this, "isParent");
13
+ i(this, "handler", (e) => {
14
14
  var t;
15
15
  if (e.data.channelId === this.channelId && (this.targetOrigin === "*" || e.origin === this.targetOrigin)) {
16
- const { channelId: n, requestId: i, method: r } = e.data;
17
- n === this.channelId && (this.params.debug && console.log(this.isParent ? "Parent received" : "Child received", e.data), this.handlers.has(i) ? this.handlers.get(i)(e.data) : this.listeners.has(r) && Promise.resolve(
18
- (t = this.listeners.get(r)) == null ? void 0 : t(e.data.payload)
16
+ const { channelId: n, requestId: r, method: a } = e.data;
17
+ n === this.channelId && (this.params.debug && console.log(this.isParent ? "Parent received" : "Child received", e.data), this.handlers.has(r) ? this.handlers.get(r)(e.data) : this.listeners.has(a) && Promise.resolve(
18
+ (t = this.listeners.get(a)) == null ? void 0 : t(e.data.payload)
19
19
  ).then((s) => this.respond(e.data, s)).catch((s) => this.respondError(e.data, s)));
20
20
  }
21
21
  });
@@ -58,11 +58,11 @@ class v {
58
58
  this.listeners.set(e, t);
59
59
  }
60
60
  call(e, t) {
61
- return new Promise((n, i) => {
62
- const r = this.createRequestId();
63
- this.send({ channelId: this.channelId, requestId: r, method: e, payload: t }), this.handlers.set(r, (s) => {
64
- if (this.handlers.delete(r), s.error)
65
- return i(s.error);
61
+ return new Promise((n, r) => {
62
+ const a = this.createRequestId();
63
+ this.send({ channelId: this.channelId, requestId: a, method: e, payload: t }), this.handlers.set(a, (s) => {
64
+ if (this.handlers.delete(a), s.error)
65
+ return r(s.error);
66
66
  n(s.payload);
67
67
  });
68
68
  });
@@ -72,21 +72,21 @@ class v {
72
72
  (t = this.targetWindow) == null || t.postMessage(e, this.targetOrigin);
73
73
  }
74
74
  createChannelId() {
75
- return `contello-channel-${++m}-${Math.random().toString(36).substring(2)}`;
75
+ return `contello-channel-${++y}-${Math.random().toString(36).substring(2)}`;
76
76
  }
77
77
  createRequestId() {
78
- return `${this.isParent ? "parent" : "child"}-request-${++y}-${Math.random().toString(36).substring(2)}`;
78
+ return `${this.isParent ? "parent" : "child"}-request-${++m}-${Math.random().toString(36).substring(2)}`;
79
79
  }
80
80
  }
81
- class f {
81
+ class P {
82
82
  constructor({ channel: e, options: t, controller: n }) {
83
- a(this, "_id");
84
- a(this, "open");
85
- a(this, "connected");
86
- a(this, "ready");
87
- a(this, "complete");
88
- a(this, "close");
89
- this.open = e.call("openDialog", t).then(({ id: i }) => this._id = i), this.connected = n.connected.promise, this.ready = n.ready.promise, this.complete = n.complete.promise, this.close = n.close;
83
+ i(this, "_id");
84
+ i(this, "open");
85
+ i(this, "connected");
86
+ i(this, "ready");
87
+ i(this, "complete");
88
+ i(this, "close");
89
+ this.open = e.call("openDialog", t).then(({ id: r }) => this._id = r), this.connected = n.connected.promise, this.ready = n.ready.promise, this.complete = n.complete.promise, this.close = n.close;
90
90
  }
91
91
  get id() {
92
92
  return this._id;
@@ -94,22 +94,22 @@ class f {
94
94
  }
95
95
  class c {
96
96
  constructor() {
97
- a(this, "resolve");
98
- a(this, "reject");
99
- a(this, "promise", new Promise((e, t) => {
97
+ i(this, "resolve");
98
+ i(this, "reject");
99
+ i(this, "promise", new Promise((e, t) => {
100
100
  this.resolve = e, this.reject = t;
101
101
  }));
102
102
  }
103
103
  }
104
104
  class h {
105
- constructor(e, t, n, i) {
106
- a(this, "channel");
107
- a(this, "applicationId");
108
- a(this, "resizeObserver");
109
- a(this, "targetOrigin");
110
- a(this, "data");
111
- a(this, "dialogs", /* @__PURE__ */ new Map());
112
- this.channel = new v({ debug: i }), this.channel.connectParent(e, t), this.applicationId = n, this.targetOrigin = e;
105
+ constructor(e, t, n, r) {
106
+ i(this, "channel");
107
+ i(this, "projectId");
108
+ i(this, "resizeObserver");
109
+ i(this, "targetOrigin");
110
+ i(this, "data");
111
+ i(this, "dialogs", /* @__PURE__ */ new Map());
112
+ this.channel = new v({ debug: r }), this.channel.connectParent(e, t), this.projectId = n, this.targetOrigin = e;
113
113
  }
114
114
  connect() {
115
115
  return this.channel.call("connect").then(({ data: e }) => {
@@ -120,8 +120,8 @@ class h {
120
120
  var n;
121
121
  return (n = this.getDialogController(t)) == null ? void 0 : n.ready.resolve();
122
122
  }), this.channel.on("dialogComplete", ({ id: t, value: n }) => {
123
- var i;
124
- return (i = this.getDialogController(t)) == null ? void 0 : i.complete.resolve(n);
123
+ var r;
124
+ return (r = this.getDialogController(t)) == null ? void 0 : r.complete.resolve(n);
125
125
  }), this.data = e;
126
126
  });
127
127
  }
@@ -131,19 +131,29 @@ class h {
131
131
  getAuthToken() {
132
132
  return this.channel.call("getAuthToken").then(({ token: e }) => e);
133
133
  }
134
- createApplicationUrl() {
135
- return `${this.targetOrigin}/ui/applications/${this.applicationId}`;
134
+ createProjectUrl() {
135
+ return `${this.targetOrigin}/ui/projects/${this.projectId}`;
136
+ }
137
+ createEntityEntryUrl(e) {
138
+ return `${this.createProjectUrl()}/entities/${e}`;
136
139
  }
137
140
  createSingletonEntityUrl(e) {
138
- return `${this.createApplicationUrl()}/entities/${e}`;
141
+ return this.createEntityEntryUrl(e);
142
+ }
143
+ createEntityDetailUrl(e, t) {
144
+ const n = this.createEntityEntryUrl(e);
145
+ return t.mode === "create" ? `${n}/create` : `${n}/${t.mode}/${t.id}`;
139
146
  }
147
+ /**
148
+ * @deprecated Use createEntityDetailUrl instead
149
+ */
140
150
  createEntityUrl(e, t) {
141
- return `${this.createApplicationUrl()}/entities/${e}/${t}`;
151
+ return this.createEntityDetailUrl(e, { mode: "edit", id: t });
142
152
  }
143
153
  createExtensionUrl(e, t) {
144
- var r;
145
- const n = ((r = t == null ? void 0 : t.path) == null ? void 0 : r.join("/")) || "", i = new URLSearchParams((t == null ? void 0 : t.query) || {}).toString();
146
- return `${this.createApplicationUrl()}/extensions/${e}${n ? `/${n}` : ""}${i ? `?${i}` : ""}`;
154
+ var a;
155
+ const n = ((a = t == null ? void 0 : t.path) == null ? void 0 : a.join("/")) || "", r = new URLSearchParams((t == null ? void 0 : t.query) || {}).toString();
156
+ return `${this.createProjectUrl()}/extensions/${e}${n ? `/${n}` : ""}${r ? `?${r}` : ""}`;
147
157
  }
148
158
  navigate(e) {
149
159
  return this.channel.call("navigate", { url: e });
@@ -161,7 +171,7 @@ class h {
161
171
  throw new Error("The channel is not yet initialized");
162
172
  this.channel.call("closeDialog", { id: n.id }), this.dialogs.delete(n);
163
173
  }
164
- }, n = new f({ channel: this.channel, options: e, controller: t });
174
+ }, n = new P({ channel: this.channel, options: e, controller: t });
165
175
  return this.dialogs.set(n, t), n.complete.then(() => this.dialogs.delete(n)), n;
166
176
  }
167
177
  listenForResize() {
@@ -183,26 +193,26 @@ class h {
183
193
  }
184
194
  }
185
195
  function d(l) {
186
- const e = new URL(location.href), t = e.searchParams.get("channelId"), n = e.searchParams.get("origin"), i = e.searchParams.get("applicationId"), r = e.searchParams.get("debug") === "true";
187
- if (!t || !n || !i)
196
+ const e = new URL(location.href), t = e.searchParams.get("channelId"), n = e.searchParams.get("origin"), r = e.searchParams.get("applicationId"), a = e.searchParams.get("debug") === "true";
197
+ if (!t || !n || !r)
188
198
  throw new Error("Missing required URL parameters");
189
199
  if (!(l != null && l.length))
190
200
  throw new Error("No trusted origins provided");
191
201
  if (!l.includes(n))
192
202
  throw new Error(`Origin ${n} is not trusted`);
193
- return { channelId: t, targetOrigin: n, applicationId: i, debug: r };
203
+ return { channelId: t, targetOrigin: n, applicationId: r, debug: a };
194
204
  }
195
205
  class g extends h {
196
- constructor(t, n, i, r) {
197
- super(t, n, i, r);
198
- a(this, "validate", () => !0);
199
- a(this, "newValue", () => null);
206
+ constructor(t, n, r, a) {
207
+ super(t, n, r, a);
208
+ i(this, "validate", () => !0);
209
+ i(this, "newValue", () => null);
200
210
  this.channel.on("validate", async () => ({ valid: await Promise.resolve(this.validate()) })), this.channel.on("newValue", async (s) => {
201
211
  await Promise.resolve(this.newValue(s.value));
202
212
  });
203
213
  }
204
214
  static connect(t) {
205
- const { targetOrigin: n, channelId: i, applicationId: r, debug: s } = d(t.trustedOrigins), o = new g(n, i, r, s);
215
+ const { targetOrigin: n, channelId: r, applicationId: a, debug: s } = d(t.trustedOrigins), o = new g(n, r, a, s);
206
216
  return o.validate = t.validator || (() => !0), o.newValue = t.newValue || (() => null), o.connect().then(() => o);
207
217
  }
208
218
  async getValue() {
@@ -221,23 +231,23 @@ class g extends h {
221
231
  }
222
232
  class u extends h {
223
233
  static connect({ trustedOrigins: e }) {
224
- const { targetOrigin: t, channelId: n, applicationId: i, debug: r } = d(e), s = new u(t, n, i, r);
234
+ const { targetOrigin: t, channelId: n, applicationId: r, debug: a } = d(e), s = new u(t, n, r, a);
225
235
  return s.connect().then(() => s);
226
236
  }
227
- constructor(e, t, n, i) {
228
- super(e, t, n, i);
237
+ constructor(e, t, n, r) {
238
+ super(e, t, n, r);
229
239
  }
230
240
  close(e) {
231
241
  return this.channel.call("complete", { value: e });
232
242
  }
233
243
  }
234
- class p extends h {
244
+ class w extends h {
235
245
  static connect({ trustedOrigins: e }) {
236
- const { targetOrigin: t, channelId: n, applicationId: i, debug: r } = d(e), s = new p(t, n, i, r);
246
+ const { targetOrigin: t, channelId: n, applicationId: r, debug: a } = d(e), s = new w(t, n, r, a);
237
247
  return s.connect().then(() => s);
238
248
  }
239
- constructor(e, t, n, i) {
240
- super(e, t, n, i);
249
+ constructor(e, t, n, r) {
250
+ super(e, t, n, r);
241
251
  }
242
252
  getUrlData() {
243
253
  return this.channel.call("getUrlData");
@@ -249,7 +259,7 @@ class p extends h {
249
259
  export {
250
260
  g as ContelloCustomProperty,
251
261
  u as ContelloDialog,
252
- f as ContelloDialogRef,
253
- p as ContelloExtension,
262
+ P as ContelloDialogRef,
263
+ w as ContelloExtension,
254
264
  v as ExtensionChannel
255
265
  };
@@ -1 +1 @@
1
- (function(l,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(l=typeof globalThis<"u"?globalThis:l||self,o(l.ContelloExtension={}))})(this,function(l){"use strict";var v=Object.defineProperty;var P=(l,o,h)=>o in l?v(l,o,{enumerable:!0,configurable:!0,writable:!0,value:h}):l[o]=h;var a=(l,o,h)=>(P(l,typeof o!="symbol"?o+"":o,h),h);let o=0,h=0;class y{constructor(e){a(this,"handlers",new Map);a(this,"listeners",new Map);a(this,"targetWindow");a(this,"channelId");a(this,"targetOrigin");a(this,"isParent");a(this,"handler",e=>{var t;if(e.data.channelId===this.channelId&&(this.targetOrigin==="*"||e.origin===this.targetOrigin)){const{channelId:n,requestId:i,method:r}=e.data;n===this.channelId&&(this.params.debug&&console.log(this.isParent?"Parent received":"Child received",e.data),this.handlers.has(i)?this.handlers.get(i)(e.data):this.listeners.has(r)&&Promise.resolve((t=this.listeners.get(r))==null?void 0:t(e.data.payload)).then(s=>this.respond(e.data,s)).catch(s=>this.respondError(e.data,s)))}});this.params=e}populateChannelId(){return this.channelId||(this.channelId=this.createChannelId()),this.channelId}connectParent(e,t){this.channelId=t,this.targetOrigin=e,this.targetWindow=window.parent,this.isParent=!1,this.connect()}connectChild(e){this.populateChannelId(),this.targetWindow=e,this.targetOrigin="*",this.isParent=!0,this.connect()}getChannelId(){return this.channelId}getIsDebug(){return this.params.debug}getTargetOrigin(){return this.targetOrigin}getTargetWindow(){return this.targetWindow}connect(){window.addEventListener("message",this.handler)}disconnect(){window.removeEventListener("message",this.handler)}respond(e,t){this.send({channelId:e.channelId,requestId:e.requestId,method:e.method,payload:t})}respondError(e,t){this.send({channelId:e.channelId,requestId:e.requestId,method:e.method,error:t})}on(e,t){this.listeners.set(e,t)}call(e,t){return new Promise((n,i)=>{const r=this.createRequestId();this.send({channelId:this.channelId,requestId:r,method:e,payload:t}),this.handlers.set(r,s=>{if(this.handlers.delete(r),s.error)return i(s.error);n(s.payload)})})}send(e){var t;(t=this.targetWindow)==null||t.postMessage(e,this.targetOrigin)}createChannelId(){return`contello-channel-${++o}-${Math.random().toString(36).substring(2)}`}createRequestId(){return`${this.isParent?"parent":"child"}-request-${++h}-${Math.random().toString(36).substring(2)}`}}class f{constructor({channel:e,options:t,controller:n}){a(this,"_id");a(this,"open");a(this,"connected");a(this,"ready");a(this,"complete");a(this,"close");this.open=e.call("openDialog",t).then(({id:i})=>this._id=i),this.connected=n.connected.promise,this.ready=n.ready.promise,this.complete=n.complete.promise,this.close=n.close}get id(){return this._id}}class g{constructor(){a(this,"resolve");a(this,"reject");a(this,"promise",new Promise((e,t)=>{this.resolve=e,this.reject=t}))}}class u{constructor(e,t,n,i){a(this,"channel");a(this,"applicationId");a(this,"resizeObserver");a(this,"targetOrigin");a(this,"data");a(this,"dialogs",new Map);this.channel=new y({debug:i}),this.channel.connectParent(e,t),this.applicationId=n,this.targetOrigin=e}connect(){return this.channel.call("connect").then(({data:e})=>{this.channel.on("dialogConnect",({id:t})=>{var n;return(n=this.getDialogController(t))==null?void 0:n.connected.resolve()}),this.channel.on("dialogReady",({id:t})=>{var n;return(n=this.getDialogController(t))==null?void 0:n.ready.resolve()}),this.channel.on("dialogComplete",({id:t,value:n})=>{var i;return(i=this.getDialogController(t))==null?void 0:i.complete.resolve(n)}),this.data=e})}ready(){return this.listenForResize(),this.channel.call("ready",{height:this.getWindowHeight()})}getAuthToken(){return this.channel.call("getAuthToken").then(({token:e})=>e)}createApplicationUrl(){return`${this.targetOrigin}/ui/applications/${this.applicationId}`}createSingletonEntityUrl(e){return`${this.createApplicationUrl()}/entities/${e}`}createEntityUrl(e,t){return`${this.createApplicationUrl()}/entities/${e}/${t}`}createExtensionUrl(e,t){var r;const n=((r=t==null?void 0:t.path)==null?void 0:r.join("/"))||"",i=new URLSearchParams((t==null?void 0:t.query)||{}).toString();return`${this.createApplicationUrl()}/extensions/${e}${n?`/${n}`:""}${i?`?${i}`:""}`}navigate(e){return this.channel.call("navigate",{url:e})}displayNotification(e,t){return this.channel.call("displayNotification",{type:e,message:t})}openDialog(e){const t={connected:new g,ready:new g,complete:new g,close:()=>{if(!this.channel)throw new Error("The channel is not yet initialized");this.channel.call("closeDialog",{id:n.id}),this.dialogs.delete(n)}},n=new f({channel:this.channel,options:e,controller:t});return this.dialogs.set(n,t),n.complete.then(()=>this.dialogs.delete(n)),n}listenForResize(){this.resizeObserver=new ResizeObserver(()=>{this.channel.call("resize",{height:this.getWindowHeight()})}),this.resizeObserver.observe(document.documentElement)}getWindowHeight(){return Math.max(document.body.scrollHeight,document.body.offsetHeight,document.body.clientHeight)}getDialogController(e){const t=Array.from(this.dialogs.keys()).find(n=>n.id===e);if(t)return this.dialogs.get(t)}}function p(c){const e=new URL(location.href),t=e.searchParams.get("channelId"),n=e.searchParams.get("origin"),i=e.searchParams.get("applicationId"),r=e.searchParams.get("debug")==="true";if(!t||!n||!i)throw new Error("Missing required URL parameters");if(!(c!=null&&c.length))throw new Error("No trusted origins provided");if(!c.includes(n))throw new Error(`Origin ${n} is not trusted`);return{channelId:t,targetOrigin:n,applicationId:i,debug:r}}class w extends u{constructor(t,n,i,r){super(t,n,i,r);a(this,"validate",()=>!0);a(this,"newValue",()=>null);this.channel.on("validate",async()=>({valid:await Promise.resolve(this.validate())})),this.channel.on("newValue",async s=>{await Promise.resolve(this.newValue(s.value))})}static connect(t){const{targetOrigin:n,channelId:i,applicationId:r,debug:s}=p(t.trustedOrigins),d=new w(n,i,r,s);return d.validate=t.validator||(()=>!0),d.newValue=t.newValue||(()=>null),d.connect().then(()=>d)}async getValue(){return(await this.channel.call("getValue")).value}async setValue(t){const n=await Promise.resolve(this.validate());return await this.channel.call("setValue",{value:t,valid:n})}async getValueByPath(t){return(await this.channel.call("getValueByPath",{path:t})).value}async setValueByPath(t,n){return this.channel.call("setValueByPath",{value:n,path:t})}}class I extends u{static connect({trustedOrigins:e}){const{targetOrigin:t,channelId:n,applicationId:i,debug:r}=p(e),s=new I(t,n,i,r);return s.connect().then(()=>s)}constructor(e,t,n,i){super(e,t,n,i)}close(e){return this.channel.call("complete",{value:e})}}class m extends u{static connect({trustedOrigins:e}){const{targetOrigin:t,channelId:n,applicationId:i,debug:r}=p(e),s=new m(t,n,i,r);return s.connect().then(()=>s)}constructor(e,t,n,i){super(e,t,n,i)}getUrlData(){return this.channel.call("getUrlData")}setBreadcrumbs(e){return this.channel.call("setBreadcrumbs",{breadcrumbs:e})}}l.ContelloCustomProperty=w,l.ContelloDialog=I,l.ContelloDialogRef=f,l.ContelloExtension=m,l.ExtensionChannel=y,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
1
+ (function(l,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(l=typeof globalThis<"u"?globalThis:l||self,o(l.ContelloExtension={}))})(this,function(l){"use strict";var P=Object.defineProperty;var v=(l,o,h)=>o in l?P(l,o,{enumerable:!0,configurable:!0,writable:!0,value:h}):l[o]=h;var r=(l,o,h)=>(v(l,typeof o!="symbol"?o+"":o,h),h);let o=0,h=0;class m{constructor(e){r(this,"handlers",new Map);r(this,"listeners",new Map);r(this,"targetWindow");r(this,"channelId");r(this,"targetOrigin");r(this,"isParent");r(this,"handler",e=>{var t;if(e.data.channelId===this.channelId&&(this.targetOrigin==="*"||e.origin===this.targetOrigin)){const{channelId:n,requestId:i,method:a}=e.data;n===this.channelId&&(this.params.debug&&console.log(this.isParent?"Parent received":"Child received",e.data),this.handlers.has(i)?this.handlers.get(i)(e.data):this.listeners.has(a)&&Promise.resolve((t=this.listeners.get(a))==null?void 0:t(e.data.payload)).then(s=>this.respond(e.data,s)).catch(s=>this.respondError(e.data,s)))}});this.params=e}populateChannelId(){return this.channelId||(this.channelId=this.createChannelId()),this.channelId}connectParent(e,t){this.channelId=t,this.targetOrigin=e,this.targetWindow=window.parent,this.isParent=!1,this.connect()}connectChild(e){this.populateChannelId(),this.targetWindow=e,this.targetOrigin="*",this.isParent=!0,this.connect()}getChannelId(){return this.channelId}getIsDebug(){return this.params.debug}getTargetOrigin(){return this.targetOrigin}getTargetWindow(){return this.targetWindow}connect(){window.addEventListener("message",this.handler)}disconnect(){window.removeEventListener("message",this.handler)}respond(e,t){this.send({channelId:e.channelId,requestId:e.requestId,method:e.method,payload:t})}respondError(e,t){this.send({channelId:e.channelId,requestId:e.requestId,method:e.method,error:t})}on(e,t){this.listeners.set(e,t)}call(e,t){return new Promise((n,i)=>{const a=this.createRequestId();this.send({channelId:this.channelId,requestId:a,method:e,payload:t}),this.handlers.set(a,s=>{if(this.handlers.delete(a),s.error)return i(s.error);n(s.payload)})})}send(e){var t;(t=this.targetWindow)==null||t.postMessage(e,this.targetOrigin)}createChannelId(){return`contello-channel-${++o}-${Math.random().toString(36).substring(2)}`}createRequestId(){return`${this.isParent?"parent":"child"}-request-${++h}-${Math.random().toString(36).substring(2)}`}}class f{constructor({channel:e,options:t,controller:n}){r(this,"_id");r(this,"open");r(this,"connected");r(this,"ready");r(this,"complete");r(this,"close");this.open=e.call("openDialog",t).then(({id:i})=>this._id=i),this.connected=n.connected.promise,this.ready=n.ready.promise,this.complete=n.complete.promise,this.close=n.close}get id(){return this._id}}class g{constructor(){r(this,"resolve");r(this,"reject");r(this,"promise",new Promise((e,t)=>{this.resolve=e,this.reject=t}))}}class u{constructor(e,t,n,i){r(this,"channel");r(this,"projectId");r(this,"resizeObserver");r(this,"targetOrigin");r(this,"data");r(this,"dialogs",new Map);this.channel=new m({debug:i}),this.channel.connectParent(e,t),this.projectId=n,this.targetOrigin=e}connect(){return this.channel.call("connect").then(({data:e})=>{this.channel.on("dialogConnect",({id:t})=>{var n;return(n=this.getDialogController(t))==null?void 0:n.connected.resolve()}),this.channel.on("dialogReady",({id:t})=>{var n;return(n=this.getDialogController(t))==null?void 0:n.ready.resolve()}),this.channel.on("dialogComplete",({id:t,value:n})=>{var i;return(i=this.getDialogController(t))==null?void 0:i.complete.resolve(n)}),this.data=e})}ready(){return this.listenForResize(),this.channel.call("ready",{height:this.getWindowHeight()})}getAuthToken(){return this.channel.call("getAuthToken").then(({token:e})=>e)}createProjectUrl(){return`${this.targetOrigin}/ui/projects/${this.projectId}`}createEntityEntryUrl(e){return`${this.createProjectUrl()}/entities/${e}`}createSingletonEntityUrl(e){return this.createEntityEntryUrl(e)}createEntityDetailUrl(e,t){const n=this.createEntityEntryUrl(e);return t.mode==="create"?`${n}/create`:`${n}/${t.mode}/${t.id}`}createEntityUrl(e,t){return this.createEntityDetailUrl(e,{mode:"edit",id:t})}createExtensionUrl(e,t){var a;const n=((a=t==null?void 0:t.path)==null?void 0:a.join("/"))||"",i=new URLSearchParams((t==null?void 0:t.query)||{}).toString();return`${this.createProjectUrl()}/extensions/${e}${n?`/${n}`:""}${i?`?${i}`:""}`}navigate(e){return this.channel.call("navigate",{url:e})}displayNotification(e,t){return this.channel.call("displayNotification",{type:e,message:t})}openDialog(e){const t={connected:new g,ready:new g,complete:new g,close:()=>{if(!this.channel)throw new Error("The channel is not yet initialized");this.channel.call("closeDialog",{id:n.id}),this.dialogs.delete(n)}},n=new f({channel:this.channel,options:e,controller:t});return this.dialogs.set(n,t),n.complete.then(()=>this.dialogs.delete(n)),n}listenForResize(){this.resizeObserver=new ResizeObserver(()=>{this.channel.call("resize",{height:this.getWindowHeight()})}),this.resizeObserver.observe(document.documentElement)}getWindowHeight(){return Math.max(document.body.scrollHeight,document.body.offsetHeight,document.body.clientHeight)}getDialogController(e){const t=Array.from(this.dialogs.keys()).find(n=>n.id===e);if(t)return this.dialogs.get(t)}}function p(c){const e=new URL(location.href),t=e.searchParams.get("channelId"),n=e.searchParams.get("origin"),i=e.searchParams.get("applicationId"),a=e.searchParams.get("debug")==="true";if(!t||!n||!i)throw new Error("Missing required URL parameters");if(!(c!=null&&c.length))throw new Error("No trusted origins provided");if(!c.includes(n))throw new Error(`Origin ${n} is not trusted`);return{channelId:t,targetOrigin:n,applicationId:i,debug:a}}class w extends u{constructor(t,n,i,a){super(t,n,i,a);r(this,"validate",()=>!0);r(this,"newValue",()=>null);this.channel.on("validate",async()=>({valid:await Promise.resolve(this.validate())})),this.channel.on("newValue",async s=>{await Promise.resolve(this.newValue(s.value))})}static connect(t){const{targetOrigin:n,channelId:i,applicationId:a,debug:s}=p(t.trustedOrigins),d=new w(n,i,a,s);return d.validate=t.validator||(()=>!0),d.newValue=t.newValue||(()=>null),d.connect().then(()=>d)}async getValue(){return(await this.channel.call("getValue")).value}async setValue(t){const n=await Promise.resolve(this.validate());return await this.channel.call("setValue",{value:t,valid:n})}async getValueByPath(t){return(await this.channel.call("getValueByPath",{path:t})).value}async setValueByPath(t,n){return this.channel.call("setValueByPath",{value:n,path:t})}}class y extends u{static connect({trustedOrigins:e}){const{targetOrigin:t,channelId:n,applicationId:i,debug:a}=p(e),s=new y(t,n,i,a);return s.connect().then(()=>s)}constructor(e,t,n,i){super(e,t,n,i)}close(e){return this.channel.call("complete",{value:e})}}class I extends u{static connect({trustedOrigins:e}){const{targetOrigin:t,channelId:n,applicationId:i,debug:a}=p(e),s=new I(t,n,i,a);return s.connect().then(()=>s)}constructor(e,t,n,i){super(e,t,n,i)}getUrlData(){return this.channel.call("getUrlData")}setBreadcrumbs(e){return this.channel.call("setBreadcrumbs",{breadcrumbs:e})}}l.ContelloCustomProperty=w,l.ContelloDialog=y,l.ContelloDialogRef=f,l.ContelloExtension=I,l.ExtensionChannel=m,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "require": "./dist/index.umd.cjs"
14
14
  }
15
15
  },
16
- "license": "UNLICENSED",
16
+ "license": "MIT",
17
17
  "scripts": {
18
18
  "start": "vite build --watch",
19
19
  "build": "vite build",
@@ -34,5 +34,5 @@
34
34
  "access": "public",
35
35
  "registry": "https://registry.npmjs.org"
36
36
  },
37
- "version": "7.8.2"
38
- }
37
+ "version": "8.0.0-next.1741104987"
38
+ }