Skip to content

Commit

Permalink
Setting URI when window is open
Browse files Browse the repository at this point in the history
  • Loading branch information
shrey committed Jan 10, 2022
1 parent 7cf7ee1 commit f2d492e
Showing 1 changed file with 78 additions and 51 deletions.
129 changes: 78 additions & 51 deletions src/promise-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)) {
Expand All @@ -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())
);
}

/**
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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");
};
}

Expand All @@ -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;
};

/**
Expand All @@ -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
};
Expand All @@ -252,16 +269,23 @@
* @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];
}
}

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(",");
};

/**
Expand Down Expand Up @@ -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;
};

Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -377,8 +404,7 @@
);
if (!this._window) {
this._reject("blocked");
}
else {
} else {
root.addEventListener("message", this._onPostMessage, true);
this._startWatcher();
}
Expand All @@ -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);
Expand All @@ -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 */

})();

0 comments on commit f2d492e

Please sign in to comment.