@smoud/playable-sdk 1.0.15 → 1.0.17

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/README.md CHANGED
@@ -330,20 +330,20 @@ sdk.off('eventName', [callback], [context]);
330
330
 
331
331
  #### Available Events
332
332
 
333
- | Event | Parameters | Description | Typical Usage |
334
- | ------------- | --------------- | ------------------------------------------ | ------------------------ |
335
- | `init` | - | SDK initialization started | Setup loading screen |
336
- | `boot` | - | Ad container is ready `pre` init callback | Initialize core systems |
337
- | `ready` | - | Ad container is ready `post` init callback | Start resource loading |
338
- | `start` | - | Playable experience started | Begin gameplay/animation |
339
- | `interaction` | `count` | User interaction occurred | Track engagement |
340
- | `resize` | `width, height` | Container size changed | Update layout |
341
- | `pause` | - | Playable entered pause state | Pause gameplay |
342
- | `resume` | - | Playable resumed from pause | Resume gameplay |
343
- | `volume` | `level` | Volume level changed (0-1) | Adjust audio |
344
- | `retry` | - | Retry/restart triggered | Reset game state |
345
- | `finish` | - | Playable marked as complete | Show end screen |
346
- | `install` | - | Install action triggered | Track conversion |
333
+ | Event | Parameters | Description | Typical Usage |
334
+ | ------------- | --------------- | ------------------------------------------------- | ------------------------ |
335
+ | `init` | - | DOM Content loaded and SDK initialization started | Setup loading screen |
336
+ | `boot` | - | Ad container is ready `pre` init callback | Initialize core systems |
337
+ | `ready` | - | Ad container is ready `post` init callback | Start resource loading |
338
+ | `start` | - | Playable experience started | Begin gameplay/animation |
339
+ | `interaction` | `count` | User interaction occurred | Track engagement |
340
+ | `resize` | `width, height` | Container size changed | Update layout |
341
+ | `pause` | - | Playable entered pause state | Pause gameplay |
342
+ | `resume` | - | Playable resumed from pause | Resume gameplay |
343
+ | `volume` | `level` | Volume level changed (0-1) | Adjust audio |
344
+ | `retry` | - | Retry/restart triggered | Reset game state |
345
+ | `finish` | - | Playable marked as complete | Show end screen |
346
+ | `install` | - | Install action triggered | Track conversion |
347
347
 
348
348
  ### Properties
349
349
 
package/dist/esm/index.js CHANGED
@@ -160,156 +160,149 @@ function handleResume() {
160
160
  sdk.isPaused = false;
161
161
  emitEvent("resume");
162
162
  }
