Reformat code
This commit is contained in:
parent
6e27539eca
commit
420cd788c4
3 changed files with 10630 additions and 2646 deletions
93
src/app/services/data.service.ts
Normal file
93
src/app/services/data.service.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { StoreService } from './store.service';
|
||||
import { Page } from '../model/page';
|
||||
|
||||
const USER_DATA_KEY = 'life-towers.user-data.v.1';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DataService {
|
||||
get active(): Page {
|
||||
return this._active;
|
||||
}
|
||||
|
||||
get pageNames(): string[] {
|
||||
return this.data.map(p => p.name);
|
||||
}
|
||||
|
||||
private subscribers: (() => void)[] = [];
|
||||
private _active: Page = null;
|
||||
private data: Page[];
|
||||
private hasLoaded = new Promise(resolve => (this.afterLoadFinished = resolve));
|
||||
private afterLoadFinished: () => void;
|
||||
|
||||
constructor(private storeService: StoreService) {
|
||||
this.init();
|
||||
}
|
||||
|
||||
push(value: Page) {
|
||||
value.subscribe(() => this.save());
|
||||
this.data.push(value);
|
||||
this._active = value;
|
||||
this.save();
|
||||
}
|
||||
|
||||
remove() {
|
||||
this.data = this.data.filter(p => p !== this.active);
|
||||
this._active = this.data.length > 0 ? this.data[0] : null;
|
||||
this.save();
|
||||
}
|
||||
|
||||
subscribe(func: () => void) {
|
||||
this.subscribers.push(func);
|
||||
}
|
||||
|
||||
async changeActiveByName(name: string): Promise<void> {
|
||||
await this.hasLoaded;
|
||||
this._active = this.data.filter(p => p.name === name)[0];
|
||||
this.saveActiveIndex(this.data.indexOf(this.active));
|
||||
this.update();
|
||||
}
|
||||
|
||||
async changeActiveByIndex(index: number): Promise<void> {
|
||||
await this.hasLoaded;
|
||||
this._active = this.data[index];
|
||||
this.saveActiveIndex(index);
|
||||
this.update();
|
||||
}
|
||||
|
||||
private async init() {
|
||||
this.data = await this.storeService.load();
|
||||
this.data.map(p => p.subscribe(() => this.save()));
|
||||
this._active = this.data.length > 0 ? this.data[0] : null;
|
||||
this.loadActiveIndex();
|
||||
this.afterLoadFinished();
|
||||
}
|
||||
|
||||
private save() {
|
||||
this.storeService.save(this.data);
|
||||
this.update();
|
||||
}
|
||||
|
||||
private update() {
|
||||
this.subscribers.map(f => f());
|
||||
}
|
||||
|
||||
private loadActiveIndex() {
|
||||
const userData = JSON.parse(window.localStorage.getItem(USER_DATA_KEY));
|
||||
if (userData === null) {
|
||||
return;
|
||||
}
|
||||
this._active = this.data[userData.index];
|
||||
}
|
||||
|
||||
private saveActiveIndex(index: number) {
|
||||
window.localStorage.setItem(
|
||||
USER_DATA_KEY,
|
||||
JSON.stringify({
|
||||
index
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
83
src/app/services/modal.service.ts
Normal file
83
src/app/services/modal.service.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Tower } from '../model/tower';
|
||||
import { top } from '../utils/top';
|
||||
|
||||
export enum ModalType {
|
||||
createBlock,
|
||||
editBlock,
|
||||
removeBlock,
|
||||
settings,
|
||||
removeTower,
|
||||
removePage,
|
||||
getStarted
|
||||
}
|
||||
|
||||
interface Modal {
|
||||
type: ModalType;
|
||||
input: any;
|
||||
resolve: (output: any) => void;
|
||||
reject: () => void;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ModalService {
|
||||
private modalStack: Modal[] = [];
|
||||
|
||||
showCreateBlock(options: string[]): Promise<{ selected: string; description: string; isDone: boolean }> {
|
||||
return this.createPromiseAndPushToStack(options, ModalType.createBlock);
|
||||
}
|
||||
|
||||
showEditBlock(data: {
|
||||
default: string;
|
||||
options: string[];
|
||||
description: string;
|
||||
isDone: boolean;
|
||||
}): Promise<{ selected: string; description: string; isDone: boolean }> {
|
||||
return this.createPromiseAndPushToStack(data, ModalType.editBlock);
|
||||
}
|
||||
|
||||
showSettings(): Promise<void> {
|
||||
return this.createPromiseAndPushToStack(null, ModalType.settings);
|
||||
}
|
||||
|
||||
showRemoveTower(tower: Tower): Promise<void> {
|
||||
return this.createPromiseAndPushToStack(tower, ModalType.removeTower);
|
||||
}
|
||||
|
||||
showRemovePage(name: string): Promise<void> {
|
||||
return this.createPromiseAndPushToStack(name, ModalType.removePage);
|
||||
}
|
||||
|
||||
get active(): Modal {
|
||||
return top(this.modalStack);
|
||||
}
|
||||
|
||||
submit(output?: any) {
|
||||
const { resolve } = this.modalStack.pop();
|
||||
resolve(output);
|
||||
}
|
||||
|
||||
cancel() {
|
||||
const { reject } = this.modalStack.pop();
|
||||
reject();
|
||||
}
|
||||
|
||||
private createPromiseAndPushToStack(input: any, type: ModalType): Promise<any> {
|
||||
const modal = {
|
||||
input,
|
||||
type,
|
||||
resolve: () => {},
|
||||
reject: () => {}
|
||||
};
|
||||
|
||||
const modalPromise = new Promise((resolve, reject) => {
|
||||
modal.resolve = resolve;
|
||||
modal.reject = reject;
|
||||
});
|
||||
|
||||
this.modalStack.push(modal);
|
||||
return modalPromise;
|
||||
}
|
||||
}
|
||||
102
src/app/services/store.service.ts
Normal file
102
src/app/services/store.service.ts
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Page } from '../model/page';
|
||||
|
||||
const LOCAL_STORAGE_KEY = 'life-towers.data.v.2.1';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class StoreService {
|
||||
private storedData: Page[];
|
||||
|
||||
private mockData: string = JSON.stringify([
|
||||
{
|
||||
name: 'Work & life',
|
||||
userData: {},
|
||||
towers: [
|
||||
{
|
||||
name: 'work',
|
||||
baseColor: { h: 0, s: 100, l: 50 },
|
||||
blocks: [
|
||||
{
|
||||
created: new Date(2015, 2, 13),
|
||||
tag: 'a',
|
||||
description: 'done it',
|
||||
isDone: true
|
||||
},
|
||||
{
|
||||
created: new Date(2016, 2, 15),
|
||||
tag: 'go to school',
|
||||
description: 'done it'
|
||||
},
|
||||
{
|
||||
created: new Date(2017, 2, 15),
|
||||
tag: 'go to work',
|
||||
isDone: true
|
||||
},
|
||||
{
|
||||
created: new Date(2018, 2, 13),
|
||||
tag: 'go to work',
|
||||
description: 'done it',
|
||||
isDone: true
|
||||
},
|
||||
{
|
||||
created: new Date(2019, 3, 13),
|
||||
tag: 'go to work'
|
||||
},
|
||||
{
|
||||
created: new Date(2020, 2, 15),
|
||||
tag: 'go to school',
|
||||
description: 'done it',
|
||||
isDone: true
|
||||
},
|
||||
{
|
||||
created: new Date(2021, 2, 15),
|
||||
tag: 'go to school'
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
baseColor: { h: 180, s: 100, l: 50 },
|
||||
name: 'life',
|
||||
blocks: [
|
||||
{
|
||||
created: new Date(2019, 3, 13),
|
||||
tag: 'go home',
|
||||
description: 'done it'
|
||||
},
|
||||
{
|
||||
created: new Date(2019, 4, 13),
|
||||
tag: 'go home'
|
||||
},
|
||||
{
|
||||
created: new Date(2019, 4, 15),
|
||||
tag: 'go to work',
|
||||
description: 'done it'
|
||||
},
|
||||
{
|
||||
created: new Date(2019, 4, 15, 14),
|
||||
tag: 'go to work'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
|
||||
constructor() {
|
||||
const localStorageData = localStorage.getItem(LOCAL_STORAGE_KEY);
|
||||
const data = JSON.parse(localStorageData ? localStorageData : this.mockData);
|
||||
this.storedData = data.map(p => new Page(p));
|
||||
}
|
||||
|
||||
async load(): Promise<Page[]> {
|
||||
return this.storedData;
|
||||
}
|
||||
|
||||
async save(data: Page[]) {
|
||||
this.storedData = data;
|
||||
console.log('save', this.storedData);
|
||||
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(this.storedData));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue