From 550e5582c4bbb4983dc2647cfca5657b5d8d0314 Mon Sep 17 00:00:00 2001 From: ijungleboy Date: Wed, 6 Jul 2022 14:32:05 +0200 Subject: [PATCH 1/5] minor bug where js-context with module id reset it --- projects/$2sxc/src/sxc-global/sxc-global-get.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/$2sxc/src/sxc-global/sxc-global-get.ts b/projects/$2sxc/src/sxc-global/sxc-global-get.ts index 84faa6f7b..d31c61b7d 100644 --- a/projects/$2sxc/src/sxc-global/sxc-global-get.ts +++ b/projects/$2sxc/src/sxc-global/sxc-global-get.ts @@ -28,8 +28,8 @@ export function $2sxcGet(id: number | ContextIdentifier | HTMLElement | Sxc, cbi if (ContextIdentifier.is(id)) { id = ContextIdentifier.ensureCompleteOrThrow(id); ctxId = id; - // create a fake id, based on zone and app because this is used to identify the object in the cache - id = id.zoneId * 100000 + id.appId; + // get moduleId or create fake, based on zone and app because this is used to identify the object in the cache + id = id.moduleId ?? id.zoneId * 100000 + id.appId; } else if (id instanceof HTMLElement && id.matches(toolbarSelector) && !id.closest(sxcDivsSelector)) { // for toolbars that are not inside 2sxc modules (e.g. in skin) const contextAttribute = id.getAttribute('sxc-context'); From 8999d90f66bfaec0b4f57aec3fbb0939fb6df204 Mon Sep 17 00:00:00 2001 From: ijungleboy Date: Wed, 6 Jul 2022 16:04:03 +0200 Subject: [PATCH 2/5] bug minor: get sxc with context could result in a different, cached sxc --- .../src/sxc-global/context-identifier.ts | 9 +++++++++ .../$2sxc/src/sxc-global/sxc-global-get.ts | 19 +++++++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/projects/$2sxc/src/sxc-global/context-identifier.ts b/projects/$2sxc/src/sxc-global/context-identifier.ts index bcf0a1bc6..dc830d647 100644 --- a/projects/$2sxc/src/sxc-global/context-identifier.ts +++ b/projects/$2sxc/src/sxc-global/context-identifier.ts @@ -71,4 +71,13 @@ export class ContextIdentifier { throw msg; } + /** + * Create a string-id to identify in a cache. + * @param ctx + * @returns + */ + static toCacheKey(ctx: ContextIdentifier): string { + this.ensureCompleteOrThrow(ctx); + return `${ctx.zoneId}/${ctx.appId}/${ctx.pageId}/${ctx.moduleId}/${ctx.blockId}`; + } } diff --git a/projects/$2sxc/src/sxc-global/sxc-global-get.ts b/projects/$2sxc/src/sxc-global/sxc-global-get.ts index d31c61b7d..50081f864 100644 --- a/projects/$2sxc/src/sxc-global/sxc-global-get.ts +++ b/projects/$2sxc/src/sxc-global/sxc-global-get.ts @@ -24,17 +24,16 @@ export function $2sxcGet(id: number | ContextIdentifier | HTMLElement | Sxc, cbi if (Sxc.is(id)) return id; // check if it's a context identifier - let ctxId: ContextIdentifier = null; + let ctx: ContextIdentifier = null; if (ContextIdentifier.is(id)) { - id = ContextIdentifier.ensureCompleteOrThrow(id); - ctxId = id; + ctx = ContextIdentifier.ensureCompleteOrThrow(id); // get moduleId or create fake, based on zone and app because this is used to identify the object in the cache - id = id.moduleId ?? id.zoneId * 100000 + id.appId; + id = ctx.moduleId ?? ctx.zoneId * 100000 + ctx.appId; } else if (id instanceof HTMLElement && id.matches(toolbarSelector) && !id.closest(sxcDivsSelector)) { // for toolbars that are not inside 2sxc modules (e.g. in skin) const contextAttribute = id.getAttribute('sxc-context'); - ctxId = JSON.parse(contextAttribute); - return $2sxcGet(ctxId); + var ctxFromAttribute = JSON.parse(contextAttribute); + return $2sxcGet(ctxFromAttribute); } else if (typeof id === 'object') { // if it's a dom-element, use auto-find const idTuple = autoFind(id); @@ -44,7 +43,7 @@ export function $2sxcGet(id: number | ContextIdentifier | HTMLElement | Sxc, cbi // if content-block is unknown, use id of module, and create an ID in the cache if (!cbid) cbid = id; - const cacheKey = id + ':' + cbid; + const cacheKey = ctx != null ? ContextIdentifier.toCacheKey(ctx) : id + ':' + cbid; // either get the cached controller from previous calls, or create a new one if ($2sxc._controllers[cacheKey]) { @@ -52,12 +51,8 @@ export function $2sxcGet(id: number | ContextIdentifier | HTMLElement | Sxc, cbi return $2sxc._controllers[cacheKey]; } - // 2022-06-01 2dm disabled, believe this is for the old .data - // not found, so also init the data-cache in case it's ever needed - // if (!$2sxc._data[cacheKey]) $2sxc._data[cacheKey] = {}; - return ($2sxc._controllers[cacheKey] - = new Sxc(id, cbid, cacheKey, $2sxc, ctxId)); + = new Sxc(id, cbid, cacheKey, $2sxc, ctx)); } function autoFind(domElement: HTMLElement): [number, number] { From 0c4c5a2e3d5fe0c8596da775228c20cbf358db3f Mon Sep 17 00:00:00 2001 From: ijungleboy Date: Wed, 6 Jul 2022 16:07:10 +0200 Subject: [PATCH 3/5] ensure SXC ctx is used in http-headers --- projects/$2sxc/src/sxc-global/sxc-global-http.ts | 6 +++++- projects/$2sxc/src/sxc/web-api/sxc-web-api.ts | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/projects/$2sxc/src/sxc-global/sxc-global-http.ts b/projects/$2sxc/src/sxc-global/sxc-global-http.ts index a3d78189a..4ad016def 100644 --- a/projects/$2sxc/src/sxc-global/sxc-global-http.ts +++ b/projects/$2sxc/src/sxc-global/sxc-global-http.ts @@ -56,7 +56,11 @@ export class SxcGlobalHttp extends HasLog { headers(id?: number, cbid?: number, ctx?: ContextIdentifier): Record { const cl = this.log.call('headers', `${id}, ${cbid}`); const fHeaders: Record = {}; - const pageId = this.env.page().toString(); + const pageId = (ctx?.pageId ?? this.env.page()).toString(); + id = ctx?.moduleId ?? id; + cbid = ctx?.blockId ?? cbid; + + // TODO: THE #_ignoreHeaders is only used in edit-ui, and should be changed to somehow say use-in-URL if (!ctx?._ignoreHeaders) { if (id) fHeaders[HeaderNames.ModuleId] = id.toString(); if (cbid) fHeaders[HeaderNames.ContentBlockId] = cbid.toString(); diff --git a/projects/$2sxc/src/sxc/web-api/sxc-web-api.ts b/projects/$2sxc/src/sxc/web-api/sxc-web-api.ts index 1efec9653..5381ee609 100644 --- a/projects/$2sxc/src/sxc/web-api/sxc-web-api.ts +++ b/projects/$2sxc/src/sxc/web-api/sxc-web-api.ts @@ -120,6 +120,7 @@ export class SxcWebApi implements ZzzSxcWebApiDeprecated { const ctxParams = {} as { appId?: number; zoneId?: number; }; const ctx = this.sxc.ctx; const urlLower = url.toLocaleLowerCase(); + // TODO: THE #_ignoreHeaders is only used in edit-ui, and should be changed to somehow say use-in-URL if (ctx?._ignoreHeaders && urlLower.includes('app/auto/')) { if (ctx?.appId && !urlLower.includes('appid=')) ctxParams.appId = ctx.appId; if (ctx?.zoneId && !urlLower.includes('zoneId=')) ctxParams.zoneId = ctx.zoneId; From ae79e07f62914657266012760a22608c9d0bdc9b Mon Sep 17 00:00:00 2001 From: ijungleboy Date: Wed, 6 Jul 2022 17:30:50 +0200 Subject: [PATCH 4/5] fix issue with toolbars in page without module --- projects/$2sxc/src/sxc-global/sxc-global-get.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/projects/$2sxc/src/sxc-global/sxc-global-get.ts b/projects/$2sxc/src/sxc-global/sxc-global-get.ts index 50081f864..6864ec63d 100644 --- a/projects/$2sxc/src/sxc-global/sxc-global-get.ts +++ b/projects/$2sxc/src/sxc-global/sxc-global-get.ts @@ -31,10 +31,14 @@ export function $2sxcGet(id: number | ContextIdentifier | HTMLElement | Sxc, cbi id = ctx.moduleId ?? ctx.zoneId * 100000 + ctx.appId; } else if (id instanceof HTMLElement && id.matches(toolbarSelector) && !id.closest(sxcDivsSelector)) { // for toolbars that are not inside 2sxc modules (e.g. in skin) - const contextAttribute = id.getAttribute('sxc-context'); - var ctxFromAttribute = JSON.parse(contextAttribute); - return $2sxcGet(ctxFromAttribute); - } else if (typeof id === 'object') { + const contextAttr = 'sxc-context'; + const contextAttribute = id.getAttribute(contextAttr); + var ctxTlbAttribute = JSON.parse(contextAttribute); + if (ctxTlbAttribute == null) throw new Error(`Toolbar outside of module without ${contextAttr} attribute found.`); + return $2sxcGet(ctxTlbAttribute); + } + // HTMLElement or anything else, try to auto-find... + if (typeof id === 'object') { // if it's a dom-element, use auto-find const idTuple = autoFind(id); id = idTuple[0]; @@ -56,6 +60,7 @@ export function $2sxcGet(id: number | ContextIdentifier | HTMLElement | Sxc, cbi } function autoFind(domElement: HTMLElement): [number, number] { +debugger; const containerTag = domElement.closest('.sc-content-block'); if (!containerTag) return null; const iid = containerTag.getAttribute('data-cb-instance'); From 91014516b39e515c66c3a2aab7e71f090fb3ec57 Mon Sep 17 00:00:00 2001 From: ijungleboy Date: Wed, 6 Jul 2022 17:33:51 +0200 Subject: [PATCH 5/5] version bump 14.07.04 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3865bae88..ca4cd7fce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "2sxc-ui", - "version": "14.07.03", + "version": "14.07.04", "description": "2sxc UI - the JS UI of 2sxc", "scripts": { "release-all": "npm run js2sxc && npm run inpage && npm run snippets && npm run quickdialog && npm run turn-on",