-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossing.js
More file actions
115 lines (106 loc) · 3.36 KB
/
crossing.js
File metadata and controls
115 lines (106 loc) · 3.36 KB
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
(function () {
'use strict';
// Provides utilities to deal with website/app urls. Provides functionality
// for deep-linking Ajax calls, and a url mapper to generate dynamic urls.
function crossing() {
if (!(this instanceof crossing)) {
throw new Error('Crossing can not be called without instatiation. Please use "new Crossing()" instead');
}
this._urls = {};
this._compiled = {};
this._lastHash = '';
this._nameMatcher = new RegExp('<([a-zA-Z0-9-_%]{1,})>', 'g');
this._getArgs = function(urlName, path) {
var args = {};
var nameMatches = this._urls[urlName].match(this._nameMatcher);
var valueMatches = path.match(this._compiled[urlName]);
if (nameMatches) {
var i, len, arg;
for (i=0, len=nameMatches.length; i<len; i+=1) {
arg = nameMatches[i].substring(1, nameMatches[i].length-1);
args[arg] = valueMatches[i+1];;
}
}
return args;
};
this._getName = function(path) {
if (!path) {
return;
}
for (var url in this._compiled) {
if (path.match(this._compiled[url])) {
return url;
}
}
return;
};
}
// Loads a set of urls names and paths
// @method load
// @static
// @param {Object} urls an Object literal with names and paths like
// {'taskEdit': '/task/edit/<taskId>/', 'taskCreate': '/task/create/'}
// @return {Object} this for method chaining
crossing.prototype.load = function(urls) {
for (var url in urls) {
if (urls.hasOwnProperty(url)) {
this._compiled[url] = new RegExp('^' + urls[url].replace(this._nameMatcher, "([a-zA-Z0-9-_%]{0,})") + '$');
}
}
this._urls = urls;
return this;
};
// Returns a url from the list that matches the specified parameters.
// A non-existant url will raise an exception.
// @method get
// @static
// @param {string} name: url name to call
// @param {string} kwargs: an option object literal with key/value
// that can be used to get urls that require parameters
// @return {String} url path
crossing.prototype.get = function(name, kwargs) {
var path = this._urls[name];
if (!path) {
throw('URL not found: ' + name);
}
var _path = path;
for (var key in kwargs) {
if (kwargs.hasOwnProperty(key)) {
if (!path.match('<' + key +'>')) {
throw('Invalid parameter ('+ key +') for '+ name);
}
path = path.replace('<' + key +'>', kwargs[key]);
}
}
var missingArgs = path.match(this._nameMatcher);
if (missingArgs) {
throw('Missing arguments (' + missingArgs.join(", ") + ') for url ' + _path);
}
return path;
};
// Recieves a url path, and returns a url object with the name and
// variables if there is match in the url list
// @method resolve
// @static
// @param {String} path the url path
// @return {Object/undefined} url object or undefined
crossing.prototype.resolve = function(path) {
var url = {};
var urlName;
var kwargs;
urlName = this._getName(path);
//console.log(url + ' => ' + urlName);
if (urlName) {
kwargs = this._getArgs(urlName, path);
url['name'] = urlName;
url['kwargs'] = kwargs;
return url;
}
return;
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = crossing;
} else {
window.crossing = crossing;
}
})();