-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
261 lines (225 loc) · 7.99 KB
/
renderer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
'use strict'
//requiring path and fs modules
const path = require('path')
const fs = require('fs')
const processedFolder = '';
//passsing directoryPath and callback function
function readGivenDirectory(directoryPath) {
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
document.getElementById("dataTable").style.display = "block";
document.getElementById("display-files").innerHTML = '';
document.getElementById("displaySelected").innerHTML = '';
//listing all files using forEach
for (var i = 0; i < files.length; i++) {
var file = files[i];
var fileName = isImageFile(file);
var imgSrc = path.join(directoryPath, file);
if (fileName && isAlignedFile(fileName)) {
document.getElementById("display-files").innerHTML += '<li class"thumb">' +
'<div style="margin-bottom: 30px;""><div class="displayFullImg"' +
'onClick=updateMainContent("' + encodeURIComponent(directoryPath) + '","' + encodeURIComponent(file) + '") id="imgThumb_' + fileName + '">' +
'<img id="img_' + fileName + '" src="' + imgSrc + '" width="120" height="150" border="3"></div>' +
'<div>' + file + '</div></div></li>';
}
}
});
createSubdirectory(directoryPath);
}
function isImageFile(file) {
var fileArr = file.split('.');
if (fileArr[1] == 'jpeg' || fileArr[1] == 'jpg' || fileArr[1] == 'png')
return fileArr[0];
else
return false;
}
function updateMainContent(directoryPath, fileName) {
var decodePath = decodeURIComponent(directoryPath);
var decodeFileName = decodeURIComponent(fileName);
updateMainImage(decodePath, decodeFileName);
var jsonFile = getJsonFileName(decodePath, decodeFileName);
displayJsonData(jsonFile);
updateHiddenInput(decodePath, decodeFileName, jsonFile)
}
function getJsonFileName(directoryPath, id) {
var jsonFilePath = path.join(directoryPath, isImageFile(id) + '_QC.json');
if (fs.existsSync(jsonFilePath)) {
} else {
jsonFilePath = path.join(directoryPath, isImageFile(id) + '.json');
}
return jsonFilePath;
}
function updateMainImage(directoryPath, id) {
document.getElementById("displaySelected").innerHTML = '<img src="' + directoryPath + '/' + id + '" width="100%" height="100%" border="2"/>'
}
function displayJsonData(jsonFileName) {
var jsonDataAll = getFileData(jsonFileName);
if (jsonDataAll) {
var jsonData = jsonDataAll.data;
var isQC = jsonDataAll.qcComplete;
}
document.getElementById("submitButton").style.display = "block";
document.getElementById("displayJson").innerHTML = '';
var QCState = "Incomplete";
if (isQC == "QCComplete-UploadPending") {
QCState = "QCComplete-UploadPending";
} else if (isQC == "Completed") {
QCState = "Completed";
}
updateQCInfo(QCState);
for (var i = 0; i < jsonData.length; i++) {
var table = jsonData[i];
var tableKey = table.Key;
var tableValue = table.value;
if (tableKey == "formType") {
document.getElementById("displayJson").innerHTML += '<div class="setMargin"></label><input id="' + tableKey + '" type="hidden" size="40" value="' + tableValue + '"></div>';
} else {
document.getElementById("displayJson").innerHTML += '<div class="setMargin"><label class="inputLabel"><b>' + tableKey + ': </b> </label><input id="' + tableKey + '" type="textbox" size="40" value="' + tableValue + '"></div>';
}
}
}
function updateQCInfo(QCState) {
document.getElementById("QCdivShow").style.display = "block";
document.getElementById("QCinfo").innerHTML = QCState;
}
function updateHiddenInput(directoryPath, id, jsonFile) {
document.getElementById("imageFilePath").value = path.join(directoryPath, processedFolder, id);
document.getElementById("JsonFilePath").value = path.join(directoryPath, processedFolder, isImageFile(id) + '_QC.json');
}
function getFileData(fileName) {
try {
if (fs.existsSync(fileName)) {
}
else {
var dirPathForjson = path.join(__dirname, 'data', 'template', 'form2KeyJson1.json');
fileName = dirPathForjson;
}
var data = fs.readFileSync(fileName, 'utf8');
var parsedData = JSON.parse(data);
} catch (err) {
console.log(err)
}
return parsedData;
}
function runPython() {
//*********Enable below two lines before making the mac Release.///***********************
/**const fixPath = require('fix-path');
fixPath();**/
const spawn = require("child_process").spawn;
var directory = document.getElementById("directoryFilePath").value;
var FormType = document.getElementById("FormType").value;
if (FormType) {
const p = path.join(__dirname, "pythonForm", "main.py")
try {
var pythonProcess = spawn('python3', [p, '-i', directory, '-t', FormType]);
console.log(pythonProcess);
pythonProcess.stdout.on('data', (data) => {
document.getElementById('consoleOutput').innerHTML += '<p>' + data.toString() + '</p>';
});
pythonProcess.on('error', function (err) {
alert('Error occured Executing python: ' + err);
});
} catch (err) {
alert("Exception: Occured Executing python " + err)
}
}
}
function openQC() {
document.getElementById("page1").style.display = "none";
document.getElementById("page2").style.display = "block";
}
function openUploadImage() {
document.getElementById("page2").style.display = "none";
document.getElementById("page1").style.display = "block";
}
function selectDirectory() {
var filePathName = document.getElementById('selectedDirectory').value;
if (filePathName) {
readGivenDirectory(filePathName);
} else {
alert('Please mention a valid dirctory path name.');
}
}
function setDirectoryPath(e) {
var directoryPath = getUploadDirectoryPath(e);
console.log(directoryPath);
document.getElementById('selectedDirectory').value = directoryPath;
}
function getUploadDirectoryPath(e) {
var files = e.target.files;
var path = files[0].path;
var directoryPath = path.replace(files[0].name, '');
return directoryPath;
}
function setUploadedDirectoryPath(e) {
document.getElementById("directoryFilePath").value = getUploadDirectoryPath(e);
}
function saveQCJson(status, count) {
var formElement = document.getElementById("jsonData").elements;
var formType = "Form 2";
var outJson = new Array();
for (var i = 0; i < formElement.length; i++) {
var itemObj = new Object();
var item = formElement.item(i);
itemObj.Key = item.id;
itemObj.value = item.value;
outJson[i] = itemObj;
if (itemObj.Key == "formType") {
formType = itemObj.value;
}
}
var obj = new Object();
obj.data = outJson;//JSON.stringify(outJson);
obj.qcComplete = status; //"QCComplete-UploadPending";
var objString = JSON.stringify(obj);
saveLocally(objString, formType, count);
}
function saveLocally(obj, formType, count) {
var files = getFilesPath();
var imageFilePath = files["imageFilePath"];
var jsonFilePath = files["jsonFilePath"];
try { fs.writeFileSync(jsonFilePath, obj, 'utf-8'); }
catch (e) { alert('Failed to save the file !'); }
if (count == 1) {
var json = { "path": jsonFilePath, "type": "jsonFilePath", "formType": formType };
var img = { "path": imageFilePath, "type": "imageFilePath", "formType": formType };
var infoSet = [json, img];
fileUploadGoogle(infoSet);
}
}
function removeQCJsonFile(path) {
try {
if (fs.existsSync(path)) {
fs.unlink(path, function (err) { });
}
}
catch (err) {
console.log(err)
}
}
function getFilesPath() {
var imageFilePath = document.getElementById("imageFilePath").value;
var jsonFilePath = document.getElementById("JsonFilePath").value;
var fileArr = new Array();
fileArr["imageFilePath"] = imageFilePath;
fileArr["jsonFilePath"] = jsonFilePath;
return fileArr;
}
function createSubdirectory(directoryPath) {
var folderPath = directoryPath + '/' + processedFolder;
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
}
}
function isAlignedFile(fileName) {
var n = fileName.includes("_aligned");
return n;
}