|
| 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