-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathnew.vue
469 lines (455 loc) · 17.2 KB
/
new.vue
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!--
Copyright 2017 ODK Central Developers
See the NOTICE file at the top-level directory of this distribution and at
https://github.com/getodk/central-frontend/blob/master/NOTICE.
This file is part of ODK Central. It is subject to the license terms in
the LICENSE file found in the top-level directory of this distribution and at
https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central,
including this file, may be copied, modified, propagated, or distributed
except according to the terms contained in the LICENSE file.
-->
<!-- A modal that can be used to create a new form or to upload a new form
definition for an existing form -->
<template>
<modal id="form-new" :state="state" :hideable="!awaitingResponse" backdrop
@hide="$emit('hide')">
<template #title>
{{ formDraft == null ? $t('title.create') : $t('title.update') }}
</template>
<template #body>
<div v-show="warnings != null" class="modal-warnings">
<p>{{ $t('warningsText[0]') }}</p>
<ul if="warnings != null">
<!-- eslint-disable-next-line vue/require-v-for-key -->
<li v-for="warning of warnings">{{ warning.trim() }}</li>
</ul>
<p>
{{ $t('warningsText[1]') }}
{{ formDraft == null ? $t('warningsText[2].create') : $t('warningsText[2].update') }}
</p>
<p>
<button type="button" class="btn btn-primary"
:disabled="awaitingResponse" @click="upload(true)">
{{ $t('action.uploadAnyway') }} <spinner :state="awaitingResponse"/>
</button>
</p>
</div>
<div class="modal-introduction">
<p>
{{ formDraft == null ? $t('introduction[0].create') : $t('introduction[0].update') }}
<i18n :tag="false" path="introduction[1].full">
<template #tools>
<doc-link to="form-tools/">{{ $t('introduction[1].tools') }}</doc-link>
</template>
</i18n>
</p>
<p v-if="formDraft == null">{{ $t('introduction[2]') }}</p>
</div>
<div id="form-new-drop-zone" ref="dropZone" :class="dropZoneClass">
<i18n tag="div" path="dropZone.full">
<template #chooseOne>
<input v-show="false" ref="input" type="file">
<button type="button" class="btn btn-primary"
:disabled="awaitingResponse" @click="$refs.input.click()">
<span class="icon-folder-open"></span>{{ $t('dropZone.chooseOne') }}
</button>
</template>
</i18n>
<div v-show="file != null" id="form-new-filename">
{{ file != null ? file.name : '' }}
</div>
</div>
<div class="modal-actions">
<button id="form-new-upload-button" type="button"
class="btn btn-primary" :disabled="awaitingResponse"
@click="upload(false)">
{{ $t('action.upload') }} <spinner :state="awaitingResponse"/>
</button>
<button type="button" class="btn btn-link" :disabled="awaitingResponse"
@click="$emit('hide')">
{{ $t('action.cancel') }}
</button>
</div>
</template>
</modal>
</template>
<script>
import DocLink from '../doc-link.vue';
import Form from '../../presenters/form';
import Modal from '../modal.vue';
import Spinner from '../spinner.vue';
import dropZone from '../../mixins/drop-zone';
import request from '../../mixins/request';
import { apiPaths, isProblem } from '../../util/request';
import { requestData } from '../../store/modules/request';
export default {
name: 'FormNew',
components: { DocLink, Modal, Spinner },
mixins: [dropZone(), request()],
props: {
state: {
type: Boolean,
default: false
}
},
data() {
return {
dragDepth: 0,
awaitingResponse: false,
file: null,
warnings: null
};
},
computed: {
// The component does not assume that this data will exist when the
// component is created.
...requestData(['project', { key: 'formDraft', getOption: true }]),
disabled() {
return this.awaitingResponse;
},
dropZoneClass() {
return {
'form-new-disabled': this.awaitingResponse,
'form-new-dragover': this.fileIsOverDropZone
};
},
// Returns the inferred content type of the file based on its extension. (We
// first tried using this.file.type rather than inferring the content type,
// but that didn't work in Edge.)
contentType() {
const { name } = this.file;
if (name.length >= 5 && name.slice(-5) === '.xlsx')
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
if (name.length >= 4 && name.slice(-4) === '.xls')
return 'application/vnd.ms-excel';
return 'application/xml';
}
},
watch: {
state(state) {
if (state) return;
this.file = null;
this.warnings = null;
this.$refs.input.value = '';
}
},
mounted() {
// Using a jQuery event handler rather than a Vue one in order to facilitate
// testing: it is possible to mock a jQuery event but not a Vue event.
$(this.$refs.input).on('change.form-new', (event) => {
this.afterFileSelection(event.target.files[0]);
});
},
beforeDestroy() {
$(this.$refs.input).off('.form-new');
},
methods: {
afterFileSelection(file) {
this.$alert().blank();
this.file = file;
this.warnings = null;
},
ondrop(jQueryEvent) {
this.afterFileSelection(jQueryEvent.originalEvent.dataTransfer.files[0]);
},
upload(ignoreWarnings) {
if (this.file == null) {
this.$alert().info(this.$t('alert.fileRequired'));
return;
}
const query = ignoreWarnings ? { ignoreWarnings } : null;
const headers = { 'Content-Type': this.contentType };
if (this.contentType !== 'application/xml')
headers['X-XlsForm-FormId-Fallback'] = this.file.name.replace(/\.xlsx?$/, '');
const { currentRoute } = this.$store.state.router;
this.request({
method: 'POST',
url: this.formDraft == null
? apiPaths.forms(this.project.id, query)
: apiPaths.formDraft(this.project.id, this.formDraft.xmlFormId, query),
headers,
data: this.file,
fulfillProblem: ({ code }) => code === 400.16,
problemToAlert: ({ code, details }) => {
if (code === 400.15)
return this.$t('problem.400_15', details);
if (code === 409.3 && details.table === 'forms') {
const { fields } = details;
if (fields.length === 2 && fields[0] === 'projectId' &&
fields[1] === 'xmlFormId') {
const xmlFormId = details.values[1];
return this.$t('problem.409_3', { xmlFormId });
}
}
if (code === 400.8 && details.field === 'xmlFormId') {
return this.$t('problem.400_8', {
expected: this.formDraft.xmlFormId,
actual: details.value
});
}
return null;
}
})
.then(({ data }) => {
if (isProblem(data)) {
this.$alert().blank();
this.warnings = data.details.warnings;
} else {
// project.forms may now be out-of-date. However, if the user
// navigates to the project overview, it should be updated.
this.$emit('success', new Form(data));
}
})
.catch(() => {
if (this.$store.state.router.currentRoute === currentRoute)
this.warnings = null;
});
}
}
};
</script>
<style lang="scss">
@import '../../assets/scss/variables';
$drop-zone-vpadding: 15px;
#form-new .modal-warnings ul {
overflow-wrap: break-word;
white-space: pre-wrap;
}
#form-new-drop-zone {
background-color: $color-panel-input-background;
border: 1px dashed $color-subpanel-border;
padding-bottom: $drop-zone-vpadding;
padding-top: $drop-zone-vpadding;
text-align: center;
&.form-new-dragover {
opacity: 0.65;
}
&.form-new-disabled {
cursor: not-allowed;
opacity: 0.65;
}
}
#form-new-filename {
background-color: $color-input-background;
border-top: 1px solid #ddd;
font-family: $font-family-monospace;
margin-bottom: -$drop-zone-vpadding;
margin-top: 10px;
padding: 6px 0;
}
</style>
<i18n lang="json5">
{
"en": {
// This is the title at the top of a pop-up.
"title": {
"create": "Create Form",
"update": "Upload New Form Definition"
},
"introduction": [
{
"create": "To create a Form, upload an XForms XML file or an XLSForm Excel file.",
"update": "To update the Draft, upload an XForms XML file or an XLSForm Excel file."
},
{
"full": "If you don’t already have one, there are {tools} to help you design your Form.",
"tools": "tools available"
},
"If you have media, you will be able to upload that on the next page, after the Form has been created."
],
"dropZone": {
"full": "Drop a file here, or {chooseOne} to upload.",
"chooseOne": "choose one"
},
"action": {
"upload": "Upload",
"uploadAnyway": "Upload anyway"
},
"alert": {
"fileRequired": "Please choose a file."
},
"problem": {
"400_8": "The Form definition you have uploaded does not appear to be for this Form. It has the wrong formId (expected “{expected}”, got “{actual}”).",
"400_15": "The XLSForm could not be converted: {error}",
"409_3": "A Form already exists in this Project with the Form ID of “{xmlFormId}”."
},
"warningsText": [
"This XLSForm file can be used, but it has the following possible problems (conversion warnings):",
"Please correct the problems and try again.",
{
"create": "If you are sure these problems can be ignored, click the button to create the Form anyway:",
"update": "If you are sure these problems can be ignored, click the button to update the Draft anyway:"
}
]
}
}
</i18n>
<!-- Autogenerated by destructure.js -->
<i18n>
{
"cs": {
"title": {
"create": "Vytvořit formulář",
"update": "Nahrát novou definici formuláře"
},
"introduction": [
{
"create": "Chcete-li vytvořit formulář, nahrajte soubor XForms XML nebo XLSForm Excel.",
"update": "Chcete-li aktualizovat pracovní verzi, nahrajte soubor XForms XML nebo XLSForm Excel."
},
{
"full": "Pokud ještě žádný nemáte, je tu {tools} který vám pomůže navrhnout formulář.",
"tools": "dostupný nástroj"
},
"Pokud máte médium, budete jej moci po vytvoření formuláře nahrát na další stránku."
],
"dropZone": {
"full": "Sem přetáhněte soubor nebo {chooseOne} a nahrajte.",
"chooseOne": "vyberte jeden"
},
"action": {
"upload": "Nahrát",
"uploadAnyway": "Přesto nahrát"
},
"alert": {
"fileRequired": "Vyberte soubor."
},
"problem": {
"400_8": "Zdá se, že definice formuláře, kterou jste nahráli, není pro tento formulář. Má nesprávnou formu (očekává se „{expected}“, a máte „{actual}“).",
"400_15": "XLSForm nelze převést: {error}",
"409_3": "Formulář již v tomto projektu existuje s ID formuláře „{xmlFormId}“."
},
"warningsText": [
"Tento soubor XLSForm lze použít, ale má následující možné problémy (upozornění při převodu):",
"Opravte problémy a zkuste to znovu.",
{
"create": "Pokud jste si jisti, že tyto problémy lze ignorovat, formulář přesto vytvořte kliknutím na tlačítko:",
"update": "Pokud jste si jisti, že tyto problémy lze ignorovat, klikněte na tlačítko a Koncept aktualizujte:"
}
]
},
"de": {
"title": {
"create": "Formular erstellen",
"update": "Neue Formulardefinition hochladen"
},
"introduction": [
{
"create": "Um ein Formular zu erstellen, laden Sie eine XML-Datei im Format XForms oder eine Excel-Datei im Format XLSForms hoch.",
"update": "Um einen Entwurf zu erstellen, laden Sie eine XML-Datei im Format XForms oder eine Excel-Datei im Format XLSForms hoch."
},
{
"full": "Wenn Sie noch kein Formular haben, gibt es {tools} mit denen Sie Ihr Formular entwickeln können.",
"tools": "Werkzeuge"
},
"Wenn Sie Mediendateien haben, können Sie diese auf der folgenden Seite hochladen, nachdem das Formular erstellt wurde."
],
"dropZone": {
"full": "Fügen Sie hier eine Datei mit Drag-and-Drop ein, oder {chooseOne} zum Hochladen.",
"chooseOne": "eine auswählen"
},
"action": {
"upload": "Hochladen",
"uploadAnyway": "Trotzdem hochladen"
},
"alert": {
"fileRequired": "Bitte wählen Sie eine Datei aus."
},
"problem": {
"400_8": "Die hochgeladene Formulardefinition scheint nicht für dieses Formular zu sein. Es hat die falsche formId (\"{expected}\" erwartet, \"{actual}\" erhalten).",
"400_15": "Das XLSForm konnte nicht konvertiert werden: {error}",
"409_3": "Es gibt bereits ein Formular in diesem Projekt mit der Formular ID \"{xmlFormId}\"."
},
"warningsText": [
"Die XLSForm-Datei kann verwendet werden, aber sie hat die folgenden möglichen Probleme (Warnungen bei der Umwandlung):",
"Bitte beheben Sie die Probleme und versuchen es erneut.",
{
"create": "Wenn Sie sicher sind, dass diese Probleme ignoriert werden können, klicken Sie den Button, um das Formular trotzdem zu erzeugen:",
"update": "Wenn Sie sicher sind, dass diese Probleme ignoriert werden können, klicken Sie den Button, um den Entwurf trotzdem zu aktualisieren:"
}
]
},
"es": {
"title": {
"create": "Crear formulario",
"update": "Cargar nueva definición de formulario"
},
"introduction": [
{
"create": "Para crear un formulario, cargue un archivo XML de XForms o un archivo XLSForm de Excel",
"update": "Para actualizar el borrador, cargue un archivo XML de XForms o un archivo XLSForm de Excel."
},
{
"full": "Si aún no tiene uno, hay {tools} para ayudarlo a diseñar su formulario.",
"tools": "herramientas disponibles"
},
"Si tiene medios, podrá cargarlos en la página siguiente, después de que se haya creado el formulario."
],
"dropZone": {
"full": "Suelta un archivo aquí o {chooseOne} para subir.",
"chooseOne": "elige uno"
},
"action": {
"upload": "Subir",
"uploadAnyway": "Subir de todos modos"
},
"alert": {
"fileRequired": "Por favor, elija un archivo."
},
"problem": {
"400_8": "La definición del formulario que ha subido no parece ser para este formulario. Tiene el FormId equivocado (esperado \"{expected}\", got \"{actual}\").",
"400_15": "El XLSForm no se pudo convertir: {error}",
"409_3": "Un formulario ya existe en este proyecto con el ID formulario de {xmlFormId}"
},
"warningsText": [
"Este archivo en formato XLS puede ser usado, pero tiene algunos problemas posibles ( advertencias de conversión ):",
"Por favor, corrija los problemas e intente nuevamente.",
{
"create": "Si esta seguro que estos errores pueden ser ignorados, haga clic en el botón para crear el formulario de todos modos:",
"update": "Si esta seguro que estos errores pueden ser ignorados, haga clic en el botón para actualizar el borrador de todos modos:"
}
]
},
"fr": {
"title": {
"create": "Créer un formualire",
"update": "Téléverser une nouvelle définition de formulaire"
},
"introduction": [
{
"create": "Pour créer un formulaire, téléversez un fichier XML XForms ou un fichier excel XLSForm",
"update": "Pour modifier l'ébauche, téléversez un fichier XML XForms ou un fichier Excel XLSForm"
},
{
"full": "Si vous n'en avez as déjà un, il y a des {tools} pour vous aider à concevoir votre formulaire.",
"tools": "outils disponibles"
},
"Si vous avez des médias, vous pourrez les envoyer à la page suivante, après que le formulaire aura été créé."
],
"dropZone": {
"full": "Déposez un fichier ici, ou {chooseOne} pour le téléverser.",
"chooseOne": "Choisissez un"
},
"action": {
"upload": "Téléverser",
"uploadAnyway": "Téléverser malgré tout"
},
"alert": {
"fileRequired": "Merci de choisir un fichier."
},
"problem": {
"400_8": "La définition de formulaire que vous avez envoyé ne semble pas correspondre à ce formulaire. Elle a un mauvais formid ( “{expected}” attendu, “{actual}” reçu).",
"400_15": "Le XLSForm n'a pas pu être converti : {error}",
"409_3": "Un formulaire portant le Form ID {xmlFormId} existe déjà dans ce projet."
},
"warningsText": [
"Ce fichier XLSForm peut être utilisé, mais il présente les problèmes possibles suivants (avertissements de conversion) :",
"Veuillez corriger les problèmes et réessayer.",
{
"create": "Si vous êtes sûr que ces problèmes peuvent être ignorés, cliquez sur le bouton pour créer quand même le formulaire :",
"update": "Si vous êtes sûr que ce problème peut-être ignoré, cliquez sur le bouton pour mettre à jour l'ébauche :"
}
]
}
}
</i18n>