@miso.ai/client-sdk-ui 1.10.0-beta.9 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/actor/index.js +0 -7
- package/dist/cjs/actor/tracker.js +47 -102
- package/dist/cjs/actor/utils.js +1 -1
- package/dist/cjs/actor/view.js +12 -1
- package/dist/cjs/actor/views.js +24 -2
- package/dist/cjs/asset/svgs.js +14 -4
- package/dist/cjs/constants.js +3 -2
- package/dist/cjs/layout/affiliation/affiliation.js +150 -58
- package/dist/cjs/layout/input/option-list.js +34 -49
- package/dist/cjs/layout/list/collection.js +22 -43
- package/dist/cjs/layout/templates.js +54 -32
- package/dist/cjs/layout/text/typewriter/index.js +12 -7
- package/dist/cjs/layout/trackable.js +124 -0
- package/dist/cjs/util/bindings.js +58 -110
- package/dist/cjs/util/states.js +31 -24
- package/dist/cjs/util/trackers.js +75 -0
- package/dist/cjs/util/viewables.js +55 -0
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/workflow/ask.js +1 -5
- package/dist/cjs/workflow/base.js +2 -7
- package/dist/cjs/workflow/recommendation.js +2 -0
- package/package.json +2 -2
- package/src/actor/index.js +0 -1
- package/src/actor/tracker.js +37 -94
- package/src/actor/utils.js +1 -1
- package/src/actor/view.js +14 -1
- package/src/actor/views.js +30 -1
- package/src/asset/svgs.js +13 -2
- package/src/constants.js +7 -1
- package/src/layout/affiliation/affiliation.js +142 -55
- package/src/layout/input/option-list.js +35 -43
- package/src/layout/list/collection.js +20 -38
- package/src/layout/templates.js +25 -19
- package/src/layout/text/typewriter/index.js +8 -6
- package/src/layout/trackable.js +106 -0
- package/src/util/bindings.js +47 -103
- package/src/util/states.js +26 -20
- package/src/util/trackers.js +69 -0
- package/src/util/viewables.js +47 -0
- package/src/version.js +1 -1
- package/src/workflow/ask.js +1 -4
- package/src/workflow/base.js +3 -5
- package/src/workflow/recommendation.js +1 -0
- package/dist/cjs/actor/trackers.js +0 -51
- package/dist/cjs/layout/affiliation/viewable.js +0 -65
- package/dist/cjs/layout/text/typewriter/progress.js +0 -45
- package/dist/cjs/util/tracker.js +0 -348
- package/src/actor/trackers.js +0 -45
- package/src/layout/affiliation/viewable.js +0 -61
- package/src/layout/text/typewriter/progress.js +0 -33
- package/src/util/tracker.js +0 -318
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CarouselItemViewabilityObserver, requestAnimationFrame as raf } from '@miso.ai/commons';
|
|
2
|
+
import { STATUS, LAYOUT_CATEGORY } from '../../constants.js';
|
|
2
3
|
import CollectionLayout from '../list/collection.js';
|
|
3
|
-
import { affiliation } from '../templates.js';
|
|
4
|
-
import
|
|
4
|
+
import { affiliation, helpers } from '../templates.js';
|
|
5
|
+
import { TRIANGLE } from '../../asset/svgs.js';
|
|
5
6
|
|
|
6
7
|
const TYPE = 'affiliation';
|
|
7
8
|
const DEFAULT_CLASSNAME = 'miso-affiliation';
|
|
@@ -20,15 +21,24 @@ function ready(layout, state) {
|
|
|
20
21
|
const items = layout._getItems(state);
|
|
21
22
|
|
|
22
23
|
if (items && items.length > 0) {
|
|
23
|
-
return templates.
|
|
24
|
+
return templates.controlContainer(layout, state, 'previous') + templates.body(layout, state) + templates.controlContainer(layout, state, 'next');
|
|
24
25
|
} else {
|
|
25
26
|
return templates.empty(layout, state);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
function controlContainer(layout, state, direction) {
|
|
31
|
+
const { className, templates } = layout;
|
|
32
|
+
return `<div class="${className}__control-${direction}-container">${templates.control(layout, state, direction)}</div>`;
|
|
33
|
+
}
|
|
34
|
+
|
|
29
35
|
function control(layout, state, direction) {
|
|
30
|
-
const { className } = layout;
|
|
31
|
-
return `<div class="${className}__control-${direction}" data-role="${direction}"
|
|
36
|
+
const { className, templates } = layout;
|
|
37
|
+
return `<div class="${className}__control-${direction}" data-role="${direction}">${templates.controlIcon(layout, state, direction)}</div>`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function controlIcon(layout, state, direction) {
|
|
41
|
+
return TRIANGLE;
|
|
32
42
|
}
|
|
33
43
|
|
|
34
44
|
function body(layout, state) {
|
|
@@ -37,49 +47,58 @@ function body(layout, state) {
|
|
|
37
47
|
return `<div class="${className}__body">${templates.list(layout, state, items)}</div>`;
|
|
38
48
|
}
|
|
39
49
|
|
|
40
|
-
function header(layout, state) {
|
|
41
|
-
const { className, templates } = layout;
|
|
42
|
-
let { headerText } = templates;
|
|
43
|
-
if (typeof headerText === 'function') {
|
|
44
|
-
headerText = headerText(layout, state);
|
|
45
|
-
}
|
|
46
|
-
if (!headerText) {
|
|
47
|
-
return '';
|
|
48
|
-
}
|
|
49
|
-
return `<div class="${className}__header"><div class="${className}__header-tab">${headerText}</div></div>`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
50
|
function item(layout, state, value, index) {
|
|
53
51
|
const { className, templates, options } = layout;
|
|
54
52
|
const { itemType } = options;
|
|
55
53
|
const header = templates.itemHeader(layout, state, value, { index });
|
|
56
54
|
const body = templates[itemType](layout, state, value, { index });
|
|
57
|
-
return `<li class="${className}__item">${header}${body}</li>`;
|
|
55
|
+
return `<li class="${className}__item"><div class="${className}__item-inner">${header}${body}</div></li>`;
|
|
58
56
|
}
|
|
59
57
|
|
|
60
58
|
function itemHeader(layout, state, value, meta) {
|
|
61
59
|
const { className, templates } = layout;
|
|
62
|
-
let {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
60
|
+
let { itemHeaderLeftText, itemHeaderRightText } = templates;
|
|
61
|
+
itemHeaderLeftText = helpers.asFunction(itemHeaderLeftText)(layout, state, value, meta);
|
|
62
|
+
itemHeaderRightText = helpers.asFunction(itemHeaderRightText)(layout, state, value, meta);
|
|
63
|
+
return `<div class="${className}__item-header">
|
|
64
|
+
<div class="${className}__item-header-left">${itemHeaderLeftText}</div>
|
|
65
|
+
<div class="${className}__item-header-right">${itemHeaderRightText}</div>
|
|
66
|
+
</div>`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function itemHeaderRightText(layout, state, value, meta) {
|
|
70
|
+
const { className, templates } = layout;
|
|
71
|
+
const logoText = helpers.asFunction(templates.logoText)(layout, state, value, meta) || '';
|
|
72
|
+
const logoImg = helpers.asFunction(templates.logoImg)(layout, state, value, meta);
|
|
73
|
+
return logoImg ? `<span class="${className}__item-header-logo-phrase">${logoText}</span>${logoImg}` : '';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function logoImg(layout, state, value, meta) {
|
|
77
|
+
const { className, templates } = layout;
|
|
78
|
+
const logoUrl = helpers.asFunction(templates.logoUrl)(layout, state, value, meta);
|
|
79
|
+
if (!logoUrl) {
|
|
67
80
|
return '';
|
|
68
81
|
}
|
|
69
|
-
|
|
82
|
+
const logoAltText = helpers.asFunction(templates.logoAltText)(layout, state, value, meta);
|
|
83
|
+
const logoAltTextAttr = logoAltText ? ` alt="${logoAltText}"` : '';
|
|
84
|
+
return `<img class="${className}__item-header-logo" src="${logoUrl}"${logoAltTextAttr}>`;
|
|
70
85
|
}
|
|
71
86
|
|
|
72
87
|
const DEFAULT_TEMPLATES = Object.freeze({
|
|
73
88
|
root,
|
|
74
89
|
[STATUS.READY]: ready,
|
|
90
|
+
controlContainer,
|
|
75
91
|
control,
|
|
92
|
+
controlIcon,
|
|
76
93
|
body,
|
|
77
|
-
header,
|
|
78
94
|
item,
|
|
79
95
|
itemHeader,
|
|
80
96
|
affiliation,
|
|
81
|
-
ctaText: 'View',
|
|
82
|
-
|
|
97
|
+
ctaText: 'View deal',
|
|
98
|
+
itemHeaderLeftText: `Today's best deals`,
|
|
99
|
+
itemHeaderRightText,
|
|
100
|
+
logoImg,
|
|
101
|
+
logoText: 'Selected by',
|
|
83
102
|
});
|
|
84
103
|
|
|
85
104
|
const INHERITED_DEFAULT_TEMPLATES = Object.freeze({
|
|
@@ -87,6 +106,31 @@ const INHERITED_DEFAULT_TEMPLATES = Object.freeze({
|
|
|
87
106
|
...DEFAULT_TEMPLATES,
|
|
88
107
|
});
|
|
89
108
|
|
|
109
|
+
/*
|
|
110
|
+
function normalizeAutoplayOptions(options) {
|
|
111
|
+
if (options === true) {
|
|
112
|
+
options = { interval: 10000 };
|
|
113
|
+
} else if (typeof options === 'number') {
|
|
114
|
+
if (options <= 0) {
|
|
115
|
+
options = false;
|
|
116
|
+
} else {
|
|
117
|
+
options = { interval: Math.max(options, 500) };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return options;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function autoplayOptionsEquals(a, b) {
|
|
124
|
+
if (a === b) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
if (!a || !b) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return a.interval === b.interval;
|
|
131
|
+
}
|
|
132
|
+
*/
|
|
133
|
+
|
|
90
134
|
export default class AffiliationLayout extends CollectionLayout {
|
|
91
135
|
|
|
92
136
|
static get category() {
|
|
@@ -105,33 +149,51 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
105
149
|
return DEFAULT_CLASSNAME;
|
|
106
150
|
}
|
|
107
151
|
|
|
108
|
-
constructor({ className = DEFAULT_CLASSNAME, templates, ...options } = {}) {
|
|
152
|
+
constructor({ className = DEFAULT_CLASSNAME, templates, autoplay, ...options } = {}) {
|
|
109
153
|
super({ className, itemType: 'affiliation', templates: { ...DEFAULT_TEMPLATES, ...templates }, ...options });
|
|
110
154
|
this._itemCount = undefined;
|
|
111
155
|
this._itemIndex = 0;
|
|
156
|
+
this._displayedCount = undefined;
|
|
112
157
|
this._displayedIndex = undefined;
|
|
113
|
-
this._viewable =
|
|
158
|
+
this._viewable = undefined;
|
|
159
|
+
|
|
160
|
+
// TODO: extract this
|
|
161
|
+
/*
|
|
162
|
+
this._autoplay = {};
|
|
163
|
+
this.autoplay(autoplay);
|
|
164
|
+
*/
|
|
114
165
|
}
|
|
115
166
|
|
|
116
|
-
|
|
117
|
-
|
|
167
|
+
initialize(view) {
|
|
168
|
+
super.initialize(view);
|
|
169
|
+
const { viewable: options } = this._view.tracker.options || {};
|
|
170
|
+
this._viewable = new CarouselItemViewabilityObserver(this._onViewable.bind(this), options);
|
|
118
171
|
}
|
|
119
172
|
|
|
120
|
-
|
|
121
|
-
|
|
173
|
+
/*
|
|
174
|
+
autoplay(options = false) {
|
|
175
|
+
options = normalizeAutoplayOptions(options);
|
|
176
|
+
if (autoplayOptionsEquals(options, this._autoplay.options)) {
|
|
122
177
|
return;
|
|
123
178
|
}
|
|
124
|
-
|
|
179
|
+
if (this._autoplay.options) {
|
|
180
|
+
clearInterval(this._autoplay.intervalId);
|
|
181
|
+
}
|
|
182
|
+
this._autoplay = { options };
|
|
183
|
+
// TODO: use slower transition speed for autoplay
|
|
184
|
+
// TODO: pause on hover
|
|
185
|
+
// TODO: cooldown after manual interaction
|
|
186
|
+
if (options) {
|
|
187
|
+
this._autoplay.intervalId = setInterval(() => this.next(), options.interval);
|
|
188
|
+
}
|
|
125
189
|
}
|
|
190
|
+
*/
|
|
126
191
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
this.go(this._itemIndex - 1);
|
|
192
|
+
get itemIndex() {
|
|
193
|
+
return this._itemIndex;
|
|
132
194
|
}
|
|
133
195
|
|
|
134
|
-
|
|
196
|
+
set itemIndex(index) {
|
|
135
197
|
if (this._itemCount === undefined) {
|
|
136
198
|
return;
|
|
137
199
|
}
|
|
@@ -141,7 +203,21 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
141
203
|
index = 0;
|
|
142
204
|
}
|
|
143
205
|
this._itemIndex = index;
|
|
144
|
-
this._syncItemIndex();
|
|
206
|
+
raf(() => this._syncItemIndex());
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
next() {
|
|
210
|
+
if (this._itemCount === undefined) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
this.itemIndex++;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
previous() {
|
|
217
|
+
if (this._itemCount === undefined) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
this.itemIndex--;
|
|
145
221
|
}
|
|
146
222
|
|
|
147
223
|
_getItems(state) {
|
|
@@ -151,8 +227,15 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
151
227
|
|
|
152
228
|
_render(element, states, controls) {
|
|
153
229
|
super._render(element, states, controls);
|
|
154
|
-
|
|
155
|
-
|
|
230
|
+
|
|
231
|
+
// update item count, but raf to display as attribute
|
|
232
|
+
const items = this._getItems(states.state);
|
|
233
|
+
this._itemCount = items ? items.length : undefined;
|
|
234
|
+
|
|
235
|
+
raf(() => {
|
|
236
|
+
this._syncItemCount();
|
|
237
|
+
this._syncItemIndex();
|
|
238
|
+
});
|
|
156
239
|
}
|
|
157
240
|
|
|
158
241
|
_syncElement(element) {
|
|
@@ -169,14 +252,17 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
169
252
|
newElement && this._viewable.observe(newElement);
|
|
170
253
|
}
|
|
171
254
|
|
|
172
|
-
_syncItemCount(
|
|
255
|
+
_syncItemCount() {
|
|
256
|
+
const itemCount = this._itemCount;
|
|
257
|
+
if (itemCount === this._displayedCount) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
let { element } = this._view;
|
|
173
261
|
element = element.firstElementChild;
|
|
174
262
|
if (!element) {
|
|
175
263
|
return;
|
|
176
264
|
}
|
|
177
|
-
|
|
178
|
-
const items = this._getItems(state);
|
|
179
|
-
const itemCount = this._itemCount = items ? items.length : undefined;
|
|
265
|
+
this._displayedCount = itemCount;
|
|
180
266
|
if (itemCount === undefined) {
|
|
181
267
|
element.removeAttribute('data-item-count');
|
|
182
268
|
} else {
|
|
@@ -185,16 +271,15 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
185
271
|
}
|
|
186
272
|
|
|
187
273
|
_syncItemIndex() {
|
|
188
|
-
|
|
274
|
+
const index = this._itemIndex;
|
|
275
|
+
if (index === this._displayedIndex) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
189
278
|
const { element } = this._view;
|
|
190
279
|
const listElement = element && this._getListElement(element);
|
|
191
280
|
if (!listElement) {
|
|
192
281
|
return;
|
|
193
282
|
}
|
|
194
|
-
const index = this._itemIndex;
|
|
195
|
-
if (index === this._displayedIndex) {
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
283
|
this._displayedIndex = index;
|
|
199
284
|
listElement.style.left = `${-index * 100}%`;
|
|
200
285
|
this._viewable.display(index);
|
|
@@ -206,7 +291,7 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
206
291
|
if (!item) {
|
|
207
292
|
return;
|
|
208
293
|
}
|
|
209
|
-
this._view.
|
|
294
|
+
this._view.tracker.viewable([item]);
|
|
210
295
|
}
|
|
211
296
|
|
|
212
297
|
_getRenderedItemData(index) {
|
|
@@ -218,8 +303,9 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
218
303
|
return products[index];
|
|
219
304
|
}
|
|
220
305
|
|
|
306
|
+
_trackViewables() {} // omit default behavior for we want to track by other means
|
|
307
|
+
|
|
221
308
|
_onClick(event) {
|
|
222
|
-
super._onClick(event);
|
|
223
309
|
const element = event.target;
|
|
224
310
|
if (element.closest(`[data-role="previous"]`)) {
|
|
225
311
|
this.previous();
|
|
@@ -229,6 +315,7 @@ export default class AffiliationLayout extends CollectionLayout {
|
|
|
229
315
|
this.next();
|
|
230
316
|
return;
|
|
231
317
|
}
|
|
318
|
+
super._onClick(event);
|
|
232
319
|
}
|
|
233
320
|
|
|
234
321
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { escapeHtml } from '@miso.ai/commons';
|
|
2
2
|
import { LAYOUT_CATEGORY } from '../../constants.js';
|
|
3
3
|
import { fields } from '../../actor/index.js';
|
|
4
4
|
import TemplateBasedLayout from '../template.js';
|
|
5
|
-
|
|
6
|
-
const OPTION_VALUE = Symbol.for('miso.option-value');
|
|
5
|
+
import { makeTrackable } from '../trackable.js';
|
|
7
6
|
|
|
8
7
|
const TYPE = 'option-list';
|
|
9
8
|
const DEFAULT_CLASSNAME = 'miso-option-list';
|
|
@@ -64,11 +63,7 @@ export default class OptionListLayout extends TemplateBasedLayout {
|
|
|
64
63
|
templates: { ...DEFAULT_TEMPLATES, ...templates },
|
|
65
64
|
...options,
|
|
66
65
|
});
|
|
67
|
-
|
|
68
|
-
bindings: Object.freeze({
|
|
69
|
-
list: this._listBindings.bind(this),
|
|
70
|
-
}),
|
|
71
|
-
});
|
|
66
|
+
this._initTrackable();
|
|
72
67
|
}
|
|
73
68
|
|
|
74
69
|
initialize(view) {
|
|
@@ -89,59 +84,48 @@ export default class OptionListLayout extends TemplateBasedLayout {
|
|
|
89
84
|
|
|
90
85
|
_render(element, data, controls) {
|
|
91
86
|
super._render(element, data, controls);
|
|
92
|
-
|
|
87
|
+
const { state } = data;
|
|
88
|
+
this._syncBindings(element, state);
|
|
89
|
+
this._syncOptionValues(element, state);
|
|
93
90
|
}
|
|
94
91
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
92
|
+
_unrender() {
|
|
93
|
+
this._clearBindings();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_syncOptionValues(element, state) {
|
|
97
|
+
const optionsElement = element && this._getOptionsElement(element);
|
|
100
98
|
if (!optionsElement) {
|
|
101
99
|
return;
|
|
102
100
|
}
|
|
103
|
-
const optionValues = state
|
|
101
|
+
const optionValues = this._getItems(state) || [];
|
|
104
102
|
optionsElement.classList[optionValues.length === 0 ? 'add' : 'remove']('empty');
|
|
105
|
-
let i = 0;
|
|
106
|
-
for (const optionElement of this._listOptionElements(optionsElement)) {
|
|
107
|
-
optionElement[OPTION_VALUE] = optionValues[i++];
|
|
108
|
-
}
|
|
109
103
|
}
|
|
110
104
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return [];
|
|
114
|
-
}
|
|
115
|
-
const optionsElement = this._getOptionsElement(rootElement);
|
|
116
|
-
const bindings = [];
|
|
117
|
-
for (const element of this._listOptionElements(optionsElement)) {
|
|
118
|
-
const value = element[OPTION_VALUE];
|
|
119
|
-
if (!value) {
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
const key = value.text || value;
|
|
123
|
-
bindings.push({ key, value, element });
|
|
124
|
-
}
|
|
125
|
-
return bindings;
|
|
105
|
+
_getItems(state) {
|
|
106
|
+
return state.value;
|
|
126
107
|
}
|
|
127
108
|
|
|
128
109
|
_getOptionsElement(element) {
|
|
129
110
|
return element.querySelector(`[data-role="options"]`);
|
|
130
111
|
}
|
|
131
112
|
|
|
132
|
-
|
|
133
|
-
return
|
|
113
|
+
_getItemElements(element) {
|
|
114
|
+
return element ? Array.from(element.querySelectorAll(`[data-role="option"]`)) : [];
|
|
134
115
|
}
|
|
135
116
|
|
|
136
|
-
_handleClick(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
117
|
+
_handleClick(event) {
|
|
118
|
+
const element = event.target.closest(`[data-role="option"]`);
|
|
119
|
+
if (!element) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const binding = this._bindings.get(element);
|
|
123
|
+
const option = binding && binding.value;
|
|
124
|
+
if (!option) {
|
|
143
125
|
return;
|
|
144
126
|
}
|
|
127
|
+
this._trackClick(event, binding);
|
|
128
|
+
this._submit(option.text);
|
|
145
129
|
}
|
|
146
130
|
|
|
147
131
|
async _submit(value) {
|
|
@@ -152,4 +136,12 @@ export default class OptionListLayout extends TemplateBasedLayout {
|
|
|
152
136
|
this._view.hub.trigger(fields.query(), { q: value });
|
|
153
137
|
}
|
|
154
138
|
|
|
139
|
+
destroy() {
|
|
140
|
+
this._destroyTrackable();
|
|
141
|
+
this._view = undefined;
|
|
142
|
+
super.destroy();
|
|
143
|
+
}
|
|
144
|
+
|
|
155
145
|
}
|
|
146
|
+
|
|
147
|
+
makeTrackable(OptionListLayout.prototype);
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { defineValues } from '@miso.ai/commons';
|
|
2
1
|
import { STATUS, LAYOUT_CATEGORY } from '../../constants.js';
|
|
3
2
|
import TemplateBasedLayout from '../template.js';
|
|
3
|
+
import { makeTrackable } from '../trackable.js';
|
|
4
4
|
import { product, article, question, productInfoBlock, articleInfoBlock, titleBlock, brandBlock, descriptionBlock, dateBlock, priceBlock, discountRateText, ctaBlock, cta, imageBlock, indexBlock, helpers } from '../templates.js';
|
|
5
5
|
|
|
6
|
-
const VALUE = Symbol.for('miso.value');
|
|
7
|
-
|
|
8
6
|
function root(layout, state) {
|
|
9
7
|
const { className, role, templates } = layout;
|
|
10
8
|
const { status } = state;
|
|
@@ -88,18 +86,13 @@ export default class CollectionLayout extends TemplateBasedLayout {
|
|
|
88
86
|
itemType,
|
|
89
87
|
...options,
|
|
90
88
|
});
|
|
91
|
-
defineValues(this, {
|
|
92
|
-
bindings: Object.freeze({
|
|
93
|
-
list: this._listBindings.bind(this),
|
|
94
|
-
}),
|
|
95
|
-
});
|
|
96
89
|
this._view = undefined;
|
|
90
|
+
this._initTrackable();
|
|
97
91
|
}
|
|
98
92
|
|
|
99
93
|
initialize(view) {
|
|
100
94
|
this._view = view;
|
|
101
|
-
|
|
102
|
-
this._unsubscribes.push(proxyElement.on('click', this._onClick.bind(this)));
|
|
95
|
+
this._unsubscribes.push(view.proxyElement.on('click', this._onClick.bind(this)));
|
|
103
96
|
}
|
|
104
97
|
|
|
105
98
|
_preprocess({ state, rendered }) {
|
|
@@ -144,18 +137,11 @@ export default class CollectionLayout extends TemplateBasedLayout {
|
|
|
144
137
|
} else {
|
|
145
138
|
element.innerHTML = html;
|
|
146
139
|
}
|
|
147
|
-
this.
|
|
140
|
+
this._syncBindings(element, state);
|
|
148
141
|
}
|
|
149
142
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const values = this._getItems(state) || [];
|
|
155
|
-
let i = 0;
|
|
156
|
-
for (const itemElement of this._listItemElements(element)) {
|
|
157
|
-
itemElement[VALUE] = values[i++];
|
|
158
|
-
}
|
|
143
|
+
_unrender() {
|
|
144
|
+
this._clearBindings();
|
|
159
145
|
}
|
|
160
146
|
|
|
161
147
|
_getItems(state) {
|
|
@@ -166,39 +152,35 @@ export default class CollectionLayout extends TemplateBasedLayout {
|
|
|
166
152
|
return element.querySelector('[data-role="list"]');
|
|
167
153
|
}
|
|
168
154
|
|
|
169
|
-
|
|
155
|
+
_getItemElements(element) {
|
|
170
156
|
return element ? Array.from(element.querySelectorAll(`[data-role="item"]`)) : [];
|
|
171
157
|
}
|
|
172
158
|
|
|
173
|
-
_listBindings(rootElement) {
|
|
174
|
-
if (!rootElement) {
|
|
175
|
-
return [];
|
|
176
|
-
}
|
|
177
|
-
const bindings = [];
|
|
178
|
-
for (const element of this._listItemElements(rootElement)) {
|
|
179
|
-
const value = element[VALUE];
|
|
180
|
-
if (!value) {
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
const key = this.options.itemType === 'product' ? value.product_id : value;
|
|
184
|
-
bindings.push({ element, key, value });
|
|
185
|
-
}
|
|
186
|
-
return bindings;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
159
|
_onClick(event) {
|
|
190
160
|
const element = event.target.closest(`[data-role="item"]`);
|
|
191
161
|
if (!element) {
|
|
192
162
|
return;
|
|
193
163
|
}
|
|
194
|
-
const
|
|
164
|
+
const binding = this._bindings.get(element);
|
|
165
|
+
if (!binding) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this._trackClick(event, binding);
|
|
169
|
+
this._emitClickEvent(event, binding);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
_emitClickEvent(event, binding) {
|
|
195
173
|
const { session } = this._view._state;
|
|
174
|
+
const { value, element } = binding;
|
|
196
175
|
this._view._events.emit('click', { session, value, element, domEvent: event });
|
|
197
176
|
}
|
|
198
177
|
|
|
199
178
|
destroy() {
|
|
179
|
+
this._destroyTrackable();
|
|
200
180
|
this._view = undefined;
|
|
201
181
|
super.destroy();
|
|
202
182
|
}
|
|
203
183
|
|
|
204
184
|
}
|
|
185
|
+
|
|
186
|
+
makeTrackable(CollectionLayout.prototype);
|
package/src/layout/templates.js
CHANGED
|
@@ -44,12 +44,11 @@ export function article(layout, state, data, meta) {
|
|
|
44
44
|
|
|
45
45
|
export function affiliation(layout, state, data, meta) {
|
|
46
46
|
const { templates } = layout;
|
|
47
|
-
const [openTag, closeTag] = tagPair(layout, data);
|
|
47
|
+
const [openTag, closeTag] = tagPair(layout, data, { link: false});
|
|
48
48
|
return [
|
|
49
49
|
openTag,
|
|
50
50
|
(templates.infoBlock || templates.affiliationInfoBlock || affiliationInfoBlock)(layout, data, meta),
|
|
51
|
-
(templates.imageBlock || imageBlock)(layout, data, meta),
|
|
52
|
-
(templates.brandBlock || brandBlock)(layout, data, meta),
|
|
51
|
+
(templates.imageBlock || imageBlock)(layout, data, { brand: true, ...meta, }),
|
|
53
52
|
closeTag,
|
|
54
53
|
].join('');
|
|
55
54
|
}
|
|
@@ -123,7 +122,7 @@ export function priceBlock(layout, data) {
|
|
|
123
122
|
content += `<span class="${className}__item-price miso-price" data-currency="${currency}">${price}</span>`;
|
|
124
123
|
// discount rate
|
|
125
124
|
if (discount_rate_percent) {
|
|
126
|
-
const text = (templates.discountRateText || discountRateText)(layout, data);
|
|
125
|
+
const text = (templates.discountRateText || discountRateText)(layout, { ...data, discount_rate_percent });
|
|
127
126
|
content += ` <span class="${className}__item-discount-rate">${text}</span>`;
|
|
128
127
|
}
|
|
129
128
|
return `<div class="${className}__item-price-container">${content}</div>`;
|
|
@@ -133,12 +132,15 @@ export function discountRateText(layout, { discount_rate_percent }) {
|
|
|
133
132
|
return `(${discount_rate_percent}% off)`;
|
|
134
133
|
}
|
|
135
134
|
|
|
136
|
-
export function imageBlock(
|
|
135
|
+
export function imageBlock(layout, data, meta) {
|
|
136
|
+
const { className, templates } = layout;
|
|
137
|
+
const { cover_image } = data;
|
|
137
138
|
if (!cover_image) {
|
|
138
139
|
return '';
|
|
139
140
|
}
|
|
140
141
|
const img = `<img class="${className}__item-cover-image" src="${cover_image}">`;
|
|
141
|
-
|
|
142
|
+
const brand = meta.brand ? (templates.brandBlock || brandBlock)(layout, data, meta) : '';
|
|
143
|
+
return `<div class="${className}__item-cover-image-container">${img}${brand}</div>`;
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
export function indexBlock({ className }, data, { index }) {
|
|
@@ -147,28 +149,27 @@ export function indexBlock({ className }, data, { index }) {
|
|
|
147
149
|
|
|
148
150
|
export function ctaBlock(layout, data, meta) {
|
|
149
151
|
const { className, templates } = layout;
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
_cta = _cta(layout, data, meta);
|
|
153
|
-
}
|
|
154
|
-
return _cta ? `<div class="${className}__item-cta-container">${_cta}</div>` : '';
|
|
152
|
+
const cta = helpers.asFunction(templates.cta)(layout, data, meta);
|
|
153
|
+
return cta ? `<div class="${className}__item-cta-container">${cta}</div>` : '';
|
|
155
154
|
}
|
|
156
155
|
|
|
157
156
|
export function cta(layout, data, meta) {
|
|
158
|
-
const {
|
|
159
|
-
|
|
160
|
-
if (
|
|
161
|
-
|
|
157
|
+
const { templates } = layout;
|
|
158
|
+
const { url } = data;
|
|
159
|
+
if (!url) {
|
|
160
|
+
return '';
|
|
162
161
|
}
|
|
163
|
-
|
|
162
|
+
const [openTag, closeTag] = tagPair(layout, { url }, { classSuffix: 'item-cta', role: 'cta' });
|
|
163
|
+
const ctaText = helpers.asFunction(templates.ctaText)(layout, data, meta);
|
|
164
|
+
return ctaText ? `${openTag}${ctaText}${closeTag}` : '';
|
|
164
165
|
}
|
|
165
166
|
|
|
166
167
|
// helpers //
|
|
167
|
-
function tagPair({ className, options = {} }, { product_id, url }, { classSuffix = 'item-body', role = 'item' } = {}) {
|
|
168
|
+
function tagPair({ className, options = {} }, { product_id, url }, { classSuffix = 'item-body', role = 'item', ...meta } = {}) {
|
|
168
169
|
const { link = {} } = options;
|
|
169
170
|
const { target = '_blank', rel = 'noopener' } = link; // TODO: other properties
|
|
170
|
-
const tag = url ? 'a' : 'div';
|
|
171
|
-
const urlAttrs = url ? `href="${url}" target="${target}" rel="${rel}"` : '';
|
|
171
|
+
const tag = meta.link !== false && url ? 'a' : 'div';
|
|
172
|
+
const urlAttrs = meta.link !== false && url ? `href="${url}" target="${target}" rel="${rel}"` : '';
|
|
172
173
|
const productAttrs = product_id ? `${ATTR_DATA_MISO_PRODUCT_ID}="${product_id}"` : '';
|
|
173
174
|
const roleAttrs = role ? `data-role="${role}"` : '';
|
|
174
175
|
return [`<${tag} class="${className}__${classSuffix}" ${roleAttrs} ${productAttrs} ${urlAttrs}>`, `</${tag}>`];
|
|
@@ -189,8 +190,13 @@ function formatDate(date, fn = DEFAULT_DATE_OPTIONS) {
|
|
|
189
190
|
}
|
|
190
191
|
}
|
|
191
192
|
|
|
193
|
+
function asFunction(template = '') {
|
|
194
|
+
return typeof template === 'function' ? template : () => template;
|
|
195
|
+
}
|
|
196
|
+
|
|
192
197
|
export const helpers = Object.freeze({
|
|
193
198
|
tagPair,
|
|
194
199
|
formatDate,
|
|
195
200
|
escapeHtml,
|
|
201
|
+
asFunction,
|
|
196
202
|
});
|