Extract navigate mixin (#1865)

* Extract navigate to function

* Remove eventsmixin properly and side effects
This commit is contained in:
Paulus Schoutsen 2018-10-27 10:56:03 +02:00 committed by GitHub
parent 13c5724d7c
commit 35194cf345
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 12 deletions

14
src/common/navigate.ts Normal file
View File

@ -0,0 +1,14 @@
import { fireEvent } from "./dom/fire_event.js";
export const navigate = (
node: HTMLElement,
path: string,
replace: boolean = false
) => {
if (replace) {
history.replaceState(null, "", path);
} else {
history.pushState(null, "", path);
}
fireEvent(node, "location-changed");
};

View File

@ -1,5 +1,5 @@
import { dedupingMixin } from "@polymer/polymer/lib/utils/mixin.js"; import { dedupingMixin } from "@polymer/polymer/lib/utils/mixin.js";
import EventsMixin from "./events-mixin"; import { navigate } from "../common/navigate";
/* /*
* @polymerMixin * @polymerMixin
@ -7,14 +7,9 @@ import EventsMixin from "./events-mixin";
*/ */
export default dedupingMixin( export default dedupingMixin(
(superClass) => (superClass) =>
class extends EventsMixin(superClass) { class extends superClass {
navigate(path, replace = false) { navigate(...args) {
if (replace) { navigate(this, ...args);
history.replaceState(null, null, path);
} else {
history.pushState(null, null, path);
}
this.fire("location-changed");
} }
} }
); );

View File

@ -52,7 +52,7 @@ class HaConfigDashboard extends NavigateMixin(LocalizeMixin(PolymerElement)) {
<template is="dom-if" if="[[computeIsLoaded(hass, 'cloud')]]"> <template is="dom-if" if="[[computeIsLoaded(hass, 'cloud')]]">
<paper-card> <paper-card>
<a href='/config/cloud' tabindex="-1"> <a href='/config/cloud' tabindex="-1">
<paper-item on-click="_navigate"> <paper-item>
<paper-item-body two-line=""> <paper-item-body two-line="">
[[localize('ui.panel.config.cloud.caption')]] [[localize('ui.panel.config.cloud.caption')]]
<template is="dom-if" if="[[cloudStatus.logged_in]]"> <template is="dom-if" if="[[cloudStatus.logged_in]]">

View File

@ -8,6 +8,7 @@ import NavigateMixin from "../../../mixins/navigate-mixin.js";
import "./ha-user-picker.js"; import "./ha-user-picker.js";
import "./ha-user-editor.js"; import "./ha-user-editor.js";
import { fireEvent } from "../../../common/dom/fire_event.js";
/* /*
* @appliesMixin NavigateMixin * @appliesMixin NavigateMixin
@ -68,8 +69,8 @@ class HaConfigUsers extends NavigateMixin(PolymerElement) {
_checkRoute(route) { _checkRoute(route) {
if (!route || route.path.substr(0, 6) !== "/users") return; if (!route || route.path.substr(0, 6) !== "/users") return;
// prevent list gettung under toolbar // prevent list getting under toolbar
this.fire("iron-resize"); fireEvent(this, "iron-resize");
this._debouncer = Debouncer.debounce( this._debouncer = Debouncer.debounce(
this._debouncer, this._debouncer,