-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
154 lines (125 loc) · 4.49 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
/* global jQuery */
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const electron = require('electron')
const Hasher = require('./build/Hasher')
const SubjectFile = require('./build/SubjectFile')
const HashMenu = require('./build/HashMenu')
const utils = require('./build/Utils')
const UI = require('./build/UI')
const myUi = new UI()
const objSectionMarker = 'obj-section-id-'
const loading = `
<div class="load-wrapp">
<div class="load-10">
<div class="bar"></div>
</div>
</div>
`
// const requiredHashes = [
// 'md5',
// 'sha256',
// 'sha512'
// ]
const menu = new HashMenu()
const fileObjects = []
const renderHash = function (fileObj, typeOfHash) {
const outputEl = document.querySelector(fileObj.hashes[typeOfHash].uiselector)
outputEl.innerHTML = fileObj.hashes[typeOfHash].hash
}
const abortHash = function (fileObj) {
const outputEl = document.querySelector('#' + fileObj.uiselector)
if (outputEl !== null) {
outputEl.parentElement.removeChild(outputEl)
}
// the only error that can cause this condition atm
myUi.uiError('Doh! It only works on files, not directories...')
}
const getRelatedFileObj = function (sectionId) {
const relatedFileObj = fileObjects.find(function (obj, index, arr) {
if (obj.uiselector === sectionId) {
return obj
}
return false
})
return relatedFileObj
}
window.compareSums = function () {
const inputTarget = window.event.target
const parentSection = utils.parentUntil(inputTarget, 'file-section')
const inputValue = inputTarget.value
const sectionId = parentSection.id
const type = inputTarget.getAttribute('data-type')
const obj = getRelatedFileObj(sectionId)
console.log(type)
console.log(obj)
const labelParent = utils.parentUntil(inputTarget, 'file--hash--output')
const label = labelParent.getElementsByClassName('file--hash--label')[0]
if (label.className.indexOf('valid-match') !== -1) {
label.className = label.className.replace('valid-match', '')
}
if (label.className.indexOf('invalid-match') !== -1) {
label.className = label.className.replace('invalid-match', '')
}
if (inputValue === obj.hashes[type].hash) {
label.className = label.className + ' valid-match'
} else {
label.className = label.className + ' invalid-match'
}
}
const setupDisplay = function (fileObj, hashItems) {
console.log(fileObj)
// todo this should be generated per hash type.
// as it is, logic can crash if there is no section
// allocated for the hash type
let output = `<div class="file-section" id="${fileObj.uiselector}">
<div class="file--name">${fileObj.filePath}</div>`
hashItems.forEach((hashType) => {
output += `
<div class="file--hash--output file--hash--output--${hashType}">
<div class="file--hash--label">${hashType}</div>
<div class="file--overflow--group">
<div class="file--${hashType} file--hash-output" id="">${loading}</div>
<div class="file--input file--input--${hashType}"><input onkeyup="compareSums()" data-type="${hashType}" class="file--input--verify" type="text" /></div>
</div>
</div>
`
})
output += '</div>'
const content = document.getElementById('output').innerHTML
document.getElementById('output').innerHTML = output + content
}
const hashAndDisplayOutput = function (path) {
const subjectFile = new SubjectFile(path, objSectionMarker, renderHash, abortHash)
const enabledItems = menu.getEnabledItems()
setupDisplay(subjectFile, enabledItems)
// subjectFile.setHashes( Hasher, renderHash );
fileObjects.push(subjectFile)
enabledItems.forEach((hashType) => {
const myhasher = new Hasher(hashType)
myhasher.doHash(subjectFile)
})
myUi.showResultsTab(jQuery)
}
document.addEventListener('drop', function (e) {
e.preventDefault()
e.stopPropagation()
myUi.clearErrors()
for (const f of e.dataTransfer.files) {
console.log('File(s) you dragged here: ', f)
hashAndDisplayOutput(f.path)
}
})
document.addEventListener('dragover', function (e) {
e.preventDefault()
e.stopPropagation()
})
const selectDirBtn = document.getElementById('select-directory')
selectDirBtn.addEventListener('click', (event) => {
electron.ipcRenderer.send('open-file-dialog')
})
electron.ipcRenderer.on('selected-directory', (event, path) => {
myUi.clearErrors()
hashAndDisplayOutput(path[0])
})