Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14.07.04 LTS #123

Merged
merged 5 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 9 additions & 0 deletions projects/$2sxc/src/sxc-global/context-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}
30 changes: 15 additions & 15 deletions projects/$2sxc/src/sxc-global/sxc-global-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ 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;
// 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;
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 = 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);
} 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];
Expand All @@ -44,23 +47,20 @@ 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]) {
$2sxc.log.add('Cache found for: ' + cacheKey);
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] {
debugger;
const containerTag = domElement.closest('.sc-content-block');
if (!containerTag) return null;
const iid = containerTag.getAttribute('data-cb-instance');
Expand Down
6 changes: 5 additions & 1 deletion projects/$2sxc/src/sxc-global/sxc-global-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export class SxcGlobalHttp extends HasLog {
headers(id?: number, cbid?: number, ctx?: ContextIdentifier): Record<string, string> {
const cl = this.log.call('headers', `${id}, ${cbid}`);
const fHeaders: Record<string, string> = {};
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();
Expand Down
1 change: 1 addition & 0 deletions projects/$2sxc/src/sxc/web-api/sxc-web-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down