2020-04-01 20:13:57 +00:00
|
|
|
import TaskCollectionService from '../../../services/taskCollection'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This mixin provides a base set of methods and properties to get tasks on a list.
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
taskCollectionService: TaskCollectionService,
|
|
|
|
tasks: [],
|
|
|
|
|
|
|
|
pages: [],
|
|
|
|
currentPage: 0,
|
|
|
|
|
|
|
|
showTaskSearch: false,
|
|
|
|
searchTerm: '',
|
2020-06-11 15:34:13 +00:00
|
|
|
|
|
|
|
showTaskFilter: false,
|
|
|
|
params: {
|
|
|
|
sort_by: ['done', 'id'],
|
|
|
|
order_by: ['asc', 'desc'],
|
|
|
|
filter_by: ['done'],
|
|
|
|
filter_value: ['false'],
|
|
|
|
filter_comparator: ['equals'],
|
|
|
|
filter_concat: 'and',
|
|
|
|
},
|
2020-04-01 20:13:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
'$route.query': 'loadTasksForPage', // Only listen for query path changes
|
|
|
|
},
|
2020-04-14 21:37:08 +00:00
|
|
|
beforeMount() {
|
|
|
|
// Triggering loading the tasks in beforeMount lets the component maintain the current page, therefore the page
|
|
|
|
// is not lost after navigating back from a task detail page for example.
|
|
|
|
this.loadTasksForPage(this.$route.query)
|
|
|
|
},
|
2020-04-01 20:13:57 +00:00
|
|
|
created() {
|
|
|
|
this.taskCollectionService = new TaskCollectionService()
|
|
|
|
},
|
|
|
|
methods: {
|
2020-06-11 15:34:13 +00:00
|
|
|
loadTasks(
|
|
|
|
page,
|
|
|
|
search = '',
|
|
|
|
params = null,
|
|
|
|
) {
|
2020-04-25 23:11:34 +00:00
|
|
|
|
|
|
|
// Because this function is triggered every time on navigation, we're putting a condition here to only load it when we actually want to show tasks
|
|
|
|
// FIXME: This is a bit hacky -> Cleanup.
|
|
|
|
if (
|
|
|
|
this.$route.name !== 'list.list' &&
|
|
|
|
this.$route.name !== 'list.table'
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-21 09:36:42 +00:00
|
|
|
this.$set(this, 'tasks', [])
|
|
|
|
|
2020-06-11 15:34:13 +00:00
|
|
|
if (params === null) {
|
|
|
|
params = this.params
|
|
|
|
}
|
|
|
|
|
2020-04-01 20:13:57 +00:00
|
|
|
if (search !== '') {
|
|
|
|
params.s = search
|
|
|
|
}
|
2020-04-25 23:11:34 +00:00
|
|
|
this.taskCollectionService.getAll({listId: this.$route.params.listId}, params, page)
|
2020-04-01 20:13:57 +00:00
|
|
|
.then(r => {
|
|
|
|
this.$set(this, 'tasks', r)
|
|
|
|
this.$set(this, 'pages', [])
|
|
|
|
this.currentPage = page
|
|
|
|
|
2020-06-11 15:34:13 +00:00
|
|
|
for (let i = 0; i < this.taskCollectionService.totalPages; i++) {
|
2020-04-01 20:13:57 +00:00
|
|
|
|
|
|
|
// Show ellipsis instead of all pages
|
2020-06-11 15:34:13 +00:00
|
|
|
if (
|
2020-04-01 20:13:57 +00:00
|
|
|
i > 0 && // Always at least the first page
|
|
|
|
(i + 1) < this.taskCollectionService.totalPages && // And the last page
|
|
|
|
(
|
|
|
|
// And the current with current + 1 and current - 1
|
|
|
|
(i + 1) > this.currentPage + 1 ||
|
|
|
|
(i + 1) < this.currentPage - 1
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Only add an ellipsis if the last page isn't already one
|
2020-06-11 15:34:13 +00:00
|
|
|
if (this.pages[i - 1] && !this.pages[i - 1].isEllipsis) {
|
2020-04-01 20:13:57 +00:00
|
|
|
this.pages.push({
|
|
|
|
number: 0,
|
|
|
|
isEllipsis: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pages.push({
|
|
|
|
number: i + 1,
|
|
|
|
isEllipsis: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.error(e, this)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
loadTasksForPage(e) {
|
|
|
|
// The page parameter can be undefined, in the case where the user loads a new list from the side bar menu
|
2020-04-14 21:37:08 +00:00
|
|
|
let page = Number(e.page)
|
2020-04-01 20:13:57 +00:00
|
|
|
if (typeof e.page === 'undefined') {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
let search = e.search
|
|
|
|
if (typeof e.search === 'undefined') {
|
|
|
|
search = ''
|
|
|
|
}
|
|
|
|
this.initTasks(page, search)
|
|
|
|
},
|
|
|
|
sortTasks() {
|
|
|
|
if (this.tasks === null || this.tasks === []) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-11 15:34:13 +00:00
|
|
|
return this.tasks.sort(function (a, b) {
|
2020-04-01 20:13:57 +00:00
|
|
|
if (a.done < b.done)
|
|
|
|
return -1
|
|
|
|
if (a.done > b.done)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if (a.id > b.id)
|
|
|
|
return -1
|
|
|
|
if (a.id < b.id)
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
},
|
|
|
|
searchTasks() {
|
2020-05-22 15:32:18 +00:00
|
|
|
// Only search if the search term changed
|
|
|
|
if (this.$route.query === this.searchTerm) {
|
2020-04-01 20:13:57 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-22 15:32:18 +00:00
|
|
|
|
2020-04-01 20:13:57 +00:00
|
|
|
this.$router.push({
|
2020-04-25 23:11:34 +00:00
|
|
|
name: 'list.list',
|
2020-09-05 20:35:52 +00:00
|
|
|
query: {search: this.searchTerm},
|
2020-04-01 20:13:57 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
hideSearchBar() {
|
|
|
|
// This is a workaround.
|
|
|
|
// When clicking on the search button, @blur from the input is fired. If we
|
|
|
|
// would then directly hide the whole search bar directly, no click event
|
|
|
|
// from the button gets fired. To prevent this, we wait 200ms until we hide
|
|
|
|
// everything so the button has a chance of firering the search event.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.showTaskSearch = false
|
|
|
|
}, 200)
|
|
|
|
},
|
|
|
|
getRouteForPagination(page = 1, type = 'list') {
|
|
|
|
return {
|
2020-04-27 19:05:53 +00:00
|
|
|
name: 'list.' + type,
|
2020-04-01 20:13:57 +00:00
|
|
|
params: {
|
2020-09-05 20:35:52 +00:00
|
|
|
type: type,
|
2020-04-01 20:13:57 +00:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
page: page,
|
|
|
|
},
|
|
|
|
}
|
2020-06-11 15:34:13 +00:00
|
|
|
},
|
2020-09-05 20:35:52 +00:00
|
|
|
},
|
2020-04-01 20:13:57 +00:00
|
|
|
}
|