mirror of
https://github.com/nasa/openmct.git
synced 2024-12-24 07:16:39 +00:00
Allow form file input to accept other MIME types (#6089)
* allow non json raw files upload * add e2e test * compress image
This commit is contained in:
parent
14c9dd0a32
commit
8125632728
76
e2e/helper/addInitFileInputObject.js
Normal file
76
e2e/helper/addInitFileInputObject.js
Normal file
@ -0,0 +1,76 @@
|
||||
class DomainObjectViewProvider {
|
||||
constructor(openmct) {
|
||||
this.key = 'doViewProvider';
|
||||
this.name = 'Domain Object View Provider';
|
||||
this.openmct = openmct;
|
||||
}
|
||||
|
||||
canView(domainObject) {
|
||||
return domainObject.type === 'imageFileInput'
|
||||
|| domainObject.type === 'jsonFileInput';
|
||||
}
|
||||
|
||||
view(domainObject, objectPath) {
|
||||
let content;
|
||||
|
||||
return {
|
||||
show: function (element) {
|
||||
const body = domainObject.selectFile.body;
|
||||
const type = typeof body;
|
||||
|
||||
content = document.createElement('div');
|
||||
content.id = 'file-input-type';
|
||||
content.textContent = JSON.stringify(type);
|
||||
element.appendChild(content);
|
||||
},
|
||||
destroy: function (element) {
|
||||
element.removeChild(content);
|
||||
content = undefined;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const openmct = window.openmct;
|
||||
|
||||
openmct.types.addType('jsonFileInput', {
|
||||
key: 'jsonFileInput',
|
||||
name: "JSON File Input Object",
|
||||
creatable: true,
|
||||
form: [
|
||||
{
|
||||
name: 'Upload File',
|
||||
key: 'selectFile',
|
||||
control: 'file-input',
|
||||
required: true,
|
||||
text: 'Select File...',
|
||||
type: 'application/json',
|
||||
property: [
|
||||
"selectFile"
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
openmct.types.addType('imageFileInput', {
|
||||
key: 'imageFileInput',
|
||||
name: "Image File Input Object",
|
||||
creatable: true,
|
||||
form: [
|
||||
{
|
||||
name: 'Upload File',
|
||||
key: 'selectFile',
|
||||
control: 'file-input',
|
||||
required: true,
|
||||
text: 'Select File...',
|
||||
type: 'image/*',
|
||||
property: [
|
||||
"selectFile"
|
||||
]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
openmct.objectViews.addProvider(new DomainObjectViewProvider(openmct));
|
||||
});
|
BIN
e2e/test-data/rick.jpg
Normal file
BIN
e2e/test-data/rick.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
@ -30,6 +30,8 @@ const genUuid = require('uuid').v4;
|
||||
const path = require('path');
|
||||
|
||||
const TEST_FOLDER = 'test folder';
|
||||
const jsonFilePath = 'e2e/test-data/ExampleLayouts.json';
|
||||
const imageFilePath = 'e2e/test-data/rick.jpg';
|
||||
|
||||
test.describe('Form Validation Behavior', () => {
|
||||
test('Required Field indicators appear if title is empty and can be corrected', async ({ page }) => {
|
||||
@ -68,6 +70,41 @@ test.describe('Form Validation Behavior', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Form File Input Behavior', () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
// eslint-disable-next-line no-undef
|
||||
await page.addInitScript({ path: path.join(__dirname, '../../helper', 'addInitFileInputObject.js') });
|
||||
});
|
||||
|
||||
test('Can select a JSON file type', async ({ page }) => {
|
||||
await page.goto('./', { waitUntil: 'networkidle' });
|
||||
|
||||
await page.getByRole('button', { name: ' Create ' }).click();
|
||||
await page.getByRole('menuitem', { name: 'JSON File Input Object' }).click();
|
||||
|
||||
await page.setInputFiles('#fileElem', jsonFilePath);
|
||||
|
||||
await page.getByRole('button', { name: 'Save' }).click();
|
||||
|
||||
const type = await page.locator('#file-input-type').textContent();
|
||||
await expect(type).toBe(`"string"`);
|
||||
});
|
||||
|
||||
test('Can select an image file type', async ({ page }) => {
|
||||
await page.goto('./', { waitUntil: 'networkidle' });
|
||||
|
||||
await page.getByRole('button', { name: ' Create ' }).click();
|
||||
await page.getByRole('menuitem', { name: 'Image File Input Object' }).click();
|
||||
|
||||
await page.setInputFiles('#fileElem', imageFilePath);
|
||||
|
||||
await page.getByRole('button', { name: 'Save' }).click();
|
||||
|
||||
const type = await page.locator('#file-input-type').textContent();
|
||||
await expect(type).toBe(`"object"`);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('Persistence operations @addInit', () => {
|
||||
// add non persistable root item
|
||||
test.beforeEach(async ({ page }) => {
|
||||
|
@ -30,7 +30,7 @@
|
||||
id="fileElem"
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
accept=".json"
|
||||
:accept="acceptableFileTypes"
|
||||
style="display:none"
|
||||
>
|
||||
<button
|
||||
@ -72,6 +72,13 @@ export default {
|
||||
},
|
||||
removable() {
|
||||
return (this.fileInfo || this.model.value) && this.model.removable;
|
||||
},
|
||||
acceptableFileTypes() {
|
||||
if (this.model.type) {
|
||||
return this.model.type;
|
||||
}
|
||||
|
||||
return 'application/json';
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
@ -80,7 +87,13 @@ export default {
|
||||
methods: {
|
||||
handleFiles() {
|
||||
const fileList = this.$refs.fileInput.files;
|
||||
this.readFile(fileList[0]);
|
||||
const file = fileList[0];
|
||||
|
||||
if (this.acceptableFileTypes === 'application/json') {
|
||||
this.readFile(file);
|
||||
} else {
|
||||
this.handleRawFile(file);
|
||||
}
|
||||
},
|
||||
readFile(file) {
|
||||
const self = this;
|
||||
@ -104,6 +117,21 @@ export default {
|
||||
|
||||
fileReader.readAsText(file);
|
||||
},
|
||||
handleRawFile(file) {
|
||||
const fileInfo = {
|
||||
name: file.name,
|
||||
body: file
|
||||
};
|
||||
|
||||
this.fileInfo = Object.assign({}, fileInfo);
|
||||
|
||||
const data = {
|
||||
model: this.model,
|
||||
value: fileInfo
|
||||
};
|
||||
|
||||
this.$emit('onChange', data);
|
||||
},
|
||||
selectFile() {
|
||||
this.$refs.fileInput.click();
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user