|
1 | | -import { StyleService } from "@paperbits/styles"; |
2 | | -import * as ko from "knockout"; |
| 1 | +import * as ko from "knockout"; |
3 | 2 | import { BackgroundModel } from "@paperbits/common/widgets/background"; |
| 3 | +import { BackgroundBehavior, BehaviorHandle } from "@paperbits/common/behaviors/behavior.background"; |
4 | 4 |
|
5 | | -ko.bindingHandlers["style"] = { |
6 | | - update(element: HTMLElement, valueAccessor): void { |
7 | | - const value = ko.utils.unwrapObservable(valueAccessor() || {}); |
8 | | - |
9 | | - ko.utils.objectForEach(value, function (styleName, styleValue) { |
10 | | - styleValue = ko.utils.unwrapObservable(styleValue); |
11 | | - |
12 | | - if (styleValue === null || styleValue === undefined || styleValue === false) { |
13 | | - // Empty string removes the value, whereas null/undefined have no effect |
14 | | - styleValue = ""; |
15 | | - } |
16 | | - |
17 | | - element.style.setProperty(styleName, styleValue); |
18 | | - }); |
19 | | - } |
20 | | -}; |
| 5 | +// ko.bindingHandlers["style"] = { ... }; // This global style binding handler remains unchanged. |
21 | 6 |
|
22 | 7 | export class BackgroundBindingHandler { |
23 | | - constructor(styleService: StyleService) { |
| 8 | + constructor() { // StyleService removed as it was not used by this specific binding handler |
24 | 9 | ko.bindingHandlers["background"] = { |
25 | | - init(element: HTMLElement, valueAccessor: () => BackgroundModel): void { |
26 | | - const configuration = valueAccessor(); |
27 | | - const styleObservable = ko.observable(); |
28 | | - |
29 | | - const setBackground = async (backgroundModel: BackgroundModel) => { |
30 | | - if (backgroundModel.sourceUrl) { |
31 | | - styleObservable({ |
32 | | - "background-image": `url("${ko.unwrap(backgroundModel.sourceUrl)}")`, |
33 | | - "background-repeat": "no-repeat", |
34 | | - "background-size": "cover", |
35 | | - "background-position": "center", |
36 | | - "background-color": backgroundModel.color |
37 | | - }); |
| 10 | + init(element: HTMLElement, valueAccessor: () => BackgroundModel | ko.Observable<BackgroundModel>): void { |
| 11 | + const configurationObservableOrModel = valueAccessor(); |
| 12 | + let behaviorHandle: BehaviorHandle | undefined; |
| 13 | + |
| 14 | + // Helper to unwrap BackgroundModel properties if they are observable |
| 15 | + // This ensures the Behavior class receives plain data. |
| 16 | + const getCleanModel = (model?: BackgroundModel): BackgroundModel => { |
| 17 | + if (!model) { |
| 18 | + return {}; |
38 | 19 | } |
39 | | - else if (backgroundModel.color) { |
40 | | - styleObservable({ |
41 | | - "background-color": backgroundModel.color |
42 | | - }); |
| 20 | + const cleanModel: BackgroundModel = {}; |
| 21 | + if (model.sourceUrl !== undefined) { |
| 22 | + cleanModel.sourceUrl = ko.unwrap(model.sourceUrl); |
43 | 23 | } |
44 | | - else { |
45 | | - styleObservable({ |
46 | | - "background-image": null, |
47 | | - "background-repeat": null, |
48 | | - "background-size": null, |
49 | | - "background-position": null, |
50 | | - "background-color": null |
51 | | - }); |
| 24 | + if (model.color !== undefined) { |
| 25 | + cleanModel.color = ko.unwrap(model.color); |
52 | 26 | } |
| 27 | + // Add other properties from BackgroundModel if they can be observable and are used |
| 28 | + return cleanModel; |
53 | 29 | }; |
54 | 30 |
|
55 | | - ko.applyBindingsToNode(element, { style: styleObservable }, null); |
| 31 | + if (ko.isObservable(configurationObservableOrModel)) { |
| 32 | + const configurationObservable = configurationObservableOrModel as ko.Observable<BackgroundModel>; |
| 33 | + |
| 34 | + const initialModel = getCleanModel(ko.unwrap(configurationObservable)); |
| 35 | + behaviorHandle = BackgroundBehavior.attach(element, initialModel); |
56 | 36 |
|
57 | | - if (ko.isObservable(configuration)) { |
58 | | - configuration.subscribe((newConfiguration) => { |
59 | | - if (!newConfiguration) { |
60 | | - setBackground({}); |
61 | | - } |
62 | | - else { |
63 | | - setBackground(ko.unwrap(newConfiguration)); |
| 37 | + configurationObservable.subscribe((newConfiguration) => { |
| 38 | + if (behaviorHandle?.update) { |
| 39 | + behaviorHandle.update(getCleanModel(newConfiguration)); |
64 | 40 | } |
65 | 41 | }); |
| 42 | + } else { |
| 43 | + const model = getCleanModel(configurationObservableOrModel as BackgroundModel); |
| 44 | + behaviorHandle = BackgroundBehavior.attach(element, model); |
66 | 45 | } |
67 | 46 |
|
68 | | - let initialConfiguration = ko.unwrap(configuration); |
69 | | - |
70 | | - if (!initialConfiguration) { |
71 | | - initialConfiguration = {}; |
72 | | - } |
73 | | - |
74 | | - setBackground(initialConfiguration); |
| 47 | + ko.utils.domNodeDisposal.addDisposeCallback(element, () => { |
| 48 | + if (behaviorHandle?.dispose) { |
| 49 | + behaviorHandle.dispose(); |
| 50 | + } |
| 51 | + }); |
75 | 52 | } |
76 | 53 | }; |
77 | 54 | } |
|
0 commit comments