popup-centered
Version:
The little function for open window popup on center display
104 lines (90 loc) • 3.06 kB
JavaScript
/*!
* popup-centered v1.2.0
* (c) Nikita Nafranets <eddimensi@gmail.com>
* Released under the MIT License.
*/
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
var StringBoolean = function StringBoolean(bool) {
return bool ? 'yes' : 'no';
};
function createOptions(settings) {
var result = {};
Object.defineProperty(result, 'toString', {
value: function value() {
var _this = this;
return Object.keys(this).map(function (key) {
return key + "=" + _this[key];
}).join(',');
},
writable: false,
enumerable: false
});
return Object.keys(settings).reduce(function (acc, key) {
if (typeof settings[key] === 'boolean') {
result[key] = StringBoolean(settings[key]);
} else {
result[key] = settings[key];
}
return result;
}, result);
}
var getScreenPosition = function getScreenPosition() {
var screenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screenX;
var screenTop = window.screenTop !== undefined ? window.screenTop : window.screenY;
return {
screenLeft: screenLeft,
screenTop: screenTop
};
};
var getSizeWindow = function getSizeWindow() {
var windowWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
return {
windowWidth: windowWidth,
windowHeight: windowHeight
};
};
var calcPosition = function calcPosition(windowSize, popupSize, offset) {
return windowSize / 2 - popupSize / 2 + offset;
};
function popupCentered(url, title, widthOrOptions, height) {
var _a = getScreenPosition(),
screenLeft = _a.screenLeft,
screenTop = _a.screenTop;
var _b = getSizeWindow(),
windowWidth = _b.windowWidth,
windowHeight = _b.windowHeight;
var options = null;
if (typeof widthOrOptions === "number" && typeof height === "number") {
options = createOptions({
width: widthOrOptions,
height: height,
left: calcPosition(windowWidth, widthOrOptions, screenLeft),
top: calcPosition(windowHeight, height, screenTop),
scrollbars: true
});
}
if (_typeof(widthOrOptions) === "object") {
options = createOptions(Object.assign({}, {
left: calcPosition(windowWidth, widthOrOptions.width, screenLeft),
top: calcPosition(windowHeight, widthOrOptions.height, screenTop)
}, widthOrOptions));
}
var newWindow = window.open(url, title, options.toString());
if (window.focus && newWindow) {
newWindow.focus();
}
return newWindow;
}
export default popupCentered;