Skip to content

Commit 0817b6b

Browse files
committed
fixes #503
1 parent 4e73f7f commit 0817b6b

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/app/demo/notification/full/demo.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export class NotificationFullDemoComponent {
4747
showWithOptions() {
4848
JigsawNotification.show(this.message, {
4949
caption: this.caption, position: this.position, icon: this.icon,
50-
timeout: this.timeout * 1000, width: this.width, height: this.height
50+
timeout: this.timeout * 1000, width: this.width, height: this.height,
51+
innerHtmlContext: this
5152
});
5253
}
5354

src/jigsaw/directive/trusted-html/trusted-html.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,36 @@ type CallbackValues = { [callbackName: string]: HtmlCallback };
1010
})
1111
export class JigsawTrustedHtml implements OnInit, OnDestroy {
1212
private static _callbacks = new Map<any, CallbackValues>();
13-
private static _contexts = [];
13+
private static _contexts: {context: any, counter: number}[] = [];
1414

1515
private static _getContextMagicNumber(context: any): number {
16-
return JigsawTrustedHtml._contexts.indexOf(context);
16+
return JigsawTrustedHtml._contexts.findIndex(i => i && i.context === context);
1717
}
1818

1919
private static _getContext(magicNumber: number): any {
2020
return JigsawTrustedHtml._contexts[magicNumber];
2121
}
2222

2323
private static _registerContext(context: any): void {
24-
if (CommonUtils.isDefined(context) && JigsawTrustedHtml._getContextMagicNumber(context) == -1) {
25-
JigsawTrustedHtml._contexts.push(context);
24+
if (CommonUtils.isUndefined(context)) {
25+
return;
26+
}
27+
let info = JigsawTrustedHtml._contexts.find(i => i && i.context === context);
28+
if (CommonUtils.isUndefined(info)) {
29+
info = {context: context, counter: 1};
30+
JigsawTrustedHtml._contexts.push(info);
31+
} else {
32+
info.counter++;
2633
}
2734
}
2835

2936
private static _jigsawInternalCallbackWrapper(callbackName: string, contextMagicNumber: number, ...args) {
30-
const context = JigsawTrustedHtml._getContext(contextMagicNumber);
31-
if (CommonUtils.isUndefined(context)) {
37+
const contextInfo = JigsawTrustedHtml._getContext(contextMagicNumber);
38+
if (CommonUtils.isUndefined(contextInfo)) {
3239
console.error('no context found by magic number, callbackName = ' + callbackName);
3340
return;
3441
}
35-
const callbacks = JigsawTrustedHtml._callbacks.get(context);
42+
const callbacks = JigsawTrustedHtml._callbacks.get(contextInfo.context);
3643
if (CommonUtils.isUndefined(callbacks)) {
3744
console.error('no callback cache info found by magic number, callbackName = ' + callbackName);
3845
return;
@@ -43,7 +50,7 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
4350
console.log(`Hint: add a member method named ${callbackName} to the context object.`);
4451
return;
4552
}
46-
CommonUtils.safeInvokeCallback(context, callback, args);
53+
CommonUtils.safeInvokeCallback(contextInfo.context, callback, args);
4754
}
4855

4956
private static _declareCallback(context: any, name: string, callback: HtmlCallback) {
@@ -65,11 +72,22 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
6572
callbacks = {};
6673
JigsawTrustedHtml._callbacks.set(context, callbacks);
6774
}
68-
callbacks[name] = CommonUtils.isDefined(callback) ? callback : context;
75+
callbacks[name] = callback;
6976
}
7077

7178
private static _clearCallbacks(context: any) {
72-
JigsawTrustedHtml._callbacks.delete(context);
79+
const idx = JigsawTrustedHtml._contexts.findIndex(i => i && i.context === context);
80+
if (idx == -1) {
81+
return;
82+
}
83+
const info = JigsawTrustedHtml._contexts[idx];
84+
info.counter--;
85+
if (info.counter == 0) {
86+
JigsawTrustedHtml._callbacks.delete(context);
87+
info.context = null;
88+
info.counter = -1;
89+
JigsawTrustedHtml._contexts[idx] = null;
90+
}
7391
}
7492

7593
//====================================================
@@ -92,9 +110,6 @@ export class JigsawTrustedHtml implements OnInit, OnDestroy {
92110
if (CommonUtils.isUndefined(value)) {
93111
return;
94112
}
95-
if (CommonUtils.isDefined(this._trustedHtmlContext)) {
96-
JigsawTrustedHtml._clearCallbacks(this._trustedHtmlContext);
97-
}
98113
this._trustedHtmlContext = value;
99114
JigsawTrustedHtml._registerContext(value);
100115
this._contextMagicNumber = JigsawTrustedHtml._getContextMagicNumber(value);

0 commit comments

Comments
 (0)