-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
96 lines (71 loc) · 2.32 KB
/
index.html
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html>
<body>
// include the Dynamic Content Extensions SDK
// note that this link will be functional when the SDK is released
<script src="https://unpkg.com/dc-extensions-sdk/dist/dc-extensions-sdk.umd.js"></script>
<p>Drag the slider to set the value.</p>
<div class="slidecontainer">
<input type="range" min="1" max="10" value="0" class="slider slider_input">
<p>Value: <span class="input_value"></span></p>
</div>
<script>
class Extension {
constructor(sdk) {
this.sdk = sdk;
this.currentValue = 0;
this.slider = document.querySelector(".slider_input");
this.inputValue = document.querySelector(".input_value");
this.setCurrentValue()
.finally(() => {
this.initializeInput();
this.sdk.frame.startAutoResizer();
})
}
async updateFieldValue(value) {
try {
await this.sdk.field.setValue(parseInt(value, 10));
} catch (err) {
// the field value is not set to the new value, write a warning on the console
console.log(err.message);
}
}
async setCurrentValue() {
try {
const savedValue = await this.sdk.field.getValue();
if (typeof savedValue !== "undefined") {
this.currentValue = parseInt(savedValue, 10);
}
} catch (err) {
console.log(err);
}
}
initializeInput() {
const { min = 0, max = 10, value = 0 } = this.sdk.params.instance;
Object.assign(this.slider, {
min,
max,
value
});
// set the slider value to the saved value if the content item has been previously saved
if (this.currentValue != 0)
this.slider.value = this.currentValue;
// get the label to show the current slider value
this.inputValue.innerHTML = this.slider.value;
this.slider.onchange = event => this.onInputChange(event);
}
onInputChange({ target: { value } }) {
this.inputValue.innerHTML = value;
this.updateFieldValue(value);
}
}
(async function () {
try {
new Extension(await dcExtensionsSdk.init())
} catch (e) {
document.body.innerHTML = 'Failed to connect'
}
})()
</script>
</body>
</html>