Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/documents/scripts/views/app.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class App extends View
'click .menu .button': 'clickMenuButton'
'click .button-login': 'clickLogin'
'click .button-add-site': 'clickAddSite'
'click .button-add-upload': 'clickAddUpload'
'change .upload-file-input': 'changeUploadFile'
'submit .site-add-form': 'submitSite'
'click .site-add-form .button-cancel': 'submitSiteCancel'

Expand Down Expand Up @@ -316,6 +318,80 @@ class App extends View
throw new Error("Offline people can't login") unless navigator.id?
navigator.id.request()


# Open a native file picker for uploading into the current collection
clickAddUpload: (e) =>
# Disable click through
e.preventDefault()
e.stopPropagation()

# Ensure we have a site and collection selected
unless @currentSite and @currentFileCollection
alert 'Select a site and collection before uploading a file'
return @

# Re-use a hidden file input so the browser handles the picker
$input = @$el.find('.upload-file-input')
unless $input.length
$input = $('<input>', {
type: 'file'
class: 'upload-file-input'
style: 'display:none'
})
@$el.append($input)

$input.val('')
$input.click()

# Chain
@

# Read the selected local file and create it in the current collection
changeUploadFile: (e) =>
# Prepare
input = e.currentTarget
file = input.files?[0]
return @ unless file

unless window.FileReader
alert 'Your browser does not support file uploads'
return @

reader = new FileReader()
reader.onload = (event) =>
content = event.target.result
relativePath = file.name
model = @currentFileCollection.get('files').create({
filename: file.name
relativePath: relativePath
contentType: file.type or 'application/octet-stream'
encoding: 'utf8'
content: content
source: content
site: @currentSite
}, {sync:false})

# Add it locally, save through the file API, then refresh the listing
@currentFileCollection.get('files').add(model)
model.sync {}, (err) =>
if err
alert err.message or err
throw err
@currentFileCollection.set('relativePaths', (@currentFileCollection.get('relativePaths') or []).concat([relativePath]))
@openApp({
site: @currentSite
fileCollection: @currentFileCollection
navigate: false
})

reader.onerror = =>
alert 'Unable to read selected file'

reader.readAsText(file)

# Chain
@

# Handle menu effects
clickMenuButton: (e) =>
# Prepare
Expand Down