You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.6 KiB
147 lines
3.6 KiB
7 years ago
|
/** File upload utility */
|
||
7 years ago
|
window.TermUpl = function (conn, input) {
|
||
7 years ago
|
let lines, // array of lines without newlines
|
||
7 years ago
|
line_i, // current line index
|
||
|
fuTout, // timeout handle for line sending
|
||
|
send_delay_ms, // delay between lines (ms)
|
||
|
nl_str, // newline string to use
|
||
|
curLine, // current line (when using fuOil)
|
||
7 years ago
|
inline_pos // Offset in line (for long lines)
|
||
7 years ago
|
|
||
|
// lines longer than this are split to chunks
|
||
|
// sending a super-ling string through the socket is not a good idea
|
||
7 years ago
|
const MAX_LINE_LEN = 128
|
||
7 years ago
|
|
||
7 years ago
|
function fuOpen () {
|
||
|
fuStatus('Ready...')
|
||
|
Modal.show('#fu_modal', onClose)
|
||
|
$('#fu_form').toggleClass('busy', false)
|
||
7 years ago
|
input.blockKeys(true)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
function onClose () {
|
||
|
console.log('Upload modal closed.')
|
||
|
clearTimeout(fuTout)
|
||
|
line_i = 0
|
||
7 years ago
|
input.blockKeys(false)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
function fuStatus (msg) {
|
||
|
qs('#fu_prog').textContent = msg
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
function fuSend () {
|
||
|
let v = qs('#fu_text').value
|
||
7 years ago
|
if (!v.length) {
|
||
7 years ago
|
fuClose()
|
||
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
lines = v.split('\n')
|
||
|
line_i = 0
|
||
|
inline_pos = 0 // offset in line
|
||
|
send_delay_ms = qs('#fu_delay').value
|
||
7 years ago
|
|
||
|
// sanitize - 0 causes overflows
|
||
|
if (send_delay_ms < 0) {
|
||
7 years ago
|
send_delay_ms = 0
|
||
|
qs('#fu_delay').value = send_delay_ms
|
||
7 years ago
|
}
|
||
|
|
||
|
nl_str = {
|
||
|
'CR': '\r',
|
||
|
'LF': '\n',
|
||
7 years ago
|
'CRLF': '\r\n'
|
||
|
}[qs('#fu_crlf').value]
|
||
7 years ago
|
|
||
7 years ago
|
$('#fu_form').toggleClass('busy', true)
|
||
|
fuStatus('Starting...')
|
||
|
fuSendLine()
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
function fuSendLine () {
|
||
7 years ago
|
if (!$('#fu_modal').hasClass('visible')) {
|
||
|
// Modal is closed, cancel
|
||
7 years ago
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
if (!conn.canSend()) {
|
||
7 years ago
|
// postpone
|
||
7 years ago
|
fuTout = setTimeout(fuSendLine, 1)
|
||
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
if (inline_pos === 0) {
|
||
7 years ago
|
curLine = lines[line_i++] + nl_str
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
let chunk
|
||
7 years ago
|
if ((curLine.length - inline_pos) <= MAX_LINE_LEN) {
|
||
7 years ago
|
chunk = curLine.substr(inline_pos, MAX_LINE_LEN)
|
||
|
inline_pos = 0
|
||
7 years ago
|
} else {
|
||
7 years ago
|
chunk = curLine.substr(inline_pos, MAX_LINE_LEN)
|
||
|
inline_pos += MAX_LINE_LEN
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
if (!input.sendString(chunk)) {
|
||
7 years ago
|
fuStatus('FAILED!')
|
||
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
let all = lines.length
|
||
7 years ago
|
|
||
7 years ago
|
fuStatus(line_i + ' / ' + all + ' (' + (Math.round((line_i / all) * 1000) / 10) + '%)')
|
||
7 years ago
|
|
||
|
if (lines.length > line_i || inline_pos > 0) {
|
||
7 years ago
|
fuTout = setTimeout(fuSendLine, send_delay_ms)
|
||
7 years ago
|
} else {
|
||
7 years ago
|
closeWhenReady()
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
function closeWhenReady () {
|
||
7 years ago
|
if (!conn.canSend()) {
|
||
7 years ago
|
// stuck in XOFF still, wait to process...
|
||
7 years ago
|
fuStatus('Waiting for Tx buffer...')
|
||
|
setTimeout(closeWhenReady, 100)
|
||
7 years ago
|
} else {
|
||
7 years ago
|
fuStatus('Done.')
|
||
7 years ago
|
// delay to show it
|
||
|
setTimeout(function () {
|
||
7 years ago
|
fuClose()
|
||
|
}, 100)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
function fuClose () {
|
||
|
Modal.hide('#fu_modal')
|
||
7 years ago
|
}
|
||
|
|
||
|
return {
|
||
|
init: function () {
|
||
|
qs('#fu_file').addEventListener('change', function (evt) {
|
||
7 years ago
|
let reader = new FileReader()
|
||
|
let file = evt.target.files[0]
|
||
|
console.log('Selected file type: ' + file.type)
|
||
7 years ago
|
if (!file.type.match(/text\/.*|application\/(json|csv|.*xml.*|.*script.*)/)) {
|
||
|
// Deny load of blobs like img - can crash browser and will get corrupted anyway
|
||
7 years ago
|
if (!confirm('This does not look like a text file: ' + file.type + '\nReally load?')) {
|
||
|
qs('#fu_file').value = ''
|
||
|
return
|
||
7 years ago
|
}
|
||
|
}
|
||
|
reader.onload = function (e) {
|
||
7 years ago
|
const txt = e.target.result.replace(/[\r\n]+/, '\n')
|
||
|
qs('#fu_text').value = txt
|
||
|
}
|
||
|
console.log('Loading file...')
|
||
|
reader.readAsText(file)
|
||
|
}, false)
|
||
7 years ago
|
},
|
||
|
close: fuClose,
|
||
|
start: fuSend,
|
||
7 years ago
|
open: fuOpen
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|