Functioning UI
This commit is contained in:
@@ -1,14 +1,40 @@
|
||||
import { DeviceInterface } from 'snjs';
|
||||
import { DeviceInterface, getGlobalScope } from 'snjs';
|
||||
import { Database } from '@/database';
|
||||
|
||||
const KEYCHAIN_STORAGE_KEY = 'keychain';
|
||||
|
||||
export class WebDeviceInterface extends DeviceInterface {
|
||||
|
||||
constructor({
|
||||
namespace,
|
||||
} = {}) {
|
||||
super({
|
||||
namespace,
|
||||
timeout: setTimeout.bind(getGlobalScope()),
|
||||
interval: setInterval.bind(getGlobalScope())
|
||||
});
|
||||
this.createDatabase();
|
||||
}
|
||||
|
||||
createDatabase() {
|
||||
this.database = new Database();
|
||||
}
|
||||
|
||||
setApplication(application) {
|
||||
this.database.setApplication(application);
|
||||
}
|
||||
|
||||
/**
|
||||
* @value storage
|
||||
*/
|
||||
|
||||
async getRawStorageValue(key) {
|
||||
return localStorage.getItem(key);
|
||||
}
|
||||
|
||||
async getAllRawStorageKeyValues() {
|
||||
const results = [];
|
||||
for(const key of Object.keys(localStorage)) {
|
||||
for (const key of Object.keys(localStorage)) {
|
||||
results.push({
|
||||
key: key,
|
||||
value: localStorage[key]
|
||||
@@ -29,6 +55,78 @@ export class WebDeviceInterface extends DeviceInterface {
|
||||
localStorage.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @database
|
||||
*/
|
||||
|
||||
async openDatabase() {
|
||||
this.database.setLocked(false);
|
||||
this.database.openDatabase({
|
||||
onUpgradeNeeded: () => {
|
||||
/**
|
||||
* New database/database wiped, delete syncToken so that items
|
||||
* can be refetched entirely from server
|
||||
*/
|
||||
/** @todo notify parent */
|
||||
// this.syncManager.clearSyncPositionTokens();
|
||||
// this.sync();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @private */
|
||||
getDatabaseKeyPrefix() {
|
||||
if (this.namespace) {
|
||||
return `${this.namespace}-item-`;
|
||||
} else {
|
||||
return `item-`;
|
||||
}
|
||||
}
|
||||
|
||||
/** @private */
|
||||
keyForPayloadId(id) {
|
||||
return `${this.getDatabaseKeyPrefix()}${id}`;
|
||||
}
|
||||
|
||||
async getAllRawDatabasePayloads() {
|
||||
return this.database.getAllPayloads();
|
||||
}
|
||||
|
||||
async saveRawDatabasePayload(payload) {
|
||||
return this.database.savePayload(payload);
|
||||
}
|
||||
|
||||
async saveRawDatabasePayloads(payloads) {
|
||||
return this.database.savePayloads(payloads);
|
||||
}
|
||||
|
||||
async removeRawDatabasePayloadWithId(id) {
|
||||
return this.database.deletePayload(id);
|
||||
}
|
||||
|
||||
async removeAllRawDatabasePayloads() {
|
||||
return this.database.clearAllPayloads();
|
||||
}
|
||||
|
||||
/** @keychian */
|
||||
async getRawKeychainValue() {
|
||||
const value = localStorage.getItem(KEYCHAIN_STORAGE_KEY);
|
||||
if(value) {
|
||||
return JSON.parse(value);
|
||||
}
|
||||
}
|
||||
|
||||
async setKeychainValue(value) {
|
||||
localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(value));
|
||||
}
|
||||
|
||||
async clearKeychainValue() {
|
||||
localStorage.removeItem(KEYCHAIN_STORAGE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @actions
|
||||
*/
|
||||
openUrl(url) {
|
||||
const win = window.open(url, '_blank');
|
||||
if (win) {
|
||||
@@ -36,85 +134,4 @@ export class WebDeviceInterface extends DeviceInterface {
|
||||
}
|
||||
}
|
||||
|
||||
/** @database */
|
||||
|
||||
_getDatabaseKeyPrefix() {
|
||||
if(this.namespace) {
|
||||
return `${this.namespace}-item-`;
|
||||
} else {
|
||||
return `item-`;
|
||||
}
|
||||
}
|
||||
|
||||
_keyForPayloadId(id) {
|
||||
return `${this._getDatabaseKeyPrefix()}${id}`;
|
||||
}
|
||||
|
||||
async getRawDatabasePayloadWithId(id) {
|
||||
return localStorage.getItem(this._keyForPayloadId(id))
|
||||
}
|
||||
|
||||
async getAllRawDatabasePayloads() {
|
||||
const models = [];
|
||||
for(const key in localStorage) {
|
||||
if(key.startsWith(this._getDatabaseKeyPrefix())) {
|
||||
models.push(JSON.parse(localStorage[key]))
|
||||
}
|
||||
}
|
||||
return models;
|
||||
}
|
||||
|
||||
async saveRawDatabasePayload(payload) {
|
||||
localStorage.setItem(
|
||||
this._keyForPayloadId(payload.uuid),
|
||||
JSON.stringify(payload)
|
||||
);
|
||||
}
|
||||
|
||||
async saveRawDatabasePayloads(payloads) {
|
||||
for(const payload of payloads) {
|
||||
await this.saveRawDatabasePayload(payload);
|
||||
}
|
||||
}
|
||||
|
||||
async removeRawDatabasePayloadWithId(id) {
|
||||
localStorage.removeItem(this._keyForPayloadId(id));
|
||||
}
|
||||
|
||||
async removeAllRawDatabasePayloads() {
|
||||
for(const key in localStorage) {
|
||||
if(key.startsWith(this._getDatabaseKeyPrefix())) {
|
||||
delete localStorage[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** @keychian */
|
||||
async getRawKeychainValue() {
|
||||
if(this.keychainValue) {
|
||||
return this.keychainValue;
|
||||
} else {
|
||||
const authParams = localStorage.getItem('auth_params');
|
||||
if(!authParams) {
|
||||
return null;
|
||||
}
|
||||
const version = JSON.parse(authParams).version;
|
||||
return {
|
||||
mk: localStorage.getItem('mk'),
|
||||
pw: localStorage.getItem('pw'),
|
||||
ak: localStorage.getItem('ak'),
|
||||
version: version
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async setKeychainValue(value) {
|
||||
this.keychainValue = value;
|
||||
}
|
||||
|
||||
async clearKeychainValue() {
|
||||
this.keychainValue = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user