Permalink
Browse files

introduce public methods to the jQuery plugin, documentation

  • Loading branch information...
1 parent e735794 commit 072b6477a6a81f5f37d39fe61f67b42c9a6d32c5 Wayne committed Oct 7, 2012
Showing with 102 additions and 69 deletions.
  1. +11 −0 README.md
  2. +91 −69 app/assets/javascripts/s3_direct_upload.js.coffee
View
11 README.md
@@ -130,6 +130,16 @@ This would be accessable in your params hash as `params[:extra_data][:key]`
This could be useful if you would like to validate the filenames of files to be uploaded for example. If true is returned file will be uploaded as normal, false will cancel the upload.
+### Public methods
+You can change the settings on your form later on by accessing the jQuery instance:
+
+```cofeescript
+jQuery ->
+ v = $("#myS3Uploader").S3Uploader()
+ ...
+ v.path = "new/path/"
+ v.exta_data = "newdata"
+
### Global Event Hooks
When all uploads finish in a batch an `s3_uploads_complete` event will be triggered on `document`, so you could do something like:
@@ -149,6 +159,7 @@ jQuery ->
```
+
## Contributing / TODO
This is just a simple gem that only really provides some javascript and a form helper.
View
160 app/assets/javascripts/s3_direct_upload.js.coffee
@@ -1,72 +1,94 @@
#= require jquery-fileupload/basic
#= require jquery-fileupload/vendor/tmpl
-
-jQuery.fn.S3Uploader = (options) ->
-
- $uploadForm = this
-
- settings =
- path: ''
- extra_data: null
- before_add: null
-
- settings = $.extend settings, options
-
- current_files = []
-
- $uploadForm.fileupload
-
- add: (e, data) ->
- current_files.push data
- file = data.files[0]
- unless settings.before_add and not settings.before_add(file)
- data.context = $(tmpl("template-upload", file))
- $uploadForm.append(data.context)
- data.submit()
-
- progress: (e, data) ->
- if data.context
- progress = parseInt(data.loaded / data.total * 100, 10)
- data.context.find('.bar').css('width', progress + '%')
-
- done: (e, data) ->
- file = data.files[0]
- domain = $uploadForm.attr('action')
- path = settings.path + $uploadForm.find('input[name=key]').val().replace('/${filename}', '')
- to = $uploadForm.data('post')
- content = {}
- content[$uploadForm.data('as')] = domain + path + '/' + file.name
- content.name = file.name
- content.path = path
- if settings.extra_data
- content.extra_data = settings.extra_data
- if 'size' of file
- content.file_size = file.size
- if 'type' of file
- content.file_type = file.type
-
- $.post(to, content)
- data.context.remove() if data.context # remove progress bar
-
- current_files.splice($.inArray(data, current_files), 1) # remove that element from the array
- if current_files.length == 0
- $(document).trigger("s3_uploads_complete")
-
- fail: (e, data) ->
- alert("#{data.files[0].name} failed to upload.")
- console.log("Upload failed:")
- console.log(data)
-
- formData: (form) ->
- data = form.serializeArray()
- fileType = ""
- if "type" of @files[0]
- fileType = @files[0].type
- data.push
- name: "Content-Type"
- value: fileType
-
- data[1].value = settings.path + data[1].value
-
- data
+(($) ->
+ $.fn.S3Uploader = (options) ->
+
+ # support multiple elements
+ if @length > 1
+ @each ->
+ $(this).S3Upleader options
+
+ return this
+
+ $uploadForm = this
+
+ settings =
+ path: ''
+ extra_data: null
+ before_add: null
+
+ settings = $.extend settings, options
+
+ current_files = []
+
+ setUploadForm = ->
+ $uploadForm.fileupload
+
+ add: (e, data) ->
+ current_files.push data
+ file = data.files[0]
+ unless settings.before_add and not settings.before_add(file)
+ data.context = $(tmpl("template-upload", file))
+ $uploadForm.append(data.context)
+ data.submit()
+
+ progress: (e, data) ->
+ if data.context
+ progress = parseInt(data.loaded / data.total * 100, 10)
+ data.context.find('.bar').css('width', progress + '%')
+
+ done: (e, data) ->
+ file = data.files[0]
+ domain = $uploadForm.attr('action')
+ path = settings.path + $uploadForm.find('input[name=key]').val().replace('/${filename}', '')
+ to = $uploadForm.data('post')
+ content = {}
+ content[$uploadForm.data('as')] = domain + path + '/' + file.name
+ content.name = file.name
+ content.path = path
+ if settings.extra_data
+ content.extra_data = settings.extra_data
+ if 'size' of file
+ content.file_size = file.size
+ if 'type' of file
+ content.file_type = file.type
+
+ $.post(to, content)
+ data.context.remove() if data.context # remove progress bar
+
+ current_files.splice($.inArray(data, current_files), 1) # remove that element from the array
+ if current_files.length == 0
+ $(document).trigger("s3_uploads_complete")
+
+ fail: (e, data) ->
+ alert("#{data.files[0].name} failed to upload.")
+ console.log("Upload failed:")
+ console.log(data)
+
+ formData: (form) ->
+ data = form.serializeArray()
+ fileType = ""
+ if "type" of @files[0]
+ fileType = @files[0].type
+ data.push
+ name: "Content-Type"
+ value: fileType
+
+ data[1].value = settings.path + data[1].value
+
+ data
+
+ #public methods
+ @initialize = ->
+ setUploadForm()
+ this
+
+ @path = (new_path) ->
+ settings.path = new_path
+
+ @extra_data = (new_data) ->
+ settings.extra_data = extra_data
+
+ @initialize()
+) jQuery

0 comments on commit 072b647

Please sign in to comment.