Skip to content

Commit b81f837

Browse files
committed
jsdialog: add base class for components
Signed-off-by: Szymon Kłos <[email protected]> Change-Id: Iee33c426cdf49b0f6b942ebc159c7e4b27db35fd
1 parent 17006c6 commit b81f837

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

browser/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ COOL_JS_LST =\
428428
src/control/Control.ESignatureDialog.ts \
429429
src/control/ColorPicker.ts \
430430
src/control/jsdialog/Util.Shortcuts.ts \
431+
src/control/jsdialog/Component.Base.ts \
431432
src/control/jsdialog/Component.Toolbar.ts \
432433
src/control/jsdialog/Definitions.Menu.ts \
433434
src/control/jsdialog/Util.Dropdown.js \
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* -*- js-indent-level: 8 -*- */
2+
/*
3+
* Copyright the Collabora Online contributors.
4+
*
5+
* SPDX-License-Identifier: MPL-2.0
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
12+
/*
13+
* Component.Base - base class for JSDialog components, wrappers which encapsulate
14+
* target DOM container and it's dedicated JSBuilder, it also receives
15+
* messages from JSDialogMessageRouter
16+
*/
17+
18+
declare var JSDialog: any;
19+
20+
abstract class JSDialogComponent {
21+
protected map: any;
22+
protected name: string;
23+
protected builder?: JSBuilder;
24+
protected container?: HTMLElement;
25+
protected allowedJsonType: string;
26+
27+
constructor(map: any, name: string, allowedJsonType: string) {
28+
this.map = map;
29+
this.name = name;
30+
this.allowedJsonType = allowedJsonType;
31+
}
32+
33+
/// connects component to the JSDialogMessageRouter
34+
protected registerMessageHandlers() {
35+
this.map.on('jsdialogupdate', this.onJSUpdate, this);
36+
this.map.on('jsdialogaction', this.onJSAction, this);
37+
}
38+
39+
/// disconnects component from JSDialogMessageRouter
40+
protected unregisterMessageHandlers() {
41+
this.map.off('jsdialogupdate', this.onJSUpdate, this);
42+
this.map.off('jsdialogaction', this.onJSAction, this);
43+
}
44+
45+
/// create JSBuilder instance for this component
46+
protected abstract createBuilder(): void;
47+
48+
/// assign or create container for the component
49+
protected abstract setupContainer(parentContainer?: HTMLElement): void;
50+
51+
/// hanlde update message
52+
protected onJSUpdate(e: any) {
53+
var data = e.data;
54+
55+
if (data.jsontype !== this.allowedJsonType) return;
56+
57+
if (!this.container) return;
58+
59+
if (!this.builder) return;
60+
61+
app.console.debug(
62+
'Component ' +
63+
this.name +
64+
' handles update message: ' +
65+
JSON.stringify(data.control),
66+
);
67+
this.builder.updateWidget(this.container, data.control);
68+
}
69+
70+
/// handle action message
71+
protected onJSAction(e: any) {
72+
var data = e.data;
73+
74+
if (data.jsontype !== this.allowedJsonType) return;
75+
76+
if (!this.builder) return;
77+
78+
if (!this.container) return;
79+
80+
app.console.debug(
81+
'Component ' +
82+
this.name +
83+
' handles action message: ' +
84+
JSON.stringify(data.data),
85+
);
86+
this.builder.executeAction(this.container, data.data);
87+
}
88+
}

0 commit comments

Comments
 (0)