@ninetailed/experience.js-plugin-preview 7.7.3 → 7.7.4-beta.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/index.cjs.js CHANGED
@@ -73,6 +73,7 @@ class WidgetContainer {
73
73
  var _a, _b;
74
74
  this.options = options;
75
75
  this.container = document.createElement('div');
76
+ this.container.classList.add(WidgetContainer.CONTAINER_CLASS);
76
77
  this.container.style.position = 'fixed';
77
78
  this.container.style.zIndex = '999999';
78
79
  this.container.style.right = '0px';
@@ -109,7 +110,11 @@ class WidgetContainer {
109
110
  get element() {
110
111
  return this.container;
111
112
  }
113
+ static isContainerAttached() {
114
+ return document.querySelector(`.${WidgetContainer.CONTAINER_CLASS}`) !== null;
115
+ }
112
116
  }
117
+ WidgetContainer.CONTAINER_CLASS = 'nt-preview-widget-container';
113
118
 
114
119
  var _a;
115
120
  class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlugin {
@@ -125,15 +130,25 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
125
130
  this.profile = null;
126
131
  this.container = null;
127
132
  this.bridge = null;
133
+ /**
134
+ * Since several instances of the plugin can be created, we need to make sure only one is marked as active.
135
+ */
136
+ this.isActiveInstance = false;
128
137
  this.onChangeEmitter = new experience_js.OnChangeEmitter();
129
138
  this.clientId = null;
130
139
  this.environment = null;
131
140
  this.initialize = () => __awaiter(this, void 0, void 0, function* () {
132
141
  var _b;
133
142
  if (typeof window !== 'undefined') {
143
+ if (WidgetContainer.isContainerAttached()) {
144
+ experience_jsShared.logger.warn('Preview plugin is already attached.');
145
+ this.isActiveInstance = false;
146
+ return;
147
+ }
134
148
  const {
135
149
  PreviewBridge
136
150
  } = yield Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@ninetailed/experience.js-preview-bridge')); });
151
+ this.isActiveInstance = true;
137
152
  this.container = new WidgetContainer({
138
153
  ui: this.options.ui
139
154
  });
@@ -155,6 +170,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
155
170
  this[_a] = ({
156
171
  payload
157
172
  }) => {
173
+ if (!this.isActiveInstance) {
174
+ return;
175
+ }
158
176
  if (payload === null || payload === void 0 ? void 0 : payload.profile) {
159
177
  this.onProfileChange(payload.profile);
160
178
  }
@@ -163,6 +181,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
163
181
  baseline,
164
182
  experiences
165
183
  }) => {
184
+ if (!this.isActiveInstance) {
185
+ return;
186
+ }
166
187
  return () => {
167
188
  const experienceIds = Object.keys(this.pluginApi.experienceVariantIndexes);
168
189
  const experience = experiences.find(experience => {
@@ -238,12 +259,18 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
238
259
  }
239
260
  open() {
240
261
  var _b;
262
+ if (!this.isActiveInstance) {
263
+ return;
264
+ }
241
265
  (_b = this.container) === null || _b === void 0 ? void 0 : _b.open();
242
266
  this.isOpen = true;
243
267
  this.onChange();
244
268
  }
245
269
  close() {
246
270
  var _b;
271
+ if (!this.isActiveInstance) {
272
+ return;
273
+ }
247
274
  (_b = this.container) === null || _b === void 0 ? void 0 : _b.close();
248
275
  setTimeout(() => {
249
276
  this.isOpen = false;
@@ -251,6 +278,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
251
278
  }, 700);
252
279
  }
253
280
  toggle() {
281
+ if (!this.isActiveInstance) {
282
+ return;
283
+ }
254
284
  if (this.isOpen) {
255
285
  this.close();
256
286
  } else {
@@ -258,6 +288,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
258
288
  }
259
289
  }
260
290
  activateAudience(id) {
291
+ if (!this.isActiveInstance) {
292
+ return;
293
+ }
261
294
  if (!this.isKnownAudience(id)) {
262
295
  experience_jsShared.logger.warn(`You cannot activate an unknown audience (id: ${id}).`);
263
296
  return;
@@ -276,6 +309,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
276
309
  this.onChange();
277
310
  }
278
311
  deactivateAudience(id) {
312
+ if (!this.isActiveInstance) {
313
+ return;
314
+ }
279
315
  if (!this.isKnownAudience(id)) {
280
316
  experience_jsShared.logger.warn(`You cannot deactivate an unkown audience (id: ${id}). How did you get it in the first place?`);
281
317
  return;
@@ -309,6 +345,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
309
345
  this.onChange();
310
346
  }
311
347
  resetAudience(id) {
348
+ if (!this.isActiveInstance) {
349
+ return;
350
+ }
312
351
  if (!this.isKnownAudience(id)) {
313
352
  experience_jsShared.logger.warn(`You cannot reset an unknown audience (id: ${id}). How did you get it in the first place?`);
314
353
  return;
@@ -324,6 +363,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
324
363
  experienceId,
325
364
  variantIndex
326
365
  }) {
366
+ if (!this.isActiveInstance) {
367
+ return;
368
+ }
327
369
  const experience = this.experiences.find(experience => experience.id === experienceId);
328
370
  if (!experience) {
329
371
  experience_jsShared.logger.warn(`You cannot active a variant for an unknown experience (id: ${experienceId})`);
@@ -346,6 +388,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
346
388
  this.onChange();
347
389
  }
348
390
  resetExperience(experienceId) {
391
+ if (!this.isActiveInstance) {
392
+ return;
393
+ }
349
394
  const _b = this.experienceVariantIndexOverwrites,
350
395
  _c = experienceId;
351
396
  _b[_c];
@@ -354,6 +399,9 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
354
399
  this.onChange();
355
400
  }
356
401
  reset() {
402
+ if (!this.isActiveInstance) {
403
+ return;
404
+ }
357
405
  if (typeof window !== 'undefined' && window.ninetailed && typeof window.ninetailed.reset === 'function') {
358
406
  window.ninetailed.reset();
359
407
  }
@@ -370,7 +418,7 @@ class NinetailedPreviewPlugin extends experience_jsPluginAnalytics.NinetailedPlu
370
418
  get pluginApi() {
371
419
  var _b;
372
420
  return {
373
- version: "7.7.3" ,
421
+ version: "7.7.4-beta.0" ,
374
422
  open: this.open.bind(this),
375
423
  close: this.close.bind(this),
376
424
  toggle: this.toggle.bind(this),
package/index.esm.js CHANGED
@@ -42,6 +42,7 @@ class WidgetContainer {
42
42
  this.container = void 0;
43
43
  this.options = options;
44
44
  this.container = document.createElement('div');
45
+ this.container.classList.add(WidgetContainer.CONTAINER_CLASS);
45
46
  this.container.style.position = 'fixed';
46
47
  this.container.style.zIndex = '999999';
47
48
  this.container.style.right = '0px';
@@ -78,7 +79,11 @@ class WidgetContainer {
78
79
  get element() {
79
80
  return this.container;
80
81
  }
82
+ static isContainerAttached() {
83
+ return document.querySelector(`.${WidgetContainer.CONTAINER_CLASS}`) !== null;
84
+ }
81
85
  }
86
+ WidgetContainer.CONTAINER_CLASS = 'nt-preview-widget-container';
82
87
 
83
88
  class NinetailedPreviewPlugin extends NinetailedPlugin {
84
89
  constructor(options) {
@@ -94,6 +99,10 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
94
99
  this.profile = null;
95
100
  this.container = null;
96
101
  this.bridge = null;
102
+ /**
103
+ * Since several instances of the plugin can be created, we need to make sure only one is marked as active.
104
+ */
105
+ this.isActiveInstance = false;
97
106
  this.onChangeEmitter = new OnChangeEmitter();
98
107
  this.onOpenExperienceEditor = void 0;
99
108
  this.onOpenAudienceEditor = void 0;
@@ -102,9 +111,15 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
102
111
  this.initialize = async function () {
103
112
  if (typeof window !== 'undefined') {
104
113
  var _window$ninetailed;
114
+ if (WidgetContainer.isContainerAttached()) {
115
+ logger.warn('Preview plugin is already attached.');
116
+ _this.isActiveInstance = false;
117
+ return;
118
+ }
105
119
  const {
106
120
  PreviewBridge
107
121
  } = await import('@ninetailed/experience.js-preview-bridge');
122
+ _this.isActiveInstance = true;
108
123
  _this.container = new WidgetContainer({
109
124
  ui: _this.options.ui
110
125
  });
@@ -126,6 +141,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
126
141
  this[PROFILE_CHANGE] = ({
127
142
  payload
128
143
  }) => {
144
+ if (!this.isActiveInstance) {
145
+ return;
146
+ }
129
147
  if (payload != null && payload.profile) {
130
148
  this.onProfileChange(payload.profile);
131
149
  }
@@ -134,6 +152,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
134
152
  baseline,
135
153
  experiences
136
154
  }) => {
155
+ if (!this.isActiveInstance) {
156
+ return;
157
+ }
137
158
  return () => {
138
159
  const experienceIds = Object.keys(this.pluginApi.experienceVariantIndexes);
139
160
  const experience = experiences.find(experience => {
@@ -210,12 +231,18 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
210
231
  }
211
232
  open() {
212
233
  var _this$container;
234
+ if (!this.isActiveInstance) {
235
+ return;
236
+ }
213
237
  (_this$container = this.container) == null || _this$container.open();
214
238
  this.isOpen = true;
215
239
  this.onChange();
216
240
  }
217
241
  close() {
218
242
  var _this$container2;
243
+ if (!this.isActiveInstance) {
244
+ return;
245
+ }
219
246
  (_this$container2 = this.container) == null || _this$container2.close();
220
247
  setTimeout(() => {
221
248
  this.isOpen = false;
@@ -223,6 +250,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
223
250
  }, 700);
224
251
  }
225
252
  toggle() {
253
+ if (!this.isActiveInstance) {
254
+ return;
255
+ }
226
256
  if (this.isOpen) {
227
257
  this.close();
228
258
  } else {
@@ -230,6 +260,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
230
260
  }
231
261
  }
232
262
  activateAudience(id) {
263
+ if (!this.isActiveInstance) {
264
+ return;
265
+ }
233
266
  if (!this.isKnownAudience(id)) {
234
267
  logger.warn(`You cannot activate an unknown audience (id: ${id}).`);
235
268
  return;
@@ -248,6 +281,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
248
281
  this.onChange();
249
282
  }
250
283
  deactivateAudience(id) {
284
+ if (!this.isActiveInstance) {
285
+ return;
286
+ }
251
287
  if (!this.isKnownAudience(id)) {
252
288
  logger.warn(`You cannot deactivate an unkown audience (id: ${id}). How did you get it in the first place?`);
253
289
  return;
@@ -281,6 +317,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
281
317
  this.onChange();
282
318
  }
283
319
  resetAudience(id) {
320
+ if (!this.isActiveInstance) {
321
+ return;
322
+ }
284
323
  if (!this.isKnownAudience(id)) {
285
324
  logger.warn(`You cannot reset an unknown audience (id: ${id}). How did you get it in the first place?`);
286
325
  return;
@@ -294,6 +333,9 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
294
333
  experienceId,
295
334
  variantIndex
296
335
  }) {
336
+ if (!this.isActiveInstance) {
337
+ return;
338
+ }
297
339
  const experience = this.experiences.find(experience => experience.id === experienceId);
298
340
  if (!experience) {
299
341
  logger.warn(`You cannot active a variant for an unknown experience (id: ${experienceId})`);
@@ -316,12 +358,18 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
316
358
  this.onChange();
317
359
  }
318
360
  resetExperience(experienceId) {
361
+ if (!this.isActiveInstance) {
362
+ return;
363
+ }
319
364
  const _this$experienceVaria = this.experienceVariantIndexOverwrites,
320
365
  experienceVariantIndexOverwrites = _objectWithoutPropertiesLoose(_this$experienceVaria, [experienceId].map(_toPropertyKey));
321
366
  this.experienceVariantIndexOverwrites = experienceVariantIndexOverwrites;
322
367
  this.onChange();
323
368
  }
324
369
  reset() {
370
+ if (!this.isActiveInstance) {
371
+ return;
372
+ }
325
373
  if (typeof window !== 'undefined' && window.ninetailed && typeof window.ninetailed.reset === 'function') {
326
374
  window.ninetailed.reset();
327
375
  }
@@ -338,7 +386,7 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
338
386
  get pluginApi() {
339
387
  var _this$profile;
340
388
  return {
341
- version: "7.7.3" ,
389
+ version: "7.7.4-beta.0" ,
342
390
  open: this.open.bind(this),
343
391
  close: this.close.bind(this),
344
392
  toggle: this.toggle.bind(this),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-plugin-preview",
3
- "version": "7.7.3",
3
+ "version": "7.7.4-beta.0",
4
4
  "description": "Ninetailed SDK plugin for preview",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -15,10 +15,10 @@
15
15
  "a/b testing"
16
16
  ],
17
17
  "dependencies": {
18
- "@ninetailed/experience.js-shared": "7.7.3",
19
- "@ninetailed/experience.js": "7.7.3",
20
- "@ninetailed/experience.js-preview-bridge": "7.7.3",
21
- "@ninetailed/experience.js-plugin-analytics": "7.7.3"
18
+ "@ninetailed/experience.js-shared": "7.7.4-beta.0",
19
+ "@ninetailed/experience.js": "7.7.4-beta.0",
20
+ "@ninetailed/experience.js-preview-bridge": "7.7.4-beta.0",
21
+ "@ninetailed/experience.js-plugin-analytics": "7.7.4-beta.0"
22
22
  },
23
23
  "module": "./index.esm.js",
24
24
  "main": "./index.cjs.js"
@@ -29,6 +29,10 @@ export declare class NinetailedPreviewPlugin extends NinetailedPlugin implements
29
29
  private profile;
30
30
  private container;
31
31
  private bridge;
32
+ /**
33
+ * Since several instances of the plugin can be created, we need to make sure only one is marked as active.
34
+ */
35
+ private isActiveInstance;
32
36
  private onChangeEmitter;
33
37
  private readonly onOpenExperienceEditor;
34
38
  private readonly onOpenAudienceEditor;
@@ -7,10 +7,12 @@ type WidgetContainerOptions = {
7
7
  };
8
8
  export declare class WidgetContainer {
9
9
  private readonly options;
10
+ private static CONTAINER_CLASS;
10
11
  private container;
11
12
  constructor(options: WidgetContainerOptions);
12
13
  open(): void;
13
14
  close(): void;
14
15
  get element(): HTMLDivElement;
16
+ static isContainerAttached(): boolean;
15
17
  }
16
18
  export {};