Skip to content

Feature/ranking question type - #285

Open
Sunilshah-7 wants to merge 7 commits into
surveydown-dev:mainfrom
Sunilshah-7:feature/ranking-question-type
Open

Feature/ranking question type#285
Sunilshah-7 wants to merge 7 commits into
surveydown-dev:mainfrom
Sunilshah-7:feature/ranking-question-type

Conversation

@Sunilshah-7

Copy link
Copy Markdown

Add ranking question type

Implements the ranking proposal discussed in the issue.

API

Matches what we agreed in the issue:

sd_question(
  type  = 'ranking',
  id    = 'albums_ranked',
  label = "Rank these Taylor Swift albums from favorite to least favorite:",
  option = c(
    "Taylor Swift (2006)" = "swift",
    "Fearless (2008)"     = "fearless",
    "Speak Now (2010)"    = "speak_now",
    "Red (2012)"          = "red",
    "1989 (2014)"         = "1989"
  )
)

Stored as a single pipe-joined string in ranked order (swift|red|fearless|...), following the mc_multiple convention.

Implementation approach

No new dependency. ~120 lines of Pointer Events drag logic ininst/js/ranking.js rather than pulling in a library. Pointer Events give mouse and touch one code path (native HTML5 drag-and-drop is unusable here - it doesn't fire on
touch devices).

What I evaluated:

Option Finding
jQuery UI (bundled in Shiny) Does include sortable, but isn't loaded by default and Shiny's build is 253KB — 5.6× SortableJS
shinyjqui Wraps that same jQuery UI: new dependency and the 253KB
Vendoring SortableJS 1.15.6 45KB raw / ~15KB gzipped. Verified the copy inside sortable is byte-identical to the upstream release
Hand-rolled Pointer Events ~120 lines, no new files beyond the ones the type needs anyway

Tradeoff as I see it: vendoring is tested and near-zero review/maintenance cost (upgrades are a file swap), but because include-resources.lua + --embed-resources inline everything in js_files, it would be baked into every rendered survey whether or not it uses a ranking question. Hand-rolling keeps the payload and the diff small and
matches the style of the existing inst/js/ files, but means surveydown owns drag-and-drop bug reports going forward — and sorting's cross-bin dragging in the follow-up is meaningfully harder to hand-roll than ranking's single-list reorder.

Files changed

File What
inst/js/ranking.js (new) Pointer Events drag, order reporting, restore handler, test hook
inst/css/surveydown.css .sd-ranking-* styles; position numbers via CSS counters
inst/lua/include-resources.lua Register ranking.js
R/question_types.R qt_render_ranking(), qt_restore_ranking(), registry entry
R/config.R Detect .sd-ranking-list, extract options, map class → ranking
R/ui.R Roxygen docs only
man/sd_question.Rd Regenerated
NEWS.md Entry

Reproducible steps

git checkout feature/ranking-question-type
R CMD INSTALL .

# minimal test survey (survey.qmd + app.R shown below)
rm -rf test_survey/_survey test_survey/preview_data.csv
Rscript -e 'shiny::runApp("test_survey", port = 3838)'

Please let me know additional test cases and reviews for this feature.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant