Skip to content

Commit d808f7c

Browse files
committed
feat(api): Update item-preview to work with api updates
* `register-pie` is no longer emitted by pie instances * `session-changed` is emitted by pie instances
1 parent 7cdf3ef commit d808f7c

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/catalog-demo/item-preview.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ export default class ItemPreview extends HTMLElement {
4242
this._env = {
4343
mode: 'gather'
4444
}
45-
46-
this.addEventListener('pie.register', (e) => {
47-
let id = e.target.getAttribute('pie-id');
48-
this._registeredPies[id] = e.target;
49-
this._updatePies();
50-
});
5145
}
5246

5347
connectedCallback() {
@@ -94,6 +88,41 @@ export default class ItemPreview extends HTMLElement {
9488
return session;
9589
}
9690

91+
_registerPiesIfNeeded() {
92+
//TODO: could be a bit more thorough here..
93+
if (this._config.models.length === Object.keys(this._registeredPies).length) {
94+
return Promise.resolve();
95+
} else {
96+
return new Promise((resolve, reject) => {
97+
this._config.models.forEach(m => {
98+
const assignedNodes = this.shadowRoot.querySelector('slot').assignedNodes({ flatten: true });
99+
const el = assignedNodes.reduce((acc, node) => {
100+
if (acc) {
101+
return acc;
102+
} else {
103+
if (node.tagName.toLowerCase() === m.element && node.getAttribute('pie-id') === m.id) {
104+
return node;
105+
} else {
106+
const selector = `${m.element}[pie-id="${m.id}"]`;
107+
return node.querySelector(selector);
108+
}
109+
}
110+
}, null);
111+
112+
if (!el) {
113+
reject(new Error(`can't find element by ${m.element}, ${m.id}`));
114+
}
115+
this._registeredPies[m.id] = el;
116+
el.addEventListener('session-changed', e => {
117+
//TODO: update the status on session-changed..
118+
console.log('session-changed: ', e.detail);
119+
})
120+
});
121+
resolve();
122+
});
123+
}
124+
}
125+
97126
_updatePies(resetSession) {
98127

99128
if (!this._config || !this._controllers) {
@@ -104,16 +133,22 @@ export default class ItemPreview extends HTMLElement {
104133
this._sessions = [];
105134
}
106135

107-
let promises = Object.keys(this._registeredPies).map(id => {
108-
let node = this._registeredPies[id];
109-
let model = this._config.models.find(v => v.id === id);
110-
let session = this._getSessionById(id)
111-
let controller = this._controllers[node.nodeName.toLowerCase()];
112-
return controller.model(model, session, this._env)
113-
.then(m => ({ id: id, model: m, session: session }));
114-
});
136+
const elements = this._config.models.map(m => m.element);
137+
138+
const callControllerModel = () => {
139+
return Object.keys(this._registeredPies).map(id => {
140+
let node = this._registeredPies[id];
141+
let model = this._config.models.find(v => v.id === id);
142+
let session = this._getSessionById(id)
143+
let controller = this._controllers[node.nodeName.toLowerCase()];
144+
return controller.model(model, session, this._env)
145+
.then(m => ({ id: id, model: m, session: session }));
146+
});
147+
}
115148

116-
Promise.all(promises)
149+
Promise.all(elements.map(e => customElements.whenDefined(e)))
150+
.then(() => this._registerPiesIfNeeded())
151+
.then(() => Promise.all(callControllerModel()))
117152
.then(results => {
118153
results.forEach(({ id, model, session }) => {
119154
const node = this._registeredPies[id];

0 commit comments

Comments
 (0)