This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
url.common.js
138 lines (110 loc) · 4.15 KB
/
url.common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true*/
/*global YUI*/
/**
* @module ActionContextAddon
*/
YUI.add('mojito-url-addon', function(Y, NAME) {
/**
* <strong>Access point:</strong> <em>ac.url.*</em>
* Generates URL's based on the applictions routing configuration
* @class Url.common
*/
function UrlAcAddon(command, adapter, ac) {
this.context = command.context;
this.rs = null;
this.pathToRoot = ac.staticAppConfig.pathToRoot;
this.maker = null;
}
UrlAcAddon.prototype = {
namespace: 'url',
/**
* Generates a URL from the given parameters
* @method make
* @param {string} base Base mojit defined at the root level of the
* Mojito application configuration.
* @param {string} action Action reference, concatenated to the base
* using a period (.) separator.
* @param {object|string} routeParams used to lookup the route in the routing
* table. If string is provided, Y.QueryString.parse will be used.
* @param {string} verb the HTTP method. Default value is "get".
* @param {object|string} urlParams added to the looked up route as query
* params, this has priority over routeParams. If string is provided,
* Y.QueryString.parse will be used.
*/
make: function(base, action, routeParams, verb, urlParams) {
var url,
key,
params,
query = base + '.' + action;
params = (typeof routeParams === 'string' ? Y.QueryString.parse(routeParams) :
Y.merge(routeParams || {}));
if (urlParams) {
urlParams = (typeof urlParams === 'string' ? Y.QueryString.parse(urlParams) :
urlParams);
// adding querystring params to routeParams and let
// the url maker to create the proper url. Empty params
// will be left out. TODO: why?
for (key in urlParams) {
if (urlParams.hasOwnProperty(key) && urlParams[key]) {
params[key] = urlParams[key];
}
}
}
url = this.getRouteMaker().make(query, (verb || 'GET').toUpperCase(), params);
// IOS PATCH
if (url && (typeof window !== 'undefined')) {
url = Y.mojito.util.iOSUrl(url);
}
// this is mainly used by html5app
if (url && this.pathToRoot) {
url = this.pathToRoot + url;
}
return url;
},
/**
* Finds the first matching route from the given URL
* @method find
* @param {string} url the URL to find a route for.
* @param {string} the HTTP method. Default value is "get".
*/
find: function(url, verb) {
// Remove http://some.domain.com/ stuff
if (url.indexOf('http://') === 0) {
url = url.slice(url.indexOf('/', 7));
}
// Remove an query params given
if (url.indexOf('?') > 0) {
url = url.slice(0, url.indexOf('?'));
}
return this.getRouteMaker().find(url, (verb || 'GET').toUpperCase());
},
getRouteMaker: function() {
if (!this.maker) {
this.maker = new Y.mojito.RouteMaker(
this.rs.getRoutes(this.context)
);
}
return this.maker;
},
/**
* Storing a reference to the store.
* @method setStore
* @private
* @param {ResourceStore} rs The resource store instance.
*/
setStore: function(rs) {
this.rs = rs;
}
};
Y.namespace('mojito.addons.ac').url = UrlAcAddon;
}, '0.1.0', {requires: [
'mojito-config-addon',
'mojito-route-maker',
'querystring-parse-simple',
'mojito-util'
]});