historyjs-rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/Gemfile +3 -0
- data/LICENSE +9 -0
- data/README.md +84 -0
- data/Rakefile +5 -0
- data/historyjs-rails.gemspec +28 -0
- data/lib/generators/historyjs/install/install_generator.rb +72 -0
- data/lib/historyjs-rails.rb +1 -0
- data/lib/historyjs/rails.rb +10 -0
- data/lib/historyjs/rails/engine.rb +6 -0
- data/lib/historyjs/rails/railtie.rb +6 -0
- data/lib/historyjs/rails/version.rb +6 -0
- data/spec/historyjs/rails/version_spec.rb +6 -0
- data/spec/spec_helper.rb +3 -0
- data/vendor/assets/javascripts/history_adapter_jquery.js +77 -0
- data/vendor/assets/javascripts/history_adapter_jquery.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_mootools.js +84 -0
- data/vendor/assets/javascripts/history_adapter_mootools.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_native.js +121 -0
- data/vendor/assets/javascripts/history_adapter_native.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_right.js +78 -0
- data/vendor/assets/javascripts/history_adapter_right.min.js +1 -0
- data/vendor/assets/javascripts/history_adapter_zepto.js +74 -0
- data/vendor/assets/javascripts/history_adapter_zepto.min.js +1 -0
- data/vendor/assets/javascripts/history_core.js +1943 -0
- data/vendor/assets/javascripts/history_core.min.js +1 -0
- data/vendor/assets/javascripts/history_html4.js +621 -0
- data/vendor/assets/javascripts/history_html4.min.js +1 -0
- data/vendor/assets/javascripts/history_jquery.js +4 -0
- data/vendor/assets/javascripts/history_jquery.min.js +4 -0
- data/vendor/assets/javascripts/history_jquery_html5.js +3 -0
- data/vendor/assets/javascripts/history_jquery_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_mootools.js +4 -0
- data/vendor/assets/javascripts/history_mootools.min.js +4 -0
- data/vendor/assets/javascripts/history_mootools_html5.js +3 -0
- data/vendor/assets/javascripts/history_mootools_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_native.js +4 -0
- data/vendor/assets/javascripts/history_native.min.js +4 -0
- data/vendor/assets/javascripts/history_native_html5.js +3 -0
- data/vendor/assets/javascripts/history_native_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_right.js +4 -0
- data/vendor/assets/javascripts/history_right.min.js +4 -0
- data/vendor/assets/javascripts/history_right_html5.js +3 -0
- data/vendor/assets/javascripts/history_right_html5.min.js +3 -0
- data/vendor/assets/javascripts/history_zepto.js +4 -0
- data/vendor/assets/javascripts/history_zepto.min.js +4 -0
- data/vendor/assets/javascripts/history_zepto_html5.js +3 -0
- data/vendor/assets/javascripts/history_zepto_html5.min.js +3 -0
- data/vendor/assets/javascripts/json2.js +480 -0
- data/vendor/assets/javascripts/json2.min.js +1 -0
- metadata +117 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (C) 2012 Phil Ostler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# History.js for Rails ![Build Status][travis_ci_build_status]
|
2
|
+
|
3
|
+
Provides History.js (1.7.1) for use with Rails 3
|
4
|
+
|
5
|
+
[RubyGems][ruby_gems] | [Ruby Toolbox][ruby_toolbox] | [GitHub][github] | [Travis CI][travis_ci] | [RubyDoc][ruby_doc]
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
### Rails 3.1+
|
9
|
+
To use History.js with your Rails 3.1+ application, add the following to your Gemfile
|
10
|
+
|
11
|
+
```
|
12
|
+
gem "historyjs-rails"
|
13
|
+
```
|
14
|
+
Run ```bundle install``` and History.js will be available for you to use via the asset pipeline. Add one of the following lines (depending on your framework) into your ```app/assets/javascripts/application.js``` file...
|
15
|
+
|
16
|
+
```
|
17
|
+
# HTML4 & 5 support (with hash fallback)
|
18
|
+
//= require history_jquery # jQuery 1.3+
|
19
|
+
//= require history_mootools # Mootools 1.3+
|
20
|
+
//= require history_right # Right.js 2.2+
|
21
|
+
//= require history_zepto # Zepto 0.5+
|
22
|
+
//= require history_native # For everything else
|
23
|
+
|
24
|
+
# HTML5 only support
|
25
|
+
//= require history_jquery_html5 # jQuery 1.3+
|
26
|
+
//= require history_mootools_html5 # Mootools 1.3+
|
27
|
+
//= require history_right_html5 # Right.js 2.2+
|
28
|
+
//= require history_zepto_html5 # Zepto 0.5+
|
29
|
+
//= require history_native_html5 # For everything else
|
30
|
+
```
|
31
|
+
...or for the minified version in development
|
32
|
+
|
33
|
+
```
|
34
|
+
# HTML4 & 5 support (with hash fallback)
|
35
|
+
//= require history_jquery.min # jQuery 1.3+
|
36
|
+
//= require history_mootools.min # Mootools 1.3+
|
37
|
+
//= require history_right.min # Right.js 2.2+
|
38
|
+
//= require history_zepto.min # Zepto 0.5+
|
39
|
+
//= require history_native.min # For everything else
|
40
|
+
|
41
|
+
# HTML5 only support
|
42
|
+
//= require history_jquery_html5.min # jQuery 1.3+
|
43
|
+
//= require history_mootools_html5.min # Mootools 1.3+
|
44
|
+
//= require history_right_html5.min # Right.js 2.2+
|
45
|
+
//= require history_zepto_html5.min # Zepto 0.5+
|
46
|
+
//= require history_native_html5.min # For everything else
|
47
|
+
```
|
48
|
+
History.js is now installed. Woop!
|
49
|
+
|
50
|
+
### Rails 3.0
|
51
|
+
To use History.js with your Rails 3.0 application, add the following to your Gemfile
|
52
|
+
|
53
|
+
```
|
54
|
+
gem "historyjs-rails"
|
55
|
+
```
|
56
|
+
Run ```bundle install``` followed by the install generator (with the desired framework option)
|
57
|
+
|
58
|
+
```
|
59
|
+
# HTML4 & 5 support (with hash fallback)
|
60
|
+
rails generate historyjs:install --jquery # jQuery 1.3+
|
61
|
+
rails generate historyjs:install --mootools # Mootools 1.3+
|
62
|
+
rails generate historyjs:install --right # Right.js 2.2+
|
63
|
+
rails generate historyjs:install --zepto # Zepto 0.5+
|
64
|
+
rails generate historyjs:install --native # For everything else
|
65
|
+
|
66
|
+
# HTML5 only support
|
67
|
+
rails generate historyjs:install --jquery --html5 # jQuery 1.3+
|
68
|
+
rails generate historyjs:install --mootools --html5 # Mootools 1.3+
|
69
|
+
rails generate historyjs:install --right --html5 # Right.js 2.2+
|
70
|
+
rails generate historyjs:install --zepto --html5 # Zepto 0.5+
|
71
|
+
rails generate historyjs:install --native --html5 # For everything else
|
72
|
+
```
|
73
|
+
History.js is now installed. Woop!
|
74
|
+
|
75
|
+
##History.js Resources
|
76
|
+
[GitHub][historyjs_github]
|
77
|
+
|
78
|
+
[github]: http://github.com/philostler/historyjs-rails
|
79
|
+
[ruby_doc]: http://rubydoc.info/github/philostler/historyjs-rails/master/frames
|
80
|
+
[ruby_gems]: http://rubygems.org/gems/historyjs-rails
|
81
|
+
[travis_ci]: http://travis-ci.org/philostler/historyjs-rails
|
82
|
+
[travis_ci_build_status]: https://secure.travis-ci.org/philostler/historyjs-rails.png
|
83
|
+
[ruby_toolbox]: http://www.ruby-toolbox.com/projects/historyjs-rails
|
84
|
+
[historyjs_github]: http://github.com/balupton/history.js
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/historyjs/rails/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "historyjs-rails"
|
6
|
+
s.version = Historyjs::Rails::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.author = "Phil Ostler"
|
9
|
+
s.email = "philostler@gmail.com"
|
10
|
+
s.homepage = "http://github.com/philostler/historyjs-rails"
|
11
|
+
s.summary = %q{History.js for Rails}
|
12
|
+
s.description = %q{Provides History.js for use with Rails 3}
|
13
|
+
|
14
|
+
s.add_dependency "railties", "~> 3.0"
|
15
|
+
|
16
|
+
s.add_development_dependency "rspec", "~> 2.0"
|
17
|
+
|
18
|
+
s.files = Dir[".gitignore"] +
|
19
|
+
Dir[".rspec"] +
|
20
|
+
Dir["Gemfile"] +
|
21
|
+
Dir["historyjs-rails.gemspec"] +
|
22
|
+
Dir["LICENSE"] +
|
23
|
+
Dir["Rakefile"] +
|
24
|
+
Dir["README.md"] +
|
25
|
+
Dir["**/*.js"] +
|
26
|
+
Dir["**/*.rb"]
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "rails"
|
2
|
+
|
3
|
+
module Historyjs
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
desc "This generator installs History.js (#{Historyjs::Rails::HISTORYJS_VERSION})"
|
7
|
+
class_option :jquery, :type => :boolean, :default => false, :desc => "For jQuery 1.3+"
|
8
|
+
class_option :mootools, :type => :boolean, :default => false, :desc => "For Mootools 1.3+"
|
9
|
+
class_option :right, :type => :boolean, :default => false, :desc => "For Right.js 2.2+"
|
10
|
+
class_option :zepto, :type => :boolean, :default => false, :desc => "For Zepto 0.5+"
|
11
|
+
class_option :native, :type => :boolean, :default => false, :desc => "For everything else"
|
12
|
+
class_option :html5, :type => :boolean, :default => false, :desc => "For HTML5 only support"
|
13
|
+
source_root File.expand_path "../../../../../vendor/assets/javascripts", __FILE__
|
14
|
+
|
15
|
+
def copy_historyjs
|
16
|
+
say_status "copying", "History.js (#{Historyjs::Rails::HISTORYJS_VERSION})", :green
|
17
|
+
|
18
|
+
copy_file "json2.js", "public/javascripts/json2.js"
|
19
|
+
copy_file "json2.min.js", "public/javascripts/json2.min.js"
|
20
|
+
copy_file "history_core.js", "public/javascripts/history_core.js"
|
21
|
+
copy_file "history_core.min.js", "public/javascripts/history_core.min.js"
|
22
|
+
|
23
|
+
unless options.html5?
|
24
|
+
copy_file "history_html4.js", "public/javascripts/history_html4.js"
|
25
|
+
copy_file "history_html4.min.js", "public/javascripts/history_html4.min.js"
|
26
|
+
|
27
|
+
if options.jquery?
|
28
|
+
copy_file "history_jquery.js", "public/javascripts/history_jquery.js"
|
29
|
+
copy_file "history_jquery.min.js", "public/javascripts/history_jquery.min.js"
|
30
|
+
end
|
31
|
+
if options.mootools?
|
32
|
+
copy_file "history_mootools.js", "public/javascripts/history_mootools.js"
|
33
|
+
copy_file "history_mootools.min.js", "public/javascripts/history_mootools.min.js"
|
34
|
+
end
|
35
|
+
if options.right?
|
36
|
+
copy_file "history_right.js", "public/javascripts/history_right.js"
|
37
|
+
copy_file "history_right.min.js", "public/javascripts/history_right.min.js"
|
38
|
+
end
|
39
|
+
if options.zepto?
|
40
|
+
copy_file "history_zepto.js", "public/javascripts/history_zepto.js"
|
41
|
+
copy_file "history_zepto.min.js", "public/javascripts/history_zepto.min.js"
|
42
|
+
end
|
43
|
+
if options.native?
|
44
|
+
copy_file "history_native.js", "public/javascripts/history_native.js"
|
45
|
+
copy_file "history_native.min.js", "public/javascripts/history_native.min.js"
|
46
|
+
end
|
47
|
+
else
|
48
|
+
if options.jquery?
|
49
|
+
copy_file "history_jquery_html5.js", "public/javascripts/history_jquery_html5.js"
|
50
|
+
copy_file "history_jquery_html5.min.js", "public/javascripts/history_jquery_html5.min.js"
|
51
|
+
end
|
52
|
+
if options.mootools?
|
53
|
+
copy_file "history_mootools_html5.js", "public/javascripts/history_mootools_html5.js"
|
54
|
+
copy_file "history_mootools_html5.min.js", "public/javascripts/history_mootools_html5.min.js"
|
55
|
+
end
|
56
|
+
if options.right?
|
57
|
+
copy_file "history_right_html5.js", "public/javascripts/history_right_html5.js"
|
58
|
+
copy_file "history_right_html5.min.js", "public/javascripts/history_right_html5.min.js"
|
59
|
+
end
|
60
|
+
if options.zepto?
|
61
|
+
copy_file "history_zepto_html5.js", "public/javascripts/history_zepto_html5.js"
|
62
|
+
copy_file "history_zepto_html5.min.js", "public/javascripts/history_zepto_html5.min.js"
|
63
|
+
end
|
64
|
+
if options.native?
|
65
|
+
copy_file "history_native_html5.js", "public/javascripts/history_native_html5.js"
|
66
|
+
copy_file "history_native_html5.min.js", "public/javascripts/history_native_html5.min.js"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end if ::Rails.version < 3.1
|
@@ -0,0 +1 @@
|
|
1
|
+
require "historyjs/rails"
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
/**
|
2
|
+
* History.js jQuery Adapter
|
3
|
+
* @author Benjamin Arthur Lupton <contact@balupton.com>
|
4
|
+
* @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
|
5
|
+
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
|
6
|
+
*/
|
7
|
+
|
8
|
+
// Closure
|
9
|
+
(function(window,undefined){
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
// Localise Globals
|
13
|
+
var
|
14
|
+
History = window.History = window.History||{},
|
15
|
+
jQuery = window.jQuery;
|
16
|
+
|
17
|
+
// Check Existence
|
18
|
+
if ( typeof History.Adapter !== 'undefined' ) {
|
19
|
+
throw new Error('History.js Adapter has already been loaded...');
|
20
|
+
}
|
21
|
+
|
22
|
+
// Add the Adapter
|
23
|
+
History.Adapter = {
|
24
|
+
/**
|
25
|
+
* History.Adapter.bind(el,event,callback)
|
26
|
+
* @param {Element|string} el
|
27
|
+
* @param {string} event - custom and standard events
|
28
|
+
* @param {function} callback
|
29
|
+
* @return {void}
|
30
|
+
*/
|
31
|
+
bind: function(el,event,callback){
|
32
|
+
jQuery(el).bind(event,callback);
|
33
|
+
},
|
34
|
+
|
35
|
+
/**
|
36
|
+
* History.Adapter.trigger(el,event)
|
37
|
+
* @param {Element|string} el
|
38
|
+
* @param {string} event - custom and standard events
|
39
|
+
* @param {Object=} extra - a object of extra event data (optional)
|
40
|
+
* @return {void}
|
41
|
+
*/
|
42
|
+
trigger: function(el,event,extra){
|
43
|
+
jQuery(el).trigger(event,extra);
|
44
|
+
},
|
45
|
+
|
46
|
+
/**
|
47
|
+
* History.Adapter.extractEventData(key,event,extra)
|
48
|
+
* @param {string} key - key for the event data to extract
|
49
|
+
* @param {string} event - custom and standard events
|
50
|
+
* @param {Object=} extra - a object of extra event data (optional)
|
51
|
+
* @return {mixed}
|
52
|
+
*/
|
53
|
+
extractEventData: function(key,event,extra){
|
54
|
+
// jQuery Native then jQuery Custom
|
55
|
+
var result = (event && event.originalEvent && event.originalEvent[key]) || (extra && extra[key]) || undefined;
|
56
|
+
|
57
|
+
// Return
|
58
|
+
return result;
|
59
|
+
},
|
60
|
+
|
61
|
+
/**
|
62
|
+
* History.Adapter.onDomLoad(callback)
|
63
|
+
* @param {function} callback
|
64
|
+
* @return {void}
|
65
|
+
*/
|
66
|
+
onDomLoad: function(callback) {
|
67
|
+
jQuery(callback);
|
68
|
+
}
|
69
|
+
};
|
70
|
+
|
71
|
+
// Try and Initialise History
|
72
|
+
if ( typeof History.init !== 'undefined' ) {
|
73
|
+
History.init();
|
74
|
+
}
|
75
|
+
|
76
|
+
})(window);
|
77
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
(function(a,b){"use strict";var c=a.History=a.History||{},d=a.jQuery;if(typeof c.Adapter!="undefined")throw new Error("History.js Adapter has already been loaded...");c.Adapter={bind:function(a,b,c){d(a).bind(b,c)},trigger:function(a,b,c){d(a).trigger(b,c)},extractEventData:function(a,c,d){var e=c&&c.originalEvent&&c.originalEvent[a]||d&&d[a]||b;return e},onDomLoad:function(a){d(a)}},typeof c.init!="undefined"&&c.init()})(window)
|
@@ -0,0 +1,84 @@
|
|
1
|
+
/**
|
2
|
+
* History.js MooTools Adapter
|
3
|
+
* @author Benjamin Arthur Lupton <contact@balupton.com>
|
4
|
+
* @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
|
5
|
+
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
|
6
|
+
*/
|
7
|
+
|
8
|
+
// Closure
|
9
|
+
(function(window,undefined){
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
// Localise Globals
|
13
|
+
var
|
14
|
+
History = window.History = window.History||{},
|
15
|
+
MooTools = window.MooTools,
|
16
|
+
Element = window.Element;
|
17
|
+
|
18
|
+
// Check Existence
|
19
|
+
if ( typeof History.Adapter !== 'undefined' ) {
|
20
|
+
throw new Error('History.js Adapter has already been loaded...');
|
21
|
+
}
|
22
|
+
|
23
|
+
// Make MooTools aware of History.js Events
|
24
|
+
Object.append(Element.NativeEvents,{
|
25
|
+
'popstate':2,
|
26
|
+
'hashchange':2
|
27
|
+
});
|
28
|
+
|
29
|
+
// Add the Adapter
|
30
|
+
History.Adapter = {
|
31
|
+
/**
|
32
|
+
* History.Adapter.bind(el,event,callback)
|
33
|
+
* @param {Element|string} el
|
34
|
+
* @param {string} event - custom and standard events
|
35
|
+
* @param {function} callback
|
36
|
+
* @return {void}
|
37
|
+
*/
|
38
|
+
bind: function(el,event,callback){
|
39
|
+
var El = typeof el === 'string' ? document.id(el) : el;
|
40
|
+
El.addEvent(event,callback);
|
41
|
+
},
|
42
|
+
|
43
|
+
/**
|
44
|
+
* History.Adapter.trigger(el,event)
|
45
|
+
* @param {Element|string} el
|
46
|
+
* @param {string} event - custom and standard events
|
47
|
+
* @param {Object=} extra - a object of extra event data (optional)
|
48
|
+
* @return void
|
49
|
+
*/
|
50
|
+
trigger: function(el,event,extra){
|
51
|
+
var El = typeof el === 'string' ? document.id(el) : el;
|
52
|
+
El.fireEvent(event,extra);
|
53
|
+
},
|
54
|
+
|
55
|
+
/**
|
56
|
+
* History.Adapter.extractEventData(key,event,extra)
|
57
|
+
* @param {string} key - key for the event data to extract
|
58
|
+
* @param {string} event - custom and standard events
|
59
|
+
* @return {mixed}
|
60
|
+
*/
|
61
|
+
extractEventData: function(key,event){
|
62
|
+
// MooTools Native then MooTools Custom
|
63
|
+
var result = (event && event.event && event.event[key]) || (event && event[key]) || undefined;
|
64
|
+
|
65
|
+
// Return
|
66
|
+
return result;
|
67
|
+
},
|
68
|
+
|
69
|
+
/**
|
70
|
+
* History.Adapter.onDomLoad(callback)
|
71
|
+
* @param {function} callback
|
72
|
+
* @return {void}
|
73
|
+
*/
|
74
|
+
onDomLoad: function(callback) {
|
75
|
+
window.addEvent('domready',callback);
|
76
|
+
}
|
77
|
+
};
|
78
|
+
|
79
|
+
// Try and Initialise History
|
80
|
+
if ( typeof History.init !== 'undefined' ) {
|
81
|
+
History.init();
|
82
|
+
}
|
83
|
+
|
84
|
+
})(window);
|
@@ -0,0 +1 @@
|
|
1
|
+
(function(a,b){"use strict";var c=a.History=a.History||{},d=a.MooTools,e=a.Element;if(typeof c.Adapter!="undefined")throw new Error("History.js Adapter has already been loaded...");Object.append(e.NativeEvents,{popstate:2,hashchange:2}),c.Adapter={bind:function(a,b,c){var d=typeof a=="string"?document.id(a):a;d.addEvent(b,c)},trigger:function(a,b,c){var d=typeof a=="string"?document.id(a):a;d.fireEvent(b,c)},extractEventData:function(a,c){var d=c&&c.event&&c.event[a]||c&&c[a]||b;return d},onDomLoad:function(b){a.addEvent("domready",b)}},typeof c.init!="undefined"&&c.init()})(window)
|
@@ -0,0 +1,121 @@
|
|
1
|
+
/**
|
2
|
+
* History.js Native Adapter
|
3
|
+
* @author Benjamin Arthur Lupton <contact@balupton.com>
|
4
|
+
* @copyright 2010-2011 Benjamin Arthur Lupton <contact@balupton.com>
|
5
|
+
* @license New BSD License <http://creativecommons.org/licenses/BSD/>
|
6
|
+
*/
|
7
|
+
|
8
|
+
// Closure
|
9
|
+
(function(window,undefined){
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
// Localise Globals
|
13
|
+
var History = window.History = window.History||{};
|
14
|
+
|
15
|
+
// Check Existence
|
16
|
+
if ( typeof History.Adapter !== 'undefined' ) {
|
17
|
+
throw new Error('History.js Adapter has already been loaded...');
|
18
|
+
}
|
19
|
+
|
20
|
+
// Add the Adapter
|
21
|
+
History.Adapter = {
|
22
|
+
/**
|
23
|
+
* History.Adapter.handlers[uid][eventName] = Array
|
24
|
+
*/
|
25
|
+
handlers: {},
|
26
|
+
|
27
|
+
/**
|
28
|
+
* History.Adapter._uid
|
29
|
+
* The current element unique identifier
|
30
|
+
*/
|
31
|
+
_uid: 1,
|
32
|
+
|
33
|
+
/**
|
34
|
+
* History.Adapter.uid(element)
|
35
|
+
* @param {Element} element
|
36
|
+
* @return {String} uid
|
37
|
+
*/
|
38
|
+
uid: function(element){
|
39
|
+
return element._uid || (element._uid = History.Adapter._uid++);
|
40
|
+
},
|
41
|
+
|
42
|
+
/**
|
43
|
+
* History.Adapter.bind(el,event,callback)
|
44
|
+
* @param {Element} element
|
45
|
+
* @param {String} eventName - custom and standard events
|
46
|
+
* @param {Function} callback
|
47
|
+
* @return
|
48
|
+
*/
|
49
|
+
bind: function(element,eventName,callback){
|
50
|
+
// Prepare
|
51
|
+
var uid = History.Adapter.uid(element);
|
52
|
+
|
53
|
+
// Apply Listener
|
54
|
+
History.Adapter.handlers[uid] = History.Adapter.handlers[uid] || {};
|
55
|
+
History.Adapter.handlers[uid][eventName] = History.Adapter.handlers[uid][eventName] || [];
|
56
|
+
History.Adapter.handlers[uid][eventName].push(callback);
|
57
|
+
|
58
|
+
// Bind Global Listener
|
59
|
+
element['on'+eventName] = (function(element,eventName){
|
60
|
+
return function(event){
|
61
|
+
History.Adapter.trigger(element,eventName,event);
|
62
|
+
};
|
63
|
+
})(element,eventName);
|
64
|
+
},
|
65
|
+
|
66
|
+
/**
|
67
|
+
* History.Adapter.trigger(el,event)
|
68
|
+
* @param {Element} element
|
69
|
+
* @param {String} eventName - custom and standard events
|
70
|
+
* @param {Object} event - a object of event data
|
71
|
+
* @return
|
72
|
+
*/
|
73
|
+
trigger: function(element,eventName,event){
|
74
|
+
// Prepare
|
75
|
+
event = event || {};
|
76
|
+
var uid = History.Adapter.uid(element),
|
77
|
+
i,n;
|
78
|
+
|
79
|
+
// Apply Listener
|
80
|
+
History.Adapter.handlers[uid] = History.Adapter.handlers[uid] || {};
|
81
|
+
History.Adapter.handlers[uid][eventName] = History.Adapter.handlers[uid][eventName] || [];
|
82
|
+
|
83
|
+
// Fire Listeners
|
84
|
+
for ( i=0,n=History.Adapter.handlers[uid][eventName].length; i<n; ++i ) {
|
85
|
+
History.Adapter.handlers[uid][eventName][i].apply(this,[event]);
|
86
|
+
}
|
87
|
+
},
|
88
|
+
|
89
|
+
/**
|
90
|
+
* History.Adapter.extractEventData(key,event,extra)
|
91
|
+
* @param {String} key - key for the event data to extract
|
92
|
+
* @param {String} event - custom and standard events
|
93
|
+
* @return {mixed}
|
94
|
+
*/
|
95
|
+
extractEventData: function(key,event){
|
96
|
+
var result = (event && event[key]) || undefined;
|
97
|
+
return result;
|
98
|
+
},
|
99
|
+
|
100
|
+
/**
|
101
|
+
* History.Adapter.onDomLoad(callback)
|
102
|
+
* @param {Function} callback
|
103
|
+
* @return
|
104
|
+
*/
|
105
|
+
onDomLoad: function(callback) {
|
106
|
+
var timeout = window.setTimeout(function(){
|
107
|
+
callback();
|
108
|
+
},2000);
|
109
|
+
window.onload = function(){
|
110
|
+
clearTimeout(timeout);
|
111
|
+
callback();
|
112
|
+
};
|
113
|
+
}
|
114
|
+
};
|
115
|
+
|
116
|
+
// Try and Initialise History
|
117
|
+
if ( typeof History.init !== 'undefined' ) {
|
118
|
+
History.init();
|
119
|
+
}
|
120
|
+
|
121
|
+
})(window);
|