Skip to content

Commit

Permalink
Forked getFPVersion + isFPVersionSupported into SWFPlayerVersion module
Browse files Browse the repository at this point in the history
  • Loading branch information
syranide committed Oct 9, 2015
1 parent 5fbdca1 commit f57f5da
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 252 deletions.
80 changes: 52 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Supports all browsers supported by React.

Depends on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Polyfill_for_non-ES6_browsers) and [`Object.assign()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill)

```
Use [SWFPlayerVersion](https://github.com/syranide/swf-player-version) to determine SWF Player support.

```jsx
<ReactSWF
src="example.swf"
id="guid_001"
Expand All @@ -16,22 +18,62 @@ Depends on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScri
flashVars={{foo: 'A', bar: 1}}
/>
```
```js
// Test if Flash Player version is supported and output the actual version.
if (ReactSWF.isFPVersionSupported('10.0')) {
alert('Flash Player ' + ReactSWF.getFPVersion() + ' is installed');
```jsx
const SWF_ID_PREFIX = '__MyExternalInterfaceExample_SWFID_';
const SWF_CALL_NAME_PREFIX = '__MyExternalInterfaceExample_SWFCall_';

let nextUID = 0;

class MyExternalInterfaceExample extends React.Component {
constructor(props) {
super(props);

// For most purposes nextUID is sufficient. However, if you rely on
// non-trivial server rendering you must generate deterministic UIDs per
// React root to avoid markup mismatch.
this._uid = nextUID++;

window[SWF_CALL_NAME_PREFIX + this._uid] = this.handleSWFCall.bind(this);
}

componentWillUnmount() {
delete window[SWF_CALL_NAME_PREFIX + this._uid];
}

handleSWFCall() {
// Beware; SWF calls are executed in the context of SWF Player.
console.log('SWFCall', arguments);
return 'foobar';
}

invokeSWFMyCallback(arg) {
// Beware; SWF Player does not sufficiently escape serialized arguments.
return this._swfPlayerNode.myCallback(arg);
}

render() {
// Globally unique ID is required for IE<11 for ExternalInterface callbacks.
return (
<ReactSWF
...
ref={c => this._swfPlayerNode = c}
id={SWF_ID_PREFIX + this._uid}
flashVars={{myCallbackName: SWF_CALL_NAME_PREFIX + this._uid}}
/>
);
}
}
```
```js
// ExternalInterface callbacks are invoked on the DOM node as usual.
var returnValue = ref.getFPDOMNode().myEICallback(...);
```

## Breaking changes

#### 0.13.0

* `getFPVersion` and `isFPVersionSupported` forked to [SWFPlayerVersion](https://github.com/syranide/swf-player-version) and dropped from ReactSWF. Replace `ReactSWF.getFPVersion => SWFPlayerVersion.get` and `ReactSWF.isFPVersionSupported => SWFPlayerVersion.isSupported`.

#### 0.12.3

* Depends on `Object.assign()`, [polyfills are available.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill)
* Depends on `Object.assign()`, [polyfills are available](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill).

#### 0.11.0

Expand Down Expand Up @@ -76,24 +118,6 @@ Detailed explanation of most properties found at [[Flash OBJECT and EMBED tag at

## API

#### Static methods

```
getFPVersion()
returns {?string} 'X.Y.Z'-version or null.
Returns installed Flash Player version. Result is cached.
Must not be called in a non-browser environment.
```
```
isFPVersionSupported(versionString)
versionString {string} 'X.Y.Z', 'X.Y' or 'X'.
returns {boolean} true if supported.
Returns if installed Flash Player meets version requirement.
Must not be called in a non-browser environment.
```

#### Instance methods

```
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-swf",
"version": "0.12.3",
"version": "0.13.0",
"license": "MIT",
"description": "Shockwave Flash Player component for React",
"authors": ["Andreas Svensson <[email protected]>"],
Expand All @@ -12,7 +12,7 @@
"url": "https://github.com/syranide/react-swf"
},
"dependencies": {
"react": "^0.14.0-beta1"
"react": "^0.14.0"
},
"keywords": [
"react",
Expand Down
80 changes: 52 additions & 28 deletions npm-react-swf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Supports all browsers supported by React.

Depends on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Polyfill_for_non-ES6_browsers) and [`Object.assign()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill)

```
Use [SWFPlayerVersion](https://github.com/syranide/swf-player-version) to determine SWF Player support.

```jsx
<ReactSWF
src="example.swf"
id="guid_001"
Expand All @@ -16,22 +18,62 @@ Depends on [`Object.is()`](https://developer.mozilla.org/en-US/docs/Web/JavaScri
flashVars={{foo: 'A', bar: 1}}
/>
```
```js
// Test if Flash Player version is supported and output the actual version.
if (ReactSWF.isFPVersionSupported('10.0')) {
alert('Flash Player ' + ReactSWF.getFPVersion() + ' is installed');
```jsx
var SWF_ID_PREFIX = '__MyExternalInterfaceExample_SWFID_';
var SWF_CALL_NAME_PREFIX = '__MyExternalInterfaceExample_SWFCall_';

var nextUID = 0;

class MyExternalInterfaceExample extends React.Component {
constructor(props) {
super(props);

// For most purposes nextUID is sufficient. However, if you rely on
// non-trivial server rendering you must generate deterministic UIDs per
// React root to avoid markup mismatch.
this._uid = nextUID++;

window[SWF_CALL_NAME_PREFIX + this._uid] = this.handleSWFCall.bind(this);
}

componentWillUnmount() {
delete window[SWF_CALL_NAME_PREFIX + this._uid];
}

handleSWFCall() {
// Beware; SWF calls are executed in the context of SWF Player.
console.log('SWFCall', arguments);
return 'foobar';
}

invokeSWFMyCallback(arg) {
// Beware; SWF Player does not sufficiently escape serialized arguments.
return this._swfPlayerNode.myCallback(arg);
}

render() {
// Globally unique ID is required for IE<11 for ExternalInterface callbacks.
return (
<ReactSWF
...
ref={c => this._swfPlayerNode = c}
id={SWF_ID_PREFIX + this._uid}
flashVars={{myCallbackName: SWF_CALL_NAME_PREFIX + this._uid}}
/>
);
}
}
```
```js
// ExternalInterface callbacks are invoked on the DOM node as usual.
var returnValue = ref.getFPDOMNode().myEICallback(...);
```

## Breaking changes

#### 0.13.0

* `getFPVersion` and `isFPVersionSupported` forked to [SWFPlayerVersion](https://github.com/syranide/swf-player-version) and dropped from ReactSWF. Replace `ReactSWF.getFPVersion => SWFPlayerVersion.get` and `ReactSWF.isFPVersionSupported => SWFPlayerVersion.isSupported`.

#### 0.12.3

* Depends on `Object.assign()`, [polyfills are available.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill)
* Depends on `Object.assign()`, [polyfills are available](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill).

#### 0.11.0

Expand Down Expand Up @@ -76,24 +118,6 @@ Detailed explanation of most properties found at [[Flash OBJECT and EMBED tag at

## API

#### Static methods

```
getFPVersion()
returns {?string} 'X.Y.Z'-version or null.
Returns installed Flash Player version. Result is cached.
Must not be called in a non-browser environment.
```
```
isFPVersionSupported(versionString)
versionString {string} 'X.Y.Z', 'X.Y' or 'X'.
returns {boolean} true if supported.
Returns if installed Flash Player meets version requirement.
Must not be called in a non-browser environment.
```

#### Instance methods

```
Expand Down
4 changes: 2 additions & 2 deletions npm-react-swf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-swf",
"version": "0.12.3",
"version": "0.13.0",
"license": "MIT",
"description": "Shockwave Flash Player component for React",
"author": "Andreas Svensson <[email protected]>",
Expand All @@ -12,7 +12,7 @@
"url": "https://github.com/syranide/react-swf"
},
"peerDependencies": {
"react": "^0.14.0-beta1"
"react": "^0.14.0"
},
"keywords": [
"react",
Expand Down
97 changes: 3 additions & 94 deletions npm-react-swf/react-swf.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*! react-swf v0.12.3 | @syranide | MIT license */
/*! react-swf v0.13.0 | @syranide | MIT license */

'use strict';

var React = require('react');
var PropTypes = React.PropTypes;

var mimeTypeFP = 'application/x-shockwave-flash';

/*
flashVars = {key: string} or "key=value&..."
Expand Down Expand Up @@ -98,92 +96,6 @@ function encodeFlashVarsObject(object) {
}


var memoizedFPVersion;

function getMemoizedFPVersion() {
if (memoizedFPVersion === undefined) {
memoizedFPVersion = getFPVersion();
}

return memoizedFPVersion;
}

/**
* Detect installed Flash Player version. Cached.
*
* @return {?string} 'X.Y.Z'-version or null.
*/
function getFPVersion() {
if (typeof navigator !== 'undefined') {
var navFPPlugin = (
navigator.plugins &&
navigator.plugins['Shockwave Flash']
);
var navFPMimeType = (
navigator.mimeTypes &&
navigator.mimeTypes[mimeTypeFP]
);

if (navFPPlugin && navFPMimeType && navFPMimeType.enabledPlugin) {
try {
return (
navFPPlugin
.description
.match(/(\d+)\.(\d+) r(\d+)/)
.slice(1)
.join('.')
);
} catch (e) {
}
}
}

// ActiveXObject-fallback for IE8-10
if (typeof ActiveXObject !== 'undefined') {
try {
return (
new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
.GetVariable('$version')
.match(/(\d+),(\d+),(\d+)/)
.slice(1)
.join('.')
);
} catch (e) {
}
}

return null;
}

/**
* Detect if installed Flash Player meets version requirement.
*
* @param {string} versionString 'X.Y.Z', 'X.Y' or 'X'.
* @return {boolean} true if supported.
*/
function isFPVersionSupported(versionString) {
var installedString = getMemoizedFPVersion();

if (installedString == null) {
return false;
}

var installedFields = installedString.split('.');
var requiredFields = versionString.split('.');

for (var i = 0; i < 3; i++) {
var installedNumber = +installedFields[i];
var requiredNumber = +(requiredFields[i] || 0);

if (installedNumber !== requiredNumber) {
return installedNumber > requiredNumber;
}
}

return true;
}


/** @constructor */
function ReactSWF(props) {
React.Component.call(this, props);
Expand Down Expand Up @@ -229,12 +141,9 @@ function ReactSWF(props) {
};
}

Object.assign(ReactSWF, React.Component);
ReactSWF.prototype = Object.create(React.Component.prototype);
ReactSWF.prototype.constructor = ReactSWF;

ReactSWF.getFPVersion = getMemoizedFPVersion;
ReactSWF.isFPVersionSupported = isFPVersionSupported;
Object.assign(ReactSWF, React.Component);

ReactSWF.propTypes = {
src: PropTypes.string.isRequired,
Expand Down Expand Up @@ -313,7 +222,7 @@ ReactSWF.prototype.render = function() {
var objectProps = {
ref: this._refCallback,
children: [],
type: mimeTypeFP,
type: 'application/x-shockwave-flash',
data: state.src,
// Discard `props.src`
src: null
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-swf",
"version": "0.12.3",
"version": "0.13.0",
"license": "MIT",
"description": "Shockwave Flash Player component for React",
"author": "Andreas Svensson <[email protected]>",
Expand All @@ -12,7 +12,7 @@
"url": "https://github.com/syranide/react-swf"
},
"peerDependencies": {
"react": "^0.14.0-beta1"
"react": "^0.14.0"
},
"keywords": [
"react",
Expand Down
Loading

0 comments on commit f57f5da

Please sign in to comment.