-
Notifications
You must be signed in to change notification settings - Fork 0
/
showModalDialogMultiStrict.js
128 lines (114 loc) · 5.71 KB
/
showModalDialogMultiStrict.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
'use strict';
(function() {
// All references to document object will be on top window
var dialogDocument = window.top.document;
window.spawn = window.spawn || function(gen) {
function continuer(verb, arg) {
var result;
try {
result = generator[verb](arg);
} catch (err) {
return Promise.reject(err);
}
if (result.done) {
return result.value;
} else {
return Promise.resolve(result.value).then(onFulfilled, onRejected);
}
}
var generator = gen();
var onFulfilled = continuer.bind(continuer, 'next');
var onRejected = continuer.bind(continuer, 'throw');
return onFulfilled();
};
// Override window.close function to close the last dialog created,
// if window.shoModalDialog function doesn't exist
window.close = window.showModalDialog ? window.close : function(close) {
return function() {
var dialogTags = dialogDocument.getElementsByTagName('dialog');
var dialog = dialogTags[dialogTags.length - 1];
if (dialog)
dialog.close();
else
return close.call();
};
}(window.close);
// Override window.showModalDialog function if doesn't exist
window.showModalDialog = window.showModalDialog || function (url, arg, opt) {
url = url || ''; //URL of a dialog
arg = arg || null; //arguments to a dialog
opt = opt || 'dialogWidth:300px;dialogHeight:200px'; //options: dialogTop;dialogLeft;dialogWidth;dialogHeight or CSS styles
var dialogTitle = 'dialog-title';
var dialog = dialogDocument.body.appendChild(dialogDocument.createElement('dialog'));
var lastDialog = dialogDocument.querySelectorAll('dialog')[dialogDocument.querySelectorAll('dialog').length - 1];
dialog.setAttribute('style', opt.replace(/dialog/gi, ''));
dialog.innerHTML = '<div id=' + dialogTitle + '>';
dialog.innerHTML += '<span id="dialog-close"><a href="#">×</a></span>';
dialog.innerHTML += '</div><iframe id="dialog-body" src="' + url + '"></iframe>';
lastDialog.querySelector('#dialog-body').contentWindow.dialogArguments = arg;
lastDialog.querySelector('#dialog-close').addEventListener('click', function (e) {
e.preventDefault();
dialog.close();
});
// --------------------------------------------------------------
// Create the css part for the dialog
if (!dialogDocument.getElementById('dialog-css')) {
var css = '<!--' +
'dialog {padding: calc(1.8em + 1px) 0 0 0;box-shadow: 0px 0px 25px 2px #aaa;border: 1px solid;position: fixed !important;}' +
'dialog::backdrop {background: rgba(0, 0, 0, .2)}' +
'#' + dialogTitle + ' {width: 100%;background-color: #395484;position: absolute;left: 0;top: 0;height: calc(1.8em + 1px);}' +
'#dialog-close {-webkit-transition: .2s ease-in-out;-moz-transition: .2s ease-in-out;-o-transition: .2s ease-in-out;transition: .2s ease-in-out;' +
'position: absolute; top: 1px; right: 1px; width: 1.8em; height: calc(1.8em - 1px);text-align: center;}' +
'#dialog-close:hover {background-color: #D00;} -->' +
'#dialog-close a {font-size: 20px; color: #FFF; text-decoration: none; outline: none;}' +
'#dialog-body {border: 0; width: 100%; height: 100%;}' +
'-->';
var head = dialogDocument.head || dialogDocument.getElementsByTagName('head')[0];
var style = dialogDocument.createElement('style');
style.type = 'text/css';
style.id = 'dialog-css';
style.appendChild(dialogDocument.createTextNode(css));
head.appendChild(style);
}
// --------------------------------------------------------------
// Make the dialog draggable
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
dialog.querySelector('#' + dialogTitle).onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup
pos3 = e.clientX;
pos4 = e.clientY;
dialogDocument.onmouseup = closeDragElement;
// call a function whenever the cursor moves
dialogDocument.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position
dialog.style.top = (dialog.offsetTop - pos2) + "px";
dialog.style.marginLeft = (dialog.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released
dialogDocument.onmouseup = null;
dialogDocument.onmousemove = null;
}
dialog.showModal();
//if using yield or async/await
return new Promise(function (resolve, reject) {
dialog.addEventListener('close', function () {
var returnValue = lastDialog.querySelector('#dialog-body').contentWindow.returnValue;
dialogDocument.body.removeChild(dialog);
resolve(returnValue);
});
});
};
})();