Convert common dir to TS (#2580)

* Convert common dir to TS

* Lint

* Update setup-leaflet-map.ts
This commit is contained in:
Paulus Schoutsen 2019-01-27 10:40:46 -08:00 committed by GitHub
parent 7dda98f139
commit 9299d548ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 71 additions and 32 deletions

View File

@ -19,11 +19,11 @@ export default function applyThemesOnElement(
if (localTheme === "default" || (localTheme && themes.themes[localTheme])) { if (localTheme === "default" || (localTheme && themes.themes[localTheme])) {
themeName = localTheme; themeName = localTheme;
} }
const styles = Object.assign({}, element._themes); const styles = { ...element._themes };
if (themeName !== "default") { if (themeName !== "default") {
var theme = themes.themes[themeName]; const theme = themes.themes[themeName];
Object.keys(theme).forEach((key) => { Object.keys(theme).forEach((key) => {
var prefixedKey = "--" + key; const prefixedKey = "--" + key;
element._themes[prefixedKey] = ""; element._themes[prefixedKey] = "";
styles[prefixedKey] = theme[key]; styles[prefixedKey] = theme[key];
}); });
@ -35,12 +35,14 @@ export default function applyThemesOnElement(
window.ShadyCSS.styleSubtree(/** @type {!HTMLElement} */ (element), styles); window.ShadyCSS.styleSubtree(/** @type {!HTMLElement} */ (element), styles);
} }
if (!updateMeta) return; if (!updateMeta) {
return;
}
const meta = document.querySelector("meta[name=theme-color]"); const meta = document.querySelector("meta[name=theme-color]");
if (meta) { if (meta) {
if (!meta.hasAttribute("default-content")) { if (!meta.hasAttribute("default-content")) {
meta.setAttribute("default-content", meta.getAttribute("content")); meta.setAttribute("default-content", meta.getAttribute("content")!);
} }
const themeColor = const themeColor =
styles["--primary-color"] || meta.getAttribute("default-content"); styles["--primary-color"] || meta.getAttribute("default-content");

View File

@ -1,9 +1,13 @@
// Load a resource and get a promise when loading done. // Load a resource and get a promise when loading done.
// From: https://davidwalsh.name/javascript-loader // From: https://davidwalsh.name/javascript-loader
function _load(tag, url, type) { const _load = (
tag: "link" | "script" | "img",
url: string,
type?: "module"
) => {
// This promise will be used by Promise.all to determine success or failure // This promise will be used by Promise.all to determine success or failure
return new Promise(function(resolve, reject) { return new Promise((resolve, reject) => {
const element = document.createElement(tag); const element = document.createElement(tag);
let attr = "src"; let attr = "src";
let parent = "body"; let parent = "body";
@ -15,14 +19,14 @@ function _load(tag, url, type) {
// Need to set different attributes depending on tag type // Need to set different attributes depending on tag type
switch (tag) { switch (tag) {
case "script": case "script":
element.async = true; (element as HTMLScriptElement).async = true;
if (type) { if (type) {
element.type = type; (element as HTMLScriptElement).type = type;
} }
break; break;
case "link": case "link":
element.type = "text/css"; (element as HTMLLinkElement).type = "text/css";
element.rel = "stylesheet"; (element as HTMLLinkElement).rel = "stylesheet";
attr = "href"; attr = "href";
parent = "head"; parent = "head";
} }
@ -31,9 +35,9 @@ function _load(tag, url, type) {
element[attr] = url; element[attr] = url;
document[parent].appendChild(element); document[parent].appendChild(element);
}); });
} };
export const loadCSS = (url) => _load("link", url); export const loadCSS = (url: string) => _load("link", url);
export const loadJS = (url) => _load("script", url); export const loadJS = (url: string) => _load("script", url);
export const loadImg = (url) => _load("img", url); export const loadImg = (url: string) => _load("img", url);
export const loadModule = (url) => _load("script", url, "module"); export const loadModule = (url: string) => _load("script", url, "module");

View File

@ -11,23 +11,23 @@ export default function scrollToTarget(element, target) {
// the scroll event will trigger _updateScrollState directly, // the scroll event will trigger _updateScrollState directly,
// However, _updateScrollState relies on the previous `scrollTop` to update the states. // However, _updateScrollState relies on the previous `scrollTop` to update the states.
// Calling _updateScrollState will ensure that the states are synced correctly. // Calling _updateScrollState will ensure that the states are synced correctly.
var top = 0; const top = 0;
var scroller = target; const scroller = target;
var easingFn = function easeOutQuad(t, b, c, d) { const easingFn = function easeOutQuad(t, b, c, d) {
/* eslint-disable no-param-reassign, space-infix-ops, no-mixed-operators */ /* eslint-disable no-param-reassign, space-infix-ops, no-mixed-operators */
t /= d; t /= d;
return -c * t * (t - 2) + b; return -c * t * (t - 2) + b;
/* eslint-enable no-param-reassign, space-infix-ops, no-mixed-operators */ /* eslint-enable no-param-reassign, space-infix-ops, no-mixed-operators */
}; };
var animationId = Math.random(); const animationId = Math.random();
var duration = 200; const duration = 200;
var startTime = Date.now(); const startTime = Date.now();
var currentScrollTop = scroller.scrollTop; const currentScrollTop = scroller.scrollTop;
var deltaScrollTop = top - currentScrollTop; const deltaScrollTop = top - currentScrollTop;
element._currentAnimationId = animationId; element._currentAnimationId = animationId;
(function updateFrame() { (function updateFrame() {
var now = Date.now(); const now = Date.now();
var elapsedTime = now - startTime; const elapsedTime = now - startTime;
if (elapsedTime > duration) { if (elapsedTime > duration) {
scroller.scrollTop = top; scroller.scrollTop = top;
} else if (element._currentAnimationId === animationId) { } else if (element._currentAnimationId === animationId) {

View File

@ -1,5 +1,6 @@
// Sets up a Leaflet map on the provided DOM element // Sets up a Leaflet map on the provided DOM element
export const setupLeafletMap = async (mapElement) => { export const setupLeafletMap = async (mapElement) => {
// tslint:disable-next-line
const Leaflet = (await import(/* webpackChunkName: "leaflet" */ "leaflet")) const Leaflet = (await import(/* webpackChunkName: "leaflet" */ "leaflet"))
.default; .default;
Leaflet.Icon.Default.imagePath = "/static/images/leaflet"; Leaflet.Icon.Default.imagePath = "/static/images/leaflet";

View File

@ -1,11 +1,18 @@
export function onChangeEvent(prop, ev) { interface OnChangeComponent {
props: {
index: number;
onChange(index: number, data: object);
};
}
export function onChangeEvent(this: OnChangeComponent, prop, ev) {
const origData = this.props[prop]; const origData = this.props[prop];
if (ev.target.value === origData[ev.target.name]) { if (ev.target.value === origData[ev.target.name]) {
return; return;
} }
const data = Object.assign({}, origData); const data = { ...origData };
if (ev.target.value) { if (ev.target.value) {
data[ev.target.name] = ev.target.value; data[ev.target.name] = ev.target.value;

View File

@ -1,5 +1,9 @@
import { render } from "preact"; import { render } from "preact";
export default function unmount(mountEl) { export default function unmount(mountEl) {
render(() => null, mountEl); render(
// @ts-ignore
() => null,
mountEl
);
} }

View File

@ -7,14 +7,20 @@
export default function debounce(func, wait, immediate) { export default function debounce(func, wait, immediate) {
let timeout; let timeout;
return function(...args) { return function(...args) {
// tslint:disable:no-this-assignment
// @ts-ignore
const context = this; const context = this;
const later = () => { const later = () => {
timeout = null; timeout = null;
if (!immediate) func.apply(context, args); if (!immediate) {
func.apply(context, args);
}
}; };
const callNow = immediate && !timeout; const callNow = immediate && !timeout;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(later, wait); timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args); if (callNow) {
func.apply(context, args);
}
}; };
} }

View File

@ -93,7 +93,8 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) {
// web components have been loaded. // web components have been loaded.
this._debouncedConfigChanged = debounce( this._debouncedConfigChanged = debounce(
() => this._selectView(this._curView, true), () => this._selectView(this._curView, true),
100 100,
false
); );
} }

View File

@ -16,6 +16,20 @@ declare global {
var __VERSION__: string; var __VERSION__: string;
} }
declare global {
interface Window {
ShadyCSS: {
nativeCss: boolean;
nativeShadow: boolean;
prepareTemplate(templateElement, elementName, elementExtension);
styleElement(element);
styleSubtree(element, overrideProperties);
styleDocument(overrideProperties);
getComputedStyleValue(element, propertyName);
};
}
}
export interface WebhookError { export interface WebhookError {
code: number; code: number;
message: string; message: string;