hyper-router 2.4.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +330 -268
  3. data/lib/hyper-router.rb +30 -15
  4. data/lib/hyper-router/class_methods.rb +53 -0
  5. data/lib/hyper-router/component_methods.rb +63 -0
  6. data/lib/hyper-router/history.rb +22 -0
  7. data/lib/hyper-router/location.rb +29 -0
  8. data/lib/hyper-router/match.rb +18 -0
  9. data/lib/hyper-router/react-router-source.rb +4 -2
  10. data/lib/{react/router → hyper-router}/version.rb +1 -1
  11. data/lib/hyperloop/router.rb +36 -0
  12. data/lib/hyperloop/router/base.rb +12 -0
  13. data/lib/hyperloop/router/base/class_methods.rb +21 -0
  14. data/lib/hyperloop/router/browser.rb +12 -0
  15. data/lib/hyperloop/router/browser/class_methods.rb +15 -0
  16. data/lib/hyperloop/router/component.rb +28 -0
  17. data/lib/hyperloop/router/hash.rb +12 -0
  18. data/lib/hyperloop/router/hash/class_methods.rb +15 -0
  19. data/lib/hyperloop/router/memory.rb +12 -0
  20. data/lib/hyperloop/router/memory/class_methods.rb +15 -0
  21. data/lib/hyperloop/router/static.rb +12 -0
  22. data/lib/hyperloop/router/static/class_methods.rb +15 -0
  23. data/lib/react/router.rb +2 -128
  24. data/lib/react/router/dom.rb +7 -0
  25. data/lib/react/router/history.rb +19 -25
  26. data/lib/src/history.min.js +1 -0
  27. data/lib/src/react-router-dom.min.js +2 -0
  28. data/lib/src/react-router.min.js +1 -0
  29. metadata +34 -21
  30. data/lib/promise_extras.rb +0 -7
  31. data/lib/react/router/dsl.rb +0 -31
  32. data/lib/react/router/dsl/index.rb +0 -11
  33. data/lib/react/router/dsl/route.rb +0 -89
  34. data/lib/react/router/dsl/route/hooks.rb +0 -21
  35. data/lib/react/router/dsl/route/wrappers.rb +0 -104
  36. data/lib/react/router/dsl/transition_context.rb +0 -27
  37. data/lib/src/react-router.js +0 -2
data/lib/hyper-router.rb CHANGED
@@ -1,37 +1,52 @@
1
- require 'hyperloop-config'
2
1
  require 'hyper-component'
3
2
  Hyperloop.import 'hyper-router/react-router-source'
4
3
  Hyperloop.import 'hyper-router'
5
4
  if RUBY_ENGINE == 'opal'
6
- no_source = `Opal.global.ReactRouter === undefined`
7
- if no_source
5
+ no_router_source = `Opal.global.ReactRouter === undefined`
6
+ no_router_dom_source = `Opal.global.ReactRouterDOM === undefined`
7
+ if no_router_source || no_router_dom_source
8
8
  error = <<-ERROR
9
- No react-router.js Available.
9
+ No react-router.js or react-router-dom.js Available.
10
10
 
11
- A global `ReactRouter` must be defined before requiring 'hyper-router'.
11
+ A global 'ReactRouter' and 'ReactRouterDOM' must be defined before requiring 'hyper-router'.
12
12
 
13
13
  To USE THE BUILT-IN SOURCE:
14
14
  add 'require \"hyper-router/react-router-source\"'
15
15
  immediately before the 'require \"hyper-router\" directive.
16
16
 
17
17
  IF USING NPM/WEBPACK:
18
- add "react-router": "~2.4.0" to your package.json.)
18
+ add '\"react-router\": \"4.0.0\"' and '\"react-router-dom\": \"4.0.0\"' to your package.json.)
19
19
  ERROR
20
20
  raise error
21
21
  end
22
- require 'active_support'
23
- require 'promise'
24
- require 'promise_extras'
22
+
23
+
25
24
  require 'react/router'
26
- require 'react/router/dsl'
27
- require 'react/router/dsl/route'
28
- require 'react/router/dsl/index'
29
- require 'react/router/dsl/transition_context'
25
+ require 'react/router/dom'
30
26
  require 'react/router/history'
27
+
28
+ require 'hyper-router/history'
29
+ require 'hyper-router/location'
30
+ require 'hyper-router/match'
31
+ require 'hyper-router/class_methods'
32
+ require 'hyper-router/component_methods'
33
+
34
+ require 'hyperloop/router/base/class_methods'
35
+ require 'hyperloop/router/browser/class_methods'
36
+ require 'hyperloop/router/hash/class_methods'
37
+ require 'hyperloop/router/memory/class_methods'
38
+ require 'hyperloop/router/static/class_methods'
39
+ require 'hyperloop/router/base'
40
+ require 'hyperloop/router/browser'
41
+ require 'hyperloop/router/component'
42
+ require 'hyperloop/router/hash'
43
+ require 'hyperloop/router/memory'
44
+ require 'hyperloop/router/static'
45
+ require 'hyperloop/router'
31
46
  else
