diff --git a/src/promise-window.js b/src/promise-window.js index c47ef60..d763f79 100644 --- a/src/promise-window.js +++ b/src/promise-window.js @@ -4,11 +4,11 @@ * @ignore */ (function() { - 'use strict'; + "use strict"; var root = window, - prototype, - html = root.document.documentElement; + prototype, + html = root.document.documentElement; /** * Merge the contents of two or more objects together into the first object. @@ -23,7 +23,9 @@ * @ignore */ function extend() { - var extended = arguments[0], key, i; + var extended = arguments[0], + key, + i; for (i = 1; i < arguments.length; i++) { for (key in arguments[i]) { if (arguments[i].hasOwnProperty(key)) { @@ -42,7 +44,9 @@ * @ignore */ function generateUniqueString(prefix) { - return prefix + new Date().getTime() + "-" + Math.floor(10e12 * Math.random()); + return ( + prefix + new Date().getTime() + "-" + Math.floor(10e12 * Math.random()) + ); } /** @@ -92,7 +96,8 @@ function PromiseWindow(uri, config) { this.uri = uri; this.config = extend({}, this.constructor.defaultConfig, config); - this.config.windowName = this.config.windowName || generateUniqueString('promise-window-'); + this.config.windowName = + this.config.windowName || generateUniqueString("promise-window-"); this._onPostMessage = this._onPostMessage.bind(this); } @@ -109,7 +114,9 @@ * @return {Function} Returns a promise provider * @static */ - PromiseWindow.getAPlusPromiseProvider = function getAPlusPromiseProvider(CustomPromise) { + PromiseWindow.getAPlusPromiseProvider = function getAPlusPromiseProvider( + CustomPromise + ) { return function promiseProvider() { var module = {}; module.promise = new CustomPromise(function(resolve, reject) { @@ -164,20 +171,23 @@ onClose: function() { this._window.close(); }, - originRegexp: new RegExp('^[^:/?]+://[^/]*') + originRegexp: new RegExp("^[^:/?]+://[^/]*") }; // Configure default Promise provider from current invironment if (root.Promise) { - PromiseWindow.defaultConfig.promiseProvider = PromiseWindow.getAPlusPromiseProvider(root.Promise); - } - else if (root.RSVP) { - PromiseWindow.defaultConfig.promiseProvider = PromiseWindow.getAPlusPromiseProvider(root.RSVP.Promise); - } - else if (root.Q) { - PromiseWindow.defaultConfig.promiseProvider = PromiseWindow.getAPlusPromiseProvider(root.Q.Promise); - } - else if (root.jQuery) { + PromiseWindow.defaultConfig.promiseProvider = PromiseWindow.getAPlusPromiseProvider( + root.Promise + ); + } else if (root.RSVP) { + PromiseWindow.defaultConfig.promiseProvider = PromiseWindow.getAPlusPromiseProvider( + root.RSVP.Promise + ); + } else if (root.Q) { + PromiseWindow.defaultConfig.promiseProvider = PromiseWindow.getAPlusPromiseProvider( + root.Q.Promise + ); + } else if (root.jQuery) { PromiseWindow.defaultConfig.promiseProvider = function promiseProvider() { var deferred = root.jQuery.Deferred(); return { @@ -186,10 +196,9 @@ reject: deferred.reject }; }; - } - else { + } else { PromiseWindow.defaultConfig.promiseProvider = function() { - throw new Error('Missing promiseProvider in PromiseWindow configuration'); + throw new Error("Missing promiseProvider in PromiseWindow configuration"); }; } @@ -213,11 +222,14 @@ * @return {string} The converted value * @protected */ - prototype._serializeFeatureValue = function _serializeFeatureValue(key, value) { + prototype._serializeFeatureValue = function _serializeFeatureValue( + key, + value + ) { if (this._isBoolean(value)) { - return value ? 'yes' : 'no'; + return value ? "yes" : "no"; } - return '' + value; + return "" + value; }; /** @@ -232,15 +244,20 @@ * @return {Number} position.height The height of the centered rectangle * @protected */ - prototype._getCenteredPosition = function _getCenteredPosition(width, height) { - var dualScreenLeft = root.screenLeft !== undefined ? root.screenLeft : screen.left, - dualScreenTop = root.screenTop !== undefined ? root.screenTop : screen.top, - w = root.innerWidth || html.clientWidth || screen.width, - h = root.innerHeight || html.clientHeight || screen.height; + prototype._getCenteredPosition = function _getCenteredPosition( + width, + height + ) { + var dualScreenLeft = + root.screenLeft !== undefined ? root.screenLeft : screen.left, + dualScreenTop = + root.screenTop !== undefined ? root.screenTop : screen.top, + w = root.innerWidth || html.clientWidth || screen.width, + h = root.innerHeight || html.clientHeight || screen.height; return { - left: (w / 2) - (width / 2) + dualScreenLeft, - top: (h / 2) - (height / 2) + dualScreenTop, + left: w / 2 - width / 2 + dualScreenLeft, + top: h / 2 - height / 2 + dualScreenTop, width: width, height: height }; @@ -252,7 +269,10 @@ * @protected */ prototype._getFeatures = function _getFeatures() { - var config = this._getCenteredPosition(this.config.width, this.config.height); + var config = this._getCenteredPosition( + this.config.width, + this.config.height + ); for (var key in this.config.window) { if (this.config.window.hasOwnProperty(key)) { config[key] = this.config.window[key]; @@ -260,8 +280,12 @@ } return Object.keys(config) - .map(function(key) { return key + '=' + this._serializeFeatureValue(key, config[key]); }.bind(this)) - .join(','); + .map( + function(key) { + return key + "=" + this._serializeFeatureValue(key, config[key]); + }.bind(this) + ) + .join(","); }; /** @@ -295,13 +319,16 @@ */ prototype._startWatcher = function _startWatcher() { if (this._watcherRunning) { - throw new Error('Watcher is already started'); + throw new Error("Watcher is already started"); } - this._watcher = root.setInterval(function () { - if (this._watcherRunning && !this._isWindowAlive()) { - this.close(); - } - }.bind(this), this.config.watcherDelay); + this._watcher = root.setInterval( + function() { + if (this._watcherRunning && !this._isWindowAlive()) { + this.close(); + } + }.bind(this), + this.config.watcherDelay + ); this._watcherRunning = true; }; @@ -312,7 +339,7 @@ */ prototype._stopWatcher = function _stopWatcher() { if (!this._watcherRunning) { - throw new Error('Watcher is already stopped'); + throw new Error("Watcher is already stopped"); } this._watcherRunning = false; root.clearInterval(this._watcher); @@ -329,7 +356,8 @@ */ prototype._onPostMessage = function _onPostMessage(event) { var expectedOriginMatches = this.config.originRegexp.exec(this.uri); - var expectedOrigin = expectedOriginMatches && expectedOriginMatches[0] || location.origin; + var expectedOrigin = + (expectedOriginMatches && expectedOriginMatches[0]) || location.origin; if (this._window === event.source && event.origin === expectedOrigin) { this.config.onPostMessage.call(this, event); } @@ -338,12 +366,11 @@ /** * Changes the URI * @param {String} uri The new URI - * @throws {Error} If the window is open * @return {PromiseWindow} Returns this object to allow chaining */ prototype.setURI = function setURI(uri) { if (this.isOpen()) { - throw new Error('Cannot change the URI while the window is open'); + this._window.location.href = uri; } this.uri = uri; return this; @@ -365,7 +392,7 @@ */ prototype.open = function open() { if (this.isOpen()) { - throw new Error('Window is already open'); + throw new Error("Window is already open"); } this._windowOpen = true; @@ -377,8 +404,7 @@ ); if (!this._window) { this._reject("blocked"); - } - else { + } else { root.addEventListener("message", this._onPostMessage, true); this._startWatcher(); } @@ -392,7 +418,7 @@ */ prototype.close = function close() { if (!this.isOpen()) { - throw new Error('Window is already closed'); + throw new Error("Window is already closed"); } this._stopWatcher(); root.removeEventListener("message", this._onPostMessage); @@ -414,13 +440,14 @@ // Exports PromiseWindow to the global scope /* jshint ignore:start */ - if (typeof define === 'function' && define.amd) { - define('promise-window', [], function() { return PromiseWindow }); - } else if (typeof exports === 'object') { + if (typeof define === "function" && define.amd) { + define("promise-window", [], function() { + return PromiseWindow; + }); + } else if (typeof exports === "object") { module.exports = PromiseWindow; } else { root.PromiseWindow = PromiseWindow; } /* jshint ignore:end */ - })();