-
Notifications
You must be signed in to change notification settings - Fork 38
/
dialog.js
130 lines (108 loc) · 3.52 KB
/
dialog.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
if (process.browser) {
module.exports = function Dialog() {
// Nothing happens
}
}
else {
const electron = require('electron');
const { getTemplate } = require('../../helper');
const path = require('path');
const photonPath = path.normalize(__dirname + "/../../");
function getBaseUrl(url) {
var re = new RegExp(/^.*\//);
return re.exec(url);
}
module.exports = async function Dialog(e, options = {}) {
var templateScript = e;
if (typeof e === "string") {
templateScript = document.querySelector(e);
}
const boundings = {
width: 500,
height: 350
};
const template = await getTemplate(templateScript);
const mainWindow = electron.remote.getCurrentWindow();
const photonWin = document.querySelector("ph-window");
var dialog = templateScript.__dialogWindow;
if (!dialog) {
dialog = new electron.remote.BrowserWindow(Object.assign({
parent: mainWindow,
//frame: false,
//hasShadow: false,
//transparent: true,
show: false,
//width: boundings.width,
//height: boundings.height,
//resizable: false,
//focusable: false,
//alwaysOnTop: true,
vibrancy: "popover",
modal: true,
//'use-content-size': true,
webPreferences: {
experimentalFeatures: true,
nodeIntegration: true,
enableRemoteModule: true
}
}, options));
const sheetOffset = photonWin.content.getBoundingClientRect().top;
dialog.setSheetOffset(sheetOffset);
dialog.once("ready-to-show", function() {
dialog.show();
setModalPos(dialog, mainWindow, {
x: 0,
y: sheetOffset
});
});
const resizeCallback = function (event) {
dialog.setSheetOffset(sheetOffset)
setModalPos(dialog, mainWindow, {
x: 0,
y: sheetOffset
})
}
dialog.on('close', function () {
mainWindow.off("resize", resizeCallback)
})
mainWindow.on("resize", resizeCallback)
dialog.loadURL("file://" + __dirname + "/templateModal.html");
const jsScript = getBaseUrl(mainWindow.getURL()) + templateScript.getAttribute("data-js") || "";
dialog.webContents.on("did-finish-load", function() {
dialog.webContents.executeJavaScript('document.querySelector(".modal-window").innerHTML = `' + template.replace(/`/g, "\\`") + '`;\n');
if (templateScript.getAttribute("data-js")) {
dialog.webContents.executeJavaScript('var script = document.createElement("script"); script.async = true; script.type = "text/javascript"; script.src = "' + jsScript + '"; document.head.append(script);');
}
});
//dialog.webContents.openDevTools();
window.dialog = dialog;
}
}
function setModalPos(modal, main, offset = {}) {
offset = {
x: Math.round(main.getPosition()[0] + main.getSize()[0] / 2 - modal.getSize()[0] / 2),
y: Math.round(main.getPosition()[1] + offset.y)
};
modal.setPosition(offset.x, offset.y);
}
function showModal(modal) {
const size = modal.getSize();
var start;
var end = 500;
function step(timestamp) {
if (!start) {
start = timestamp;
}
var progress = timestamp - start;
if (progress < end) {
requestAnimationFrame(step);
}
else {
progress = end;
}
var height = Math.round(progress / end * size[1]);
modal.setSize(size[0], height);
}
requestAnimationFrame(step);
}
}