++ Added "Projects" (step 1)

This commit is contained in:
Paolo Arena
2019-03-22 20:51:42 +01:00
parent 4d32467eb1
commit dbd062636b
12 changed files with 790 additions and 490 deletions

View File

@@ -1,6 +1,6 @@
import Api from '@api'
import { ITodo } from '@src/model'
import { GlobalStore, Todos, UserStore } from '@store'
import { GlobalStore, Todos, Projects, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import { serv_constants } from '@src/store/Modules/serv_constants'
import { tools } from '@src/store/Modules/tools'
@@ -268,6 +268,7 @@ async function sendSwMsgIfAvailable() {
}
async function waitAndRefreshData() {
return await Projects.actions.dbLoadProjects({ checkPending: false })
return await Todos.actions.dbLoadTodo({ checkPending: false })
}
@@ -401,3 +402,11 @@ export function table_DeleteRecord(nametable, myobjtrov, id) {
Sync_DeleteItem(nametable, myobjtrov, id)
}
export function getLinkByTableName(nametable) {
if (nametable === 'todos') {
return 'todos'
} else if (nametable === 'projects') {
return 'projects'
}
}

View File

@@ -0,0 +1,364 @@
import { IProject, IProjectsState, IParamTodo, IDrag } from 'model'
import { storeBuilder } from './Store/Store'
import Api from '@api'
import { tools } from './tools'
import * as ApiTables from './ApiTables'
import { GlobalStore, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import objectId from '@src/js/objectId'
import { costanti } from '@src/store/Modules/costanti'
const nametable = 'projs'
// import _ from 'lodash'
const state: IProjectsState = {
showtype: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED,
projs: {},
categories: [],
insidePending: false,
visuLastCompleted: 10
}
const fieldtochange: string [] = ['descr', 'completed', 'category', 'expiring_at', 'priority', 'id_prev', 'pos', 'enableExpiring', 'progress']
const b = storeBuilder.module<IProjectsState>('Projects', state)
const stateGetter = b.state()
function getindexbycategory(category: string) {
return state.categories.indexOf(category)
}
function gettodosByCategory(category: string) {
const indcat = state.categories.indexOf(category)
if (!state.projs[indcat]) {
return []
}
return state.projs[indcat]
}
function isValidIndex(cat, index) {
const myarr = gettodosByCategory(cat)
return (index >= 0 && index < myarr.length)
}
function initcat() {
const tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1)
const objproj: IProject = {
// _id: new Date().toISOString(), // Create NEW
_id: objectId(),
userId: UserStore.state.userId,
descr: '',
priority: tools.Priority.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(objproj)
return objproj
}
namespace Getters {
const projs_dacompletare = b.read((state: IProjectsState) => (cat: string): IProject[] => {
const indcat = getindexbycategory(cat)
if (state.projs[indcat]) {
return state.projs[indcat].filter((proj) => !proj.completed)
} else {
return []
}
}, 'projs_dacompletare')
const projs_completati = b.read((state: IProjectsState) => (cat: string): IProject[] => {
const indcat = getindexbycategory(cat)
if (state.projs[indcat]) {
if (state.showtype === costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED) {
return state.projs[indcat].filter((proj) => proj.completed).slice(0, state.visuLastCompleted)
} // Show only the first N completed
else if (state.showtype === costanti.ShowTypeTask.SHOW_ALL) {
return state.projs[indcat].filter((proj) => proj.completed)
}
else {
return []
}
} else {
return []
}
}, 'projs_completati')
const doneProjectsCount = b.read((state: IProjectsState) => (cat: string): number => {
return getters.projs_completati(cat).length
}, 'doneProjectsCount')
const ProjectsCount = b.read((state: IProjectsState) => (cat: string): number => {
const indcat = getindexbycategory(cat)
if (state.projs[indcat]) {
return state.projs[indcat].length
} else {
return 0
}
}, 'ProjectsCount')
export const getters = {
get projs_dacompletare() {
return projs_dacompletare()
},
get projs_completati() {
return projs_completati()
},
get doneProjectsCount() {
return doneProjectsCount()
},
get ProjectsCount() {
return ProjectsCount()
}
}
}
namespace Mutations {
function findIndTodoById(state: IProjectsState, data: IParamTodo) {
const indcat = state.categories.indexOf(data.categorySel)
if (indcat >= 0) {
return state.projs[indcat].findIndex((elem) => elem._id === data.id)
}
return -1
}
function createNewItem(state: IProjectsState, { objproj, atfirst, categorySel }) {
let indcat = state.categories.indexOf(categorySel)
if (indcat === -1) {
state.categories.push(categorySel)
indcat = state.categories.indexOf(categorySel)
}
console.log('createNewItem', objproj, 'cat=', categorySel, 'state.projs[indcat]', state.projs[indcat])
if (state.projs[indcat] === undefined) {
state.projs[indcat] = []
state.projs[indcat].push(objproj)
console.log('push state.projs[indcat]', state.projs)
return
}
if (atfirst) {
state.projs[indcat].unshift(objproj)
}
else {
state.projs[indcat].push(objproj)
}
console.log('state.projs[indcat]', state.projs[indcat])
}
function deletemyitem(state: IProjectsState, myitem: IProject) {
// Find record
const indcat = state.categories.indexOf(myitem.category)
const ind = findIndTodoById(state, { id: myitem._id, categorySel: myitem.category })
ApiTables.removeitemfromarray(state.projs[indcat], ind)
}
export const mutations = {
deletemyitem: b.commit(deletemyitem),
createNewItem: b.commit(createNewItem)
}
}
namespace Actions {
async function dbLoadProjects(context, { checkPending }) {
console.log('dbLoadProjects', checkPending, 'userid=', UserStore.state.userId)
if (UserStore.state.userId === '') {
return false // Login not made
}
const ris = await Api.SendReq('/projects/' + UserStore.state.userId, 'GET', null)
.then((res) => {
if (res.data.projs) { // console.log('RISULTANTE CATEGORIES DAL SERVER = ', res.data.categories)
state.projs = res.data.projs
state.categories = res.data.categories
} else {
state.projs = [[]]
}
state.showtype = parseInt(GlobalStore.getters.getConfigStringbyId({
id: costanti.CONFIG_ID_SHOW_TYPE_TODOS,
default: costanti.ShowTypeTask.SHOW_LAST_N_COMPLETED
}), 10)
// console.log('ARRAY TODOS = ', state.projs)
if (process.env.DEBUG === '1') {
console.log('dbLoadProjects', 'state.projs', state.projs, 'state.categories', state.categories)
}
return res
})
.catch((error) => {
console.log('error dbLoadProjects', error)
UserStore.mutations.setErrorCatch(error)
return error
})
ApiTables.aftercalling(ris, checkPending, 'categories')
}
async function deleteItem(context, { cat, idobj }) {
console.log('deleteItem: KEY = ', idobj)
const myarr = gettodosByCategory(cat)
const myobjtrov = tools.getElemById(myarr, idobj)
console.log('myobjtrov', myobjtrov.descr)
if (!!myobjtrov) {
const myobjnext = tools.getElemPrevById(myarr, myobjtrov._id)
if (!!myobjnext) {
myobjnext.id_prev = myobjtrov.id_prev
myobjnext.modified = true
await modify(context, { myitem: myobjnext, field: 'id_prev' })
}
ApiTables.table_DeleteRecord(nametable, myobjtrov, idobj)
}
}
async function insertProject(context, { myobj, atfirst }) {
const objproj = initcat()
objproj.descr = myobj.descr
objproj.category = myobj.category
let elemtochange: IProject = null
const myarr = gettodosByCategory(objproj.category)
if (atfirst) {
console.log('INSERT AT THE TOP')
elemtochange = tools.getFirstList(myarr)
objproj.id_prev = ApiTables.LIST_START
} else {
console.log('INSERT AT THE BOTTOM')
// INSERT AT THE BOTTOM , so GET LAST ITEM
const lastelem = tools.getLastListNotCompleted(nametable, objproj.category)
objproj.id_prev = (!!lastelem) ? lastelem._id : ApiTables.LIST_START
}
objproj.modified = false
Mutations.mutations.createNewItem({ objproj, atfirst, categorySel: objproj.category }) // 1) Create record in Memory
const id = await globalroutines(context, 'write', nametable, objproj) // 2) Insert into the IndexedDb
let field = ''
if (atfirst) { // update also the last elem
if (!!elemtochange) {
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
return await ApiTables.Sync_SaveItem(nametable, 'POST', objproj)
.then((ris) => {
// *** Check if need to be moved because of the --- Priority Ordering --- ...
const indelem = tools.getIndexById(myarr, objproj._id)
let itemdragend
if (atfirst) {
// Check the second item, if it's different priority, then move to the first position of the priority
const secondindelem = indelem + 1
if (tools.isOkIndex(myarr, secondindelem)) {
const secondelem = tools.getElemByIndex(myarr, secondindelem)
if (secondelem.priority !== objproj.priority) {
itemdragend = {
field: 'priority',
idelemtochange: objproj._id,
prioritychosen: objproj.priority,
category: objproj.category,
atfirst
}
}
}
} else {
// get previous of the last
const prevlastindelem = indelem - 1
if (tools.isOkIndex(myarr, prevlastindelem)) {
const prevlastelem = tools.getElemByIndex(myarr, prevlastindelem)
if (prevlastelem.priority !== objproj.priority) {
itemdragend = {
field: 'priority',
idelemtochange: objproj._id,
prioritychosen: objproj.priority,
category: objproj.category,
atfirst
}
}
}
}
if (itemdragend) {
swapElems(context, itemdragend)
}
return ris
})
}
async function modify(context, { myitem, field }) {
return await ApiTables.table_ModifyRecord(nametable, myitem, fieldtochange)
}
async function swapElems(context, itemdragend: IDrag) {
console.log('swapElems', itemdragend)
console.log('state.projs', state.projs)
console.log('state.categories', state.categories)
const cat = itemdragend.category
const indcat = state.categories.indexOf(cat)
const myarr = state.projs[indcat]
tools.swapGeneralElem(nametable, myarr, itemdragend, fieldtochange)
}
export const actions = {
dbLoadProjects: b.dispatch(dbLoadProjects),
swapElems: b.dispatch(swapElems),
deleteItem: b.dispatch(deleteItem),
insertProject: b.dispatch(insertProject),
modify: b.dispatch(modify)
}
}
// Module
const ProjectsModule = {
get state() {
return stateGetter()
},
getters: Getters.getters,
mutations: Mutations.mutations,
actions: Actions.actions
}
export default ProjectsModule

View File

@@ -44,102 +44,6 @@ function gettodosByCategory(category: string) {
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)
return myarr.find((elem) => elem._id === id)
}
function getIndexById(cat, id) {
const myarr = gettodosByCategory(cat)
return myarr.findIndex((elem) => elem._id === id)
}
function getElemPrevById(cat, id) {
const myarr = gettodosByCategory(cat)
return myarr.find((elem) => elem.id_prev === id)
}
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.Priority.PRIORITY_LOW) {
return myarr.length - 1
}
else if (priority === tools.Priority.PRIORITY_HIGH) {
return 0
}
}
}
function getFirstList(cat) {
const myarr = gettodosByCategory(cat)
return myarr.find((elem) => elem.id_prev === ApiTables.LIST_START)
}
function getLastListNotCompleted(cat) {
const arr = Todos.getters.todos_dacompletare(cat)
return (arr.length > 0) ? arr[arr.length - 1] : 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 : ApiTables.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() {
const tomorrow = new Date()
@@ -313,12 +217,15 @@ namespace Actions {
async function deleteItem(context, { cat, idobj }) {
console.log('deleteItem: KEY = ', idobj)
const myobjtrov = getElemById(cat, idobj)
const myarr = gettodosByCategory(cat)
const myobjtrov = tools.getElemById(myarr, idobj)
console.log('myobjtrov', myobjtrov.descr)
if (!!myobjtrov) {
const myobjnext = getElemPrevById(cat, myobjtrov._id)
const myobjnext = tools.getElemPrevById(myarr, myobjtrov._id)
if (!!myobjnext) {
myobjnext.id_prev = myobjtrov.id_prev
@@ -339,28 +246,24 @@ namespace Actions {
let elemtochange: ITodo = null
const myarr = gettodosByCategory(objtodo.category)
if (atfirst) {
console.log('INSERT AT THE TOP')
elemtochange = getFirstList(objtodo.category)
elemtochange = tools.getFirstList(myarr)
objtodo.id_prev = ApiTables.LIST_START
} else {
console.log('INSERT AT THE BOTTOM')
// INSERT AT THE BOTTOM , so GET LAST ITEM
const lastelem = getLastListNotCompleted(objtodo.category)
console.log('lastelem', lastelem)
const lastelem = tools.getLastListNotCompleted(nametable, objtodo.category)
objtodo.id_prev = (!!lastelem) ? lastelem._id : ApiTables.LIST_START
}
objtodo.modified = false
// console.log('objtodo', objtodo, 'ID_PREV=', objtodo.id_prev)
Todos.mutations.createNewItem({ objtodo, atfirst, categorySel: objtodo.category }) // 1) Create record in Memory
// 1) Create record in Memory
Todos.mutations.createNewItem({ objtodo, atfirst, categorySel: objtodo.category })
// 2) Insert into the IndexedDb
const id = await globalroutines(context, 'write', nametable, objtodo)
const id = await globalroutines(context, 'write', nametable, objtodo) // 2) Insert into the IndexedDb
let field = ''
if (atfirst) { // update also the last elem
@@ -377,14 +280,15 @@ namespace Actions {
// 3) send to the Server
return await ApiTables.Sync_SaveItem(nametable, 'POST', objtodo)
.then((ris) => {
// Check if need to be moved...
const indelem = getIndexById(objtodo.category, objtodo._id)
// *** Check if need to be moved because of the --- Priority Ordering --- ...
const indelem = tools.getIndexById(myarr, objtodo._id)
let itemdragend
if (atfirst) {
// Check the second item, if it's different priority, then move to the first position of the priority
const secondindelem = indelem + 1
if (isValidIndex(objtodo.category, secondindelem)) {
const secondelem = getElemByIndex(objtodo.category, secondindelem)
if (tools.isOkIndex(myarr, secondindelem)) {
const secondelem = tools.getElemByIndex(myarr, secondindelem)
if (secondelem.priority !== objtodo.priority) {
itemdragend = {
field: 'priority',
@@ -399,8 +303,8 @@ namespace Actions {
} else {
// get previous of the last
const prevlastindelem = indelem - 1
if (isValidIndex(objtodo.category, prevlastindelem)) {
const prevlastelem = getElemByIndex(objtodo.category, prevlastindelem)
if (tools.isOkIndex(myarr, prevlastindelem)) {
const prevlastelem = tools.getElemByIndex(myarr, prevlastindelem)
if (prevlastelem.priority !== objtodo.priority) {
itemdragend = {
field: 'priority',
@@ -429,67 +333,12 @@ namespace Actions {
console.log('swapElems', itemdragend)
console.log('state.todos', state.todos)
console.log('state.categories', state.categories)
const cat = itemdragend.category
const indcat = state.categories.indexOf(cat)
const myarr = state.todos[indcat]
if (itemdragend.field === 'priority') {
// get last elem priority
console.log('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)
}
console.log('indcat', indcat)
if (isValidIndex(cat, indcat) && isValidIndex(cat, itemdragend.newIndex) && isValidIndex(cat, itemdragend.oldIndex)) {
console.log('isValidIndex')
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') {
const precind = itemdragend.newIndex - 1
const 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)) {
console.log(' 1)')
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)) {
console.log(' 2)')
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)) {
console.log(' 3)')
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' })
}
tools.swapGeneralElem(nametable, myarr, itemdragend, fieldtochange)
}

View File

@@ -2,3 +2,4 @@ export {storeBuilder} from './Store/Store'
export {default as GlobalStore} from './GlobalStore'
export {default as UserStore} from './UserStore'
export {default as Todos} from './Todos'
export {default as Projects} from './Projects'

View File

@@ -1,7 +1,9 @@
import { UserStore } from '@store'
import { Todos, Projects, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import { costanti } from './costanti'
import Quasar from 'quasar'
import { ITodo } from '@src/model'
import * as ApiTables from '@src/store/Modules/ApiTables'
export interface INotify {
color?: string | 'primary'
@@ -346,6 +348,66 @@ export const tools = {
]
},
getTitlePriority(priority) {
let cl = ''
if (priority === tools.Priority.PRIORITY_HIGH) {
cl = 'high_priority'
}
else if (priority === tools.Priority.PRIORITY_NORMAL) {
cl = 'medium_priority'
}
else if (priority === tools.Priority.PRIORITY_LOW) {
cl = 'low_priority'
}
return cl + ' titlePriority'
},
getPriorityByInd(index) {
// console.log('LANG in PRIOR', UserStore.state.lang)
try {
const arr = tools.selectPriority[UserStore.state.lang]
for (const rec of arr) {
if (rec.value === index) {
return rec.label
}
}
} catch (e) {
console.log('Error: ', e)
}
return ''
},
logelem(mystr, elem) {
console.log(mystr, 'elem [', elem._id, '] ', elem.descr, ' Pr(', tools.getPriorityByInd(elem.priority), ') [', elem.id_prev, '] modif=', elem.modified)
},
getstrelem(elem) {
return 'elem [' + elem._id + '] ' + elem.descr + ' Pr(' + tools.getPriorityByInd(elem.priority) + ') [ID_PREV=' + elem.id_prev + '] modif=' + elem.modified + ' '
},
logga_arr(myarr: ITodo[]) {
let mystr = '\n'
myarr.forEach((item) => {
mystr += '[' + item.pos + '] ' + item.descr + ' Pr(' + tools.getPriorityByInd(item.priority) + ') [' + item.id_prev + '] modif=' + item.modified + '\n'
// mystr += '[' + item.pos + '] ' + item.descr + '\n'
})
return mystr
},
touchmove(scrollable) {
if (window) {
window.addEventListener('touchmove', (e) => {
// console.log('touchmove')
if (!scrollable) {
e.preventDefault()
}
}, { passive: false })
}
},
jsonCopy(src) {
return JSON.parse(JSON.stringify(src))
},
@@ -365,6 +427,156 @@ export const tools = {
}
},
isOkIndex(myarr, index) {
return (index >= 0 && index < myarr.length)
},
update_idprev(myarr, indelemchange, indelemId) {
if (indelemchange >= 0 && indelemchange < myarr.length) {
const id_prev = (indelemId >= 0) ? myarr[indelemId]._id : ApiTables.LIST_START
if (myarr[indelemchange].id_prev !== id_prev) {
myarr[indelemchange].id_prev = id_prev
tools.notifyarraychanged(myarr[indelemchange])
// myarr[indelemchange].modified = true
console.log('Index=', indelemchange, 'indtoget', indelemId, tools.getstrelem(myarr[indelemchange]))
return myarr[indelemchange]
}
}
return null
},
async swapGeneralElem(nametable, myarr, itemdragend, fieldtochange) {
if (itemdragend.field === 'priority') {
// get last elem priority
console.log('get last elem priority')
itemdragend.newIndex = tools.getLastFirstElemPriority(itemdragend.category, itemdragend.prioritychosen, itemdragend.atfirst, itemdragend.idelemtochange)
itemdragend.oldIndex = tools.getIndexById(itemdragend.category, itemdragend.idelemtochange)
console.log('swapElems PRIORITY', itemdragend)
}
if (tools.isOkIndex(myarr, itemdragend.newIndex) && tools.isOkIndex(myarr, itemdragend.oldIndex)) {
myarr.splice(itemdragend.newIndex, 0, myarr.splice(itemdragend.oldIndex, 1)[0])
tools.notifyarraychanged(myarr[itemdragend.newIndex])
tools.notifyarraychanged(myarr[itemdragend.oldIndex])
if (itemdragend.field !== 'priority') {
const precind = itemdragend.newIndex - 1
const nextind = itemdragend.newIndex + 1
if (tools.isOkIndex(myarr, precind) && tools.isOkIndex(myarr, nextind)) {
if ((myarr[precind].priority === myarr[nextind].priority) && (myarr[precind].priority !== myarr[itemdragend.newIndex].priority)) {
// console.log(' 1)')
myarr[itemdragend.newIndex].priority = myarr[precind].priority
tools.notifyarraychanged(myarr[itemdragend.newIndex])
}
} else {
if (!tools.isOkIndex(myarr, precind)) {
if ((myarr[nextind].priority !== myarr[itemdragend.newIndex].priority)) {
// console.log(' 2)')
myarr[itemdragend.newIndex].priority = myarr[nextind].priority
tools.notifyarraychanged(myarr[itemdragend.newIndex])
}
} else {
if ((myarr[precind].priority !== myarr[itemdragend.newIndex].priority)) {
console.log(' 3)')
myarr[itemdragend.newIndex].priority = myarr[precind].priority
tools.notifyarraychanged(myarr[itemdragend.newIndex])
}
}
}
}
// Update the id_prev property
const elem1 = tools.update_idprev(myarr, itemdragend.newIndex, itemdragend.newIndex - 1)
const elem2 = tools.update_idprev(myarr, itemdragend.newIndex + 1, itemdragend.newIndex)
const elem3 = tools.update_idprev(myarr, itemdragend.oldIndex, itemdragend.oldIndex - 1)
const elem4 = tools.update_idprev(myarr, itemdragend.oldIndex + 1, itemdragend.oldIndex)
await ApiTables.table_ModifyRecord(nametable, elem1, fieldtochange)
await ApiTables.table_ModifyRecord(nametable, elem2, fieldtochange)
await ApiTables.table_ModifyRecord(nametable, elem3, fieldtochange)
await ApiTables.table_ModifyRecord(nametable, elem4, fieldtochange)
// Update the records:
}
},
getIndexById(myarr, id) {
return myarr.findIndex((elem) => elem._id === id)
},
getElemById(myarr, id) {
return myarr.find((elem) => elem._id === id)
},
getElemPrevById(myarr, id) {
return myarr.find((elem) => elem.id_prev === id)
},
getLastFirstElemPriority(myarr, priority: number, atfirst: boolean, escludiId: string) {
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.Priority.PRIORITY_LOW) {
return myarr.length - 1
}
else if (priority === tools.Priority.PRIORITY_HIGH) {
return 0
}
}
},
getFirstList(myarr) {
return myarr.find((elem) => elem.id_prev === ApiTables.LIST_START)
},
getLastListNotCompleted(nametable, cat) {
let arr
if (nametable === 'todos') {
arr = Todos.getters.todos_dacompletare(cat)
} else if (nametable === 'projects') {
arr = Projects.getters.projs_dacompletare(cat)
}
return (arr.length > 0) ? arr[arr.length - 1] : null
},
getElemByIndex(myarr, index) {
if (index >= 0 && index < myarr.length) {
return myarr[index]
}
else {
return null
}
},
existArr(x) {
return x = (typeof x !== 'undefined' && x instanceof Array) ? x : []
},
@@ -378,7 +590,7 @@ export const tools = {
return result
},
showNotif(q: any, msg, data?: INotify | null) {
showNotif(q: any, msg, data ?: INotify | null) {
let myicon = data ? data.icon : 'ion-add'
if (!myicon) {
myicon = 'ion-add'
@@ -396,6 +608,26 @@ export const tools = {
})
},
isRegistered() {
return localStorage.getItem(tools.localStorage.userId) !== ''
},
checkIfUserExist(mythis) {
if (UserStore.state.userId === undefined) {
tools.showNotif(mythis.$q, mythis.$t('todo.usernotdefined'))
return false
}
if (!tools.isRegistered()) {
// Not logged
tools.showNotif(mythis.$q, mythis.$t('user.notregistered'))
return false
}
return true
},
checkLangPassed(mylang) {
const mybrowserLang = Quasar.lang.isoName
@@ -408,7 +640,6 @@ export const tools = {
mylang = 'es'
}
if (!(tools.arrLangUsed.includes(mylang))) {
console.log('non incluso ', mylang)
mylang = tools.arrLangUsed[0]
@@ -434,6 +665,38 @@ export const tools = {
globalroutines(null, 'log', strlog + ' ' + strlog2 + ' ' + strlog3, null)
},
/*
get todos_vista() {
let mystr = ''
const arr = Todos.getters.todos_dacompletare(this.categoryAtt)
for (const ind in arr) {
mystr += this.getstrelem(arr[ind]) + '\n'
}
return mystr + ''
}
*/
/*
public getArrTodos() {
let mystr = ''
return globalroutines(null, 'readall', 'todos', null)
.then((alldata) => {
const myrecs = [...alldata]
myrecs.forEach((rec) => {
mystr = mystr + rec.descr + rec.completed + '] ['
})
// this.tmpstrTodos = 'TODOS: ' + mystr
})
}
*/
aspettansec(numsec) {
return new Promise((resolve, reject) => {
setTimeout(() => {
@@ -458,11 +721,11 @@ export const tools = {
})
},
// _.cloneDeep( Per clonare un oggetto
isLoggedToSystem() {
const tok = tools.getItemLS(tools.localStorage.token)
return !!tok
}
// _.cloneDeep( Per clonare un oggetto
}