tethys/resources/js/datasetPublish.js

602 lines
24 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';
import messagesEN from './strings/messages/en.js';
2018-10-10 16:28:51 +00:00
import VeeValidate from 'vee-validate';
import Dataset from './components/Dataset';
import LocationsMap from './components/locations-map.vue';
import VueCountdown from './components/vue-countdown';
import PersonTable from './components/PersonTable.vue';
import Modal from './components/ShowModal.vue';
import datetime from 'vuejs-datetimepicker';
// import datetime from 'vuejs-datetimepicker';
2018-10-10 16:28:51 +00:00
// import { Validator } from 'vee-validate';
import VueToast from 'vue-toast-notification';
2020-09-14 11:46:27 +00:00
import 'vue-toast-notification/dist/theme-default.css';
Vue.use(VueToast);
2018-10-10 16:28:51 +00:00
import Tooltip from 'vue-directive-tooltip';
import 'vue-directive-tooltip/dist/vueDirectiveTooltip.css';
Vue.use(Tooltip);
const dict = {
custom: {
keyword_list: {
keywords_length: 'Minimum 3 keywords are required'
}
}
};
// Vue.use(VeeValidate);
Vue.use(VeeValidate, {
// validity: true
locale: 'en',
useConstraintAttrs: true,
dictionary: {
en: { messages: messagesEN }
}
});
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',
components: { MyAutocomplete, LocationsMap, VueCountdown, Modal, PersonTable, datetime },
2018-08-29 15:18:15 +00:00
data() {
return {
list: [
{ id: 1, name: "Abby", sport: "basket" },
{ id: 2, name: "Brooke", sport: "foot" },
{ id: 3, name: "Courtenay", sport: "volley" },
{ id: 4, name: "David", sport: "rugby" }
],
dragging: true,
2018-08-29 15:18:15 +00:00
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: [],
2019-01-22 17:24:18 +00:00
2018-08-29 15:18:15 +00:00
uploadedFiles: [],
uploadError: null,
currentStatus: null,
2019-01-22 17:24:18 +00:00
uploadFieldName: 'photos',
2018-08-29 15:18:15 +00:00
fileCount: 0,
editLink: null,
releaseLink: null,
deleteLink: null,
contributorTypes : [],
2019-01-22 17:24:18 +00:00
2019-01-30 16:46:12 +00:00
isModalVisible: false,
2018-08-29 15:18:15 +00:00
2019-03-19 17:04:41 +00:00
step: 0,
dataset: new Dataset(),
2019-03-20 17:40:14 +00:00
elevation: "no_elevation",
depth: "no_depth",
time: "no_time"
2018-08-29 15:18:15 +00:00
}
2019-01-22 17:24:18 +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;
2019-01-22 17:24:18 +00:00
return value || options.some((option) => option[testProp]);
2018-10-10 16:28:51 +00:00
}
});
2019-03-19 17:04:41 +00:00
// add the required rule
VeeValidate.Validator.extend('translatedLanguage', {
getMessage: field => 'The translated ' + field + ' must be in a language other than than the dataset language.',
validate: (value, [mainLanguage, type]) => {
if (type == "Translated") {
2019-03-19 17:04:41 +00:00
return value !== mainLanguage;
}
return true;
2019-03-19 17:04:41 +00:00
}
});
const isUnique = (value, [objectArray, index, attribute]) =>
new Promise(resolve => {
setTimeout(() => {
if (objectArray.some((item, i) => item[attribute] === value && index !== i)) {
return resolve({
valid: false,
data: {
message: value + ' is already taken.'
}
});
}
return resolve({
valid: true
});
}, 200);
});
VeeValidate.Validator.extend("unique", {
getMessage: (field, params, data) => field + ' ' + data.message,
validate: isUnique,
});
2018-08-29 15:18:15 +00:00
},
mounted() {
2019-01-08 13:06:21 +00:00
//this.step = 2;
this.reset();
},
beforeMount() {
this.messages = window.Laravel.messages;
this.contributorTypes = window.Laravel.contributorTypes;
2018-08-29 15:18:15 +00:00
},
computed: {
keywords_length() {
return this.dataset.subjects.length;
},
2020-01-23 16:52:26 +00:00
files_length() {
return this.dataset.files.length;
},
2018-08-29 15:18:15 +00:00
isInitial() {
return this.currentStatus === STATUS_INITIAL;
},
isSaving() {
return this.currentStatus === STATUS_SAVING;
},
isSuccess() {
return this.currentStatus === STATUS_SUCCESS;
},
isFailed() {
return this.currentStatus === STATUS_FAILED;
2019-03-20 17:40:14 +00:00
},
isElevationAbsolut() {
return this.elevation === "absolut";
},
isElevationRange() {
return this.elevation === "range";
},
isDepthAbsolut() {
return this.depth === "absolut";
},
isDepthRange() {
return this.depth === "range";
2019-03-29 17:29:20 +00:00
},
isTimeAbsolut() {
return this.time === "absolut";
},
isTimeRange() {
return this.time === "range";
2019-03-20 17:40:14 +00:00
}
2018-08-29 15:18:15 +00:00
},
methods: {
2019-01-30 16:46:12 +00:00
showModal() {
this.isModalVisible = true;
},
closeModal() {
2019-01-30 16:46:12 +00:00
this.isModalVisible = false;
},
2018-08-29 15:18:15 +00:00
reset() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL;
this.uploadedFiles = [];
this.uploadError = null;
2018-11-05 15:44:25 +00:00
this.dataset.reset();//reset methods will trigger property changed.
2019-03-19 17:04:41 +00:00
this.step = 0;
2018-11-05 15:44:25 +00:00
},
2018-11-28 16:06:00 +00:00
retry() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL;
// this.uploadedFiles = [];
// this.uploadError = null;
// this.dataset.reset();//reset methods will trigger property changed.
// this.step = 1;
},
editNewDataset() {
window.location = this.editLink;
},
releaseNewDataset() {
window.location = this.releaseLink;
},
deleteNewDataset() {
window.location = this.deleteLink;
2018-08-29 15:18:15 +00:00
},
resetDropbox() {
// reset form to initial state
this.currentStatus = STATUS_INITIAL;
this.dataset.files = [];
},
save(status) {
2018-11-05 15:44:25 +00:00
// upload data to the server
2018-09-14 16:16:02 +00:00
var _this = this;
2018-11-05 15:44:25 +00:00
this.currentStatus = STATUS_SAVING;
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
*/
2019-03-21 16:28:42 +00:00
formData.append('server_state', status);
2018-08-29 15:18:15 +00:00
formData.append('type', this.dataset.type);
2019-03-21 16:28:42 +00:00
formData.append('language', this.dataset.language);
// formData.append('server_state', this.dataset.server_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);
2019-03-29 17:29:20 +00:00
if (this.dataset.coverage.xmin !== "" && this.dataset.coverage.ymin != '' &&
this.dataset.coverage.xmax !== '' && this.dataset.coverage.ymax !== '') {
formData.append('coverage[x_min]', this.dataset.coverage.x_min);
formData.append('coverage[y_min]', this.dataset.coverage.y_min);
formData.append('coverage[x_max]', this.dataset.coverage.x_max);
formData.append('coverage[y_max]', this.dataset.coverage.y_max);
2019-03-29 17:29:20 +00:00
}
2019-03-20 17:40:14 +00:00
if (this.isElevationAbsolut) {
formData.append('coverage[elevation_absolut]', this.dataset.coverage.elevation_absolut);
}
else if (this.isElevationRange) {
formData.append('coverage[elevation_min]', this.dataset.coverage.elevation_min);
formData.append('coverage[elevation_max]', this.dataset.coverage.elevation_max);
}
2019-03-20 17:40:14 +00:00
if (this.isDepthAbsolut) {
formData.append('coverage[depth_absolut]', this.dataset.coverage.depth_absolut);
}
else if (this.isDepthRange) {
formData.append('coverage[depth_min]', this.dataset.coverage.depth_min);
formData.append('coverage[depth_max]', this.dataset.coverage.depth_max);
}
2019-03-29 17:29:20 +00:00
if (this.isTimeAbsolut) {
formData.append('coverage[time_absolut]', this.dataset.coverage.time_absolut);
}
else if (this.isTimeRange) {
formData.append('coverage[time_min]', this.dataset.coverage.time_min);
formData.append('coverage[time_max]', this.dataset.coverage.time_max);
}
2018-08-29 15:18:15 +00:00
for (var i = 0; i < this.dataset.checkedLicenses.length; i++) {
formData.append('licenses[' + i + ']', this.dataset.checkedLicenses[i]);
}
// for (var i = 0; i < this.dataset.checkedAuthors.length; i++) {
// formData.append('authors[' + i + ']', this.dataset.checkedAuthors[i]);
// }
for (var i = 0; i < this.dataset.persons.length; i++) {
let person = this.dataset.persons[i];
formData.append('authors[' + i + '][first_name]', person.first_name);
formData.append('authors[' + i + '][last_name]', person.last_name);
formData.append('authors[' + i + '][email]', person.email);
formData.append('authors[' + i + '][identifier_orcid]', person.identifier_orcid);
formData.append('authors[' + i + '][status]', person.status);
if (person.id !== undefined) {
formData.append('authors[' + i + '][id]', person.id);
}
2018-10-18 14:51:46 +00:00
}
// for (var i = 0; i < this.dataset.checkedContributors.length; i++) {
// formData.append('contributors[' + i + ']', this.dataset.checkedContributors[i]);
// }
for (var i = 0; i < this.dataset.contributors.length; i++) {
let contributor = this.dataset.contributors[i];
formData.append('contributors[' + i + '][first_name]', contributor.first_name);
formData.append('contributors[' + i + '][last_name]', contributor.last_name);
formData.append('contributors[' + i + '][email]', contributor.email);
formData.append('contributors[' + i + '][identifier_orcid]', contributor.identifier_orcid);
formData.append('contributors[' + i + '][status]', contributor.status);
formData.append('contributors[' + i + '][pivot][contributor_type]', contributor.pivot.contributor_type);
if (contributor.id !== undefined) {
formData.append('contributors[' + i + '][id]', contributor.id);
}
2018-10-19 10:54:40 +00:00
}
// for (var i = 0; i < this.dataset.checkedSubmitters.length; i++) {
// formData.append('submitters[' + i + ']', this.dataset.checkedSubmitters[i]);
// }
2018-10-29 13:24:41 +00:00
for (var i = 0; i < this.dataset.references.length; i++) {
let reference = this.dataset.references[i];
formData.append('references[' + i + '][value]', reference.value);
formData.append('references[' + i + '][label]', reference.label);
formData.append('references[' + i + '][type]', reference.type);
formData.append('references[' + i + '][relation]', reference.relation);
}
2018-10-18 14:51:46 +00:00
for (var i = 0; i < this.dataset.subjects.length; i++) {
let keyword = this.dataset.subjects[i];
formData.append('keywords[' + i + '][value]', keyword.value);
2019-03-18 13:32:29 +00:00
formData.append('keywords[' + i + '][type]', keyword.type);
formData.append('keywords[' + i + '][language]', keyword.language);
2019-03-18 13:32:29 +00:00
}
2019-01-24 15:56:39 +00:00
for (var i = 0; i < this.dataset.titles.length; i++) {
let title = this.dataset.titles[i];
formData.append('titles[' + i + '][value]', title.value);
formData.append('titles[' + i + '][language]', title.language);
formData.append('titles[' + i + '][type]', title.type);
2019-01-24 15:56:39 +00:00
}
for (var i = 0; i < this.dataset.abstracts.length; i++) {
let description = this.dataset.abstracts[i];
2019-01-24 16:33:04 +00:00
formData.append('descriptions[' + i + '][value]', description.value);
formData.append('descriptions[' + i + '][language]', description.language);
formData.append('descriptions[' + i + '][type]', description.type);
2019-01-24 16:33:04 +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',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'),
'X-Requested-With': 'XMLHttpRequest',
2018-08-29 15:18:15 +00:00
}
})
.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;
_this.editLink = response.data.edit;
_this.releaseLink = response.data.release;
_this.deleteLink = response.data.delete;
if (response.data.redirect) {
window.location = response.data.redirect;
}
2018-08-29 15:18:15 +00:00
})
.catch((error) => {
// this.loading = false;
2018-11-05 15:44:25 +00:00
this.uploadError = error.response;
console.log('FAILURE!!');
// let errorObject = JSON.parse(JSON.stringify(error));
2018-08-29 15:18:15 +00:00
// console.log(errorObject);
if (error.response && error.response.data.errors) {
var errorsArray = error.response.data.errors;
2018-08-29 15:18:15 +00:00
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
}
}
2019-07-04 16:46:03 +00:00
//fill with server error
if (error.response && error.response.data.error) {
var error = error.response.data.error;
2018-10-10 16:28:51 +00:00
_this.serrors.push(error.message);
2018-08-29 15:18:15 +00:00
}
//roundtrip to server was not possible
2019-07-04 16:46:03 +00:00
if (error.message && error.message.includes('413')) {
// console.log('The file you tried to upload is too large.')
2019-07-04 16:46:03 +00:00
var error = 'The file you tried to upload is too large.';
_this.serrors.push(error);
}
else if (error.response && error.response.status == 419) {
// session timed out | not authenticated
// _this.serrors.push(error.response.data.message);
_this.serrors.push('This page has expired due to inactivity, please refresh and try again');
window.location = '/login';
}
if (error.message && error.message) {
_this.serrors.push(error.message);
}
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
*/
2019-01-22 17:24:18 +00:00
addReference() {
let newReference = { value: '', label: '', relation: '', type: '' };
//this.dataset.files.push(uploadedFiles[i]);
this.dataset.references.push(newReference);
},
2018-10-29 13:24:41 +00:00
/*
Removes a selected reference
*/
removeReference(key) {
this.dataset.references.splice(key, 1);
},
/*
adds a new Keyword
*/
addKeyword() {
let newKeyword = { value: '', type: 'uncontrolled', language: this.dataset.language };
//this.dataset.files.push(uploadedFiles[i]);
this.dataset.subjects.push(newKeyword);
},
2019-03-18 13:32:29 +00:00
/*
Removes a selected keyword
*/
removeKeyword(key) {
this.dataset.subjects.splice(key, 1);
},
2019-01-22 17:24:18 +00:00
addTitle() {
let newTitle = { value: '', language: '', type: '' };
//this.dataset.files.push(uploadedFiles[i]);
this.dataset.titles.push(newTitle);
},
/*
2019-01-24 16:33:04 +00:00
Removes a selected title
2019-01-22 17:24:18 +00:00
*/
removeTitle(key) {
this.dataset.titles.splice(key, 1);
},
2019-01-24 16:33:04 +00:00
addDescription() {
let newTitle = { value: '', language: '', type: '' };
//this.dataset.files.push(uploadedFiles[i]);
this.dataset.abstracts.push(newTitle);
2019-01-24 16:33:04 +00:00
},
/*
Removes a selected description
*/
removeDescription(key) {
this.dataset.abstracts.splice(key, 1);
2019-01-24 16:33:04 +00:00
},
2018-08-29 15:18:15 +00:00
filesChange(fieldName, fileList) {
2018-11-05 15:44:25 +00:00
this.fileCount = fileList.length
2018-08-29 15:18:15 +00:00
// this.dataset.files = this.$refs.files.files;
let uploadedFiles = fileList;
/*
Adds the uploaded file to the files array
*/
2018-08-29 15:18:15 +00:00
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;
// }
},
addNewAuthor() {
let newAuthor = { status: 0, first_name: '', last_name: '', email: '', academic_title: '', identifier_orcid: '' };
this.dataset.persons.push(newAuthor);
},
removeAuthor(key) {
this.dataset.persons.splice(key, 1);
},
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.dataset.persons.filter(e => e.id === person.id).length > 0) {
this.$toast.error("person is already defined as author");
} else if (this.dataset.contributors.filter(e => e.id === person.id).length > 0) {
this.$toast.error("person is already defined as contributor");
} else {
//person.sort_order = this.dataset.persons.length;
this.dataset.persons.push(person);
this.dataset.checkedAuthors.push(person.id);
this.$toast.success("person has been successfully added as author");
2018-10-18 14:51:46 +00:00
}
2018-09-14 16:16:02 +00:00
},
addNewContributor() {
let newContributor = { status: 0, first_name: '', last_name: '', email: '', academic_title: '', identifier_orcid: '', pivot: { contributor_type: ''} };
this.dataset.contributors.push(newContributor);
},
2018-10-19 10:54:40 +00:00
onAddContributor(person) {
2018-10-29 13:24:41 +00:00
//if person is not in contributors array
//if (this.contributors.includes(person) == false) {
if (this.dataset.contributors.filter(e => e.id === person.id).length > 0) {
this.$toast.error("person is already defined as contributor");
} else if (this.dataset.persons.filter(e => e.id === person.id).length > 0) {
this.$toast.error("person is already defined as author");
} else {
person.pivot = { contributor_type: '' };
2018-11-05 15:44:25 +00:00
this.dataset.contributors.push(person);
2018-10-19 10:54:40 +00:00
this.dataset.checkedContributors.push(person.id);
this.$toast.success("person has been successfully added as contributor");
2018-10-19 10:54:40 +00:00
}
},
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
},
2020-01-23 16:52:26 +00:00
submit(scope) {
2018-08-29 15:18:15 +00:00
// alert('Submit to blah and show blah and etc.');
// save it
2020-01-23 16:52:26 +00:00
// this.save(status);
this.$validator.validateAll(scope).then((result) => {
if (result) {
this.save("inprogress");
}
});
},
handleTimeExpire() {
window.location = '/login';
2018-08-29 15:18:15 +00:00
}
}
});