mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-12-05 14:36:36 +01:00
Adding or editing a transaction used to fire "updated", which re-rendered the whole list. Saves now send "transactions_changed" with the saved ids. Lists with the transaction_mover behavior ask their own endpoint for just those rows (?only=<id>), and the server answers with where each row goes: the row after it and, for lists split by date, its divider. Rows it doesn't return have left the list and are removed. Covers the monthly overview, all transactions (a row joining or leaving the current page still reloads it), and the installment, recurring and calendar lists. Pages without a mover fall back to "updated".
214 lines
7.5 KiB
HTML
214 lines
7.5 KiB
HTML
<script type="text/hyperscript">
|
|
-- Removes deleted transactions from the lists without reloading them.
|
|
--
|
|
-- The delete endpoints answer with a `transactions_deleted` HX-Trigger
|
|
-- carrying the affected ids instead of firing `updated`, which every
|
|
-- transaction list listens to and would answer with a full re-render.
|
|
--
|
|
-- transaction_remover is installed once on the body wrapper so a page with
|
|
-- hundreds of rows still only holds a single listener. Once the rows are
|
|
-- gone it announces transactions_removed, and every divider decides for
|
|
-- itself whether it still has anything to show.
|
|
|
|
behavior transaction_remover
|
|
on transactions_deleted from window
|
|
set ids to event.detail.ids
|
|
if no ids then exit end
|
|
|
|
set rows to []
|
|
set lists to []
|
|
repeat for row in <.transaction[data-transaction-id]/>
|
|
if ids.includes(the @data-transaction-id of row)
|
|
append row to rows
|
|
set list to the closest <#transactions-list/> to row
|
|
if list and not lists.includes(list) then append list to lists end
|
|
end
|
|
end
|
|
if rows.length is 0 then exit end
|
|
|
|
-- freeze the current height so it has somewhere to animate from
|
|
repeat for row in rows
|
|
set *height of row to `${row.offsetHeight}px`
|
|
set *overflow of row to 'hidden'
|
|
end
|
|
|
|
repeat for row in rows
|
|
get row.offsetHeight -- force a reflow so the height above is the start
|
|
add .transaction-removing to row
|
|
set *height of row to '0px'
|
|
end
|
|
|
|
-- `wait for transitionend` can only watch `me`, which here is the wrapper,
|
|
-- so the rows get a plain wait. Keep it just above the .transaction-removing
|
|
-- duration in _animations.scss.
|
|
wait 300ms
|
|
|
|
repeat for row in rows
|
|
remove row
|
|
end
|
|
|
|
trigger transactions_removed on window
|
|
|
|
repeat for list in lists
|
|
send change to list -- the selection action bar recounts what's left
|
|
set remaining to <.transaction/> in list
|
|
if remaining.length is 0
|
|
-- nothing left to show, let the server render the empty state
|
|
trigger updated on window
|
|
end
|
|
end
|
|
|
|
-- Saved transactions only get moved in place by a list with
|
|
-- transaction_mover, everywhere else the page reloads like before
|
|
on transactions_changed from window
|
|
if no <[data-moves-transactions]/> then trigger updated on window end
|
|
end
|
|
|
|
-- Puts added or edited transactions in place without reloading the list.
|
|
--
|
|
-- The add and edit endpoints answer with `transactions_changed` and the ids
|
|
-- they saved. The list asks its own endpoint (hx-get, with its hx-include
|
|
-- values) for just those rows with ?only=. The server answers with each row
|
|
-- it still shows plus the row that comes after it (and for a list split by
|
|
-- date, the row's divider and the divider after that), so where a row goes is
|
|
-- always decided by the server's ordering. Rows it doesn't answer with have
|
|
-- left the list (filter, month) and go away.
|
|
-- See apps/transactions/utils/changed_rows.py
|
|
--
|
|
-- With data-moves-transactions="page" the list is one page of many: a row
|
|
-- that joins or leaves it shifts every page after it, so that reloads instead.
|
|
behavior transaction_mover
|
|
on transactions_changed from window
|
|
set ids to event.detail.ids
|
|
if me.matches('#transactions-list')
|
|
set list to me
|
|
else
|
|
set list to the first <#transactions-list/> in me
|
|
end
|
|
if no ids or no list then exit end
|
|
if no <.transaction/> in list
|
|
-- the empty state is server-rendered
|
|
trigger updated on window
|
|
exit
|
|
end
|
|
|
|
make a URLSearchParams called params
|
|
if my @hx-include
|
|
repeat for source in document.querySelectorAll(my @hx-include)
|
|
if source.matches('form')
|
|
make a FormData from source called fields
|
|
repeat for pair in Array.from(fields.entries())
|
|
call params.append(pair[0], pair[1])
|
|
end
|
|
else if source.name
|
|
call params.set(source.name, source.value)
|
|
end
|
|
end
|
|
end
|
|
repeat for id in ids
|
|
call params.append('only', id)
|
|
end
|
|
call fetch(`${my @hx-get}?${params}`, {headers: {'HX-Request': 'true'}})
|
|
set res to it
|
|
if not res.ok
|
|
trigger updated on window
|
|
exit
|
|
end
|
|
call res.text()
|
|
set markup to it
|
|
|
|
make a <template/> called holder
|
|
set holder.innerHTML to markup
|
|
set entries to Array.from(holder.content.children)
|
|
|
|
set shown to []
|
|
repeat for row in <.transaction[data-transaction-id]/> in list
|
|
if ids.includes(the @data-transaction-id of row) then append row to shown end
|
|
end
|
|
|
|
if my @data-moves-transactions is 'page'
|
|
set shownIds to shown.map(\ r -> r.dataset.transactionId).sort().join()
|
|
set answeredIds to entries.map(\ e -> e.content.querySelector('.transaction').dataset.transactionId).sort().join()
|
|
if shownIds is not answeredIds
|
|
trigger updated on window
|
|
exit
|
|
end
|
|
end
|
|
|
|
repeat for row in shown
|
|
remove row
|
|
end
|
|
|
|
-- last first, so the row each one goes before is already in place
|
|
repeat for entry in entries.reverse()
|
|
set node to entry.content.firstElementChild
|
|
set nextRow to null
|
|
if entry.dataset.nextId
|
|
set nextRow to list.querySelector(`.transaction[data-transaction-id="${entry.dataset.nextId}"]`)
|
|
end
|
|
|
|
if node.matches('.transaction')
|
|
-- a list without dividers
|
|
if nextRow
|
|
call nextRow.before(node)
|
|
else
|
|
set rows to list.querySelectorAll('.transaction')
|
|
if rows.length
|
|
call rows[rows.length - 1].after(node)
|
|
else
|
|
call list.prepend(node)
|
|
end
|
|
end
|
|
else
|
|
set existing to list.querySelector(`#${CSS.escape(node.id)}`)
|
|
if existing
|
|
set node to node.querySelector('.transaction')
|
|
if nextRow and existing.contains(nextRow)
|
|
call nextRow.before(node)
|
|
else
|
|
call existing.querySelector('.transactions-divider-collapse > .flex').append(node)
|
|
end
|
|
else
|
|
set nextDivider to null
|
|
if entry.dataset.nextGroup
|
|
set nextDivider to list.querySelector(`#${CSS.escape(entry.dataset.nextGroup)}`)
|
|
end
|
|
set dividers to list.querySelectorAll(':scope > .transactions-divider')
|
|
if nextDivider
|
|
call nextDivider.before(node)
|
|
else if dividers.length
|
|
call dividers[dividers.length - 1].after(node)
|
|
else
|
|
call list.prepend(node)
|
|
end
|
|
end
|
|
end
|
|
call htmx.process(node)
|
|
call _hyperscript.processNode(node)
|
|
end
|
|
|
|
-- dividers left empty collapse themselves
|
|
trigger transactions_removed on window
|
|
send change to list -- the selection action bar recounts what's left
|
|
if no <.transaction/> in list
|
|
trigger updated on window
|
|
else
|
|
trigger selective_update on window -- totals and summaries
|
|
end
|
|
end
|
|
|
|
behavior transaction_divider
|
|
on transactions_removed from window
|
|
set remaining to <.transaction/> in me
|
|
if remaining.length is not 0 then exit end
|
|
|
|
set *height of me to `${my offsetHeight}px`
|
|
set *overflow of me to 'hidden'
|
|
get my offsetHeight
|
|
add .transaction-removing to me
|
|
set *height of me to '0px'
|
|
wait for transitionend or 1s
|
|
remove me
|
|
end
|
|
</script>
|