ajaxify_rails 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,22 +2,24 @@
2
2
 
3
3
  No more full page reloads for your Rails app! Yay!
4
4
 
5
- Automatically makes your app loading content in the background via ajax.
5
+ Automatically makes your app loading content in the background via Ajax.
6
6
 
7
- Works by turning all internal links into ajax links that trigger an update of the page's content area.
8
- Also form submissions are automatically turned into ajax requests.
7
+ Works by turning all internal links into Ajax links that trigger an update of the page's content area.
8
+ Also form submissions are automatically turned into Ajax requests.
9
9
 
10
10
  Features:
11
11
 
12
- - Uses the html5 history interface for changing the url and making the browser's back and forward buttons work with ajax.
13
- - Falls back to a hash based approach for browsers without the history interface (like Internet Explorer version <10)
12
+ - Uses the html5 history interface for changing the url and making the browser's back and forward buttons work with Ajax.
13
+ - Falls back to a hash based approach for browsers without the history interface (like Internet Explorer version <10).
14
14
  - Hash based and non-hash URLs are interchangeable.
15
15
  - Transparently handles redirects and supports page titles and flash messages.
16
- - Tested with Chrome, Firefox, Safari and Internet Explorer 8+
16
+ - Tested with Chrome, Firefox, Safari and Internet Explorer 8+.
17
17
 
18
- Demo: http://ajaxify-demo.herokuapp.com/
18
+ Demo: http://ajaxify-demo.herokuapp.com/ (the first page load might take a while, as heroku needs to spin up a dyno)
19
19
 
