2019-03-04 17:28:29 +01:00
|
|
|
import { ITodo, ITodosState, IParamTodo, IDrag } from 'model'
|
2019-01-14 22:40:30 +01:00
|
|
|
import { storeBuilder } from './Store/Store'
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
import Api from '@api'
|
2019-02-27 02:58:41 +01:00
|
|
|
import { tools } from './tools'
|
2019-02-08 17:10:25 +01:00
|
|
|
import { GlobalStore, Todos, UserStore } from '@store'
|
2019-02-03 14:40:20 +01:00
|
|
|
import globalroutines from './../../globalroutines/index'
|
2019-02-12 12:06:01 +01:00
|
|
|
import { Mutation } from 'vuex-module-decorators'
|
|
|
|
|
import { serv_constants } from '@src/store/Modules/serv_constants'
|
2019-02-27 02:58:41 +01:00
|
|
|
import { GetterTree } from 'vuex'
|
|
|
|
|
import objectId from '@src/js/objectId'
|
2019-03-04 17:28:29 +01:00
|
|
|
import { costanti } from '@src/store/Modules/costanti'
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
// import _ from 'lodash'
|
2019-01-14 22:40:30 +01:00
|
|
|
|
|
|
|
|
const state: ITodosState = {
|
2019-03-04 17:28:29 +01:00
|
|
|
showtype: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED,
|
2019-02-27 02:58:41 +01:00
|
|
|
todos: [[]],
|
|
|
|
|
categories: [],
|
|
|
|
|
// todos_changed: 1,
|
2019-02-14 18:38:23 +01:00
|
|
|
reload_fromServer: 0,
|
2019-02-04 03:09:15 +01:00
|
|
|
testpao: 'Test',
|
2019-02-27 02:58:41 +01:00
|
|
|
insidePending: false,
|
|
|
|
|
visuLastCompleted: 10
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
const fieldtochange: String [] = ['descr', 'completed', 'category', 'expiring_at', 'priority', 'id_prev', 'pos', 'enableExpiring', 'progress']
|
|
|
|
|
|
|
|
|
|
const b = storeBuilder.module<ITodosState>('Todos', state)
|
2019-02-01 04:10:31 +01:00
|
|
|
const stateGetter = b.state()
|
2019-01-14 22:40:30 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
function getindexbycategory(category: string) {
|
|
|
|
|
return state.categories.indexOf(category)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function gettodosByCategory(category: string) {
|
|
|
|
|
const indcat = state.categories.indexOf(category)
|
|
|
|
|
if (!state.todos[indcat])
|
|
|
|
|
return []
|
|
|
|
|
return state.todos[indcat]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isValidIndex(cat, index) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
return (index >= 0 && index < myarr.length)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getElemByIndex(cat, index) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
|
|
|
|
|
if (index >= 0 && index < myarr.length)
|
|
|
|
|
return myarr[index]
|
|
|
|
|
else
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getElemById(cat, id) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
for (let indrec = 0; indrec < myarr.length; indrec++) {
|
|
|
|
|
if (myarr[indrec]._id === id) {
|
|
|
|
|
return myarr[indrec]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getIndexById(cat, id) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
for (let indrec = 0; indrec < myarr.length; indrec++) {
|
|
|
|
|
if (myarr[indrec]._id === id) {
|
|
|
|
|
return indrec
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getElemPrevById(cat, id_prev) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
for (let indrec = 0; indrec < myarr.length; indrec++) {
|
|
|
|
|
if (myarr[indrec].id_prev === id_prev) {
|
|
|
|
|
return myarr[indrec]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLastFirstElemPriority(cat: string, priority: number, atfirst: boolean, escludiId: string) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
if (myarr === null)
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
let trovato: boolean = false
|
|
|
|
|
|
|
|
|
|
console.log('priority', priority)
|
|
|
|
|
|
|
|
|
|
for (let indrec = 0; indrec < myarr.length; indrec++) {
|
|
|
|
|
if ((myarr[indrec].priority === priority) && (myarr[indrec]._id !== escludiId)) {
|
|
|
|
|
trovato = true
|
|
|
|
|
if (atfirst) {
|
|
|
|
|
return indrec - 1
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (trovato) {
|
|
|
|
|
return indrec
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('trovato?', trovato, 'indrec')
|
|
|
|
|
|
|
|
|
|
if (trovato) {
|
|
|
|
|
return myarr.length - 1
|
|
|
|
|
} else {
|
|
|
|
|
if (priority === tools.Todos.PRIORITY_LOW)
|
|
|
|
|
return myarr.length - 1
|
|
|
|
|
else if (priority === tools.Todos.PRIORITY_HIGH)
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFirstList(cat) {
|
|
|
|
|
const myarr = gettodosByCategory(cat)
|
|
|
|
|
for (let indrec in myarr) {
|
|
|
|
|
if (myarr[indrec].id_prev === tools.LIST_START) {
|
|
|
|
|
return myarr[indrec]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLastListNotCompleted(cat) {
|
|
|
|
|
const arr = Todos.getters.todos_dacompletare(cat)
|
|
|
|
|
// console.log('cat', cat, 'arr', arr)
|
|
|
|
|
if (arr.length > 0)
|
|
|
|
|
return arr[arr.length - 1]
|
|
|
|
|
else
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getstrelem(elem) {
|
|
|
|
|
return 'ID [' + elem._id + '] ' + elem.descr + ' [ID_PREV=' + elem.id_prev + '] modif=' + elem.modified
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function update_idprev(indcat, indelemchange, indelemId) {
|
|
|
|
|
if (indelemchange >= 0 && indelemchange < state.todos[indcat].length) {
|
|
|
|
|
const id_prev = (indelemId >= 0) ? state.todos[indcat][indelemId]._id : tools.LIST_START
|
|
|
|
|
if (state.todos[indcat][indelemchange].id_prev !== id_prev) {
|
|
|
|
|
state.todos[indcat][indelemchange].id_prev = id_prev
|
|
|
|
|
tools.notifyarraychanged(state.todos[indcat][indelemchange])
|
|
|
|
|
// state.todos[indcat][indelemchange].modified = true
|
|
|
|
|
console.log('Index=', indelemchange, 'indtoget', indelemId, getstrelem(state.todos[indcat][indelemchange]))
|
|
|
|
|
return state.todos[indcat][indelemchange]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function initcat() {
|
|
|
|
|
|
|
|
|
|
let tomorrow = new Date()
|
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
|
|
|
|
|
|
|
|
const objtodo: ITodo = {
|
|
|
|
|
// _id: new Date().toISOString(), // Create NEW
|
|
|
|
|
_id: objectId(),
|
|
|
|
|
userId: UserStore.state.userId,
|
|
|
|
|
descr: '',
|
|
|
|
|
priority: tools.Todos.PRIORITY_NORMAL,
|
|
|
|
|
completed: false,
|
|
|
|
|
created_at: new Date(),
|
|
|
|
|
modify_at: new Date(),
|
|
|
|
|
completed_at: new Date(),
|
|
|
|
|
category: '',
|
|
|
|
|
expiring_at: tomorrow,
|
|
|
|
|
enableExpiring: false,
|
|
|
|
|
id_prev: '',
|
|
|
|
|
pos: 0,
|
|
|
|
|
modified: false,
|
|
|
|
|
progress: 0
|
|
|
|
|
}
|
|
|
|
|
// return this.copy(objtodo)
|
|
|
|
|
return objtodo
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteItemToSyncAndDb(table: String, item: ITodo, id) {
|
|
|
|
|
cmdToSyncAndDbTodo(tools.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function saveItemToSyncAndDb(table: String, method, item: ITodo) {
|
|
|
|
|
return await cmdToSyncAndDbTodo(tools.DB.CMD_SYNC_NEW_TODOS, table, method, item, 0, '')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function cmdToSyncAndDbTodo(cmd, table, method, item: ITodo, id, msg: String) {
|
|
|
|
|
// Send to Server to Sync
|
|
|
|
|
|
|
|
|
|
console.log('cmdToSyncAndDbTodo', cmd, table, method, item.descr, id, msg)
|
|
|
|
|
|
|
|
|
|
const risdata = await tools.cmdToSyncAndDb(cmd, table, method, item, id, msg)
|
|
|
|
|
|
|
|
|
|
if (cmd === tools.DB.CMD_SYNC_NEW_TODOS) {
|
|
|
|
|
if (method === 'POST')
|
|
|
|
|
await Todos.actions.dbInsertTodo(item)
|
|
|
|
|
else if (method === 'PATCH')
|
|
|
|
|
await Todos.actions.dbSaveTodo(item)
|
|
|
|
|
} else if (cmd === tools.DB.CMD_DELETE_TODOS) {
|
|
|
|
|
await Todos.actions.dbdeleteItem(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return risdata
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
namespace Getters {
|
2019-02-27 02:58:41 +01:00
|
|
|
// const fullName = b.read(function fullName(state): string {
|
|
|
|
|
// return state.userInfos.firstname?capitalize(state.userInfos.firstname) + " " + capitalize(state.userInfos.lastname):null;
|
|
|
|
|
// })
|
|
|
|
|
const todos_dacompletare = b.read((state: ITodosState) => (cat: string): ITodo[] => {
|
|
|
|
|
const indcat = getindexbycategory(cat)
|
|
|
|
|
if (state.todos[indcat]) {
|
|
|
|
|
return state.todos[indcat].filter(todo => !todo.completed)
|
|
|
|
|
} else return []
|
|
|
|
|
}, 'todos_dacompletare')
|
|
|
|
|
|
|
|
|
|
const todos_completati = b.read((state: ITodosState) => (cat: string): ITodo[] => {
|
|
|
|
|
const indcat = getindexbycategory(cat)
|
|
|
|
|
if (state.todos[indcat]) {
|
2019-03-04 17:28:29 +01:00
|
|
|
if (state.showtype === costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED)
|
|
|
|
|
return state.todos[indcat].filter(todo => todo.completed).slice(0, state.visuLastCompleted) // Show only the first N completed
|
|
|
|
|
else if (state.showtype === costanti.ShowTypeTask.SHOW_ALL)
|
|
|
|
|
return state.todos[indcat].filter(todo => todo.completed)
|
|
|
|
|
else
|
|
|
|
|
return []
|
2019-02-27 02:58:41 +01:00
|
|
|
} else return []
|
|
|
|
|
}, 'todos_completati')
|
|
|
|
|
|
|
|
|
|
const doneTodosCount = b.read((state: ITodosState) => (cat: string): number => {
|
|
|
|
|
return getters.todos_completati(cat).length
|
|
|
|
|
}, 'doneTodosCount')
|
|
|
|
|
const TodosCount = b.read((state: ITodosState) => (cat: string): number => {
|
|
|
|
|
const indcat = getindexbycategory(cat)
|
|
|
|
|
if (state.todos[indcat]) {
|
|
|
|
|
return state.todos[indcat].length
|
|
|
|
|
} else {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}, 'TodosCount')
|
2019-01-14 22:40:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getters = {
|
2019-02-27 02:58:41 +01:00
|
|
|
// get fullName() { return fullName();},
|
|
|
|
|
get todos_dacompletare() {
|
|
|
|
|
return todos_dacompletare()
|
|
|
|
|
},
|
|
|
|
|
get todos_completati() {
|
|
|
|
|
return todos_completati()
|
|
|
|
|
},
|
|
|
|
|
get doneTodosCount() {
|
|
|
|
|
return doneTodosCount()
|
|
|
|
|
},
|
|
|
|
|
get TodosCount() {
|
|
|
|
|
return TodosCount()
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
namespace Mutations {
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
function setTestpao(state: ITodosState, testpao: String) {
|
|
|
|
|
state.testpao = testpao
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
function findTodoById(state: ITodosState, data: IParamTodo) {
|
|
|
|
|
const indcat = state.categories.indexOf(data.categorySel)
|
|
|
|
|
if (indcat >= 0) {
|
|
|
|
|
if (state.todos[indcat]) {
|
|
|
|
|
for (let i = 0; i < state.todos[indcat].length; i++) {
|
|
|
|
|
if (state.todos[indcat][i]._id === data.id)
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-15 01:25:44 +01:00
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
function createNewItem(state: ITodosState, { objtodo, atfirst, categorySel }) {
|
|
|
|
|
const indcat = state.categories.indexOf(categorySel)
|
|
|
|
|
console.log('createNewItem', objtodo, 'cat=', categorySel, 'state.todos[indcat]', state.todos[indcat])
|
|
|
|
|
if (state.todos[indcat] === undefined) {
|
|
|
|
|
state.todos[indcat] = []
|
|
|
|
|
}
|
|
|
|
|
if (!state.todos[indcat]) {
|
|
|
|
|
state.todos[indcat].push(objtodo)
|
|
|
|
|
console.log('state.todos[indcat]', state.todos[indcat])
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-02-20 17:54:48 +01:00
|
|
|
if (atfirst)
|
2019-02-27 02:58:41 +01:00
|
|
|
state.todos[indcat].unshift(objtodo)
|
2019-02-20 17:54:48 +01:00
|
|
|
else
|
2019-02-27 02:58:41 +01:00
|
|
|
state.todos[indcat].push(objtodo)
|
2019-02-15 01:25:44 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
console.log('state.todos[indcat]', state.todos[indcat])
|
2019-02-15 01:25:44 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deletemyitem(state: ITodosState, myitem: ITodo) {
|
|
|
|
|
// Find record
|
2019-02-27 02:58:41 +01:00
|
|
|
const indcat = state.categories.indexOf(myitem.category)
|
|
|
|
|
const ind = findTodoById(state, { id: myitem._id, categorySel: myitem.category })
|
2019-02-15 01:25:44 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
console.log('PRIMA state.todos', state.todos)
|
2019-02-15 01:25:44 +01:00
|
|
|
// Delete Item in to Array
|
|
|
|
|
if (ind >= 0)
|
2019-02-27 02:58:41 +01:00
|
|
|
state.todos[indcat].splice(ind, 1)
|
|
|
|
|
|
|
|
|
|
console.log('DOPO state.todos', state.todos, 'ind', ind)
|
2019-02-15 01:25:44 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
// tools.notifyarraychanged(state.todos[indcat])
|
2019-02-15 01:25:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
export const mutations = {
|
|
|
|
|
setTestpao: b.commit(setTestpao),
|
2019-02-15 01:25:44 +01:00
|
|
|
deletemyitem: b.commit(deletemyitem),
|
|
|
|
|
createNewItem: b.commit(createNewItem)
|
2019-02-03 14:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function consolelogpao(strlog, strlog2 = '', strlog3 = '') {
|
2019-02-27 02:58:41 +01:00
|
|
|
globalroutines(null, 'log', strlog + ' ' + strlog2 + ' ' + strlog3, null)
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
namespace Actions {
|
|
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
// If something in the call of Service Worker went wrong (Network or Server Down), then retry !
|
|
|
|
|
async function sendSwMsgIfAvailable() {
|
|
|
|
|
let something = false
|
|
|
|
|
|
2019-02-08 17:10:25 +01:00
|
|
|
if ('serviceWorker' in navigator) {
|
2019-02-12 12:06:01 +01:00
|
|
|
console.log(' -------- sendSwMsgIfAvailable')
|
2019-02-08 17:10:25 +01:00
|
|
|
|
|
|
|
|
let count = await checkPendingMsg(null)
|
|
|
|
|
if (count > 0) {
|
2019-02-14 18:38:23 +01:00
|
|
|
return await navigator.serviceWorker.ready
|
2019-02-04 03:09:15 +01:00
|
|
|
.then(function (sw) {
|
|
|
|
|
|
2019-02-14 18:38:23 +01:00
|
|
|
return globalroutines(null, 'readall', 'swmsg')
|
2019-02-04 03:09:15 +01:00
|
|
|
.then(function (arr_recmsg) {
|
2019-02-07 00:53:10 +01:00
|
|
|
// let recclone = [...arr_recmsg]
|
2019-02-04 03:09:15 +01:00
|
|
|
if (arr_recmsg.length > 0) {
|
|
|
|
|
|
2019-02-07 00:53:10 +01:00
|
|
|
// console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
|
2019-02-14 20:08:22 +01:00
|
|
|
let promiseChain = Promise.resolve()
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
for (let indrec in arr_recmsg) {
|
2019-02-12 12:06:01 +01:00
|
|
|
// console.log(' .... sw.sync.register ( ', rec._id)
|
|
|
|
|
// if ('SyncManager' in window) {
|
|
|
|
|
// sw.sync.register(rec._id)
|
|
|
|
|
// } else {
|
2019-02-14 20:08:22 +01:00
|
|
|
|
|
|
|
|
// #Alternative to SyncManager
|
|
|
|
|
promiseChain = promiseChain.then(() => {
|
2019-02-27 02:58:41 +01:00
|
|
|
return Api.syncAlternative(arr_recmsg[indrec]._id)
|
2019-02-15 14:54:56 +01:00
|
|
|
.then(() => {
|
|
|
|
|
something = true
|
|
|
|
|
})
|
2019-02-14 20:08:22 +01:00
|
|
|
})
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
// }
|
2019-02-04 03:09:15 +01:00
|
|
|
}
|
2019-02-14 20:08:22 +01:00
|
|
|
return promiseChain
|
2019-02-04 03:09:15 +01:00
|
|
|
}
|
|
|
|
|
})
|
2019-02-08 17:10:25 +01:00
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-08 17:10:25 +01:00
|
|
|
|
2019-02-14 20:08:22 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
resolve(something)
|
|
|
|
|
})
|
2019-02-04 03:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 10:23:00 +01:00
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
async function waitAndcheckPendingMsg(context) {
|
|
|
|
|
|
2019-02-22 10:23:00 +01:00
|
|
|
// await aspettansec(1000)
|
2019-02-04 03:09:15 +01:00
|
|
|
|
|
|
|
|
return await checkPendingMsg(context)
|
|
|
|
|
.then(ris => {
|
|
|
|
|
if (ris) {
|
2019-02-22 10:23:00 +01:00
|
|
|
// console.log('risPending = ', ris)
|
2019-02-14 18:38:23 +01:00
|
|
|
return sendSwMsgIfAvailable()
|
2019-02-04 03:09:15 +01:00
|
|
|
.then(something => {
|
|
|
|
|
if (something) {
|
2019-02-15 14:54:56 +01:00
|
|
|
console.log('something')
|
2019-02-04 03:09:15 +01:00
|
|
|
// Refresh data
|
2019-02-14 18:38:23 +01:00
|
|
|
return waitAndRefreshData(context)
|
2019-02-04 03:09:15 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function waitAndRefreshData(context) {
|
2019-02-22 10:23:00 +01:00
|
|
|
// await aspettansec(3000)
|
2019-02-04 03:09:15 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
return await dbLoadTodo(context, { checkPending: false })
|
2019-02-04 03:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
async function readConfig(id) {
|
|
|
|
|
return await globalroutines(null, 'read', 'config', null, String(id))
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
async function checkPendingMsg(context) {
|
2019-02-04 04:17:50 +01:00
|
|
|
// console.log('checkPendingMsg')
|
2019-02-04 03:09:15 +01:00
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
const config = await globalroutines(null, 'read', 'config', null, '1')
|
2019-02-12 12:06:01 +01:00
|
|
|
// console.log('config', config)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (config) {
|
|
|
|
|
if (config[1].stateconn !== undefined) {
|
|
|
|
|
// console.log('config.stateconn', config[1].stateconn)
|
|
|
|
|
|
|
|
|
|
if (config[1].stateconn !== GlobalStore.state.stateConnection) {
|
|
|
|
|
GlobalStore.mutations.setStateConnection(config[1].stateconn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
// Check if there is something
|
2019-02-14 20:08:22 +01:00
|
|
|
return globalroutines(null, 'count', 'swmsg')
|
2019-02-04 03:09:15 +01:00
|
|
|
.then(function (count) {
|
|
|
|
|
if (count > 0) {
|
2019-02-22 10:23:00 +01:00
|
|
|
// console.log('count = ', count)
|
2019-02-04 03:09:15 +01:00
|
|
|
return resolve(true)
|
|
|
|
|
} else {
|
|
|
|
|
return resolve(false)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
return reject()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
async function dbLoadTodo(context, { checkPending }) {
|
|
|
|
|
console.log('dbLoadTodo', checkPending, 'userid=', UserStore.state.userId)
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
if (UserStore.state.userId === '')
|
|
|
|
|
return false // Login not made
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
let ris = await Api.SendReq('/todos/' + UserStore.state.userId, 'GET', null)
|
|
|
|
|
.then(res => {
|
|
|
|
|
if (res.data.todos) {
|
2019-02-27 02:58:41 +01:00
|
|
|
// console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
|
|
|
|
|
// console.log('RISULTANTE TODOS DAL SERVER = ', res.data.todos)
|
|
|
|
|
|
|
|
|
|
state.todos = res.data.todos
|
|
|
|
|
state.categories = res.data.categories
|
|
|
|
|
} else {
|
|
|
|
|
state.todos = [[]]
|
2019-02-12 12:06:01 +01:00
|
|
|
}
|
2019-02-04 03:09:15 +01:00
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
// console.log('PRIMA showtype = ', state.showtype)
|
|
|
|
|
|
|
|
|
|
state.showtype = parseInt(GlobalStore.getters.getConfigStringbyId(costanti.CONFIG_ID_SHOW_TYPE_TODOS))
|
|
|
|
|
|
|
|
|
|
// console.log('showtype = ', state.showtype)
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
// console.log('ARRAY TODOS = ', state.todos)
|
|
|
|
|
|
|
|
|
|
console.log('dbLoadTodo', 'state.todos', state.todos, 'state.categories', state.categories)
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
return res
|
2019-02-01 04:10:31 +01:00
|
|
|
})
|
2019-02-12 12:06:01 +01:00
|
|
|
.catch(error => {
|
2019-02-20 11:53:56 +01:00
|
|
|
console.log('error dbLoadTodo', error)
|
2019-02-06 18:47:54 +01:00
|
|
|
UserStore.mutations.setErrorCatch(error)
|
2019-02-19 02:33:02 +01:00
|
|
|
return error
|
2019-02-01 04:10:31 +01:00
|
|
|
})
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
if (ris.status !== 200) {
|
|
|
|
|
console.log('ris.status', ris.status)
|
2019-02-09 18:04:49 +01:00
|
|
|
if (ris.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
|
|
|
|
|
consolelogpao('UNAUTHORIZING... TOKEN EXPIRED... !! ')
|
|
|
|
|
} else {
|
2019-02-12 12:06:01 +01:00
|
|
|
consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)', UserStore.getters.getServerCode, ris.status)
|
2019-02-09 18:04:49 +01:00
|
|
|
}
|
2019-02-15 01:25:44 +01:00
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
// Read all data from IndexedDB Store into Memory
|
|
|
|
|
await updatefromIndexedDbToStateTodo(context)
|
|
|
|
|
}
|
2019-02-04 03:09:15 +01:00
|
|
|
} else {
|
2019-02-27 02:58:41 +01:00
|
|
|
if (ris.status === tools.OK && checkPending) {
|
2019-02-04 03:09:15 +01:00
|
|
|
waitAndcheckPendingMsg(context)
|
|
|
|
|
}
|
2019-02-03 14:40:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
async function updatefromIndexedDbToStateTodo(context) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('Update the array in memory, from todos table from IndexedDb')
|
2019-02-27 02:58:41 +01:00
|
|
|
await globalroutines(null, 'updatefromIndexedDbToStateTodo', 'categories', null)
|
2019-02-03 19:28:06 +01:00
|
|
|
.then(() => {
|
2019-02-15 01:25:44 +01:00
|
|
|
console.log('updatefromIndexedDbToStateTodo! ')
|
2019-02-03 19:28:06 +01:00
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
function aspettansec(numsec) {
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
resolve('anything')
|
|
|
|
|
}, numsec)
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
async function testfunc() {
|
|
|
|
|
while (true) {
|
|
|
|
|
consolelogpao('testfunc')
|
2019-02-03 19:28:06 +01:00
|
|
|
// console.log('Todos.state.todos_changed:', Todos.state.todos_changed)
|
2019-02-03 14:40:20 +01:00
|
|
|
await aspettansec(5000)
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbSaveTodo(context, itemtodo: ITodo) {
|
2019-02-03 00:51:58 +01:00
|
|
|
return await dbInsertSaveTodo(context, itemtodo, 'PATCH')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbInsertTodo(context, itemtodo: ITodo) {
|
|
|
|
|
return await dbInsertSaveTodo(context, itemtodo, 'POST')
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
async function dbInsertSaveTodo(context, itemtodo: ITodo, method) {
|
2019-02-03 03:44:25 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
if (!('serviceWorker' in navigator)) {
|
2019-02-03 02:40:24 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
console.log('dbInsertSaveTodo', itemtodo, method)
|
2019-02-19 02:33:02 +01:00
|
|
|
let call = '/todos'
|
2019-02-08 17:10:25 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
if (UserStore.state.userId === '')
|
|
|
|
|
return false // Login not made
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
if (method !== 'POST')
|
|
|
|
|
call += '/' + itemtodo._id
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
console.log('TODO TO SAVE: ', itemtodo)
|
2019-02-11 02:58:53 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
let res = await Api.SendReq(call, method, itemtodo)
|
2019-02-19 02:33:02 +01:00
|
|
|
.then(res => {
|
|
|
|
|
console.log('dbInsertSaveTodo to the Server', res.data)
|
2019-02-03 02:40:24 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
return (res.status === 200)
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
// return UserStore.getters.getServerCode
|
|
|
|
|
return false
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
return true
|
2019-02-02 20:13:06 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
async function dbdeleteItem(context, item: ITodo) {
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
if (!('serviceWorker' in navigator)) {
|
2019-02-27 02:58:41 +01:00
|
|
|
// console.log('dbdeleteItem', item)
|
2019-02-15 01:25:44 +01:00
|
|
|
if (UserStore.state.userId === '')
|
|
|
|
|
return false // Login not made
|
2019-02-03 03:44:25 +01:00
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
let res = await Api.SendReq('/todos/' + item._id, 'DELETE', item)
|
|
|
|
|
.then(res => {
|
2019-02-27 02:58:41 +01:00
|
|
|
console.log('dbdeleteItem to the Server')
|
2019-02-15 01:25:44 +01:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return UserStore.getters.getServerCode
|
|
|
|
|
})
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-15 01:25:44 +01:00
|
|
|
return res
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
function setmodifiedIfchanged(recOut, recIn, field) {
|
|
|
|
|
if (String(recOut[field]) !== String(recIn[field])) {
|
|
|
|
|
// console.log('*************** CAMPO ', field, 'MODIFICATO!', recOut[field], recIn[field])
|
|
|
|
|
recOut.modified = true
|
|
|
|
|
recOut[field] = recIn[field]
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function deleteItem(context, { cat, idobj }) {
|
|
|
|
|
console.log('deleteItem: KEY = ', idobj)
|
|
|
|
|
|
|
|
|
|
let myobjtrov = getElemById(cat, idobj)
|
|
|
|
|
|
|
|
|
|
if (myobjtrov !== null) {
|
|
|
|
|
let myobjnext = getElemPrevById(cat, myobjtrov._id)
|
|
|
|
|
|
|
|
|
|
if (myobjnext !== null) {
|
|
|
|
|
myobjnext.id_prev = myobjtrov.id_prev
|
|
|
|
|
myobjnext.modified = true
|
|
|
|
|
console.log('calling MODIFY 1')
|
|
|
|
|
await modify(context, { myitem: myobjnext, field: 'id_prev' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1) Delete from the Todos Array
|
|
|
|
|
Todos.mutations.deletemyitem(myobjtrov)
|
|
|
|
|
|
|
|
|
|
// 2) Delete from the IndexedDb
|
|
|
|
|
globalroutines(context, 'delete', 'todos', null, idobj)
|
|
|
|
|
.then((ris) => {
|
|
|
|
|
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
console.log('err: ', error)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 3) Delete from the Server (call)
|
|
|
|
|
deleteItemToSyncAndDb(tools.DB.TABLE_DELETE_TODOS, myobjtrov, idobj)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// console.log('FINE deleteItem')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function insertTodo(context, { myobj, atfirst }) {
|
|
|
|
|
|
|
|
|
|
const objtodo = initcat()
|
|
|
|
|
|
|
|
|
|
objtodo.descr = myobj.descr
|
|
|
|
|
objtodo.category = myobj.category
|
|
|
|
|
|
|
|
|
|
let elemtochange: ITodo = null
|
|
|
|
|
|
|
|
|
|
if (atfirst) {
|
|
|
|
|
console.log('INSERT AT THE TOP')
|
|
|
|
|
elemtochange = getFirstList(objtodo.category)
|
|
|
|
|
objtodo.id_prev = tools.LIST_START
|
|
|
|
|
// objtodo.pos = (elemtochange !== null) ? elemtochange.pos - 1 : 1
|
|
|
|
|
} else {
|
|
|
|
|
console.log('INSERT AT THE BOTTOM')
|
|
|
|
|
// INSERT AT THE BOTTOM , so GET LAST ITEM
|
|
|
|
|
const lastelem = getLastListNotCompleted(objtodo.category)
|
|
|
|
|
|
|
|
|
|
console.log('lastelem', lastelem)
|
|
|
|
|
|
|
|
|
|
objtodo.id_prev = (lastelem !== null) ? lastelem._id : tools.LIST_START
|
|
|
|
|
// objtodo.pos = (elemtochange !== null) ? elemtochange.pos + 1 : 1
|
|
|
|
|
}
|
|
|
|
|
console.log('elemtochange TORNATO:', elemtochange)
|
|
|
|
|
objtodo.modified = false
|
|
|
|
|
|
|
|
|
|
console.log('objtodo', objtodo, 'ID_PREV=', objtodo.id_prev)
|
|
|
|
|
|
|
|
|
|
// 1) Create record in Memory
|
|
|
|
|
Todos.mutations.createNewItem({ objtodo, atfirst, categorySel: objtodo.category })
|
|
|
|
|
|
|
|
|
|
// 2) Insert into the IndexedDb
|
|
|
|
|
const id = await globalroutines(context, 'write', 'todos', objtodo)
|
|
|
|
|
|
|
|
|
|
let field = ''
|
|
|
|
|
// update also the last elem
|
|
|
|
|
if (atfirst) {
|
|
|
|
|
if (elemtochange !== null) {
|
|
|
|
|
elemtochange.id_prev = id
|
|
|
|
|
console.log('elemtochange', elemtochange)
|
|
|
|
|
field = 'id_prev'
|
|
|
|
|
|
|
|
|
|
// Modify the other record
|
|
|
|
|
await modify(context, { myitem: elemtochange, field })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3) send to the Server
|
|
|
|
|
await saveItemToSyncAndDb(tools.DB.TABLE_SYNC_TODOS, 'POST', objtodo)
|
|
|
|
|
.then((ris) => {
|
|
|
|
|
// Check if need to be moved...
|
|
|
|
|
const indelem = getIndexById(objtodo.category, objtodo._id)
|
|
|
|
|
let itemdragend = undefined
|
|
|
|
|
if (atfirst) {
|
|
|
|
|
// Check the second item, if it's different priority, then move to the first position of the priority
|
|
|
|
|
const secondindelem = indelem + 1
|
2019-03-04 17:28:29 +01:00
|
|
|
if (isValidIndex(objtodo.category, secondindelem)) {
|
|
|
|
|
const secondelem = getElemByIndex(objtodo.category, secondindelem)
|
|
|
|
|
if (secondelem.priority !== objtodo.priority) {
|
|
|
|
|
itemdragend = {
|
|
|
|
|
field: 'priority',
|
|
|
|
|
idelemtochange: objtodo._id,
|
|
|
|
|
prioritychosen: objtodo.priority,
|
|
|
|
|
category: objtodo.category,
|
|
|
|
|
atfirst
|
|
|
|
|
}
|
2019-02-27 02:58:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// get previous of the last
|
|
|
|
|
const prevlastindelem = indelem - 1
|
2019-03-04 17:28:29 +01:00
|
|
|
if (isValidIndex(objtodo.category, prevlastindelem)) {
|
|
|
|
|
const prevlastelem = getElemByIndex(objtodo.category, prevlastindelem)
|
|
|
|
|
if (prevlastelem.priority !== objtodo.priority) {
|
|
|
|
|
itemdragend = {
|
|
|
|
|
field: 'priority',
|
|
|
|
|
idelemtochange: objtodo._id,
|
|
|
|
|
prioritychosen: objtodo.priority,
|
|
|
|
|
category: objtodo.category,
|
|
|
|
|
atfirst
|
|
|
|
|
}
|
2019-02-27 02:58:41 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (itemdragend)
|
|
|
|
|
swapElems(context, itemdragend)
|
|
|
|
|
|
|
|
|
|
return ris
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function modify(context, { myitem, field }) {
|
|
|
|
|
if (myitem === null)
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
const myobjsaved = tools.jsonCopy(myitem)
|
|
|
|
|
// get record from IndexedDb
|
|
|
|
|
const miorec = await globalroutines(context, 'read', 'todos', null, myobjsaved._id)
|
|
|
|
|
if (miorec === undefined) {
|
|
|
|
|
console.log('~~~~~~~~~~~~~~~~~~~~ !!!!!!!!!!!!!!!!!! Record not Found !!!!!! id=', myobjsaved._id)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (setmodifiedIfchanged(miorec, myobjsaved, 'completed'))
|
|
|
|
|
miorec.completed_at = new Date().getDate()
|
|
|
|
|
|
|
|
|
|
fieldtochange.forEach(field => {
|
|
|
|
|
setmodifiedIfchanged(miorec, myobjsaved, field)
|
2019-02-01 04:10:31 +01:00
|
|
|
})
|
|
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
if (miorec.modified) {
|
|
|
|
|
// console.log('Todo MODIFICATO! ', miorec.descr, miorec.pos, 'SALVALO SULLA IndexedDB todos')
|
|
|
|
|
miorec.modify_at = new Date().getDate()
|
|
|
|
|
miorec.modified = false
|
|
|
|
|
|
|
|
|
|
// 1) Permit to Update the Views
|
|
|
|
|
tools.notifyarraychanged(miorec)
|
|
|
|
|
|
|
|
|
|
// Todos.mutations.modifymyItem(miorec)
|
|
|
|
|
|
|
|
|
|
// this.logelem('modify', miorec)
|
|
|
|
|
// 2) Modify on IndexedDb
|
|
|
|
|
return globalroutines(context, 'write', 'todos', miorec)
|
|
|
|
|
.then(ris => {
|
|
|
|
|
|
|
|
|
|
// 3) Modify on the Server (call)
|
|
|
|
|
saveItemToSyncAndDb(tools.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async function updateModifyRecords(context, cat: string) {
|
|
|
|
|
//
|
|
|
|
|
// const indcat = getindexbycategory(cat)
|
|
|
|
|
// for (const elem of state.todos[indcat]) {
|
|
|
|
|
// if (elem.modified) {
|
|
|
|
|
// console.log('calling MODIFY 3')
|
|
|
|
|
// await modify(context, { myitem: elem, field })
|
|
|
|
|
// .then(() => {
|
|
|
|
|
// elem.modified = false
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function swapElems(context, itemdragend: IDrag) {
|
|
|
|
|
// console.log('swapElems', itemdragend)
|
|
|
|
|
const cat = itemdragend.category
|
|
|
|
|
const indcat = state.categories.indexOf(cat)
|
|
|
|
|
|
|
|
|
|
if (itemdragend.field === 'priority') {
|
|
|
|
|
// get last elem priority
|
|
|
|
|
itemdragend.newIndex = getLastFirstElemPriority(itemdragend.category, itemdragend.prioritychosen, itemdragend.atfirst, itemdragend.idelemtochange)
|
|
|
|
|
itemdragend.oldIndex = getIndexById(itemdragend.category, itemdragend.idelemtochange)
|
|
|
|
|
|
|
|
|
|
console.log('swapElems PRIORITY', itemdragend)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isValidIndex(cat, indcat) && isValidIndex(cat, itemdragend.newIndex) && isValidIndex(cat, itemdragend.oldIndex)) {
|
|
|
|
|
state.todos[indcat].splice(itemdragend.newIndex, 0, state.todos[indcat].splice(itemdragend.oldIndex, 1)[0])
|
|
|
|
|
tools.notifyarraychanged(state.todos[indcat][itemdragend.newIndex])
|
|
|
|
|
tools.notifyarraychanged(state.todos[indcat][itemdragend.oldIndex])
|
|
|
|
|
|
|
|
|
|
if (itemdragend.field !== 'priority') {
|
|
|
|
|
let precind = itemdragend.newIndex - 1
|
|
|
|
|
let nextind = itemdragend.newIndex + 1
|
|
|
|
|
|
|
|
|
|
if (isValidIndex(cat, precind) && isValidIndex(cat, nextind)) {
|
|
|
|
|
if ((state.todos[indcat][precind].priority === state.todos[indcat][nextind].priority) && (state.todos[indcat][precind].priority !== state.todos[indcat][itemdragend.newIndex].priority)) {
|
|
|
|
|
state.todos[indcat][itemdragend.newIndex].priority = state.todos[indcat][precind].priority
|
|
|
|
|
tools.notifyarraychanged(state.todos[indcat][itemdragend.newIndex])
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!isValidIndex(cat, precind)) {
|
|
|
|
|
if ((state.todos[indcat][nextind].priority !== state.todos[indcat][itemdragend.newIndex].priority)) {
|
|
|
|
|
state.todos[indcat][itemdragend.newIndex].priority = state.todos[indcat][nextind].priority
|
|
|
|
|
tools.notifyarraychanged(state.todos[indcat][itemdragend.newIndex])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if ((state.todos[indcat][precind].priority !== state.todos[indcat][itemdragend.newIndex].priority)) {
|
|
|
|
|
state.todos[indcat][itemdragend.newIndex].priority = state.todos[indcat][precind].priority
|
|
|
|
|
tools.notifyarraychanged(state.todos[indcat][itemdragend.newIndex])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update the id_prev property
|
|
|
|
|
const elem1 = update_idprev(indcat, itemdragend.newIndex, itemdragend.newIndex - 1)
|
|
|
|
|
const elem2 = update_idprev(indcat, itemdragend.newIndex + 1, itemdragend.newIndex)
|
|
|
|
|
const elem3 = update_idprev(indcat, itemdragend.oldIndex, itemdragend.oldIndex - 1)
|
|
|
|
|
const elem4 = update_idprev(indcat, itemdragend.oldIndex + 1, itemdragend.oldIndex)
|
|
|
|
|
|
|
|
|
|
// Update the records:
|
|
|
|
|
await modify(context, { myitem: elem1, field: 'id_prev' })
|
|
|
|
|
await modify(context, { myitem: elem2, field: 'id_prev' })
|
|
|
|
|
await modify(context, { myitem: elem3, field: 'id_prev' })
|
|
|
|
|
await modify(context, { myitem: elem4, field: 'id_prev' })
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
export const actions = {
|
2019-02-03 00:51:58 +01:00
|
|
|
dbInsertTodo: b.dispatch(dbInsertTodo),
|
2019-02-01 04:10:31 +01:00
|
|
|
dbSaveTodo: b.dispatch(dbSaveTodo),
|
|
|
|
|
dbLoadTodo: b.dispatch(dbLoadTodo),
|
2019-02-27 02:58:41 +01:00
|
|
|
dbdeleteItem: b.dispatch(dbdeleteItem),
|
2019-02-15 01:25:44 +01:00
|
|
|
updatefromIndexedDbToStateTodo: b.dispatch(updatefromIndexedDbToStateTodo),
|
2019-02-04 03:09:15 +01:00
|
|
|
checkPendingMsg: b.dispatch(checkPendingMsg),
|
2019-02-22 10:23:00 +01:00
|
|
|
waitAndcheckPendingMsg: b.dispatch(waitAndcheckPendingMsg),
|
2019-02-27 02:58:41 +01:00
|
|
|
swapElems: b.dispatch(swapElems),
|
|
|
|
|
// updateModifyRecords: b.dispatch(updateModifyRecords),
|
|
|
|
|
deleteItem: b.dispatch(deleteItem),
|
|
|
|
|
insertTodo: b.dispatch(insertTodo),
|
|
|
|
|
modify: b.dispatch(modify)
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Module
|
|
|
|
|
const TodosModule = {
|
|
|
|
|
get state() {
|
|
|
|
|
return stateGetter()
|
|
|
|
|
},
|
|
|
|
|
getters: Getters.getters,
|
2019-02-03 14:40:20 +01:00
|
|
|
mutations: Mutations.mutations,
|
2019-01-14 22:40:30 +01:00
|
|
|
actions: Actions.actions
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
export default TodosModule
|