vikunja-frontend/src/components/lists/ShowList.vue

536 lines
19 KiB
Vue
Raw Normal View History

2018-09-08 23:33:23 +02:00
<template>
2018-12-25 16:03:51 +01:00
<div class="loader-container" :class="{ 'is-loading': loading}">
2018-09-08 23:33:23 +02:00
<div class="content">
<router-link :to="{ name: 'editList', params: { id: list.id } }" class="icon settings is-medium">
<icon icon="cog" size="2x"/>
</router-link>
2018-09-08 23:33:23 +02:00
<h1>{{ list.title }}</h1>
2018-09-10 21:06:39 +02:00
</div>
<form @submit.prevent="addTask()">
<div class="field is-grouped">
2018-12-25 16:03:51 +01:00
<p class="control has-icons-left is-expanded" :class="{ 'is-loading': loading}">
<input v-focus class="input" :class="{ 'disabled': loading}" v-model="newTask.text" type="text" placeholder="Add a new task...">
2018-09-10 21:06:39 +02:00
<span class="icon is-small is-left">
<icon icon="tasks"/>
</span>
</p>
<p class="control">
<button type="submit" class="button is-success">
<span class="icon is-small">
<icon icon="plus"/>
</span>
Add
</button>
</p>
</div>
</form>
2018-09-10 21:06:39 +02:00
<div class="columns">
<div class="column">
2018-12-25 16:03:51 +01:00
<div class="tasks" v-if="this.list.tasks && this.list.tasks.length > 0" :class="{'short': isTaskEdit}">
<div class="task" v-for="l in list.tasks" :key="l.id">
<label :for="l.id">
2018-11-25 23:20:35 +01:00
<div class="fancycheckbox">
2018-12-25 16:03:51 +01:00
<input @change="markAsDone" type="checkbox" :id="l.id" :checked="l.done" style="display: none;">
<label :for="l.id" class="check">
2018-11-25 23:20:35 +01:00
<svg width="18px" height="18px" viewBox="0 0 18 18">
<path d="M1,9 L1,3.5 C1,2 2,1 3.5,1 L14.5,1 C16,1 17,2 17,3.5 L17,14.5 C17,16 16,17 14.5,17 L3.5,17 C2,17 1,16 1,14.5 L1,9 Z"></path>
<polyline points="1 9 7 14 15 4"></polyline>
</svg>
</label>
</div>
2018-12-25 16:03:51 +01:00
<span class="tasktext" :class="{ 'done': l.done}">
2018-11-25 23:20:35 +01:00
{{l.text}}
2018-12-26 10:13:23 +01:00
<i v-if="l.dueDate > 0" :class="{'overdue': (l.dueDate <= new Date())}"> - Due on {{new Date(l.dueDate).toLocaleString()}}</i>
2018-11-25 23:20:35 +01:00
</span>
2018-09-10 21:06:39 +02:00
</label>
<div @click="editTask(l.id)" class="icon settings">
<icon icon="cog"/>
</div>
</div>
</div>
2018-09-10 21:06:39 +02:00
</div>
<div class="column is-4" v-if="isTaskEdit">
<div class="card taskedit">
<header class="card-header">
<p class="card-header-title">
Edit Task
</p>
<a class="card-header-icon" @click="isTaskEdit = false">
<span class="icon">
<icon icon="angle-right"/>
</span>
</a>
</header>
<div class="card-content">
<div class="content">
2018-12-19 15:38:21 +01:00
<form @submit.prevent="editTaskSubmit()">
2018-09-10 21:06:39 +02:00
<div class="field">
<label class="label" for="tasktext">Task Text</label>
<div class="control">
2018-12-25 16:03:51 +01:00
<input v-focus :class="{ 'disabled': loading}" :disabled="loading" class="input" type="text" id="tasktext" placeholder="The task text is here..." v-model="taskEditTask.text">
2018-09-10 21:06:39 +02:00
</div>
</div>
<div class="field">
<label class="label" for="taskdescription">Description</label>
<div class="control">
2018-09-11 07:13:55 +02:00
<textarea :class="{ 'disabled': loading}" :disabled="loading" class="textarea" placeholder="The tasks description goes here..." id="taskdescription" v-model="taskEditTask.description"></textarea>
2018-09-10 21:06:39 +02:00
</div>
</div>
2018-11-25 22:02:22 +01:00
<b>Reminder Dates</b>
2018-12-25 16:03:51 +01:00
<div class="reminder-input" :class="{ 'overdue': (r < nowUnix && index !== (taskEditTask.reminderDates.length - 1))}" v-for="(r, index) in taskEditTask.reminderDates" :key="index">
2018-11-25 22:02:22 +01:00
<flat-pickr
:class="{ 'disabled': loading}"
:disabled="loading"
:v-model="taskEditTask.reminderDates"
:config="flatPickerConfig"
:id="'taskreminderdate' + index"
:value="r"
:data-index="index"
placeholder="Add a new reminder...">
</flat-pickr>
<a v-if="index !== (taskEditTask.reminderDates.length - 1)" @click="removeReminderByIndex(index)"><icon icon="times"></icon></a>
2018-11-25 22:02:22 +01:00
</div>
<div class="field">
<label class="label" for="taskduedate">Due Date</label>
<div class="control">
<flat-pickr
:class="{ 'disabled': loading}"
class="input"
:disabled="loading"
v-model="taskEditTask.dueDate"
:config="flatPickerConfig"
id="taskduedate"
placeholder="The tasks due date is here...">
</flat-pickr>
2018-09-10 21:06:39 +02:00
</div>
</div>
2018-12-22 21:50:32 +01:00
<div class="field">
<label class="label" for="">Duration</label>
<div class="control columns">
<div class="column">
<flat-pickr
:class="{ 'disabled': loading}"
class="input"
:disabled="loading"
v-model="taskEditTask.startDate"
:config="flatPickerConfig"
id="taskduedate"
placeholder="Start date">
</flat-pickr>
</div>
<div class="column">
<flat-pickr
:class="{ 'disabled': loading}"
class="input"
:disabled="loading"
v-model="taskEditTask.endDate"
:config="flatPickerConfig"
id="taskduedate"
2018-12-25 16:03:51 +01:00
placeholder="End date">
2018-12-22 21:50:32 +01:00
</flat-pickr>
</div>
</div>
</div>
2018-11-26 22:31:05 +01:00
<div class="field">
<label class="label" for="">Repeat after</label>
<div class="control repeat-after-input columns">
<div class="column">
<input class="input" placeholder="Specify an amount..." v-model="repeatAfter.amount"/>
</div>
2018-12-25 16:03:51 +01:00
<div class="column is-3">
2018-11-26 22:31:05 +01:00
<div class="select">
<select v-model="repeatAfter.type">
<option value="hours">Hours</option>
<option value="days">Days</option>
<option value="weeks">Weeks</option>
<option value="months">Months</option>
<option value="years">Years</option>
</select>
</div>
</div>
</div>
</div>
2018-12-19 15:38:21 +01:00
<div class="field">
<label class="label" for="subtasks">Subtasks</label>
2018-12-25 16:03:51 +01:00
<div class="tasks noborder" v-if="taskEditTask.subtasks && taskEditTask.subtasks.length > 0">
<div class="task" v-for="s in taskEditTask.subtasks" :key="s.id">
<label :for="s.id">
<div class="fancycheckbox">
<input @change="markAsDone" type="checkbox" :id="s.id" :checked="s.done" style="display: none;">
<label :for="s.id" class="check">
<svg width="18px" height="18px" viewBox="0 0 18 18">
<path d="M1,9 L1,3.5 C1,2 2,1 3.5,1 L14.5,1 C16,1 17,2 17,3.5 L17,14.5 C17,16 16,17 14.5,17 L3.5,17 C2,17 1,16 1,14.5 L1,9 Z"></path>
<polyline points="1 9 7 14 15 4"></polyline>
</svg>
</label>
</div>
<span class="tasktext" :class="{ 'done': s.done}">
{{s.text}}
</span>
</label>
2018-12-19 15:38:21 +01:00
</div>
2018-12-25 16:03:51 +01:00
</div>
</div>
<div class="field has-addons">
<div class="control is-expanded">
<input @keyup.enter="addSubtask()" :class="{ 'disabled': loading}" :disabled="loading" class="input" type="text" id="tasktext" placeholder="New subtask" v-model="newTask.text"/>
</div>
<div class="control">
2018-12-19 15:38:21 +01:00
<a class="button" @click="addSubtask()"><icon icon="plus"></icon></a>
</div>
</div>
2018-09-11 07:13:55 +02:00
<button type="submit" class="button is-success is-fullwidth" :class="{ 'is-loading': loading}">
2018-09-10 21:06:39 +02:00
Save
</button>
</form>
</div>
</div>
</div>
</div>
2018-09-08 23:33:23 +02:00
</div>
</div>
</template>
<script>
import auth from '../../auth'
import router from '../../router'
import {HTTP} from '../../http-common'
import message from '../../message'
import flatPickr from 'vue-flatpickr-component';
import 'flatpickr/dist/flatpickr.css';
2018-09-08 23:33:23 +02:00
export default {
data() {
return {
listID: this.$route.params.id,
list: {},
2018-12-19 15:38:21 +01:00
newTask: {text: ''},
2018-09-08 23:33:23 +02:00
error: '',
2018-09-10 21:06:39 +02:00
loading: false,
isTaskEdit: false,
2018-12-19 15:38:21 +01:00
taskEditTask: {
subtasks: [],
},
2018-11-25 22:02:22 +01:00
lastReminder: 0,
nowUnix: new Date(),
2018-11-26 22:31:05 +01:00
repeatAfter: {type: 'days', amount: null},
flatPickerConfig:{
altFormat: 'j M Y H:i',
altInput: true,
dateFormat: 'Y-m-d H:i',
enableTime: true,
2018-11-25 22:02:22 +01:00
onOpen: this.updateLastReminderDate,
onClose: this.addReminderDate,
},
2018-09-08 23:33:23 +02:00
}
},
components: {
flatPickr
},
2018-09-08 23:33:23 +02:00
beforeMount() {
// Check if the user is already logged in, if so, redirect him to the homepage
if (!auth.user.authenticated) {
router.push({name: 'home'})
}
},
created() {
2018-09-08 23:33:23 +02:00
this.loadList()
},
watch: {
// call again the method if the route changes
'$route': 'loadList'
},
methods: {
loadList() {
2018-09-10 21:06:39 +02:00
this.isTaskEdit = false
2018-11-27 11:23:50 +01:00
const cancel = message.setLoading(this)
2018-09-08 23:33:23 +02:00
HTTP.get(`lists/` + this.$route.params.id, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
.then(response => {
for (const t in response.data.tasks) {
response.data.tasks[t] = this.fixStuffComingFromAPI(response.data.tasks[t])
}
2018-09-08 23:33:23 +02:00
// This adds a new elemednt "list" to our object which contains all lists
2018-12-25 16:03:51 +01:00
response.data.tasks = this.sortTasks(response.data.tasks)
2018-09-08 23:33:23 +02:00
this.$set(this, 'list', response.data)
if (this.list.tasks === null) {
this.list.tasks = []
}
2018-11-27 11:23:50 +01:00
cancel() // cancel the timer
})
.catch(e => {
cancel()
this.handleError(e)
})
},
addTask() {
2018-11-27 11:23:50 +01:00
const cancel = message.setLoading(this)
2018-12-19 15:38:21 +01:00
HTTP.put(`lists/` + this.$route.params.id, this.newTask, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
.then(response => {
2018-12-19 15:38:21 +01:00
this.addTaskToList(response.data)
this.handleSuccess({message: 'The task was successfully created.'})
2018-11-27 11:23:50 +01:00
cancel() // cancel the timer
2018-09-08 23:33:23 +02:00
})
.catch(e => {
cancel()
this.handleError(e)
2018-09-08 23:33:23 +02:00
})
2018-12-19 15:38:21 +01:00
this.newTask = {}
2018-09-08 23:33:23 +02:00
},
2018-12-19 15:38:21 +01:00
addTaskToList(task) {
// If it's a subtask, add it to its parent, otherwise append it to the list of tasks
if (task.parentTaskID === 0) {
this.list.tasks.push(task)
} else {
for (const t in this.list.tasks) {
if (this.list.tasks[t].id === task.parentTaskID) {
this.list.tasks[t].subtasks.push(task)
break
}
}
}
// Update the current edit task if needed
if (task.ParentTask === this.taskEditTask.id) {
this.taskEditTask.subtasks.push(task)
}
2018-12-25 16:03:51 +01:00
this.list.tasks = this.sortTasks(this.list.tasks)
2018-12-19 15:38:21 +01:00
},
2018-09-10 07:37:10 +02:00
markAsDone(e) {
2018-12-25 16:03:51 +01:00
let context = this
if (e.target.checked) {
setTimeout(doTheDone, 300); // Delay it to show the animation when marking a task as done
} else {
doTheDone() // Don't delay it when un-marking it as it doesn't have an animation the other way around
}
2018-09-10 07:37:10 +02:00
2018-12-25 16:03:51 +01:00
function doTheDone() {
const cancel = message.setLoading(context)
HTTP.post(`tasks/` + e.target.id, {done: e.target.checked}, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
.then(response => {
context.updateTaskByID(parseInt(e.target.id), response.data)
context.handleSuccess({message: 'The task was successfully ' + (e.target.checked ? '' : 'un-') + 'marked as done.'})
cancel() // To not set the spinner to loading when the request is made in less than 100ms, would lead to loading infinitly.
})
.catch(e => {
cancel()
context.handleError(e)
})
}
2018-09-10 07:37:10 +02:00
},
2018-09-10 21:06:39 +02:00
editTask(id) {
2018-11-25 22:02:22 +01:00
// Find the selected task and set it to the current object
2018-09-10 21:06:39 +02:00
for (const t in this.list.tasks) {
if (this.list.tasks[t].id === id) {
this.taskEditTask = this.list.tasks[t]
break
}
}
2018-11-25 22:02:22 +01:00
if (this.taskEditTask.reminderDates === null) {
this.taskEditTask.reminderDates = []
}
this.taskEditTask.reminderDates = this.removeNullsFromArray(this.taskEditTask.reminderDates)
this.taskEditTask.reminderDates.push(null)
2018-11-26 22:31:05 +01:00
// Re-convert the the amount from seconds to be used with our form
let repeatAfterHours = (this.taskEditTask.repeatAfter / 60) / 60
// if its dividable by 24, its something with days
if (repeatAfterHours % 24 === 0) {
let repeatAfterDays = repeatAfterHours / 24
if (repeatAfterDays % 7 === 0) {
this.repeatAfter.type = 'weeks'
this.repeatAfter.amount = repeatAfterDays / 7
} else if (repeatAfterDays % 30 === 0) {
this.repeatAfter.type = 'months'
this.repeatAfter.amount = repeatAfterDays / 30
} else if (repeatAfterDays % 365 === 0) {
this.repeatAfter.type = 'years'
this.repeatAfter.amount = repeatAfterDays / 365
} else {
this.repeatAfter.type = 'days'
this.repeatAfter.amount = repeatAfterDays
}
} else {
// otherwise hours
this.repeatAfter.type = 'hours'
this.repeatAfter.amount = repeatAfterHours
}
2018-12-19 15:38:21 +01:00
if(this.taskEditTask.subtasks === null) {
this.taskEditTask.subtasks = [];
}
2018-09-10 21:06:39 +02:00
this.isTaskEdit = true
},
editTaskSubmit() {
2018-11-27 11:23:50 +01:00
const cancel = message.setLoading(this)
2018-09-10 21:06:39 +02:00
// Convert the date in a unix timestamp
2018-12-22 21:50:32 +01:00
this.taskEditTask.dueDate = (+ new Date(this.taskEditTask.dueDate)) / 1000
this.taskEditTask.startDate = (+ new Date(this.taskEditTask.startDate)) / 1000
this.taskEditTask.endDate = (+ new Date(this.taskEditTask.endDate)) / 1000
2018-11-25 22:02:22 +01:00
// remove all nulls
this.taskEditTask.reminderDates = this.removeNullsFromArray(this.taskEditTask.reminderDates)
// Make normal timestamps from js timestamps
for (const t in this.taskEditTask.reminderDates) {
this.taskEditTask.reminderDates[t] = Math.round(this.taskEditTask.reminderDates[t] / 1000)
}
2018-09-10 21:06:39 +02:00
2018-11-26 22:31:05 +01:00
// Make the repeating amount to seconds
let repeatAfterSeconds = 0
if (this.repeatAfter.amount !== null || this.repeatAfter.amount !== 0) {
switch (this.repeatAfter.type) {
case 'hours':
repeatAfterSeconds = this.repeatAfter.amount * 60 * 60
break;
case 'days':
repeatAfterSeconds = this.repeatAfter.amount * 60 * 60 * 24
break;
case 'weeks':
repeatAfterSeconds = this.repeatAfter.amount * 60 * 60 * 24 * 7
break;
case 'months':
repeatAfterSeconds = this.repeatAfter.amount * 60 * 60 * 24 * 30
break;
case 'years':
repeatAfterSeconds = this.repeatAfter.amount * 60 * 60 * 24 * 365
break;
}
}
this.taskEditTask.repeatAfter = repeatAfterSeconds
2018-09-10 21:06:39 +02:00
HTTP.post(`tasks/` + this.taskEditTask.id, this.taskEditTask, {headers: {'Authorization': 'Bearer ' + localStorage.getItem('token')}})
.then(response => {
2018-12-25 16:03:51 +01:00
// Update the task in the list
2018-09-10 21:06:39 +02:00
this.updateTaskByID(this.taskEditTask.id, response.data)
2018-11-25 22:02:22 +01:00
// Also update the current taskedit object so the ui changes
2018-12-25 16:03:51 +01:00
response.data.reminderDates.push(null) // To be able to add a new one
this.$set(this, 'taskEditTask', response.data)
2018-09-10 21:06:39 +02:00
this.handleSuccess({message: 'The task was successfully updated.'})
2018-11-27 11:23:50 +01:00
cancel() // cancel the timers
2018-09-10 21:06:39 +02:00
})
.catch(e => {
cancel()
this.handleError(e)
2018-09-10 21:06:39 +02:00
})
},
2018-12-19 15:38:21 +01:00
addSubtask() {
this.newTask.parentTaskID = this.taskEditTask.id
this.addTask()
},
2018-09-10 21:06:39 +02:00
updateTaskByID(id, updatedTask) {
for (const t in this.list.tasks) {
if (this.list.tasks[t].id === id) {
this.$set(this.list.tasks, t, this.fixStuffComingFromAPI(updatedTask))
2018-09-10 21:06:39 +02:00
break
}
2018-12-19 15:38:21 +01:00
if (this.list.tasks[t].id === updatedTask.parentTaskID) {
for (const s in this.list.tasks[t].subtasks) {
if (this.list.tasks[t].subtasks[s].id === updatedTask.id) {
this.$set(this.list.tasks[t].subtasks, s, updatedTask)
break
}
}
}
2018-09-10 21:06:39 +02:00
}
2018-12-25 16:03:51 +01:00
this.list.tasks = this.sortTasks(this.list.tasks)
2018-09-10 21:06:39 +02:00
},
fixStuffComingFromAPI(task) {
// Make date objects from timestamps
2018-12-22 21:50:32 +01:00
task.dueDate = this.parseDateIfNessecary(task.dueDate)
task.startDate = this.parseDateIfNessecary(task.startDate)
task.endDate = this.parseDateIfNessecary(task.endDate)
for (const rd in task.reminderDates) {
2018-12-22 21:50:32 +01:00
task.reminderDates[rd] = this.parseDateIfNessecary(task.reminderDates[rd])
}
// Make subtasks into empty array if null
if (task.subtasks === null) {
task.subtasks = []
}
2018-12-25 16:03:51 +01:00
return task
},
2018-12-25 16:03:51 +01:00
sortTasks(tasks) {
if (tasks === null) {
return tasks
}
return tasks.sort(function(a,b) {
if (a.done < b.done)
return -1
if (a.done > b.done)
return 1
return 0
})
},
2018-12-22 21:50:32 +01:00
parseDateIfNessecary(dateUnix) {
let dateobj = (+new Date(dateUnix * 1000))
if (dateobj === 0 || dateUnix === 0) {
dateUnix = null
} else {
dateUnix = dateobj
}
return dateUnix
},
2018-11-25 22:02:22 +01:00
updateLastReminderDate(selectedDates) {
this.lastReminder = +new Date(selectedDates[0])
},
addReminderDate(selectedDates, dateStr, instance) {
let newDate = +new Date(selectedDates[0])
// Don't update if nothing changed
if (newDate === this.lastReminder) {
return
}
let index = parseInt(instance.input.dataset.index)
this.taskEditTask.reminderDates[index] = newDate
let lastIndex = this.taskEditTask.reminderDates.length - 1
// put a new null at the end if we changed something
if (lastIndex === index && !isNaN(newDate)) {
this.taskEditTask.reminderDates.push(null)
}
},
removeReminderByIndex(index) {
this.taskEditTask.reminderDates.splice(index, 1)
// Reset the last to 0 to have the "add reminder" button
this.taskEditTask.reminderDates[this.taskEditTask.reminderDates.length - 1] = null
},
removeNullsFromArray(array) {
for (const index in array) {
if (array[index] === null) {
array.splice(index, 1)
}
}
return array
},
2018-12-26 10:13:23 +01:00
formatUnixDate(dateUnix) {
return (new Date(dateUnix)).toLocaleString()
},
2018-09-08 23:33:23 +02:00
handleError(e) {
message.error(e, this)
},
handleSuccess(e) {
message.success(e, this)
2018-09-08 23:33:23 +02:00
}
}
}
2018-11-06 15:54:25 +01:00
</script>