js-routes 1.4.11 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.eslintrc.js +15 -0
- data/.gitignore +4 -0
- data/.nvmrc +1 -0
- data/.travis.yml +2 -1
- data/CHANGELOG.md +26 -1
- data/Readme.md +164 -50
- data/VERSION_2_UPGRADE.md +61 -0
- data/js-routes.gemspec +7 -4
- data/lib/js_routes.rb +96 -55
- data/lib/js_routes/version.rb +1 -1
- data/lib/routes.d.ts +64 -0
- data/lib/routes.js +480 -516
- data/lib/routes.js.map +1 -0
- data/lib/routes.ts +683 -0
- data/package.json +36 -0
- data/spec/js_routes/default_serializer_spec.rb +19 -3
- data/spec/js_routes/{amd_compatibility_spec.rb → module_types/amd_spec.rb} +1 -12
- data/spec/js_routes/module_types/cjs_spec.rb +15 -0
- data/spec/js_routes/module_types/esm_spec.rb +45 -0
- data/spec/js_routes/{generated_javascript_spec.rb → module_types/umd_spec.rb} +32 -21
- data/spec/js_routes/options_spec.rb +19 -6
- data/spec/js_routes/rails_routes_compatibility_spec.rb +54 -25
- data/spec/js_routes/zzz_last_post_rails_init_spec.rb +2 -2
- data/spec/spec_helper.rb +25 -17
- data/tsconfig.json +32 -0
- data/yarn.lock +2145 -0
- metadata +26 -16
- data/lib/routes.js.coffee +0 -407
data/js-routes.gemspec
CHANGED
@@ -7,13 +7,16 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{js-routes}
|
8
8
|
s.version = JsRoutes::VERSION
|
9
9
|
|
10
|
-
|
10
|
+
if s.respond_to? :required_rubygems_version=
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0")
|
12
|
+
end
|
11
13
|
s.authors = ["Bogdan Gusiev"]
|
12
14
|
s.description = %q{Generates javascript file that defines all Rails named routes as javascript helpers}
|
13
15
|
s.email = %q{agresso@gmail.com}
|
14
16
|
s.extra_rdoc_files = [
|
15
17
|
"LICENSE.txt"
|
16
18
|
]
|
19
|
+
s.required_ruby_version = '>= 2.4.0'
|
17
20
|
s.files = `git ls-files`.split("\n")
|
18
21
|
s.homepage = %q{http://github.com/railsware/js-routes}
|
19
22
|
s.licenses = ["MIT"]
|
@@ -21,16 +24,16 @@ Gem::Specification.new do |s|
|
|
21
24
|
s.summary = %q{Brings Rails named routes to javascript}
|
22
25
|
|
23
26
|
s.add_runtime_dependency(%q<railties>, [">= 4"])
|
24
|
-
s.
|
27
|
+
s.add_development_dependency(%q<sprockets-rails>)
|
25
28
|
s.add_development_dependency(%q<rspec>, [">= 3.0.0"])
|
26
29
|
s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
|
27
|
-
s.add_development_dependency(%q<coffee-script>, [">= 0"])
|
28
30
|
s.add_development_dependency(%q<appraisal>, [">= 0.5.2"])
|
31
|
+
s.add_development_dependency(%q<bump>, [">= 0.10.0"])
|
29
32
|
if defined?(JRUBY_VERSION)
|
30
33
|
s.add_development_dependency(%q<therubyrhino>, [">= 2.0.4"])
|
31
34
|
else
|
32
35
|
s.add_development_dependency(%q<byebug>)
|
33
36
|
s.add_development_dependency(%q<pry-byebug>)
|
34
|
-
s.add_development_dependency(%q<mini_racer>, [">= 0.
|
37
|
+
s.add_development_dependency(%q<mini_racer>, [">= 0.4.0"])
|
35
38
|
end
|
36
39
|
end
|
data/lib/js_routes.rb
CHANGED
@@ -3,6 +3,7 @@ if defined?(::Rails) && defined?(::Sprockets::Railtie)
|
|
3
3
|
require 'js_routes/engine'
|
4
4
|
end
|
5
5
|
require 'js_routes/version'
|
6
|
+
require 'active_support/core_ext/string/indent'
|
6
7
|
|
7
8
|
class JsRoutes
|
8
9
|
|
@@ -15,10 +16,11 @@ class JsRoutes
|
|
15
16
|
exclude: [],
|
16
17
|
include: //,
|
17
18
|
file: -> do
|
19
|
+
webpacker_dir = Rails.root.join('app', 'javascript')
|
18
20
|
sprockets_dir = Rails.root.join('app','assets','javascripts')
|
19
21
|
sprockets_file = sprockets_dir.join('routes.js')
|
20
|
-
webpacker_file =
|
21
|
-
Dir.
|
22
|
+
webpacker_file = webpacker_dir.join('routes.js')
|
23
|
+
!Dir.exist?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
|
22
24
|
end,
|
23
25
|
prefix: -> { Rails.application.config.relative_url_root || "" },
|
24
26
|
url_links: false,
|
@@ -27,7 +29,9 @@ class JsRoutes
|
|
27
29
|
compact: false,
|
28
30
|
serializer: nil,
|
29
31
|
special_options_key: "_options",
|
30
|
-
application: -> { Rails.application }
|
32
|
+
application: -> { Rails.application },
|
33
|
+
module_type: 'ESM',
|
34
|
+
documentation: true,
|
31
35
|
} #:nodoc:
|
32
36
|
|
33
37
|
NODE_TYPES = {
|
@@ -71,6 +75,10 @@ class JsRoutes
|
|
71
75
|
def to_hash
|
72
76
|
Hash[*members.zip(values).flatten(1)].symbolize_keys
|
73
77
|
end
|
78
|
+
|
79
|
+
def esm?
|
80
|
+
self.module_type === 'ESM'
|
81
|
+
end
|
74
82
|
end
|
75
83
|
|
76
84
|
#
|
@@ -82,11 +90,6 @@ class JsRoutes
|
|
82
90
|
configuration.tap(&block) if block
|
83
91
|
end
|
84
92
|
|
85
|
-
def options
|
86
|
-
ActiveSupport::Deprecation.warn('JsRoutes.options method is deprecated use JsRoutes.configuration instead')
|
87
|
-
configuration
|
88
|
-
end
|
89
|
-
|
90
93
|
def configuration
|
91
94
|
@configuration ||= Configuration.new
|
92
95
|
end
|
@@ -124,8 +127,7 @@ class JsRoutes
|
|
124
127
|
|
125
128
|
{
|
126
129
|
'GEM_VERSION' => JsRoutes::VERSION,
|
127
|
-
'
|
128
|
-
'NODE_TYPES' => json(NODE_TYPES),
|
130
|
+
'ROUTES_OBJECT' => routes_object,
|
129
131
|
'RAILS_VERSION' => ActionPack.version,
|
130
132
|
'DEPRECATED_GLOBBING_BEHAVIOR' => ActionPack::VERSION::MAJOR == 4 && ActionPack::VERSION::MINOR == 0,
|
131
133
|
|
@@ -135,9 +137,12 @@ class JsRoutes
|
|
135
137
|
'PREFIX' => json(@configuration.prefix),
|
136
138
|
'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
|
137
139
|
'SERIALIZER' => @configuration.serializer || json(nil),
|
140
|
+
'MODULE_TYPE' => json(@configuration.module_type),
|
141
|
+
'WRAPPER' => @configuration.esm? ? 'const __jsr = ' : '',
|
138
142
|
}.inject(File.read(File.dirname(__FILE__) + "/routes.js")) do |js, (key, value)|
|
139
|
-
js.gsub!(key, value.to_s)
|
140
|
-
|
143
|
+
js.gsub!("RubyVariables.#{key}", value.to_s) ||
|
144
|
+
raise("Missing key #{key} in JS template")
|
145
|
+
end + routes_export
|
141
146
|
end
|
142
147
|
|
143
148
|
def generate!(file_name = nil)
|
@@ -169,19 +174,37 @@ class JsRoutes
|
|
169
174
|
application.routes.named_routes.to_a
|
170
175
|
end
|
171
176
|
|
172
|
-
def
|
173
|
-
|
174
|
-
|
177
|
+
def routes_object
|
178
|
+
return json({}) if @configuration.esm?
|
179
|
+
properties = routes_list.map do |comment, name, body|
|
180
|
+
"#{comment}#{name}: #{body}".indent(2)
|
181
|
+
end
|
182
|
+
"{\n" + properties.join(",\n\n") + "}\n"
|
183
|
+
end
|
184
|
+
|
185
|
+
STATIC_EXPORTS = [:configure, :config, :serialize].map do |name|
|
186
|
+
["", name, "__jsr.#{name}"]
|
187
|
+
end
|
188
|
+
|
189
|
+
def routes_export
|
190
|
+
return "" unless @configuration.esm?
|
191
|
+
[*STATIC_EXPORTS, *routes_list].map do |comment, name, body|
|
192
|
+
"#{comment}export const #{name} = #{body};"
|
193
|
+
end.join("\n\n")
|
194
|
+
end
|
195
|
+
|
196
|
+
def routes_list
|
197
|
+
named_routes.sort_by(&:first).flat_map do |_, route|
|
198
|
+
build_routes_if_match(route) + mounted_app_routes(route)
|
175
199
|
end.compact
|
176
|
-
"{\n" + js_routes.join(",\n") + "}\n"
|
177
200
|
end
|
178
201
|
|
179
202
|
def mounted_app_routes(route)
|
180
203
|
rails_engine_app = get_app_from_route(route)
|
181
204
|
if rails_engine_app.respond_to?(:superclass) &&
|
182
205
|
rails_engine_app.superclass == Rails::Engine && !route.path.anchored
|
183
|
-
rails_engine_app.routes.named_routes.
|
184
|
-
|
206
|
+
rails_engine_app.routes.named_routes.flat_map do |_, engine_route|
|
207
|
+
build_routes_if_match(engine_route, route)
|
185
208
|
end
|
186
209
|
else
|
187
210
|
[]
|
@@ -198,12 +221,17 @@ class JsRoutes
|
|
198
221
|
end
|
199
222
|
end
|
200
223
|
|
201
|
-
def
|
224
|
+
def build_routes_if_match(route, parent_route = nil)
|
202
225
|
if any_match?(route, parent_route, @configuration[:exclude]) ||
|
203
226
|
!any_match?(route, parent_route, @configuration[:include])
|
204
|
-
|
227
|
+
[]
|
205
228
|
else
|
206
|
-
|
229
|
+
name = [parent_route.try(:name), route.name].compact
|
230
|
+
parent_spec = parent_route.try(:path).try(:spec)
|
231
|
+
route_arguments = route_js_arguments(route, parent_spec)
|
232
|
+
return [false, true].map do |absolute|
|
233
|
+
route_js(name, parent_spec, route, route_arguments, absolute)
|
234
|
+
end
|
207
235
|
end
|
208
236
|
end
|
209
237
|
|
@@ -214,59 +242,72 @@ class JsRoutes
|
|
214
242
|
matchers.any? { |regex| full_route =~ regex }
|
215
243
|
end
|
216
244
|
|
217
|
-
def
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
245
|
+
def route_js(name_parts, parent_spec, route, route_arguments, absolute)
|
246
|
+
if absolute
|
247
|
+
return nil unless @configuration[:url_links]
|
248
|
+
route_arguments = route_arguments + [json(true)]
|
249
|
+
end
|
250
|
+
name_suffix = absolute ? :url : @configuration[:compact] ? nil : :path
|
251
|
+
name = generate_route_name(name_parts, name_suffix)
|
252
|
+
body = "__jsr.r(#{route_arguments.join(', ')})"
|
253
|
+
comment = documentation(route, parent_spec)
|
254
|
+
[ comment, name, body ]
|
255
|
+
end
|
256
|
+
|
257
|
+
def documentation(route, parent_spec)
|
258
|
+
return nil unless @configuration[:documentation]
|
259
|
+
<<-JS
|
260
|
+
/**
|
261
|
+
* Generates rails route to
|
262
|
+
* #{parent_spec}#{route.path.spec}#{build_params(route.required_parts)}
|
263
|
+
* @param {object | undefined} options
|
264
|
+
* @returns {string} route path
|
265
|
+
*/
|
266
|
+
JS
|
228
267
|
end
|
229
268
|
|
230
269
|
def route_js_arguments(route, parent_spec)
|
231
270
|
required_parts = route.required_parts
|
232
|
-
parts_table =
|
233
|
-
|
271
|
+
parts_table = {}
|
272
|
+
route.parts.each do |part, hash|
|
273
|
+
parts_table[part] ||= {}
|
274
|
+
if required_parts.include?(part)
|
275
|
+
# Using shortened keys to reduce js file size
|
276
|
+
parts_table[part][:r] = true
|
277
|
+
end
|
234
278
|
end
|
235
|
-
|
236
|
-
FILTERED_DEFAULT_PARTS.exclude?(part) &&
|
279
|
+
route.defaults.each do |part, value|
|
280
|
+
if FILTERED_DEFAULT_PARTS.exclude?(part) &&
|
237
281
|
URL_OPTIONS.include?(part) || parts_table[part]
|
282
|
+
parts_table[part] ||= {}
|
283
|
+
# Using shortened keys to reduce js file size
|
284
|
+
parts_table[part][:d] = value
|
285
|
+
end
|
238
286
|
end
|
239
287
|
[
|
240
|
-
|
241
|
-
# so array is a better choice.
|
242
|
-
parts_table.to_a,
|
243
|
-
default_options,
|
288
|
+
parts_table,
|
244
289
|
serialize(route.path.spec, parent_spec)
|
245
290
|
].map do |argument|
|
246
291
|
json(argument)
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
def generate_url_link(name, route_arguments)
|
251
|
-
return '' unless @configuration[:url_links]
|
252
|
-
|
253
|
-
<<-JS.strip!
|
254
|
-
#{generate_route_name(name, :url)}: Utils.route(#{route_arguments}, true)
|
255
|
-
JS
|
292
|
+
end
|
256
293
|
end
|
257
294
|
|
258
295
|
def generate_route_name(*parts)
|
259
|
-
|
260
|
-
@configuration[:camel_case] ? route_name.camelize(:lower) : route_name
|
296
|
+
apply_case(parts.compact.join('_'))
|
261
297
|
end
|
262
298
|
|
263
299
|
def json(string)
|
264
300
|
self.class.json(string)
|
265
301
|
end
|
266
302
|
|
303
|
+
def apply_case(value)
|
304
|
+
@configuration[:camel_case] ? value.to_s.camelize(:lower) : value
|
305
|
+
end
|
306
|
+
|
267
307
|
def build_params(required_parts)
|
268
|
-
|
269
|
-
|
308
|
+
required_parts.map do |param|
|
309
|
+
"\n * @param {any} #{apply_case(param)}"
|
310
|
+
end.join
|
270
311
|
end
|
271
312
|
|
272
313
|
# This function serializes Journey route into JSON structure
|
@@ -295,7 +336,7 @@ class JsRoutes
|
|
295
336
|
[
|
296
337
|
NODE_TYPES[spec.type],
|
297
338
|
serialize(spec.left, parent_spec),
|
298
|
-
spec.respond_to?(:right)
|
299
|
-
]
|
339
|
+
spec.respond_to?(:right) ? serialize(spec.right) : nil
|
340
|
+
].compact
|
300
341
|
end
|
301
342
|
end
|
data/lib/js_routes/version.rb
CHANGED
data/lib/routes.d.ts
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
/**
|
2
|
+
* File generated by js-routes RubyVariables.GEM_VERSION
|
3
|
+
* Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
|
4
|
+
*/
|
5
|
+
declare type RouteParameter = unknown;
|
6
|
+
declare type RouteParameters = Record<string, RouteParameter>;
|
7
|
+
declare type Serializer = (value: unknown) => string;
|
8
|
+
declare type RouteHelper = {
|
9
|
+
(...args: RouteParameter[]): string;
|
10
|
+
requiredParams(): string[];
|
11
|
+
toString(): string;
|
12
|
+
};
|
13
|
+
declare type RouteHelpers = Record<string, RouteHelper>;
|
14
|
+
declare type Configuration = {
|
15
|
+
prefix: string;
|
16
|
+
default_url_options: RouteParameters;
|
17
|
+
special_options_key: string;
|
18
|
+
serializer: Serializer;
|
19
|
+
};
|
20
|
+
declare type Optional<T> = {
|
21
|
+
[P in keyof T]?: T[P] | null;
|
22
|
+
};
|
23
|
+
interface RouterExposedMethods {
|
24
|
+
config(): Configuration;
|
25
|
+
configure(arg: Partial<Configuration>): Configuration;
|
26
|
+
serialize: Serializer;
|
27
|
+
}
|
28
|
+
declare type KeywordUrlOptions = Optional<{
|
29
|
+
host: string;
|
30
|
+
protocol: string;
|
31
|
+
subdomain: string;
|
32
|
+
port: string;
|
33
|
+
anchor: string;
|
34
|
+
trailing_slash: boolean;
|
35
|
+
}>;
|
36
|
+
declare type PartsTable = Record<
|
37
|
+
string,
|
38
|
+
{
|
39
|
+
r?: boolean;
|
40
|
+
d?: unknown;
|
41
|
+
}
|
42
|
+
>;
|
43
|
+
declare type ModuleType = "CJS" | "AMD" | "UMD" | "ESM";
|
44
|
+
declare const RubyVariables: {
|
45
|
+
PREFIX: string;
|
46
|
+
DEPRECATED_GLOBBING_BEHAVIOR: boolean;
|
47
|
+
SPECIAL_OPTIONS_KEY: string;
|
48
|
+
DEFAULT_URL_OPTIONS: RouteParameters;
|
49
|
+
SERIALIZER: Serializer;
|
50
|
+
NAMESPACE: string;
|
51
|
+
ROUTES_OBJECT: RouteHelpers;
|
52
|
+
MODULE_TYPE: ModuleType | null;
|
53
|
+
WRAPPER: <T>(callback: T) => T;
|
54
|
+
};
|
55
|
+
declare const define:
|
56
|
+
| undefined
|
57
|
+
| (((arg: unknown[], callback: () => unknown) => void) & {
|
58
|
+
amd?: unknown;
|
59
|
+
});
|
60
|
+
declare const module:
|
61
|
+
| {
|
62
|
+
exports: any;
|
63
|
+
}
|
64
|
+
| undefined;
|
data/lib/routes.js
CHANGED
@@ -1,520 +1,484 @@
|
|
1
|
-
|
2
|
-
File generated by js-routes GEM_VERSION
|
3
|
-
Based on Rails RAILS_VERSION routes of APP_CLASS
|
1
|
+
/**
|
2
|
+
* File generated by js-routes RubyVariables.GEM_VERSION
|
3
|
+
* Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
|
4
4
|
*/
|
5
|
-
|
6
|
-
(
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
5
|
+
RubyVariables.WRAPPER((that) => {
|
6
|
+
const hasProp = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
7
|
+
let NodeTypes;
|
8
|
+
(function (NodeTypes) {
|
9
|
+
NodeTypes[NodeTypes["GROUP"] = 1] = "GROUP";
|
10
|
+
NodeTypes[NodeTypes["CAT"] = 2] = "CAT";
|
11
|
+
NodeTypes[NodeTypes["SYMBOL"] = 3] = "SYMBOL";
|
12
|
+
NodeTypes[NodeTypes["OR"] = 4] = "OR";
|
13
|
+
NodeTypes[NodeTypes["STAR"] = 5] = "STAR";
|
14
|
+
NodeTypes[NodeTypes["LITERAL"] = 6] = "LITERAL";
|
15
|
+
NodeTypes[NodeTypes["SLASH"] = 7] = "SLASH";
|
16
|
+
NodeTypes[NodeTypes["DOT"] = 8] = "DOT";
|
17
|
+
})(NodeTypes || (NodeTypes = {}));
|
18
|
+
const Root = that;
|
19
|
+
const ModuleReferences = {
|
20
|
+
CJS: {
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
22
|
+
define: (routes) => (module.exports = routes),
|
23
|
+
support: () => typeof module === "object",
|
24
|
+
},
|
25
|
+
AMD: {
|
26
|
+
define: (routes) =>
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
28
|
+
define([], function () {
|
29
|
+
return routes;
|
30
|
+
}),
|
31
|
+
support: () => typeof define === "function" && !!define.amd,
|
32
|
+
},
|
33
|
+
UMD: {
|
34
|
+
define: (routes) => {
|
35
|
+
if (ModuleReferences.AMD.support()) {
|
36
|
+
ModuleReferences.AMD.define(routes);
|
37
|
+
}
|
38
|
+
else {
|
39
|
+
if (ModuleReferences.CJS.support()) {
|
40
|
+
try {
|
41
|
+
ModuleReferences.CJS.define(routes);
|
42
|
+
}
|
43
|
+
catch (error) {
|
44
|
+
if (error.name !== "TypeError")
|
45
|
+
throw error;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
},
|
50
|
+
support: () => ModuleReferences.AMD.support() || ModuleReferences.CJS.support(),
|
51
|
+
},
|
52
|
+
ESM: {
|
53
|
+
define: () => null,
|
54
|
+
support: () => true,
|
55
|
+
},
|
56
|
+
};
|
57
|
+
class ParametersMissing extends Error {
|
58
|
+
constructor(...keys) {
|
59
|
+
super(`Route missing required keys: ${keys.join(", ")}`);
|
60
|
+
this.keys = keys;
|
61
|
+
Object.setPrototypeOf(this, Object.getPrototypeOf(this));
|
62
|
+
this.name = ParametersMissing.name;
|
63
|
+
}
|
23
64
|
}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
65
|
+
const DeprecatedGlobbingBehavior = RubyVariables.DEPRECATED_GLOBBING_BEHAVIOR;
|
66
|
+
const UriEncoderSegmentRegex = /[^a-zA-Z0-9\-._~!$&'()*+,;=:@]/g;
|
67
|
+
const ReservedOptions = [
|
68
|
+
"anchor",
|
69
|
+
"trailing_slash",
|
70
|
+
"subdomain",
|
71
|
+
"host",
|
72
|
+
"port",
|
73
|
+
"protocol",
|
74
|
+
];
|
75
|
+
class UtilsClass {
|
76
|
+
constructor() {
|
77
|
+
this.configuration = {
|
78
|
+
prefix: RubyVariables.PREFIX,
|
79
|
+
default_url_options: RubyVariables.DEFAULT_URL_OPTIONS,
|
80
|
+
special_options_key: RubyVariables.SPECIAL_OPTIONS_KEY,
|
81
|
+
serializer: RubyVariables.SERIALIZER || this.default_serializer.bind(this),
|
82
|
+
};
|
83
|
+
}
|
84
|
+
default_serializer(value, prefix) {
|
85
|
+
if (this.is_nullable(value)) {
|
86
|
+
return "";
|
87
|
+
}
|
88
|
+
if (!prefix && !this.is_object(value)) {
|
89
|
+
throw new Error("Url parameters should be a javascript hash");
|
90
|
+
}
|
91
|
+
prefix = prefix || "";
|
92
|
+
const result = [];
|
93
|
+
if (this.is_array(value)) {
|
94
|
+
for (const element of value) {
|
95
|
+
result.push(this.default_serializer(element, prefix + "[]"));
|
96
|
+
}
|
97
|
+
}
|
98
|
+
else if (this.is_object(value)) {
|
99
|
+
for (let key in value) {
|
100
|
+
if (!hasProp(value, key))
|
101
|
+
continue;
|
102
|
+
let prop = value[key];
|
103
|
+
if (this.is_nullable(prop) && prefix) {
|
104
|
+
prop = "";
|
105
|
+
}
|
106
|
+
if (this.is_not_nullable(prop)) {
|
107
|
+
if (prefix) {
|
108
|
+
key = prefix + "[" + key + "]";
|
109
|
+
}
|
110
|
+
result.push(this.default_serializer(prop, key));
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
else {
|
115
|
+
if (this.is_not_nullable(value)) {
|
116
|
+
result.push(encodeURIComponent(prefix) + "=" + encodeURIComponent("" + value));
|
117
|
+
}
|
118
|
+
}
|
119
|
+
return result.join("&");
|
120
|
+
}
|
121
|
+
serialize(object) {
|
122
|
+
return this.configuration.serializer(object);
|
123
|
+
}
|
124
|
+
extract_options(number_of_params, args) {
|
125
|
+
const last_el = args[args.length - 1];
|
126
|
+
if ((args.length > number_of_params && last_el === 0) ||
|
127
|
+
(this.is_object(last_el) && !this.looks_like_serialized_model(last_el))) {
|
128
|
+
if (this.is_object(last_el)) {
|
129
|
+
delete last_el[this.configuration.special_options_key];
|
130
|
+
}
|
131
|
+
return {
|
132
|
+
args: args.slice(0, args.length - 1),
|
133
|
+
options: last_el,
|
134
|
+
};
|
135
|
+
}
|
136
|
+
else {
|
137
|
+
return { args, options: {} };
|
138
|
+
}
|
139
|
+
}
|
140
|
+
looks_like_serialized_model(object) {
|
141
|
+
return (this.is_object(object) &&
|
142
|
+
!object[this.configuration.special_options_key] &&
|
143
|
+
("id" in object || "to_param" in object || "toParam" in object));
|
144
|
+
}
|
145
|
+
path_identifier(object) {
|
146
|
+
const result = this.unwrap_path_identifier(object);
|
147
|
+
return this.is_nullable(result) ? "" : "" + result;
|
148
|
+
}
|
149
|
+
unwrap_path_identifier(object) {
|
150
|
+
let result = object;
|
151
|
+
if (!this.is_object(object)) {
|
152
|
+
return object;
|
153
|
+
}
|
154
|
+
if ("to_param" in object) {
|
155
|
+
result = object.to_param;
|
156
|
+
}
|
157
|
+
else if ("toParam" in object) {
|
158
|
+
result = object.toParam;
|
159
|
+
}
|
160
|
+
else if ("id" in object) {
|
161
|
+
result = object.id;
|
162
|
+
}
|
163
|
+
else {
|
164
|
+
result = object;
|
165
|
+
}
|
166
|
+
return this.is_callable(result) ? result.call(object) : result;
|
167
|
+
}
|
168
|
+
partition_parameters(parts, required_params, default_options, call_arguments) {
|
169
|
+
// eslint-disable-next-line prefer-const
|
170
|
+
let { args, options } = this.extract_options(parts.length, call_arguments);
|
171
|
+
if (args.length > parts.length) {
|
172
|
+
throw new Error("Too many parameters provided for path");
|
173
|
+
}
|
174
|
+
let use_all_parts = args.length > required_params.length;
|
175
|
+
const parts_options = {};
|
176
|
+
for (const key in options) {
|
177
|
+
const value = options[key];
|
178
|
+
if (!hasProp(options, key))
|
179
|
+
continue;
|
180
|
+
use_all_parts = true;
|
181
|
+
if (parts.includes(key)) {
|
182
|
+
parts_options[key] = value;
|
183
|
+
}
|
184
|
+
}
|
185
|
+
options = {
|
186
|
+
...this.configuration.default_url_options,
|
187
|
+
...default_options,
|
188
|
+
...options,
|
189
|
+
};
|
190
|
+
const url_parameters = {};
|
191
|
+
const query_parameters = {};
|
192
|
+
for (const key in options) {
|
193
|
+
if (!hasProp(options, key))
|
194
|
+
continue;
|
195
|
+
const value = options[key];
|
196
|
+
if (this.is_reserved_option(key)) {
|
197
|
+
url_parameters[key] = value;
|
198
|
+
}
|
199
|
+
else {
|
200
|
+
if (!this.is_nullable(value) &&
|
201
|
+
(value !== default_options[key] || required_params.includes(key))) {
|
202
|
+
query_parameters[key] = value;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
const route_parts = use_all_parts ? parts : required_params;
|
207
|
+
let i = 0;
|
208
|
+
for (const part of route_parts) {
|
209
|
+
if (i < args.length) {
|
210
|
+
if (!hasProp(parts_options, part)) {
|
211
|
+
query_parameters[part] = args[i];
|
212
|
+
++i;
|
213
|
+
}
|
214
|
+
}
|
215
|
+
}
|
216
|
+
return { url_parameters, query_parameters };
|
217
|
+
}
|
218
|
+
build_route(parts, required_params, default_options, route, full_url, args) {
|
219
|
+
const { url_parameters, query_parameters } = this.partition_parameters(parts, required_params, default_options, args);
|
220
|
+
const missing_params = required_params.filter((param) => !hasProp(query_parameters, param) ||
|
221
|
+
this.is_nullable(query_parameters[param]));
|
222
|
+
if (missing_params.length) {
|
223
|
+
throw new ParametersMissing(...missing_params);
|
224
|
+
}
|
225
|
+
let result = this.get_prefix() + this.visit(route, query_parameters);
|
226
|
+
if (url_parameters.trailing_slash) {
|
227
|
+
result = result.replace(/(.*?)[/]?$/, "$1/");
|
228
|
+
}
|
229
|
+
const url_params = this.serialize(query_parameters);
|
230
|
+
if (url_params.length) {
|
231
|
+
result += "?" + url_params;
|
232
|
+
}
|
233
|
+
result += url_parameters.anchor ? "#" + url_parameters.anchor : "";
|
234
|
+
if (full_url) {
|
235
|
+
result = this.route_url(url_parameters) + result;
|
236
|
+
}
|
237
|
+
return result;
|
238
|
+
}
|
239
|
+
visit(route, parameters, optional = false) {
|
240
|
+
switch (route[0]) {
|
241
|
+
case NodeTypes.GROUP:
|
242
|
+
return this.visit(route[1], parameters, true);
|
243
|
+
case NodeTypes.CAT:
|
244
|
+
return this.visit_cat(route, parameters, optional);
|
245
|
+
case NodeTypes.SYMBOL:
|
246
|
+
return this.visit_symbol(route, parameters, optional);
|
247
|
+
case NodeTypes.STAR:
|
248
|
+
return this.visit_globbing(route[1], parameters, true);
|
249
|
+
case NodeTypes.LITERAL:
|
250
|
+
case NodeTypes.SLASH:
|
251
|
+
case NodeTypes.DOT:
|
252
|
+
return route[1];
|
253
|
+
default:
|
254
|
+
throw new Error("Unknown Rails node type");
|
255
|
+
}
|
256
|
+
}
|
257
|
+
is_not_nullable(object) {
|
258
|
+
return !this.is_nullable(object);
|
259
|
+
}
|
260
|
+
is_nullable(object) {
|
261
|
+
return object === undefined || object === null;
|
262
|
+
}
|
263
|
+
visit_cat(
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
265
|
+
[_type, left, right], parameters, optional) {
|
266
|
+
const left_part = this.visit(left, parameters, optional);
|
267
|
+
const right_part = this.visit(right, parameters, optional);
|
268
|
+
if (optional &&
|
269
|
+
((this.is_optional_node(left[0]) && !left_part) ||
|
270
|
+
(this.is_optional_node(right[0]) && !right_part))) {
|
271
|
+
return "";
|
272
|
+
}
|
273
|
+
return left_part + right_part;
|
274
|
+
}
|
275
|
+
visit_symbol(
|
276
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
277
|
+
[_type, key], parameters, optional) {
|
278
|
+
const value = this.path_identifier(parameters[key]);
|
279
|
+
delete parameters[key];
|
280
|
+
if (value.length) {
|
281
|
+
return this.encode_segment(value);
|
282
|
+
}
|
283
|
+
if (optional) {
|
284
|
+
return "";
|
285
|
+
}
|
286
|
+
else {
|
287
|
+
throw new ParametersMissing(key);
|
288
|
+
}
|
289
|
+
}
|
290
|
+
encode_segment(segment) {
|
291
|
+
return segment.replace(UriEncoderSegmentRegex, function (str) {
|
292
|
+
return encodeURIComponent(str);
|
293
|
+
});
|
294
|
+
}
|
295
|
+
is_optional_node(node) {
|
296
|
+
return [NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT].includes(node);
|
297
|
+
}
|
298
|
+
build_path_spec(route, wildcard = false) {
|
299
|
+
let key;
|
300
|
+
switch (route[0]) {
|
301
|
+
case NodeTypes.GROUP:
|
302
|
+
return "(" + this.build_path_spec(route[1]) + ")";
|
303
|
+
case NodeTypes.CAT:
|
304
|
+
return (this.build_path_spec(route[1]) + this.build_path_spec(route[2]));
|
305
|
+
case NodeTypes.STAR:
|
306
|
+
return this.build_path_spec(route[1], true);
|
307
|
+
case NodeTypes.SYMBOL:
|
308
|
+
key = route[1];
|
309
|
+
if (wildcard) {
|
310
|
+
return (key.startsWith("*") ? "" : "*") + key;
|
311
|
+
}
|
312
|
+
else {
|
313
|
+
return ":" + key;
|
314
|
+
}
|
315
|
+
break;
|
316
|
+
case NodeTypes.SLASH:
|
317
|
+
case NodeTypes.DOT:
|
318
|
+
case NodeTypes.LITERAL:
|
319
|
+
return route[1];
|
320
|
+
default:
|
321
|
+
throw new Error("Unknown Rails node type");
|
322
|
+
}
|
323
|
+
}
|
324
|
+
visit_globbing(route, parameters, optional) {
|
325
|
+
const key = route[1];
|
326
|
+
let value = parameters[key];
|
327
|
+
delete parameters[key];
|
328
|
+
if (this.is_nullable(value)) {
|
329
|
+
return this.visit(route, parameters, optional);
|
330
|
+
}
|
331
|
+
if (this.is_array(value)) {
|
332
|
+
value = value.join("/");
|
333
|
+
}
|
334
|
+
value = this.path_identifier(value);
|
335
|
+
return DeprecatedGlobbingBehavior ? "" + value : encodeURI("" + value);
|
336
|
+
}
|
337
|
+
get_prefix() {
|
338
|
+
const prefix = this.configuration.prefix;
|
339
|
+
return prefix.match("/$")
|
340
|
+
? prefix.substring(0, prefix.length - 1)
|
341
|
+
: prefix;
|
342
|
+
}
|
343
|
+
route(parts_table, route_spec, full_url = false) {
|
344
|
+
const required_params = [];
|
345
|
+
const parts = [];
|
346
|
+
const default_options = {};
|
347
|
+
for (const [part, { r: required, d: value }] of Object.entries(parts_table)) {
|
348
|
+
parts.push(part);
|
349
|
+
if (required) {
|
350
|
+
required_params.push(part);
|
351
|
+
}
|
352
|
+
if (this.is_not_nullable(value)) {
|
353
|
+
default_options[part] = value;
|
354
|
+
}
|
355
|
+
}
|
356
|
+
const result = (...args) => {
|
357
|
+
return this.build_route(parts, required_params, default_options, route_spec, full_url, args);
|
358
|
+
};
|
359
|
+
result.requiredParams = () => required_params;
|
360
|
+
result.toString = () => {
|
361
|
+
return this.build_path_spec(route_spec);
|
362
|
+
};
|
363
|
+
return result;
|
364
|
+
}
|
365
|
+
route_url(route_defaults) {
|
366
|
+
const hostname = route_defaults.host || this.current_host();
|
367
|
+
if (!hostname) {
|
368
|
+
return "";
|
369
|
+
}
|
370
|
+
const subdomain = route_defaults.subdomain
|
371
|
+
? route_defaults.subdomain + "."
|
372
|
+
: "";
|
373
|
+
const protocol = route_defaults.protocol || this.current_protocol();
|
374
|
+
let port = route_defaults.port ||
|
375
|
+
(!route_defaults.host ? this.current_port() : undefined);
|
376
|
+
port = port ? ":" + port : "";
|
377
|
+
return protocol + "://" + subdomain + hostname + port;
|
378
|
+
}
|
379
|
+
has_location() {
|
380
|
+
return this.is_not_nullable(window) && !!window.location;
|
381
|
+
}
|
382
|
+
current_host() {
|
383
|
+
if (this.has_location()) {
|
384
|
+
return window.location.hostname;
|
385
|
+
}
|
386
|
+
else {
|
387
|
+
return null;
|
388
|
+
}
|
389
|
+
}
|
390
|
+
current_protocol() {
|
391
|
+
if (this.has_location() && window.location.protocol !== "") {
|
392
|
+
return window.location.protocol.replace(/:$/, "");
|
393
|
+
}
|
394
|
+
else {
|
395
|
+
return "http";
|
396
|
+
}
|
397
|
+
}
|
398
|
+
current_port() {
|
399
|
+
if (this.has_location() && window.location.port !== "") {
|
400
|
+
return window.location.port;
|
401
|
+
}
|
402
|
+
else {
|
403
|
+
return "";
|
404
|
+
}
|
405
|
+
}
|
406
|
+
is_object(value) {
|
407
|
+
return (typeof value === "object" &&
|
408
|
+
Object.prototype.toString.call(value) === "[object Object]");
|
409
|
+
}
|
410
|
+
is_array(object) {
|
411
|
+
return object instanceof Array;
|
412
|
+
}
|
413
|
+
is_callable(object) {
|
414
|
+
return typeof object === "function" && !!object.call;
|
415
|
+
}
|
416
|
+
is_reserved_option(key) {
|
417
|
+
return ReservedOptions.includes(key);
|
418
|
+
}
|
419
|
+
namespace(object, namespace, routes) {
|
420
|
+
const parts = (namespace === null || namespace === void 0 ? void 0 : namespace.split(".")) || [];
|
421
|
+
if (parts.length === 0) {
|
422
|
+
return routes;
|
423
|
+
}
|
424
|
+
for (let index = 0; index < parts.length; index++) {
|
425
|
+
const part = parts[index];
|
426
|
+
if (index < parts.length - 1) {
|
427
|
+
object = object[part] || (object[part] = {});
|
428
|
+
}
|
429
|
+
else {
|
430
|
+
return (object[part] = routes);
|
431
|
+
}
|
432
|
+
}
|
433
|
+
}
|
434
|
+
configure(new_config) {
|
435
|
+
this.configuration = { ...this.configuration, ...new_config };
|
436
|
+
return this.configuration;
|
437
|
+
}
|
438
|
+
config() {
|
439
|
+
return { ...this.configuration };
|
440
|
+
}
|
441
|
+
is_module_supported(name) {
|
442
|
+
return ModuleReferences[name].support();
|
443
|
+
}
|
444
|
+
ensure_module_supported(name) {
|
445
|
+
if (!this.is_module_supported(name)) {
|
446
|
+
throw new Error(`${name} is not supported by runtime`);
|
447
|
+
}
|
448
|
+
}
|
449
|
+
define_module(name, module) {
|
450
|
+
if (!name) {
|
451
|
+
return;
|
452
|
+
}
|
453
|
+
this.ensure_module_supported(name);
|
454
|
+
ModuleReferences[name].define(module);
|
455
|
+
}
|
33
456
|
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
},
|
59
|
-
default_serializer: function(object, prefix) {
|
60
|
-
var element, i, j, key, len, prop, s;
|
61
|
-
if (prefix == null) {
|
62
|
-
prefix = null;
|
63
|
-
}
|
64
|
-
if (object == null) {
|
65
|
-
return "";
|
66
|
-
}
|
67
|
-
if (!prefix && !(this.get_object_type(object) === "object")) {
|
68
|
-
throw new Error("Url parameters should be a javascript hash");
|
69
|
-
}
|
70
|
-
s = [];
|
71
|
-
switch (this.get_object_type(object)) {
|
72
|
-
case "array":
|
73
|
-
for (i = j = 0, len = object.length; j < len; i = ++j) {
|
74
|
-
element = object[i];
|
75
|
-
s.push(this.default_serializer(element, prefix + "[]"));
|
76
|
-
}
|
77
|
-
break;
|
78
|
-
case "object":
|
79
|
-
for (key in object) {
|
80
|
-
if (!hasProp.call(object, key)) continue;
|
81
|
-
prop = object[key];
|
82
|
-
if ((prop == null) && (prefix != null)) {
|
83
|
-
prop = "";
|
84
|
-
}
|
85
|
-
if (prop != null) {
|
86
|
-
if (prefix != null) {
|
87
|
-
key = prefix + "[" + key + "]";
|
88
|
-
}
|
89
|
-
s.push(this.default_serializer(prop, key));
|
90
|
-
}
|
91
|
-
}
|
92
|
-
break;
|
93
|
-
default:
|
94
|
-
if (object != null) {
|
95
|
-
s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
|
96
|
-
}
|
97
|
-
}
|
98
|
-
if (!s.length) {
|
99
|
-
return "";
|
100
|
-
}
|
101
|
-
return s.join("&");
|
102
|
-
},
|
103
|
-
serialize: function(object) {
|
104
|
-
var custom_serializer;
|
105
|
-
custom_serializer = this.configuration.serializer;
|
106
|
-
if ((custom_serializer != null) && this.get_object_type(custom_serializer) === "function") {
|
107
|
-
return custom_serializer(object);
|
108
|
-
} else {
|
109
|
-
return this.default_serializer(object);
|
110
|
-
}
|
111
|
-
},
|
112
|
-
clean_path: function(path) {
|
113
|
-
var last_index;
|
114
|
-
path = path.split("://");
|
115
|
-
last_index = path.length - 1;
|
116
|
-
path[last_index] = path[last_index].replace(/\/+/g, "/");
|
117
|
-
return path.join("://");
|
118
|
-
},
|
119
|
-
extract_options: function(number_of_params, args) {
|
120
|
-
var last_el, options;
|
121
|
-
last_el = args[args.length - 1];
|
122
|
-
if ((args.length > number_of_params && last_el === void 0) || ((last_el != null) && "object" === this.get_object_type(last_el) && !this.looks_like_serialized_model(last_el))) {
|
123
|
-
options = args.pop() || {};
|
124
|
-
delete options[this.configuration.special_options_key];
|
125
|
-
return options;
|
126
|
-
} else {
|
127
|
-
return {};
|
128
|
-
}
|
129
|
-
},
|
130
|
-
looks_like_serialized_model: function(object) {
|
131
|
-
return !object[this.configuration.special_options_key] && ("id" in object || "to_param" in object);
|
132
|
-
},
|
133
|
-
path_identifier: function(object) {
|
134
|
-
var property;
|
135
|
-
if (object === 0) {
|
136
|
-
return "0";
|
137
|
-
}
|
138
|
-
if (!object) {
|
139
|
-
return "";
|
140
|
-
}
|
141
|
-
property = object;
|
142
|
-
if (this.get_object_type(object) === "object") {
|
143
|
-
if ("to_param" in object) {
|
144
|
-
if (object.to_param == null) {
|
145
|
-
throw new ParameterMissing("Route parameter missing: to_param");
|
146
|
-
}
|
147
|
-
property = object.to_param;
|
148
|
-
} else if ("id" in object) {
|
149
|
-
if (object.id == null) {
|
150
|
-
throw new ParameterMissing("Route parameter missing: id");
|
151
|
-
}
|
152
|
-
property = object.id;
|
153
|
-
} else {
|
154
|
-
property = object;
|
155
|
-
}
|
156
|
-
if (this.get_object_type(property) === "function") {
|
157
|
-
property = property.call(object);
|
158
|
-
}
|
159
|
-
}
|
160
|
-
return property.toString();
|
161
|
-
},
|
162
|
-
clone: function(obj) {
|
163
|
-
var attr, copy, key;
|
164
|
-
if ((obj == null) || "object" !== this.get_object_type(obj)) {
|
165
|
-
return obj;
|
166
|
-
}
|
167
|
-
copy = obj.constructor();
|
168
|
-
for (key in obj) {
|
169
|
-
if (!hasProp.call(obj, key)) continue;
|
170
|
-
attr = obj[key];
|
171
|
-
copy[key] = attr;
|
172
|
-
}
|
173
|
-
return copy;
|
174
|
-
},
|
175
|
-
merge: function() {
|
176
|
-
var tap, xs;
|
177
|
-
xs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
178
|
-
tap = function(o, fn) {
|
179
|
-
fn(o);
|
180
|
-
return o;
|
181
|
-
};
|
182
|
-
if ((xs != null ? xs.length : void 0) > 0) {
|
183
|
-
return tap({}, function(m) {
|
184
|
-
var j, k, len, results, v, x;
|
185
|
-
results = [];
|
186
|
-
for (j = 0, len = xs.length; j < len; j++) {
|
187
|
-
x = xs[j];
|
188
|
-
results.push((function() {
|
189
|
-
var results1;
|
190
|
-
results1 = [];
|
191
|
-
for (k in x) {
|
192
|
-
v = x[k];
|
193
|
-
results1.push(m[k] = v);
|
194
|
-
}
|
195
|
-
return results1;
|
196
|
-
})());
|
197
|
-
}
|
198
|
-
return results;
|
199
|
-
});
|
200
|
-
}
|
201
|
-
},
|
202
|
-
normalize_options: function(parts, required_parts, default_options, actual_parameters) {
|
203
|
-
var i, j, key, len, options, part, parts_options, result, route_parts, url_parameters, use_all_parts, value;
|
204
|
-
options = this.extract_options(parts.length, actual_parameters);
|
205
|
-
if (actual_parameters.length > parts.length) {
|
206
|
-
throw new Error("Too many parameters provided for path");
|
207
|
-
}
|
208
|
-
use_all_parts = actual_parameters.length > required_parts.length;
|
209
|
-
parts_options = {};
|
210
|
-
for (key in options) {
|
211
|
-
if (!hasProp.call(options, key)) continue;
|
212
|
-
use_all_parts = true;
|
213
|
-
if (this.indexOf(parts, key) >= 0) {
|
214
|
-
parts_options[key] = value;
|
215
|
-
}
|
216
|
-
}
|
217
|
-
options = this.merge(this.configuration.default_url_options, default_options, options);
|
218
|
-
result = {};
|
219
|
-
url_parameters = {};
|
220
|
-
result['url_parameters'] = url_parameters;
|
221
|
-
for (key in options) {
|
222
|
-
if (!hasProp.call(options, key)) continue;
|
223
|
-
value = options[key];
|
224
|
-
if (this.indexOf(ReservedOptions, key) >= 0) {
|
225
|
-
result[key] = value;
|
226
|
-
} else {
|
227
|
-
url_parameters[key] = value;
|
228
|
-
}
|
229
|
-
}
|
230
|
-
route_parts = use_all_parts ? parts : required_parts;
|
231
|
-
i = 0;
|
232
|
-
for (j = 0, len = route_parts.length; j < len; j++) {
|
233
|
-
part = route_parts[j];
|
234
|
-
if (i < actual_parameters.length) {
|
235
|
-
if (!parts_options.hasOwnProperty(part)) {
|
236
|
-
url_parameters[part] = actual_parameters[i];
|
237
|
-
++i;
|
238
|
-
}
|
239
|
-
}
|
240
|
-
}
|
241
|
-
return result;
|
242
|
-
},
|
243
|
-
build_route: function(parts, required_parts, default_options, route, full_url, args) {
|
244
|
-
var options, parameters, result, url, url_params;
|
245
|
-
args = Array.prototype.slice.call(args);
|
246
|
-
options = this.normalize_options(parts, required_parts, default_options, args);
|
247
|
-
parameters = options['url_parameters'];
|
248
|
-
result = "" + (this.get_prefix()) + (this.visit(route, parameters));
|
249
|
-
url = Utils.clean_path(result);
|
250
|
-
if (options['trailing_slash'] === true) {
|
251
|
-
url = url.replace(/(.*?)[\/]?$/, "$1/");
|
252
|
-
}
|
253
|
-
if ((url_params = this.serialize(parameters)).length) {
|
254
|
-
url += "?" + url_params;
|
255
|
-
}
|
256
|
-
url += options.anchor ? "#" + options.anchor : "";
|
257
|
-
if (full_url) {
|
258
|
-
url = this.route_url(options) + url;
|
259
|
-
}
|
260
|
-
return url;
|
261
|
-
},
|
262
|
-
visit: function(route, parameters, optional) {
|
263
|
-
var left, left_part, right, right_part, type, value;
|
264
|
-
if (optional == null) {
|
265
|
-
optional = false;
|
266
|
-
}
|
267
|
-
type = route[0], left = route[1], right = route[2];
|
268
|
-
switch (type) {
|
269
|
-
case NodeTypes.GROUP:
|
270
|
-
return this.visit(left, parameters, true);
|
271
|
-
case NodeTypes.STAR:
|
272
|
-
return this.visit_globbing(left, parameters, true);
|
273
|
-
case NodeTypes.LITERAL:
|
274
|
-
case NodeTypes.SLASH:
|
275
|
-
case NodeTypes.DOT:
|
276
|
-
return left;
|
277
|
-
case NodeTypes.CAT:
|
278
|
-
left_part = this.visit(left, parameters, optional);
|
279
|
-
right_part = this.visit(right, parameters, optional);
|
280
|
-
if (optional && ((this.is_optional_node(left[0]) && !left_part) || ((this.is_optional_node(right[0])) && !right_part))) {
|
281
|
-
return "";
|
282
|
-
}
|
283
|
-
return "" + left_part + right_part;
|
284
|
-
case NodeTypes.SYMBOL:
|
285
|
-
value = parameters[left];
|
286
|
-
delete parameters[left];
|
287
|
-
if (value != null) {
|
288
|
-
return this.encode_segment(this.path_identifier(value));
|
289
|
-
}
|
290
|
-
if (optional) {
|
291
|
-
return "";
|
292
|
-
} else {
|
293
|
-
throw new ParameterMissing("Route parameter missing: " + left);
|
294
|
-
}
|
295
|
-
break;
|
296
|
-
default:
|
297
|
-
throw new Error("Unknown Rails node type");
|
298
|
-
}
|
299
|
-
},
|
300
|
-
encode_segment: function(segment) {
|
301
|
-
return segment.replace(UriEncoderSegmentRegex, function(str) {
|
302
|
-
return encodeURIComponent(str);
|
303
|
-
});
|
304
|
-
},
|
305
|
-
is_optional_node: function(node) {
|
306
|
-
return this.indexOf([NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT], node) >= 0;
|
307
|
-
},
|
308
|
-
build_path_spec: function(route, wildcard) {
|
309
|
-
var left, right, type;
|
310
|
-
if (wildcard == null) {
|
311
|
-
wildcard = false;
|
312
|
-
}
|
313
|
-
type = route[0], left = route[1], right = route[2];
|
314
|
-
switch (type) {
|
315
|
-
case NodeTypes.GROUP:
|
316
|
-
return "(" + (this.build_path_spec(left)) + ")";
|
317
|
-
case NodeTypes.CAT:
|
318
|
-
return "" + (this.build_path_spec(left)) + (this.build_path_spec(right));
|
319
|
-
case NodeTypes.STAR:
|
320
|
-
return this.build_path_spec(left, true);
|
321
|
-
case NodeTypes.SYMBOL:
|
322
|
-
if (wildcard === true) {
|
323
|
-
return "" + (left[0] === '*' ? '' : '*') + left;
|
324
|
-
} else {
|
325
|
-
return ":" + left;
|
326
|
-
}
|
327
|
-
break;
|
328
|
-
case NodeTypes.SLASH:
|
329
|
-
case NodeTypes.DOT:
|
330
|
-
case NodeTypes.LITERAL:
|
331
|
-
return left;
|
332
|
-
default:
|
333
|
-
throw new Error("Unknown Rails node type");
|
334
|
-
}
|
335
|
-
},
|
336
|
-
visit_globbing: function(route, parameters, optional) {
|
337
|
-
var left, right, type, value;
|
338
|
-
type = route[0], left = route[1], right = route[2];
|
339
|
-
value = parameters[left];
|
340
|
-
delete parameters[left];
|
341
|
-
if (value == null) {
|
342
|
-
return this.visit(route, parameters, optional);
|
343
|
-
}
|
344
|
-
value = (function() {
|
345
|
-
switch (this.get_object_type(value)) {
|
346
|
-
case "array":
|
347
|
-
return value.join("/");
|
348
|
-
default:
|
349
|
-
return value;
|
350
|
-
}
|
351
|
-
}).call(this);
|
352
|
-
if (DeprecatedGlobbingBehavior) {
|
353
|
-
return this.path_identifier(value);
|
354
|
-
} else {
|
355
|
-
return encodeURI(this.path_identifier(value));
|
356
|
-
}
|
357
|
-
},
|
358
|
-
get_prefix: function() {
|
359
|
-
var prefix;
|
360
|
-
prefix = this.configuration.prefix;
|
361
|
-
if (prefix !== "") {
|
362
|
-
prefix = (prefix.match("/$") ? prefix : prefix + "/");
|
363
|
-
}
|
364
|
-
return prefix;
|
365
|
-
},
|
366
|
-
route: function(parts_table, default_options, route_spec, full_url) {
|
367
|
-
var j, len, part, parts, path_fn, ref, required, required_parts;
|
368
|
-
required_parts = [];
|
369
|
-
parts = [];
|
370
|
-
for (j = 0, len = parts_table.length; j < len; j++) {
|
371
|
-
ref = parts_table[j], part = ref[0], required = ref[1];
|
372
|
-
parts.push(part);
|
373
|
-
if (required) {
|
374
|
-
required_parts.push(part);
|
375
|
-
}
|
376
|
-
}
|
377
|
-
path_fn = function() {
|
378
|
-
return Utils.build_route(parts, required_parts, default_options, route_spec, full_url, arguments);
|
379
|
-
};
|
380
|
-
path_fn.required_params = required_parts;
|
381
|
-
path_fn.toString = function() {
|
382
|
-
return Utils.build_path_spec(route_spec);
|
383
|
-
};
|
384
|
-
return path_fn;
|
385
|
-
},
|
386
|
-
route_url: function(route_defaults) {
|
387
|
-
var hostname, port, protocol, subdomain;
|
388
|
-
if (typeof route_defaults === 'string') {
|
389
|
-
return route_defaults;
|
390
|
-
}
|
391
|
-
hostname = route_defaults.host || Utils.current_host();
|
392
|
-
if (!hostname) {
|
393
|
-
return '';
|
394
|
-
}
|
395
|
-
subdomain = route_defaults.subdomain ? route_defaults.subdomain + '.' : '';
|
396
|
-
protocol = route_defaults.protocol || Utils.current_protocol();
|
397
|
-
port = route_defaults.port || (!route_defaults.host ? Utils.current_port() : void 0);
|
398
|
-
port = port ? ":" + port : '';
|
399
|
-
return protocol + "://" + subdomain + hostname + port;
|
400
|
-
},
|
401
|
-
has_location: function() {
|
402
|
-
return (typeof window !== "undefined" && window !== null ? window.location : void 0) != null;
|
403
|
-
},
|
404
|
-
current_host: function() {
|
405
|
-
if (this.has_location()) {
|
406
|
-
return window.location.hostname;
|
407
|
-
} else {
|
408
|
-
return null;
|
409
|
-
}
|
410
|
-
},
|
411
|
-
current_protocol: function() {
|
412
|
-
if (this.has_location() && window.location.protocol !== '') {
|
413
|
-
return window.location.protocol.replace(/:$/, '');
|
414
|
-
} else {
|
415
|
-
return 'http';
|
416
|
-
}
|
417
|
-
},
|
418
|
-
current_port: function() {
|
419
|
-
if (this.has_location() && window.location.port !== '') {
|
420
|
-
return window.location.port;
|
421
|
-
} else {
|
422
|
-
return '';
|
423
|
-
}
|
424
|
-
},
|
425
|
-
_classToTypeCache: null,
|
426
|
-
_classToType: function() {
|
427
|
-
var j, len, name, ref;
|
428
|
-
if (this._classToTypeCache != null) {
|
429
|
-
return this._classToTypeCache;
|
430
|
-
}
|
431
|
-
this._classToTypeCache = {};
|
432
|
-
ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
|
433
|
-
for (j = 0, len = ref.length; j < len; j++) {
|
434
|
-
name = ref[j];
|
435
|
-
this._classToTypeCache["[object " + name + "]"] = name.toLowerCase();
|
436
|
-
}
|
437
|
-
return this._classToTypeCache;
|
438
|
-
},
|
439
|
-
get_object_type: function(obj) {
|
440
|
-
if (root.jQuery && (root.jQuery.type != null)) {
|
441
|
-
return root.jQuery.type(obj);
|
442
|
-
}
|
443
|
-
if (obj == null) {
|
444
|
-
return "" + obj;
|
445
|
-
}
|
446
|
-
if (typeof obj === "object" || typeof obj === "function") {
|
447
|
-
return this._classToType()[Object.prototype.toString.call(obj)] || "object";
|
448
|
-
} else {
|
449
|
-
return typeof obj;
|
450
|
-
}
|
451
|
-
},
|
452
|
-
indexOf: function(array, element) {
|
453
|
-
if (Array.prototype.indexOf) {
|
454
|
-
return array.indexOf(element);
|
455
|
-
} else {
|
456
|
-
return this.indexOfImplementation(array, element);
|
457
|
-
}
|
458
|
-
},
|
459
|
-
indexOfImplementation: function(array, element) {
|
460
|
-
var el, i, j, len, result;
|
461
|
-
result = -1;
|
462
|
-
for (i = j = 0, len = array.length; j < len; i = ++j) {
|
463
|
-
el = array[i];
|
464
|
-
if (el === element) {
|
465
|
-
result = i;
|
466
|
-
}
|
467
|
-
}
|
468
|
-
return result;
|
469
|
-
},
|
470
|
-
namespace: function(root, namespace, routes) {
|
471
|
-
var index, j, len, part, parts;
|
472
|
-
parts = namespace ? namespace.split(".") : [];
|
473
|
-
if (parts.length === 0) {
|
474
|
-
return routes;
|
475
|
-
}
|
476
|
-
for (index = j = 0, len = parts.length; j < len; index = ++j) {
|
477
|
-
part = parts[index];
|
478
|
-
if (index < parts.length - 1) {
|
479
|
-
root = (root[part] || (root[part] = {}));
|
480
|
-
} else {
|
481
|
-
return root[part] = routes;
|
482
|
-
}
|
483
|
-
}
|
484
|
-
},
|
485
|
-
configure: function(new_config) {
|
486
|
-
return this.configuration = this.merge(this.configuration, new_config);
|
487
|
-
},
|
488
|
-
config: function() {
|
489
|
-
return this.clone(this.configuration);
|
490
|
-
},
|
491
|
-
make: function() {
|
492
|
-
var routes;
|
493
|
-
routes = ROUTES;
|
494
|
-
routes.configure = function(config) {
|
495
|
-
return Utils.configure(config);
|
496
|
-
};
|
497
|
-
routes.config = function() {
|
498
|
-
return Utils.config();
|
499
|
-
};
|
500
|
-
routes.default_serializer = function(object, prefix) {
|
501
|
-
return Utils.default_serializer(object, prefix);
|
502
|
-
};
|
503
|
-
Utils.namespace(root, NAMESPACE, routes);
|
504
|
-
return Object.assign({
|
505
|
-
"default": routes
|
506
|
-
}, routes);
|
457
|
+
const Utils = new UtilsClass();
|
458
|
+
// We want this helper name to be short
|
459
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
460
|
+
const __jsr = {
|
461
|
+
r(parts_table, route_spec, full_url) {
|
462
|
+
return Utils.route(parts_table, route_spec, full_url);
|
463
|
+
},
|
464
|
+
};
|
465
|
+
const result = {
|
466
|
+
...__jsr,
|
467
|
+
configure: (config) => {
|
468
|
+
return Utils.configure(config);
|
469
|
+
},
|
470
|
+
config: () => {
|
471
|
+
return Utils.config();
|
472
|
+
},
|
473
|
+
serialize: (object) => {
|
474
|
+
return Utils.serialize(object);
|
475
|
+
},
|
476
|
+
...RubyVariables.ROUTES_OBJECT,
|
477
|
+
};
|
478
|
+
Utils.namespace(Root, RubyVariables.NAMESPACE, result);
|
479
|
+
if (RubyVariables.MODULE_TYPE) {
|
480
|
+
Utils.define_module(RubyVariables.MODULE_TYPE, result);
|
507
481
|
}
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
if (typeof define === "function" && define.amd) {
|
513
|
-
define([], function() {
|
514
|
-
return result;
|
515
|
-
});
|
516
|
-
}
|
517
|
-
|
518
|
-
return result;
|
519
|
-
|
520
|
-
}).call(this);
|
482
|
+
return result;
|
483
|
+
})(this);
|
484
|
+
//# sourceMappingURL=routes.js.map
|