-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
144 lines (116 loc) · 3.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Simple wrapper component around `window.open()`.
*
* Usage:
*
* var Popup = require('popup');
* var win = new Popup('http://google.com', { width: 100, height: 100 });
* win.on('close', function () {
* console.log('popup window was closed');
* });
*/
/**
* Module derencencies.
*/
var Emitter = require('emitter')
, inherit = require('inherit');
/**
* Module exports.
*/
exports = module.exports = Popup;
/**
* Default Popup options.
*/
var defaults = {
width: 700
, height: 520
, menubar: 'no'
, resizable: 'yes'
, location: 'yes'
, scrollbars: 'no'
, centered: true
};
/**
* The "Popup" constructor.
*/
function Popup (src, opts) {
if (!(this instanceof Popup)) {
return new Popup(src, opts);
}
// ensure an opts object exists
opts = opts || {};
// set the defaults if not provided
for (var i in defaults) {
if (!(i in opts)) {
opts[i] = defaults[i];
}
}
// we try to place it at the center of the current window
// note: this "centering" logic borrowed from the Facebook JavaScript SDK
if (opts.centered) {
var screenX = null == window.screenX ? window.screenLeft : window.screenX;
var screenY = null == window.screenY ? window.screenTop : window.screenY;
var outerWidth = null == window.outerWidth
? document.documentElement.clientWidth : window.outerWidth;
var outerHeight = null == window.outerHeight
// 22= IE toolbar height
? (document.documentElement.clientHeight - 22) : window.outerHeight;
if (null == opts.left)
opts.left = parseInt(screenX + ((outerWidth - opts.width) / 2), 10);
if (null == opts.top)
opts.top = parseInt(screenY + ((outerHeight - opts.height) / 2.5), 10);
delete opts.centered;
}
// interval to check for the window being closed
var interval = 500;
if (opts.interval) {
interval = +opts.interval;
delete opts.interval;
}
// turn the "opts" object into a window.open()-compatible String
var optsStr = [];
for (var key in opts) {
optsStr.push(key + '=' + opts[key]);
}
optsStr = optsStr.join(',');
// every popup window has a unique "name"
var name = opts.name;
// if a "name" was not provided, then create a random one
if (!name) name = 'popup-' + (Math.random() * 0x10000000 | 0).toString(36);
Emitter.call(this);
this.name = name;
this.opts = opts;
this.optsStr = optsStr;
// finally, open and return the popup window
this.window = window.open(src, name, optsStr);
this.focus();
this.interval = setInterval(checkClose(this), interval);
}
// inherit from Emitter
inherit(Popup, Emitter);
/**
* Closes the popup window.
*/
Popup.prototype.close = function () {
this.window.close();
}
/**
* Focuses the popup window (brings to front).
*/
Popup.prototype.focus = function () {
this.window.focus();
}
/**
* Emits the "close" event.
*/
Popup.prototype._checkClose = function () {
if (this.window.closed) {
this.emit('close');
clearInterval(this.interval);
}
}
function checkClose (popup) {
return function () {
popup._checkClose();
}
}