Skip to content

Commit 05fd470

Browse files
committed
Improved API and bundling
1 parent 06e7be7 commit 05fd470

File tree

7 files changed

+337
-312
lines changed

7 files changed

+337
-312
lines changed

build/react-swf.js

Lines changed: 106 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -24,127 +24,67 @@
2424

2525
'use strict';
2626

27+
2728
(function (root, factory) {
2829
if (typeof define === 'function' && define.amd) {
2930
// AMD. Register as an anonymous module.
30-
define(['exports', 'react'], factory);
31+
define(['react'], factory);
3132
} else if (typeof exports === 'object') {
32-
// CommonJS
33-
factory(exports, require('react'));
33+
// Node. Does not work with strict CommonJS, but
34+
// only CommonJS-like enviroments that support module.exports,
35+
// like Node.
36+
module.exports = factory(require('react'));
3437
} else {
35-
// Browser globals
36-
factory((root.ReactSWF = {}), root.React);
38+
// Browser globals (root is window)
39+
root.ReactSWF = factory(root.React);
3740
}
38-
}(this, function (exports, React) {
39-
41+
}(this, function (React) {
42+
43+
var encodeFlashKeyValueRegex = /[\r%&+]/g;
4044
var encodeFlashKeyValueLookup = {
4145
'\r': '%0D', '%': '%25', '&': '%26', '+': '%2B', '=': '%3D'
4246
};
4347

4448
var objectParamNames = {
45-
play: true, loop: true, menu: true, quality: true, scale: true,
46-
bgColor: true, wmode: true, base: true, allowScriptAccess: true,
49+
play: true, loop: true, menu: true, quality: true, scale: true, align: true,
50+
salign: true, bgColor: true, wmode: true, base: true, allowScriptAccess: true,
4751
allowFullScreen: true, fullScreenAspectRatio: true
4852
};
4953

50-
var flashPlayerVersion;
51-
5254
var nextUniqueObjectSwfId = 0;
5355

54-
var memoryLeakWorkaround;
55-
56-
57-
/**
58-
* Get the installed Flash Player version.
59-
*
60-
* @return {?string} Version as X.Y.Z, null if not installed/enabled.
61-
*/
62-
function getFPVersion() {
63-
if (flashPlayerVersion === undefined) {
64-
flashPlayerVersion = null;
65-
66-
if ('plugins' in navigator) {
67-
var plugin = navigator.plugins['Shockwave Flash'];
68-
if (plugin) {
69-
var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];
70-
if (mimeType && mimeType.enabledPlugin) {
71-
flashPlayerVersion = plugin.description
72-
.match(/(\d+)\.(\d+) r(\d+)/).slice(1).join('.');
73-
}
74-
}
75-
} else if ('ActiveXObject' in window) {
76-
try {
77-
var axObject = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
78-
if (axObject) {
79-
flashPlayerVersion = axObject.GetVariable('$version')
80-
.match(/(\d+),(\d+),(\d+)/).slice(1).join('.');
81-
}
82-
}
83-
catch (e) {}
84-
}
85-
}
86-
87-
return flashPlayerVersion;
88-
}
8956

90-
/**
91-
* Checks if the required Flash Player version is supported by the client.
92-
*
93-
* @param {string} version Required version.
94-
* @return {boolean} True if the version is supported.
95-
*/
96-
function isFPVersionSupported(version) {
97-
var supportedVersion = getFPVersion();
98-
99-
if (supportedVersion === null) {
100-
return false;
101-
}
102-
103-
var supportedVersionArray = supportedVersion.split('.');
104-
var requiredVersionArray = version.split('.');
105-
106-
for (var i = 0; i < requiredVersionArray.length; i++) {
107-
if (+supportedVersionArray[i] > +requiredVersionArray[i]) {
108-
return true;
109-
}
110-
if (+supportedVersionArray[i] < +requiredVersionArray[i]) {
111-
return false;
112-
}
113-
}
114-
115-
return true;
57+
function encodeFlashKeyValueEncoder(match) {
58+
return encodeFlashKeyValueLookup[match];
11659
}
11760

118-
/**
119-
* Encodes text for safe transport through flashVars.
120-
*
121-
* @param {*} text Text value to encode.
122-
* @return {string} An encoded string.
123-
*/
12461
function encodeFlashKeyValue(string) {
12562
// Encode \r or it may be normalized into \n
126-
return ('' + string).replace(/[\r%&+]/g, function(match) {
127-
return encodeFlashKeyValueLookup[match];
128-
});
63+
return ('' + string).replace(
64+
encodeFlashKeyValueRegex,
65+
encodeFlashKeyValueEncoder
66+
);
12967
}
13068

131-
13269
function encodeFlashVarsObject(obj) {
13370
// Pushing encoded key-values to an array instead of immediately
13471
// concatenating is faster and scales better with large values.
13572
var list = [];
13673

13774
for (var key in obj) {
138-
list.push(
139-
encodeFlashKeyValue(key) + '=' +
140-
encodeFlashKeyValue(obj[key])
141-
);
75+
if (obj[key] !== null) {
76+
list.push(
77+
encodeFlashKeyValue(key) + '=' +
78+
encodeFlashKeyValue(obj[key])
79+
);
80+
}
14281
}
14382

14483
return list.join('&');
14584
}
14685

147-
var swf = React.createClass({
86+
87+
var ReactSWF = React.createClass({
14888
getInitialState: function() {
14989
return {
15090
// flash.external.ExternalInterface.addCallback requires a unique id
@@ -154,13 +94,9 @@
15494
componentWillUnmount: function() {
15595
// IE8: leaks memory if all ExternalInterface-callbacks have not been
15696
// removed. Only IE implements readyState, hasOwnProperty does not exist
157-
// for DOM nodes in IE8.
97+
// for DOM nodes in IE8, but does in IE9+.
15898

159-
if (memoryLeakWorkaround === undefined) {
160-
memoryLeakWorkaround =
161-
'readyState' in document && !('hasOwnProperty' in document);
162-
}
163-
if (memoryLeakWorkaround) {
99+
if ('readyState' in document && !('hasOwnProperty' in document)) {
164100
this._cleanup();
165101
}
166102
},
@@ -176,7 +112,7 @@
176112
render: function() {
177113
var params = [];
178114

179-
// IE8: requires the use of the movie param instead of src
115+
// IE8: requires the use of the movie param to function
180116
params.push(
181117
React.DOM.param({
182118
key: 'movie',
@@ -209,8 +145,8 @@
209145

210146
params.push(
211147
React.DOM.param({
212-
key: 'flashVars',
213-
name: 'flashVars',
148+
key: 'flashvars',
149+
name: 'flashvars',
214150
value: encodedFlashVars
215151
})
216152
);
@@ -228,10 +164,80 @@
228164
});
229165

230166

231-
module.exports.getFPVersion = getFPVersion;
232-
module.exports.isFPVersionSupported = isFPVersionSupported;
167+
var cachedFPVersion;
168+
169+
/**
170+
* Detect installed Flash Player version. Result is cached.
171+
*
172+
* @return {?string} 'X.Y.Z'-version, or null.
173+
*/
174+
function getFPVersion() {
175+
if (cachedFPVersion === undefined) {
176+
cachedFPVersion = null;
177+
178+
if (navigator.plugins) {
179+
var plugin = navigator.plugins['Shockwave Flash'];
180+
if (plugin) {
181+
var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];
182+
if (mimeType && mimeType.enabledPlugin) {
183+
var matches = plugin.description
184+
.match(/^Shockwave Flash (\d+)(?:\.(\d+))?(?: r(\d+))?/);
185+
186+
cachedFPVersion =
187+
matches[1] + '.' + (matches[2] || 0) + '.' + (matches[3] || 0);
188+
}
189+
}
190+
}
191+
if (window.ActiveXObject) {
192+
try {
193+
var axObject = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
194+
if (axObject) {
195+
cachedFPVersion = axObject.GetVariable('$version')
196+
.match(/^WIN (\d+),(\d+),(\d+)/).slice(1).join('.');
197+
}
198+
}
199+
catch (e) {}
200+
}
201+
}
202+
203+
return cachedFPVersion;
204+
}
233205

234-
module.exports.DOM = {};
235-
module.exports.DOM.swf = swf;
206+
/**
207+
* Detect if installed Flash Player version meets requirements.
208+
*
209+
* @param {string} version 'X.Y.Z' or 'X.Y' or 'X'-version.
210+
* @return {boolean} True if version is supported.
211+
*/
212+
function isFPVersionSupported(version) {
213+
var supportedVersion = getFPVersion();
214+
215+
if (supportedVersion === null) {
216+
return false;
217+
}
218+
219+
var supportedVersionArray = supportedVersion.split('.');
220+
var requiredVersionArray = version.split('.');
221+
222+
for (var i = 0; i < requiredVersionArray.length; i++) {
223+
if (+supportedVersionArray[i] > +requiredVersionArray[i]) {
224+
return true;
225+
}
226+
if (+supportedVersionArray[i] < +requiredVersionArray[i]) {
227+
return false;
228+
}
229+
}
230+
231+
return true;
232+
}
233+
234+
235+
ReactSWF.utils = {
236+
getFPVersion: getFPVersion,
237+
isFPVersionSupported: isFPVersionSupported
238+
};
239+
240+
241+
return ReactSWF;
236242

237243
}));

build/react-swf.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm-react-swf/README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,4 @@
22

33
Shockwave Flash Player component for [React](https://github.com/facebook/react)
44

5-
## Component
6-
7-
`ReactSWF.DOM.swf`
8-
9-
`src` `width` `height` `play` `loop` `menu` `quality` `scale` `bgColor` `wmode` `base` `allowScriptAccess` `allowFullScreen` `fullScreenAspectRatio` `flashVars`
10-
11-
http://helpx.adobe.com/flash/kb/flash-object-embed-tag-attributes.html
12-
13-
## Utility functions
14-
15-
`ReactSWF.getFPVersion()`
16-
17-
`ReactSWF.isFPVersionSupported(version)`
5+
[Documentation](https://github.com/syranide/react-swf/)

0 commit comments

Comments
 (0)