pretty_file_input 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e57d9efb1610be2863391f09c934f208c3a5f2e
4
+ data.tar.gz: d46c59fca21ff1b76fa3c1bf37304b9de6eb721e
5
+ SHA512:
6
+ metadata.gz: 30f013d6bcc509c9a77bf227291e4e5a522add470edc1ccff78e059821fd24cd550fc0f577ba6347489de04caeefef8bcbb9c97c369e8ea426cddf323660ef3e
7
+ data.tar.gz: 5707ec9e0e91111b18da1d53b024d4bcd9bb7cc33264749883f5bc4c45703b466dc4d9a2068ce8ee48570cf53fbabb795fb92b720e0d89f9349b4669dcba434a
@@ -0,0 +1,7 @@
1
+ .bundle/
2
+ log/*.log
3
+ tmp/*
4
+ coverage/*
5
+ *.gem
6
+ Gemfile.lock
7
+ .ruby-gemset
@@ -0,0 +1 @@
1
+ 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,6 @@
1
+ pretty_file_input
2
+ =======
3
+
4
+ ## License
5
+
6
+ [MIT](http://dobtco.mit-license.org/)
@@ -0,0 +1,136 @@
1
+ (($, window) ->
2
+
3
+ class PrettyFileInput
4
+ defaults:
5
+ additionalParams: {}
6
+ name: undefined
7
+ persisted: undefined
8
+ action: undefined
9
+ method: undefined
10
+
11
+ constructor: ($el, options) ->
12
+ @$el = $el
13
+ @options = $.extend {}, @defaults, options, @$el.data('pfi')
14
+
15
+ @$form = @$el.closest('form')
16
+ @$input = @$el.find('input').attr('name', if @options.persisted then false else @options.name)
17
+ @$filename = @$el.find('.pfi_existing_filename')
18
+ @$status = @$el.find('.pfi_status')
19
+
20
+ removeKey = if @options.name.match(/\[/)
21
+ i = @options.name.lastIndexOf('[')
22
+ "#{@options.name.substring(0, i)}[remove_#{@options.name.substring(i + 1, @options.name.length)}"
23
+ else
24
+ "remove_#{@options.name}"
25
+
26
+ @removeParams = {}
27
+ @removeParams[removeKey] = true
28
+
29
+ @options.action ||= @$form.attr('action')
30
+ @options.method ||= @$form.find('[name=_method]').val() || @$form.attr('method')
31
+
32
+ @_bindEvents()
33
+
34
+ remove: ->
35
+ if @options.persisted
36
+ @_ajaxRemove()
37
+ else
38
+ @$input.val('')
39
+
40
+ @options.name = undefined
41
+ @$el.removeClass('is_uploaded')
42
+
43
+ _baseParams: ->
44
+ $.extend { pretty_file_input: true }, @options.additionalParams
45
+
46
+ _ajaxRemove: ->
47
+ $.ajax
48
+ url: @options.action
49
+ type: @options.method
50
+ dataType: 'json'
51
+ data: $.extend @_baseParams(), @removeParams
52
+
53
+ _ajaxUpload: ->
54
+ $tmpForm = @_createTemporaryForm()
55
+
56
+ $tmpForm.ajaxSubmit
57
+ dataType: 'json'
58
+ data: @_baseParams()
59
+ uploadProgress: (_, __, ___, percentComplete) =>
60
+ @$status.text(
61
+ if percentComplete == 100
62
+ 'Finishing'
63
+ else
64
+ "Uploading (#{percentComplete}%)"
65
+ )
66
+ complete: =>
67
+ @$input.show()
68
+ $tmpForm.remove()
69
+ success: $.proxy(@_uploadSuccess, @)
70
+ error: $.proxy(@_uploadError, @)
71
+
72
+ _createTemporaryForm: ->
73
+ form = $("""
74
+ <form action='#{@options.action}' method='post' style='display: inline;'>
75
+ <input type='hidden' name='_method' value='#{@options.method}' />
76
+ </form>
77
+ """)
78
+
79
+ $oldInput = @$input
80
+ @$input = $oldInput.clone().hide().val('').insertBefore($oldInput)
81
+ @_bindInputChange()
82
+ $oldInput.appendTo(form)
83
+ $oldInput.attr('name', @options.name)
84
+ form.insertBefore(@$input)
85
+
86
+ form
87
+
88
+ _uploadSuccess: (data) ->
89
+ if data?.additionalParams?
90
+ @options.additionalParams = data.additionalParams
91
+
92
+ @$status.text('')
93
+ @$el.addClass('is_uploaded')
94
+
95
+ _uploadError: ->
96
+ @$status.text 'Error'
97
+ setTimeout ( => @$status.text('') ), 2000
98
+
99
+ _eventToFilename: (e) ->
100
+ if e.target.files?
101
+ e.target.files[0].name
102
+ else if e.target.value
103
+ e.target.value.replace(/^.+\\/, '')
104
+
105
+ _onChange: (e) ->
106
+ @$filename.text @_eventToFilename(e)
107
+
108
+ if @options.persisted
109
+ @$status.text 'Uploading...'
110
+ @_ajaxUpload()
111
+ else
112
+ @_uploadSuccess()
113
+
114
+ _bindEvents: ->
115
+ @$el.on 'click', '[data-pfi-remove]', $.proxy(@remove, @)
116
+ @_bindInputChange()
117
+
118
+ # FF6 doesn't bubble the 'change' event, so we need to bind
119
+ # directly to the @$input. Since we need to re-bind later,
120
+ # we break this out into a separate method.
121
+ _bindInputChange: ->
122
+ @$input.on 'change', $.proxy(@_onChange, @)
123
+
124
+ $.fn.extend prettyFileInput: (option, args...) ->
125
+ @each ->
126
+ data = $(@).data('pretty-file-input')
127
+
128
+ if !data
129
+ $(@).data 'pretty-file-input', (data = new PrettyFileInput($(@), option))
130
+ if typeof option == 'string'
131
+ data[option].apply(data, args)
132
+
133
+ ) window.jQuery, window
134
+
135
+ $ ->
136
+ $('[data-pfi]').prettyFileInput()
@@ -0,0 +1,19 @@
1
+ // A more functional file input
2
+ .pfi {
3
+ input {
4
+ display: inline;
5
+ }
6
+ }
7
+
8
+ .pfi_uploaded{
9
+ display: none;
10
+ }
11
+
12
+ .is_uploaded {
13
+ .pfi_uploaded {
14
+ display: block;
15
+ }
16
+ .pfi_not_uploaded {
17
+ display: none;
18
+ }
19
+ }
@@ -0,0 +1,16 @@
1
+ class PrettyFileInputInput < SimpleForm::Inputs::Base
2
+ def input
3
+ @builder.multipart = true
4
+ PrettyFileInput::Component.new(
5
+ name: tag_name,
6
+ persisted: object.persisted?,
7
+ filename: object.try(:send, attribute_name).try(:file).try(:filename)
8
+ ).to_html
9
+ end
10
+
11
+ # Not sure why this is so goddamn hard to retrieve
12
+ def tag_name
13
+ ActionView::Helpers::Tags::Base.new(object_name, attribute_name, nil).
14
+ send(:tag_name)
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ require 'pretty_file_input/version'
2
+ require 'pretty_file_input/component'
3
+
4
+ module PrettyFileInput
5
+ class Engine < ::Rails::Engine
6
+
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ require 'erector'
2
+
3
+ class PrettyFileInput::Component < Erector::Widget
4
+ needs :name, # input name that will be sent to the server
5
+ persisted: false, # is parent object is not persisted, no AJAX calls will be made
6
+ filename: nil, # pre-populate the filename span
7
+ method: nil, # override the parent form's method
8
+ action: nil, # override the parent form's action
9
+ additional_params: {} # additional parameters to be sent to server with each request
10
+
11
+ def content
12
+ div(
13
+ class: "pfi cf #{@filename ? 'is_uploaded' : ''}",
14
+ 'data-pfi' => {
15
+ name: @name,
16
+ persisted: @persisted,
17
+ action: @action,
18
+ method: @method,
19
+ additionalParams: @additional_params
20
+ }.to_json
21
+ ) {
22
+ div.pfi_uploaded {
23
+ span.pfi_existing_filename @filename
24
+ text ' '
25
+ a.button.mini.info 'Remove', 'data-pfi-remove' => true
26
+ }
27
+ div.pfi_not_uploaded {
28
+ input type: 'file'
29
+ span.pfi_status
30
+ }
31
+ }
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module PrettyFileInput
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require 'pretty_file_input/version'
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = 'pretty_file_input'
9
+ s.version = PrettyFileInput::VERSION
10
+ s.required_ruby_version = Gem::Requirement.new('>= 2.0.0')
11
+ s.authors = ['Adam Becker']
12
+ s.summary = 'A more powerful file input.'
13
+ s.email = 'adam@dobt.co'
14
+ s.license = 'MIT'
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {features,spec}/*`.split("\n")
17
+ s.homepage = 'http://github.com/dobtco/pretty_file_input'
18
+
19
+ s.add_dependency 'coffee-script'
20
+ s.add_dependency 'erector-rails4'
21
+ s.add_dependency 'sass'
22
+ end
@@ -0,0 +1,38 @@
1
+ #!/bin/sh
2
+ # Tag and push a release.
3
+
4
+ set -e
5
+
6
+ # Make sure we're in the project root.
7
+
8
+ cd $(dirname "$0")/..
9
+
10
+ # Build a new gem archive.
11
+
12
+ rm -rf pretty_file_input-*.gem
13
+ gem build -q pretty_file_input.gemspec
14
+
15
+ # Make sure we're on the master branch.
16
+
17
+ (git branch | grep -q '* master') || {
18
+ echo "Only release from the master branch."
19
+ exit 1
20
+ }
21
+
22
+ # Figure out what version we're releasing.
23
+
24
+ tag=v`ls pretty_file_input-*.gem | sed 's/^pretty_file_input-\(.*\)\.gem$/\1/'`
25
+
26
+ # Make sure we haven't released this version before.
27
+
28
+ git fetch -t origin
29
+
30
+ (git tag -l | grep -q "$tag") && {
31
+ echo "Whoops, there's already a '${tag}' tag."
32
+ exit 1
33
+ }
34
+
35
+ # Tag it and bag it.
36
+
37
+ gem push pretty_file_input-*.gem && git tag "$tag" &&
38
+ git push origin master && git push origin "$tag"
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_file_input
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Becker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: coffee-script
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erector-rails4
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sass
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email: adam@dobt.co
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - ".ruby-version"
63
+ - Gemfile
64
+ - README.md
65
+ - app/assets/javascripts/pretty_file_input.coffee
66
+ - app/assets/stylesheets/pretty_file_input.scss
67
+ - app/inputs/pretty_file_input_input.rb
68
+ - lib/pretty_file_input.rb
69
+ - lib/pretty_file_input/component.rb
70
+ - lib/pretty_file_input/version.rb
71
+ - pretty_file_input.gemspec
72
+ - script/release
73
+ homepage: http://github.com/dobtco/pretty_file_input
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.2.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A more powerful file input.
97
+ test_files: []
98
+ has_rdoc: