-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
95 lines (79 loc) · 2.88 KB
/
popup.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
var popup = (function(configModule) {
var dce = function(str) { return document.createElement(str); };
var cfg = configModule.popupConfirmBox;
var popupBoxTemplate = dce('li');
popupBoxTemplate.innerHTML = cfg.innerHTML;
var PopupConfirmBoxList = function(listElement) {
this.listElement = listElement;
this.list = [];
};
PopupConfirmBoxList.prototype.getListElement = function() {
return this.listElement;
};
PopupConfirmBoxList.prototype.append = function(event) {
var box = new PopupConfirmBox(this, event);
this.list.push(box);
this.listElement.appendChild(box.getBoxElement());
};
PopupConfirmBoxList.prototype.removeBox = function(box) {
for (var i = 0; i < this.list.length; ++i) {
if (this.list[i] == box) {
this.listElement.removeChild(box.getBoxElement());
this.list.splice(i, 1);
break;
}
}
};
var PopupConfirmBox = function(popupList, event) {
this.popupList = popupList;
this.event = event;
this.url = event.targetUrl;
this.boxElement = popupBoxTemplate.cloneNode(true);
this.initBoxElement();
};
PopupConfirmBox.prototype.initBoxElement = function() {
var urlSpan = this.boxElement.querySelector('.' + cfg.urlSpanClass);
var acceptLink = this.boxElement.querySelector('.' + cfg.acceptLinkClass);
var denyLink = this.boxElement.querySelector('.' + cfg.denyLinkClass);
urlSpan.innerText = this.url;
(function(box) {
acceptLink.addEventListener('click', function(e) { box.doAccept(); });
denyLink.addEventListener('click', function(e) { box.doDeny(); });
}(this));
};
PopupConfirmBox.prototype.getBoxElement = function() {
return this.boxElement;
};
PopupConfirmBox.prototype.doAccept = function() {
(function(box) {
// TODO: Inspect box.event for window.open()'s name and attributes to
// correctly manage:
// 1. Multiple popups in the same window (i.e., window.open() twice with
// the same window name)
// 2. Set attributes of popup window (e.g., size, location)
chrome.app.window.create(
'browser.html',
function(newWindow) {
// Pass new window event through global: window.newWindowEvent.
// NOTE: Webview creation and box.event.window.attach(webview)
// cannot be performed in this context; that has to happen in the
// context of the new window.
newWindow.contentWindow.newWindowEvent = box.event;
});
}(this));
this.popupList.removeBox(this);
this.detach();
};
PopupConfirmBox.prototype.doDeny = function() {
this.event.window.discard();
this.popupList.removeBox(this);
this.detach();
};
PopupConfirmBox.prototype.detach = function() {
this.popupList = null;
};
return {
'PopupConfirmBoxList': PopupConfirmBoxList,
'PopupConfirmBox': PopupConfirmBox
};
}(config));