Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to upload templates #403

Merged
merged 7 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions lib/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'bootstrap/js/dist/modal';
import '@selectize/selectize';
import '@selectize/selectize/dist/css/selectize.bootstrap4.css';

import { exportFile, exportJsonFile } from './utils/files';
import { exportFile, exportJsonFile, readFileAsync } from './utils/files';

import template from './toolbar.html';

Expand Down Expand Up @@ -71,6 +71,27 @@ class Toolbar {
// Triggers show/hide of draft templates
$('#view-template-drafts').on('change', () => this.updateTemplateOptions());

// File -> Upload Template
const $templateInput = $('#upload-template-input');

$templateInput.on('change', () => {
const file = $templateInput[0].files[0];
const ext = file.name.split('.').pop();
if (ext !== 'json') {
const errMsg = `Please upload a template schema.json file`;
$('#upload-template-err-msg').text(errMsg);
$('#upload-template-error-modal').modal('show');
} else {
dh.invalid_cells = {};
this.loadSelectedTemplate(file);
}
// Allow consecutive uploads of the same file
$templateInput[0].value = '';

self.hideValidationResultButtons();
dh.current_selection = [null, null, null, null];
});

// File -> New
$('#new-dropdown-item, #clear-data-confirm-btn').click((e) => {
const isNotEmpty = dh.hot.countRows() - dh.hot.countEmptyRows();
Expand Down Expand Up @@ -298,16 +319,27 @@ class Toolbar {
}
}

async loadSelectedTemplate() {
const template_path = this.$selectTemplate.val();
const [schema_name, template_name] = template_path.split('/');

// Update DataHarmonizer
const schema = await this.getSchema(schema_name, template_name);
const exportFormats = await this.getExportFormats(
schema_name,
template_name
);
async loadSelectedTemplate(file = null) {
let template_name, schema, exportFormats;
if (file) {
exportFormats = {};
try {
let contentBuffer = await readFileAsync(file);
schema = JSON.parse(contentBuffer);
template_name = Object.keys(schema.classes).filter(
(e) => schema.classes[e].is_a === 'dh_interface'
)[0];
} catch (err) {
console.log(err);
return;
}
} else {
const template_path = this.$selectTemplate.val();
let schema_name;
[schema_name, template_name] = template_path.split('/');
schema = await this.getSchema(schema_name, template_name);
exportFormats = await this.getExportFormats(schema_name, template_name);
}
this.dh.useSchema(schema, exportFormats, template_name);

// Update own interface
Expand Down
4 changes: 4 additions & 0 deletions lib/toolbar.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#upload-template-input {
display: none;
}

#open-file-input {
display: none;
}
Expand Down
43 changes: 42 additions & 1 deletion lib/toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@
class="dropdown-item"
data-toggle="modal"
data-target="#select-template-modal"
>Change Template</span
>Select Template</span
>
<label
for="upload-template-input"
class="dropdown-item mb-0"
id="upload-template-dropdown-item"
>Upload Template</label
>
<input
type="file"
accept=".json"
class="form-control-file"
id="upload-template-input"
/>
<span class="dropdown-item" id="new-dropdown-item">New</span>
<label
for="open-file-input"
Expand Down Expand Up @@ -496,6 +508,35 @@
</div>
</div>
</div>
<div
class="modal"
id="upload-template-error-modal"
tabindex="-1"
role="dialog"
>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col text-danger" id="upload-template-err-msg"></div>
</div>
<div class="row mt-3">
<div class="col d-flex justify-content-end">
<button
type="button"
class="btn btn-secondary"
data-dismiss="modal"
>
Ok
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal" id="jump-to-modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
Expand Down