forked from contentful/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
149 lines (120 loc) · 4.24 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Optimizely audiences</title>
<link rel="stylesheet" href="https://contentful.github.io/ui-extensions-sdk/cf-extension.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css">
<style>
body {
margin: 0;
background: none;
height: 130px;
}
select {
width: 100%;
}
.optimizely-logo {
height: 3em;
background: url(https://a.mtstatic.com/@public/production/site_6806/1461019488-logo.svg) no-repeat;
background-position: top -1em left -0px;
background-size: auto 4em;
}
.select2-choices {
min-height: 150px;
max-height: 150px;
overflow-y: auto;
}
.cf-field-error,
.cf-form-field {
display: none;
}
</style>
<script src="https://unpkg.com/contentful-ui-extensions-sdk@3"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
</head>
<body>
<p class="loading">Loading ...</p>
<p class="cf-field-error"></p>
<div class="cf-form-field">
<div class="select-container"></div>
<div class="cf-form-hint">
Please select the target audience for this content.
</div>
<div class="optimizely-logo"></div>
</div>
<script>
window.contentfulExtension.init(function (api) {
api.window.startAutoResizer();
var detachValueChangeHandler;
window.addEventListener('onbeforeunload', unloadHandler);
$.ajax({
type: 'GET',
beforeSend: function (request) {
// Read-only api token for Optimizely test account.
request.setRequestHeader('Authorization', 'Bearer ' + api.parameters.instance.token);
},
url: 'https://api.optimizely.com/v2/audiences?project_id=' + api.parameters.instance.projectId,
success: function (response) {
// Filter archived audiences.
var audiences = response.filter(function (item) {
return !item.archived;
})
// Hide loader.
document.querySelector('.loading').style.display = 'none';
// Show form.
document.querySelector('.cf-form-field').style.display = 'block';
createDropDown(audiences);
},
error: function (response) {
showError(response.status + ' ' + response.statusText + ' - ' + response.responseJSON.message);
}
});
function showError(msg) {
document.querySelector('.loading').style.display = 'none';
var errorEl = document.querySelector('.cf-field-error');
errorEl.style.display = 'block';
errorEl.appendChild(document.createTextNode(msg));
document.querySelector('.cf-form-field').style.display = 'none';
}
function createDropDown(data) {
// Map structure of data so it can seed the dropdown.
var dropdownData = data.map(function (data) {
return { id: data.id, text: data.name };
});
var selectEl = document.createElement('select');
selectEl.id = 'audiences-select';
selectEl.setAttribute('multiple', 'multiple');
document.querySelector('.select-container').append(selectEl);
var $select2 = $('#audiences-select').select2({
placeholder: 'Select audiences',
data: dropdownData
});
$select2.on('change', function () {
api.field.setValue(getSelection());
});
detachValueChangeHandler = api.field.onValueChanged(function () {
var value = api.field.getValue();
if (value.join(',') !== getSelection().join(',')) {
$select2.val(value);
$select2.trigger('change');
}
})
function getSelection() {
return $select2.select2('data').map(function (item) {
return item.id;
});
}
}
// Event handler for window unload.
function unloadHandler() {
window.removeEventListener('onbeforeunload', unloadHandler);
if (detachValueChangeHandler) {
detachValueChangeHandler();
}
}
});
</script>
</body>
</html>