163
- function mraidIsViewable(viewable) {
163
+ function fireResizeEvent(width, height) {
164
+ handleResize(width, height);
165
+ }
166
+ function startMraidProtocol() {
164
167
  if ("mraid" !== AD_PROTOCOL) return;
165
- if (viewable) {
166
- if (sdk.isReady) {
168
+ if (isProtocolInitialized) return;
169
+ mraid.removeEventListener("ready", startMraidProtocol);
170
+ function mraidStateChanged(viewable) {
171
+ if (viewable) {
167
172
  handleResume();
168
- } else {
169
- if (AD_NETWORK == "ironsource") {
173
+ if (!sdk.isReady) {
170
174
  const maxSize = mraid.getMaxSize();
171
175
  sdk.maxWidth = Math.floor(maxSize.width);
172
176
  sdk.maxHeight = Math.floor(maxSize.height);
177
+ bootAd();
173
178
  }
174
- bootAd();
179
+ } else {
180
+ handlePause();
175
181
  }
176
- } else {
177
- handlePause();
178
182
  }
179
- }
180
- function dapiIsViewable(event) {
181
- if ("dapi" !== AD_PROTOCOL) return;
182
- if (event.isViewable) {
183
- if (sdk.isReady) {
184
- handleResume();
183
+ function isViewable() {
184
+ return mraid.isViewable() && "hidden" !== mraid.getState();
185
+ }
186
+ function stateChanged() {
187
+ mraidStateChanged(isViewable());
188
+ }
189
+ mraid.addEventListener("viewableChange", stateChanged);
190
+ mraid.addEventListener("stateChange", stateChanged);
191
+ if (isViewable()) {
192
+ mraidStateChanged(true);
193
+ }
194
+ if (mraid.getAudioVolume) {
195
+ const isAudioEnabled = mraid.getAudioVolume();
196
+ if (isAudioEnabled) {
197
+ handleVolumeChange(1);
185
198
  } else {
186
- const screenSize = dapi.getScreenSize();
187
- screenSize.width = Math.floor(screenSize.width);
188
- screenSize.height = Math.floor(screenSize.height);
189
- bootAd();
199
+ handleVolumeChange(0);
190
200
  }
191
- } else {
192
- handlePause();
193
201
  }
194
- }
195
- function fireResizeEvent(width, height) {
196
- handleResize(width, height);
197
- }
198
- function startMraidProtocol() {
199
- if ("mraid" !== AD_PROTOCOL) return;
200
- if (!isProtocolInitialized) {
201
- mraid.removeEventListener("ready", startMraidProtocol);
202
- mraid.addEventListener("viewableChange", function() {
203
- const isViewable = mraid.isViewable();
204
- mraidIsViewable(isViewable);
205
- });
206
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
207
- mraidIsViewable(true);
208
- }
209
- if (mraid.getAudioVolume) {
210
- const isAudioEnabled = mraid.getAudioVolume();
211
- if (isAudioEnabled) {
202
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
203
+ if (newVolume !== null) {
204
+ if (newVolume > 0) {
212
205
  handleVolumeChange(1);
213
206
  } else {
214
207
  handleVolumeChange(0);
215
208
  }
216
209
  }
217
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
218
- if (newVolume !== null) {
219
- if (newVolume > 0) {
220
- handleVolumeChange(1);
221
- } else {
222
- handleVolumeChange(0);
223
- }
224
- }
225
- });
226
- mraid.addEventListener("error", function(t, e) {
227
- console.log("mraid error: " + t + " action: " + e);
228
- });
229
- mraid.addEventListener("sizeChange", function() {
230
- const maxSize = mraid.getMaxSize();
231
- fireResizeEvent(maxSize.width, maxSize.height);
232
- });
233
- isProtocolInitialized = true;
234
- }
210
+ });
211
+ mraid.addEventListener("error", function(t, e) {
212
+ console.log("mraid error: " + t + " action: " + e);
213
+ });
214
+ mraid.addEventListener("sizeChange", function() {
215
+ const maxSize = mraid.getMaxSize();
216
+ fireResizeEvent(maxSize.width, maxSize.height);
217
+ });
218
+ isProtocolInitialized = true;
235
219
  }
236
220
  function startDapiProtocol() {
237
221
  if ("dapi" !== AD_PROTOCOL) return;
238
- if (!isProtocolInitialized) {
239
- dapi.removeEventListener("ready", startDapiProtocol);
240
- const isAudioEnabled = dapi.getAudioVolume();
241
- if (isAudioEnabled) {
222
+ if (isProtocolInitialized) return;
223
+ function dapiIsViewable(event) {
224
+ if (event.isViewable) {
225
+ handleResume();
226
+ if (!sdk.isReady) {
227
+ const screenSize = dapi.getScreenSize();
228
+ screenSize.width = Math.floor(screenSize.width);
229
+ screenSize.height = Math.floor(screenSize.height);
230
+ bootAd();
231
+ }
232
+ } else {
233
+ handlePause();
234
+ }
235
+ }
236
+ dapi.removeEventListener("ready", startDapiProtocol);
237
+ const isAudioEnabled = dapi.getAudioVolume();
238
+ if (isAudioEnabled) {
239
+ handleVolumeChange(1);
240
+ } else {
241
+ handleVolumeChange(0);
242
+ }
243
+ dapi.addEventListener("audioVolumeChange", function(volume) {
244
+ const isAudioEnabled2 = !!volume;
245
+ if (isAudioEnabled2) {
242
246
  handleVolumeChange(1);
243
247
  } else {
244
248
  handleVolumeChange(0);
245
249
  }
246
- dapi.addEventListener("audioVolumeChange", function(volume) {
247
- const isAudioEnabled2 = !!volume;
248
- if (isAudioEnabled2) {
249
- handleVolumeChange(1);
250
- } else {
251
- handleVolumeChange(0);
252
- }
253
- });
254
- dapi.addEventListener("adResized", function(event) {
255
- const maxSize = dapi.getScreenSize();
256
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
257
- });
258
- if (dapi.isViewable()) {
259
- dapiIsViewable({ isViewable: true });
260
- }
261
- dapi.addEventListener("viewableChange", dapiIsViewable);
262
- isProtocolInitialized = true;
250
+ });
251
+ dapi.addEventListener("adResized", function(event) {
252
+ const maxSize = dapi.getScreenSize();
253
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
254
+ });
255
+ if (dapi.isViewable()) {
256
+ dapiIsViewable({ isViewable: true });
263
257
  }
258
+ dapi.addEventListener("viewableChange", dapiIsViewable);
259
+ isProtocolInitialized = true;
264
260
  }
265
261
  function startDefaultProtocol() {
266
- if (!isProtocolInitialized) {
267
- let documentReady2 = function() {
268
- if (document.visibilityState === "visible") bootAd();
262
+ if (isProtocolInitialized) return;
263
+ if ("mintegral" === AD_NETWORK) {
264
+ window.mintGameStart = function() {
265
+ handleResume();
266
+ handleResize(sdk.maxWidth, sdk.maxHeight);
269
267
  };
270
- var documentReady = documentReady2;
271
- if ("mintegral" === AD_NETWORK) {
272
- window.mintGameStart = function() {
273
- handleResume();
274
- handleResize(sdk.maxWidth, sdk.maxHeight);
275
- };
276
- window.mintGameClose = function() {
277
- handlePause();
278
- };
279
- }
280
- document.addEventListener("visibilitychange", () => {
281
- if (document.visibilityState === "visible") {
282
- if (sdk.isReady) {
283
- handleResume();
284
- } else {
285
- bootAd();
286
- }
287
- } else {
288
- handlePause();
268
+ window.mintGameClose = function() {
269
+ handlePause();
270
+ };
271
+ }
272
+ document.addEventListener("visibilitychange", () => {
273
+ if (document.visibilityState === "visible") {
274
+ handleResume();
275
+ if (!sdk.isReady && document.readyState === "complete") {
276
+ bootAd();
289
277
  }
290
- });
291
- if (document.readyState === "complete") {
292
- documentReady2();
293
278
  } else {
294
- window.addEventListener("load", documentReady2);
279
+ handlePause();
295
280
  }
296
- window.addEventListener("resize", function() {
297
- handleResize();
298
- });
299
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
300
- const tapjoyApi = {
301
- skipAd: function() {
302
- try {
303
- sdk.finish();
304
- } catch (e) {
305
- console.warn("Could not skip ad! | " + e);
306
- }
281
+ });
282
+ function documentIsReady() {
283
+ if (document.visibilityState === "visible") bootAd();
284
+ }
285
+ if (document.readyState === "complete") {
286
+ documentIsReady();
287
+ } else {
288
+ window.addEventListener("load", documentIsReady);
289
+ }
290
+ window.addEventListener("resize", function() {
291
+ handleResize();
292
+ });
293
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
294
+ const tapjoyApi = {
295
+ skipAd: function() {
296
+ try {
297
+ sdk.finish();
298
+ } catch (e) {
299
+ console.warn("Could not skip ad! | " + e);
307
300
  }
308
- };
309
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
310
- }
311
- isProtocolInitialized = true;
301
+ }
302
+ };
303
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
312
304
  }
305
+ isProtocolInitialized = true;
313
306
  }
314
307
  function finishTapjoy() {
315
308
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -339,6 +332,18 @@ function registerTouchHandlers() {
339
332
  isTouchEventRegistered = true;
340
333
  }
341
334
  }
335
+ function initSDK() {
336
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
337
+ ensureProtocol();
338
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
339
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
340
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
341
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
342
+ } else {
343
+ startDefaultProtocol();
344
+ }
345
+ emitEvent("init");
346
+ }
342
347
  var _sdk = class _sdk {
343
348
  /**
344
349
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -360,23 +365,8 @@ var _sdk = class _sdk {
360
365
  */
361
366
  static init(callback) {
362
367
  if (isSDKInitialized) return;
363
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
364
- ensureProtocol();
365
368
  if (callback) initCallback = callback;
366
- if ("mraid" === AD_PROTOCOL && isMraid()) {
367
- if (mraid.getState() !== "ready") {
368
- mraid.addEventListener("ready", startMraidProtocol);
369
- } else {
370
- startMraidProtocol();
371
- }
372
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
373
- window.addEventListener("load", function() {
374
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
375
- });
376
- } else {
377
- startDefaultProtocol();
378
- }
379
- emitEvent("init");
369
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
380
370
  isSDKInitialized = true;
381
371
  }
382
372
  /**
@@ -601,7 +591,7 @@ var _sdk = class _sdk {
601
591
  }
602
592
  };
603
593
  /** Current version of the SDK */
604
- _sdk.version = "1.0.15";
594
+ _sdk.version = "1.0.17";
605
595
  /** Current maximum width of the playable ad container in pixels */
606
596
  _sdk.maxWidth = Math.floor(window.innerWidth);
607
597
  /** Current maximum height of the playable ad container in pixels */
@@ -162,156 +162,149 @@
162
162
  sdk.isPaused = false;
163
163
  emitEvent("resume");
164
164
  }
165
- function mraidIsViewable(viewable) {
165
+ function fireResizeEvent(width, height) {
166
+ handleResize(width, height);
167
+ }
168
+ function startMraidProtocol() {
166
169
  if ("mraid" !== AD_PROTOCOL) return;
167
- if (viewable) {
168
- if (sdk.isReady) {
170
+ if (isProtocolInitialized) return;
171
+ mraid.removeEventListener("ready", startMraidProtocol);
172
+ function mraidStateChanged(viewable) {
173
+ if (viewable) {
169
174
  handleResume();
170
- } else {
171
- if (AD_NETWORK == "ironsource") {
175
+ if (!sdk.isReady) {
172
176
  const maxSize = mraid.getMaxSize();
173
177
  sdk.maxWidth = Math.floor(maxSize.width);
174
178
  sdk.maxHeight = Math.floor(maxSize.height);
179
+ bootAd();
175
180
  }
176
- bootAd();
181
+ } else {
182
+ handlePause();
177
183
  }
178
- } else {
179
- handlePause();
180
184
  }
181
- }
182
- function dapiIsViewable(event) {
183
- if ("dapi" !== AD_PROTOCOL) return;
184
- if (event.isViewable) {
185
- if (sdk.isReady) {
186
- handleResume();
185
+ function isViewable() {
186
+ return mraid.isViewable() && "hidden" !== mraid.getState();
187
+ }
188
+ function stateChanged() {
189
+ mraidStateChanged(isViewable());
190
+ }
191
+ mraid.addEventListener("viewableChange", stateChanged);
192
+ mraid.addEventListener("stateChange", stateChanged);
193
+ if (isViewable()) {
194
+ mraidStateChanged(true);
195
+ }
196
+ if (mraid.getAudioVolume) {
197
+ const isAudioEnabled = mraid.getAudioVolume();
198
+ if (isAudioEnabled) {
199
+ handleVolumeChange(1);
187
200
  } else {
188
- const screenSize = dapi.getScreenSize();
189
- screenSize.width = Math.floor(screenSize.width);
190
- screenSize.height = Math.floor(screenSize.height);
191
- bootAd();
201
+ handleVolumeChange(0);
192
202
  }
193
- } else {
194
- handlePause();
195
203
  }
196
- }
197
- function fireResizeEvent(width, height) {
198
- handleResize(width, height);
199
- }
200
- function startMraidProtocol() {
201
- if ("mraid" !== AD_PROTOCOL) return;
202
- if (!isProtocolInitialized) {
203
- mraid.removeEventListener("ready", startMraidProtocol);
204
- mraid.addEventListener("viewableChange", function() {
205
- const isViewable = mraid.isViewable();
206
- mraidIsViewable(isViewable);
207
- });
208
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
209
- mraidIsViewable(true);
210
- }
211
- if (mraid.getAudioVolume) {
212
- const isAudioEnabled = mraid.getAudioVolume();
213
- if (isAudioEnabled) {
204
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
205
+ if (newVolume !== null) {
206
+ if (newVolume > 0) {
214
207
  handleVolumeChange(1);
215
208
  } else {
216
209
  handleVolumeChange(0);
217
210
  }
218
211
  }
219
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
220
- if (newVolume !== null) {
221
- if (newVolume > 0) {
222
- handleVolumeChange(1);
223
- } else {
224
- handleVolumeChange(0);
225
- }
226
- }
227
- });
228
- mraid.addEventListener("error", function(t, e) {
229
- console.log("mraid error: " + t + " action: " + e);
230
- });
231
- mraid.addEventListener("sizeChange", function() {
232
- const maxSize = mraid.getMaxSize();
233
- fireResizeEvent(maxSize.width, maxSize.height);
234
- });
235
- isProtocolInitialized = true;
236
- }
212
+ });
213
+ mraid.addEventListener("error", function(t, e) {
214
+ console.log("mraid error: " + t + " action: " + e);
215
+ });
216
+ mraid.addEventListener("sizeChange", function() {
217
+ const maxSize = mraid.getMaxSize();
218
+ fireResizeEvent(maxSize.width, maxSize.height);
219
+ });
220
+ isProtocolInitialized = true;
237
221
  }
238
222
  function startDapiProtocol() {
239
223
  if ("dapi" !== AD_PROTOCOL) return;
240
- if (!isProtocolInitialized) {
241
- dapi.removeEventListener("ready", startDapiProtocol);
242
- const isAudioEnabled = dapi.getAudioVolume();
243
- if (isAudioEnabled) {
224
+ if (isProtocolInitialized) return;
225
+ function dapiIsViewable(event) {
226
+ if (event.isViewable) {
227
+ handleResume();
228
+ if (!sdk.isReady) {
229
+ const screenSize = dapi.getScreenSize();
230
+ screenSize.width = Math.floor(screenSize.width);
231
+ screenSize.height = Math.floor(screenSize.height);
232
+ bootAd();
233
+ }
234
+ } else {
235
+ handlePause();
236
+ }
237
+ }
238
+ dapi.removeEventListener("ready", startDapiProtocol);
239
+ const isAudioEnabled = dapi.getAudioVolume();
240
+ if (isAudioEnabled) {
241
+ handleVolumeChange(1);
242
+ } else {
243
+ handleVolumeChange(0);
244
+ }
245
+ dapi.addEventListener("audioVolumeChange", function(volume) {
246
+ const isAudioEnabled2 = !!volume;
247
+ if (isAudioEnabled2) {
244
248
  handleVolumeChange(1);
245
249
  } else {
246
250
  handleVolumeChange(0);
247
251
  }
248
- dapi.addEventListener("audioVolumeChange", function(volume) {
249
- const isAudioEnabled2 = !!volume;
250
- if (isAudioEnabled2) {
251
- handleVolumeChange(1);
252
- } else {
253
- handleVolumeChange(0);
254
- }
255
- });
256
- dapi.addEventListener("adResized", function(event) {
257
- const maxSize = dapi.getScreenSize();
258
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
259
- });
260
- if (dapi.isViewable()) {
261
- dapiIsViewable({ isViewable: true });
262
- }
263
- dapi.addEventListener("viewableChange", dapiIsViewable);
264
- isProtocolInitialized = true;
252
+ });
253
+ dapi.addEventListener("adResized", function(event) {
254
+ const maxSize = dapi.getScreenSize();
255
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
256
+ });
257
+ if (dapi.isViewable()) {
258
+ dapiIsViewable({ isViewable: true });
265
259
  }
260
+ dapi.addEventListener("viewableChange", dapiIsViewable);
261
+ isProtocolInitialized = true;
266
262
  }
267
263
  function startDefaultProtocol() {
268
- if (!isProtocolInitialized) {
269
- let documentReady2 = function() {
270
- if (document.visibilityState === "visible") bootAd();
264
+ if (isProtocolInitialized) return;
265
+ if ("mintegral" === AD_NETWORK) {
266
+ window.mintGameStart = function() {
267
+ handleResume();
268
+ handleResize(sdk.maxWidth, sdk.maxHeight);
271
269
  };
272
- var documentReady = documentReady2;
273
- if ("mintegral" === AD_NETWORK) {
274
- window.mintGameStart = function() {
275
- handleResume();
276
- handleResize(sdk.maxWidth, sdk.maxHeight);
277
- };
278
- window.mintGameClose = function() {
279
- handlePause();
280
- };
281
- }
282
- document.addEventListener("visibilitychange", () => {
283
- if (document.visibilityState === "visible") {
284
- if (sdk.isReady) {
285
- handleResume();
286
- } else {
287
- bootAd();
288
- }
289
- } else {
290
- handlePause();
270
+ window.mintGameClose = function() {
271
+ handlePause();
272
+ };
273
+ }
274
+ document.addEventListener("visibilitychange", () => {
275
+ if (document.visibilityState === "visible") {
276
+ handleResume();
277
+ if (!sdk.isReady && document.readyState === "complete") {
278
+ bootAd();
291
279
  }
292
- });
293
- if (document.readyState === "complete") {
294
- documentReady2();
295
280
  } else {
296
- window.addEventListener("load", documentReady2);
281
+ handlePause();
297
282
  }
298
- window.addEventListener("resize", function() {
299
- handleResize();
300
- });
301
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
302
- const tapjoyApi = {
303
- skipAd: function() {
304
- try {
305
- sdk.finish();
306
- } catch (e) {
307
- console.warn("Could not skip ad! | " + e);
308
- }
283
+ });
284
+ function documentIsReady() {
285
+ if (document.visibilityState === "visible") bootAd();
286
+ }
287
+ if (document.readyState === "complete") {
288
+ documentIsReady();
289
+ } else {
290
+ window.addEventListener("load", documentIsReady);
291
+ }
292
+ window.addEventListener("resize", function() {
293
+ handleResize();
294
+ });
295
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
296
+ const tapjoyApi = {
297
+ skipAd: function() {
298
+ try {
299
+ sdk.finish();
300
+ } catch (e) {
301
+ console.warn("Could not skip ad! | " + e);
309
302
  }
310
- };
311
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
312
- }
313
- isProtocolInitialized = true;
303
+ }
304
+ };
305
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
314
306
  }
307
+ isProtocolInitialized = true;
315
308
  }
316
309
  function finishTapjoy() {
317
310
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -341,6 +334,18 @@
341
334
  isTouchEventRegistered = true;
342
335
  }
343
336
  }
337
+ function initSDK() {
338
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
339
+ ensureProtocol();
340
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
341
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
342
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
343
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
344
+ } else {
345
+ startDefaultProtocol();
346
+ }
347
+ emitEvent("init");
348
+ }
344
349
  var _sdk = class _sdk {
345
350
  /**
346
351
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -362,23 +367,8 @@
362
367
  */
363
368
  static init(callback) {
364
369
  if (isSDKInitialized) return;
365
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
366
- ensureProtocol();
367
370
  if (callback) initCallback = callback;
368
- if ("mraid" === AD_PROTOCOL && isMraid()) {
369
- if (mraid.getState() !== "ready") {
370
- mraid.addEventListener("ready", startMraidProtocol);
371
- } else {
372
- startMraidProtocol();
373
- }
374
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
375
- window.addEventListener("load", function() {
376
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
377
- });
378
- } else {
379
- startDefaultProtocol();
380
- }
381
- emitEvent("init");
371
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
382
372
  isSDKInitialized = true;
383
373
  }
384
374
  /**
@@ -603,7 +593,7 @@
603
593
  }
604
594
  };
605
595
  /** Current version of the SDK */
606
- _sdk.version = "1.0.15";
596
+ _sdk.version = "1.0.17";
607
597
  /** Current maximum width of the playable ad container in pixels */
608
598
  _sdk.maxWidth = Math.floor(window.innerWidth);
609
599
  /** Current maximum height of the playable ad container in pixels */
package/dist/index.js CHANGED
@@ -187,156 +187,149 @@ function handleResume() {
187
187
  sdk.isPaused = false;
188
188
  emitEvent("resume");
189
189
  }
190
- function mraidIsViewable(viewable) {
190
+ function fireResizeEvent(width, height) {
191
+ handleResize(width, height);
192
+ }
193
+ function startMraidProtocol() {
191
194
  if ("mraid" !== AD_PROTOCOL) return;
192
- if (viewable) {
193
- if (sdk.isReady) {
195
+ if (isProtocolInitialized) return;
196
+ mraid.removeEventListener("ready", startMraidProtocol);
197
+ function mraidStateChanged(viewable) {
198
+ if (viewable) {
194
199
  handleResume();
195
- } else {
196
- if (AD_NETWORK == "ironsource") {
200
+ if (!sdk.isReady) {
197
201
  const maxSize = mraid.getMaxSize();
198
202
  sdk.maxWidth = Math.floor(maxSize.width);
199
203
  sdk.maxHeight = Math.floor(maxSize.height);
204
+ bootAd();
200
205
  }
201
- bootAd();
206
+ } else {
207
+ handlePause();
202
208
  }
203
- } else {
204
- handlePause();
205
209
  }
206
- }
207
- function dapiIsViewable(event) {
208
- if ("dapi" !== AD_PROTOCOL) return;
209
- if (event.isViewable) {
210
- if (sdk.isReady) {
211
- handleResume();
210
+ function isViewable() {
211
+ return mraid.isViewable() && "hidden" !== mraid.getState();
212
+ }
213
+ function stateChanged() {
214
+ mraidStateChanged(isViewable());
215
+ }
216
+ mraid.addEventListener("viewableChange", stateChanged);
217
+ mraid.addEventListener("stateChange", stateChanged);
218
+ if (isViewable()) {
219
+ mraidStateChanged(true);
220
+ }
221
+ if (mraid.getAudioVolume) {
222
+ const isAudioEnabled = mraid.getAudioVolume();
223
+ if (isAudioEnabled) {
224
+ handleVolumeChange(1);
212
225
  } else {
213
- const screenSize = dapi.getScreenSize();
214
- screenSize.width = Math.floor(screenSize.width);
215
- screenSize.height = Math.floor(screenSize.height);
216
- bootAd();
226
+ handleVolumeChange(0);
217
227
  }
218
- } else {
219
- handlePause();
220
228
  }
221
- }
222
- function fireResizeEvent(width, height) {
223
- handleResize(width, height);
224
- }
225
- function startMraidProtocol() {
226
- if ("mraid" !== AD_PROTOCOL) return;
227
- if (!isProtocolInitialized) {
228
- mraid.removeEventListener("ready", startMraidProtocol);
229
- mraid.addEventListener("viewableChange", function() {
230
- const isViewable = mraid.isViewable();
231
- mraidIsViewable(isViewable);
232
- });
233
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
234
- mraidIsViewable(true);
235
- }
236
- if (mraid.getAudioVolume) {
237
- const isAudioEnabled = mraid.getAudioVolume();
238
- if (isAudioEnabled) {
229
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
230
+ if (newVolume !== null) {
231
+ if (newVolume > 0) {
239
232
  handleVolumeChange(1);
240
233
  } else {
241
234
  handleVolumeChange(0);
242
235
  }
243
236
  }
244
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
245
- if (newVolume !== null) {
246
- if (newVolume > 0) {
247
- handleVolumeChange(1);
248
- } else {
249
- handleVolumeChange(0);
250
- }
251
- }
252
- });
253
- mraid.addEventListener("error", function(t, e) {
254
- console.log("mraid error: " + t + " action: " + e);
255
- });
256
- mraid.addEventListener("sizeChange", function() {
257
- const maxSize = mraid.getMaxSize();
258
- fireResizeEvent(maxSize.width, maxSize.height);
259
- });
260
- isProtocolInitialized = true;
261
- }
237
+ });
238
+ mraid.addEventListener("error", function(t, e) {
239
+ console.log("mraid error: " + t + " action: " + e);
240
+ });
241
+ mraid.addEventListener("sizeChange", function() {
242
+ const maxSize = mraid.getMaxSize();
243
+ fireResizeEvent(maxSize.width, maxSize.height);
244
+ });
245
+ isProtocolInitialized = true;
262
246
  }
263
247
  function startDapiProtocol() {
264
248
  if ("dapi" !== AD_PROTOCOL) return;
265
- if (!isProtocolInitialized) {
266
- dapi.removeEventListener("ready", startDapiProtocol);
267
- const isAudioEnabled = dapi.getAudioVolume();
268
- if (isAudioEnabled) {
249
+ if (isProtocolInitialized) return;
250
+ function dapiIsViewable(event) {
251
+ if (event.isViewable) {
252
+ handleResume();
253
+ if (!sdk.isReady) {
254
+ const screenSize = dapi.getScreenSize();
255
+ screenSize.width = Math.floor(screenSize.width);
256
+ screenSize.height = Math.floor(screenSize.height);
257
+ bootAd();
258
+ }
259
+ } else {
260
+ handlePause();
261
+ }
262
+ }
263
+ dapi.removeEventListener("ready", startDapiProtocol);
264
+ const isAudioEnabled = dapi.getAudioVolume();
265
+ if (isAudioEnabled) {
266
+ handleVolumeChange(1);
267
+ } else {
268
+ handleVolumeChange(0);
269
+ }
270
+ dapi.addEventListener("audioVolumeChange", function(volume) {
271
+ const isAudioEnabled2 = !!volume;
272
+ if (isAudioEnabled2) {
269
273
  handleVolumeChange(1);
270
274
  } else {
271
275
  handleVolumeChange(0);
272
276
  }
273
- dapi.addEventListener("audioVolumeChange", function(volume) {
274
- const isAudioEnabled2 = !!volume;
275
- if (isAudioEnabled2) {
276
- handleVolumeChange(1);
277
- } else {
278
- handleVolumeChange(0);
279
- }
280
- });
281
- dapi.addEventListener("adResized", function(event) {
282
- const maxSize = dapi.getScreenSize();
283
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
284
- });
285
- if (dapi.isViewable()) {
286
- dapiIsViewable({ isViewable: true });
287
- }
288
- dapi.addEventListener("viewableChange", dapiIsViewable);
289
- isProtocolInitialized = true;
277
+ });
278
+ dapi.addEventListener("adResized", function(event) {
279
+ const maxSize = dapi.getScreenSize();
280
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
281
+ });
282
+ if (dapi.isViewable()) {
283
+ dapiIsViewable({ isViewable: true });
290
284
  }
285
+ dapi.addEventListener("viewableChange", dapiIsViewable);
286
+ isProtocolInitialized = true;
291
287
  }
292
288
  function startDefaultProtocol() {
293
- if (!isProtocolInitialized) {
294
- let documentReady2 = function() {
295
- if (document.visibilityState === "visible") bootAd();
289
+ if (isProtocolInitialized) return;
290
+ if ("mintegral" === AD_NETWORK) {
291
+ window.mintGameStart = function() {
292
+ handleResume();
293
+ handleResize(sdk.maxWidth, sdk.maxHeight);
296
294
  };
297
- var documentReady = documentReady2;
298
- if ("mintegral" === AD_NETWORK) {
299
- window.mintGameStart = function() {
300
- handleResume();
301
- handleResize(sdk.maxWidth, sdk.maxHeight);
302
- };
303
- window.mintGameClose = function() {
304
- handlePause();
305
- };
306
- }
307
- document.addEventListener("visibilitychange", () => {
308
- if (document.visibilityState === "visible") {
309
- if (sdk.isReady) {
310
- handleResume();
311
- } else {
312
- bootAd();
313
- }
314
- } else {
315
- handlePause();
295
+ window.mintGameClose = function() {
296
+ handlePause();
297
+ };
298
+ }
299
+ document.addEventListener("visibilitychange", () => {
300
+ if (document.visibilityState === "visible") {
301
+ handleResume();
302
+ if (!sdk.isReady && document.readyState === "complete") {
303
+ bootAd();
316
304
  }
317
- });
318
- if (document.readyState === "complete") {
319
- documentReady2();
320
305
  } else {
321
- window.addEventListener("load", documentReady2);
306
+ handlePause();
322
307
  }
323
- window.addEventListener("resize", function() {
324
- handleResize();
325
- });
326
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
327
- const tapjoyApi = {
328
- skipAd: function() {
329
- try {
330
- sdk.finish();
331
- } catch (e) {
332
- console.warn("Could not skip ad! | " + e);
333
- }
308
+ });
309
+ function documentIsReady() {
310
+ if (document.visibilityState === "visible") bootAd();
311
+ }
312
+ if (document.readyState === "complete") {
313
+ documentIsReady();
314
+ } else {
315
+ window.addEventListener("load", documentIsReady);
316
+ }
317
+ window.addEventListener("resize", function() {
318
+ handleResize();
319
+ });
320
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
321
+ const tapjoyApi = {
322
+ skipAd: function() {
323
+ try {
324
+ sdk.finish();
325
+ } catch (e) {
326
+ console.warn("Could not skip ad! | " + e);
334
327
  }
335
- };
336
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
337
- }
338
- isProtocolInitialized = true;
328
+ }
329
+ };
330
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
339
331
  }
332
+ isProtocolInitialized = true;
340
333
  }
341
334
  function finishTapjoy() {
342
335
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -366,6 +359,18 @@ function registerTouchHandlers() {
366
359
  isTouchEventRegistered = true;
367
360
  }
368
361
  }
362
+ function initSDK() {
363
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
364
+ ensureProtocol();
365
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
366
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
367
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
368
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
369
+ } else {
370
+ startDefaultProtocol();
371
+ }
372
+ emitEvent("init");
373
+ }
369
374
  var _sdk = class _sdk {
370
375
  /**
371
376
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -387,23 +392,8 @@ var _sdk = class _sdk {
387
392
  */
388
393
  static init(callback) {
389
394
  if (isSDKInitialized) return;
390
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
391
- ensureProtocol();
392
395
  if (callback) initCallback = callback;
393
- if ("mraid" === AD_PROTOCOL && isMraid()) {
394
- if (mraid.getState() !== "ready") {
395
- mraid.addEventListener("ready", startMraidProtocol);
396
- } else {
397
- startMraidProtocol();
398
- }
399
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
400
- window.addEventListener("load", function() {
401
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
402
- });
403
- } else {
404
- startDefaultProtocol();
405
- }
406
- emitEvent("init");
396
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
407
397
  isSDKInitialized = true;
408
398
  }
409
399
  /**
@@ -628,7 +618,7 @@ var _sdk = class _sdk {
628
618
  }
629
619
  };
630
620
  /** Current version of the SDK */
631
- _sdk.version = "1.0.15";
621
+ _sdk.version = "1.0.17";
632
622
  /** Current maximum width of the playable ad container in pixels */
633
623
  _sdk.maxWidth = Math.floor(window.innerWidth);
634
624
  /** Current maximum height of the playable ad container in pixels */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoud/playable-sdk",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "It's unified Playable SDK that supports MRAID, Google, Facebook, Vungle, and many more Ad Networks, simplifying playable ad development with a standardized interface for seamless cross-network compatibility.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",