initial $move implementation (#249)

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-10-09 10:54:08 +02:00 committed by GitHub
parent 1eb5ff15f8
commit 4f4d2f8e48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 21 deletions

View File

@ -47,6 +47,16 @@ function $pull (document: Doc, keyval: Record<string, PropertyType>): void {
} }
} }
function $move (document: Doc, keyval: Record<string, PropertyType>): void {
const doc = document as any
for (const key in keyval) {
const arr = doc[key] as Array<any>
const desc = keyval[key]
doc[key] = arr.filter(val => val !== desc.$value)
doc[key].splice(desc.$position, 0, desc.$value)
}
}
function $pushMixin (document: Doc, options: any): void { function $pushMixin (document: Doc, options: any): void {
const doc = document as any const doc = document as any
const mixinId = options.$mixin const mixinId = options.$mixin
@ -74,6 +84,7 @@ function $inc (document: Doc, keyval: Record<string, number>): void {
const operators: Record<string, _OperatorFunc> = { const operators: Record<string, _OperatorFunc> = {
$push, $push,
$pull, $pull,
$move,
$pushMixin, $pushMixin,
$inc $inc
} }

View File

@ -63,49 +63,52 @@
return x return x
} }
const statesQuery = createQuery() const statesQuery = createQuery()
$: if (_space) statesQuery.query(core.class.State, { _id: { $in: _space.states } }, result => { states = sort(result); console.log('states', sort(result)) }) $: statesQuery.query(core.class.State, { _id: { $in: _space?.states ?? [] } }, result => { states = sort(result) })
const query = createQuery() const query = createQuery()
$: if (kanban) query.query(_class, { space }, result => { objects = sortObjects(result) }, options) $: query.query(_class, { space }, result => { objects = sortObjects(result) }, options)
function dragover(ev: MouseEvent, object: Doc) { function dragover(ev: MouseEvent, object: Doc) {
// if (dragswap(ev, i)) {
if (dragCard !== object) { if (dragCard !== object) {
const dragover = objects.indexOf(object) const dragover = objects.indexOf(object)
const dragging = objects.indexOf(dragCard) objects = objects.filter(x => x !== dragCard)
objects[dragover] = dragCard objects = [...objects.slice(0, dragover), dragCard, ...objects.slice(dragover)]
objects[dragging] = object
} }
} }
let currentOp: Promise<void> | undefined
async function move(to: number, state: Ref<State>) { async function move(to: number, state: Ref<State>) {
console.log('move version 12') console.log('INITIAL', dragCardInitialPosition, 'TO', to)
const id = dragCard._id const id = dragCard._id
const txes: TxCUD<Doc>[] = [] const txes: TxCUD<Doc>[] = []
if (dragCardInitialState !== state) if (dragCardInitialState !== state) {
client.updateDoc(_class, space, id, { state }) client.updateDoc(_class, space, id, { state })
// txes.push(client.txFactory.createTxUpdateDoc(_class, space, id, { state }))
}
if (dragCardInitialPosition !== to) { if (dragCardInitialPosition !== to) {
await client.updateDoc(view.class.Kanban, space, kanban._id, {
$pull: {
order: id
}
})
client.updateDoc(view.class.Kanban, space, kanban._id, { client.updateDoc(view.class.Kanban, space, kanban._id, {
$push: { $move: {
order: { order: {
$each: [id], $value: id,
$position: to $position: to
} }
} }
}) })
// txes.push(client.txFactory.createTxUpdateDoc(view.class.Kanban, space, kanban._id, {
// $move: {
// order: {
// $value: id,
// $position: to
// }
// }
// }))
// await client.updateDoc(core.class.SpaceWithStates, core.space.Model, space, { // await client.updateDoc(core.class.SpaceWithStates, core.space.Model, space, {
// $pull: { // $pull: {
// order: id // order: id
@ -124,7 +127,10 @@
// if (txes.length > 0) { // if (txes.length > 0) {
// const updateTx = client.txFactory.createTxBulkWrite(space, txes) // const updateTx = client.txFactory.createTxBulkWrite(space, txes)
// await client.tx(updateTx) // if (currentOp) {
// await currentOp
// }
// currentOp = client.tx(updateTx).then(() => console.log('move done')).catch(err => console.log('move error ' + err))
// } // }
} }
@ -175,6 +181,7 @@
<KanbanCardEmpty label={'Create new application'} /> <KanbanCardEmpty label={'Create new application'} />
{#each objects as object, j} {#each objects as object, j}
{#if object.state === state._id} {#if object.state === state._id}
{j}
<div <div
on:dragover|preventDefault={(ev) => { on:dragover|preventDefault={(ev) => {
dragover(ev, object) dragover(ev, object)

View File

@ -133,8 +133,45 @@ class MongoAdapter extends MongoAdapterBase {
protected override async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> { protected override async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<void> {
const domain = this.hierarchy.getDomain(tx.objectClass) const domain = this.hierarchy.getDomain(tx.objectClass)
const op = isOperator(tx.operations) ? tx.operations : { $set: tx.operations } if (isOperator(tx.operations)) {
await this.db.collection(domain).updateOne({ _id: tx.objectId }, op) const operator = Object.keys(tx.operations)[0]
if (operator === '$move') {
const keyval = (tx.operations as any).$move
const arr = Object.keys(keyval)[0]
const desc = keyval[arr]
const ops = [
{
updateOne: {
filter: { _id: tx.objectId },
update: {
$pull: {
[arr]: desc.$value
}
}
}
},
{
updateOne: {
filter: { _id: tx.objectId },
update: {
$push: {
[arr]: {
$each: [desc.$value],
$position: desc.$position
}
}
}
}
}
]
console.log('ops', ops)
await this.db.collection(domain).bulkWrite(ops as any)
} else {
await this.db.collection(domain).updateOne({ _id: tx.objectId }, tx.operations)
}
} else {
await this.db.collection(domain).updateOne({ _id: tx.objectId }, { $set: tx.operations })
}
} }
override tx (tx: Tx): Promise<void> { override tx (tx: Tx): Promise<void> {