|
| 1 | +import { LitElement, html, customElement } from 'lit-element'; |
| 2 | +import { store } from '../../store.js'; // connect to the Redux store. |
| 3 | +import { updateLayoutMode } from '../../actions/app.js'; // redux actions |
| 4 | +import './pl-toggle-layout.scss?external'; |
| 5 | + |
| 6 | +@customElement('pl-toggle-layout') |
| 7 | +class LayoutToggle extends LitElement { |
| 8 | + constructor(self) { |
| 9 | + self = super(self); |
| 10 | + self.handleClick = self.handleClick.bind(self); |
| 11 | + return self; |
| 12 | + } |
| 13 | + |
| 14 | + createRenderRoot() { |
| 15 | + return this; |
| 16 | + } |
| 17 | + |
| 18 | + connectedCallback() { |
| 19 | + if (super.connectedCallback) { |
| 20 | + super.connectedCallback(); |
| 21 | + } |
| 22 | + |
| 23 | + const state = store.getState(); |
| 24 | + this.layoutMode = state.app.layoutMode || 'vertical'; |
| 25 | + |
| 26 | + this.__storeUnsubscribe = store.subscribe(() => |
| 27 | + this._stateChanged(store.getState()) |
| 28 | + ); |
| 29 | + this._stateChanged(store.getState()); |
| 30 | + |
| 31 | + this.size = this.size || 'medium'; |
| 32 | + } |
| 33 | + |
| 34 | + disconnectedCallback() { |
| 35 | + this.__storeUnsubscribe && this.__storeUnsubscribe(); |
| 36 | + |
| 37 | + if (super.disconnectedCallback) { |
| 38 | + super.disconnectedCallback(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static get properties() { |
| 43 | + return { |
| 44 | + layoutMode: { |
| 45 | + attribute: true, |
| 46 | + type: String, |
| 47 | + }, |
| 48 | + text: { |
| 49 | + attribute: true, |
| 50 | + type: String, |
| 51 | + }, |
| 52 | + size: { |
| 53 | + attribute: true, |
| 54 | + type: String, |
| 55 | + }, |
| 56 | + iconOnly: { |
| 57 | + attribute: 'icon-only', |
| 58 | + type: Boolean, |
| 59 | + reflect: true, |
| 60 | + }, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + _stateChanged(state) { |
| 65 | + if (this.layoutMode !== state.app.layoutMode) { |
| 66 | + this.layoutMode = state.app.layoutMode; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + handleClick() { |
| 71 | + const getLayoutMode = |
| 72 | + this.layoutMode !== 'vertical' ? 'vertical' : 'horizontal'; |
| 73 | + |
| 74 | + this.layoutMode = getLayoutMode; |
| 75 | + store.dispatch(updateLayoutMode(this.layoutMode)); |
| 76 | + } |
| 77 | + |
| 78 | + render() { |
| 79 | + return html` |
| 80 | + <pl-button |
| 81 | + title="Switch Layout" |
| 82 | + @click="${this.handleClick}" |
| 83 | + size="${this.size}" |
| 84 | + ?icon-only="${this.iconOnly || false}" |
| 85 | + > |
| 86 | + ${this.text} |
| 87 | + <pl-icon |
| 88 | + slot="after" |
| 89 | + name="${this.layoutMode === 'horizontal' ? 'layout-h' : 'layout-v'}" |
| 90 | + ></pl-icon> |
| 91 | + </pl-button> |
| 92 | + `; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export { LayoutToggle }; |
0 commit comments