Set the baseURL for ajax requests (#373)
* Set the baseURL for ajax requests * Use async/await on AJAX calls * Add mix_public() cache generated asset cache busting * Move storage container into separate class * Fix some styling
This commit is contained in:
77
resources/js/storage.js
Normal file
77
resources/js/storage.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Simple browser storage interface
|
||||
*/
|
||||
export default class Storage {
|
||||
constructor(name, default_value) {
|
||||
this.name = name;
|
||||
|
||||
// Read the object from storage; if it doesn't exist, set
|
||||
// it to the default value
|
||||
const st = window.localStorage.getItem(this.name);
|
||||
if (!st) {
|
||||
console.log('Nothing found in storage, starting from default');
|
||||
this.data = default_value;
|
||||
} else {
|
||||
console.log('Found in storage: ', st);
|
||||
this.data = JSON.parse(st);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save to local storage
|
||||
*/
|
||||
save() {
|
||||
window.localStorage.setItem(this.name, JSON.stringify(this.data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list from a given key
|
||||
*
|
||||
* @param {String} key
|
||||
*
|
||||
* @returns {Array|*}
|
||||
*/
|
||||
getList(key) {
|
||||
if (!(key in this.data)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.data[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add `value` to a given `key`
|
||||
*
|
||||
* @param {string} key
|
||||
* @param {*} value
|
||||
*/
|
||||
addToList(key, value) {
|
||||
if (!(key in this.data)) {
|
||||
this.data[key] = [];
|
||||
}
|
||||
|
||||
const index = this.data[key].indexOf(value);
|
||||
if (index === -1) {
|
||||
this.data[key].push(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove `value` from the given `key`
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {*} value
|
||||
*/
|
||||
removeFromList(key, value) {
|
||||
if (!(key in this.data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const index = this.data[key].indexOf(value);
|
||||
if (index !== -1) {
|
||||
this.data[key].splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user