-module (demos_upload).
-include_lib ("nitrogen_core/include/wf.hrl").

main() -> #template { file="./templates/demos46.html" }.

title() -> "File Upload".
headline() -> "File Upload".

left() -> 
        The <code>#upload{}</code> element allows a user to upload a
        file into a scratch directory.

        The element fires events when the upload starts and when the
        upload completes, and passes back original filename, the name
        of the file on disk, and the node to which the file was

right() -> 
        #flash {},
  #h3 { text="Upload Example #1" },
  "Upload example with modified button text. The default text is 'Upload'.",
  #upload { tag=myUpload1, button_text="Upload File" },


  #h3 { text="Upload Example #2" },
  "This example hides he upload button. When the user selects a file it will automatically start uploading.",
  #upload { tag=myUpload1, show_button=false },


  #h3{ text="Upload Example #3" },
  "This example is drag and drop enabled for browsers that support it (Chrome, Firefox). Further it's enabled for multiple files",
  #upload{ tag=myUpload1, show_button=false, multiple=true, droppable=true }

event(_) -> ok.

start_upload_event(myUpload1) ->
    wf:flash("Upload started.").

finish_upload_event(_Tag, undefined, _, _) ->
    wf:flash("Please select a file."),

finish_upload_event(_Tag, FileName, LocalFileData, Node) ->
    FileSize = filelib:file_size(LocalFileData),
    wf:flash(wf:f("Uploaded file: ~s (~p bytes) on node ~s.", [FileName, FileSize, Node])),