UNPKG

4.1 kBJavaScriptView Raw
1import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
2
3const appMapping = {
4 'make-music': 'music',
5 'make-light': 'art',
6 'kano-draw': 'art',
7 'make-apps': 'app',
8 lightboard: 'app',
9};
10const loadedImports = {};
11
12class KwcSharePlayer extends PolymerElement {
13 static get template() {
14 return html`
15 <style>
16 kwc-app-player,
17 kwc-art-player {
18 animation: fade-in 200ms linear;
19 }
20 </style>
21 <template is="dom-if" if="[[_usePlayer('app', _player)]]" restamp>
22 <kwc-app-player share="[[share]]" display-code="[[displayCode]]" on-hide-code="_hideCode">
23 <slot slot="hardware" name="hardware-list"></slot>
24 </kwc-app-player>
25 </template>
26 <template is="dom-if" if="[[_usePlayer('art', _player)]]" restamp>
27 <kwc-art-player share="[[share]]" display-code="[[displayCode]]"></kwc-art-player>
28 </template>
29 <template is="dom-if" if="[[_usePlayer('music', _player)]]" restamp>
30 <kwc-music-player share="[[share]]"></kwc-music-player>
31 </template>
32 <template is="dom-if" if="[[_usePlayer('default', _player)]]" restamp>
33 <kwc-player share="[[share]]"></kwc-player>
34 </template>
35 `;
36 }
37 static get properties() {
38 return {
39 /**
40 * Flag to indicate to the player whether the show code display element
41 * or not.
42 * @type {Boolean}
43 */
44 displayCode: {
45 type: Boolean,
46 value: false,
47 notify: true,
48 },
49 /**
50 * The current share to be played.
51 * @type {Object}
52 */
53 share: {
54 type: Object,
55 value: () => ({}),
56 },
57 /**
58 * String to select which player to use for a given share.
59 * @type {String}
60 */
61 _player: {
62 type: String,
63 value: '',
64 },
65 };
66 }
67 static get observers() {
68 return [
69 '_shareChanged(share.*)',
70 ];
71 }
72 /** OBSERVERS * */
73 /**
74 * The share data is used to set the _player property which selects
75 * which player to use. It will import the player if not imported
76 * previously and then trigger a change in the iron pages.
77 * @param {Object} share Current share data
78 */
79 _shareChanged(shareChange) {
80 const share = shareChange.base;
81
82 if (!share || !Object.keys(share).length) {
83 return;
84 }
85 const player = appMapping[share.app] || 'default';
86
87 if (loadedImports[player]) {
88 this.set('_player', player);
89 } else {
90 this.lazyImport(player).then(() => {
91 loadedImports[player] = true;
92 this.set('_player', player);
93 });
94 }
95 }
96 lazyImport(id) {
97 // Switch to make sure bundler resolve the import
98 switch (id) {
99 case 'app': {
100 return import('./kwc-app-player.js');
101 }
102 case 'art': {
103 return import('./kwc-art-player.js');
104 }
105 case 'music': {
106 return import('./kwc-music-player.js');
107 }
108 default: {
109 return import('./kwc-player.js');
110 }
111 }
112 }
113 /**
114 * Boolean to indicate iron pages which page to display.
115 * @param {String} key from iton page
116 * @param {String} player selected for current page
117 * @return {Boolean} do the key and player match?
118 */
119 _usePlayer(key, player) {
120 return key === player;
121 }
122 /** EVENT HANDLERS* */
123 /**
124 * Set the property responsible for displaying and hiding the
125 * display code element.
126 *
127 * @event hide-code
128 */
129 _hideCode(e) {
130 this.displayCode = false;
131 this.dispatchEvent(new CustomEvent('hide-code', { detail: e.detail }));
132 }
133}
134
135
136customElements.define('kwc-share-player', KwcSharePlayer);