Skip to content

Commit

Permalink
Merge i03-display-selector in develop
Browse files Browse the repository at this point in the history
  • Loading branch information
olange committed Mar 6, 2019
2 parents d1504ca + 725ab51 commit b0faafd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 4 deletions.
56 changes: 56 additions & 0 deletions packages/dia-show/dia-display-selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { LitElement, html, css } from "lit-element";
import { CommonStyles } from "./shared-styles.js";

export class DiaDisplaySelector extends LitElement {
static get styles() {
return [
CommonStyles,
css`
:host { display: inline-block }
div.select {
display: inline-block;
background-color: white;
border-radius: 1em;
padding: 0.25em 0.5em }
span.item {
padding: 0 0.5em;
cursor: pointer }
span.item:hover {
color: blue }
`
];
};

static get properties() {
return {
displayList: { type: Object, attribute: false } // an object of type `Set` actually
}
}

render() {
return html`<div class="select">${
Array.from( this.displayList.values())
.map(( display) =>
html`<span id="${display}" class="item"
@click=${this.selected}>${display}</span>`)
}</div>`;
}

constructor() {
super();
this.displayList = new Set();
}

selected( e) {
const selectedDisplay = e.target.id;
this.dispatchEvent(
new CustomEvent( "display-selected", {
detail: { display: selectedDisplay },
bubbles: true, composed: true
})
);
}
}

// Register the element with the browser
customElements.define( "dia-display-selector", DiaDisplaySelector);
35 changes: 33 additions & 2 deletions packages/dia-show/dia-show.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,37 @@ export class DiaShow extends LitElement {
render() {
return html`
<div>‹dia-show slide ${this.slide}, display ${this.display}</div>
<dia-display-selector .displayList=${this._displayList}></dia-display-selector>
<slot></slot>
`;
}

constructor() {
super();

// Attach event listeners
this.addEventListener( "display-selected", this._displaySelected);

// Public observed properties
this.slide = undefined;
this.display = undefined;

// Private properties
this._displayList = this._enumerateDisplays(); // returns a `Set`
}

firstUpdated() {
// Sets the new slide when the custom event `slide-selected` is fired from a child.
this.addEventListener("slide-selected", (e) => {
e.stopPropagation()
e.stopPropagation();
if(this.slide == undefined){
this.slide = e.detail.slide;
}
});

// Sets the new display when the custom event `display-selected` is fired from a child.
this.addEventListener("display-selected", (e) => {
e.stopPropagation()
e.stopPropagation();
if(this.display == undefined){
this.display = e.detail.display;
}
Expand Down Expand Up @@ -106,6 +114,29 @@ export class DiaShow extends LitElement {
fullscreen() {
this.requestFullscreen();
}

// Returns a `Set` of distinct display identifiers used
// on child ‹dia-po› elements
_enumerateDisplays() {
const diapoElements = this.querySelectorAll( "dia-po"),
displays = new Set();

diapoElements.forEach(( element) => {
// We use `getAttribute( "display")` to get the attribute value
// from the DOM, instead of `element.display`, because at the time
// this method gets called, the ‹dia-po› custom element might not
// have been defined yet
displays.add( element.getAttribute( "display"));
});
return displays;
}

_displaySelected( e) {
e.stopPropagation();
const selectedDisplay = e.detail.display;
console.log( `dia-show › _displaySelected(): ${selectedDisplay}`);
this.display = selectedDisplay;
}
}

// Register the element with the browser
Expand Down
4 changes: 3 additions & 1 deletion packages/dia-show/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { DiaShow } from "./dia-show.js";
import { DiaSlide } from "./dia-slide.js";
import { DiaPo } from "./dia-po.js";
import { DiaController } from "./dia-controller.js";
import { DiaDisplaySelector } from "./dia-display-selector.js";

export default {
DiaShow,
DiaSlide,
DiaPo,
DiaController
DiaController,
DiaDisplaySelector
}
2 changes: 1 addition & 1 deletion packages/dia-show/shared-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { css } from "lit-element";

const
CommonStyles = css`
:host([hidden]) { display: none }
:host([ hidden]) { display: none }
`,
DiaShowStyles = css`
:host {
Expand Down

0 comments on commit b0faafd

Please sign in to comment.