-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathVPAIDFLASHClient.js
166 lines (129 loc) · 4.9 KB
/
VPAIDFLASHClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const swfobject = require('swfobject');
const JSFlashBridge = require('./jsFlashBridge').JSFlashBridge;
const VPAIDAdUnit = require('./VPAIDAdUnit').VPAIDAdUnit;
const noop = require('./utils').noop;
const callbackTimeout = require('./utils').callbackTimeout;
const isPositiveInt = require('./utils').isPositiveInt;
const createElementWithID = require('./utils').createElementWithID;
const uniqueVPAID = require('./utils').unique('vpaid');
const createFlashTester = require('./flashTester.js').createFlashTester;
const ERROR = 'error';
const FLASH_VERSION = '10.1.0';
let flashTester = {isSupported: ()=> true}; // if the runFlashTest is not run the flashTester will always return true
class VPAIDFLASHClient {
constructor (vpaidParentEl, callback, swfConfig = {data: 'VPAIDFlash.swf', width: 800, height: 400}, params = { wmode: 'transparent', salign: 'tl', align: 'left', allowScriptAccess: 'always', scale: 'noScale', allowFullScreen: 'true', quality: 'high'}, vpaidOptions = { debug: false, timeout: 10000 }) {
var me = this;
this._vpaidParentEl = vpaidParentEl;
this._flashID = uniqueVPAID();
this._destroyed = false;
callback = callback || noop;
swfConfig.width = isPositiveInt(swfConfig.width, 800);
swfConfig.height = isPositiveInt(swfConfig.height, 400);
createElementWithID(vpaidParentEl, this._flashID, true);
params.movie = swfConfig.data;
params.FlashVars = `flashid=${this._flashID}&handler=${JSFlashBridge.VPAID_FLASH_HANDLER}&debug=${vpaidOptions.debug}&salign=${params.salign}`;
if (!VPAIDFLASHClient.isSupported()) {
return onError('user don\'t support flash or doesn\'t have the minimum required version of flash ' + FLASH_VERSION);
}
this.el = swfobject.createSWF(swfConfig, params, this._flashID);
if (!this.el) {
return onError( 'swfobject failed to create object in element' );
}
var handler = callbackTimeout(vpaidOptions.timeout,
(err, data) => {
$loadPendedAdUnit.call(this);
callback(err, data);
}, () => {
callback('vpaid flash load timeout ' + vpaidOptions.timeout);
}
);
this._flash = new JSFlashBridge(this.el, swfConfig.data, this._flashID, swfConfig.width, swfConfig.height, handler);
function onError(error) {
setTimeout(() => {
callback(new Error(error));
}, 0);
return me;
}
}
destroy () {
this._destroyAdUnit();
if (this._flash) {
this._flash.destroy();
this._flash = null;
}
this.el = null;
this._destroyed = true;
}
isDestroyed () {
return this._destroyed;
}
_destroyAdUnit() {
delete this._loadLater;
if (this._adUnitLoad) {
this._adUnitLoad = null;
this._flash.removeCallback(this._adUnitLoad);
}
if (this._adUnit) {
this._adUnit._destroy();
this._adUnit = null;
}
}
loadAdUnit(adURL, callback) {
$throwIfDestroyed.call(this);
if (this._adUnit) {
this._destroyAdUnit();
}
if (this._flash.isReady()) {
this._adUnitLoad = (err, message) => {
if (!err) {
this._adUnit = new VPAIDAdUnit(this._flash);
}
this._adUnitLoad = null;
callback(err, this._adUnit);
};
this._flash.callFlashMethod('loadAdUnit', [adURL], this._adUnitLoad);
}else {
this._loadLater = {url: adURL, callback};
}
}
unloadAdUnit(callback = undefined) {
$throwIfDestroyed.call(this);
this._destroyAdUnit();
this._flash.callFlashMethod('unloadAdUnit', [], callback);
}
getFlashID() {
$throwIfDestroyed.call(this);
return this._flash.getFlashID();
}
getFlashURL() {
$throwIfDestroyed.call(this);
return this._flash.getFlashURL();
}
}
setStaticProperty('isSupported', () => {
return swfobject.hasFlashPlayerVersion(FLASH_VERSION) && flashTester.isSupported();
}, true);
setStaticProperty('runFlashTest', (swfConfig) => {
flashTester = createFlashTester(document.body, swfConfig);
});
function $throwIfDestroyed() {
if(this._destroyed) {
throw new Error('VPAIDFlashToJS is destroyed!');
}
}
function $loadPendedAdUnit() {
if (this._loadLater) {
this.loadAdUnit(this._loadLater.url, this._loadLater.callback);
delete this._loadLater;
}
}
function setStaticProperty(propertyName, value, writable = false) {
Object.defineProperty(VPAIDFLASHClient, propertyName, {
writable: writable,
configurable: false,
value: value
});
}
VPAIDFLASHClient.swfobject = swfobject;
module.exports = VPAIDFLASHClient;