-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
199 lines (172 loc) · 5.5 KB
/
Copy pathscripts.js
File metadata and controls
199 lines (172 loc) · 5.5 KB
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
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
function download_file(name){
var form = document.getElementById("my_form");
if (!form.checkValidity()) {
form.reportValidity();
return;
}
var result = $('#my_form').serializeObject();
result['form_id'] = generateUUID();
var txt = JSON.stringify(result);
var a = window.document.createElement('a');
a.href = 'data:application/json;encoding=UTF-8,'+ encodeURIComponent(txt);
a.download = name + '.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
function generateUUID() { // Public Domain/MIT
var d = new Date().getTime();//Timestamp
var d2 = (performance && performance.now && (performance.now()*1000)) || 0;//Time in microseconds since page-load or 0 if unsupported
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16;//random number between 0 and 16
if(d > 0){//Use timestamp until depleted
r = (d + r)%16 | 0;
d = Math.floor(d/16);
} else {//Use microseconds since page-load if supported
r = (d2 + r)%16 | 0;
d2 = Math.floor(d2/16);
}
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
function roundString(value, decimals)
{
// adjusted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
// convert to number
value = +value;
decimals = +decimals;
// check
if( isNaN(value) || !(typeof decimals === 'number' && decimals % 1 === 0) )
{
return NaN;
}
// check negative
var isNegative = value < 0;
if( isNegative )
{
value = -value;
}
if (typeof decimals === 'undefined' || decimals === 0)
{
value = Math.round(value);
}
else
{
value = +(Math.round(+(value + 'e+' + decimals)) + 'e-' + decimals);
}
// handle negative
if( isNegative )
{
value = -value;
}
// convert to string
value = value.toString();
return value;
}
function updateTextAreaAndLabel( fromTextAreaId, toTextAreaID, labelID, qNumRows, qNumCols, decimals )
{
// get txt
var txtArea = document.getElementById( fromTextAreaId );
var rawTxt = txtArea.value;
// convert to csv format
var lines = rawTxt.replace(/(\r\n)/g,';').replace(/[\r\n]/g,';').replace(/[ \t]/g,',');
// split each line
var arrayLines = lines.split(';');
// detected dimension
var numRows = arrayLines.length;
var numCols = 1 + (arrayLines[0].match(/,/g)||[]).length;
// init
var success = true;
var failIncorrectDimensions = ( numRows <= 0 ) || ( numRows != qNumRows ) || ( numCols <= 0 ) || ( numCols != qNumCols );
var failInconsistentDimensions = false;
var failValues = false;
// parse including round
var currentNumCols = 0;
for( var i=0; i < arrayLines.length; i++ )
{
// check dimension
currentNumCols = 1 + (arrayLines[i].match(/,/g)||[]).length;
if( currentNumCols != numCols )
{
failInconsistentDimensions = true;
}
// insert 0's if needed
if( arrayLines[i].length == 0 )
{
arrayLines[i] = '0';
}
else
{
arrayLines[i] = arrayLines[i].replace(/^,/g, '0,').replace(/,$/g, ',0').replace(/,(?=,)/g, ',0');
}
// convert each element to number and round, then convert back
var arrayElems = arrayLines[i].split(',');
for( var j=0; j < arrayElems.length; j++ )
{
arrayElems[j] = roundString( arrayElems[j], decimals );
if( isNaN(arrayElems[j]) )
{
failValues = true;
}
}
// combine and store new string
arrayLines[i] = arrayElems.join(',');
}
// combine using newline instead of ; for readability
lines = arrayLines.join('\n');
// successful?
success = (success && !failIncorrectDimensions && !failInconsistentDimensions && !failValues );
// make new label
var newLabel = '';
if( success )
{
newLabel += '<font color="green"><b>Success! Detected a matrix of size ' + numRows + ' x ' + numCols + '.</b></font>';
}
else
{
newLabel += '<font color="red"><b>Failure!';
if( failInconsistentDimensions )
{
newLabel += ' Inconsistent dimensions detected!';
}
else
{
if( failIncorrectDimensions )
{
newLabel += ' Incorrect dimensions detected (detected a matrix of size ' + numRows + ' x ' + numCols + ')!';
}
}
if( failValues )
{
newLabel += ' Invalid values detected!';
}
newLabel += '</b></font>';
}
// set textarea
var resultTxtArea = document.getElementById( toTextAreaID );
resultTxtArea.value = lines;
// set label
var resultLabel = document.getElementById( labelID );
resultLabel.innerHTML = newLabel;
}
function getFormElement()
{
var elem = document.getElementById( 'util_functions_div_id' ).parentElement;
return elem;
}