Files
freeplanet/src/views/todo/todo.ts

235 lines
5.1 KiB
TypeScript
Raw Normal View History

import Vue from 'vue'
import { Component, Watch } from 'vue-property-decorator'
2019-03-22 20:51:42 +01:00
import { IDrag, ITodo, ITodosState } from '../../model/index'
2019-03-22 15:32:32 +01:00
import { SingleTodo } from '../../components/todos/SingleTodo/index'
2019-03-22 15:32:32 +01:00
import { tools } from '../../store/Modules/tools'
import * as ApiTables from '../../store/Modules/ApiTables'
2019-03-04 17:28:29 +01:00
import { GlobalStore, Todos } from '@store'
import { UserStore } from '@store'
2019-03-22 20:51:42 +01:00
import { Getter } from 'vuex-class'
2019-03-04 18:48:07 +01:00
2019-03-22 18:49:38 +01:00
const namespace: string = 'Todos'
@Component({
2019-03-04 17:28:29 +01:00
components: { SingleTodo },
filters: {
2019-03-13 01:53:53 +01:00
capitalize(value) {
2019-03-22 20:51:42 +01:00
if (!value) {
return ''
}
2019-03-04 17:28:29 +01:00
value = value.toString()
return value.charAt(0).toUpperCase() + value.slice(1)
}
}
})
export default class Todo extends Vue {
2019-03-22 20:51:42 +01:00
public $q: any
public todotop: string = ''
public todobottom: string = ''
public polling = null
public service: any
public scrollable = true
public categoryAtt: string = ''
2019-03-22 18:49:38 +01:00
public dragname: string = 'first'
2019-03-22 20:51:42 +01:00
public $refs: {
single: SingleTodo[]
}
get tools() {
return tools
}
2019-03-13 01:53:53 +01:00
get showtype() {
2019-03-04 17:28:29 +01:00
return Todos.state.showtype
}
2019-03-13 01:53:53 +01:00
set showtype(value) {
console.log('showtype', value)
2019-03-04 17:28:29 +01:00
GlobalStore.mutations.setShowType(value)
}
get doneTodosCount() {
return Todos.getters.doneTodosCount(this.categoryAtt)
}
2019-03-04 17:28:29 +01:00
get menuPopupConfigTodo() {
return tools.menuPopupConfigTodo[UserStore.state.lang]
}
get listOptionShowTask() {
return tools.listOptionShowTask[UserStore.state.lang]
}
get TodosCount() {
return Todos.getters.TodosCount(this.categoryAtt)
}
2019-03-13 01:53:53 +01:00
@Getter('todos_dacompletare', { namespace })
public todos_dacompletare: (state: ITodosState, category: string) => ITodo[]
@Getter('todos_completati', { namespace })
public todos_completati: (state: ITodosState, category: string) => ITodo[]
2019-03-22 20:51:42 +01:00
@Watch('$route.params.category')
public changecat() {
2019-03-13 01:53:53 +01:00
this.categoryAtt = this.$route.params.category
}
public getmyid(id) {
return 'row' + id
}
2019-03-13 01:53:53 +01:00
public showTask(field_value) {
2019-03-04 17:28:29 +01:00
return field_value === tools.MenuAction.SHOW_TASK
}
2019-03-13 01:53:53 +01:00
public async onEnd(itemdragend) {
await Todos.actions.swapElems(itemdragend)
}
2019-03-13 01:53:53 +01:00
public created() {
const $service = this.$dragula.$service
2019-03-22 18:49:38 +01:00
tools.dragula_option($service, this.dragname)
$service.eventBus.$on('dragend', (args) => {
2019-03-13 01:53:53 +01:00
const itemdragend: IDrag = {
category: this.categoryAtt,
newIndex: this.getElementIndex(args.el),
oldIndex: this.getElementOldIndex(args.el)
}
this.onEnd(itemdragend)
})
2019-03-08 02:04:56 +01:00
$service.eventBus.$on('drag', (el, source) => {
this.scrollable = false
})
2019-03-08 02:04:56 +01:00
$service.eventBus.$on('drop', (el, source) => {
this.scrollable = true
})
this.load()
}
2019-03-13 01:53:53 +01:00
public mounted() {
this.categoryAtt = this.$route.params.category
2019-03-22 20:51:42 +01:00
tools.touchmove(this.scrollable)
}
2019-03-13 01:53:53 +01:00
public async load() {
2019-03-04 17:28:29 +01:00
console.log('LOAD TODO....')
this.categoryAtt = this.$route.params.category
// Set last category selected
localStorage.setItem(tools.localStorage.categorySel, this.categoryAtt)
this.checkUpdate_everytime()
}
// Call to check if need to refresh
2019-03-13 01:53:53 +01:00
public checkUpdate_everytime() {
this.polling = setInterval(() => {
this.checkUpdate()
}, 60000)
}
2019-03-13 01:53:53 +01:00
public beforeDestroy() {
clearInterval(this.polling)
}
2019-03-13 01:53:53 +01:00
public mydeleteItem(idobj: string) {
// console.log('mydeleteItem', idobj)
return Todos.actions.deleteItem({ cat: this.categoryAtt, idobj })
}
2019-03-13 01:53:53 +01:00
public insertTodo(atfirst: boolean = false) {
let descr = this.todobottom.trim()
2019-03-13 01:53:53 +01:00
if (atfirst) {
descr = this.todotop.trim()
2019-03-13 01:53:53 +01:00
}
2019-03-13 01:53:53 +01:00
if (descr === '') {
return
2019-03-13 01:53:53 +01:00
}
2019-03-22 20:51:42 +01:00
if (!tools.checkIfUserExist(this)) {
return
}
2019-03-13 01:53:53 +01:00
const myobj: ITodo = {
descr,
category: this.categoryAtt
}
2019-03-22 15:32:32 +01:00
// empty the field
if (atfirst) {
this.todotop = ''
}
else {
this.todobottom = ''
}
2019-02-02 20:13:06 +01:00
2019-03-22 15:32:32 +01:00
return Todos.actions.insertTodo({ myobj, atfirst })
2019-02-02 20:13:06 +01:00
}
2019-03-13 01:53:53 +01:00
public async updateitem({ myitem, field }) {
console.log('calling MODIFY updateitem', myitem, field)
2019-02-02 20:13:06 +01:00
const itemdragend: IDrag = {
category: this.categoryAtt,
field,
idelemtochange: myitem._id,
prioritychosen: myitem.priority,
atfirst: false
}
await Todos.actions.swapElems(itemdragend)
await Todos.actions.modify({ myitem, field })
}
2019-03-13 01:53:53 +01:00
public deselectAllRows(item: ITodo, check, onlythis: boolean = false) {
// console.log('deselectAllRows : ', item)
for (let i = 0; i < this.$refs.single.length; i++) {
2019-03-13 01:53:53 +01:00
const contr = this.$refs.single[i] as SingleTodo
// @ts-ignore
2019-03-13 01:53:53 +01:00
const id = contr.itemtodo._id
// Don't deselect the actual clicked!
let des = false
if (onlythis) {
des = item._id === id
} else {
des = ((check && (item._id !== id)) || (!check))
}
if (des) {
// @ts-ignore
contr.deselectAndExitEdit()
}
}
}
2019-03-13 01:53:53 +01:00
public checkUpdate() {
2019-03-22 15:32:32 +01:00
ApiTables.waitAndcheckPendingMsg()
}
2019-03-13 01:53:53 +01:00
private getElementIndex(el: any) {
return [].slice.call(el.parentElement.children).indexOf(el)
}
private getElementOldIndex(el: any) {
return parseInt(el.attributes.index.value, 10)
}
}