javascript_util_asset_pack 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in javascript_util_asset_pack.gemspec
4
+ gemspec
@@ -0,0 +1,52 @@
1
+ == Rails 3.1 javascript - Util asset pack
2
+
3
+ == Sets up a window.Util object which includes
4
+
5
+ * Spinner, with methods to set spinner next to element or hide the spinner
6
+ * AjaxForm, to ajax enable simple forms
7
+ * jQuery ajaxError global handler, exception data during development and friendly message in production
8
+
9
+ == Usage
10
+
11
+ spinner (js version)
12
+
13
+ window.Util.spinner.nextTo('#my_button');
14
+ window.Util.spinner.nextTo('#my_button', 3, 4); // selector, top offset, left offset
15
+ window.Util.spinner.hide();
16
+
17
+ ajax form (coffee script version)
18
+
19
+ jQuery ->
20
+ new window.Util.AjaxForm '#my_form', ->
21
+ log "my_form submit success callback"
22
+
23
+ == Install
24
+
25
+ Update the Gemfile in your rails project, add the following line
26
+
27
+ gem 'javascript_util_asset_pack'
28
+
29
+ Run the generator
30
+
31
+ rails generate javascript_util_asset_pack
32
+
33
+ does the following:
34
+
35
+ * Copy spinner.gif to /app/assets/images
36
+ * Update application.html.erb adding javascript create window.Rails.env variable
37
+ * Update application.html.erb adding image_tag for spinner.gif
38
+ * Update application.js adding util_pack
39
+
40
+ == Coming Soon
41
+
42
+ * configuration object so text in ajaxError can be overridden
43
+
44
+ == Resources
45
+
46
+ * spinner.gif generated using http://www.ajaxload.info
47
+
48
+ == License
49
+
50
+ The Unlicense (aka: public domain)
51
+ http://unlicense.org
52
+
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "javascript_util_asset_pack/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "javascript_util_asset_pack"
7
+ s.version = JavascriptUtilAssetPack::VERSION
8
+ s.authors = ["Jesse House"]
9
+ s.email = ["jesse.house@gmail.com"]
10
+ s.homepage = "https://github.com/house9/javascript_util_asset_pack"
11
+ s.summary = %q{
12
+ Rails 3.1 javascript Util asset pack, show hide spinner, global jquery error handler, etc...
13
+ }
14
+ s.description = File.open('README.rdoc') { |f| f.read }
15
+
16
+ s.rubyforge_project = "javascript_util_asset_pack"
17
+
18
+ s.add_dependency 'rails', '~> 3.1.0.rc'
19
+ s.add_dependency 'javascript_safe_logger', '~> 0.0.3'
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate javascript_util_asset_pack
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,37 @@
1
+ class JavascriptUtilAssetPackGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def manifest
5
+ copy_spinner_gif
6
+ update_layout_add_spinner
7
+ update_layout_add_rails_env_to_js
8
+ update_application_js
9
+ end
10
+
11
+ private
12
+
13
+ def update_application_js
14
+ inject_into_file "app/assets/javascripts/application.js", "//= require util_pack\n", :before => "//= require_tree .\n",
15
+ end
16
+
17
+ def copy_spinner_gif
18
+ copy_file 'spinner.gif', 'app/assets/images/spinner.gif'
19
+ end
20
+
21
+ def update_layout_add_spinner
22
+ inject_into_file "app/views/layouts/application.html.erb", :before => "</body>\n" do
23
+ " <%= image_tag 'spinner.gif', :alt => 'Spinner Icon', :id => 'spinner', :style => 'display:none' %>\n"
24
+ end
25
+ end
26
+
27
+ def update_layout_add_rails_env_to_js
28
+ inject_into_file "app/views/layouts/application.html.erb", :before => "</head>\n" do
29
+ <<-CONTENT
30
+ <script>
31
+ window.Rails = window.Rails || {};
32
+ window.Rails.env = "<%= Rails.env.upcase %>";
33
+ </script>
34
+ CONTENT
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module JavascriptUtilAssetPack
2
+ require 'javascript_util_asset_pack/engine'
3
+ end
@@ -0,0 +1,5 @@
1
+ module JavascriptUtilAssetPack
2
+ class Engine < Rails::Engine
3
+ # auto-wire to rails host
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module JavascriptUtilAssetPack
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,15 @@
1
+ class AjaxForm
2
+ constructor: (selector, callback = null) ->
3
+ $(selector).submit (e) ->
4
+ e.preventDefault()
5
+ form = this
6
+ success = if callback? then callback else () -> log "no success callback"
7
+
8
+ $.ajax $(form).attr('action'),
9
+ type: "POST"
10
+ data: $(form).serialize()
11
+ success: success
12
+
13
+ window.Util ?= {}
14
+ window.Util.AjaxForm = AjaxForm
15
+
@@ -0,0 +1,3 @@
1
+ //= require ./spinner
2
+ //= require ./ajax_form
3
+ //= require ./jquery_ajax_error
@@ -0,0 +1,33 @@
1
+ jQuery(document).ajaxError (event, xhr, settings, exception) ->
2
+ baseMessage = "Error: "
3
+ friendly = baseMessage
4
+ local = "#{baseMessage}#{xhr.status}, #{xhr.statusText} #{exception}\n#{settings.url}"
5
+
6
+ if xhr.status is 404
7
+ friendly = "#{friendly}Page not found"
8
+ else
9
+ friendly = "#{friendly}an exception has occurred\nPlease try again"
10
+
11
+ window.Util.spinner.hide()
12
+
13
+ if window.Rails?.env is "DEVELOPMENT"
14
+ contentType = xhr.getResponseHeader("content-type")
15
+ if contentType.indexOf('html') > -1
16
+ body = "<div>#{xhr.responseText}</div>"
17
+ fragment = $(body).find "h1"
18
+ local = "#{local} \n#{fragment.text()}"
19
+ fragment = $(body).find "pre"
20
+
21
+ if fragment.length > 0
22
+ fragment.each () -> local = "#{local}\n#{$(this).text()}"
23
+
24
+ else
25
+ # json, text, xml, etc... add specific parsing later if needed
26
+ local = "#{local}"
27
+
28
+ lengthOfVisibleErrorMessage = if local.length > 350 then 350 else local.length
29
+ message = local.substring(0, lengthOfVisibleErrorMessage)
30
+ alert "#{message}...\n\n - Check browser console for more info.\n - This message for development only."
31
+
32
+ else
33
+ alert friendly
@@ -0,0 +1,31 @@
1
+ class Spinner
2
+ constructor: (spinnerId) ->
3
+ @spinnerId = spinnerId
4
+
5
+ hide: () ->
6
+ jQuery(@spinnerId).hide()
7
+
8
+ nextTo: (controlID, topOffset = 0, leftOffset = 14) ->
9
+ if controlID?
10
+ $control = jQuery(controlID)
11
+ return null if $control.length is 0
12
+
13
+ offset = $control.offset()
14
+ top = parseInt(offset.top + topOffset)
15
+ left = parseInt(offset.left + leftOffset + $control.width())
16
+
17
+ # sometimes things are not what they seem, use the parent coordinates if needed
18
+ if left < 0
19
+ left = jQuery(controlID).parent().offset().left + leftOffset
20
+
21
+ # show spinner
22
+ jQuery(@spinnerId)
23
+ .css('position', 'absolute')
24
+ .css('top', "#{top}px")
25
+ .css('left', "#{left}px ")
26
+ .show()
27
+ .css('z-index', 33010)
28
+
29
+ window.Util ?= {}
30
+ window.Util.spinner = new Spinner('#spinner')
31
+
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: javascript_util_asset_pack
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Jesse House
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-28 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 3.1.0.rc
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: javascript_safe_logger
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 0.0.3
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: |+
38
+ == Rails 3.1 javascript - Util asset pack
39
+
40
+ == Sets up a window.Util object which includes
41
+
42
+ * Spinner, with methods to set spinner next to element or hide the spinner
43
+ * AjaxForm, to ajax enable simple forms
44
+ * jQuery ajaxError global handler, exception data during development and friendly message in production
45
+
46
+ == Usage
47
+
48
+ spinner (js version)
49
+
50
+ window.Util.spinner.nextTo('#my_button');
51
+ window.Util.spinner.nextTo('#my_button', 3, 4); // selector, top offset, left offset
52
+ window.Util.spinner.hide();
53
+
54
+ ajax form (coffee script version)
55
+
56
+ jQuery ->
57
+ new window.Util.AjaxForm '#my_form', ->
58
+ log "my_form submit success callback"
59
+
60
+ == Install
61
+
62
+ Update the Gemfile in your rails project, add the following line
63
+
64
+ gem 'javascript_util_asset_pack'
65
+
66
+ Run the generator
67
+
68
+ rails generate javascript_util_asset_pack
69
+
70
+ does the following:
71
+
72
+ * Copy spinner.gif to /app/assets/images
73
+ * Update application.html.erb adding javascript create window.Rails.env variable
74
+ * Update application.html.erb adding image_tag for spinner.gif
75
+ * Update application.js adding util_pack
76
+
77
+ == Coming Soon
78
+
79
+ * configuration object so text in ajaxError can be overridden
80
+
81
+ == Resources
82
+
83
+ * spinner.gif generated using http://www.ajaxload.info
84
+
85
+ == License
86
+
87
+ The Unlicense (aka: public domain)
88
+ http://unlicense.org
89
+
90
+ email:
91
+ - jesse.house@gmail.com
92
+ executables: []
93
+
94
+ extensions: []
95
+
96
+ extra_rdoc_files: []
97
+
98
+ files:
99
+ - .gitignore
100
+ - Gemfile
101
+ - README.rdoc
102
+ - Rakefile
103
+ - javascript_util_asset_pack.gemspec
104
+ - lib/generators/javascript_util_asset_pack/USAGE
105
+ - lib/generators/javascript_util_asset_pack/javascript_util_asset_pack_generator.rb
106
+ - lib/generators/javascript_util_asset_pack/templates/spinner.gif
107
+ - lib/javascript_util_asset_pack.rb
108
+ - lib/javascript_util_asset_pack/engine.rb
109
+ - lib/javascript_util_asset_pack/version.rb
110
+ - vendor/assets/javascripts/util_pack/ajax_form.coffee
111
+ - vendor/assets/javascripts/util_pack/index.js
112
+ - vendor/assets/javascripts/util_pack/jquery_ajax_error.coffee
113
+ - vendor/assets/javascripts/util_pack/spinner.coffee
114
+ homepage: https://github.com/house9/javascript_util_asset_pack
115
+ licenses: []
116
+
117
+ post_install_message:
118
+ rdoc_options: []
119
+
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: "0"
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: "0"
134
+ requirements: []
135
+
136
+ rubyforge_project: javascript_util_asset_pack
137
+ rubygems_version: 1.8.5
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Rails 3.1 javascript Util asset pack, show hide spinner, global jquery error handler, etc...
141
+ test_files: []
142
+