Last active
May 8, 2017 11:50
-
-
Save jmvtrinidad/fa022c4210079c4f8e7a5fbddb32efed to your computer and use it in GitHub Desktop.
Aurelia Image Uploader Gist with delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<require from="./image-uploader"></require> | |
Hello world! | |
<image-uploader></image-uploader> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class App { | |
message = 'sample' | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<!-- markup for preview functionality, irrelevant here --> | |
<input type="file" class="sr-only" id="${id}" aria-describedby="${id}-help" | |
multiple accept="image/*" change.delegate="imageGroupChanged($event)" /> | |
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(event) { | |
console.log('imageGroup'); | |
let fileList = event.target.files; | |
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. | |
} | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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