32
47
  require 'opal'
33
- require 'hyper-react'
34
- require 'react/router/version'
48
+ # require 'hyper-react'
49
+ require 'hyper-router/version'
35
50
 
36
51
  Opal.append_path File.expand_path('../', __FILE__).untaint
37
52
  end
@@ -0,0 +1,53 @@
1
+ module HyperRouter
2
+ module ClassMethods
3
+ def prerender_path(*args)
4
+ name = args[0].is_a?(Hash) ? args[0].first[0] : args[0]
5
+
6
+ define_method(:prerender_path) do
7
+ params.send(:"#{name}")
8
+ end
9
+
10
+ param(*args)
11
+ end
12
+
13
+ def history(history_type)
14
+ define_method(:history) do
15
+ @history ||= self.class.send(:"#{history_type}_history")
16
+ end
17
+ end
18
+
19
+ def prerender_router(&block)
20
+ define_method(:render) do
21
+ location = {}.tap do |hash|
22
+ pathname, search = (respond_to?(:prerender_path) ? prerender_path : '').split('?', 2)
23
+ hash[:pathname] = pathname
24
+ hash[:search] = search ? "?#{search}" : ''
25
+ end
26
+
27
+ React::Router::StaticRouter(location: location.to_n, context: {}.to_n) do
28
+ instance_eval(&block)
29
+ end
30
+ end
31
+ end
32
+
33
+ def route(&block)
34
+ if React::IsomorphicHelpers.on_opal_client?
35
+ render_router(&block)
36
+ else
37
+ prerender_router(&block)
38
+ end
39
+ end
40
+
41
+ def browser_history
42
+ React::Router::History.current.create_browser_history
43
+ end
44
+
45
+ def hash_history(*args)
46
+ React::Router::History.current.create_hash_history(*args)
47
+ end
48
+
49
+ def memory_history(*args)
50
+ React::Router::History.current.create_memory_history(*args)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,63 @@
1
+ # rubocop:disable Style/MethodName
2
+
3
+ module HyperRouter
4
+ module ComponentMethods
5
+ def Link(to, opts = {}, &children)
6
+ opts[:to] = {}.tap do |hash|
7
+ hash[:pathname] = to
8
+ hash[:search] = opts.delete(:search) if opts[:search]
9
+ hash[:hash] = opts.delete(:hash) if opts[:hash]
10
+ end.to_n
11
+ React::Router::DOM::Link(opts, &children)
12
+ end
13
+
14
+ def NavLink(to, opts = {}, &children)
15
+ opts[:to] = to.to_n
16
+ opts[:activeClassName] = opts.delete(:active_class).to_n if opts[:active_class]
17
+ opts[:activeStyle] = opts.delete(:active_style).to_n if opts[:active_style]
18
+ opts[:isActive] = opts.delete(:active).to_n if opts[:active]
19
+ React::Router::DOM::NavLink(opts, &children)
20
+ end
21
+
22
+ def Redirect(to, opts = {})
23
+ opts[:to] = to.to_n
24
+ React::Router::Redirect(opts)
25
+ end
26
+
27
+ def format_params(e)
28
+ {
29
+ match: HyperRouter::Match.new(`#{e}.match`),
30
+ location: HyperRouter::Location.new(`#{e}.location`),
31
+ history: HyperRouter::History.new(`#{e}.history`)
32
+ }
33
+ end
34
+
35
+ def Route(to, opts = {}, &block)
36
+ opts[:path] = to.to_n
37
+
38
+ if opts[:mounts]
39
+ component = opts.delete(:mounts)
40
+
41
+ opts[:component] = lambda do |e|
42
+ route_params = format_params(e)
43
+
44
+ React.create_element(component, route_params).to_n
45
+ end
46
+ end
47
+
48
+ if block
49
+ opts[:render] = lambda do |e|
50
+ route_params = format_params(e)
51
+
52
+ yield(route_params.values).to_n
53
+ end
54
+ end
55
+
56
+ React::Router::Route(opts)
57
+ end
58
+
59
+ def Switch(&children)
60
+ React::Router::Switch(&children)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,22 @@
1
+ module HyperRouter
2
+ class History
3
+ include Native
4
+
5
+ def initialize(native)
6
+ @native = native
7
+ end
8
+
9
+ def to_n
10
+ @native
11
+ end
12
+
13
+ alias_native :block
14
+ alias_native :create_href, :createHref
15
+ alias_native :go
16
+ alias_native :go_back, :goBack
17
+ alias_native :go_forward, :goForward
18
+ alias_native :location, :location
19
+ alias_native :push, :push
20
+ alias_native :replace, :replace
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module HyperRouter
2
+ class Location
3
+ include Native
4
+
5
+ def initialize(native)
6
+ @native = native
7
+ end
8
+
9
+ def to_n
10
+ @native
11
+ end
12
+
13
+ def query
14
+ return {} if search.blank?
15
+
16
+ Hash[search[1..-1].split('&').map { |part|
17
+ name, value = part.split('=')
18
+
19
+ [`decodeURIComponent(#{name})`, `decodeURIComponent(#{value})`]
20
+ }]
21
+ end
22
+
23
+ alias_native :pathname
24
+ alias_native :search
25
+ alias_native :hash
26
+ alias_native :state
27
+ alias_native :key
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ module HyperRouter
2
+ class Match
3
+ include Native
4
+
5
+ def initialize(native)
6
+ @native = native
7
+ end
8
+
9
+ def to_n
10
+ @native
11
+ end
12
+
13
+ alias_native :params
14
+ alias_native :is_exact, :isExact
15
+ alias_native :path
16
+ alias_native :url
17
+ end
18
+ end
@@ -1,2 +1,4 @@
1
- # rubocop:disable
2
- require 'src/react-router.js'
1
+ # rubocop:disable Style/FileName
2
+ require 'src/react-router.min.js'
3
+ require 'src/react-router-dom.min.js'
4
+ require 'src/history.min.js'
@@ -1,3 +1,3 @@
1
1
  module HyperRouter
