benjamine/frame-channels

View on GitHub
public/build/frame-channels.min.js

Summary

Maintainability
A
0 mins
Test Coverage
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.frameChannels=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function(e){var r=require("./channel");if(exports.Channel=r,exports.create=function(e,o){return new r(e,o)},e.browser)exports.version="0.0.65",exports.homepage="https://github.com/benjamine/frame-channels";else{var o=require("../package.json");exports.version=o.version,exports.homepage=o.homepage}}).call(this,require("_process"));
},{"../package.json":13,"./channel":15,"_process":2}],2:[function(require,module,exports){
function noop(){}var process=module.exports={};process.nextTick=function(){var o="undefined"!=typeof window&&window.setImmediate,e="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(o)return function(o){return window.setImmediate(o)};if(e){var s=[];return window.addEventListener("message",function(o){var e=o.source;if((e===window||null===e)&&"process-tick"===o.data&&(o.stopPropagation(),s.length>0)){var n=s.shift();n()}},!0),function(o){s.push(o),window.postMessage("process-tick","*")}}return function(o){setTimeout(o,0)}}(),process.title="browser",process.browser=!0,process.env={},process.argv=[],process.on=noop,process.addListener=noop,process.once=noop,process.off=noop,process.removeListener=noop,process.removeAllListeners=noop,process.emit=noop,process.binding=function(){throw new Error("process.binding is not supported")},process.cwd=function(){return"/"},process.chdir=function(){throw new Error("process.chdir is not supported")};
},{}],3:[function(require,module,exports){
"use strict";var Promise=require("./promise/promise").Promise,polyfill=require("./promise/polyfill").polyfill;exports.Promise=Promise,exports.polyfill=polyfill;
},{"./promise/polyfill":7,"./promise/promise":8}],4:[function(require,module,exports){
"use strict";function all(r){var n=this;if(!isArray(r))throw new TypeError("You must pass an array to all.");return new n(function(n,t){function i(r){return function(n){u(r,n)}}function u(r,t){e[r]=t,0===--s&&n(e)}var a,e=[],s=r.length;0===s&&n([]);for(var o=0;o<r.length;o++)a=r[o],a&&isFunction(a.then)?a.then(i(o),t):u(o,a)})}var isArray=require("./utils").isArray,isFunction=require("./utils").isFunction;exports.all=all;
},{"./utils":12}],5:[function(require,module,exports){
(function(e,n){"use strict";function t(){return function(){e.nextTick(r)}}function o(){var e=0,n=new a(r),t=document.createTextNode("");return n.observe(t,{characterData:!0}),function(){t.data=e=++e%2}}function i(){return function(){c.setTimeout(r,1)}}function r(){for(var e=0;e<s.length;e++){var n=s[e],t=n[0],o=n[1];t(o)}s=[]}function u(e,n){var t=s.push([e,n]);1===t&&f()}var f,d="undefined"!=typeof window?window:{},a=d.MutationObserver||d.WebKitMutationObserver,c="undefined"!=typeof n?n:void 0===this?window:this,s=[];f="undefined"!=typeof e&&"[object process]"==={}.toString.call(e)?t():a?o():i(),exports.asap=u}).call(this,require("_process"),"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{});
},{"_process":2}],6:[function(require,module,exports){
"use strict";function configure(n,i){return 2!==arguments.length?config[n]:void(config[n]=i)}var config={instrument:!1};exports.config=config,exports.configure=configure;
},{}],7:[function(require,module,exports){
(function(e){"use strict";function i(){var i;i="undefined"!=typeof e?e:"undefined"!=typeof window&&window.document?window:self;var r="Promise"in i&&"resolve"in i.Promise&&"reject"in i.Promise&&"all"in i.Promise&&"race"in i.Promise&&function(){var e;return new i.Promise(function(i){e=i}),o(e)}();r||(i.Promise=n)}var n=require("./promise").Promise,o=require("./utils").isFunction;exports.polyfill=i}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{});
},{"./promise":8,"./utils":12}],8:[function(require,module,exports){
"use strict";function Promise(e){if(!isFunction(e))throw new TypeError("You must pass a resolver function as the first argument to the promise constructor");if(!(this instanceof Promise))throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");this._subscribers=[],invokeResolver(e,this)}function invokeResolver(e,t){function r(e){resolve(t,e)}function i(e){reject(t,e)}try{e(r,i)}catch(n){i(n)}}function invokeCallback(e,t,r,i){var n,s,o,c,u=isFunction(r);if(u)try{n=r(i),o=!0}catch(l){c=!0,s=l}else n=i,o=!0;handleThenable(t,n)||(u&&o?resolve(t,n):c?reject(t,s):e===FULFILLED?resolve(t,n):e===REJECTED&&reject(t,n))}function subscribe(e,t,r,i){var n=e._subscribers,s=n.length;n[s]=t,n[s+FULFILLED]=r,n[s+REJECTED]=i}function publish(e,t){for(var r,i,n=e._subscribers,s=e._detail,o=0;o<n.length;o+=3)r=n[o],i=n[o+t],invokeCallback(t,r,i,s);e._subscribers=null}function handleThenable(e,t){var r,i=null;try{if(e===t)throw new TypeError("A promises callback cannot return that same promise.");if(objectOrFunction(t)&&(i=t.then,isFunction(i)))return i.call(t,function(i){return r?!0:(r=!0,void(t!==i?resolve(e,i):fulfill(e,i)))},function(t){return r?!0:(r=!0,void reject(e,t))}),!0}catch(n){return r?!0:(reject(e,n),!0)}return!1}function resolve(e,t){e===t?fulfill(e,t):handleThenable(e,t)||fulfill(e,t)}function fulfill(e,t){e._state===PENDING&&(e._state=SEALED,e._detail=t,config.async(publishFulfillment,e))}function reject(e,t){e._state===PENDING&&(e._state=SEALED,e._detail=t,config.async(publishRejection,e))}function publishFulfillment(e){publish(e,e._state=FULFILLED)}function publishRejection(e){publish(e,e._state=REJECTED)}var config=require("./config").config,configure=require("./config").configure,objectOrFunction=require("./utils").objectOrFunction,isFunction=require("./utils").isFunction,now=require("./utils").now,all=require("./all").all,race=require("./race").race,staticResolve=require("./resolve").resolve,staticReject=require("./reject").reject,asap=require("./asap").asap,counter=0;config.async=asap;var PENDING=void 0,SEALED=0,FULFILLED=1,REJECTED=2;Promise.prototype={constructor:Promise,_state:void 0,_detail:void 0,_subscribers:void 0,then:function(e,t){var r=this,i=new this.constructor(function(){});if(this._state){var n=arguments;config.async(function(){invokeCallback(r._state,i,n[r._state-1],r._detail)})}else subscribe(this,i,e,t);return i},"catch":function(e){return this.then(null,e)}},Promise.all=all,Promise.race=race,Promise.resolve=staticResolve,Promise.reject=staticReject,exports.Promise=Promise;
},{"./all":4,"./asap":5,"./config":6,"./race":9,"./reject":10,"./resolve":11,"./utils":12}],9:[function(require,module,exports){
"use strict";function race(r){var e=this;if(!isArray(r))throw new TypeError("You must pass an array to race.");return new e(function(e,t){for(var a,n=0;n<r.length;n++)a=r[n],a&&"function"==typeof a.then?a.then(e,t):e(a)})}var isArray=require("./utils").isArray;exports.race=race;
},{"./utils":12}],10:[function(require,module,exports){
"use strict";function reject(e){var t=this;return new t(function(t,r){r(e)})}exports.reject=reject;
},{}],11:[function(require,module,exports){
"use strict";function resolve(e){if(e&&"object"==typeof e&&e.constructor===this)return e;var t=this;return new t(function(t){t(e)})}exports.resolve=resolve;
},{}],12:[function(require,module,exports){
"use strict";function objectOrFunction(t){return isFunction(t)||"object"==typeof t&&null!==t}function isFunction(t){return"function"==typeof t}function isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}var now=Date.now||function(){return(new Date).getTime()};exports.objectOrFunction=objectOrFunction,exports.isFunction=isFunction,exports.isArray=isArray,exports.now=now;
},{}],13:[function(require,module,exports){
module.exports={
  "name": "frame-channels",
  "version": "0.0.65",
  "description": "pub/sub channels between browser [i]frames",
  "main": "./src/main",
  "scripts": {
    "test": "gulp test && gulp test-browser",
    "cover": "istanbul cover --root src gulp test",
    "cover-report": "open coverage/lcov-report/index.html",
    "cover-publish": "istanbul cover _mocha --report lcovonly && codeclimate < coverage/lcov.info",
    "bump": "gulp bump"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/benjamine/frame-channels.git"
  },
  "keywords": [
    "pubsub",
    "channel",
    "frame",
    "iframe",
    "postmessage"
  ],
  "author": "Benjamin Eidelman <beneidel@gmail.com>",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/benjamine/frame-channels/issues"
  },
  "homepage": "https://github.com/benjamine/frame-channels",
  "devDependencies": {
    "brfs": "^1.2.0",
    "codeclimate-test-reporter": "0.0.3",
    "expect.js": "~0.3.1",
    "fiberglass": "~0.0.11",
    "gulp": "^3.8.8",
    "istanbul": "^0.3.2",
    "bulk-require": "^0.2.1"
  },
  "testling": {
    "harness": "mocha",
    "files": "test/index.js",
    "scripts": [
      "build/frame-channels.js"
    ],
    "browsers": [
      "ie/8..latest",
      "chrome/27..latest",
      "firefox/22..latest",
      "safari/5.1..latest",
      "opera/12..latest",
      "iphone/6..latest",
      "ipad/6..latest",
      "android-browser/4.2..latest"
    ]
  },
  "dependencies": {
    "es6-promise": "^1.0.0"
  }
}

},{}],14:[function(require,module,exports){
function ChannelIFrame(e){this.channel=e}var Promise=require("es6-promise").Promise;ChannelIFrame.prototype.ready=function(){var e=this,t=this.readyPromise;if(!t){var i=this.channel,n=i.options.iframe;t=new Promise(function(t,o){var r=!1,s=!1,l=function(e){r||s||!e.ready&&!e.pong||(r=!0,i.unsubscribe(l),t())};i.subscribe(l);var h=e.element||document.getElementById(n.id);if(h){e.element=h;try{i.push({ping:!0,respond:!0})}catch(a){console.log("failed to ping frame channel")}}else{if(h=document.createElement("iframe"),e.element=h,h.id=n.id,h.style.display="none",n.allowPositionControl&&e.subscribeToPositionMessages(),document.body.appendChild(h),n.url)h.src=n.url;else if(n.html){var m=h.contentDocument||h.contentWindow.document;m.write(n.html.toString()),m.close()}"function"==typeof n.setup&&n.setup(h)}setTimeout(function(){r||(s=!0,console.error&&console.error("timeout waiting for frame channel"),i.unsubscribe(l),o(new Error("timeout waiting for frame channel")))},n.readyTimeout||15e3)}),this.readyPromise=t}return t},ChannelIFrame.prototype.subscribeToPositionMessages=function(){var e=this;this.channel.subscribe(function(t,i){t.maximize&&e.maximize(),t.size&&e.size(t.size.width,t.size.height),t.restore&&e.restore(),t.dock&&e.dock(t.dock),t.show&&e.show(),t.hide&&e.hide(),i&&i()})},ChannelIFrame.prototype.show=function(){return this.element.style.display="",this},ChannelIFrame.prototype.hide=function(){return this.element.style.display="none",this},ChannelIFrame.prototype.size=function(e,t){if("100%"===e&&"100%"===t)return this.maximize();this.preMaximize&&this.restore();var i=this.element;return i.style.width=e,i.style.height=t,this},ChannelIFrame.prototype.dock=function(e){e=e||"bottom right";var t=e.indexOf("left")>=0,i=e.indexOf("right")>=0,n=e.indexOf("bottom")>=0,o=e.indexOf("top")>=0,r=this.element;r.style.position="fixed",r.style.left=t?0:"auto",r.style.right=i?0:"auto",r.style.bottom=n?0:"auto",r.style.top=o?0:"auto",r.style.margin=0,r.style.padding=0,r.style.border=0,r.style.zIndex=99999999;var s=this.preMaximize;return s&&(r.style.width=s.width,r.style.height=s.height,this.preMaximize=null),this},ChannelIFrame.prototype.maximize=function(){var e={},t=this.element;return t.style.position="fixed",e.left=t.style.left,e.right=t.style.right,e.bottom=t.style.bottom,e.top=t.style.top,e.width=t.style.width,e.height=t.style.height,t.style.left=0,t.style.right=0,t.style.bottom=0,t.style.top=0,t.style.width="100%",t.style.height="100%",t.contentWindow.focus(),this.preMaximize||(this.preMaximize=e),this},ChannelIFrame.prototype.restore=function(){var e=this.preMaximize;if(e){var t=this.element;for(var i in e)t.style[i]=e[i];return this.preMaximize=null,this}},module.exports=ChannelIFrame;
},{"es6-promise":3}],15:[function(require,module,exports){
function Channel(n,e){if(this.options=e||{},"string"!=typeof n||n.length<1)throw new Error("name must be a non-empty string");this.name=n,this.options=e||{},this.handlers=[],this.listening=!1,this.isReady=!1,this.options.iframe&&(this.iframe=new ChannelIFrame(this))}function waitMessageResponse(n,e,t,i,o){var r=n.findMyWindow(),s=function(t){if(t.data&&t.data.channel===n.name&&n.originIsAllowed(t.origin)&&t.data._responseTo===e){if(!s)return;r.removeEventListener("message",s),s=null,t.data&&t.data.error?o(new Error(t.data.error)):i(t.data)}};r.addEventListener("message",s,!1),setTimeout(function(){if(s){r.removeEventListener("message",s),s=null;var n=new Error("timeout waiting for cross-frame response");n.timeout=!0,o(n)}},t||n.options.responseTimeout||3e3)}function handleMessage(n,e,t){var i;e.respond&&(i=function(t){var i=t;i instanceof Error&&(i={error:i.message}),"object"!=typeof i&&(i={value:i}),i._responseTo=e._messageid,n.push(i)});try{t(e,i)}catch(o){console&&console.error&&(console.error("error on subscriber: ",o),console.error(o.stack))}}var Promise=require("es6-promise").Promise,ChannelIFrame=require("./channel-iframe"),messageCount=0;Channel.prototype.findMyWindow=function(){if(this._myWindow)return this._myWindow;var n=this.options.myWindow;return n&&"function"==typeof n.addEventListener?(this._myWindow=n,this._myWindow):(this._myWindow=window,this._myWindow)},Channel.prototype.findWindow=function(){if(this._window)return this._window;var n=this.options.target;if(!n&&this.options.iframe&&this.options.iframe.id&&(n="#"+this.options.iframe.id),"string"==typeof n){var e=document.querySelector(n);if(!e||!e.contentWindow)throw new Error("unable to find contentWindow of "+n);return this._window=e.contentWindow,this._window}if(n&&"function"==typeof n.postMessage)return this._window=n,this._window;throw new Error("no valid target was provided (eg. #iframeIdOrSelector, window.parent, someWindow)")},Channel.prototype.push=function(n){var e=n;"object"!=typeof e&&(e={value:e}),e.channel=this.name,messageCount++;var t=e._messageid=messageCount,i=this.findWindow(),o=this;return setTimeout(function(){i.postMessage(e,o.options.targetOrigin||"*")},1),new Promise(function(n,i){return e.respond?void waitMessageResponse(o,t,e.timeout,n,i):void n()})},Channel.prototype.request=function(n){var e=n;return"object"!=typeof e&&(e={value:e}),e.respond=!0,this.push(e)},Channel.prototype.originIsAllowed=function(n){var e=this.options.originFilter;return e?"function"==typeof e.test?e.test(event.origin):"string"==typeof e?n===e:!1:!0},Channel.prototype.subscribe=function(n){if("function"!=typeof n)throw new Error("handler function is required");if(this.handlers.push(n),!this.listening){var e=this,t=e.findMyWindow();t.addEventListener("message",function(n){if(n.data&&n.data.channel===e.name&&e.originIsAllowed(n.origin))for(var t=Array.prototype.slice.apply(e.handlers);t.length;)handleMessage(e,n.data,t.shift())},!1),this.listening=!0,this.respondToPings(),this.options.autoReady!==!1&&this.notifyReady()}return this},Channel.prototype.respondToPings=function(){this.respondingToPings||this.subscribe(function(n,e){n.ping&&e&&e({pong:!0})})},Channel.prototype.notifyReady=function(){this.respondToPings();try{this.push({ready:!0})}catch(n){}},Channel.prototype.unsubscribe=function(n){for(var e=this.handlers.length-1;e>=0;e--)this.handlers[e]===n&&this.handlers.splice(e,1);return this},module.exports=Channel;
},{"./channel-iframe":14,"es6-promise":3}]},{},[1])(1)
});


//# sourceMappingURL=frame-channels.min.map