-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuba-file-field.html
148 lines (126 loc) · 3.82 KB
/
cuba-file-field.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!--
Polymer element for uploading files to CUBA application. Once file is uploaded file descriptor object is
bound to `fileDescriptor` property.
This component uses `vaadin-upload` but does not import it. You should import `vaadin-upload` manually.
```html
<cuba-file-field file-descriptor="{{fileDescriptor}}"></cuba-file-field>
```
The element wraps `vaadin-upload` component.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../cuba-app/cuba-app-aware-behavior.html">
<dom-module id="cuba-file-field">
<template>
<style>
:host {
display: block;
}
vaadin-upload-file {
--vaadin-upload-file-meta-complete: {
color: var(--primary-color);
text-decoration: underline;
cursor: pointer;
}
}
</style>
<vaadin-upload id="vaadinUpload"
max-files="1"
target="[[app.apiUrl]]v2/files"
headers="[[_computeHeaders(app, app.restApiToken)]]"
files="{{files}}"
on-file-remove="_handleFileRemove"
on-upload-success="_handleUploadSuccess">
<div slot="file-list">
<template is="dom-repeat" items="[[files]]" as="file">
<vaadin-upload-file file="[[file]]" on-click="_handleFileClick"></vaadin-upload-file>
</template>
</div>
</vaadin-upload>
</template>
<script>
Polymer({
is: 'cuba-file-field',
behaviors: [CubaAppAwareBehavior],
properties: {
/**
* Label to be shown on the upload button.
*/
label: {
type: String
},
/**
* File descriptor object.
*/
fileDescriptor: {
type: Object,
notify: true
},
/**
* The array of files being processed, or already uploaded. Bound to the corresponding property of
* `vaadin-upload`.
* Currently only single file is supported.
*/
files: {
type: Array,
notify: true
}
},
observers: [
'_handleFdChange(fileDescriptor)',
'_handleLabelChange(label)'
],
_computeHeaders: function() {
if (this.app.restApiToken) {
return {
Authorization: "Bearer " + this.app.restApiToken
};
}
return {};
},
_handleUploadSuccess: function(event, data) {
var resp = JSON.parse(data.xhr.responseText);
this.fileDescriptor = resp;
this.fire('cuba-upload-success', resp);
},
_handleFdChange: function() {
if (this.fileDescriptor) {
this.$.vaadinUpload.files = [{
fileDescriptorId: this.fileDescriptor.id,
name: this.fileDescriptor.name,
complete: true
}];
} else {
this.$.vaadinUpload.files = [];
}
},
_handleFileRemove: function() {
this.fileDescriptor = null;
},
_handleLabelChange: function() {
if (this.label) {
this.$.vaadinUpload.i18n.addFiles.one = this.label;
this.$.vaadinUpload.notifyPath('i18n.addFiles.one');
}
},
_handleFileClick: function(event) {
if (Polymer.dom(event).rootTarget.id !== 'name') {
return;
}
var file = event.model.file;
if (file && file.fileDescriptorId) {
var url = this.app.apiUrl + 'v2/files/' + file.fileDescriptorId;
if (this.app.restApiToken) {
url += '?access_token=' + this.app.restApiToken;
}
return window.open(url, '_blank');
}
}
});
/**
* Fired when a file has been successfully uploaded
*
* @event cuba-upload-success
* @param {Object} detail response from the server
*/
</script>
</dom-module>