import { Component, ViewChild, OnInit, OnDestroy } from '@angular/core'; import { AlertController, ToastController, Events } from 'ionic-angular'; import { DateIdUtil } from '@canvuus-internal/mvp0-task-core'; import { UniversalNote } from '@canvuus-internal/mvp0-task-data'; import { CommonTextEntryComponent, CommonMarkEntryComponent } from '@canvuus-internal/mvp0-task-mark'; // import { CommonMarkEditorComponent } from '@canvuus-internal/mvp0-task-mark'; import { MarkEvents } from '@canvuus-internal/mvp0-task-mark'; import { LocalUserService } from '@canvuus-internal/mvp0-task-view'; import { CommonEvents } from '@canvuus-internal/mvp0-task-view'; @Component({ selector: 'mvp0-universal-note', // templateUrl: 'mvp0-universal-note.html' template: `

` }) export class Mvp0UniversalNoteComponent implements OnInit, OnDestroy { @ViewChild("commonTextEntry") commonTextEntry: CommonTextEntryComponent; @ViewChild("commonMarkEntry") commonMarkEntry: CommonMarkEntryComponent; // @ViewChild("commonMarkEditor") commonMarkEditor: CommonMarkEditorComponent; // temporary // title: string; data: UniversalNote; _isShowHTML: boolean = false; get isShowHTML(): boolean { return this._isShowHTML; } set isShowHTML(_isShowHTML: boolean) { this._isShowHTML = _isShowHTML; // tbd: refresh the screen???? } flipMarkdownHTMLView(event) { this.isShowHTML = !this._isShowHTML; } private _textChangedEventHandler: (newText: string) => void; constructor(private alertCtrl: AlertController, private toastCtrl: ToastController, private events: Events, private localUserService: LocalUserService ) { // console.log('Hello Mvp0UniversalNote Component'); this._textChangedEventHandler = (newText: string) => { this.udpateMarkdownText(newText); } // temporary this.data = new UniversalNote(); this.data.userId = this.localUserService.currentUserId; // .... // tbd.... // this.title = '(Title)'; } ngOnInit() { // console.log('::: ngOnInit()'); this.events.subscribe(MarkEvents.MVP0_TEXT_ENTRY_TEXT_CHANGED, this._textChangedEventHandler); } ngOnDestroy() { // console.log('::: ngOnDestroy()'); this.events.unsubscribe(MarkEvents.MVP0_TEXT_ENTRY_TEXT_CHANGED, this._textChangedEventHandler); } // titleChanged(newTitle) { // console.log('titleChanged(): newTitle = ' + newTitle); // this.data.setTitle(newTitle); // } // titleChanged(event) { // console.log('titleChanged(): event = ' + event); // this.data.isDirty = true; // } titleChanged(newTitle) { console.log('Mvp0UniversalNoteComponent: titleChanged(): newTitle = ' + newTitle); this.data.setTitle(newTitle); } udpateMarkdownText(newText) { // console.log('udpateMarkdownText(): newText = ' + newText); this.data.setMarkdownText(newText); // Not needed, obviously. // this.commonTextEntry.setTextInput(newText); let showDebugLog = false; this.commonMarkEntry.setMarkdownInput(newText, showDebugLog); } // tbd refreshData(_data: UniversalNote): void { console.log('Mvp0UniversalNoteComponent: >>> refreshData() _data = ' + _data); if (_data) { this.data = _data.clone(); // ?? } else { // ???? } // tbd.... // if (this.data.title) { // this.title = this.data.title; // } else { // // ???? // // let todayId = this.localUserService.todayId; // // this.title = DateIdUtil.getLocaleDateString(todayId); // this.title = ''; // } // ???? if (!this.data.markdownText) { this.data.markdownText = ''; } this.commonTextEntry.setTextInput(this.data.markdownText); this.commonMarkEntry.setMarkdownInput(this.data.markdownText); // console.log('>>> refreshData() this.data = ' + this.data); } saveNote(): void { // console.log('saveNote()'); // // console.log('%%%%%%%% saveNote(): this.data = ' + this.data); // ??? if (this.data.isSynched) { this.events.publish(CommonEvents.MVP0_DATA_UNIVERSALNOTE_UPDATE, this.data); } else { this.events.publish(CommonEvents.MVP0_DATA_UNIVERSALNOTE_CREATE, this.data); } // this.events.publish(CommonEvents.MVP0_NAV_DISMISS_SELF, true); // tbd: // This should be really done by the parent, but just as a shortcut... // (note: this should not be necessary if we dismiss the dialog after saving...) this.data.isDirty = false; // This can be a problem, if saving fails in the parent page.... // ... } }