-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinding-behavior.ts
46 lines (39 loc) · 1.59 KB
/
binding-behavior.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Loader } from "aurelia-loader";
import { getModuleId } from "./utils";
const placeholderIconDefintion = {
prefix: "none",
iconName: "placeholder",
icon: [1, 1, [], "", ""],
};
export class FontawesomeBindingBehavior {
public static inject() {
return [Loader];
}
constructor(private loader: Loader) {}
public bind(binding: any, scope: any, pro?: boolean) {
binding.originalUpdateTarget = binding.updateTarget;
binding.updateTarget = (value: IconName | [IconPrefix, IconName]) => {
// Serialize value before handling equality check to handle the case when the value is an array
const serializedValue = JSON.stringify(value);
if (serializedValue === binding.currentSerializedValue) {
// Back out, the value has not changed
return;
}
binding.currentSerializedValue = serializedValue;
// Set a placeholder until the icon defintion has loaded
binding.originalUpdateTarget(placeholderIconDefintion);
const moduleId = getModuleId(value, !!pro);
this.loader.loadModule(moduleId).then((icon) => {
// Only set the value if the behavior is still bound
if (binding.originalUpdateTarget) {
binding.originalUpdateTarget(icon.definition);
}
});
};
}
public unbind(binding: any) {
binding.updateTarget = binding.originalUpdateTarget;
delete binding.originalUpdateTarget;
delete binding.currentSerializedValue;
}
}