-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathindex.html
258 lines (235 loc) · 9.13 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hygraph Bynder UI Extension</title>
<link rel="stylesheet" type="text/css" href="index.css" />
<link
rel="stylesheet"
type="text/css"
href="https://fonts.googleapis.com/css?family=Inter:500"
/>
<script src="https://cdn.jsdelivr.net/npm/@graphcms/[email protected]/lib/zoid.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@graphcms/[email protected]/dist/gcuix.umd.production.min.js"></script>
<script>
((props) => {
const declaration = {
config: {
BR_DEFAULT_DOMAIN: {
type: 'string',
displayName: 'Default domain',
description: 'Your Bynder portal url.',
defaultValue: '',
required: true,
},
BR_DEFAULT_DOMAIN_EDITABLE: {
displayName: 'Default domain editable',
description: 'Should users be able to change the default domain?',
type: 'boolean',
required: true,
defaultValue: false,
},
BR_ASSET_TYPES: {
displayName: 'Available asset types',
description:
'Determines which asset types your users will be able to select.',
type: 'string',
required: true,
defaultValue: '["image", "audio", "video", "document"]',
},
},
extensionType: 'field',
fieldType: 'JSON',
name: 'Bynder',
description: 'Select assets from Bynder',
features: ['FieldRenderer', 'ListRenderer', 'TableRenderer'],
};
function deserialize(value) {
try {
return (value && JSON.parse(value)) || [];
} catch (error) {
console.log(error);
console.log('Invalid JSON: ' + value);
return null;
}
}
function serialize(value) {
try {
return value.length ? JSON.stringify(value) : '';
} catch (error) {
console.log(error);
return '';
}
}
async function init() {
try {
window.GCUIX.init({
declaration,
onProps: function (newProps) {
props = newProps;
updatePreview();
},
}).then((initialState) => {
props = initialState.props;
if (initialState.status === 'ok') {
if (props.isTableCell) {
return;
}
initDialogButton();
}
});
} catch (error) {
console.error('Failed to init extension:', error.message);
console.error('- error code:', error.code);
}
}
function getAssets() {
return props && props.value && props.value.length ? props.value : [];
}
function initDialogButton() {
const buttonElement = document.getElementById('open-dialog-button');
buttonElement.style.display = 'block';
//Convert BR_DEFAULT_DOMAIN_EDITABLE to boolean if it is a string
let defaultDomainEditableBool = false;
if (
typeof props.extension.config.BR_DEFAULT_DOMAIN_EDITABLE ===
'string'
)
defaultDomainEditableBool =
props.extension.config.BR_DEFAULT_DOMAIN_EDITABLE === 'true';
else
defaultDomainEditableBool =
props.extension.config.BR_DEFAULT_DOMAIN_EDITABLE;
const options = {
disableOverlayClick: false,
maxWidth: '90vw',
ariaLabel: 'Bynder',
defaultDomain: props.extension.config.BR_DEFAULT_DOMAIN,
defaultDomainEditable: defaultDomainEditableBool,
allowedAssetTypes: props.extension.config.BR_ASSET_TYPES,
isList: props.field.isList,
};
buttonElement.addEventListener('click', () => showDialog(options));
}
function updateDialogButton(nrofassets) {
if (props.field.isList) return;
const buttonElement = document.getElementById('open-dialog-button');
if (nrofassets == 0) {
buttonElement.innerText = 'Select asset from Bynder';
return;
}
buttonElement.innerText = 'Replace asset from Bynder';
}
async function showDialog(options) {
try {
options.selectedAssetIds = serialize(
getAssets().map((item) => item.databaseId)
);
const response = await props.openDialog('./dialog.html', options);
if (response === undefined) return;
const assets = deserialize(response);
await props.onChange(assets);
} catch (error) {
console.error('Error opening dialog: ', error.code, error.message);
}
}
async function updatePreview() {
const assets = getAssets();
var container = document.getElementById('assets');
if (props.isTableCell) container.classList.add('assets-table');
container.innerHTML = '';
addThumbnails(container, assets);
updateDialogButton(assets.length);
}
function addThumbnails(container, assets) {
assets.map(makeThumbnail.bind(null, container)).forEach((div) => {
container.appendChild(div);
});
}
function makeThumbnail(container, asset) {
const previewdiv = document.createElement('div');
if (props.isTableCell) previewdiv.classList.add('previewdiv-table');
else previewdiv.classList.add('previewdiv');
const text = document.createElement('p');
text.classList.add('overlay-text');
text.innerText = asset.type.toLowerCase();
previewdiv.appendChild(text);
switch (asset.type.toLowerCase()) {
case 'image':
case 'audio':
case 'document':
const img = document.createElement('img');
if (props.isTableCell) img.classList.add('previewelt-table');
else img.classList.add('previewelt');
img.src = asset.files.webImage.url.length
? asset.files.webImage.url
: './images/no-preview-available.png';
img.addEventListener('click', () =>
window.open(asset.url, '_blank').focus()
);
previewdiv.appendChild(img);
break;
case 'video':
if (asset.previewUrls.length && !props.isTableCell) {
const video = document.createElement('video');
video.classList.add('previewelt');
video.controls = true;
const source = document.createElement('source');
source.type = 'video/webm';
source.src = asset.previewUrls[0];
video.appendChild(source);
video.addEventListener('click', () =>
window.open(asset.url, '_blank').focus()
);
previewdiv.appendChild(video);
break;
}
const videoimg = document.createElement('img');
if (props.isTableCell) videoimg.classList.add('previewelt-table');
else videoimg.classList.add('previewelt');
videoimg.src = asset.files.webImage.url.length
? asset.files.webImage.url
: './images/no-preview-available.png';
videoimg.addEventListener('click', () =>
window.open(asset.url, '_blank').focus()
);
previewdiv.appendChild(videoimg);
break;
default:
const defaultimg = document.createElement('img');
if (props.isTableCell)
defaultimg.classList.add('previewelt-table');
else defaultimg.classList.add('previewelt');
defaultimg.src = './images/no-preview-available.png';
defaultimg.addEventListener('click', () =>
window.open(asset.url, '_blank').focus()
);
previewdiv.appendChild(defaultimg);
}
if (!props.isTableCell) {
const button = document.createElement('button');
button.className = 'remove-asset-button';
button.addEventListener('click', async () => {
await removeAsset(asset);
});
button.innerText = 'X';
previewdiv.appendChild(button);
}
return previewdiv;
}
async function removeAsset({ databaseId }) {
const updatedAssets = getAssets().filter(
(asset) => databaseId !== asset.databaseId
);
await props.onChange(updatedAssets);
}
document.addEventListener('DOMContentLoaded', init);
})();
</script>
</head>
<body>
<div id="assets"></div>
<button id="open-dialog-button">Select assets from Bynder</button>
</body>
</html>