Files
firefly-iii/resources/assets/v4/store/Basic.js

105 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-07-11 11:45:55 +02:00
// basic store for preferred date range and some other vars.
// used in layout.
import Get from '../api/preferences/index.js';
2023-07-15 15:25:04 +02:00
import store from 'store2';
2023-07-15 15:25:04 +02:00
/**
* A basic store for Firefly III persistent UI data and preferences.
*/
2023-07-11 11:45:55 +02:00
class Basic {
2023-07-15 15:25:04 +02:00
// currently availabel variables:
2023-07-11 11:45:55 +02:00
viewRange = '1M';
darkMode = 'browser';
language = 'en-US';
2023-07-14 06:04:48 +02:00
locale = 'en-US';
2023-07-15 15:25:04 +02:00
// start and end are used by most pages to allow the user to browse back and forth.
start = null;
end = null;
2023-07-14 06:04:48 +02:00
// others, to be used in the future.
listPageSize = 10;
2023-07-11 11:45:55 +02:00
currencyCode = 'AAA';
currencyId = '0';
ready = false;
2023-07-15 15:25:04 +02:00
// a very basic way to signal the store now contains all variables.
count = 0;
readyCount = 4;
2023-07-15 15:25:04 +02:00
/**
*
*/
2023-07-11 11:45:55 +02:00
constructor() {
2023-07-15 15:25:04 +02:00
console.log('Basic constructor')
2023-07-11 11:45:55 +02:00
}
2023-07-15 15:25:04 +02:00
/**
*
*/
2023-07-11 11:45:55 +02:00
init() {
2023-07-15 15:25:04 +02:00
console.log('Basic init')
2023-07-11 11:45:55 +02:00
this.loadVariable('viewRange')
this.loadVariable('darkMode')
this.loadVariable('language')
this.loadVariable('locale')
2023-07-11 11:45:55 +02:00
}
2023-07-15 15:25:04 +02:00
/**
* Load a variable, fresh or from storage.
* @param name
*/
2023-07-11 11:45:55 +02:00
loadVariable(name) {
2023-07-15 15:25:04 +02:00
// currently unused, window.X can be used by the blade template
// to make things available quicker than if the store has to grab it through the API.
// then again, it's not that slow.
if (window.hasOwnProperty(name)) {
2023-07-11 11:45:55 +02:00
this[name] = window[name];
2023-07-15 15:25:04 +02:00
this.triggerReady();
2023-07-11 11:45:55 +02:00
return;
}
2023-07-15 15:25:04 +02:00
// load from store2
if (store.has(name)) {
this[name] = store.get(name);
this.triggerReady();
2023-07-11 11:45:55 +02:00
return;
}
// grab
let getter = (new Get);
getter.getByName(name).then((response) => this.parseResponse(name, response));
}
parseResponse(name, response) {
let value = response.data.data.attributes.data;
this[name] = value;
2023-07-15 15:25:04 +02:00
store.set(name, value);
this.triggerReady();
2023-07-11 11:45:55 +02:00
}
2023-07-14 06:04:48 +02:00
2023-07-15 15:25:04 +02:00
set(name, value) {
2023-07-14 06:04:48 +02:00
this[name] = value;
2023-07-15 15:25:04 +02:00
store.set(name, value);
2023-07-14 06:04:48 +02:00
}
2023-07-15 15:25:04 +02:00
get(name) {
return store.get(name, this[name]);
2023-07-14 06:04:48 +02:00
}
isReady() {
return this.count === this.readyCount;
}
2023-07-15 15:25:04 +02:00
triggerReady() {
this.count++;
if (this.count === this.readyCount) {
console.log('Basic store is ready!')
// trigger event:
const event = new Event("BasicStoreReady");
document.dispatchEvent(event);
}
}
2023-07-11 11:45:55 +02:00
}
export default Basic;