@miso.ai/client-sdk-ui 1.10.0-beta.8 → 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.
Files changed (51) hide show
  1. package/dist/cjs/actor/index.js +0 -7
  2. package/dist/cjs/actor/tracker.js +47 -97
  3. package/dist/cjs/actor/utils.js +3 -1
  4. package/dist/cjs/actor/view.js +12 -1
  5. package/dist/cjs/actor/views.js +24 -2
  6. package/dist/cjs/affiliation/index.js +26 -10
  7. package/dist/cjs/asset/svgs.js +14 -4
  8. package/dist/cjs/constants.js +3 -2
  9. package/dist/cjs/layout/affiliation/affiliation.js +263 -13
  10. package/dist/cjs/layout/input/option-list.js +34 -49
  11. package/dist/cjs/layout/list/collection.js +24 -45
  12. package/dist/cjs/layout/templates.js +54 -32
  13. package/dist/cjs/layout/text/typewriter/index.js +12 -7
  14. package/dist/cjs/layout/trackable.js +124 -0
  15. package/dist/cjs/util/bindings.js +58 -110
  16. package/dist/cjs/util/states.js +31 -24
  17. package/dist/cjs/util/trackers.js +75 -0
  18. package/dist/cjs/util/viewables.js +55 -0
  19. package/dist/cjs/version.js +1 -1
  20. package/dist/cjs/workflow/ask.js +1 -2
  21. package/dist/cjs/workflow/base.js +2 -7
  22. package/dist/cjs/workflow/recommendation.js +2 -0
  23. package/package.json +2 -2
  24. package/src/actor/index.js +0 -1
  25. package/src/actor/tracker.js +37 -88
  26. package/src/actor/utils.js +3 -1
  27. package/src/actor/view.js +14 -1
  28. package/src/actor/views.js +30 -1
  29. package/src/affiliation/index.js +25 -8
  30. package/src/asset/svgs.js +13 -2
  31. package/src/constants.js +7 -1
  32. package/src/layout/affiliation/affiliation.js +247 -14
  33. package/src/layout/input/option-list.js +35 -43
  34. package/src/layout/list/collection.js +22 -40
  35. package/src/layout/templates.js +25 -19
  36. package/src/layout/text/typewriter/index.js +8 -6
  37. package/src/layout/trackable.js +106 -0
  38. package/src/util/bindings.js +47 -103
  39. package/src/util/states.js +26 -20
  40. package/src/util/trackers.js +69 -0
  41. package/src/util/viewables.js +47 -0
  42. package/src/version.js +1 -1
  43. package/src/workflow/ask.js +1 -2
  44. package/src/workflow/base.js +3 -5
  45. package/src/workflow/recommendation.js +1 -0
  46. package/dist/cjs/actor/trackers.js +0 -51
  47. package/dist/cjs/layout/text/typewriter/progress.js +0 -45
  48. package/dist/cjs/util/tracker.js +0 -348
  49. package/src/actor/trackers.js +0 -45
  50. package/src/layout/text/typewriter/progress.js +0 -33
  51. package/src/util/tracker.js +0 -318
@@ -1,6 +1,8 @@
1
+ import { CarouselItemViewabilityObserver, requestAnimationFrame as raf } from '@miso.ai/commons';
1
2
  import { STATUS, LAYOUT_CATEGORY } from '../../constants.js';
2
3
  import CollectionLayout from '../list/collection.js';
3
- import { affiliation } from '../templates.js';
4
+ import { affiliation, helpers } from '../templates.js';
5
+ import { TRIANGLE } from '../../asset/svgs.js';
4
6
 
5
7
  const TYPE = 'affiliation';
6
8
  const DEFAULT_CLASSNAME = 'miso-affiliation';
@@ -19,38 +21,84 @@ function ready(layout, state) {
19
21
  const items = layout._getItems(state);
20
22
 
21
23
  if (items && items.length > 0) {
22
- return templates.body(layout, state);
24
+ return templates.controlContainer(layout, state, 'previous') + templates.body(layout, state) + templates.controlContainer(layout, state, 'next');
23
25
  } else {
24
26
  return templates.empty(layout, state);
25
27
  }
26
28
  }
27
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
+
35
+ function control(layout, state, 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;
42
+ }
43
+
28
44
  function body(layout, state) {
29
45
  const { className, templates } = layout;
30
46
  const items = layout._getItems(state);
31
- return `<div class="${className}__body">${templates.header(layout, state)}${templates.list(layout, state, items)}</div>`;
47
+ return `<div class="${className}__body">${templates.list(layout, state, items)}</div>`;
48
+ }
49
+
50
+ function item(layout, state, value, index) {
51
+ const { className, templates, options } = layout;
52
+ const { itemType } = options;
53
+ const header = templates.itemHeader(layout, state, value, { index });
54
+ const body = templates[itemType](layout, state, value, { index });
55
+ return `<li class="${className}__item"><div class="${className}__item-inner">${header}${body}</div></li>`;
32
56
  }
