diff --git a/js/src/configviewmodel.js b/js/src/configviewmodel.js
new file mode 100644
index 0000000..a219138
--- /dev/null
+++ b/js/src/configviewmodel.js
@@ -0,0 +1,64 @@
+/*
+ * μlogger
+ *
+ * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
+ *
+ * This is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see .
+ */
+
+import { config, lang } from './initializer.js';
+import ViewModel from './viewmodel.js';
+import uUtils from './utils.js';
+
+/**
+ * @class ConfigViewModel
+ */
+export default class ConfigViewModel extends ViewModel {
+ /**
+ * @param {uState} state
+ */
+ constructor(state) {
+ super(config);
+ this.state = state;
+ this.intervalEl = document.querySelector('#interval');
+ this.model.onSetInterval = () => this.setAutoReloadInterval();
+ this.bindAll();
+ this.onChanged('mapApi', (api) => {
+ uUtils.setCookie('api', api);
+ });
+ this.onChanged('lang', (_lang) => {
+ uUtils.setCookie('lang', _lang);
+ ConfigViewModel.reload();
+ });
+ this.onChanged('units', (units) => {
+ uUtils.setCookie('units', units);
+ ConfigViewModel.reload();
+ });
+ this.onChanged('interval', (interval) => {
+ this.intervalEl.innerHTML = interval.toString();
+ uUtils.setCookie('interval', interval);
+ });
+ }
+
+ static reload() {
+ window.location.reload();
+ }
+
+ setAutoReloadInterval() {
+ const interval = parseInt(prompt(lang.strings['newinterval']));
+ if (!isNaN(interval) && interval !== this.model.interval) {
+ this.model.interval = interval;
+ }
+ }
+}
diff --git a/js/src/trackviewmodel.js b/js/src/trackviewmodel.js
index ceee88b..2ba5285 100644
--- a/js/src/trackviewmodel.js
+++ b/js/src/trackviewmodel.js
@@ -53,16 +53,13 @@ export default class TrackViewModel extends ViewModel {
/** @type {function} */
onExportKml: null,
/** @type {function} */
- onImportGpx: null,
- /** @type {function} */
- onSetInterval: null
+ onImportGpx: null
});
this.setClickHandlers();
/** @type HTMLSelectElement */
const listEl = document.querySelector('#track');
this.summaryEl = document.querySelector('#summary');
this.importEl = document.querySelector('#input-file');
- this.intervalEl = document.querySelector('#interval');
this.select = new uSelect(listEl);
this.state = state;
this.timerId = 0;
@@ -80,7 +77,6 @@ export default class TrackViewModel extends ViewModel {
this.model.onExportGpx = exportCb('gpx');
this.model.onExportKml = exportCb('kml');
this.model.onImportGpx = () => this.importEl.click();
- this.model.onSetInterval = () => this.setAutoReloadInterval();
}
setObservers() {
@@ -119,6 +115,12 @@ export default class TrackViewModel extends ViewModel {
this.loadAllUsersPosition();
}
});
+ config.onChanged('interval', () => {
+ if (this.timerId) {
+ this.stopAutoReload();
+ this.startAutoReload();
+ }
+ });
}
/**
@@ -275,19 +277,6 @@ export default class TrackViewModel extends ViewModel {
this.model.autoReload = false;
}
- setAutoReloadInterval() {
- const interval = parseInt(prompt(lang.strings['newinterval']));
- if (!isNaN(interval) && interval !== config.interval) {
- config.interval = interval;
- this.intervalEl.innerHTML = config.interval.toString();
- // if live tracking on, reload with new interval
- if (this.timerId) {
- this.stopAutoReload();
- this.startAutoReload();
- }
- }
- }
-
renderSummary() {
if (!this.state.currentTrack || !this.state.currentTrack.hasPositions) {
this.summaryEl.innerHTML = '';
diff --git a/js/test/configviewmodel.test.js b/js/test/configviewmodel.test.js
new file mode 100644
index 0000000..7691de6
--- /dev/null
+++ b/js/test/configviewmodel.test.js
@@ -0,0 +1,200 @@
+/*
+ * μlogger
+ *
+ * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
+ *
+ * This is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see .
+ */
+
+import ConfigViewModel from '../src/configviewmodel.js';
+import ViewModel from '../src/viewmodel.js';
+import { config } from '../src/initializer.js';
+import uObserve from '../src/observe.js';
+import uState from '../src/state.js';
+import uUtils from '../src/utils.js';
+
+describe('ConfigViewModel tests', () => {
+
+ let vm;
+ let state;
+ /** @type {HTMLSpanElement} */
+ let intervalEl;
+ /** @type {HTMLAnchorElement} */
+ let setIntervalEl;
+ /** @type {HTMLSelectElement} */
+ let apiEl;
+ /** @type {HTMLSelectElement} */
+ let langEl;
+ /** @type {HTMLSelectElement} */
+ let unitsEl;
+ const newInterval = 99;
+ const newMapApi = 'openlayers';
+ const newLang = 'pl';
+ const newUnits = 'imperial';
+
+ beforeEach(() => {
+ config.initialize();
+ uObserve.setSilently(config, 'interval', 10);
+ uObserve.setSilently(config, 'lang', 'en');
+ uObserve.setSilently(config, 'units', 'metric');
+ uObserve.setSilently(config, 'mapApi', 'gmaps');
+
+ const fixture = `