forked from ReDEnergy/SessionSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfirm-box.js
More file actions
113 lines (85 loc) · 2.87 KB
/
Copy pathconfirm-box.js
File metadata and controls
113 lines (85 loc) · 2.87 KB
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
define(function(require, exports) {
'use strict';
// *****************************************************************************
// Custom Modules
const { HTMLCreator } = require('./dom');
const { WindowEvents, GlobalEvents } = require('./global-events');
var ID = 0;
// *****************************************************************************
// Bookmark Confirm Box
function ConfirmBox(document, options)
{
var DomElem = HTMLCreator(document);
var panel = DomElem('div', {class : 'confirm-box'});
var info = DomElem('div', {class : 'info'});
var controls = DomElem('div', {class : 'controls'});
var ok = DomElem('div', {class : 'button ok'});
ok.textContent = 'Confirm';
var cancel = DomElem('div', {class : 'button cancel'});
cancel.textContent = 'Cancel';
var flexArea1 = DomElem('div', {class : 'flex'});
var flexArea2 = DomElem('div', {class : 'flex'});
controls.appendChild(flexArea1);
controls.appendChild(ok);
controls.appendChild(cancel);
controls.appendChild(flexArea2);
panel.appendChild(info);
panel.appendChild(controls);
var contextID = ID++;
var callback;
var defaultCallback = function defaultCallback() {};
// ------------------------------------------------------------------------
// Events
ok.addEventListener('click', function() {
callback();
hidePanel();
});
cancel.addEventListener('click', function() {
hidePanel();
});
var openPanel = function openPanel(options)
{
panel.setAttribute('data-active', 'true');
panel.style.left = options.event.pageX + 'px';
panel.style.top = options.event.pageY + 'px';
document.addEventListener('mousedown', blurMenu);
info.textContent = options.message;
setCallback(options.callback);
GlobalEvents.emit('ConfimBoxOpen', contextID);
};
var hidePanel = function hidePanel()
{
panel.removeAttribute('data-active');
document.removeEventListener('mousedown', blurMenu);
callback = defaultCallback;
};
var hideIfNotInvoked = function hideIfNotInvoked(ctxID)
{
if (contextID !== ctxID) hidePanel();
};
var blurMenu = function hideWhenClickOutside(e)
{
var target = e.target;
while (target) {
if (target === panel)
return;
target = target.parentElement;
}
hidePanel();
};
function setCallback(func) {
callback = typeof func === 'function' ? func : defaultCallback;
}
// probably used for closing other opened confirm boxes ?
GlobalEvents.on('ConfimBoxOpen', hideIfNotInvoked);
// Register custom events
WindowEvents.on(document, options.name + '-Open', openPanel);
WindowEvents.on(document, 'ConfimBox-Hide', hidePanel);
// ------------------------------------------------------------------------
// Public properties
this.DOMRoot = panel;
}
// *****************************************************************************
// Public API
exports.ConfirmBox = ConfirmBox;
});