js-routes-zigexn 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/routes.js ADDED
@@ -0,0 +1,470 @@
1
+ /*
2
+ File generated by js-routes GEM_VERSION
3
+ Based on Rails routes of APP_CLASS
4
+ */
5
+
6
+ (function() {
7
+ var DeprecatedBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, defaults, root,
8
+ hasProp = {}.hasOwnProperty,
9
+ slice = [].slice;
10
+
11
+ root = typeof exports !== "undefined" && exports !== null ? exports : this;
12
+
13
+ ParameterMissing = function(message) {
14
+ this.message = message;
15
+ };
16
+
17
+ ParameterMissing.prototype = new Error();
18
+
19
+ defaults = {
20
+ prefix: PREFIX,
21
+ default_url_options: DEFAULT_URL_OPTIONS
22
+ };
23
+
24
+ if(window.currentLang){
25
+ defaults.default_url_options['lang'] = window.currentLang;
26
+ }
27
+
28
+ NodeTypes = NODE_TYPES;
29
+
30
+ SpecialOptionsKey = SPECIAL_OPTIONS_KEY;
31
+
32
+ DeprecatedBehavior = DEPRECATED_BEHAVIOR;
33
+
34
+ ReservedOptions = ['anchor', 'trailing_slash', 'host', 'port', 'protocol'];
35
+
36
+ Utils = {
37
+ default_serializer: function(object, prefix) {
38
+ var element, i, j, key, len, prop, s;
39
+ if (prefix == null) {
40
+ prefix = null;
41
+ }
42
+ if (object == null) {
43
+ return "";
44
+ }
45
+ if (!prefix && !(this.get_object_type(object) === "object")) {
46
+ throw new Error("Url parameters should be a javascript hash");
47
+ }
48
+ s = [];
49
+ switch (this.get_object_type(object)) {
50
+ case "array":
51
+ for (i = j = 0, len = object.length; j < len; i = ++j) {
52
+ element = object[i];
53
+ s.push(this.default_serializer(element, prefix + "[]"));
54
+ }
55
+ break;
56
+ case "object":
57
+ for (key in object) {
58
+ if (!hasProp.call(object, key)) continue;
59
+ prop = object[key];
60
+ if ((prop == null) && (prefix != null)) {
61
+ prop = "";
62
+ }
63
+ if (prop != null) {
64
+ if (prefix != null) {
65
+ key = prefix + "[" + key + "]";
66
+ }
67
+ s.push(this.default_serializer(prop, key));
68
+ }
69
+ }
70
+ break;
71
+ default:
72
+ if (object != null) {
73
+ s.push((encodeURIComponent(prefix.toString())) + "=" + (encodeURIComponent(object.toString())));
74
+ }
75
+ }
76
+ if (!s.length) {
77
+ return "";
78
+ }
79
+ return s.join("&");
80
+ },
81
+ custom_serializer: SERIALIZER,
82
+ serialize: function(object) {
83
+ if ((this.custom_serializer != null) && this.get_object_type(this.custom_serializer) === "function") {
84
+ return this.custom_serializer(object);
85
+ } else {
86
+ return this.default_serializer(object);
87
+ }
88
+ },
89
+ clean_path: function(path) {
90
+ var last_index;
91
+ path = path.split("://");
92
+ last_index = path.length - 1;
93
+ path[last_index] = path[last_index].replace(/\/+/g, "/");
94
+ return path.join("://");
95
+ },
96
+ extract_options: function(number_of_params, args) {
97
+ var last_el, options;
98
+ last_el = args[args.length - 1];
99
+ 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))) {
100
+ options = args.pop() || {};
101
+ delete options[SpecialOptionsKey];
102
+ return options;
103
+ } else {
104
+ return {};
105
+ }
106
+ },
107
+ looks_like_serialized_model: function(object) {
108
+ return !object[SpecialOptionsKey] && ("id" in object || "to_param" in object);
109
+ },
110
+ path_identifier: function(object) {
111
+ var property;
112
+ if (object === 0) {
113
+ return "0";
114
+ }
115
+ if (!object) {
116
+ return "";
117
+ }
118
+ property = object;
119
+ if (this.get_object_type(object) === "object") {
120
+ if ("to_param" in object) {
121
+ if (object.to_param == null) {
122
+ throw new ParameterMissing("Route parameter missing: to_param");
123
+ }
124
+ property = object.to_param;
125
+ } else if ("id" in object) {
126
+ if (object.id == null) {
127
+ throw new ParameterMissing("Route parameter missing: id");
128
+ }
129
+ property = object.id;
130
+ } else {
131
+ property = object;
132
+ }
133
+ if (this.get_object_type(property) === "function") {
134
+ property = property.call(object);
135
+ }
136
+ }
137
+ return property.toString();
138
+ },
139
+ clone: function(obj) {
140
+ var attr, copy, key;
141
+ if ((obj == null) || "object" !== this.get_object_type(obj)) {
142
+ return obj;
143
+ }
144
+ copy = obj.constructor();
145
+ for (key in obj) {
146
+ if (!hasProp.call(obj, key)) continue;
147
+ attr = obj[key];
148
+ copy[key] = attr;
149
+ }
150
+ return copy;
151
+ },
152
+ merge: function() {
153
+ var tap, xs;
154
+ xs = 1 <= arguments.length ? slice.call(arguments, 0) : [];
155
+ tap = function(o, fn) {
156
+ fn(o);
157
+ return o;
158
+ };
159
+ if ((xs != null ? xs.length : void 0) > 0) {
160
+ return tap({}, function(m) {
161
+ var j, k, len, results, v, x;
162
+ results = [];
163
+ for (j = 0, len = xs.length; j < len; j++) {
164
+ x = xs[j];
165
+ results.push((function() {
166
+ var results1;
167
+ results1 = [];
168
+ for (k in x) {
169
+ v = x[k];
170
+ results1.push(m[k] = v);
171
+ }
172
+ return results1;
173
+ })());
174
+ }
175
+ return results;
176
+ });
177
+ }
178
+ },
179
+ normalize_options: function(parts, required_parts, default_options, actual_parameters) {
180
+ var i, j, key, len, options, part, parts_options, result, route_parts, url_parameters, use_all_parts, value;
181
+ options = this.extract_options(parts.length, actual_parameters);
182
+ if (actual_parameters.length > parts.length) {
183
+ throw new Error("Too many parameters provided for path");
184
+ }
185
+ use_all_parts = DeprecatedBehavior || actual_parameters.length > required_parts.length;
186
+ parts_options = {};
187
+ for (key in options) {
188
+ if (!hasProp.call(options, key)) continue;
189
+ use_all_parts = true;
190
+ if (this.indexOf(parts, key) >= 0) {
191
+ parts_options[key] = value;
192
+ }
193
+ }
194
+ options = this.merge(defaults.default_url_options, default_options, options);
195
+ result = {};
196
+ url_parameters = {};
197
+ result['url_parameters'] = url_parameters;
198
+ for (key in options) {
199
+ if (!hasProp.call(options, key)) continue;
200
+ value = options[key];
201
+ if (this.indexOf(ReservedOptions, key) >= 0) {
202
+ result[key] = value;
203
+ } else {
204
+ url_parameters[key] = value;
205
+ }
206
+ }
207
+ route_parts = use_all_parts ? parts : required_parts;
208
+ i = 0;
209
+ for (j = 0, len = route_parts.length; j < len; j++) {
210
+ part = route_parts[j];
211
+ if (i < actual_parameters.length) {
212
+ if (!parts_options.hasOwnProperty(part)) {
213
+ url_parameters[part] = actual_parameters[i];
214
+ ++i;
215
+ }
216
+ }
217
+ }
218
+ return result;
219
+ },
220
+ build_route: function(parts, required_parts, default_options, route, full_url, args) {
221
+ var options, parameters, result, url, url_params;
222
+ args = Array.prototype.slice.call(args);
223
+ options = this.normalize_options(parts, required_parts, default_options, args);
224
+ parameters = options['url_parameters'];
225
+ result = "" + (this.get_prefix()) + (this.visit(route, parameters));
226
+ url = Utils.clean_path(result);
227
+ if (options['trailing_slash'] === true) {
228
+ url = url.replace(/(.*?)[\/]?$/, "$1/");
229
+ }
230
+ if ((url_params = this.serialize(parameters)).length) {
231
+ url += "?" + url_params;
232
+ }
233
+ url += options.anchor ? "#" + options.anchor : "";
234
+ if (full_url) {
235
+ url = this.route_url(options) + url;
236
+ }
237
+ return url;
238
+ },
239
+ visit: function(route, parameters, optional) {
240
+ var left, left_part, right, right_part, type, value;
241
+ if (optional == null) {
242
+ optional = false;
243
+ }
244
+ type = route[0], left = route[1], right = route[2];
245
+ switch (type) {
246
+ case NodeTypes.GROUP:
247
+ return this.visit(left, parameters, true);
248
+ case NodeTypes.STAR:
249
+ return this.visit_globbing(left, parameters, true);
250
+ case NodeTypes.LITERAL:
251
+ case NodeTypes.SLASH:
252
+ case NodeTypes.DOT:
253
+ return left;
254
+ case NodeTypes.CAT:
255
+ left_part = this.visit(left, parameters, optional);
256
+ right_part = this.visit(right, parameters, optional);
257
+ if (optional && ((this.is_optional_node(left[0]) && !left_part) || ((this.is_optional_node(right[0])) && !right_part))) {
258
+ return "";
259
+ }
260
+ return "" + left_part + right_part;
261
+ case NodeTypes.SYMBOL:
262
+ value = parameters[left];
263
+ if (value != null) {
264
+ delete parameters[left];
265
+ return this.path_identifier(value);
266
+ }
267
+ if (optional) {
268
+ return "";
269
+ } else {
270
+ throw new ParameterMissing("Route parameter missing: " + left);
271
+ }
272
+ break;
273
+ default:
274
+ throw new Error("Unknown Rails node type");
275
+ }
276
+ },
277
+ is_optional_node: function(node) {
278
+ return this.indexOf([NodeTypes.STAR, NodeTypes.SYMBOL, NodeTypes.CAT], node) >= 0;
279
+ },
280
+ build_path_spec: function(route, wildcard) {
281
+ var left, right, type;
282
+ if (wildcard == null) {
283
+ wildcard = false;
284
+ }
285
+ type = route[0], left = route[1], right = route[2];
286
+ switch (type) {
287
+ case NodeTypes.GROUP:
288
+ return "(" + (this.build_path_spec(left)) + ")";
289
+ case NodeTypes.CAT:
290
+ return "" + (this.build_path_spec(left)) + (this.build_path_spec(right));
291
+ case NodeTypes.STAR:
292
+ return this.build_path_spec(left, true);
293
+ case NodeTypes.SYMBOL:
294
+ if (wildcard === true) {
295
+ return "" + (left[0] === '*' ? '' : '*') + left;
296
+ } else {
297
+ return ":" + left;
298
+ }
299
+ break;
300
+ case NodeTypes.SLASH:
301
+ case NodeTypes.DOT:
302
+ case NodeTypes.LITERAL:
303
+ return left;
304
+ default:
305
+ throw new Error("Unknown Rails node type");
306
+ }
307
+ },
308
+ visit_globbing: function(route, parameters, optional) {
309
+ var left, right, type, value;
310
+ type = route[0], left = route[1], right = route[2];
311
+ if (left.replace(/^\*/i, "") !== left) {
312
+ route[1] = left = left.replace(/^\*/i, "");
313
+ }
314
+ value = parameters[left];
315
+ if (value == null) {
316
+ return this.visit(route, parameters, optional);
317
+ }
318
+ parameters[left] = (function() {
319
+ switch (this.get_object_type(value)) {
320
+ case "array":
321
+ return value.join("/");
322
+ default:
323
+ return value;
324
+ }
325
+ }).call(this);
326
+ return this.visit(route, parameters, optional);
327
+ },
328
+ get_prefix: function() {
329
+ var prefix;
330
+ prefix = defaults.prefix;
331
+ if (prefix !== "") {
332
+ prefix = (prefix.match("/$") ? prefix : prefix + "/");
333
+ }
334
+ return prefix;
335
+ },
336
+ route: function(parts_table, default_options, route_spec, full_url) {
337
+ var j, len, part, parts, path_fn, ref, required, required_parts;
338
+ required_parts = [];
339
+ parts = [];
340
+ for (j = 0, len = parts_table.length; j < len; j++) {
341
+ ref = parts_table[j], part = ref[0], required = ref[1];
342
+ parts.push(part);
343
+ if (required) {
344
+ required_parts.push(part);
345
+ }
346
+ }
347
+ path_fn = function() {
348
+ return Utils.build_route(parts, required_parts, default_options, route_spec, full_url, arguments);
349
+ };
350
+ path_fn.required_params = required_parts;
351
+ path_fn.toString = function() {
352
+ return Utils.build_path_spec(route_spec);
353
+ };
354
+ return path_fn;
355
+ },
356
+ route_url: function(route_defaults) {
357
+ var hostname, port, protocol;
358
+ if (typeof route_defaults === 'string') {
359
+ return route_defaults;
360
+ }
361
+ protocol = route_defaults.protocol || Utils.current_protocol();
362
+ hostname = route_defaults.host || window.location.hostname;
363
+ port = route_defaults.port || (!route_defaults.host ? Utils.current_port() : void 0);
364
+ port = port ? ":" + port : '';
365
+ return protocol + "://" + hostname + port;
366
+ },
367
+ has_location: function() {
368
+ return typeof window !== 'undefined' && typeof window.location !== 'undefined';
369
+ },
370
+ current_host: function() {
371
+ if (this.has_location()) {
372
+ return window.location.hostname;
373
+ } else {
374
+ return null;
375
+ }
376
+ },
377
+ current_protocol: function() {
378
+ if (this.has_location() && window.location.protocol !== '') {
379
+ return window.location.protocol.replace(/:$/, '');
380
+ } else {
381
+ return 'http';
382
+ }
383
+ },
384
+ current_port: function() {
385
+ if (this.has_location() && window.location.port !== '') {
386
+ return window.location.port;
387
+ } else {
388
+ return '';
389
+ }
390
+ },
391
+ _classToTypeCache: null,
392
+ _classToType: function() {
393
+ var j, len, name, ref;
394
+ if (this._classToTypeCache != null) {
395
+ return this._classToTypeCache;
396
+ }
397
+ this._classToTypeCache = {};
398
+ ref = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
399
+ for (j = 0, len = ref.length; j < len; j++) {
400
+ name = ref[j];
401
+ this._classToTypeCache["[object " + name + "]"] = name.toLowerCase();
402
+ }
403
+ return this._classToTypeCache;
404
+ },
405
+ get_object_type: function(obj) {
406
+ if (root.jQuery && (root.jQuery.type != null)) {
407
+ return root.jQuery.type(obj);
408
+ }
409
+ if (obj == null) {
410
+ return "" + obj;
411
+ }
412
+ if (typeof obj === "object" || typeof obj === "function") {
413
+ return this._classToType()[Object.prototype.toString.call(obj)] || "object";
414
+ } else {
415
+ return typeof obj;
416
+ }
417
+ },
418
+ indexOf: function(array, element) {
419
+ if (Array.prototype.indexOf) {
420
+ return array.indexOf(element);
421
+ } else {
422
+ return this.indexOfImplementation(array, element);
423
+ }
424
+ },
425
+ indexOfImplementation: function(array, element) {
426
+ var el, i, j, len, result;
427
+ result = -1;
428
+ for (i = j = 0, len = array.length; j < len; i = ++j) {
429
+ el = array[i];
430
+ if (el === element) {
431
+ result = i;
432
+ }
433
+ }
434
+ return result;
435
+ },
436
+ namespace: function(root, namespace, routes) {
437
+ var index, j, len, part, parts;
438
+ parts = namespace.split(".");
439
+ if (parts.length === 0) {
440
+ return routes;
441
+ }
442
+ for (index = j = 0, len = parts.length; j < len; index = ++j) {
443
+ part = parts[index];
444
+ if (index < parts.length - 1) {
445
+ root = (root[part] || (root[part] = {}));
446
+ } else {
447
+ return root[part] = routes;
448
+ }
449
+ }
450
+ },
451
+ make: function() {
452
+ var routes;
453
+ routes = ROUTES;
454
+ routes.options = defaults;
455
+ routes.default_serializer = function(object, prefix) {
456
+ return Utils.default_serializer(object, prefix);
457
+ };
458
+ return Utils.namespace(root, NAMESPACE, routes);
459
+ }
460
+ };
461
+
462
+ if (typeof define === "function" && define.amd) {
463
+ define([], function() {
464
+ return Utils.make();
465
+ });
466
+ } else {
467
+ Utils.make();
468
+ }
469
+
470
+ }).call(this);