Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete single item #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/capacity-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ const syncChartToAnchor = canvas => {
'a'
).href = `data:image/svg+xml;base64,\n${window.btoa(canvas.innerHTML)}`
})

const removeByIndexButtons = document.querySelectorAll(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qui spezzerei in due la gestione. Sul componente Board gestirei il click sulle "x", la board poi emette un evento custom che index.js legge e invoca il cambio di dati. Inoltre a cosa ti serve l'attributo listner-added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mi torna, listner-added mi serve per non aggiungere N volte il listner

'text[data-remove-by-index]'
)
removeByIndexButtons.forEach((deleteButton, i) => {
if (deleteButton.getAttribute('listner-added') === 'false') {
deleteButton.setAttribute('listner-added', 'true')
deleteButton.addEventListener('click', e => onRemoveItemClick(e, canvas))
}
})
}

const onRemoveItemClick = (e, canvas) => {
const newData = [...canvas.data]
const indexToRemove = e.target.getAttribute('data-remove-by-index')
newData.splice(indexToRemove, 1)
canvas.data = newData
}

const onAddClick = (canvas, label) => {
Expand Down
22 changes: 19 additions & 3 deletions apps/options-board/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ if (!window.location.hash) {
const channel = window.location.hash.substr(1)
let board

const syncChartToAnchor = board => {
const removePostit = e => {
const indexToRemove = e.target.getAttribute('data-remove-by-index')
optionsBoardData.removeOptionByIndex(board, indexToRemove)
}

const syncChart = board => {
window.requestIdleCallback(() => {
document.querySelector(
'a'
).href = `data:image/svg+xml;base64,\n${window.btoa(board.innerHTML)}`
})

const removeByIndexButtons = document.querySelectorAll(
'text[data-remove-by-index]'
)

removeByIndexButtons.forEach((deleteButton, i) => {
if (deleteButton.getAttribute('listner-added') === 'false') {
deleteButton.setAttribute('listner-added', 'true')
deleteButton.addEventListener('click', removePostit)
}
})
}

optionsBoardData.init(channel).then(_ => {
Expand Down Expand Up @@ -49,14 +65,14 @@ optionsBoardData.init(channel).then(_ => {
})

createAttributesObserver(board, () => {
syncChartToAnchor(board)
syncChart(board)
})
})
})

const syncData = data => {
board.data = data
window.requestAnimationFrame(() => syncChartToAnchor(board))
window.requestAnimationFrame(() => syncChart(board))
}

optionsBoardData.addOnChangeListener(syncData)
9 changes: 9 additions & 0 deletions apps/options-board/model/optionsBoardData.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export const factory = ({
set(board.data)
}

const removeOptionByIndex = (board, i) => {
const newData = [...board.data]
newData.splice(i, 1)
board.data = newData

set(board.data)
}

const reset = () => {
set(initialData)
return initialData
Expand Down Expand Up @@ -83,6 +91,7 @@ export const factory = ({
get,
addOption,
removeOption,
removeOptionByIndex,
changePosition,
reset
}
Expand Down
21 changes: 21 additions & 0 deletions lib/postIt/nodeFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ const createTextNode = (width, height, label, index) => {
return textNode
}

const createDeleteNode = (width, height, label, index, color) => {
const textNode = document.createElementNS(SVG_NS_URI, 'text')

textNode.setAttribute('alignment-baseline', 'middle')
textNode.setAttribute('text-anchor', 'middle')
textNode.setAttribute('x', width)
textNode.setAttribute('y', height)
textNode.setAttribute('font-size', '18')
textNode.setAttribute('font-weight', 'bold')
textNode.setAttribute('cursor', 'default')
textNode.setAttribute('data-index', index)
textNode.setAttribute('data-remove-by-index', index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qui cambierei la gestione degli attributi. Dato che abbiamo già il data-index per indicare a quale post-it si far riferimento non aggiungerei due volte lo stesso valore. Ma dato che questo attributo ci fa da selettore possiamo semplicemente aggiungerlo ma vuoto.

textNode.setAttribute('listner-added', false)
textNode.setAttribute('stroke', color)
textNode.appendChild(document.createTextNode(label))
return textNode
}

export default ({ color, width, height, x, y, index, label }) => {
const group = document.createElementNS(SVG_NS_URI, 'g')

Expand All @@ -46,6 +64,9 @@ export default ({ color, width, height, x, y, index, label }) => {

group.appendChild(createPostItNode(width, height, color, index))
group.appendChild(createTextNode(width, height, textLabel, index))
group.appendChild(createDeleteNode(width, 0, '✖', index, color))

// const toggleButton = document.querySelector('button[data-toggle-legend]')

return group
}