/**
* AngularJS Service wrapper for localDataStorage angular plugin and global data store
*
* @module localDataService
* @see https://docs.angularjs.org/api/ng/type/angular.Module#service
*/
angular.module('tgaApp').service('localDataService', ['$log', 'gameConfig', 'localStorageService',
function localDataService($log, gameConfig, localStorageService) {
let { namespace } = gameConfig;
if (angular.isUndefined(namespace)) {
namespace = 'tta_wrapper';
localStorageService.setPrefix(`tta.${namespace}.`);
$log.error('No namespace defined in game config. Saving local data to namespace tta.tta-wrapper!');
}
/**
* Stored local data object.
*
* @private
* @member {Object} data
*/
let data = {};
/**
* Stored default value for local data object.
*
* @private
* @member {Object} defaultData
*/
let defaultData = {};
/**
* Get the stored local data object.
*
* @returns {Object}
*/
this.getDataObject = () => data;
/**
* Set a default value for the stored local data object.
*
* @param {Object} obj
*/
this.setDefaultData = (obj) => {
defaultData = { ...obj };
};
/**
* Reset the local data object to the default value.
*/
this.resetDataToDefault = () => {
const defaultStr = angular.toJson(defaultData);
data = angular.fromJson(defaultStr);
};
/**
* Set data in localStorage only.
*
* @param {string} key
* @param {*} val
*/
this.setLocalStorageData = (key, val) => {
localStorageService.set(key, val);
};
/**
* Set data in stored local data (and optionally localStorage).
*
* @param {string} key
* @param {*} val
* @param {boolean} [localStorage=false]
*/
this.setData = (key, val, localStorage = false) => {
data[key] = val;
if (localStorage) {
this.setLocalStorageData(key, val);
}
};
/**
* Get data from localStorage only.
*
* @param {string} key
* @returns {*}
*/
this.getLocalStorageData = (key) => localStorageService.get(key);
/**
* Get data from stored local data (falling back to localStorage).
*
* @param {string} key
* @returns {*}
*/
this.getData = (key) => data[key] ?? this.getLocalStorageData(key);
/**
* Increment a number value in stored local data (and optionally localStorage).
*
* @param {string} key
* @param {*} val
* @param {boolean} [localStorage=false]
*/
this.increment = (key, val, localStorage = false) => {
const previous = this.getData(val);
if (angular.isNumber(previous)) {
this.setData(key, previous + val, localStorage);
}
};
/**
* Remove data from stored local data and localStorage
*
* @param {string} key
*/
this.removeData = (key) => {
delete data[key];
localStorageService.remove(key);
};
/**
* Set a cookie with a duration. Also updates stored local data.
*
* @param {string} key
* @param {*} val
* @param {number} [days=7]
*/
this.setCookieData = (key, val, days = 7) => {
localStorageService.cookie.set(key, val, days);
this.setData(key, val);
};
/**
* Get cookie data from browser (falling back to stored local data).
*
* @param {string} key
* @return {*}
*/
this.getCookieData = (key) => localStorageService.cookie.get(key) ?? this.getData(key);
/**
* Remove cookie data.
*
* @param {string} key
*/
this.removeCookieData = (key) => {
localStorageService.cookie.remove(key);
};
/**
* Clear stored local data.
*/
this.clearData = () => {
data = {};
};
/**
* Clear localStorage only.
*/
this.clearLocalStorage = () => {
localStorageService.clearAll();
};
/**
* Clear cookies
*/
this.clearCookies = () => {
localStorageService.cookie.clearAll();
};
/**
* Clear all stored local data, localStorage, and cookies.
*/
this.clearAll = () => {
this.clearData();
this.clearLocalStorage();
this.clearCookies();
};
},
]);