options: use live prefs + reset button

This commit is contained in:
tophf 2017-04-04 20:21:03 +03:00
parent 7f0fd4f6f6
commit efd3e9ef6c
6 changed files with 30 additions and 54 deletions

View File

@ -550,11 +550,8 @@
"optionsActions": { "optionsActions": {
"message": "Actions" "message": "Actions"
}, },
"optionsSave": { "optionsReset": {
"message": "Save" "message": "Reset"
},
"optionsSaved": {
"message": "Options saved."
}, },
"optionsOpenManager": { "optionsOpenManager": {
"message": "Open styles manager" "message": "Open styles manager"

View File

@ -53,24 +53,11 @@ input[type=number] {
margin-top: -2em; margin-top: -2em;
} }
#save { #reset {
text-align: right; text-align: right;
position: relative; position: relative;
} }
#save-status {
margin-top: .5ex;
position: absolute;
right: 0;
visibility: hidden;
}
#save-status.fadeinout {
visibility: visible;
animation: fadeinout 1s ease-in-out;
animation-fill-mode: both;
}
#notes { #notes {
background-color: #f4f4f4; background-color: #f4f4f4;
margin-top: .75rem; margin-top: .75rem;

View File

@ -49,9 +49,8 @@
<td><input type="number" min="0" id="updateInterval"></td> <td><input type="number" min="0" id="updateInterval"></td>
</tr> </tr>
</table> </table>
<div id="save"> <div id="reset">
<button id="save-button" i18n-text="optionsSave"></button> <button data-cmd="reset" i18n-text="optionsReset"></button>
<div id="save-status" i18n-text="optionsSaved"></div>
</div> </div>
</div> </div>

View File

@ -2,31 +2,15 @@
'use strict'; 'use strict';
function restore() { setupLivePrefs([
setupLivePrefs([
'show-badge', 'show-badge',
'popup.stylesFirst' 'popup.stylesFirst',
]); 'badgeNormal',
//$('#show-badge').value = bg.prefs.get('show-badge'); 'badgeDisabled',
$('#badgeDisabled').value = prefs.get('badgeDisabled'); 'popupWidth',
$('#badgeNormal').value = prefs.get('badgeNormal');
$('#popupWidth').value = prefs.get('popupWidth');
$('#updateInterval').value = prefs.get('updateInterval');
enforceValueRange('popupWidth');
}
function save() {
prefs.set('badgeDisabled', $('#badgeDisabled').value);
prefs.set('badgeNormal', $('#badgeNormal').value);
prefs.set('popupWidth', enforceValueRange('popupWidth'));
prefs.set(
'updateInterval', 'updateInterval',
Math.max(0, Number($('#updateInterval').value)) ]);
); enforceValueRange('popupWidth');
animateElement($('#save-status'), {className: 'fadeinout'});
}
function enforceValueRange(id) { function enforceValueRange(id) {
const element = document.getElementById(id); const element = document.getElementById(id);
@ -41,10 +25,6 @@ function enforceValueRange(id) {
return value | 0; return value | 0;
} }
restore();
$('#save').onclick = save;
// overwrite the default URL if browser is Opera // overwrite the default URL if browser is Opera
$('[data-cmd="open-keyboard"]').href = configureCommands.url; $('[data-cmd="open-keyboard"]').href = configureCommands.url;
@ -104,5 +84,10 @@ document.onclick = e => {
configureCommands.open(); configureCommands.open();
break; break;
case 'reset':
$$('input')
.filter(input => input.id in prefs.readOnlyValues)
.forEach(input => prefs.reset(input.id));
break;
} }
}; };

View File

@ -33,18 +33,24 @@ chrome.runtime.onMessage.addListener(msg => {
const actions = $('body > .actions'); const actions = $('body > .actions');
const before = stylesFirst ? actions : actions.nextSibling; const before = stylesFirst ? actions : actions.nextSibling;
document.body.insertBefore(installed, before); document.body.insertBefore(installed, before);
} else if ('popupWidth' in msg.prefs) {
setPopupWidth(msg.prefs.popupWidth);
} }
break; break;
} }
}); });
function setPopupWidth(width = prefs.get('popupWidth')) {
document.body.style.width =
Math.max(200, Math.min(800, width)) + 'px';
}
function initPopup(url) { function initPopup(url) {
installed = $('#installed'); installed = $('#installed');
// popup width setPopupWidth();
document.body.style.width =
Math.max(200, Math.min(800, prefs.get('popupWidth'))) + 'px';
// force Chrome to resize the popup // force Chrome to resize the popup
document.body.style.height = '10px'; document.body.style.height = '10px';

View File

@ -652,6 +652,8 @@ prefs = prefs || new function Prefs() {
remove: key => this.set(key, undefined), remove: key => this.set(key, undefined),
reset: key => this.set(key, deepCopy(defaults[key])),
broadcast(key, value, {noSync} = {}) { broadcast(key, value, {noSync} = {}) {
broadcastPrefs[key] = value; broadcastPrefs[key] = value;
debounce(doBroadcast); debounce(doBroadcast);