diff --git a/src/app/components/modal/modals/settings/settings.component.ts b/src/app/components/modal/modals/settings/settings.component.ts index 09e470c..b39886c 100644 --- a/src/app/components/modal/modals/settings/settings.component.ts +++ b/src/app/components/modal/modals/settings/settings.component.ts @@ -9,14 +9,13 @@ import { Page } from '../../../../model/page'; styleUrls: ['./settings.component.scss'] }) export class SettingsComponent implements OnInit { + page: Page; constructor(public modalService: ModalService, public dataService: DataService) {} ngOnInit() { this.modalService.active.input.subscribe(p => (this.page = p)); } - page: Page; - async deletePage() { try { await this.modalService.showRemovePage(this.page.name); diff --git a/src/app/components/pages/page/tower/tower.component.ts b/src/app/components/pages/page/tower/tower.component.ts index 1176eec..d9030b2 100644 --- a/src/app/components/pages/page/tower/tower.component.ts +++ b/src/app/components/pages/page/tower/tower.component.ts @@ -41,7 +41,7 @@ export class TowerComponent implements OnInit { this.blocks = value.coloredBlocks .filter(b => b.isDone) .map(b => { - let classedBlock = b as StyledBlock; + const classedBlock = b as StyledBlock; classedBlock.shouldDraw = true; classedBlock.style = { transform: 'translateY(0)', opacity: '1' }; classedBlock.cssClass = ''; @@ -52,7 +52,7 @@ export class TowerComponent implements OnInit { console.log(this.tower, this.tower.latestVersion, value); } if (this.tower && this.tower.latestVersion === value) { - let difference = this.tower.blocks.map((b, index) => { + const difference = this.tower.blocks.map((b, index) => { return b === value.blocks[index]; }); diff --git a/src/app/components/shared/double-slider/double-slider.component.ts b/src/app/components/shared/double-slider/double-slider.component.ts index 9a0278c..09f402a 100644 --- a/src/app/components/shared/double-slider/double-slider.component.ts +++ b/src/app/components/shared/double-slider/double-slider.component.ts @@ -93,7 +93,7 @@ export class DoubleSliderComponent { } private emitValue() { - const range = { + this.range.emit({ from: this.oneValue < this.otherValue ? this.values[this.indexFromValue(this.oneValue)] @@ -102,7 +102,6 @@ export class DoubleSliderComponent { this.oneValue < this.otherValue ? this.values[this.indexFromValue(this.otherValue)] : this.values[this.indexFromValue(this.oneValue)] - }; - this.range.emit(range); + }); } } diff --git a/src/app/model/block.ts b/src/app/model/block.ts index eab92c4..35b61d5 100644 --- a/src/app/model/block.ts +++ b/src/app/model/block.ts @@ -1,6 +1,5 @@ import { Serializable } from './serializable'; import { IBlock } from '../interfaces/persistance/block'; -import { Node } from '../store/node'; import { InnerNodeState } from '../store/inner-node'; export interface BlockState extends IBlock, InnerNodeState {} @@ -12,7 +11,6 @@ export class Block extends Serializable implements IBlock, BlockState { readonly tag: string; constructor(props: IBlock) { - console.log('b'); if (props.created.constructor.name !== 'Date') { props.created = new Date(props.created); } diff --git a/src/app/model/page.ts b/src/app/model/page.ts index b9493c9..3b4777a 100644 --- a/src/app/model/page.ts +++ b/src/app/model/page.ts @@ -1,7 +1,7 @@ import { Serializable } from './serializable'; import { IPage } from '../interfaces/persistance/page'; import { Tower } from './tower'; -import { InnerNodeState } from '../store/inner-node'; +import { InnerNode, InnerNodeState } from '../store/inner-node'; export interface PageState extends InnerNodeState, IPage {} @@ -17,7 +17,7 @@ export class Page extends Serializable implements IPage, PageState { }; constructor(props: IPage) { - super(props, 'Page'); + super(props, 'Page', props.towers.map(t => new Tower(t))); } get towers(): Array { diff --git a/src/app/model/serializable.ts b/src/app/model/serializable.ts index 52c9834..c8608b5 100644 --- a/src/app/model/serializable.ts +++ b/src/app/model/serializable.ts @@ -12,8 +12,8 @@ export class Serializable extends InnerNode { private static propertyList: any = {}; protected type: string; - protected constructor(properties: any, type: string) { - super(); + protected constructor(properties: any, type: string, children?: Array) { + super(children); const compiledType = this.constructor.name; if (!Serializable.propertyList.hasOwnProperty(compiledType)) { @@ -21,19 +21,7 @@ export class Serializable extends InnerNode { } for (const property in properties) { if (properties.hasOwnProperty(property)) { - const propertyValue = properties[property]; - // This should be ran after the original constructor has finished. - if (property === Serializable.childrenMap[type].childrenListName) { - new Promise(r => r()).then(() => { - const children = propertyValue.map( - c => - new Serializable.childrenMap[type].childrenConstructor(c, Serializable.childrenMap[type].childrenType) - ); - console.log(type, 'created'); - this.addChildren(children); - console.log(type, 'added'); - }); - } else { + if (property !== Serializable.childrenMap[type].childrenListName) { this[property] = properties[property]; } diff --git a/src/app/model/tower.ts b/src/app/model/tower.ts index fea3fac..bb2e29a 100644 --- a/src/app/model/tower.ts +++ b/src/app/model/tower.ts @@ -4,7 +4,7 @@ import { Block } from './block'; import { Serializable } from './serializable'; import { hash } from '../utils/hash'; import { IColor } from '../interfaces/color'; -import { InnerNodeState } from '../store/inner-node'; +import { InnerNode, InnerNodeState } from '../store/inner-node'; export type ColoredBlock = Block & { color: IColor }; @@ -23,7 +23,8 @@ export class Tower extends Serializable implements ITower, TowerState { readonly baseColor: IColor; constructor(props: ITower) { - super(props, 'Tower'); + super(props, 'Tower', props.blocks.map(b => new Block(b))); + this.onAfterClone(); } protected onAfterClone() { diff --git a/src/app/services/data.service.ts b/src/app/services/data.service.ts index 555acc3..f483e1b 100644 --- a/src/app/services/data.service.ts +++ b/src/app/services/data.service.ts @@ -13,9 +13,6 @@ import { Observable } from 'rxjs/internal/Observable'; providedIn: 'root' }) export class DataService extends Root { - private readonly _safeChildren: BehaviorSubject> = new BehaviorSubject(null); - readonly safeChildren$: Observable> = this._safeChildren.asObservable(); - constructor(private storeService: StoreService>) { super(); this.init().catch(); @@ -66,13 +63,9 @@ export class DataService extends Root { }; this.children$.subscribe(value => { this.log(); + this.save(0); }); this.addChildren(pages.map(p => new Page(p))); - - this.children$.subscribe(value => { - this._safeChildren.next(value); - this.save(0); - }); } } diff --git a/src/app/store/inner-node.ts b/src/app/store/inner-node.ts index 66409ba..d76277e 100644 --- a/src/app/store/inner-node.ts +++ b/src/app/store/inner-node.ts @@ -10,8 +10,9 @@ export class InnerNode extends Node implements InnerNodeState { private nextVersion: this = null; readonly children: Array = []; - constructor() { + constructor(children?: Array) { super(); + this.children = children ? children : []; } get latestVersion(): this { @@ -22,12 +23,12 @@ export class InnerNode extends Node implements InnerNodeState { return version; } - addChildren(children: Array) { - super.addChildren.call(this.latestVersion, children); + addChildren(children: Array): this { + return super.addChildren.call(this.latestVersion, children); } - replaceChild(update: { oldValue: InnerNode; newValue: InnerNode }) { - super.replaceChild.call(this.latestVersion, update); + replaceChild(update: { oldValue: InnerNode; newValue: InnerNode }): this { + return super.replaceChild.call(this.latestVersion, update); } changeKeys(props: Partial): this { diff --git a/src/app/store/node.ts b/src/app/store/node.ts index fc6363f..47c44e6 100644 --- a/src/app/store/node.ts +++ b/src/app/store/node.ts @@ -9,25 +9,28 @@ export abstract class Node extends Unique implements NodeState { protected copyCount = 1; abstract readonly children: Array; + protected constructor() { + super(); + } protected abstract changeKeys(props: Partial): this; protected initiate() { super.initiate(); - ++this.copyCount; + this.copyCount++; } - addChildren(children: Array) { - this.changeKeys({ + addChildren(children: Array): this { + return this.changeKeys({ children: [...this.children, ...children] }); } - replaceChild({ oldValue, newValue }: { oldValue: InnerNode; newValue: InnerNode }) { + replaceChild({ oldValue, newValue }: { oldValue: InnerNode; newValue: InnerNode }): this { if (oldValue === newValue) { - return; + return this; } - this.changeKeys({ + return this.changeKeys({ children: this.children.map(c => (c === oldValue ? newValue : c)) }); } diff --git a/src/app/store/root.ts b/src/app/store/root.ts index 7483da1..22764a4 100644 --- a/src/app/store/root.ts +++ b/src/app/store/root.ts @@ -4,11 +4,13 @@ import { Node, NodeState } from './node'; import { InnerNode } from './inner-node'; export class Root extends Node { - private readonly _children: BehaviorSubject> = new BehaviorSubject([]); - readonly children$: Observable> = this._children.asObservable(); + readonly children$: Observable>; + private readonly _children: BehaviorSubject>; - constructor() { + constructor(children?: Array) { super(); + this._children = new BehaviorSubject(children ? children : []); + this.children$ = this._children.asObservable(); } get children(): Array { @@ -16,6 +18,7 @@ export class Root extends Node { } set children(value: Array) { + console.log(value); this._children.next(value); } diff --git a/src/app/utils/hash.ts b/src/app/utils/hash.ts index ae2412b..dc783e3 100644 --- a/src/app/utils/hash.ts +++ b/src/app/utils/hash.ts @@ -4,8 +4,7 @@ export const hash = (text: string): number => { return 0; } const hashValue = Array.prototype.reduce.call( - // tslint:disable-next-line:no-bitwise - text, + text, // tslint:disable-next-line:no-bitwise (value, char) => ((value << 5) - value + (char.charCodeAt(0) as number)) | 0, 7 );