tethys/resources/assets/js/datasetPublish.js

302 lines
11 KiB
JavaScript
Raw Normal View History

2018-08-29 15:18:15 +00:00
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
2018-09-06 15:58:54 +00:00
// window.axios = require('axios');
// window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
// let token = document.head.querySelector('meta[name="csrf-token"]');
// if (token) {
// window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
// }
// else {
// console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
// }
2018-10-18 14:51:46 +00:00
// window._ = require('lodash');
2018-08-29 15:18:15 +00:00
// window.Vue = require('vue');
2018-09-06 15:58:54 +00:00
// Vue.prototype.$http = axios;
2018-08-29 15:18:15 +00:00
2018-10-18 14:51:46 +00:00
import Vue from 'vue';
2018-10-18 15:05:51 +00:00
import axios from 'axios';
2018-10-18 14:51:46 +00:00
2018-08-29 15:18:15 +00:00
// Vue.component('example', require('./components/Example.vue'));
2018-09-17 16:04:26 +00:00
//Vue.component('my-autocomplete', require('./components/MyAutocomplete.vue'));
import MyAutocomplete from './components/MyAutocomplete.vue';
2018-10-10 16:28:51 +00:00
import VeeValidate from 'vee-validate';
// import { Validator } from 'vee-validate';
Vue.use(VeeValidate);
2018-08-29 15:18:15 +00:00
2018-09-14 16:16:02 +00:00
const STATUS_INITIAL = 0, STATUS_SAVING = 1, STATUS_SUCCESS = 2, STATUS_FAILED = 3;
2018-08-29 15:18:15 +00:00
const app = new Vue({
el: '#app',
2018-09-17 16:04:26 +00:00
components: { MyAutocomplete },
2018-08-29 15:18:15 +00:00
data() {
return {
rows: [
//initial data
// { qty: 5, value: "Something", language: 10, type: "additional", sort_order: 0 },
// { qty: 2, value: "Something else", language: 20, type: "additional", sort_order: 0 },
],
2018-10-10 16:28:51 +00:00
serrors: [],
2018-08-29 15:18:15 +00:00
uploadedFiles: [],
uploadError: null,
currentStatus: null,
uploadFieldName: 'photos',
fileCount: 0,
2018-09-14 16:16:02 +00:00
persons: [],
2018-10-19 10:54:40 +00:00
contributors: [],
2018-08-29 15:18:15 +00:00
step: 1,
dataset: {
type: '',
2018-10-19 10:54:40 +00:00
state: '',
2018-10-11 14:49:08 +00:00
rights: null,
2018-08-31 14:47:04 +00:00
project_id: '',
2018-08-29 15:18:15 +00:00
creating_corporation: "GBA",
embargo_date: '',
belongs_to_bibliography: 0,
title_main: {
value: '',
language: ''
},
abstract_main: {
value: '',
language: ''
},
2018-10-18 14:51:46 +00:00
checkedAuthors: [],
2018-10-10 16:28:51 +00:00
checkedLicenses: [],// [],
2018-10-19 10:54:40 +00:00
files: [],
checkedContributors: [],
2018-08-29 15:18:15 +00:00
}
}
2018-10-10 16:28:51 +00:00
},
created: function () {
VeeValidate.Validator.extend('Name', {
getMessage: field => '* Enter valid ' + field + '',
validate: value => /^[a-zA-Z]*$/.test(value)
});
// add the required rule
VeeValidate.Validator.extend('oneChecked', {
getMessage: field => 'At least one ' + field + ' needs to be checked.',
validate: (value, [testProp]) => {
const options = this.dataset.checkedLicenses;
return value || options.some((option) => option[testProp]);
}
});
2018-08-29 15:18:15 +00:00
},
mounted() {
this.step = 1;
2018-10-10 16:28:51 +00:00
this.reset();
2018-08-29 15:18:15 +00:00
},
computed: {
isInitial() {
return this.currentStatus === STATUS_INITIAL;
},
isSaving() {
return this.currentStatus === STATUS_SAVING;
},
isSuccess() {
return this.currentStatus === STATUS_SUCCESS;
},
isFailed() {
return this.currentStatus === STATUS_FAILED;
}
},
methods: {
reset() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL;
this.uploadedFiles = [];
this.uploadError = null;
},
resetDropbox() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL;
this.dataset.files = [];
},
save() {
2018-09-14 16:16:02 +00:00
var _this = this;
2018-10-10 16:28:51 +00:00
this.serrors = [];
2018-08-29 15:18:15 +00:00
/*
Initialize the form data
*/
let formData = new FormData();
/*
Iteate over any file sent over appending the files
to the form data.
*/
// formData.append('files', []);
for (var i = 0; i < this.dataset.files.length; i++) {
let file = this.dataset.files[i];
formData.append('files[' + i + '][file]', file.file);
formData.append('files[' + i + '][label]', file.label);
formData.append('files[' + i + '][sorting]', i + 1);
// formData.append('files[' + i + ']', JSON.stringify(file));
}
/*
Additional POST Data
*/
formData.append('type', this.dataset.type);
formData.append('server_state', this.dataset.state);
2018-10-10 16:28:51 +00:00
formData.append('rights', Number(this.dataset.rights));
2018-08-29 15:18:15 +00:00
formData.append('creating_corporation', this.dataset.creating_corporation);
2018-08-31 14:47:04 +00:00
formData.append('project_id', this.dataset.project_id);
2018-08-29 15:18:15 +00:00
formData.append('embargo_date', this.dataset.embargo_date);
formData.append('belongs_to_bibliography', this.dataset.belongs_to_bibliography);
formData.append('title_main[value]', this.dataset.title_main.value);
formData.append('title_main[language]', this.dataset.title_main.language);
formData.append('abstract_main[value]', this.dataset.abstract_main.value);
formData.append('abstract_main[language]', this.dataset.abstract_main.language);
for (var i = 0; i < this.dataset.checkedLicenses.length; i++) {
formData.append('licenses[' + i + ']', this.dataset.checkedLicenses[i]);
}
2018-10-18 14:51:46 +00:00
for (var i = 0; i < this.dataset.checkedAuthors.length; i++) {
formData.append('authors[' + i + ']', this.dataset.checkedAuthors[i]);
}
2018-10-19 10:54:40 +00:00
for (var i = 0; i < this.dataset.checkedContributors.length; i++) {
formData.append('contributors[' + i + ']', this.dataset.checkedContributors[i]);
}
2018-10-18 14:51:46 +00:00
2018-08-29 15:18:15 +00:00
/*
Make the request to the POST /multiple-files URL
*/
axios.post('/publish/dataset/store',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then((response) => {
// success callback
console.log(response.data);
// this.loading = false;
// this.items = response.data;
//Vue.set(app.skills, 1, "test55");
2018-09-14 16:16:02 +00:00
_this.currentStatus = STATUS_SUCCESS;
2018-08-29 15:18:15 +00:00
if (response.data.redirect) {
window.location = response.data.redirect;
}
})
.catch((error) => {
// this.loading = false;
// console.log("test");
let errorObject = JSON.parse(JSON.stringify(error));
// console.log(errorObject);
if (errorObject.response.data.errors) {
var errorsArray = errorObject.response.data.errors;
for (var index in errorsArray) {
console.log(errorsArray[index]);
2018-10-10 16:28:51 +00:00
_this.serrors.push(errorsArray[index]);
2018-08-29 15:18:15 +00:00
}
}
if (errorObject.response.data.error) {
var error = errorObject.response.data.error;
2018-10-10 16:28:51 +00:00
_this.serrors.push(error.message);
2018-08-29 15:18:15 +00:00
}
2018-09-14 16:16:02 +00:00
_this.currentStatus = STATUS_FAILED;
2018-08-29 15:18:15 +00:00
});
},
// filesChange(fieldName, fileList) {
// // handle file changes
// // const formData = new FormData();
// if (!fileList.length) return;
// // append the files to FormData
// Array
// .from(Array(fileList.length).keys())
// .map(x => {
// dataset.files.append(fieldName, fileList[x], fileList[x].name);
// });
// // save it
// // this.save(formData);
// },
/*
Handles a change on the file upload
*/
filesChange(fieldName, fileList) {
// this.dataset.files = this.$refs.files.files;
let uploadedFiles = fileList;
/*
Adds the uploaded file to the files array
*/
for (var i = 0; i < uploadedFiles.length; i++) {
let fileName = uploadedFiles[i].name.replace(/\.[^/.]+$/, '');
let uploadeFile = { file: uploadedFiles[i], label: fileName, sorting: 0 };
//this.dataset.files.push(uploadedFiles[i]);
this.dataset.files.push(uploadeFile);
}
// if (this.dataset.files.length > 0)
// {
// this.currentStatus = STATUS_SAVING;
// }
},
2018-09-17 16:04:26 +00:00
onAddAuthor(person) {
2018-10-18 14:51:46 +00:00
//if person is not in person array
//if (this.persons.includes(person) == false) {
if (this.persons.filter(e => e.id === person.id).length == 0) {
this.persons.push(person);
this.dataset.checkedAuthors.push(person.id);
}
2018-09-14 16:16:02 +00:00
},
2018-10-19 10:54:40 +00:00
onAddContributor(person) {
//if person is not in person array
//if (this.persons.includes(person) == false) {
if (this.contributors.filter(e => e.id === person.id).length == 0) {
this.contributors.push(person);
this.dataset.checkedContributors.push(person.id);
}
},
2018-08-29 15:18:15 +00:00
/*
Removes a select file the user has uploaded
*/
removeFile(key) {
this.dataset.files.splice(key, 1);
},
prev() {
this.step--;
},
2018-10-10 16:28:51 +00:00
next(scope) {
// if(this.validate(scope)) {
// this.step++;
// }
this.$validator.validateAll(scope).then((result) => {
if (result) {
this.step++;
}
});
},
validate: function (scope) {
this.$validator.validateAll(scope);
if (this.errors.any()) {
console.log('The form is invalid');
return false;
}
return true;
2018-08-29 15:18:15 +00:00
},
submit() {
// alert('Submit to blah and show blah and etc.');
// save it
this.save();
}
}
});