@panneau/core 4.0.38 → 4.0.40-alpha.3

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/index.js CHANGED
@@ -1,662 +1,538 @@
1
- import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
- import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
3
- import _createClass from '@babel/runtime/helpers/createClass';
4
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
5
- import { getComponentFromName, flattenComponents } from '@panneau/core/utils';
6
- import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
1
+ import { flattenComponents, getComponentFromName } from '@panneau/core/utils';
7
2
  import isArray from 'lodash/isArray';
8
3
  import uniqBy from 'lodash/uniqBy';
9
- import _callSuper from '@babel/runtime/helpers/callSuper';
10
- import _inherits from '@babel/runtime/helpers/inherits';
11
4
  import EventEmitter from 'wolfy87-eventemitter';
12
- import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
13
5
  import isObject from 'lodash/isObject';
14
6
  import uniq from 'lodash/uniq';
15
7
  import { Tracking as Tracking$1 } from '@folklore/tracking';
16
8
 
17
- var ComponentsManager = /*#__PURE__*/function () {
18
- function ComponentsManager() {
19
- var components = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
20
- _classCallCheck(this, ComponentsManager);
21
- _defineProperty(this, "components", void 0);
9
+ class ComponentsManager {
10
+ constructor(components = {}) {
22
11
  this.components = flattenComponents(components);
23
12
  }
24
- return _createClass(ComponentsManager, [{
25
- key: "addComponent",
26
- value: function addComponent(name, component) {
27
- var namespace = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
28
- return this.addComponents(_defineProperty({}, name, component), namespace);
29
- }
30
- }, {
31
- key: "addComponents",
32
- value: function addComponents(components) {
33
- var namespace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
34
- var newComponents = namespace !== null ? Object.keys(components).reduce(function (componentsMaps, name) {
35
- return _objectSpread(_objectSpread({}, componentsMaps), {}, _defineProperty({}, "".concat(namespace, ".").concat(name), components[name]));
36
- }, {}) : components;
37
- this.components = _objectSpread(_objectSpread({}, this.components), newComponents);
38
- return this;
39
- }
40
- }, {
41
- key: "merge",
42
- value: function merge(manager) {
43
- var namespace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
44
- return this.addComponents(manager.getComponents(), namespace);
45
- }
46
- }, {
47
- key: "addNamespace",
48
- value: function addNamespace(namespace) {
49
- var _this = this;
50
- if (namespace === null) {
51
- return this;
52
- }
53
- this.components = Object.keys(this.components).reduce(function (componentsMap, name) {
54
- return _objectSpread(_objectSpread({}, componentsMap), {}, _defineProperty({}, "".concat(namespace, ".").concat(name), _this.components[name]));
55
- }, {});
13
+ addComponent(name, component, namespace = null) {
14
+ return this.addComponents({
15
+ [name]: component
16
+ }, namespace);
17
+ }
18
+ addComponents(components, namespace = null) {
19
+ const newComponents = namespace !== null ? Object.keys(components).reduce((componentsMaps, name) => ({
20
+ ...componentsMaps,
21
+ [`${namespace}.${name}`]: components[name]
22
+ }), {}) : components;
23
+ this.components = {
24
+ ...this.components,
25
+ ...newComponents
26
+ };
27
+ return this;
28
+ }
29
+ merge(manager, namespace = null) {
30
+ return this.addComponents(manager.getComponents(), namespace);
31
+ }
32
+ addNamespace(namespace) {
33
+ if (namespace === null) {
56
34
  return this;
57
35
  }
58
- }, {
59
- key: "getComponent",
60
- value: function getComponent(name) {
61
- var namespace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
62
- var components = this.getComponents(namespace);
63
- return getComponentFromName(name, components);
64
- }
65
- }, {
66
- key: "getComponents",
67
- value: function getComponents() {
68
- var _this2 = this;
69
- var namespace = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
70
- return namespace !== null ? Object.keys(this.components || {}).reduce(function (componentsMap, name) {
71
- var pattern = new RegExp("^".concat(namespace, "\\.(.*)$"));
72
- var matches = pattern.exec(name);
73
- return matches !== null ? _objectSpread(_objectSpread({}, componentsMap), {}, _defineProperty({}, matches[1], _this2.components[name])) : componentsMap;
74
- }, null) : this.components;
75
- }
76
- }, {
77
- key: "hasComponent",
78
- value: function hasComponent(name) {
79
- var namespace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
80
- return this.components !== null && typeof this.components[namespace !== null ? "".concat(namespace, ".").concat(name) : name] !== 'undefined';
81
- }
82
- }]);
83
- }();
36
+ this.components = Object.keys(this.components).reduce((componentsMap, name) => ({
37
+ ...componentsMap,
38
+ [`${namespace}.${name}`]: this.components[name]
39
+ }), {});
40
+ return this;
41
+ }
42
+ getComponent(name, namespace = null) {
43
+ const components = this.getComponents(namespace);
44
+ return getComponentFromName(name, components);
45
+ }
46
+ getComponents(namespace = null) {
47
+ return namespace !== null ? Object.keys(this.components || {}).reduce((componentsMap, name) => {
48
+ const pattern = new RegExp(`^${namespace}\\.(.*)$`);
49
+ const matches = pattern.exec(name);
50
+ return matches !== null ? {
51
+ ...(componentsMap || {}),
52
+ [matches[1]]: this.components[name]
53
+ } : componentsMap;
54
+ }, null) : this.components;
55
+ }
56
+ hasComponent(name, namespace = null) {
57
+ return this.components !== null && typeof this.components[namespace !== null ? `${namespace}.${name}` : name] !== 'undefined';
58
+ }
59
+ }
84
60
 
85
- var DefinitionsManager = /*#__PURE__*/function () {
86
- function DefinitionsManager() {
87
- var definitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
88
- _classCallCheck(this, DefinitionsManager);
89
- _defineProperty(this, "definitions", void 0);
61
+ class DefinitionsManager {
62
+ constructor(definitions = []) {
90
63
  this.definitions = definitions || [];
91
64
  }
92
- return _createClass(DefinitionsManager, [{
93
- key: "addDefinition",
94
- value: function addDefinition(definition) {
95
- this.addDefinitions(isArray(definition) ? definition : [definition]);
96
- return this;
97
- }
98
- }, {
99
- key: "addDefinitions",
100
- value: function addDefinitions(definitions) {
101
- this.definitions = uniqBy([].concat(_toConsumableArray(definitions), _toConsumableArray(this.definitions)), function (it) {
102
- return it.id;
103
- });
104
- return this;
105
- }
106
- }, {
107
- key: "merge",
108
- value: function merge(manager) {
109
- return this.addDefinitions(manager.getDefinitions());
110
- }
111
- }, {
112
- key: "getDefinition",
113
- value: function getDefinition(id) {
114
- if (id === null) {
115
- return null;
116
- }
117
- return this.definitions.find(function (it) {
118
- return it.id === id;
119
- }) || null;
120
- }
121
- }, {
122
- key: "getDefinitions",
123
- value: function getDefinitions() {
124
- return this.definitions;
125
- }
126
- }, {
127
- key: "hasDefinition",
128
- value: function hasDefinition(id) {
129
- return this.getDefinition(id) !== null;
130
- }
131
- }, {
132
- key: "getComponent",
133
- value: function getComponent(id) {
134
- var _ref = this.getDefinition(id) || {},
135
- _ref$component = _ref.component,
136
- component = _ref$component === void 0 ? null : _ref$component;
137
- return component;
138
- }
139
- }, {
140
- key: "getComponents",
141
- value: function getComponents() {
142
- return this.definitions.reduce(function (allComponents, _ref2) {
143
- var id = _ref2.id,
144
- _ref2$component = _ref2.component,
145
- component = _ref2$component === void 0 ? null : _ref2$component;
146
- return component !== null ? _objectSpread(_objectSpread({}, allComponents), {}, _defineProperty({}, id, component)) : allComponents;
147
- }, {});
65
+ addDefinition(definition) {
66
+ this.addDefinitions(isArray(definition) ? definition : [definition]);
67
+ return this;
68
+ }
69
+ addDefinitions(definitions) {
70
+ this.definitions = uniqBy([...definitions, ...this.definitions], it => it.id);
71
+ return this;
72
+ }
73
+ merge(manager) {
74
+ return this.addDefinitions(manager.getDefinitions());
75
+ }
76
+ getDefinition(id) {
77
+ if (id === null) {
78
+ return null;
148
79
  }
149
- }]);
150
- }();
80
+ return this.definitions.find(it => it.id === id) || null;
81
+ }
82
+ getDefinitions() {
83
+ return this.definitions;
84
+ }
85
+ hasDefinition(id) {
86
+ return this.getDefinition(id) !== null;
87
+ }
88
+ getComponent(id) {
89
+ const {
90
+ component = null
91
+ } = this.getDefinition(id) || {};
92
+ return component;
93
+ }
94
+ getComponents() {
95
+ return this.definitions.reduce((allComponents, {
96
+ id,
97
+ component = null
98
+ }) => component !== null ? {
99
+ ...allComponents,
100
+ [id]: component
101
+ } : allComponents, {});
102
+ }
103
+ }
151
104
 
152
- var EventsManager = /*#__PURE__*/function (_EventEmitter) {
153
- function EventsManager(element) {
154
- var _this;
155
- _classCallCheck(this, EventsManager);
156
- _this = _callSuper(this, EventsManager);
157
- _defineProperty(_this, "element", void 0);
158
- _defineProperty(_this, "events", void 0);
159
- _defineProperty(_this, "listeners", void 0);
160
- _this.element = element;
161
- _this.events = {};
162
- _this.listeners = {};
163
- return _this;
164
- }
165
- _inherits(EventsManager, _EventEmitter);
166
- return _createClass(EventsManager, [{
167
- key: "subscribe",
168
- value: function subscribe(event, callback) {
169
- this.on(event, callback);
170
- this.events = _objectSpread(_objectSpread({}, this.events), {}, _defineProperty({}, event, [].concat(_toConsumableArray(this.events[event] || []), [callback])));
171
- if (this.events[event].length === 1) {
172
- this.addEventListener(event);
173
- }
174
- }
175
- }, {
176
- key: "unsubscribe",
177
- value: function unsubscribe(event, callback) {
178
- var _this2 = this;
179
- this.off(event, callback);
180
- this.events = Object.keys(this.events).reduce(function (newEvents, eventName) {
181
- if (eventName !== event) {
182
- return _objectSpread(_objectSpread({}, newEvents), {}, _defineProperty({}, eventName, _this2.events[eventName]));
183
- }
184
- var newListeners = _this2.events[eventName].filter(function (listener) {
185
- return listener !== callback;
186
- });
187
- return newListeners.length > 0 ? _objectSpread(_objectSpread({}, newEvents), {}, _defineProperty({}, eventName, newListeners)) : newEvents;
188
- }, {});
189
- if (typeof this.events[event] === 'undefined') {
190
- this.removeEventListener(event);
191
- }
105
+ class EventsManager extends EventEmitter {
106
+ constructor(element) {
107
+ super();
108
+ this.element = element;
109
+ this.events = {};
110
+ this.listeners = {};
111
+ }
112
+ subscribe(event, callback) {
113
+ this.on(event, callback);
114
+ this.events = {
115
+ ...this.events,
116
+ [event]: [...(this.events[event] || []), callback]
117
+ };
118
+ if (this.events[event].length === 1) {
119
+ this.addEventListener(event);
192
120
  }
193
- }, {
194
- key: "addEventListener",
195
- value: function addEventListener(event) {
196
- var _this3 = this;
197
- if (typeof this.listeners[event] === 'undefined') {
198
- this.listeners[event] = function () {
199
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
200
- args[_key] = arguments[_key];
201
- }
202
- return _this3.emit.apply(_this3, [event].concat(args));
121
+ }
122
+ unsubscribe(event, callback) {
123
+ this.off(event, callback);
124
+ this.events = Object.keys(this.events).reduce((newEvents, eventName) => {
125
+ if (eventName !== event) {
126
+ return {
127
+ ...newEvents,
128
+ [eventName]: this.events[eventName]
203
129
  };
204
130
  }
205
- this.element.addEventListener(event, this.listeners[event]);
131
+ const newListeners = this.events[eventName].filter(listener => listener !== callback);
132
+ return newListeners.length > 0 ? {
133
+ ...newEvents,
134
+ [eventName]: newListeners
135
+ } : newEvents;
136
+ }, {});
137
+ if (typeof this.events[event] === 'undefined') {
138
+ this.removeEventListener(event);
206
139
  }
207
- }, {
208
- key: "removeEventListener",
209
- value: function removeEventListener(event) {
210
- this.element.removeEventListener(event, this.listeners[event]);
140
+ }
141
+ addEventListener(event) {
142
+ if (typeof this.listeners[event] === 'undefined') {
143
+ this.listeners[event] = (...args) => this.emit(event, ...args);
211
144
  }
212
- }]);
213
- }(EventEmitter);
145
+ this.element.addEventListener(event, this.listeners[event]);
146
+ }
147
+ removeEventListener(event) {
148
+ this.element.removeEventListener(event, this.listeners[event]);
149
+ }
150
+ }
214
151
 
215
- var FieldsManager = /*#__PURE__*/function (_DefinitionsManager) {
216
- function FieldsManager() {
217
- _classCallCheck(this, FieldsManager);
218
- return _callSuper(this, FieldsManager, arguments);
219
- }
220
- _inherits(FieldsManager, _DefinitionsManager);
221
- return _createClass(FieldsManager);
222
- }(DefinitionsManager);
152
+ class FieldsManager extends DefinitionsManager {}
223
153
 
224
- var _excluded$2 = ["fonts"];
225
- var FontsParser = /*#__PURE__*/function () {
226
- function FontsParser(_ref) {
227
- var fieldsManager = _ref.fieldsManager,
228
- screensManager = _ref.screensManager;
229
- _classCallCheck(this, FontsParser);
230
- _defineProperty(this, "fieldsManager", void 0);
231
- _defineProperty(this, "screensManager", void 0);
154
+ class FontsParser {
155
+ constructor({
156
+ fieldsManager,
157
+ screensManager
158
+ }) {
232
159
  this.fieldsManager = fieldsManager;
233
160
  this.screensManager = screensManager;
234
161
  }
235
162
 
236
163
  // Extract fonts
237
- return _createClass(FontsParser, [{
238
- key: "parse",
239
- value: function parse(story) {
240
- var _this = this;
241
- if (story === null) {
242
- return story;
243
- }
164
+ parse(story) {
165
+ if (story === null) {
166
+ return story;
167
+ }
244
168
 
245
- // Extract fonts from screen
246
- var _ref2 = story || {},
247
- _ref2$theme = _ref2.theme,
248
- theme = _ref2$theme === void 0 ? null : _ref2$theme,
249
- _ref2$components = _ref2.components,
250
- components = _ref2$components === void 0 ? [] : _ref2$components;
251
- var fonts = uniq(components.reduce(function (currentFonts, screen) {
252
- var type = screen.type;
253
- var _ref3 = _this.screensManager.getDefinition(type) || {},
254
- _ref3$fields = _ref3.fields,
255
- fields = _ref3$fields === void 0 ? [] : _ref3$fields;
256
- var fieldsPattern = _this.getFieldsPattern(fields);
257
- var newFonts = FontsParser.extractFontsWithPaths(screen, fieldsPattern);
258
- return newFonts.length > 0 ? [].concat(_toConsumableArray(currentFonts), _toConsumableArray(newFonts)) : currentFonts;
259
- }, []), 'name');
169
+ // Extract fonts from screen
170
+ const {
171
+ theme = null,
172
+ components = []
173
+ } = story || {};
174
+ const fonts = uniq(components.reduce((currentFonts, screen) => {
175
+ const {
176
+ type
177
+ } = screen;
178
+ const {
179
+ fields = []
180
+ } = this.screensManager.getDefinition(type) || {};
181
+ const fieldsPattern = this.getFieldsPattern(fields);
182
+ const newFonts = FontsParser.extractFontsWithPaths(screen, fieldsPattern);
183
+ return newFonts.length > 0 ? [...currentFonts, ...newFonts] : currentFonts;
184
+ }, []), 'name');
260
185
 
261
- // Extract fonts from theme
262
- if (theme !== null) {
263
- var _ref4 = this.parse(theme),
264
- _ref4$fonts = _ref4.fonts,
265
- themeFonts = _ref4$fonts === void 0 ? [] : _ref4$fonts,
266
- newTheme = _objectWithoutProperties(_ref4, _excluded$2);
267
- return fonts.length > 0 || themeFonts.length > 0 ? _objectSpread(_objectSpread({}, story), {}, {
268
- theme: newTheme,
269
- fonts: uniq([].concat(_toConsumableArray(themeFonts), _toConsumableArray(fonts)), 'name')
270
- }) : story;
271
- }
272
- return fonts.length > 0 ? _objectSpread(_objectSpread({}, story), {}, {
273
- fonts: fonts
274
- }) : story;
275
- }
276
- }, {
277
- key: "getFieldsPattern",
278
- value: function getFieldsPattern(fields) {
279
- var _this2 = this;
280
- var namePrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
281
- return fields.reduce(function (patterns, field) {
282
- var _field$name = field.name,
283
- name = _field$name === void 0 ? null : _field$name,
284
- _field$type = field.type,
285
- type = _field$type === void 0 ? null : _field$type;
286
- var path = [namePrefix, name].filter(function (it) {
287
- return it !== null;
288
- }).join('\\.');
289
- var fieldDefinition = _objectSpread(_objectSpread({}, type !== null ? _this2.fieldsManager.getDefinition(type) : null), field);
186
+ // Extract fonts from theme
187
+ if (theme !== null) {
188
+ const {
189
+ fonts: themeFonts = [],
190
+ ...newTheme
191
+ } = this.parse(theme);
192
+ return fonts.length > 0 || themeFonts.length > 0 ? {
193
+ ...story,
194
+ theme: newTheme,
195
+ fonts: uniq([...themeFonts, ...fonts], 'name')
196
+ } : story;
197
+ }
198
+ return fonts.length > 0 ? {
199
+ ...story,
200
+ fonts
201
+ } : story;
202
+ }
203
+ getFieldsPattern(fields, namePrefix = null) {
204
+ return fields.reduce((patterns, field) => {
205
+ const {
206
+ name = null,
207
+ type = null
208
+ } = field;
209
+ const path = [namePrefix, name].filter(it => it !== null).join('\\.');
210
+ const fieldDefinition = {
211
+ ...(type !== null ? this.fieldsManager.getDefinition(type) : null),
212
+ ...field
213
+ };
290
214
 
291
- // also check settings fields
292
- var _fieldDefinition$fiel = fieldDefinition.fields,
293
- subFields = _fieldDefinition$fiel === void 0 ? [] : _fieldDefinition$fiel,
294
- _fieldDefinition$item = fieldDefinition.itemsField,
295
- itemsField = _fieldDefinition$item === void 0 ? null : _fieldDefinition$item,
296
- _fieldDefinition$sett = fieldDefinition.settings,
297
- settings = _fieldDefinition$sett === void 0 ? [] : _fieldDefinition$sett;
298
- return [].concat(_toConsumableArray(patterns), _toConsumableArray(FontsParser.fieldIsFontFamily(fieldDefinition) ? [new RegExp("^".concat(path, "$"))] : []), _toConsumableArray(_this2.getFieldsPattern(subFields, path)), _toConsumableArray(_this2.getFieldsPattern(settings, path)), _toConsumableArray(itemsField !== null ? _this2.getFieldsPattern([itemsField], "".concat(path, "\\.[0-9]+")) : []));
299
- }, []);
300
- }
301
- }], [{
302
- key: "fieldIsFontFamily",
303
- value: function fieldIsFontFamily(_ref5) {
304
- var _ref5$id = _ref5.id,
305
- id = _ref5$id === void 0 ? null : _ref5$id;
306
- return id === 'font-family';
307
- }
308
- }, {
309
- key: "valueIsFont",
310
- value: function valueIsFont(_ref6) {
311
- var _ref6$type = _ref6.type,
312
- type = _ref6$type === void 0 ? null : _ref6$type;
313
- return type === 'custom' || type === 'google';
314
- }
315
- }, {
316
- key: "extractFontsWithPaths",
317
- value: function extractFontsWithPaths(data, patterns) {
318
- var keyPrefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
319
- var dataIsArray = isArray(data);
320
- var keys = dataIsArray ? _toConsumableArray(data.keys()) : Object.keys(data);
321
- return keys.reduce(function (currentFonts, key) {
322
- var path = [keyPrefix, key].filter(function (it) {
323
- return it !== null;
324
- }).join('.');
325
- var patternMatch = patterns.reduce(function (found, pattern) {
326
- return found || pattern.test(path);
327
- }, false);
328
- var value = data[key];
329
- var font = null;
330
- var subFonts = null;
331
- if (patternMatch && isObject(value) && FontsParser.valueIsFont(value)) {
332
- font = value;
333
- } else if (isObject(value) || isArray(value)) {
334
- subFonts = FontsParser.extractFontsWithPaths(value, patterns, path);
335
- }
336
- return subFonts !== null || font !== null ? [].concat(_toConsumableArray(currentFonts), _toConsumableArray(subFonts || []), _toConsumableArray(font !== null ? [font] : [])) : currentFonts;
337
- }, []);
338
- }
339
- }]);
340
- }();
215
+ // also check settings fields
216
+ const {
217
+ fields: subFields = [],
218
+ itemsField = null,
219
+ settings = []
220
+ } = fieldDefinition;
221
+ return [...patterns, ...(FontsParser.fieldIsFontFamily(fieldDefinition) ? [new RegExp(`^${path}$`)] : []), ...this.getFieldsPattern(subFields, path), ...this.getFieldsPattern(settings, path), ...(itemsField !== null ? this.getFieldsPattern([itemsField], `${path}\\.[0-9]+`) : [])];
222
+ }, []);
223
+ }
224
+ static fieldIsFontFamily({
225
+ id = null
226
+ }) {
227
+ return id === 'font-family';
228
+ }
229
+ static valueIsFont({
230
+ type = null
231
+ }) {
232
+ return type === 'custom' || type === 'google';
233
+ }
234
+ static extractFontsWithPaths(data, patterns, keyPrefix = null) {
235
+ const dataIsArray = isArray(data);
236
+ const keys = dataIsArray ? [...data.keys()] : Object.keys(data);
237
+ return keys.reduce((currentFonts, key) => {
238
+ const path = [keyPrefix, key].filter(it => it !== null).join('.');
239
+ const patternMatch = patterns.reduce((found, pattern) => found || pattern.test(path), false);
240
+ const value = data[key];
241
+ let font = null;
242
+ let subFonts = null;
243
+ if (patternMatch && isObject(value) && FontsParser.valueIsFont(value)) {
244
+ font = value;
245
+ } else if (isObject(value) || isArray(value)) {
246
+ subFonts = FontsParser.extractFontsWithPaths(value, patterns, path);
247
+ }
248
+ return subFonts !== null || font !== null ? [...currentFonts, ...(subFonts || []), ...(font !== null ? [font] : [])] : currentFonts;
249
+ }, []);
250
+ }
251
+ }
341
252
 
342
- var FormsManager = /*#__PURE__*/function (_ComponentsManager) {
343
- function FormsManager() {
344
- _classCallCheck(this, FormsManager);
345
- return _callSuper(this, FormsManager, arguments);
346
- }
347
- _inherits(FormsManager, _ComponentsManager);
348
- return _createClass(FormsManager);
349
- }(ComponentsManager);
253
+ class FormsManager extends ComponentsManager {}
350
254
 
351
- var _excluded$1 = ["medias"];
352
- var MediasParser = /*#__PURE__*/function () {
353
- function MediasParser(_ref) {
354
- var fieldsManager = _ref.fieldsManager,
355
- screensManager = _ref.screensManager;
356
- _classCallCheck(this, MediasParser);
357
- _defineProperty(this, "fieldsManager", void 0);
358
- _defineProperty(this, "screensManager", void 0);
255
+ class MediasParser {
256
+ constructor({
257
+ fieldsManager,
258
+ screensManager
259
+ }) {
359
260
  this.fieldsManager = fieldsManager;
360
261
  this.screensManager = screensManager;
361
262
  }
362
263
 
363
264
  // Convert medias object to path
364
- return _createClass(MediasParser, [{
365
- key: "toPath",
366
- value: function toPath(story) {
367
- var _this = this;
368
- if (story === null) {
369
- return story;
370
- }
371
- var _ref2 = story || {},
372
- _ref2$theme = _ref2.theme,
373
- theme = _ref2$theme === void 0 ? null : _ref2$theme,
374
- _ref2$components = _ref2.components,
375
- components = _ref2$components === void 0 ? [] : _ref2$components;
376
- var _components$reduce = components.reduce(function (_ref3, screen) {
377
- var previousComponents = _ref3.components,
378
- currentMedias = _ref3.medias;
379
- var type = screen.type;
380
- var _ref4 = _this.screensManager.getDefinition(type) || {},
381
- _ref4$fields = _ref4.fields,
382
- fields = _ref4$fields === void 0 ? [] : _ref4$fields;
383
- var fieldsPattern = _this.getMediaFieldsPattern(fields);
384
- var _MediasParser$replace = MediasParser.replaceMediasWithPaths(screen, fieldsPattern),
385
- newScreen = _MediasParser$replace.data,
386
- newMedias = _MediasParser$replace.medias;
387
- return {
388
- components: [].concat(_toConsumableArray(previousComponents), [newScreen]),
389
- medias: _objectSpread(_objectSpread({}, currentMedias), newMedias)
390
- };
391
- }, {
392
- components: [],
393
- medias: null
394
- }),
395
- newComponents = _components$reduce.components,
396
- medias = _components$reduce.medias;
397
- if (theme !== null) {
398
- var _ref5 = this.toPath(theme),
399
- themeMedias = _ref5.medias,
400
- newTheme = _objectWithoutProperties(_ref5, _excluded$1);
401
- return medias !== null || themeMedias !== null ? _objectSpread(_objectSpread({}, story), {}, {
402
- theme: newTheme,
403
- components: newComponents,
404
- medias: _objectSpread(_objectSpread({}, themeMedias), medias)
405
- }) : story;
406
- }
407
- return medias !== null ? _objectSpread(_objectSpread({}, story), {}, {
265
+ toPath(story) {
266
+ if (story === null) {
267
+ return story;
268
+ }
269
+ const {
270
+ theme = null,
271
+ components = []
272
+ } = story || {};
273
+ const {
274
+ components: newComponents,
275
+ medias
276
+ } = components.reduce(({
277
+ components: previousComponents,
278
+ medias: currentMedias
279
+ }, screen) => {
280
+ const {
281
+ type
282
+ } = screen;
283
+ const {
284
+ fields = []
285
+ } = this.screensManager.getDefinition(type) || {};
286
+ const fieldsPattern = this.getMediaFieldsPattern(fields);
287
+ const {
288
+ data: newScreen,
289
+ medias: newMedias
290
+ } = MediasParser.replaceMediasWithPaths(screen, fieldsPattern);
291
+ return {
292
+ components: [...previousComponents, newScreen],
293
+ medias: {
294
+ ...currentMedias,
295
+ ...newMedias
296
+ }
297
+ };
298
+ }, {
299
+ components: [],
300
+ medias: null
301
+ });
302
+ if (theme !== null) {
303
+ const {
304
+ medias: themeMedias,
305
+ ...newTheme
306
+ } = this.toPath(theme);
307
+ return medias !== null || themeMedias !== null ? {
308
+ ...story,
309
+ theme: newTheme,
408
310
  components: newComponents,
409
- medias: medias
410
- }) : story;
311
+ medias: {
312
+ ...themeMedias,
313
+ ...medias
314
+ }
315
+ } : story;
411
316
  }
317
+ return medias !== null ? {
318
+ ...story,
319
+ components: newComponents,
320
+ medias
321
+ } : story;
322
+ }
412
323
 
413
- // Convert path to medias object
414
- }, {
415
- key: "fromPath",
416
- value: function fromPath(story) {
417
- var _this2 = this;
418
- var defaultMedias = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
419
- if (story === null) {
420
- return story;
421
- }
422
- var _ref6 = story || {},
423
- _ref6$theme = _ref6.theme,
424
- theme = _ref6$theme === void 0 ? null : _ref6$theme,
425
- _ref6$components = _ref6.components,
426
- components = _ref6$components === void 0 ? [] : _ref6$components,
427
- _ref6$medias = _ref6.medias,
428
- medias = _ref6$medias === void 0 ? defaultMedias : _ref6$medias;
429
- if (medias === null && theme === null) {
430
- return story;
431
- }
324
+ // Convert path to medias object
325
+ fromPath(story, defaultMedias = null) {
326
+ if (story === null) {
327
+ return story;
328
+ }
329
+ const {
330
+ theme = null,
331
+ components = [],
332
+ medias = defaultMedias
333
+ } = story || {};
334
+ if (medias === null && theme === null) {
335
+ return story;
336
+ }
432
337
 
433
- // Replace path with medias objects
434
- var newComponents = medias !== null ? components.map(function (screen) {
435
- var type = screen.type;
436
- var _ref7 = _this2.screensManager.getDefinition(type) || {},
437
- _ref7$fields = _ref7.fields,
438
- fields = _ref7$fields === void 0 ? [] : _ref7$fields;
439
- var fieldsPattern = _this2.getMediaFieldsPattern(fields);
440
- return MediasParser.replacePathsWithMedias(screen, medias, fieldsPattern);
441
- }) : components;
338
+ // Replace path with medias objects
339
+ const newComponents = medias !== null ? components.map(screen => {
340
+ const {
341
+ type
342
+ } = screen;
343
+ const {
344
+ fields = []
345
+ } = this.screensManager.getDefinition(type) || {};
346
+ const fieldsPattern = this.getMediaFieldsPattern(fields);
347
+ return MediasParser.replacePathsWithMedias(screen, medias, fieldsPattern);
348
+ }) : components;
442
349
 
443
- // Replace path with medias object in theme
444
- if (theme !== null) {
445
- var newTheme = this.fromPath(theme, medias);
446
- return newTheme !== theme || newComponents !== components ? _objectSpread(_objectSpread({}, story), {}, {
447
- theme: newTheme,
448
- components: newComponents
449
- }) : story;
450
- }
451
- return newComponents !== components ? _objectSpread(_objectSpread({}, story), {}, {
350
+ // Replace path with medias object in theme
351
+ if (theme !== null) {
352
+ const newTheme = this.fromPath(theme, medias);
353
+ return newTheme !== theme || newComponents !== components ? {
354
+ ...story,
355
+ theme: newTheme,
452
356
  components: newComponents
453
- }) : story;
357
+ } : story;
454
358
  }
455
- }, {
456
- key: "getMediaFieldsPattern",
457
- value: function getMediaFieldsPattern(fields) {
458
- var _this3 = this;
459
- var namePrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
460
- return fields.reduce(function (patterns, field) {
461
- var _field$name = field.name,
462
- name = _field$name === void 0 ? null : _field$name,
463
- _field$type = field.type,
464
- type = _field$type === void 0 ? null : _field$type;
465
- var path = [namePrefix, name].filter(function (it) {
466
- return it !== null;
467
- }).join('\\.');
468
- var fieldDefinition = _objectSpread(_objectSpread({}, type !== null ? _this3.fieldsManager.getDefinition(type) : null), field);
359
+ return newComponents !== components ? {
360
+ ...story,
361
+ components: newComponents
362
+ } : story;
363
+ }
364
+ getMediaFieldsPattern(fields, namePrefix = null) {
365
+ return fields.reduce((patterns, field) => {
366
+ const {
367
+ name = null,
368
+ type = null
369
+ } = field;
370
+ const path = [namePrefix, name].filter(it => it !== null).join('\\.');
371
+ const fieldDefinition = {
372
+ ...(type !== null ? this.fieldsManager.getDefinition(type) : null),
373
+ ...field
374
+ };
469
375
 
470
- // also check settings fields
471
- var _fieldDefinition$fiel = fieldDefinition.fields,
472
- subFields = _fieldDefinition$fiel === void 0 ? [] : _fieldDefinition$fiel,
473
- _fieldDefinition$item = fieldDefinition.itemsField,
474
- itemsField = _fieldDefinition$item === void 0 ? null : _fieldDefinition$item,
475
- _fieldDefinition$sett = fieldDefinition.settings,
476
- settings = _fieldDefinition$sett === void 0 ? [] : _fieldDefinition$sett;
477
- return [].concat(_toConsumableArray(patterns), _toConsumableArray(MediasParser.fieldIsMedia(fieldDefinition) ? [new RegExp("^".concat(path, "$"))] : []), _toConsumableArray(MediasParser.fieldIsFontFamily(fieldDefinition) ? [new RegExp("^".concat(path, "\\.media$"))] : []), _toConsumableArray(_this3.getMediaFieldsPattern(subFields, path)), _toConsumableArray(_this3.getMediaFieldsPattern(settings, path)), _toConsumableArray(itemsField !== null ? _this3.getMediaFieldsPattern([itemsField], "".concat(path, "\\.[0-9]+")) : []));
478
- }, []);
479
- }
480
- }], [{
481
- key: "fieldIsMedia",
482
- value: function fieldIsMedia(_ref8) {
483
- var _ref8$media = _ref8.media,
484
- media = _ref8$media === void 0 ? false : _ref8$media;
485
- return media;
486
- }
487
- }, {
488
- key: "fieldIsFontFamily",
489
- value: function fieldIsFontFamily(_ref9) {
490
- var _ref9$id = _ref9.id,
491
- id = _ref9$id === void 0 ? null : _ref9$id;
492
- return id === 'font-family';
493
- }
494
- }, {
495
- key: "replacePathsWithMedias",
496
- value: function replacePathsWithMedias(data, medias, patterns) {
497
- var keyPrefix = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
498
- var dataIsArray = isArray(data);
499
- var keys = dataIsArray ? _toConsumableArray(data.keys()) : Object.keys(data);
500
- return keys.reduce(function (newData, key) {
501
- var path = [keyPrefix, key].filter(function (it) {
502
- return it !== null;
503
- }).join('.');
504
- var patternMatch = patterns.reduce(function (found, pattern) {
505
- return found || pattern.test(path);
506
- }, false);
507
- var value = data[key];
508
- var newValue;
509
- if (patternMatch) {
510
- newValue = isObject(value) ? value : medias[value] || null;
511
- } else {
512
- newValue = isObject(value) || isArray(value) ? MediasParser.replacePathsWithMedias(value, medias, patterns, path) : value;
513
- }
514
- return dataIsArray ? [].concat(_toConsumableArray(newData), [newValue]) : _objectSpread(_objectSpread({}, newData), {}, _defineProperty({}, key, newValue));
515
- }, dataIsArray ? [] : {});
516
- }
517
- }, {
518
- key: "getMediaPath",
519
- value: function getMediaPath(_ref0) {
520
- var _ref0$id = _ref0.id,
521
- id = _ref0$id === void 0 ? null : _ref0$id;
522
- return id !== null ? "media://".concat(id) : null;
523
- }
524
- }, {
525
- key: "replaceMediasWithPaths",
526
- value: function replaceMediasWithPaths(data, patterns) {
527
- var medias = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
528
- var keyPrefix = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
529
- var dataIsArray = isArray(data);
530
- var keys = dataIsArray ? _toConsumableArray(data.keys()) : Object.keys(data);
531
- return keys.reduce(function (_ref1, key) {
532
- var currentData = _ref1.data,
533
- currentMedias = _ref1.medias;
534
- var path = [keyPrefix, key].filter(function (it) {
535
- return it !== null;
536
- }).join('.');
537
- var patternMatch = patterns.reduce(function (found, pattern) {
538
- return found || pattern.test(path);
539
- }, false);
540
- var value = data[key];
541
- var newValue;
542
- var media = null;
543
- var subMedias = null;
544
- if (patternMatch && isObject(value)) {
545
- var mediaPath = MediasParser.getMediaPath(value);
546
- newValue = mediaPath !== null ? mediaPath : value;
547
- media = mediaPath !== null ? value : null;
548
- } else if (isObject(value) || isArray(value)) {
549
- var subReturn = MediasParser.replaceMediasWithPaths(value, patterns, medias, path);
550
- newValue = subReturn.data;
551
- subMedias = subReturn.medias;
552
- } else {
553
- newValue = value;
376
+ // also check settings fields
377
+ const {
378
+ fields: subFields = [],
379
+ itemsField = null,
380
+ settings = []
381
+ } = fieldDefinition;
382
+ return [...patterns, ...(MediasParser.fieldIsMedia(fieldDefinition) ? [new RegExp(`^${path}$`)] : []), ...(MediasParser.fieldIsFontFamily(fieldDefinition) ? [new RegExp(`^${path}\\.media$`)] : []), ...this.getMediaFieldsPattern(subFields, path), ...this.getMediaFieldsPattern(settings, path), ...(itemsField !== null ? this.getMediaFieldsPattern([itemsField], `${path}\\.[0-9]+`) : [])];
383
+ }, []);
384
+ }
385
+ static fieldIsMedia({
386
+ media = false
387
+ }) {
388
+ return media;
389
+ }
390
+ static fieldIsFontFamily({
391
+ id = null
392
+ }) {
393
+ return id === 'font-family';
394
+ }
395
+ static replacePathsWithMedias(data, medias, patterns, keyPrefix = null) {
396
+ const dataIsArray = isArray(data);
397
+ const keys = dataIsArray ? [...data.keys()] : Object.keys(data);
398
+ return keys.reduce((newData, key) => {
399
+ const path = [keyPrefix, key].filter(it => it !== null).join('.');
400
+ const patternMatch = patterns.reduce((found, pattern) => found || pattern.test(path), false);
401
+ const value = data[key];
402
+ let newValue;
403
+ if (patternMatch) {
404
+ newValue = isObject(value) ? value : medias[value] || null;
405
+ } else {
406
+ newValue = isObject(value) || isArray(value) ? MediasParser.replacePathsWithMedias(value, medias, patterns, path) : value;
407
+ }
408
+ return dataIsArray ? [...newData, newValue] : {
409
+ ...newData,
410
+ [key]: newValue
411
+ };
412
+ }, dataIsArray ? [] : {});
413
+ }
414
+ static getMediaPath({
415
+ id = null
416
+ }) {
417
+ return id !== null ? `media://${id}` : null;
418
+ }
419
+ static replaceMediasWithPaths(data, patterns, medias = null, keyPrefix = null) {
420
+ const dataIsArray = isArray(data);
421
+ const keys = dataIsArray ? [...data.keys()] : Object.keys(data);
422
+ return keys.reduce(({
423
+ data: currentData,
424
+ medias: currentMedias
425
+ }, key) => {
426
+ const path = [keyPrefix, key].filter(it => it !== null).join('.');
427
+ const patternMatch = patterns.reduce((found, pattern) => found || pattern.test(path), false);
428
+ const value = data[key];
429
+ let newValue;
430
+ let media = null;
431
+ let subMedias = null;
432
+ if (patternMatch && isObject(value)) {
433
+ const mediaPath = MediasParser.getMediaPath(value);
434
+ newValue = mediaPath !== null ? mediaPath : value;
435
+ media = mediaPath !== null ? value : null;
436
+ } else if (isObject(value) || isArray(value)) {
437
+ const subReturn = MediasParser.replaceMediasWithPaths(value, patterns, medias, path);
438
+ newValue = subReturn.data;
439
+ subMedias = subReturn.medias;
440
+ } else {
441
+ newValue = value;
442
+ }
443
+ return {
444
+ data: dataIsArray ? [...(currentData || []), newValue] : {
445
+ ...currentData,
446
+ [key]: newValue
447
+ },
448
+ medias: media !== null ? {
449
+ ...currentMedias,
450
+ ...subMedias,
451
+ [newValue]: media
452
+ } : {
453
+ ...currentMedias,
454
+ ...subMedias
554
455
  }
555
- return {
556
- data: dataIsArray ? [].concat(_toConsumableArray(currentData || []), [newValue]) : _objectSpread(_objectSpread({}, currentData), {}, _defineProperty({}, key, newValue)),
557
- medias: media !== null ? _objectSpread(_objectSpread(_objectSpread({}, currentMedias), subMedias), {}, _defineProperty({}, newValue, media)) : _objectSpread(_objectSpread({}, currentMedias), subMedias)
558
- };
559
- }, {
560
- data: null,
561
- medias: medias
562
- });
563
- }
564
- }]);
565
- }();
456
+ };
457
+ }, {
458
+ data: null,
459
+ medias
460
+ });
461
+ }
462
+ }
566
463
 
567
- var _excluded = ["value"],
568
- _excluded2 = ["value"];
569
- var Tracking = /*#__PURE__*/function (_BaseTracking) {
570
- function Tracking() {
571
- var _this;
572
- var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
573
- _classCallCheck(this, Tracking);
574
- _this = _callSuper(this, Tracking, [opts]);
575
- _defineProperty(_this, "variables", void 0);
576
- var _this$options$variabl = _this.options.variables,
577
- variables = _this$options$variabl === void 0 ? null : _this$options$variabl;
578
- _this.variables = null;
464
+ /* eslint-disable no-console */
465
+ class Tracking extends Tracking$1 {
466
+ constructor(opts = {}) {
467
+ super(opts);
468
+ const {
469
+ variables = null
470
+ } = this.options;
471
+ this.variables = null;
579
472
  if (variables !== null) {
580
- _this.setVariables(variables);
473
+ this.setVariables(variables);
581
474
  }
582
- return _this;
583
- }
584
- _inherits(Tracking, _BaseTracking);
585
- return _createClass(Tracking, [{
586
- key: "setVariables",
587
- value: function setVariables(variables) {
588
- this.variables = variables;
589
- if (variables !== null) {
590
- this.push(variables);
591
- }
592
- }
593
- }, {
594
- key: "getVariables",
595
- value: function getVariables() {
596
- return this.variables;
597
- }
598
- }, {
599
- key: "trackScreenView",
600
- value: function trackScreenView(screen, screenIndex) {
601
- var _ref = screen || {},
602
- _ref$id = _ref.id,
603
- screenId = _ref$id === void 0 ? null : _ref$id,
604
- _ref$type = _ref.type,
605
- screenType = _ref$type === void 0 ? null : _ref$type;
606
- var data = {
607
- event: 'screenView',
608
- screenId: screenId,
609
- screenType: screenType,
610
- screenIndex: screenIndex
611
- };
612
- this.push(data);
613
- }
614
- }, {
615
- key: "trackEvent",
616
- value: function trackEvent(category, action, label) {
617
- var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
618
- _ref2$value = _ref2.value,
619
- value = _ref2$value === void 0 ? null : _ref2$value,
620
- opts = _objectWithoutProperties(_ref2, _excluded);
621
- var data = _objectSpread(_objectSpread({}, opts), {}, {
622
- event: 'eventInteraction',
623
- eventCategory: category,
624
- eventAction: action,
625
- eventLabel: label,
626
- eventValue: value
627
- });
628
- this.push(data);
629
- }
630
- }, {
631
- key: "trackMedia",
632
- value: function trackMedia(type, media, action) {
633
- var _ref3 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
634
- _ref3$value = _ref3.value,
635
- value = _ref3$value === void 0 ? null : _ref3$value,
636
- opts = _objectWithoutProperties(_ref3, _excluded2);
637
- var _ref4 = media || {},
638
- _ref4$id = _ref4.id,
639
- mediaId = _ref4$id === void 0 ? null : _ref4$id,
640
- _ref4$name = _ref4.name,
641
- name = _ref4$name === void 0 ? null : _ref4$name,
642
- _ref4$duration = _ref4.duration,
643
- duration = _ref4$duration === void 0 ? null : _ref4$duration,
644
- _ref4$currentTime = _ref4.currentTime,
645
- currentTime = _ref4$currentTime === void 0 ? null : _ref4$currentTime;
646
- var label = name;
647
- var data = _objectSpread(_objectSpread({}, opts), {}, {
648
- event: 'eventInteraction',
649
- eventCategory: type,
650
- eventAction: action,
651
- eventLabel: label,
652
- eventValue: value,
653
- mediaId: mediaId,
654
- mediaCurrentTime: currentTime !== null ? Math.round(currentTime) : null,
655
- mediaProgress: currentTime !== null && duration !== null && duration > 0 ? Math.round(currentTime / duration * 100) : null
656
- });
657
- this.push(data);
475
+ }
476
+ setVariables(variables) {
477
+ this.variables = variables;
478
+ if (variables !== null) {
479
+ this.push(variables);
658
480
  }
659
- }]);
660
- }(Tracking$1);
481
+ }
482
+ getVariables() {
483
+ return this.variables;
484
+ }
485
+ trackScreenView(screen, screenIndex) {
486
+ const {
487
+ id: screenId = null,
488
+ type: screenType = null
489
+ } = screen || {};
490
+ const data = {
491
+ event: 'screenView',
492
+ screenId,
493
+ screenType,
494
+ screenIndex
495
+ };
496
+ this.push(data);
497
+ }
498
+ trackEvent(category, action, label, {
499
+ value = null,
500
+ ...opts
501
+ } = {}) {
502
+ const data = {
503
+ ...opts,
504
+ event: 'eventInteraction',
505
+ eventCategory: category,
506
+ eventAction: action,
507
+ eventLabel: label,
508
+ eventValue: value
509
+ };
510
+ this.push(data);
511
+ }
512
+ trackMedia(type, media, action, {
513
+ value = null,
514
+ ...opts
515
+ } = {}) {
516
+ const {
517
+ id: mediaId = null,
518
+ name = null,
519
+ duration = null,
520
+ currentTime = null
521
+ } = media || {};
522
+ const label = name;
523
+ const data = {
524
+ ...opts,
525
+ event: 'eventInteraction',
526
+ eventCategory: type,
527
+ eventAction: action,
528
+ eventLabel: label,
529
+ eventValue: value,
530
+ mediaId,
531
+ mediaCurrentTime: currentTime !== null ? Math.round(currentTime) : null,
532
+ mediaProgress: currentTime !== null && duration !== null && duration > 0 ? Math.round(currentTime / duration * 100) : null
533
+ };
534
+ this.push(data);
535
+ }
536
+ }
661
537
 
662
538
  export { ComponentsManager, DefinitionsManager, EventsManager, FieldsManager, FontsParser, FormsManager, MediasParser, Tracking };