-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.js
399 lines (370 loc) · 15.4 KB
/
initialize.js
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*jslint browser: true*/
/*jslint devel: true*/
/*jslint es6 */
/*global Headers, btoa*/
let spreadsheet = {};
// TODO: make URL and Authentication configurable.
let jsonApiPrefix = 'http://localhost:8000/jsonapi/';
let jsonApiHeaders = new Headers();
jsonApiHeaders.append('Authorization', 'Basic ' + btoa('admin' + ':' + 'islandora'));
let currentColumnDefinition = [];
let widgetMap = {
// Only includes non-text items.
boolean_checkbox: 'checkbox',
entity_reference: 'autocomplete',
number: 'numeric',
options_buttons: 'dropdown',
options_select: 'dropdown',
typed_relation: 'dropdown', // Eventually a custom type or handler
image: 'image'
};
// Function to load dropdown boxes with Taxonomy terms.
// TODO: sort the terms after an update.
// TODO: jsonapi pagination support
function updateDropdown(dropdown, termsPrefix, ...vocabs) {
let promises = [];
vocabs.forEach(function (vocab) {
promises.push(fetch(termsPrefix + vocab)
.then( (response) => response.json() )
.then(function (jsonapiResponse) {
jsonapiResponse.data.forEach(function (term) {
// Add the term if it is NOT already in the dropdown.
if (dropdown.findIndex( (existingTerm) => existingTerm.id === term.attributes.drupal_internal__tid) === -1) {
term = {
'name': term.attributes.name,
'id': term.attributes.drupal_internal__tid
};
dropdown.push(term);
}
});
}));
});
return promises;
}
// Populate content types dropdown
function listContentTypes() {
fetch(jsonApiPrefix + 'node_type/node_type')
.then( (response) => response.json() )
.then(function (jsonapiResponse) {
select = document.getElementById('content_type_select');
jsonapiResponse.data.forEach(function (contentType) {
select.options[select.options.length] = new Option(contentType.attributes.name, contentType.attributes.drupal_internal__type);
});
});
}
// Get the dropdowns ready.
let subjectsDropdown = [];
updateDropdown(subjectsDropdown, jsonApiPrefix + 'taxonomy_term/', 'subject', 'geo_location', 'person', 'family', 'corporate_body');
let accessDropdown = [];
updateDropdown(accessDropdown, jsonApiPrefix + 'taxonomy_term/', 'islandora_access');
// Configure and initialize the spreadsheet.
function loadData(data, columns = []) {
// Reset the sheet.
spreadsheetDiv = document.getElementById('spreadsheet');
spreadsheetDiv.innerHTML = '';
jexcelConfig = {
search: true,
updateTable: function (instance, cell, col, row, val, label, cellName) {
// Odd row colours
if ( (row % 2) !== 0 ) {
cell.style.backgroundColor = '#edf3ff';
} else {
cell.style.backgroundColor = '#ffffff';
}
},
toolbar: [{
type: 'i',
content: 'undo',
onclick: function () {
spreadsheet.undo();
}
},
{
type: 'i',
content: 'redo',
onclick: function () {
spreadsheet.redo();
}
},
{
type: 'i',
content: 'save',
onclick: function () {
spreadsheet.download();
}
},
{
type: 'i',
content: 'format_align_left',
k: 'text-align',
v: 'left'
},
{
type: 'i',
content: 'format_align_center',
k: 'text-align',
v: 'center'
},
{
type: 'i',
content: 'format_align_right',
k: 'text-align',
v: 'right'
}
],
// Disallow column inserts until we can update currentColumnDefinition.
allowInsertColumn: false,
allowManualInsertColumn: false,
// All columns need to be present for update.
// We may be able to implement a column hide feature....
allowDeleteColumn: false,
minSpareRows: 1
};
// Set Source
if (Array.isArray(data)) {
jexcelConfig.data = data;
if(Array.isArray(columns) && columns.length > 1) {
currentColumnDefinition = columns;
// Column alignments, first (thumbnail) is centered, the rest are left.
let colAlignments = Array(columns.length - 1).fill('left');
colAlignments.unshift('center');
jexcelConfig.colAlignments = colAlignments;
jexcelConfig.columns = columns;
}
// Load the spreadsheet.
spreadsheet = jexcel(spreadsheetDiv, jexcelConfig);
} else if (typeof data === 'string') {
// jexcelConfig.csv = data;
loadViewsFields(data, jexcelConfig);
} else {
alert("Could not load the provided spreadsheet data: "+String(data));
return false;
}
}
// Load Views Fields
function loadViewsFields(restViewURI, jexcelConfig){
let viewFields = fetch(jsonApiPrefix + 'view/view?filter[type][condition][path]=drupal_internal__id&filter[type][condition][value]=test', {
headers: jsonApiHeaders
})
.then( (response) => response.json() )
.then( function(jsonapiResponse){
// console.log('View Fields', jsonapiResponse.data[0].attributes.display.default.fields);
console.log('View Fields', jsonapiResponse.data[0].attributes.display.default.display_options);
return jsonapiResponse.data[0].attributes.display.default.display_options.fields;
});
let baseFieldOverrides = fetch(jsonApiPrefix + 'base_field_override/base_field_override', {
headers: jsonApiHeaders
})
.then( (response) => response.json() )
.then(function (jsonapiResponse) {
let fields = {};
jsonapiResponse.data.forEach(function (field) {
fields[field.attributes.field_name] = {
displayName: field.attributes.label,
required: field.attributes.required,
defaultValue: field.attributes.default_value,
settings: field.attributes.settings,
fieldType: field.attributes.field_type
};
});
return fields;
});
let fieldSettings = fetch(jsonApiPrefix + 'field_config/field_config', {
headers: jsonApiHeaders
})
.then( (response) => response.json() )
.then(function (jsonapiResponse) {
let fields = {};
jsonapiResponse.data.forEach(function (field) {
fields[field.attributes.field_name] = {
displayName: field.attributes.label,
required: field.attributes.required,
defaultValue: field.attributes.default_value,
settings: field.attributes.settings,
type: field.attributes.field_type
};
});
return fields;
});
let data = fetch(restViewURI).then( function(response) {return response.json();} );
Promise.all([viewFields, baseFieldOverrides, fieldSettings, data]).then(function (promises) {
viewFields = promises[0];
fieldSettings = { ...promises[1], ...promises[2] };
viewData = promises[3];
console.log('View Fields', viewFields);
console.log('Field Settings', fieldSettings);
console.log('View Data', viewData);
columns = [];
dropdownPromises = [];
Object.keys(viewFields).forEach(function(field) {
// console.log('Processing field '+field);
let column = {
id: field,
type: 'text',
title: field,
width: 200,
align: 'left'
};
// The resulting fields are probably too big...
// if (formFields[field].settings.size > 1) {
// // One character is roughly 16 pixels wide at 12pt font (http://pxtoem.com/).
// column.width = formFields[field].settings.size * 16;
// }
if (field in fieldSettings) {
column.title = fieldSettings[field].displayName;
if (fieldSettings[field].type in widgetMap) {
column.type = widgetMap[fieldSettings[field].type];
if (['image','boolean'].includes(column.type)) {
column.align = 'center';
}
}
if (fieldSettings[field].type in ['text_long','string_long']) {
column.wordWrap = true;
}
// Assume dropdowns and autocomplete are multiples until we
// are able to check field storage configs.
if (['autocomplete', 'dropdown'].includes(column.type)) {
dropdownSource = [];
column.source = dropdownSource;
column.multiple = true; // @TODO: detect cardinality
// TODO: We don't yet support missing target_bundles (all bundles of type).
if (typeof fieldSettings[field].settings.handler_settings.target_bundles !== 'undefined') {
targetType = fieldSettings[field].settings.handler.replace(/^(default:)/, '');
targetBundles = Object.keys(fieldSettings[field].settings.handler_settings.target_bundles);
dropdownPromises.push(...updateDropdown(dropdownSource, jsonApiPrefix + targetType + '/', ...targetBundles));
}
}
} else if (field === 'nid') {
column.type = 'hidden';
}
columns.push(column);
});
data = [];
viewData.forEach(function (sourceRow) {
row = [];
columns.forEach(function(column){
if(typeof column.id !== 'undefined' && typeof sourceRow[column.id] !== 'undefined') {
if(['dropdown','autocomplete'].includes(column.type) ){
row.push(sourceRow[column.id].replace(',', ';').replace(' ', ''));
} else {
row.push(sourceRow[column.id]);
}
} else {
row.push('');
}
});
data.push(row);
});
console.log('Processed Columns', columns);
console.log('Processed data', data);
jexcelConfig.data = data;
jexcelConfig.columns = columns;
Promise.all(dropdownPromises).then(function(promises) {
spreadsheet = jexcel(spreadsheetDiv, jexcelConfig);
});
});
}
// Load Spreadsheet based on content type
function loadContentType() {
contentType = document.getElementById('content_type_select').value;
let formFields = fetch(jsonApiPrefix + 'entity_form_display/entity_form_display?filter[type][condition][path]=bundle&filter[type][condition][value]=' + contentType, {
headers: jsonApiHeaders
})
.then( (response) => response.json() )
.then( (jsonapiResponse) => jsonapiResponse.data[0].attributes.content );
let baseFieldOverrides = fetch(jsonApiPrefix + 'base_field_override/base_field_override?filter[type][condition][path]=bundle&filter[type][condition][value]=' + contentType, {
headers: jsonApiHeaders
})
.then( (response) => response.json() )
.then(function (jsonapiResponse) {
let fields = {};
jsonapiResponse.data.forEach(function (field) {
fields[field.attributes.field_name] = {
displayName: field.attributes.label,
required: field.attributes.required,
defaultValue: field.attributes.default_value,
settings: field.attributes.settings
};
});
return fields;
});
let fieldSettings = fetch(jsonApiPrefix + 'field_config/field_config?filter[type][condition][path]=bundle&filter[type][condition][value]=' + contentType, {
headers: jsonApiHeaders
})
.then( (response) => response.json() )
.then(function (jsonapiResponse) {
let fields = {};
jsonapiResponse.data.forEach(function (field) {
fields[field.attributes.field_name] = {
displayName: field.attributes.label,
required: field.attributes.required,
defaultValue: field.attributes.default_value,
settings: field.attributes.settings
};
});
return fields;
});
Promise.all([formFields, baseFieldOverrides, fieldSettings]).then(function (promises) {
formFields = promises[0];
fieldSettings = { ...promises[1], ...promises[2] };
console.log('Form Fields', formFields);
console.log('Field Settings', fieldSettings);
columns = [];
Object.keys(formFields).forEach(function(field) {
// Defaults
column = {
id: field,
type: 'text',
title: field,
width: 200,
weight: formFields[field].weight
};
if (formFields[field].type in widgetMap) {
column.type = widgetMap[formFields[field].type];
}
if (formFields[field].settings.rows > 1) {
column.wordWrap = true;
}
// The resulting fields are probably too big...
// if (formFields[field].settings.size > 1) {
// // One character is roughly 16 pixels wide at 12pt font (http://pxtoem.com/).
// column.width = formFields[field].settings.size * 16;
// }
if (field in fieldSettings) {
column.title = fieldSettings[field].displayName;
// Assume dropdowns and autocomplete are multiples until we
// are able to check field storage configs.
if (['autocomplete', 'dropdown'].includes(column.type)) {
dropdownSource = [];
column.source = dropdownSource;
column.multiple = true;
// TODO: We don't yet support missing target_bundles (all bundles of type).
if (typeof fieldSettings[field].settings.handler_settings.target_bundles !== 'undefined') {
targetType = fieldSettings[field].settings.handler.replace(/^(default:)/, '');
targetBundles = Object.keys(fieldSettings[field].settings.handler_settings.target_bundles);
updateDropdown(dropdownSource, jsonApiPrefix + targetType + '/', ...targetBundles);
}
}
}
columns.push(column);
});
columns.sort((a, b) => {
return a.weight - b.weight;
});
columns.unshift({
id: 'local_path_original',
type: 'text',
title: 'Original File Path',
width: 120
});
columns.unshift({
id: 'local_thumbnail',
type: 'image',
title: 'Thumbnail',
width: 120
});
data = [Array(columns.length).fill('')];
console.log('Spreadsheet columns:', columns);
loadData(data, columns);
});
}