++ 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

@@ -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)
}