Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cayasso committed Oct 17, 2013
0 parents commit cafcc42
Show file tree
Hide file tree
Showing 22 changed files with 8,324 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
components
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "test/mocha"]
path = test/mocha
url = git://github.com/visionmedia/mocha.git
[submodule "mocha"]
path = mocha
url = git://github.com/visionmedia/mocha.git
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
support
test
examples
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
build: components
@./node_modules/.bin/component-build \
--standalone postmessage \
--out . --name postmessage

components:
@./node_modules/.bin/component \
install --dev

test:
@./node_modules/.bin/mocha-phantomjs \
./test/index.html

clean:
rm -fr postmessage.js components

.PHONY: clean test
168 changes: 168 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# PostMessage

[![Build Status](https://travis-ci.org/cayasso/postmessage.png?branch=master)](https://travis-ci.org/cayasso/postmessage)
[![NPM version](https://badge.fury.io/js/postmessage.png)](http://badge.fury.io/js/postmessage)

Cross domain messaging made easy with `window.postMessage`.

## Instalation

As component:

```bash
$ component install cayasso/postmessage
```

Or you can just grab the postmessage.js bundle file and include it in your page:

```html
<script src="postmessage.js"></script>
```

## Usage

### On the main window

```javascript
var target = document.getElementById('myiframe').contentWindow;
var pub = postmessage('pub')();
var sub = postmessage('sub')(target);

sub.bind(function(message){
console.log(message); // -> THANK YOU
});

```

### On the iframe

```javascript
var pub = postmessage('pub')();
var sub = postmessage('sub')();

sub.subscribe(function(message){
// send back to parent window
pub.send('THANK YOU');
});

```

## API

### postmessage([type])

This method return a postmessage `type`, this can be `pub` for publishing or `sub` for subscribing.

```javascript
var Pub = postmessage('pub');
var Sub = postmessage('sub');
```
### Pub([target])

Create a publisher instance, a target window can be passed as first argument, if none the target
window will be `window.parent`.

```javascript
var Pub = postmessage('pub');
var pub = Pub(targetWindow);
```
### pub.target([window]);

Target window setter and/or getter, if no argument is passed it will get the current
target window, else it will set a target window.

```javascript
pub.target(targetWindow);

// get the current target window
var target = pub.target();
```

### pub.origin([domain]);

Target origin setter and/or getter, if no argument is passed it will get the current
target origin, else it will set a target origin.

```javascript
pub.origin('http://example.com');

// get the current target window
console.log(pub.origin()); // -> http://example.com
```

### pub.defaults()

Reset publisher `target` window and `origin` to defaul values.

```javascript
pub.origin('http://example.com');

// reset publisher
pub.defaults();

// get the current target window
console.log(pub.origin()); // -> '*'
```

### pub.send(data, [target, [origin]])

Send a message to a target window. You can pass the target and origin as second
and third parametters correspondingly.

```javascript
pub.send({ hello: 'world' });

or with a target window:

pub.send({ hello: 'world' }, targetWindow);

or with specifying the origin:

pub.send({ hello: 'world' }, targetWindow, 'http://example.com');
```

### Sub([window])

## Run tests

First in the root directory do:

``` bash
$ npm install
```

Then run the test like this:

``` bash
$ make test
```

## Other plugins

* [primus-rooms](https://github.com/cayasso/primus-rooms)
* [primus-multiplex](https://github.com/cayasso/primus-multiplex)

## License

(The MIT License)

Copyright (c) 2013 Jonathan Brumley &lt;[email protected]&gt;

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 changes: 18 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "postmessage",
"version": "0.0.0",
"description": "Cross domain messaging made easy with postMessage.",
"repo": "cayasso/postmessage",
"main": "index.js",
"scripts": [
"lib/pub.js",
"lib/sub.js",
"lib/index.js",
"index.js"
],
"dependencies": {
"component/bind": "*"
},
"license": "MIT",
"remotes": []
}
Empty file added examples/child.html
Empty file.
Empty file added examples/index.html
Empty file.
Empty file added examples/index.js
Empty file.
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Module dependencies.
*/

var pm = require('./lib');

/**
* Expose module.
*/

module.exports = PostMessage;

/**
* Factory method for getting a post message object.
*
* @param {String} type
* @return {Subcriber|Publisher}
* @api public
*/

function PostMessage(type){
return pm[type];
}

/**
* Expose modules.
*/

PostMessage.Pub = pm.pub;
PostMessage.Sub = pm.sub;
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Expose modules.
*/

module.exports = {
pub: require('./pub'),
sub: require('./sub')
};
79 changes: 79 additions & 0 deletions lib/pub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Expose `Subscriber`.
*/

module.exports = Publisher;

/**
* Initialize a new `Publisher`.
*
* @param {window} win The target window.
* @api public
*/

function Publisher(target) {
if (!(this instanceof Publisher)) return new Publisher(target);
this.defaults().target(target);
}

/**
* Reset publisher setting (target & origin) to defaults.
*
* @return {Publisher} self
* @api public
*/

Publisher.prototype.defaults = function defaults() {
this._origin = "*";
this._target = window.parent;
return this;
};

/**
* Set the target window object, it defaults to `window`.
*
* @param {window} target The target window.
* @return {Publisher} self
* @api public
*/

Publisher.prototype.target = function target(target) {
if (!arguments.length) return this._target;
this._target = target || this._target;
return this;
};

/**
* Set target origin, it default to `*`.
*
* @param {String} origin The domain eg. http://mysite.com
* @return {Publisher} self
* @api public
*/

Publisher.prototype.origin = function origin(origin) {
if (!arguments.length) return this._origin;
this._origin = origin || this._origin;
return this;
};

/**
* Send a message to the target origin.
*
* @param {Mixed} data The message to send.
* @param {window} [target] The target window, its optional.
* @param {String} [origin] The domain, its optional.
* @return {Publisher} self
* @api public
*/

Publisher.prototype.send =
Publisher.prototype.publish = function send(data, target, origin) {
var pub = this;
data = JSON.stringify(data);
this.target(target).origin(origin);
setTimeout(function(){
pub._target.postMessage(data, pub._origin);
}, 0);
return this;
};
Loading

0 comments on commit cafcc42

Please sign in to comment.