Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
pleroma-fe
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
David Yin
pleroma-fe
Commits
f11af289
Commit
f11af289
authored
Jun 18, 2019
by
Shpuld Shpludson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarify poll choice logic a lot
parent
8a4b64f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
35 deletions
+22
-35
src/components/poll/poll.js
src/components/poll/poll.js
+22
-35
No files found.
src/components/poll/poll.js
View file @
f11af289
import
Timeago
from
'
../timeago/timeago.vue
'
import
{
forEach
}
from
'
lodash
'
import
{
forEach
,
map
}
from
'
lodash
'
export
default
{
name
:
'
Poll
'
,
...
...
@@ -8,14 +8,14 @@ export default {
data
()
{
return
{
loading
:
false
,
multipleChoices
:
[],
singleChoiceIndex
:
null
,
choices
:
[],
refreshInterval
:
null
}
},
created
()
{
this
.
refreshInterval
=
setTimeout
(
this
.
refreshPoll
,
30
*
1000
)
this
.
multipleChoices
=
this
.
poll
.
options
.
map
(
_
=>
false
)
// Initialize choices to booleans and set its length to match options
this
.
choices
=
this
.
poll
.
options
.
map
(
_
=>
false
)
},
destroyed
()
{
clearTimeout
(
this
.
refreshInterval
)
...
...
@@ -42,14 +42,15 @@ export default {
}
},
choiceIndices
()
{
return
this
.
multipleChoices
// Convert array of booleans into an array of indices of the
// items that were 'true', so [true, false, false, true] becomes
// [0, 3].
return
this
.
choices
.
map
((
entry
,
index
)
=>
entry
&&
index
)
.
filter
(
value
=>
typeof
value
===
'
number
'
)
},
isDisabled
()
{
const
noChoice
=
this
.
poll
.
multiple
?
this
.
choiceIndices
.
length
===
0
:
this
.
singleChoiceIndex
===
undefined
const
noChoice
=
this
.
choiceIndices
.
length
===
0
return
this
.
loading
||
noChoice
}
},
...
...
@@ -77,44 +78,30 @@ export default {
// same poll appears multiple times on the site (notifs and
// timeline for example). With code we can make sure it just
// works without altering the pseudo element implementation.
const
allElements
=
this
.
$el
.
querySelectorAll
(
'
input
'
)
const
clickedElement
=
this
.
$el
.
querySelector
(
`input[value="
${
index
}
"]`
)
if
(
this
.
poll
.
multiple
)
{
// Checkboxes
const
wasChecked
=
this
.
multipleChoices
[
index
]
clickedElement
.
checked
=
!
wasChecked
this
.
$set
(
this
.
multipleChoices
,
index
,
!
wasChecked
)
// Checkboxes, toggle only the clicked one
clickedElement
.
checked
=
!
clickedElement
.
checked
}
else
{
// Radio button
const
allElements
=
this
.
$el
.
querySelectorAll
(
'
input
'
)
forEach
(
allElements
,
element
=>
{
element
.
checked
=
false
})
// Radio button, uncheck everything and check the clicked one
forEach
(
allElements
,
element
=>
{
element
.
checked
=
false
})
clickedElement
.
checked
=
true
this
.
singleChoiceIndex
=
index
}
this
.
choices
=
map
(
allElements
,
e
=>
e
.
checked
)
},
optionId
(
index
)
{
return
`poll
${
this
.
poll
.
id
}
-
${
index
}
`
},
vote
()
{
if
(
this
.
choiceIndices
.
length
===
0
)
return
this
.
loading
=
true
if
(
this
.
poll
.
multiple
)
{
if
(
this
.
choiceIndices
.
length
===
0
)
return
this
.
$store
.
dispatch
(
'
votePoll
'
,
{
id
:
this
.
statusId
,
pollId
:
this
.
poll
.
id
,
choices
:
this
.
choiceIndices
}
).
then
(
poll
=>
{
this
.
loading
=
false
})
}
else
{
if
(
this
.
singleChoiceIndex
===
undefined
)
return
this
.
$store
.
dispatch
(
'
votePoll
'
,
{
id
:
this
.
statusId
,
pollId
:
this
.
poll
.
id
,
choices
:
[
this
.
singleChoiceIndex
]
}
).
then
(
poll
=>
{
this
.
loading
=
false
})
}
this
.
$store
.
dispatch
(
'
votePoll
'
,
{
id
:
this
.
statusId
,
pollId
:
this
.
poll
.
id
,
choices
:
this
.
choiceIndices
}
).
then
(
poll
=>
{
this
.
loading
=
false
})
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment