Skip to content

Commit 1e21623

Browse files
committed
Refactor chart settings panel to use reusable select control function
Signed-off-by: Nick2bad4u <20943337+Nick2bad4u@users.noreply.github.com>
1 parent 279fb08 commit 1e21623

1 file changed

Lines changed: 32 additions & 27 deletions

File tree

github/github-star-history/github-star-history.user.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,22 @@
595595
: 'Chart settings restored to defaults.\n\nOpen any repo page to see the updated chart.')
596596
}
597597

598+
function createSelectControl(values, labels, selectedValue, onChange) {
599+
const select = document.createElement('select')
600+
values.forEach(value => {
601+
const option = document.createElement('option')
602+
option.value = value
603+
option.textContent = labels[value]
604+
option.selected = value == selectedValue
605+
select.append(option)
606+
})
607+
select.onchange = event => onChange(event.target.value)
608+
return select
609+
}
610+
598611
function createHoverSettingsPanel(starHistoryDiv, options = {}) {
599612
if (starHistoryDiv.querySelector('#star-history-settings-btn')) return
600-
const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0,
613+
const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0,
601614
alwaysVisible = !!options.alwaysVisible,
602615
disableAutoHide = !!options.disableAutoHide,
603616
keepPanelOpenOnSave = !!options.keepPanelOpenOnSave
@@ -612,8 +625,9 @@
612625
logscale: !!settings.logscale
613626
}
614627

615-
if (!options.preserveContainerStyles)
628+
if (!options.preserveContainerStyles) {
616629
Object.assign(starHistoryDiv.style, { position: 'relative', overflow: 'visible' })
630+
}
617631

618632
const settingsBtn = document.createElement('button')
619633
settingsBtn.id = 'star-history-settings-btn'
@@ -663,34 +677,22 @@
663677
row.append(label, controlElem) ; panel.append(row)
664678
}
665679

666-
const typeSelect = document.createElement('select')
667-
for (const value of chartConfig.values.type) {
668-
const option = document.createElement('option')
669-
option.value = value ; option.textContent = chartConfig.labels.type[value]
670-
if (value == settings.type) option.selected = true
671-
typeSelect.append(option)
672-
}
673-
typeSelect.onchange = event => { draftSettings.type = event.target.value }
680+
const typeSelect = createSelectControl(
681+
chartConfig.values.type, chartConfig.labels.type, settings.type,
682+
value => { draftSettings.type = value }
683+
)
674684
addField('Type', typeSelect)
675685

676-
const legendSelect = document.createElement('select')
677-
for (const value of chartConfig.values.legend) {
678-
const option = document.createElement('option')
679-
option.value = value ; option.textContent = chartConfig.labels.legend[value]
680-
if (value == settings.legend) option.selected = true
681-
legendSelect.append(option)
682-
}
683-
legendSelect.onchange = event => { draftSettings.legend = event.target.value }
686+
const legendSelect = createSelectControl(
687+
chartConfig.values.legend, chartConfig.labels.legend, settings.legend,
688+
value => { draftSettings.legend = value }
689+
)
684690
addField('Legend', legendSelect)
685691

686-
const themeSelect = document.createElement('select')
687-
for (const value of chartConfig.values.theme) {
688-
const option = document.createElement('option')
689-
option.value = value ; option.textContent = chartConfig.labels.theme[value]
690-
if (value == settings.theme) option.selected = true
691-
themeSelect.append(option)
692-
}
693-
themeSelect.onchange = event => { draftSettings.theme = event.target.value }
692+
const themeSelect = createSelectControl(
693+
chartConfig.values.theme, chartConfig.labels.theme, settings.theme,
694+
value => { draftSettings.theme = value }
695+
)
694696
addField('Theme', themeSelect)
695697

696698
const logscaleToggle = document.createElement('input')
@@ -768,7 +770,10 @@
768770
const togglePanel = event => {
769771
event.stopPropagation()
770772
const opening = panel.style.display == 'none'
771-
if (!opening) return panel.style.display = 'none'
773+
if (!opening) {
774+
panel.style.display = 'none'
775+
return
776+
}
772777
panel.style.display = 'block'
773778
panel.style.visibility = 'hidden'
774779
positionPanel()

0 commit comments

Comments
 (0)