Commit 4f96443b authored by Mihkel Putrinš's avatar Mihkel Putrinš

refactor

parent ecd9f3de
...@@ -21,28 +21,63 @@ var uploader = require(path.join(__dirname, 'upload.js')) ...@@ -21,28 +21,63 @@ var uploader = require(path.join(__dirname, 'upload.js'))
var user_data = {} var user_data = {}
var data = ipc.sendSync('getUser', null) var data = ipc.sendSync('getUser', null)
function initialize() { function setFormState(state) {
if (!data) { switch(state) {
data = JSON.parse(clipboard.readText()) case 'select':
clipboard.clear() document.getElementById('selectLocal').removeAttribute('hidden')
ipc.send('setUser', data) // ---
} document.getElementById('loading').setAttribute('hidden', '')
// console.log('user_data: ' + JSON.stringify(data, null, 4)) document.getElementById('resourceStats').setAttribute('hidden', '')
if (op.get(data, 'result.user_id', false)) { document.getElementById('uploading').setAttribute('hidden', '')
user_data.user_id = op.get(data, 'result.user_id') document.getElementById('uploadResource').setAttribute('hidden', '')
user_data.session_key = op.get(data, 'result.session_key') document.getElementById('resourceName').setAttribute('hidden', '')
user_data.name = op.get(data, 'result.name') // ---
document.getElementById('userName').innerHTML = user_data.name // document.getElementById('uploading').removeAttribute('hidden')
var title = UPLOADER_VERSION + ' | ' + user_data.name break
ipc.send('setTitle', title) case 'loading':
setFormState('select') document.getElementById('loading').removeAttribute('hidden')
} else { document.getElementById('resourceStats').removeAttribute('hidden')
ipc.send('log', 'User data incomplete.') // -------- //
ipc.send('data', data) document.getElementById('uploading').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'loaded':
document.getElementById('resourceStats').removeAttribute('hidden')
document.getElementById('uploadResource').removeAttribute('hidden')
document.getElementById('selectLocal').removeAttribute('hidden')
document.getElementById('selectLocalButton').innerHTML = 'Vali uuesti'
document.getElementById('resourceName').removeAttribute('hidden')
// -------- //
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
break
case 'uploading':
document.getElementById('uploading').removeAttribute('hidden')
// -------- //
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'uploaded':
document.getElementById('thankYou').removeAttribute('hidden')
// -------- //
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('selectLocalButton').innerHTML = 'Vali uuesti'
document.getElementById('resourceName').setAttribute('hidden', '')
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
break
} }
ipc.send('closeAuth')
} }
var resource = {} var resource = {}
var resource_stats = {} var resource_stats = {}
var renderer_interval var renderer_interval
...@@ -68,46 +103,6 @@ function renderResource() { ...@@ -68,46 +103,6 @@ function renderResource() {
}) })
} }
function selectLocal () {
resource = {name: 'root'}
resource_stats = {files: {count: 0, size: 0}, directories: {count: 0}, mime:{}}
dialog.showOpenDialog({properties:['openFile', 'openDirectory', 'multiSelections']}, function selectedPath(_paths) {
if (!_paths) {
return
}
renderer_interval = setInterval(function () {
renderResource()
}, 100)
setFormState('loading')
if (_paths.length === 1) {
var single_file = _paths[0]
op.set(resource, 'name', path.basename(single_file))
document.getElementById('resourceNameInput').value = resource.name
fs.stat(single_file, function(err, stats) {
if (err) {
throw (err)
}
if (stats.isDirectory()) {
fs.readdir(single_file, function(err, files) {
if (err) {
throw (err)
}
_paths = files.map(function(file) {
var fullpath = path.join(single_file, file)
return fullpath
})
recurseLocal(resource, _paths, resourceLoaded)
})
} else {
recurseLocal(resource, _paths, resourceLoaded)
}
})
} else {
recurseLocal(resource, _paths, resourceLoaded)
}
})
}
function resourceLoaded() { function resourceLoaded() {
renderResource() renderResource()
setFormState('loaded') setFormState('loaded')
...@@ -162,60 +157,68 @@ function recurseLocal(parent_resource, paths, loadedCB) { ...@@ -162,60 +157,68 @@ function recurseLocal(parent_resource, paths, loadedCB) {
}) })
} }
function selectLocal () {
resource = {name: 'root'}
resource_stats = {files: {count: 0, size: 0}, directories: {count: 0}, mime:{}}
dialog.showOpenDialog({properties:['openFile', 'openDirectory', 'multiSelections']}, function selectedPath(_paths) {
if (!_paths) {
return
}
renderer_interval = setInterval(function () {
renderResource()
}, 100)
setFormState('loading')
if (_paths.length === 1) {
var single_file = _paths[0]
op.set(resource, 'name', path.basename(single_file))
document.getElementById('resourceNameInput').value = resource.name
fs.stat(single_file, function(err, stats) {
if (err) {
throw (err)
}
if (stats.isDirectory()) {
fs.readdir(single_file, function(err, files) {
if (err) {
throw (err)
}
_paths = files.map(function(file) {
var fullpath = path.join(single_file, file)
return fullpath
})
recurseLocal(resource, _paths, resourceLoaded)
})
} else {
recurseLocal(resource, _paths, resourceLoaded)
}
})
} else {
recurseLocal(resource, _paths, resourceLoaded)
}
})
}
function setFormState(state) {
switch(state) {
case 'select': function initialize() {
document.getElementById('selectLocal').removeAttribute('hidden') if (!data) {
// --- data = JSON.parse(clipboard.readText())
document.getElementById('loading').setAttribute('hidden', '') clipboard.clear()
document.getElementById('resourceStats').setAttribute('hidden', '') ipc.send('setUser', data)
document.getElementById('uploading').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
// ---
// document.getElementById('uploading').removeAttribute('hidden')
break
case 'loading':
document.getElementById('loading').removeAttribute('hidden')
document.getElementById('resourceStats').removeAttribute('hidden')
// -------- //
document.getElementById('uploading').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'loaded':
document.getElementById('resourceStats').removeAttribute('hidden')
document.getElementById('uploadResource').removeAttribute('hidden')
document.getElementById('selectLocal').removeAttribute('hidden')
document.getElementById('selectLocalButton').innerHTML = 'Vali uuesti'
document.getElementById('resourceName').removeAttribute('hidden')
// -------- //
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
break
case 'uploading':
document.getElementById('uploading').removeAttribute('hidden')
// -------- //
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('resourceName').setAttribute('hidden', '')
break
case 'uploaded':
document.getElementById('thankYou').removeAttribute('hidden')
// -------- //
document.getElementById('resourceStats').setAttribute('hidden', '')
document.getElementById('uploadResource').setAttribute('hidden', '')
document.getElementById('selectLocal').setAttribute('hidden', '')
document.getElementById('selectLocalButton').innerHTML = 'Vali uuesti'
document.getElementById('resourceName').setAttribute('hidden', '')
document.getElementById('loading').setAttribute('hidden', '')
document.getElementById('uploading').setAttribute('hidden', '')
break
} }
// console.log('user_data: ' + JSON.stringify(data, null, 4))
if (op.get(data, 'result.user_id', false)) {
user_data.user_id = op.get(data, 'result.user_id')
user_data.session_key = op.get(data, 'result.session_key')
user_data.name = op.get(data, 'result.name')
document.getElementById('userName').innerHTML = user_data.name
var title = UPLOADER_VERSION + ' | ' + user_data.name
ipc.send('setTitle', title)
setFormState('select')
} else {
ipc.send('log', 'User data incomplete.')
ipc.send('data', data)
}
ipc.send('closeAuth')
} }
initialize() initialize()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment