Skip to content

Commit 305ad91

Browse files
Improve upload button handler code
1 parent c63e7cf commit 305ad91

7 files changed

+78
-63
lines changed

dist/angular-material-form-builder.js

Lines changed: 41 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-material-form-builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-material-form-builder.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-material-form-builder.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/directives/upload-item/upload-view.controller.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class UploadViewCtrl {
2626
}
2727
_updateMultiple() {
2828
this.isMultiple = !!this.formItem.config.multipleUpload
29-
const input = angular.element(
30-
this.Element[0].querySelector('input[type=file]')
31-
)
29+
const input = this.Element.find('input[type=file]')
3230
if (input) {
3331
this.formItem.options = []
3432
if (this.isMultiple) {
@@ -41,9 +39,7 @@ class UploadViewCtrl {
4139

4240
_updateAccept() {
4341
this.showAllowed = !!this.formItem.config.showAccept
44-
const input = angular.element(
45-
this.Element[0].querySelector('input[type=file]')
46-
)
42+
const input = this.Element.find('input[type=file]')
4743
if (input) {
4844
if (this.showAllowed) {
4945
input[0].setAttribute('accept', this.formItem.config.accept)

src/lib/directives/upload-item/upload-view.directive.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { UploadViewCtrl } from './upload-view.controller'
22
import UploadViewTemplate from './upload-view.tpl.html'
33

4+
const MB = 1024 * 1024
5+
46
class UploadView {
57
/**
68
* @ngInject
@@ -34,36 +36,39 @@ class UploadView {
3436
ctrl.init()
3537
}, 50)
3638

37-
const button = element.find('button')
39+
const button = angular.element(element[0].querySelector('.upload-button'))
3840
const input = angular.element(element[0].querySelector('input[type=file]'))
41+
const label = angular.element(element[0].querySelector('label'))
42+
43+
if (label.length) {
44+
label.css('display', 'none')
45+
}
3946

40-
const label = element.find('label')
41-
label[0].style.display = 'none'
42-
button.bind('click', function () {
43-
label[0].style.display = 'none'
44-
input[0].click()
47+
button.on('click', () => {
48+
label.css('display', 'none')
49+
typeof input.trigger === 'function'
50+
? input.trigger('click')
51+
: input[0].click()
4552
})
4653

47-
input.bind('change', function (e) {
54+
input.on('change', (e) => {
4855
scope.$apply(function () {
49-
const files = e.target.files
50-
51-
if (files.length > 0) {
52-
for (let i = 0; i < files.length; i += 1) {
53-
if (files[i].size >= ctrl.formItem.config.size * 1048576) {
54-
label[0].style.display = 'block'
55-
label[0].textContent = ctrl.formItem.config.sizeErrMessage
56-
return
57-
}
58-
59-
if (!ctrl.formItem.config.multipleUpload) ctrl.formItem.options = []
60-
61-
ctrl.formItem.options.push({
62-
name: files[i].name,
63-
size: files[i].size,
64-
type: files[i].type,
65-
})
66-
}
56+
/**
57+
* @type {File[]}
58+
*/
59+
const files = Array.from(e.target.files)
60+
// Max allowed size in MB
61+
const maxSizeMB = ctrl.formItem.config.size * MB
62+
const exceedsSize = files.some((file) => file.size >= maxSizeMB)
63+
if (exceedsSize) {
64+
label.css('display', 'block')
65+
label.text(ctrl.formItem.config.sizeErrMessage)
66+
ctrl.formItem.options = []
67+
} else {
68+
ctrl.formItem.options = files.map((file) => {
69+
const { name, size, type } = file
70+
return { name, size, type, file }
71+
})
6772
}
6873
})
6974
})

src/lib/directives/upload-item/upload-view.tpl.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div layout="row" class="option-item">
1+
<div layout="row" class="option-item upload-item">
22
<md-input-container class="md-block" style="margin: 0">
33
<input type="file" class="ng-hide" aria-label="file" />
44
<md-input-container flex class="md-block">
@@ -12,7 +12,7 @@
1212
</md-input-container>
1313
</md-input-container>
1414

15-
<md-button id="uploadButton" class="md-fab md-mini">
15+
<md-button class="md-fab md-mini upload-button">
1616
<md-icon class="material-icons">attach_file</md-icon>
1717
</md-button>
1818
</div>

0 commit comments

Comments
 (0)