-
Notifications
You must be signed in to change notification settings - Fork 0
/
popupOpener.js
88 lines (74 loc) · 1.93 KB
/
popupOpener.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
;(function($, win) {
$.fn.popupOpener = function() {
var defaults = {
url: '',
name: '',
specs: {
width: 680,
height: 430,
resizable: 'no',
status: 'no',
location: 'no',
menubar: 'no',
toolbar: 'no'
}
};
String.prototype.toDash = function(){
return this.replace(/([A-Z])/g, function($1) {
return '-' + $1.toLowerCase();
});
};
var serialize = function(obj) {
var str = [];
var pushData;
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
pushData = encodeURIComponent(p).toDash();
pushData += obj[p] ? '=' + encodeURIComponent(obj[p]) : '';
str.push(pushData);
}
}
return str.join(',');
};
var getRandomKey = function() {
return Math.random().toString(36).substr(2, 5);
};
popups = {};
return this.each(function() {
$(this).on('click', function(e) {
e.preventDefault();
var screenSize = win.screen;
var screenW = screenSize.width;
var screenH = screenSize.height;
var data = $(this).data();
var topPos = (screenH - (data.winheight || 0)) / 2;
var leftPos = (screenW - (data.winwidth || 0 )) / 2;
var randomId = ['popup_', getRandomKey()].join('');
var options = {
url: data.winurl,
name: data.winname || randomId,
specs: {
width: data.winwidth,
height: data.winheight,
resizable: data.winresizable,
status: data.winstatus,
location: data.winlocation,
menubar: data.winmenubar,
toolbar: data.wintoolbar,
top: topPos,
left: leftPos
}
};
options = $.extend(true, {}, defaults, options);
var urlWithParams = [options.url, '?id=', randomId].join('');
popups[randomId] = {};
popups[randomId].isOpened = false;
popups[randomId].size = {
width: options.specs.width,
height: options.specs.height
};
win.open(urlWithParams, options.name, serialize(options.specs));
});
});
};
})(jQuery, window);