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

443 lines
11 KiB
TypeScript
Raw Normal View History

import Vue from 'vue'
import { Component, Watch } from 'vue-property-decorator'
2019-03-22 15:32:32 +01:00
import { ICfgServer, IDrag, IGlobalState, ITodo, ITodosState } from '../../model/index'
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'
// _.cloneDeep( Per clonare un oggetto
2019-02-02 20:13:06 +01:00
2019-03-22 15:32:32 +01:00
import { costanti } from '../../store/Modules/costanti'
2019-03-13 01:53:53 +01:00
import { Getter, Mutation, State } from 'vuex-class'
const namespace: string = 'Todos'
2019-03-22 15:32:32 +01:00
import globalroutines from '../../globalroutines/index'
2019-03-04 18:48:07 +01:00
@Component({
2019-03-04 17:28:29 +01:00
components: { SingleTodo },
filters: {
2019-03-13 01:53:53 +01:00
capitalize(value) {
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-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)
}
get todos_vista() {
let mystr = ''
const arr = Todos.getters.todos_dacompletare(this.categoryAtt)
2019-03-13 01:53:53 +01:00
for (const ind in arr) {
mystr += this.getstrelem(arr[ind]) + '\n'
}
return mystr + ''
}
// get mytodos_dacompletare() {
// return todos_dacompletare(this.categoryAtt)
// }
// @Watch('$route', { immediate: true, deep: true })
// onUrlChange(newVal: any) {
// // Some action
// }
// Computed:
get reload_fromServer() {
return Todos.state.reload_fromServer
}
set reload_fromServer(value: number) {
Todos.state.reload_fromServer = value
}
2019-03-13 01:53:53 +01:00
public $q: any
public filter: boolean = false
public title: string = ''
public todotop: string = ''
public todobottom: string = ''
public drag: boolean = true
public startpos: number = 0
public listPriorityLabel: number[] = []
public arrPrior: number[] = []
public itemDragStart: any = null
public polling = null
public loadDone: boolean = false
public inddragging: number = -1
public service: any
public actualMaxPosition: number = 15
public scrollable = true
public tmpstrTodos: string = ''
public categoryAtt: string = ''
// public showtype: number = Todos.state.showtype
2019-03-13 01:53:53 +01:00
public $refs: {
single: SingleTodo[]
}
@Getter('todos_dacompletare', { namespace })
public todos_dacompletare: (state: ITodosState, category: string) => ITodo[]
@Getter('todos_completati', { namespace })
public todos_completati: (state: ITodosState, category: string) => ITodo[]
@Watch('$route.params.category') public changecat() {
this.categoryAtt = this.$route.params.category
}
// clickaggshowtype () {
// console.log('1B) clickaggshowtype Todos.state.showtype=', Todos.state.showtype)
// Todos.state.showtype = costanti.ShowTypeTask.SHOW_ALL
// console.log('2B) Dopo: showtype=', this.showtype)
// }
public loadval(e) {
console.log('1) loadval, showtype=', this.showtype)
this.showtype = Todos.state.showtype
console.log('2) Dopo: showtype=', this.showtype)
}
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 onStart() {
this.startpos = 0
this.itemDragStart = null
}
2019-03-13 01:53:53 +01:00
public logelem(mystr, elem) {
console.log(mystr, 'elem [', elem._id, '] ', elem.descr, ' Pr(', this.getPriorityByInd(elem.priority), ') [', elem.id_prev, '] modif=', elem.modified)
}
2019-03-13 01:53:53 +01:00
public getstrelem(elem) {
return 'elem [' + elem._id + '] ' + elem.descr + ' Pr(' + this.getPriorityByInd(elem.priority) + ') [ID_PREV=' + elem.id_prev + '] modif=' + elem.modified + ' '
}
2019-03-13 01:53:53 +01:00
public getTitlePriority(priority) {
let cl = ''
2019-03-13 01:53:53 +01:00
if (priority === tools.Todos.PRIORITY_HIGH) {
cl = 'high_priority'
2019-03-13 01:53:53 +01:00
}
else if (priority === tools.Todos.PRIORITY_NORMAL) {
cl = 'medium_priority'
2019-03-13 01:53:53 +01:00
}
else if (priority === tools.Todos.PRIORITY_LOW) {
cl = 'low_priority'
2019-03-13 01:53:53 +01:00
}
return cl + ' titlePriority'
}
2019-03-13 01:53:53 +01:00
public logga_arr(myarr: ITodo[]) {
let mystr = '\n'
2019-03-13 01:53:53 +01:00
myarr.forEach((item) => {
mystr += '[' + item.pos + '] ' + item.descr + ' Pr(' + this.getPriorityByInd(item.priority) + ') [' + item.id_prev + '] modif=' + item.modified + '\n'
// mystr += '[' + item.pos + '] ' + item.descr + '\n'
})
return mystr
}
2019-03-13 01:53:53 +01:00
public async onEnd(itemdragend) {
console.log('************ END DRAG: ', itemdragend)
this.inddragging = -1
await Todos.actions.swapElems(itemdragend)
}
2019-03-13 01:53:53 +01:00
public created() {
const $service = this.$dragula.$service
$service.options('first',
{
// isContainer: function (el) {
// return el.classList.contains('dragula-container')
// },
2019-03-13 01:53:53 +01:00
moves(el, source, handle, sibling) {
// console.log('moves')
return !el.classList.contains('donotdrag') // elements are always draggable by default
},
2019-03-13 01:53:53 +01:00
accepts(el, target, source, sibling) {
2019-03-22 15:32:32 +01:00
console.log('accepts dragging '+ el.id + ' from ' + source.id + ' to ' + target.id)
return true // elements can be dropped in any of the `containers` by default
},
2019-03-13 01:53:53 +01:00
invalid(el, handle) {
// console.log('invalid')
return el.classList.contains('donotdrag') // don't prevent any drags from initiating by default
},
direction: 'vertical'
})
$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) => {
// mythis.inddragging = mythis.getElementIndex(el)
2019-03-08 02:04:56 +01:00
console.log('+++ DRAG ind=', this.inddragging)
this.scrollable = false
})
2019-03-08 02:04:56 +01:00
$service.eventBus.$on('drop', (el, source) => {
console.log('+++ DROP')
2019-03-08 02:04:56 +01:00
this.scrollable = true
})
this.load()
}
2019-03-13 01:53:53 +01:00
public mounted() {
// console.log('*** MOUNTED ***')
this.categoryAtt = this.$route.params.category
if (window) {
2019-03-08 02:04:56 +01:00
window.addEventListener('touchmove', (e) => {
// console.log('touchmove')
2019-03-08 02:04:56 +01:00
if (!this.scrollable) {
e.preventDefault()
}
}, { passive: false })
}
}
2019-03-13 01:53:53 +01:00
public setarrPriority() {
this.arrPrior = []
const arr = tools.selectPriority[UserStore.state.lang]
2019-02-16 02:01:17 +01:00
if (arr) {
2019-03-13 01:53:53 +01:00
arr.forEach((rec) => {
2019-02-16 02:01:17 +01:00
this.arrPrior.push(rec.value)
})
}
// console.log('Array PRIOR:', this.arrPrior)
}
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)
2019-03-22 15:32:32 +01:00
for (const todosKey in ApiTables.Priority) {
this.listPriorityLabel.push(ApiTables.Priority[todosKey])
}
// console.log('Priority:' + this.listPriorityLabel)
this.setarrPriority()
2019-02-14 20:08:22 +01:00
this.loadDone = true
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 getPriorityByInd(index) {
2019-02-16 02:01:17 +01:00
// console.log('LANG in PRIOR', UserStore.state.lang)
try {
const arr = tools.selectPriority[UserStore.state.lang]
2019-03-13 01:53:53 +01:00
for (const rec of arr) {
if (rec.value === index) {
2019-02-16 02:01:17 +01:00
return rec.label
2019-03-13 01:53:53 +01:00
}
2019-02-16 02:01:17 +01:00
}
} catch (e) {
}
return ''
}
2019-03-13 01:53:53 +01:00
public isRegistered() {
return localStorage.getItem(tools.localStorage.userId) !== ''
}
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
}
if (UserStore.state.userId === undefined) {
2019-03-13 13:40:17 +01:00
tools.showNotif(this.$q, this.$t('todo.usernotdefined'))
return
}
if (!this.isRegistered()) {
// Not logged
2019-03-13 13:40:17 +01:00
tools.showNotif(this.$q, this.$t('user.notregistered'))
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)
// Update the others components...
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
public loadMoreTodo(index, done) {
setTimeout(() => {
this.actualMaxPosition += 15
done()
}, 100)
}
2019-03-13 01:53:53 +01:00
public getArrTodos() {
2019-03-04 18:48:07 +01:00
let mystr = ''
2019-03-08 02:04:56 +01:00
this.tmpstrTodos = ''
2019-03-04 18:48:07 +01:00
return globalroutines(null, 'readall', 'todos', null)
2019-03-08 02:04:56 +01:00
.then((alldata) => {
2019-03-04 18:48:07 +01:00
const myrecs = [...alldata]
2019-03-13 01:53:53 +01:00
myrecs.forEach((rec) => {
2019-03-04 18:48:07 +01:00
mystr = mystr + rec.descr + rec.completed + '] ['
})
2019-03-08 02:04:56 +01:00
this.tmpstrTodos = 'TODOS: ' + mystr
2019-03-04 18:48:07 +01:00
})
}
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)
}
2019-03-04 18:48:07 +01:00
// setArrTodos() {
//
// let mystr = ''
// let mythis = this
//
// mythis.tmpstrTodos = ''
// return globalroutines(null, 'write', 'todos', this.todos_arr[0])
// .then(function (alldata) {
// mythis.getArrTodos()
// })
// }
//
}