forked from cowboy/jquery-postmessage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.ba-postmessage_amd.min.js
17 lines (14 loc) · 1.82 KB
/
jquery.ba-postmessage_amd.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
---------------------------------------------------------------
postMessage (optionally, jQuery plugin) - v2.0 - 2012
http://github.com/willowsystems/jquery-postmessage
Copyright (c) 2012 Willow Systems Corporation (willow-systems.com)
Copyright (c) 2009 "Cowboy" Ben Alman
Dual licensed under the MIT and GPL v.2 licenses.
See source distribution bundle for full licenses texts,
or, lacking that, see http://benalman.com/about/license/
---------------------------------------------------------------
*/
(function(){var e=function(){function e(){this.topics={};this.publish=function(b,a,c,g){if(this.topics[b]){var f=this.topics[b],j=Array.prototype.slice.call(arguments,1),e=[],d,h;d=0;for(h=f.length;d<h;d++)f[d][1]?(f[d][0]=function(){},e.push(d)):f[d][0].apply(null,j);d=0;for(h=e.length;d<h;d++)f.splice(e[d],1)}};this.subscribe=function(b,a,c){this.topics[b]?this.topics[b].push([a,c]):this.topics[b]=[[a,c]];return{topic:b,callback:a}};this.unsubscribe=function(b){if(this.topics[b.topic])for(var a=
this.topics[b.topic],c=0,e=a.length;c<e;c++)a[c][0]===b.callback&&a.splice(c,1)}}var g;return{receiveMessage:function(b,a,c){if(window.postMessage&&b){if(!g){g=new e;var i=function(a){g.publish(a.origin,a.data,a)};window.addEventListener?window.addEventListener("message",i,!1):window.attachEvent("onmessage",i)}if(void 0===a)a="*";else if("string"===typeof a)a=a.replace(/([^:]+:\/\/[^\/]+).*/,"$1");else throw Error("We don't support functions as origin conformance resolvers. Strings only.\nInstead, just register your callback with '*' origin and make it decideif it wants to handle it");
var f=g.subscribe(a,b,!c);return function(){g.unsubscribe(f)}}},postMessage:function(b,a,c){return window.postMessage?(c===undef&&(c=parent),a=a===undef?"*":a.replace(/([^:]+:\/\/[^\/]+).*/,"$1"),c.postMessage(b,a),!0):!1}}}();define(function(){return e})})();