app/js/renderer/bw/fileinput.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 1.5

Size

Lines of Code 215
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 98
mnd 6
bc 6
fnc 12
dl 0
loc 215
rs 10
bpm 0.5
cpm 1.5
noi 5
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
F fileinput.js ➔ dragDropInitialization 0 23 14
1
// jshint esversion: 8, -W069
2
/** global: appSettings */
3
4
const whois = require('../../common/whoisWrapper'),
0 ignored issues
show
Unused Code introduced by
The constant whois seems to be never used. Consider removing it.
Loading history...
5
  conversions = require('../../common/conversions'),
6
  fs = require('fs');
7
8
const {
9
  ipcRenderer
10
} = require('electron'), {
11
  tableReset
12
} = require('./auxiliary');
13
14
require('../../common/stringFormat');
15
16
var bwFileContents;
17
18
/*
19
  ipcRenderer.on('bw:fileinput.confirmation', function(...) {...});
20
    // File input, path and information confirmation container
21
  parameters
22
    event
23
    filePath
24
    isDragDrop
25
 */
26
ipcRenderer.on('bw:fileinput.confirmation', function(event, filePath = null, isDragDrop = false) {
27
  var bwFileStats; // File stats, size, last changed, etc
28
  const misc = settings['lookup.misc'];
0 ignored issues
show
Bug introduced by
The variable settings seems to be never declared. If this is a global, consider adding a /** global: settings */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
29
  const lookup = {
30
    randomize: {
31
      timeBetween: settings['lookup.randomize.timeBetween']
32
    }
33
  };
34
35
  //console.log(filePath);
36
  if (filePath === undefined || filePath == '' || filePath === null) {
37
    //console.log(filePath);
38
    $('#bwFileinputloading').addClass('is-hidden');
39
    $('#bwEntry').removeClass('is-hidden');
40
  } else {
41
    $('#bwLoadingInfo').text('Loading file stats...');
42
    if (isDragDrop === true) {
43
      $('#bwEntry').addClass('is-hidden');
44
      $('#bwFileinputloading').removeClass('is-hidden');
45
      bwFileStats = fs.statSync(filePath);
46
      bwFileStats['filename'] = filePath.replace(/^.*[\\\/]/, '');
47
      bwFileStats['humansize'] = conversions.byteToHumanFileSize(bwFileStats['size'], misc.usestandardsize);
48
      $('#bwFileSpanInfo').text('Loading file contents...');
49
      bwFileContents = fs.readFileSync(filePath);
50
    } else {
51
      bwFileStats = fs.statSync(filePath[0]);
52
      bwFileStats['filename'] = filePath[0].replace(/^.*[\\\/]/, '');
53
      bwFileStats['humansize'] = conversions.byteToHumanFileSize(bwFileStats['size'], misc.usestandardsize);
54
      $('#bwFileSpanInfo').text('Loading file contents...');
55
      bwFileContents = fs.readFileSync(filePath[0]);
56
    }
57
    $('#bwFileSpanInfo').text('Getting line count...');
58
    bwFileStats['linecount'] = bwFileContents.toString().split('\n').length;
59
60
    if (lookup.randomize.timeBetween.randomize === true) {
61
      bwFileStats['minestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timeBetween.minimum);
62
      bwFileStats['maxestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timeBetween.maximum);
63
64
      $('#bwFileSpanTimebetweenmin').text('{0}ms '.format(lookup.randomize.timeBetween.minimum));
65
      $('#bwFileSpanTimebetweenmax').text('/ {0}ms'.format(lookup.randomize.timeBetween.maximum));
66
      $('#bwFileTdEstimate').text('{0} to {1}'.format(bwFileStats['minestimate'], bwFileStats['maxestimate']));
67
    } else {
68
      bwFileStats['minestimate'] = conversions.msToHumanTime(bwFileStats['linecount'] * lookup.randomize.timeBetween.minimum);
69
      $('#bwFileSpanTimebetweenminmax').addClass('is-hidden');
70
      $('#bwFileSpanTimebetweenmin').text(lookup.randomize.timeBetween.minimum + 'ms');
71
      $('#bwFileTdEstimate').text('> {0}'.format(bwFileStats['minestimate']));
72
    }
73
74
75
76
    bwFileStats['filepreview'] = bwFileContents.toString().substring(0, 50);
77
    //console.log(readLines(filePath[0]));
78
    //console.log(bwFileStats['filepreview']);
79
80
    //console.log(lineCount(bwFileContents));
81
    $('#bwFileinputloading').addClass('is-hidden');
82
    $('#bwFileinputconfirm').removeClass('is-hidden');
83
84
    // stats
85
    $('#bwFileTdName').text(bwFileStats['filename']);
86
    $('#bwFileTdLastmodified').text(conversions.getDate(bwFileStats['mtime']));
87
    $('#bwFileTdLastaccess').text(conversions.getDate(bwFileStats['atime']));
88
    $('#bwFileTdFilesize').text(bwFileStats['humansize'] + ' ({0} line(s))'.format(bwFileStats['linecount']));
89
    $('#bwFileTdFilepreview').text(bwFileStats['filepreview'] + '...');
90
    //$('#bwTableMaxEstimate').text(bwFileStats['maxestimate']);
91
    //console.log('cont:'+ bwFileContents);
92
93
    //console.log(bwFileStats['linecount']);
94
  }
95
96
  return;
97
});
98
99
/*
100
  $('#bwEntryButtonFile').click(function() {...});
101
    File Input, Entry container button
102
 */
103
$('#bwEntryButtonFile').click(function() {
104
  $('#bwEntry').addClass('is-hidden');
105
  $.when($('#bwFileinputloading').removeClass('is-hidden').delay(10)).done(function() {
106
    ipcRenderer.send("bw:input.file");
107
  });
108
109
  return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
110
});
111
112
/*
113
  $('#bwFileButtonCancel').click(function() {...});
114
    File Input, cancel file confirmation
115
 */
116
$('#bwFileButtonCancel').click(function() {
117
  $('#bwFileinputconfirm').addClass('is-hidden');
118
  $('#bwEntry').removeClass('is-hidden');
119
120
  return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
121
});
122
123
/*
124
  $('#bwFileButtonConfirm').click(function() {...});
125
    File Input, proceed to bulk whois
126
 */
127
$('#bwFileButtonConfirm').click(function() {
128
  var bwDomainArray = bwFileContents.toString().split('\n').map(Function.prototype.call, String.prototype.trim);
129
  var bwTldsArray = $('#bwFileInputTlds').val().toString().split(',');
130
131
  tableReset(bwDomainArray.length, bwTldsArray.length);
132
  $('#bwFileinputconfirm').addClass('is-hidden');
133
  $('#bwProcessing').removeClass('is-hidden');
134
135
  /*
136
  console.log(bwDomainArray);
137
  console.log(bwTldsArray);
138
  */
139
140
  ipcRenderer.send("bw:lookup", bwDomainArray, bwTldsArray);
141
});
142
143
/*
144
  dragDropInitialization (self-executing)
145
    Bulk whois file input by drag and drop
146
 */
147
(function dragDropInitialization() {
148
  var holder = $('#bwMainContainer');
149
  holder.ondragover = function() {
150
    return false;
151
  };
152
153
  holder.ondragleave = function() {
154
    return false;
155
  };
156
157
  holder.ondragend = function() {
158
    return false;
159
  };
160
161
  holder.ondrop = function(event) {
162
    event.preventDefault();
163
    for (let f of event.dataTransfer.files) {
164
      ipcRenderer.send('File(s) you dragged here: {0}'.format(f.path));
165
      ipcRenderer.send('ondragstart', f.path);
166
    }
167
    return false;
168
  };
169
})();
170
171
/*
172
$("html").on("dragover", function(event) {
173
    event.preventDefault();
174
    event.stopPropagation();
175
  console.log('dragging');
176
});
177
178
$("html").on("dragleave", function(event) {
179
    event.preventDefault();
180
    event.stopPropagation();
181
    console.log('dragging');
182
});
183
184
$("html").on("drop", function(event) {
185
    event.preventDefault();
186
    event.stopPropagation();
187
    alert("Dropped!");
188
});*/
189
190
/*
191
  $('#bwMainContainer').on('drop', function(...) {...});
192
    On Drop ipsum
193
 */
194
$('#bwMainContainer').on('drop', function(event) {
195
  event.preventDefault();
196
  for (let f of event.originalEvent.dataTransfer.files) {
197
    ipcRenderer.send('File(s) you dragged here: {0}'.format(f.path));
198
    ipcRenderer.send('ondragstart', f.path);
199
  }
200
201
  return false;
202
});
203
204
/*
205
  $('#bwFileInputTlds').keyup(function(...) {...});
206
    ipsum
207
 */
208
$('#bwFileInputTlds').keyup(function(event) {
209
  // Cancel the default action, if needed
210
  event.preventDefault();
211
  // Number 13 is the "Enter" key on the keyboard
212
  if (event.keyCode === 13) {
213
    // Trigger the button element with a click
214
    $('#bwFileButtonConfirm').click();
215
  }
216
217
  return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
218
});
219