Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
feat(linear-progress): Convert JS to TypeScript
Browse files Browse the repository at this point in the history
Refs #4225
  • Loading branch information
acdvorak committed Jan 19, 2019
1 parent d76279d commit 566ceb3
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/material-components-web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import * as formField from '@material/form-field/index';
import * as gridList from '@material/grid-list/index';
import * as iconButton from '@material/icon-button/index';
import * as iconToggle from '@material/icon-toggle/index';
import * as linearProgress from '@material/linear-progress/index';
import * as linearProgress from '@material/linear-progress/index.ts';
import * as lineRipple from '@material/line-ripple/index';
import * as list from '@material/list/index';
import * as menu from '@material/menu/index';
Expand Down
33 changes: 33 additions & 0 deletions packages/mdc-linear-progress/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

interface MDCLinearProgressAdapter {
addClass(className: string): void;
getBuffer(): HTMLElement;
getPrimaryBar(): HTMLElement;
hasClass(className: string): boolean;
removeClass(className: string): void;
setStyle(el: HTMLElement, styleProperty: string, value: string): void;
}

export default MDCLinearProgressAdapter;
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export const cssClasses = {
};

export const strings = {
PRIMARY_BAR_SELECTOR: '.mdc-linear-progress__primary-bar',
BUFFER_SELECTOR: '.mdc-linear-progress__buffer',
PRIMARY_BAR_SELECTOR: '.mdc-linear-progress__primary-bar',
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
* THE SOFTWARE.
*/

import {transformStyleProperties} from '@material/animation/index';
import MDCFoundation from '@material/base/foundation';
import {transformStyleProperties} from '@material/animation/index.ts';
import MDCLinearProgressAdapter from './adapter';

import {cssClasses, strings} from './constants';

export default class MDCLinearProgressFoundation extends MDCFoundation {
export default class MDCLinearProgressFoundation extends MDCFoundation<MDCLinearProgressAdapter> {
static get cssClasses() {
return cssClasses;
}
Expand All @@ -35,30 +36,32 @@ export default class MDCLinearProgressFoundation extends MDCFoundation {
return strings;
}

static get defaultAdapter() {
static get defaultAdapter(): MDCLinearProgressAdapter {
/* tslint:disable:no-empty */
return {
addClass: (/* className: string */) => {},
getPrimaryBar: () => /* el: Element */ {},
getBuffer: () => /* el: Element */ {},
hasClass: (/* className: string */) => false,
removeClass: (/* className: string */) => {},
setStyle: (/* el: Element, styleProperty: string, value: string */) => {},
addClass: (_className: string) => {},
getBuffer: () => null,
getPrimaryBar: () => null,
hasClass: (_className: string) => false,
removeClass: (_className: string) => {},
setStyle: (_el: HTMLElement, _styleProperty: string, _value: string) => {},
};
/* tslint:enable:no-empty */
}

constructor(adapter) {
super(Object.assign(MDCLinearProgressFoundation.defaultAdapter, adapter));
}
private isDeterminate_: boolean;
private isReversed_: boolean;
private progress_: number;

init() {
this.determinate_ = !this.adapter_.hasClass(cssClasses.INDETERMINATE_CLASS);
this.reverse_ = this.adapter_.hasClass(cssClasses.REVERSED_CLASS);
this.isDeterminate_ = !this.adapter_.hasClass(cssClasses.INDETERMINATE_CLASS);
this.isReversed_ = this.adapter_.hasClass(cssClasses.REVERSED_CLASS);
this.progress_ = 0;
}

setDeterminate(isDeterminate) {
this.determinate_ = isDeterminate;
if (this.determinate_) {
setDeterminate(isDeterminate: boolean) {
this.isDeterminate_ = isDeterminate;
if (this.isDeterminate_) {
this.adapter_.removeClass(cssClasses.INDETERMINATE_CLASS);
this.setScale_(this.adapter_.getPrimaryBar(), this.progress_);
} else {
Expand All @@ -68,22 +71,22 @@ export default class MDCLinearProgressFoundation extends MDCFoundation {
}
}

setProgress(value) {
setProgress(value: number) {
this.progress_ = value;
if (this.determinate_) {
if (this.isDeterminate_) {
this.setScale_(this.adapter_.getPrimaryBar(), value);
}
}

setBuffer(value) {
if (this.determinate_) {
setBuffer(value: number) {
if (this.isDeterminate_) {
this.setScale_(this.adapter_.getBuffer(), value);
}
}

setReverse(isReversed) {
this.reverse_ = isReversed;
if (this.reverse_) {
setReverse(isReversed: boolean) {
this.isReversed_ = isReversed;
if (this.isReversed_) {
this.adapter_.addClass(cssClasses.REVERSED_CLASS);
} else {
this.adapter_.removeClass(cssClasses.REVERSED_CLASS);
Expand All @@ -98,7 +101,7 @@ export default class MDCLinearProgressFoundation extends MDCFoundation {
this.adapter_.addClass(cssClasses.CLOSED_CLASS);
}

setScale_(el, scaleValue) {
private setScale_(el: HTMLElement, scaleValue: number) {
const value = 'scaleX(' + scaleValue + ')';
transformStyleProperties.forEach((transformStyleProperty) => {
this.adapter_.setStyle(el, transformStyleProperty, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ import MDCLinearProgressFoundation from './foundation';

export {MDCLinearProgressFoundation};

export class MDCLinearProgress extends MDCComponent {
static attachTo(root) {
export class MDCLinearProgress extends MDCComponent<MDCLinearProgressFoundation> {
static attachTo(root: Element) {
return new MDCLinearProgress(root);
}

set determinate(value) {
set determinate(value: boolean) {
this.foundation_.setDeterminate(value);
}

set progress(value) {
set progress(value: number) {
this.foundation_.setProgress(value);
}

set buffer(value) {
set buffer(value: number) {
this.foundation_.setBuffer(value);
}

set reverse(value) {
set reverse(value: boolean) {
this.foundation_.setReverse(value);
}

Expand All @@ -57,12 +57,12 @@ export class MDCLinearProgress extends MDCComponent {

getDefaultFoundation() {
return new MDCLinearProgressFoundation({
addClass: (className) => this.root_.classList.add(className),
getPrimaryBar: () => this.root_.querySelector(MDCLinearProgressFoundation.strings.PRIMARY_BAR_SELECTOR),
addClass: (className: string) => this.root_.classList.add(className),
getBuffer: () => this.root_.querySelector(MDCLinearProgressFoundation.strings.BUFFER_SELECTOR),
hasClass: (className) => this.root_.classList.contains(className),
removeClass: (className) => this.root_.classList.remove(className),
setStyle: (el, styleProperty, value) => el.style[styleProperty] = value,
getPrimaryBar: () => this.root_.querySelector(MDCLinearProgressFoundation.strings.PRIMARY_BAR_SELECTOR),
hasClass: (className: string) => this.root_.classList.contains(className),
removeClass: (className: string) => this.root_.classList.remove(className),
setStyle: (el: HTMLElement, styleProperty: string, value: string) => el.style.setProperty(styleProperty, value),
});
}
}
2 changes: 1 addition & 1 deletion scripts/webpack/js-bundle-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class JsBundleFactory {
iconToggle: getAbsolutePath('/packages/mdc-icon-toggle/index.js'),
list: getAbsolutePath('/packages/mdc-list/index.js'),
lineRipple: getAbsolutePath('/packages/mdc-line-ripple/index.js'),
linearProgress: getAbsolutePath('/packages/mdc-linear-progress/index.js'),
linearProgress: getAbsolutePath('/packages/mdc-linear-progress/index.ts'),
menu: getAbsolutePath('/packages/mdc-menu/index.js'),
menuSurface: getAbsolutePath('/packages/mdc-menu-surface/index.js'),
notchedOutline: getAbsolutePath('/packages/mdc-notched-outline/index.js'),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/mdc-linear-progress/mdc-linear-progress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import {assert} from 'chai';
import bel from 'bel';

import {MDCLinearProgress, MDCLinearProgressFoundation} from '../../../packages/mdc-linear-progress/index';
import {MDCLinearProgress, MDCLinearProgressFoundation} from '../../../packages/mdc-linear-progress/index.ts';

function getFixture() {
return bel`
Expand Down

0 comments on commit 566ceb3

Please sign in to comment.