-
Notifications
You must be signed in to change notification settings - Fork 14
/
react-swf.js
257 lines (212 loc) · 6.95 KB
/
react-swf.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*! react-swf v1.0.7 | @syranide | MIT license */
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['prop-types', 'react'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('prop-types'), require('react'));
} else {
root.ReactSWF = factory(root.PropTypes, root.React);
}
}(this, function(PropTypes, React) {
'use strict';
/*
flashVars = {key: string} or "key=value&..."
allowFullScreen = true, false*
allowFullScreenInteractive = true, false*
allowNetworking = all*, internal, none
allowScriptAccess = always, sameDomain, never
align = l, t, r
base = url
bgcolor = #RRGGBB
browserZoom = scale*, noscale
fullScreenAspectRatio = portrait, landscape
loop = true*, false
menu = true*, false
play = true*, false
quality = low, autolow, autohigh, medium, high, best
salign = l, t, r, tl, tr
scale = default*, noborder, exactfit, noscale
seamlessTabbing = true*, false
wmode = window*, direct, opaque, transparent, gpu
*/
var supportedFPParamNames = {
flashVars: 'flashvars',
allowFullScreen: 'allowfullscreen',
allowFullScreenInteractive: 'allowfullscreeninteractive',
allowNetworking: 'allownetworking',
allowScriptAccess: 'allowscriptaccess',
align: 'align',
base: 'base',
bgcolor: 'bgcolor',
browserZoom: 'browserzoom',
fullScreenAspectRatio: 'fullscreenaspectratio',
loop: 'loop',
menu: 'menu',
play: 'play',
quality: 'quality',
salign: 'salign',
scale: 'scale',
seamlessTabbing: 'seamlesstabbing',
wmode: 'wmode'
};
var booleanFPParams = {
allowFullScreen: true,
allowFullScreenInteractive: true,
loop: true,
menu: true,
play: true,
seamlessTabbing: true
};
var ENCODE_FLASH_VARS_REGEX = /[\r%&+=]/g;
var ENCODE_FLASH_VARS_LOOKUP = {
'\r': '%0D',
'%': '%25',
'&': '%26',
'+': '%2B',
'=': '%3D'
};
function encodeFlashVarsStringReplacer(match) {
return ENCODE_FLASH_VARS_LOOKUP[match];
}
function encodeFlashVarsString(string) {
return ('' + string).replace(
ENCODE_FLASH_VARS_REGEX,
encodeFlashVarsStringReplacer
);
}
function encodeFlashVarsObject(object) {
// Push to array; faster and scales better than string concat.
var output = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
var value = object[key];
if (value != null) {
output.push(
encodeFlashVarsString(key) + '=' + encodeFlashVarsString(value)
);
}
}
}
return output.join('&');
}
/** @constructor */
function ReactSWF(props) {
React.Component.call(this, props);
var that = this;
this._refCallback = function(c) {
that._node = c;
};
// The only way to change Flash parameters or reload the movie is to update
// the key of the ReactSWF element. This unmounts the previous instance and
// reloads the movie. Store initial values to keep the DOM consistent.
var params = {};
for (var key in supportedFPParamNames) {
if (supportedFPParamNames.hasOwnProperty(key) &&
props.hasOwnProperty(key)) {
var value = props[key];
if (value != null) {
var name = supportedFPParamNames[key];
if (name === 'flashvars' && typeof value === 'object') {
value = encodeFlashVarsObject(value);
} else if (booleanFPParams.hasOwnProperty(key)) {
// Force boolean parameter arguments to be boolean.
value = !!value;
}
params[name] = '' + value;
}
}
}
this._node = null;
this.state = {
src: props.src,
params: params
};
}
ReactSWF.prototype = Object.create(React.Component.prototype);
ReactSWF.prototype.constructor = ReactSWF;
Object.assign(ReactSWF, React.Component);
ReactSWF.propTypes = {
src: PropTypes.string.isRequired,
flashVars: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
allowFullScreen: PropTypes.bool,
allowFullScreenInteractive: PropTypes.bool,
allowNetworking: PropTypes.oneOf(['all', 'internal', 'none']),
allowScriptAccess: PropTypes.oneOf(['always', 'sameDomain', 'never']),
align: PropTypes.oneOf(['l', 't', 'r']),
base: PropTypes.string,
bgcolor: PropTypes.string,
browserZoom: PropTypes.oneOf(['scale', 'noscale']),
fullScreenAspectRatio: PropTypes.oneOf(['portrait', 'landscape']),
loop: PropTypes.bool,
menu: PropTypes.bool,
play: PropTypes.bool,
quality: PropTypes.oneOf(['low', 'autolow', 'autohigh', 'medium', 'high', 'best']),
salign: PropTypes.oneOf(['l', 't', 'r', 'tl', 'tr']),
scale: PropTypes.oneOf(['default', 'noborder', 'exactfit', 'noscale']),
seamlessTabbing: PropTypes.bool,
wmode: PropTypes.oneOf(['window', 'direct', 'opaque', 'transparent', 'gpu'])
};
ReactSWF.prototype.getFPDOMNode = function() {
return this._node;
};
ReactSWF.prototype.shouldComponentUpdate = function(nextProps) {
var prevProps = this.props;
for (var key in prevProps) {
// Ignore all Flash parameter props
if (prevProps.hasOwnProperty(key) &&
!supportedFPParamNames.hasOwnProperty(key) &&
(!nextProps.hasOwnProperty(key) ||
!Object.is(prevProps[key], nextProps[key]))) {
return true;
}
}
for (var key in nextProps) {
if (nextProps.hasOwnProperty(key) &&
!supportedFPParamNames.hasOwnProperty(key) &&
!prevProps.hasOwnProperty(key)) {
return true;
}
}
return false;
};
ReactSWF.prototype.render = function() {
var props = this.props;
var state = this.state;
// AS3 `ExternalInterface.addCallback` requires a unique node ID in IE8-10.
// There is however no isolated way to play nice with server-rendering, so
// we must leave it up to the user.
var objectProps = {
ref: this._refCallback,
children: [],
type: 'application/x-shockwave-flash',
data: state.src,
// Discard `props.src`
src: null
};
for (var key in props) {
// Ignore props that are Flash parameters or managed by this component.
if (props.hasOwnProperty(key) &&
!supportedFPParamNames.hasOwnProperty(key) &&
!objectProps.hasOwnProperty(key)) {
objectProps[key] = props[key];
}
}
var objectChildren = objectProps.children;
for (var name in state.params) {
objectChildren.push(
React.createElement('param', {
key: name,
name: name,
value: state.params[name]
})
);
}
// Push `props.children` to the end of the children, React will generate a
// key warning if there are multiple children. This is preferable for now.
if (props.children != null) {
objectChildren.push(props.children);
}
return React.createElement('object', objectProps);
};
return ReactSWF;
}));