Commit da8a54be authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

std-switch: Use private fields in SwitchTrack class.

This CL has no behavior changes.

Bug: 972476
Change-Id: I0c42df15da53b361602e64ae6891ee9c45a9ab9a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1777448
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Auto-Submit: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarDomenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#692147}
parent 2f2b401b
...@@ -2,60 +2,57 @@ ...@@ -2,60 +2,57 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Private property symbols
// TODO(tkent): Use private fields.
const _value = Symbol('A boolean track value field');
const _trackElement = Symbol('A track element field');
const _fillElement = Symbol('A trackFill element field');
const _slotElement = Symbol('A slot element field');
export class SwitchTrack { export class SwitchTrack {
#value;
#trackElement;
#fillElement;
#slotElement;
/** /**
* @param {!Document} factory A factory for elements created for this track. * @param {!Document} factory A factory for elements created for this track.
*/ */
constructor(factory) { constructor(factory) {
this[_value] = false; this.#value = false;
this._initializeDOM(factory); this.#initializeDOM(factory);
} }
/** /**
* @return {!Element} * @return {!Element}
*/ */
get element() { get element() {
return this[_trackElement]; return this.#trackElement;
} }
/** /**
* @param {Boolean} newValue * @param {Boolean} newValue
*/ */
set value(newValue) { set value(newValue) {
const oldValue = this[_value]; const oldValue = this.#value;
this[_value] = Boolean(newValue); this.#value = Boolean(newValue);
const bar = this[_fillElement]; const bar = this.#fillElement;
if (bar) { if (bar) {
bar.style.inlineSize = this[_value] ? '100%' : '0%'; bar.style.inlineSize = this.#value ? '100%' : '0%';
if (oldValue !== this[_value]) { if (oldValue !== this.#value) {
this._addSlot(); this.#addSlot();
} }
} }
} }
// TODO(tkent): Use private fields. #initializeDOM = factory => { ...};
/** /**
* @param {!Document} factory A factory for elements created for this track. * @param {!Document} factory A factory for elements created for this track.
*/ */
_initializeDOM(factory) { #initializeDOM = factory => {
this[_trackElement] = factory.createElement('div'); this.#trackElement = factory.createElement('div');
this[_trackElement].id = 'track'; this.#trackElement.id = 'track';
this[_trackElement].part.add('track'); this.#trackElement.part.add('track');
this[_fillElement] = factory.createElement('span'); this.#fillElement = factory.createElement('span');
this[_fillElement].id = 'trackFill'; this.#fillElement.id = 'trackFill';
this[_fillElement].part.add('track-fill'); this.#fillElement.part.add('track-fill');
this[_trackElement].appendChild(this[_fillElement]); this.#trackElement.appendChild(this.#fillElement);
this[_slotElement] = factory.createElement('slot'); this.#slotElement = factory.createElement('slot');
this._addSlot(); this.#addSlot();
} };
/** /**
* Add the <slot> * Add the <slot>
...@@ -63,11 +60,11 @@ export class SwitchTrack { ...@@ -63,11 +60,11 @@ export class SwitchTrack {
* - as a child of _fillElement if _value is false * - as a child of _fillElement if _value is false
* This behavior is helpful to show text in the track. * This behavior is helpful to show text in the track.
*/ */
_addSlot() { #addSlot = () => {
if (this[_value]) { if (this.#value) {
this[_fillElement].appendChild(this[_slotElement]); this.#fillElement.appendChild(this.#slotElement);
} else { } else {
this[_trackElement].appendChild(this[_slotElement]); this.#trackElement.appendChild(this.#slotElement);
} }
} };
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment