This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindowCreator.js
133 lines (126 loc) · 3.98 KB
/
windowCreator.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
131
132
133
const {BrowserWindow} = require("electron");
const {noSpace} = require("./transformName.js");
const {translateBattFile} = require("./battToJs.js");
const fs = require('fs');
function writeGeneratedScriptFile(folder,title,Iscripts,Itext,callback){
var text=Itext;
var folderR=folder.replace(/\\/g,"\\\\");
var dots=null;
var scripts=null;
if(typeof Iscripts =="string"){
scripts=[];
scripts.push(Iscripts);
} else {
scripts=Iscripts;
}
for(var i=0;i<scripts.length;i++){
dots=scripts[i].split(".");
if(dots[dots.length-1]==="batt"){
translateBattFile(folderR+"\\\\"+scripts[i].replace(/\//g,"\\\\"));
text+="require('"+folderR+"\\\\"+scripts[i].replace(/\//g,"\\\\").replace(".batt","-translated.js")+"').init(globalProject);\n";
} else if(dots[dots.length-1]==="js"){
text+="require('"+folderR+"\\\\"+scripts[i].replace(/\//g,"\\\\")+"').init(globalProject);\n";
}
}
text+="}\n}";
fs.writeFile(folder+"\\"+noSpace(title)+"-script.js",text, function(err) {
if(err) {
return console.log(err);
}
callback();
});
}
function generateScriptFile(data,callback){
if(data.window.title !== undefined&&data.scripts !== undefined&&data.window.folder !== undefined){
var text="document.onreadystatechange = function () {\nif (document.readyState == '"+data.runat+"') {\n";
text+="var globalProject=require('"+__dirname.replace(/\\/g,"\\\\")+"\\\\Projects\\\\global.js');\n";
var iCallback=callback;
if(data.popup !== undefined){
iCallback=function(){};
}
writeGeneratedScriptFile(data.window.folder,data.window.title,data.scripts,text,iCallback);
if(data.popup !== undefined){
writeGeneratedScriptFile(data.window.folder,data.window.title+"-popups",data.popup,text,callback);
}
}
}
function readConfig(folderPath,callback,creation=true){
fs.readFile(folderPath+"\\config.json", 'utf8', function (err, data) {
if (err) throw err;
var obj=JSON.parse(data);
if(creation){
obj.window.folder = folderPath;
if(!("battStorage" in obj)){
obj.battStorage={};
}
obj.window.webPreferences.preload=folderPath+"\\"+noSpace(obj.window.title)+"-script.js";
}
return callback(obj);
});
}
function ValidURL(str) {
//https://stackoverflow.com/a/30229098
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
if(!regex .test(str)) {
return false;
} else {
return true;
}
}
function createWindow(data){
let window = new BrowserWindow(data.window);
window.getConfig=data;
if(data.url!=undefined){
if(ValidURL(data.url)){
window.loadURL(data.url);
} else {
window.loadURL(data.window.folder+"\\"+data.url);
}
}
window.on('closed', () => {
var configData=window.getConfig;
fs.writeFile(configData.window.folder+"\\config.json",JSON.stringify(configData),function(){
window=null;
});
})
if(data.popup!=undefined){
var showPopup=true;
if(data.popupshow!=undefined && data.popupshow==false){
showPopup=false;
}
window.webContents.on('new-window', (event, url) => {
event.preventDefault()
const defaultWin=new BrowserWindow(
{
width: 800,
height: 600,
show:showPopup,
webPreferences:{
preload:data.window.folder+"\\"+noSpace(data.window.title)+"-popups-script.js",
"nodeIntegration":false,
"webSecurity":false,
"allowRunningInsecureContent":true
}
});
defaultWin.loadURL(url);
event.newGuest=defaultWin;
})
}
if(data.main!=undefined){
require(data.window.folder+"\\"+data.main).init(window);
}
return window;
}
exports.readConfig=function(path,callback){
readConfig(path,callback);
}
exports.create=function(project,callback=null){
readConfig(project,function(data){
generateScriptFile(data,function(){
var window=createWindow(data);
if(callback!=null){
callback(window);
}
});
});
}