20
- Inspired by the pjax_rails gem (https://github.com/rails/pjax_rails)
20
+ Blog Post: http://rubyandrails.posterous.com/introducing-the-ajaxify-rails-gem
21
+
22
+ Inspired by the pjax_rails gem (https://github.com/rails/pjax_rails).
21
23
 
22
24
  ## Requirements
23
25
 
@@ -64,16 +66,16 @@ You can change the content wrapper in your javascript by setting
64
66
 
65
67
  ### Loader Animation
66
68
 
67
- You probably like to have a loader image to be displayed to the user while content loads via ajax.
69
+ You probably like to have a loader image to be displayed to the user while content loads via Ajax.
68
70
  This is simple. Ajaxify automatically inserts a loader div with the class `ajaxify_loader` into
69
- the content wrapper before starting an ajax request. So just supply styles for `.ajaxify_loader` in your css, with an
71
+ the content wrapper before starting an Ajax request. So just supply styles for `.ajaxify_loader` in your css, with an
70
72
  animated gif as a background.
71
73
 
72
74
 
73
75
  ### Page Title
74
76
 
75
- If you define a method called `page_title` in your application controller, Ajaxify will automatically
76
- update the page's title tag after the main content has changed.
77
+ If you define a method called `page_title` in your application controller, returning the current page's title, Ajaxify will
78
+ use it to automatically update the title tag after the main content has changed.
77
79
 
78
80
  ### Navigation and other Layout Updates
79
81
 
@@ -87,24 +89,24 @@ updates of the page's main content. Just hook into it in your javascript and mak
87
89
 
88
90
  ### Flash Messages
89
91
 
90
- Ajaxify Rails correctly displays your flash messages after ajaxified requests. To do so it stores them in cookies.
92
+ Ajaxify correctly displays your flash messages after ajaxified requests.
91
93
  By default, only `flash[:notice]` is supported. If you are using for example `flash[:warning]` as well you have to set:
92
94
 
93
95
  Ajaxify.flash_types = ['notice', 'warning']
94
96
 
95
- Also make sure that you supply invisible wrapper tags in your layout with the flash type as its id, e.g.:
97
+ Also make sure that you supply invisible wrapper tags in your layout for each flash type you use, with the id set to the type, e.g.:
96
98
 
97
99
  #notice{ style: "#{'display:none' unless flash[:notice]}" }
98
100
  = flash[:notice]
99
101
 
100
- ### Links that need to trigger full Page Reloads
102
+ ### Links or Forms that need to trigger full Page Reloads
101
103
 
102
- We all know them. Those big requests changing the layout of the page so significantly that
103
- loading ajax into a content area and doing some minor layout tweaks here and there simply doesn't cut it. Sigh.
104
+ Sometimes requests change the layout of the page so significantly that loading only the main content via Ajax
105
+ and doing some minor layout tweaks here and there are simply not sufficient.
104
106
 
105
- There might also be links and forms which already have their own ajax functionality.
107
+ There might also be links and forms which already have their own Ajax functionality.
106
108
 
107
- To turn Ajaxify off for certain links and forms, simply add the class `no_ajaxify` directly to the link or form:
109
+ For these cases you can turn off Ajaxify, by simply adding the class `no_ajaxify` directly to links or forms:
108
110
 
109
111
  = link_to 'Change everything!', re_render_it_all_path, class: 'no_ajaxify'
110
112
 
@@ -156,7 +158,7 @@ Here is a reference of all options and callbacks you can set on the client side
156
158
 
157
159
  Option/Callback Default Description
158
160
 
159
- active true Toggles link ajaxification.
161
+ active true Switches Ajaxify on or off. Needs to be set before Ajaxify.init() is called.
160
162
  content_container 'main' Id of the container to insert the main content into ("yield wrapper").
161
163
  base_paths null Base path segments for applications with root url redirects.
162
164
 
data/lib/ajaxify_rails.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "ajaxify_rails/version"
2
+ require "ajaxify_rails/action_controller_additions"
2
3
 
3
4
  module AjaxifyRails
4
5
 
@@ -11,74 +12,4 @@ module AjaxifyRails
11
12
  include ActionControllerAdditions
12
13
  end
13
14
 
14
- module ActionControllerAdditions
15
-
16
- def self.included(controller)
17
- controller.class_eval do
18
-
19
- private
20
-
21
- def ajaxified?
22
- request.xhr? and params[:ajaxified]
23
- end
24
-
25
-
26
- def render *args, &block
27
- if ajaxified?
28
- args = _normalize_args(*args, &block)
29
- layout = args[:layout] || current_layout
30
- layout = (layout == 'application' or layout == true) ? false : layout
31
- args[:layout] = layout
32
-
33
- flash.keys.each do |key|
34
- cookies["flash_#{key}"] = flash[key]
35
- flash[key] = nil
36
- end
37
-
38
- extra_content = (respond_to?(:ajaxify_extra_content) ? ajaxify_extra_content : '')
39
-
40
- super args
41
-
42
- # Store current path for redirect url changes. Also used to remove the ajaxify parameter that gets added to some auto generated urls
43
- # like e.g. pagination links see (ajaxify.js -> on_ajaxify_success())
44
- #
45
- current_url_tag = view_context.content_tag(:span, request.fullpath.sub(/\?ajaxified=true&(.*)/, '?\1').sub(/(&|\?)ajaxified=true/, ''),
46
- id: 'ajaxify_location')
47
-
48
- response_body[0] += view_context.content_tag(:div, current_url_tag + extra_content,
49
- id: 'ajaxify_content', style: 'display:none',
50
- data: { page_title: respond_to?(:page_title) ? page_title : nil })
51
- response.body = self.response_body[0]
52
- return
53
- end
54
- super
55
- end
56
-
57
-
58
- def current_layout
59
- return @current_layout if @current_layout
60
- @current_layout = _layout
61
- @current_layout = File.basename(@current_layout.identifier).split('.').first unless @current_layout.instance_of? String
62
- @current_layout
63
- end
64
-
65
-
66
- def redirect_to(options = {}, response_status = {})
67
- request.referer.sub!('#/', '') if request.referer # make redirect to back work for browsers without history api
68
-
69
- super
70
-
71
- ajaxify_params = "ajaxified=true&ajaxify_redirect=true"
72
- self.location += "#{self.location =~ /\?/ ? '&' : '?'}#{ajaxify_params}" if request.xhr? # to avoid the full layout from being rendered
73
- end
74
-
75
-
76
- def ajaxify_redirect_to url
77
- render inline: "<%= javascript_tag(\"Ajaxify.load({url: '#{url}'});\") %>", layout: true
78
- end
79
- end
80
-
81
- end
82
- end
83
-
84
15
  end
@@ -0,0 +1,83 @@
1
+ module ActionControllerAdditions
2
+
3
+ def self.included(controller)
4
+ controller.class_eval do
5
+
6
+ # override in your controller
7
+ def page_title
8
+ nil
9
+ end
10
+
11
+ # override in your controller
12
+ def ajaxify_extra_content
13
+ ''
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def ajaxified?
20
+ request.xhr? and params[:ajaxified]
21
+ end
22
+
23
+
24
+ def render *args, &block
25
+ if ajaxified?
26
+ args = _normalize_args(*args, &block)
27
+ layout = args[:layout] || current_layout
28
+ layout = (layout == 'application' or layout == true) ? false : layout
29
+ args[:layout] = layout
30
+
31
+ flashes = {}
32
+ flash.keys.each do |key|
33
+ flashes[key] = flash[key]
34
+ flash[key] = nil
35
+ end
36
+
37
+
38
+ extra_content = ajaxify_extra_content
39
+
40
+ super args
41
+
42
+ # Store current path for redirect url changes. Also used to remove the ajaxify parameter that gets added to some auto generated urls
43
+ # like e.g. pagination links see (ajaxify.js -> on_ajaxify_success())
44
+ #
45
+ current_url_tag = view_context.content_tag(:span, request.fullpath.sub(/\?ajaxified=true&(.*)/, '?\1').sub(/(&|\?)ajaxified=true/, ''),
46
+ id: 'ajaxify_location')
47
+
48
+ response_body[0] += view_context.content_tag(:div, current_url_tag + extra_content,
49
+ id: 'ajaxify_content', style: 'display:none',
50
+ data: { page_title: page_title,
51
+ flashes: flashes.to_json } )
52
+ response.body = self.response_body[0]
53
+ return
54
+ end
55
+ super
56
+ end
57
+
58
+
59
+ def current_layout
60
+ return @current_layout if @current_layout
61
+ @current_layout = _layout
62
+ @current_layout = File.basename(@current_layout.identifier).split('.').first unless @current_layout.instance_of? String
63
+ @current_layout
64
+ end
65
+
66
+
67
+ def redirect_to(options = {}, response_status = {})
68
+ request.referer.sub!('#/', '') if request.referer # make redirect to back work for browsers without history api
69
+
70
+ super
71
+
72
+ ajaxify_params = "ajaxified=true&ajaxify_redirect=true"
73
+ self.location += "#{self.location =~ /\?/ ? '&' : '?'}#{ajaxify_params}" if request.xhr? # to avoid the full layout from being rendered
74
+ end
75
+
76
+
77
+ def ajaxify_redirect_to url
78
+ render inline: "<%= javascript_tag(\"Ajaxify.load({url: '#{url}'});\") %>", layout: true
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -1,3 +1,3 @@
1
1
  module AjaxifyRails
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -12,7 +12,6 @@
12
12
  on_before_load: null
13
13
  on_success: null
14
14
  on_success_once: null
15
- on_before_correct_url: null
16
15
 
17
16
  # flash
18
17
  #
@@ -141,13 +140,11 @@
141
140
  self.on_ajaxify_success data, status, jqXHR, pop_state, options
142
141
 
143
142
 
144
- show_flash: () ->
143
+ show_flashes: (flashes) ->
145
144
  self = this
146
145
  $.each this.flash_types, ->
147
- cookie_name = "flash_#{this}"
148
- if $.cookie cookie_name
149
- $("##{this}").html $.cookie(cookie_name)
150
- $.cookie cookie_name, null
146
+ if flashes and flashes[this]
147
+ $("##{this}").html flashes[this]
151
148
  $("##{this}").show()
152
149
  if self.flash_effect
153
150
  if self.clear_flash_effect
@@ -162,12 +159,13 @@
162
159
  $("##{this.content_container}").html data
163
160
 
164
161
  title = $('#ajaxify_content').data('page-title')
162
+ flashes = $('#ajaxify_content').data('flashes')
165
163
 
166
164
  # Correct the url after a redirect and when it has the ajaxify param in it.
167
165
  # The latter can happen e.g. for pagination links that are auto generated.
168
- current_url = $('#ajaxify_content #ajaxify_location').text()
166
+ current_url = $('#ajaxify_content #ajaxify_location').html()
169
167
  if options.url != current_url
170
- options.url = current_url.replace(/(&|\?)ajaxify_redirect=true/,'')
168
+ options.url = current_url.replace(/(&|&amp;|\?)ajaxify_redirect=true/,'')
171
169
  options.type = 'GET'
172
170
 
173
171
  this.update_url options, pop_state
@@ -180,7 +178,7 @@
180
178
  if title
181
179
  document.title = title.replace /&amp;/, '&' # Todo: need to figure out what else needs to be unescaped
182
180
 
183
- this.show_flash()
181
+ this.show_flashes(flashes)
184
182
 
185
183
  if this.on_success
186
184
  this.on_success( data, status, jqXHR, options.url )
@@ -237,6 +235,9 @@
237
235
  if this.active
238
236
 
239
237
  if window.location.hash.indexOf('#') == 0 # if url has a '#' in it treat it as a non history interface hash based scheme url
238
+
239
+ return unless window.location.hash.match(/^#(\/|\?)/) # check hash format
240
+
240
241
  if !window.history.pushState
241
242
  Ajaxify.load_page_from_hash = true # notify Ajaxify that a hash will be loaded and ignore all other calls to load until hash url is loaded
242
243
  else
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajaxify_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-29 00:00:00.000000000 Z
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -49,10 +49,9 @@ files:
49
49
  - Rakefile
50
50
  - ajaxify_rails.gemspec
51
51
  - lib/ajaxify_rails.rb
52
+ - lib/ajaxify_rails/action_controller_additions.rb
52
53
  - lib/ajaxify_rails/version.rb
53
- - vendor/assets/javascripts/ajaxify_rails/ajaxify_rails.js.coffee
54
- - vendor/assets/javascripts/ajaxify_rails/index.js
55
- - vendor/assets/javascripts/ajaxify_rails/jquery_cookie.js
54
+ - vendor/assets/javascripts/ajaxify_rails.js.coffee
56
55
  homepage: https://github.com/ncri/ajaxify_rails
57
56
  licenses: []
58
57
  post_install_message:
@@ -67,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
66
  version: '0'
68
67
  segments:
69
68
  - 0
70
- hash: 3891379638515987853
69
+ hash: -4098210962024842492
71
70
  required_rubygems_version: !ruby/object:Gem::Requirement
72
71
  none: false
73
72
  requirements:
@@ -76,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
75
  version: '0'
77
76
  segments:
78
77
  - 0
79
- hash: 3891379638515987853
78
+ hash: -4098210962024842492
80
79
  requirements: []
81
80
  rubyforge_project:
82
81
  rubygems_version: 1.8.24
@@ -1,2 +0,0 @@
1
- //= require ./jquery_cookie
2
- //= require ./ajaxify_rails
@@ -1,61 +0,0 @@
1
- /*jshint eqnull:true */
2
- /*!
3
- * jQuery Cookie Plugin v1.1
4
- * https://github.com/carhartl/jquery-cookie
5
- *
6
- * Copyright 2011, Klaus Hartl
7
- * Dual licensed under the MIT or GPL Version 2 licenses.
8
- * http://www.opensource.org/licenses/mit-license.php
9
- * http://www.opensource.org/licenses/GPL-2.0
10
- */
11
- (function($, document) {
12
-
13
- var pluses = /\+/g;
14
- function raw(s) {
15
- return s;
16
- }
17
- function decoded(s) {
18
- return decodeURIComponent(s.replace(pluses, ' '));
19
- }
20
-
21
- $.cookie = function(key, value, options) {
22
-
23
- // key and at least value given, set cookie...
24
- if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value == null)) {
25
- options = $.extend({}, $.cookie.defaults, options);
26
-
27
- if (value == null) {
28
- options.expires = -1;
29
- }
30
-
31
- if (typeof options.expires === 'number') {
32
- var days = options.expires, t = options.expires = new Date();
33
- t.setDate(t.getDate() + days);
34
- }
35
-
36
- value = String(value);
37
-
38
- return (document.cookie = [
39
- encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
40
- options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
41
- options.path ? '; path=' + options.path : '',
42
- options.domain ? '; domain=' + options.domain : '',
43
- options.secure ? '; secure' : ''
44
- ].join(''));
45
- }
46
-
47
- // key and possibly options given, get cookie...
48
- options = value || $.cookie.defaults || {};
49
- var decode = options.raw ? raw : decoded;
50
- var cookies = document.cookie.split('; ');
51
- for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
52
- if (decode(parts.shift()) === key) {
53
- return decode(parts.join('='));
54
- }
55
- }
56
- return null;
57
- };
58
-
59
- $.cookie.defaults = {};
60
-
61
- })(jQuery, document);