2
- VERSION = '2.4.1'
2
+ VERSION = '4.0.0'
3
3
  end
@@ -0,0 +1,36 @@
1
+ module Hyperloop
2
+ class Router
3
+ def self.inherited(child)
4
+ child.include(React::Component)
5
+ child.include(Base)
6
+ end
7
+ end
8
+
9
+ class BrowserRouter
10
+ def self.inherited(child)
11
+ child.include(React::Component)
12
+ child.include(Hyperloop::Router::Browser)
13
+ end
14
+ end
15
+
16
+ class HashRouter
17
+ def self.inherited(child)
18
+ child.include(React::Component)
19
+ child.include(Hyperloop::Router::Hash)
20
+ end
21
+ end
22
+
23
+ class MemoryRouter
24
+ def self.inherited(child)
25
+ child.include(React::Component)
26
+ child.include(Hyperloop::Router::Memory)
27
+ end
28
+ end
29
+
30
+ class StaticRouter
31
+ def self.inherited(child)
32
+ child.include(React::Component)
33
+ child.include(Hyperloop::Router::Static)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,12 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Base
4
+ def self.included(base)
5
+ base.extend(HyperRouter::ClassMethods)
6
+ base.extend(ClassMethods)
7
+
8
+ base.include(HyperRouter::ComponentMethods)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Base
4
+ module ClassMethods
5
+ def render_router(&block)
6
+ define_method(:render) do
7
+ if respond_to?(:history)
8
+ React::Router::Router(history: history.to_n) do
9
+ instance_eval(&block)
10
+ end
11
+ else
12
+ React::Router::MemoryRouter(location: { pathname: '/' }.to_n) do
13
+ instance_eval(&block)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Browser
4
+ def self.included(base)
5
+ base.extend(HyperRouter::ClassMethods)
6
+ base.extend(ClassMethods)
7
+
8
+ base.include(HyperRouter::ComponentMethods)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Browser
4
+ module ClassMethods
5
+ def render_router(&block)
6
+ define_method(:render) do
7
+ React::Router::DOM::BrowserRouter() do
8
+ instance_eval(&block)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module Hyperloop
2
+ class Router
3
+ class Component
4
+ def self.inherited(base)
5
+ base.include(React::Component)
6
+ base.include(HyperRouter::ComponentMethods)
7
+
8
+ base.class_eval do
9
+ param :match, default: nil
10
+ param :location, default: nil
11
+ param :history, default: nil
12
+
13
+ define_method(:match) do
14
+ params.match
15
+ end
16
+
17
+ define_method(:location) do
18
+ params.location
19
+ end
20
+
21
+ define_method(:history) do
22
+ params.history
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Browser
4
+ def self.included(base)
5
+ base.extend(HyperRouter::ClassMethods)
6
+ base.extend(ClassMethods)
7
+
8
+ base.include(HyperRouter::ComponentMethods)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Hash
4
+ module ClassMethods
5
+ def render(&block)
6
+ define_method(:render) do
7
+ React::Router::DOM::HashRouter() do
8
+ instance_eval(&block)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module Hyperloop
2
+ class Router
3
+ module Memory
4
+ def self.included(base)
5
+ base.extend(HyperRouter::ClassMethods)
6
+ base.extend(ClassMethods)
7
+
8
+ base.include(HyperRouter::ComponentMethods)
9
+ end
10
+ end
11
+ end
12
+ end