From 6f23cf88f362122cee735f8df314dc4caae87947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 16 Jun 2022 19:08:59 +0200 Subject: Change to proper javascript privates. --- doc/ref/javascript/vevent.texi | 8 ++++++-- static/components/changelog.ts | 2 +- static/components/input-list.ts | 6 +++--- static/vevent.ts | 28 +++++++++++++++++----------- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/doc/ref/javascript/vevent.texi b/doc/ref/javascript/vevent.texi index 4ceaa380..97d15f2a 100644 --- a/doc/ref/javascript/vevent.texi +++ b/doc/ref/javascript/vevent.texi @@ -28,7 +28,7 @@ Component for a single instance of a calendar event. Almost all data access should go through @code{getProperty} and @code{setProperty}, with the exception of the current calendar (which is accessed directly through @code{calendar}). Almost all changes through these interfaces -are logged, and can be viewed in @var{_changelog}. +are logged, and can be viewed through @var{changelog}. @deftypemethod VEvent {any?} getProperty {key: string} @anchor{VEvent.getProperty} @@ -68,12 +68,16 @@ list, but only calls @var{redraw} once at the end. Returns an iterator of all our properties. @end deftypemethod -@deftypeivar VEvent {ChangeLogEntry[]} {_changelog} +@deftypeivar VEvent {ChangeLogEntry[]} {#changelog} Every write through getProperty gets logged here, and can be consumed. Hopefully this will one day turn into an undo system. @ref{ChangeLogEntry}. @end deftypeivar +@deftypeivar VEvent {IterableIterator<[number, ChangeLogEntry]>} changelog +Public (read only) interface to changelog. +@end deftypeivar + @deftypeivar VEvent {string?} calendar The name of the calendar which this event belongs to. @end deftypeivar diff --git a/static/components/changelog.ts b/static/components/changelog.ts index 831e4ced..d08f7cb3 100644 --- a/static/components/changelog.ts +++ b/static/components/changelog.ts @@ -21,7 +21,7 @@ class VEventChangelog extends ComponentVEvent { redraw(data: VEvent) { /* TODO only redraw what is needed */ let children = [] - for (let el of data._changelog) { + for (let [_, el] of data.changelog) { let msg = ''; switch (el.type) { case 'property': diff --git a/static/components/input-list.ts b/static/components/input-list.ts index c31066da..34696e3e 100644 --- a/static/components/input-list.ts +++ b/static/components/input-list.ts @@ -10,7 +10,7 @@ class InputList extends HTMLElement { el: HTMLInputElement; - private _listeners: [string, (e: Event) => void][] = []; + #listeners: [string, (e: Event) => void][] = []; constructor() { super(); @@ -48,7 +48,7 @@ class InputList extends HTMLElement { } }); - for (let [type, proc] of this._listeners) { + for (let [type, proc] of this.#listeners) { new_el.addEventListener(type, proc); } @@ -113,7 +113,7 @@ class InputList extends HTMLElement { addEventListener(type: string, proc: ((e: Event) => void)) { // if (type != 'input') throw "Only input supported"; - this._listeners.push([type, proc]) + this.#listeners.push([type, proc]) for (let child of this.children) { child.addEventListener(type, proc); diff --git a/static/vevent.ts b/static/vevent.ts index 5419eb60..6a2c6f0f 100644 --- a/static/vevent.ts +++ b/static/vevent.ts @@ -101,31 +101,37 @@ class VEvent { */ registered: Redrawable[] - _calendar: string | null = null; + #calendar: string | null = null; - _changelog: ChangeLogEntry[] = [] + #changelog: ChangeLogEntry[] = [] + + /* Iterator instead of direct return to ensure the receiver doesn't + modify the array */ + get changelog(): IterableIterator<[number, ChangeLogEntry]> { + return this.#changelog.entries(); + } addlog(entry: ChangeLogEntry) { - let len = this._changelog.length - let last = this._changelog[len - 1] + let len = this.#changelog.length + let last = this.#changelog[len - 1] // console.log('entry = ', entry, ', last = ', last); if (!last) { // console.log('Adding new entry', entry, this.getProperty('uid')); - this._changelog.push(entry); + this.#changelog.push(entry); return; } if (entry.type === last.type && entry.name === last.name && entry.from === last.to) { - this._changelog.pop(); + this.#changelog.pop(); entry.from = last.from // console.log('Changing old entry', entry, this.getProperty('uid')); - this._changelog.push(entry) + this.#changelog.push(entry) } else { - this._changelog.push(entry) + this.#changelog.push(entry) } } @@ -244,17 +250,17 @@ class VEvent { this.addlog({ type: 'calendar', name: '', - from: this._calendar, + from: this.#calendar, to: calendar, }); - this._calendar = calendar; + this.#calendar = calendar; for (let el of this.registered) { el.redraw(this); } } get calendar(): string | null { - return this._calendar; + return this.#calendar; } register(htmlNode: Redrawable) { -- cgit v1.2.3