33
57
 
34
- function header(layout, state) {
58
+ function itemHeader(layout, state, value, meta) {
35
59
  const { className, templates } = layout;
36
- let { headerText } = templates;
37
- if (typeof headerText === 'function') {
38
- headerText = headerText(layout, state);
39
- }
40
- if (!headerText) {
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) {
41
80
  return '';
42
81
  }
43
- return `<div class="${className}__header"><div class="${className}__header-tab">${headerText}</div></div>`;
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}>`;
44
85
  }
45
86
 
46
87
  const DEFAULT_TEMPLATES = Object.freeze({
47
88
  root,
48
89
  [STATUS.READY]: ready,
90
+ controlContainer,
91
+ control,
92
+ controlIcon,
49
93
  body,
50
- header,
94
+ item,
95
+ itemHeader,
51
96
  affiliation,
52
- ctaText: 'View',
53
- headerText: 'Featured',
97
+ ctaText: 'View deal',
98
+ itemHeaderLeftText: `Today's best deals`,
99
+ itemHeaderRightText,
100
+ logoImg,
101
+ logoText: 'Selected by',
54
102
  });
55
103
 
56
104
  const INHERITED_DEFAULT_TEMPLATES = Object.freeze({
@@ -58,6 +106,31 @@ const INHERITED_DEFAULT_TEMPLATES = Object.freeze({
58
106
  ...DEFAULT_TEMPLATES,
59
107
  });
60
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
+
61
134
  export default class AffiliationLayout extends CollectionLayout {
62
135
 
63
136
  static get category() {
@@ -76,8 +149,75 @@ export default class AffiliationLayout extends CollectionLayout {
76
149
  return DEFAULT_CLASSNAME;
77
150
  }
78
151
 
79
- constructor({ className = DEFAULT_CLASSNAME, templates, ...options } = {}) {
152
+ constructor({ className = DEFAULT_CLASSNAME, templates, autoplay, ...options } = {}) {
80
153
  super({ className, itemType: 'affiliation', templates: { ...DEFAULT_TEMPLATES, ...templates }, ...options });
154
+ this._itemCount = undefined;
155
+ this._itemIndex = 0;
156
+ this._displayedCount = undefined;
157
+ this._displayedIndex = undefined;
158
+ this._viewable = undefined;
159
+
160
+ // TODO: extract this
161
+ /*
162
+ this._autoplay = {};
163
+ this.autoplay(autoplay);
164
+ */
165
+ }
166
+
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);
171
+ }
172
+
173
+ /*
174
+ autoplay(options = false) {
175
+ options = normalizeAutoplayOptions(options);
176
+ if (autoplayOptionsEquals(options, this._autoplay.options)) {
177
+ return;
178
+ }
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
+ }
189
+ }
190
+ */
191
+
192
+ get itemIndex() {
193
+ return this._itemIndex;
194
+ }
195
+
196
+ set itemIndex(index) {
197
+ if (this._itemCount === undefined) {
198
+ return;
199
+ }
200
+ if (index < 0) {
201
+ index = this._itemCount - 1;
202
+ } else if (index >= this._itemCount) {
203
+ index = 0;
204
+ }
205
+ this._itemIndex = index;
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--;
81
221
  }
82
222
 
83
223
  _getItems(state) {
@@ -85,4 +225,97 @@ export default class AffiliationLayout extends CollectionLayout {
85
225
  return products;
86
226
  }
87
227
 
228
+ _render(element, states, controls) {
229
+ super._render(element, states, controls);
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
+ });
239
+ }
240
+
241
+ _syncElement(element) {
242
+ const oldElement = this._element;
243
+ super._syncElement(element);
244
+ this._syncViewableElement(oldElement, element);
245
+ }
246
+
247
+ _syncViewableElement(oldElement, newElement) {
248
+ if (oldElement === newElement) {
249
+ return;
250
+ }
251
+ oldElement && this._viewable.unobserve(oldElement);
252
+ newElement && this._viewable.observe(newElement);
253
+ }
254
+
255
+ _syncItemCount() {
256
+ const itemCount = this._itemCount;
257
+ if (itemCount === this._displayedCount) {
258
+ return;
259
+ }
260
+ let { element } = this._view;
261
+ element = element.firstElementChild;
262
+ if (!element) {
263
+ return;
264
+ }
265
+ this._displayedCount = itemCount;
266
+ if (itemCount === undefined) {
267
+ element.removeAttribute('data-item-count');
268
+ } else {
269
+ element.setAttribute('data-item-count', itemCount);
270
+ }
271
+ }
272
+
273
+ _syncItemIndex() {
274
+ const index = this._itemIndex;
275
+ if (index === this._displayedIndex) {
276
+ return;
277
+ }
278
+ const { element } = this._view;
279
+ const listElement = element && this._getListElement(element);
280
+ if (!listElement) {
281
+ return;
282
+ }
283
+ this._displayedIndex = index;
284
+ listElement.style.left = `${-index * 100}%`;
285
+ this._viewable.display(index);
286
+ this._view._events.emit('display', { index });
287
+ }
288
+
289
+ _onViewable(index) {
290
+ const item = this._getRenderedItemData(index);
291
+ if (!item) {
292
+ return;
293
+ }
294
+ this._view.tracker.viewable([item]);
295
+ }
296
+
297
+ _getRenderedItemData(index) {
298
+ const { value } = this._rendered.get(this._element) || {};
299
+ if (!value) {
300
+ return undefined;
301
+ }
302
+ const { products = [] } = value;
303
+ return products[index];
304
+ }
305
+
306
+ _trackViewables() {} // omit default behavior for we want to track by other means
307
+
308
+ _onClick(event) {
309
+ const element = event.target;
310
+ if (element.closest(`[data-role="previous"]`)) {
311
+ this.previous();
312
+ return;
313
+ }
314
+ if (element.closest(`[data-role="next"]`)) {
315
+ this.next();
316
+ return;
317
+ }
318
+ super._onClick(event);
319
+ }
320
+
88
321
  }
@@ -1,9 +1,8 @@
1
- import { defineValues, escapeHtml } from '@miso.ai/commons';
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
- defineValues(this, {
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
- this._syncOptionValues(element, data);
87
+ const { state } = data;
88
+ this._syncBindings(element, state);
89
+ this._syncOptionValues(element, state);
93
90
  }
94
91
 
95
- _syncOptionValues(element, { state }) {
96
- if (!element) {
97
- return;
98
- }
99
- const optionsElement = this._getOptionsElement(element);
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.value || [];
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
- _listBindings(rootElement) {
112
- if (!rootElement) {
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
- _listOptionElements(optionsElement) {
133
- return optionsElement ? Array.from(optionsElement.querySelectorAll(`[data-role="option"]`)) : [];
113
+ _getItemElements(element) {
114
+ return element ? Array.from(element.querySelectorAll(`[data-role="option"]`)) : [];
134
115
  }
135
116
 
136
- _handleClick(e) {
137
- for (let element = e.target; element && element !== this._view.element; element = element.parentElement) {
138
- const option = element[OPTION_VALUE];
139
- if (!option) {
140
- continue;
141
- }
142
- this._submit(option.text);
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;
@@ -28,7 +26,7 @@ function list(layout, state, values) {
28
26
  const { className, templates, options } = layout;
29
27
  const listTag = templates.ordered ? 'ol' : 'ul';
30
28
  // TODO: support separator?
31
- return `<${listTag} class="${className}__list" data-item-type="${options.itemType}">${templates.items(layout, state, values)}</${listTag}>`;
29
+ return `<${listTag} class="${className}__list" data-role="list" data-item-type="${options.itemType}">${templates.items(layout, state, values)}</${listTag}>`;
32
30
  }
33
31
 
34
32
  function items(layout, state, values, { offset = 0 } = {}) {
@@ -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
- const { proxyElement } = this._view = view;
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._syncValues(element, state);
140
+ this._syncBindings(element, state);
148
141
  }
149
142
 
150
- _syncValues(element, state) {
151
- if (!element) {
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) {
@@ -163,42 +149,38 @@ export default class CollectionLayout extends TemplateBasedLayout {
163
149
  }
164
150
 
165
151
  _getListElement(element) {
166
- return element.querySelector(`.${this.className}__list`);
152
+ return element.querySelector('[data-role="list"]');
167
153
  }
168
154
 
169
- _listItemElements(element) {
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 value = element[VALUE];
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);
@@ -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({ className }, { cover_image }) {
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
- return `<div class="${className}__item-cover-image-container">${img}</div>`;
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
- let _cta = templates.cta || cta;
151
- if (typeof _cta === 'function') {
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 { className, templates } = layout;
159
- let text = templates.ctaText;
160
- if (typeof text === 'function') {
161
- text = text(layout, data, meta);
157
+ const { templates } = layout;
158
+ const { url } = data;
159
+ if (!url) {
160
+ return '';
162
161
  }
163
- return text ? `<div class="${className}__item-cta">${text}</div>` : '';
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
  });