Skip to content

Instantly share code, notes, and snippets.

@jmvtrinidad
Last active May 2, 2017 03:45
Show Gist options
  • Save jmvtrinidad/11f8af9e1d33917ef34141374acd80c6 to your computer and use it in GitHub Desktop.
Save jmvtrinidad/11f8af9e1d33917ef34141374acd80c6 to your computer and use it in GitHub Desktop.
Aurelia Image Uploader Gist with observable
<template>
<require from="./image-uploader"></require>
Hello world!
<image-uploader></image-uploader>
</template>
export class App {
message = 'sample'
}
<template>
<!-- markup for preview functionality, irrelevant here -->
<input type="file" class="sr-only" id="${id}" aria-describedby="${id}-help" multiple accept="image/*" files.bind="imageGroup" />
<label class="btn btn-default btn-sm" aria-hidden="true" for="${id}" role="button">
<i class="fa fa-plus" aria-hidden="true"></i> Add files
</label>
</template>
import { bindable, bindingMode, autoinject, observable } from 'aurelia-framework';
export class ImageUploader {
@bindable({ defaultBindingMode: bindingMode.twoWay }) imageArray = null;
@bindable id = null;
@bindable maxImageCount = 10;
@bindable maxFileSizeKiloBytes = 2048;
@observable imageGroup= [];
constructor(dialogService) {
this.dialogService = dialogService
}
idChanged(newValue) {
if (!newValue) {
throw Error('The id parameter needs a value in order to make the file uploader work.');
}
}
imageGroupChanged(fileList) {
console.log('imageGroup');
if (!fileList || !fileList.length) return;
if (!this.imageArray) return;
for (let i = 0; i < fileList.length; ++i) {
let file = fileList.item(i);
if (this.imageArray.length >= this.maxImageCount) {
// TODO: Alert: maximum number of images reached
} else {
if (file && file.type.startsWith('image/')) {
// Size is in bytes. Div by 1024 to get kilobytes.
if (file.size / (1024) > this.maxFileSizeKiloBytes) {
// TODO: Alert: Image file too large.
} else {
this.imageArray.push(file);
}
} else {
// TODO: Alert: unsupported media type.
}
}
}
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
Loading...
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(() => aurelia.setRoot());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment