From 1632a8f364672555bce25d35149b17584bc20c65 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Tue, 4 Sep 2018 21:16:40 -0500 Subject: [PATCH] Add number & range variables. See #492 --- js/usercss.js | 20 +++++++++++++++++-- manage/config-dialog.css | 3 ++- manage/config-dialog.js | 43 ++++++++++++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/js/usercss.js b/js/usercss.js index 8f5e6eda..c91df4ca 100644 --- a/js/usercss.js +++ b/js/usercss.js @@ -29,7 +29,7 @@ var usercss = (() => { ['version', 0], ]); const MANDATORY_META = ['name', 'namespace', 'version']; - const META_VARS = ['text', 'color', 'checkbox', 'select', 'dropdown', 'image']; + const META_VARS = ['text', 'color', 'checkbox', 'select', 'dropdown', 'image', 'number', 'range']; const META_URLS = [...KNOWN_META.keys()].filter(k => k.endsWith('URL')); const BUILDER = { @@ -203,6 +203,22 @@ var usercss = (() => { break; } + case 'range': { + state.errorPrefix = 'Invalid JSON: '; + parseJSONValue(state); + state.errorPrefix = ''; + // [ start, end, step, default ] + if (Array.isArray(state.value) && state.value.length === 4) { + result.range = state.value; + result.default = state.value[3]; + } else { + // not a range, fallback to text + result.type = 'text'; + result.default = state.value; + } + break; + } + case 'dropdown': case 'image': { if (text[re.lastIndex] !== '{') { @@ -235,7 +251,7 @@ var usercss = (() => { } default: { - // text, color + // text, color, number parseStringToEnd(state); result.default = state.value; } diff --git a/manage/config-dialog.css b/manage/config-dialog.css index 9a895641..30fb6660 100644 --- a/manage/config-dialog.css +++ b/manage/config-dialog.css @@ -94,7 +94,8 @@ flex-shrink: 0; } -.config-value:not(.onoffswitch):not(.select-resizer) > :not(:last-child) { +.config-value:not(.onoffswitch):not(.select-resizer) > :not(:last-child), +.current-value { margin-right: 4px; } diff --git a/manage/config-dialog.js b/manage/config-dialog.js index cd056ebd..796ce88a 100644 --- a/manage/config-dialog.js +++ b/manage/config-dialog.js @@ -213,7 +213,7 @@ function configDialog(style) { ]) ]); for (const va of vars) { - let children; + let children, options; switch (va.type) { case 'color': children = [ @@ -259,14 +259,27 @@ function configDialog(style) { break; default: - children = [ - va.input = $create('input.config-value', { - va, - type: 'text', - onchange: updateVarOnChange, - oninput: updateVarOnInput, - }), - ]; + options = { + va, + type: va.type, + onchange: updateVarOnChange, + oninput: updateVarOnInput, + }; + if (va.type === 'range') { + options.min = va.range[0]; + options.max = va.range[1]; + options.step = va.range[2]; + options.value = va.default; + children = [ + $create('span.current-value', {textContent: va.value}), + va.input = $create('input.config-value', options), + ]; + } else { + children = [ + va.input = $create('input.config-value', options), + ]; + } + break; } @@ -287,6 +300,9 @@ function configDialog(style) { function updateVarOnChange() { this.va.value = this.type !== 'checkbox' ? this.value : this.checked ? '1' : '0'; + if (this.type === 'range') { + $('.current-value', this.parentNode).textContent = this.va.value; + } } function updateVarOnInput(event, debounced = false) { @@ -307,9 +323,14 @@ function configDialog(style) { } } else if (va.type === 'checkbox') { va.input.checked = Number(value); - } else { - va.input.value = value; } + if (va.type === 'range') { + const span = $('.current-value', va.input.parentNode); + if (span) { + span.textContent = value; + } + } + va.input.value = value; if (!prefs.get('config.autosave')) { renderValueState(va); }