@trapcode/codebase
Version:
For TrapCode
110 lines (87 loc) • 2.76 kB
JavaScript
let DataHelper;
export default DataHelper = (function () {
class DataHelper {
getStorageKey(name) {
return 'app-store:' + name;
};
hasStoredData(key) {
return localStorage.getItem(this.getStorageKey(key)) !== null;
}
storeData(name, value) {
if (localStorage) {
localStorage.setItem(this.getStorageKey(name), value);
}
}
getStoredData(key, $default = null) {
if (!this.hasStoredData(key)) {
return $default;
}
return localStorage.getItem(this.getStorageKey(key));
}
delStoredData(key){
localStorage.removeItem(this.getStorageKey(key));
}
setData(name, value, store = false) {
this.data[name] = value;
if (store) {
this.storeData(name, value);
}
return this.data[name];
}
getData(name, value = void 0) {
if (this.hasData(name)) {
return this.data[name];
} else {
return value;
}
}
hasData(name, value = null) {
if (value === null) {
return this.data.hasOwnProperty(name);
} else {
if (this.data.hasOwnProperty(name) && this.data[name] === value) {
return true;
}
}
return false;
}
delData(name) {
if (this.hasData(name)) {
return delete this.data[name];
}
}
prevData(refresh = false) {
let id;
if (refresh) {
id = this.setData('prevData_id', ng.randomStr());
} else {
id = this.getData('prevData_id');
}
return id;
}
getComponentCache(key, $default = {}) {
let cacheKey;
if (typeof key === 'string') {
cacheKey = 'componentCache:' + key.trim();
if (ng.s().hasData(cacheKey)) {
return ng.s().getData(cacheKey);
}
}
return $default;
}
setComponentCache(key, value) {
let cacheKey;
if (typeof key === 'string') {
cacheKey = 'componentCache:' + key.trim();
return ng.s().setData(cacheKey, value);
}
return value;
}
hasComponentCache(key) {
let cacheKey = 'componentCache:' + key.trim();
return ng.hasData(cacheKey);
}
};
DataHelper.prototype.data = {};
return DataHelper;
}).call(this);