Skip to content

Commit

Permalink
Override default settings through url parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromebon committed Nov 11, 2021
1 parent 5c4d7f7 commit 8588932
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/js/page/main-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,10 @@ export default class MainController {
}

async _loadSettings() {
let settings = await storage.get('settings');
settings = Object.assign(
{},
settings,
this._settingsUi.getSettingsOverride(),
);
const settings = {
...(await storage.get('settings')),
...this._settingsUi.getSettingsOverride(),
};
if (settings) this._settingsUi.setSettings(settings);
}

Expand Down
26 changes: 9 additions & 17 deletions src/js/page/ui/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export default class Settings {
if (event.target.closest('input[type=range]')) return;
event.preventDefault();
});

const settingsOverride = this.getSettingsOverride();
if (settingsOverride) this.setSettings(settingsOverride);
});
}

Expand All @@ -65,34 +68,23 @@ export default class Settings {
_onReset() {
this._resetRipple.animate();
const oldSettings = this.getSettings();
const overrides = this.getSettingsOverride();

// Set all inputs according to their initial attributes
for (const inputEl of this._globalInputs) {
if (inputEl.type === 'checkbox') {
inputEl.checked = Object.prototype.hasOwnProperty.call(
overrides,
inputEl.name,
)
? overrides[inputEl.name]
: inputEl.hasAttribute('checked');
inputEl.checked = inputEl.hasAttribute('checked');
} else if (inputEl.type === 'range') {
this._sliderMap.get(inputEl).value =
Object.prototype.hasOwnProperty.call(overrides, inputEl.name)
? overrides[inputEl.name]
: inputEl.getAttribute('value');
this._sliderMap.get(inputEl).value = inputEl.getAttribute('value');
}
}

for (const inputEl of this._pluginInputs) {
inputEl.checked = Object.prototype.hasOwnProperty.call(
overrides.plugins,
inputEl.name,
)
? overrides.plugins[inputEl.name]
: inputEl.hasAttribute('checked');
inputEl.checked = inputEl.hasAttribute('checked');
}

const settingsOverride = this.getSettingsOverride();
if (settingsOverride) this.setSettings(settingsOverride);

this.emitter.emit('reset', oldSettings);
this.emitter.emit('change');
}
Expand Down

0 comments on commit 8588932

Please sign in to comment.