Commit 147aa91b authored by Thomas Lechauve's avatar Thomas Lechauve

Can parse, serialize and deserialize an hashtag

parent 1bbc3909
...@@ -4,11 +4,14 @@ ...@@ -4,11 +4,14 @@
router: { router: {
routes: { routes: {
list: [], list: [],
current: null,
add: function (route, level, callback, context) { add: function (route, level, callback, context) {
var r, keys, i;
if (typeof this.list[level] === 'undefined') { if (typeof this.list[level] === 'undefined') {
this.list[level] = []; this.list[level] = [];
} }
var r = { route = route.replace(/:\w+/g, '([^\/]+)');
r = {
'route': route, 'route': route,
'level': level, 'level': level,
'callback': function (params) { 'callback': function (params) {
...@@ -19,7 +22,7 @@ ...@@ -19,7 +22,7 @@
callback.call(context, params); callback.call(context, params);
} }
} }
} },
}, },
i = this.list[level].length; i = this.list[level].length;
this.list[level][i] = r; this.list[level][i] = r;
...@@ -29,14 +32,75 @@ ...@@ -29,14 +32,75 @@
}, },
cleanAll: function () { cleanAll: function () {
this.list = this.list.slice(0, 0); this.list = this.list.slice(0, 0);
},
search: function (hash) {
var stop = false,
i = 0,
j = 0,
regex,
result;
console.log(hash)
while ((stop === false) && (i < this.list.length)) {
while ((stop === false) && (j < this.list[i].length)) {
regex = new RegExp('^' + this.list[i][j].route + '$');
if (regex.test(hash.route)) {
result = regex.exec(hash.route);
stop = true;
console.log(result)
result.shift();
//delete hash.route;
this.list[i][j].callback(hash);
}
j += 1;
}
i += 1;
}
}
},
deserialize: function (params) {
var result = {},
p,
params = params.split('&');
while (params.length) {
p = params.shift().split('=');
if (p[0] !== '') {
if (p.length === 2) {
result[p[0]] = p[1] === 'true' ? true : p[1];
} else {
result[p[0]] = true;
}
}
}
return result;
},
serialize: function (obj) {
return $.param(obj);
},
parseHash: function (hashTag) {
var re = new RegExp(/(?:^#([a-zA-Z0-9\/_-]+))(?:\?([A-Za-z0-9\/&=_-]+))?/g),
groups = re.exec(hashTag),
r, params = {};
groups.shift();
// route
r = groups[0];
// params
if (groups[1] !== undefined) {
params = this.deserialize(groups[1]);
} }
return params.length === 0 ? {'route': r} : $.extend({'route': r}, params);
}, },
hashHandler: function () { hashHandler: function () {
var newHash = window.location.href.split('#')[1]; var hashTag = window.location.href.split('#')[1],
console.log('hash change to : ' + newHash); hashInfo = this.parseHash(hashTag);
this.routes.call(hashInfo)
}, },
} }
}); });
$(window).bind('hashchange', $.router.hashchangeHandler); $(window).bind('hashchange', $.router.hashchangeHandler);
$(window).bind('load', $.router.hashchangeHandler);
}(jQuery)); }(jQuery));
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
<!-- Load local QUnit (grunt requires v1.0.0 or newer). --> <!-- Load local QUnit (grunt requires v1.0.0 or newer). -->
<link rel="stylesheet" href="../libs/qunit/qunit.css" media="screen"> <link rel="stylesheet" href="../libs/qunit/qunit.css" media="screen">
<script src="../libs/qunit/qunit.js"></script> <script src="../libs/qunit/qunit.js"></script>
<!--Load SinonJS-->
<script src="../libs/sinon/sinon-1.3.2.js"></script>
<script src="../libs/sinon/sinon-qunit-1.0.0.js"></script>
<!-- Load local lib and tests. --> <!-- Load local lib and tests. -->
<script src="../src/jquery.urljs.js"></script> <script src="../src/jquery.urljs.js"></script>
<script src="jquery.urljs_test.js"></script> <script src="jquery.urljs_test.js"></script>
......
$(function () { $(function () {
module("routes list tests", { module('routes list tests', {
setup: function () { setup: function () {
var url1 = "/url1", var url1 = '/url1',
url11 = "/url1/1", url11 = '/url1/1',
url12 = "/url1/2", url12 = '/url1/2',
url111 = "/url1/1/1", url111 = '/url1/1/1',
url2 = "/url2"; url2 = '/url2';
$.router.routes.add(url1, 0); $.router.routes.add(url1, 0);
$.router.routes.add(url2, 0); $.router.routes.add(url2, 0);
$.router.routes.add(url11, 1); $.router.routes.add(url11, 1);
...@@ -13,27 +13,87 @@ $(function () { ...@@ -13,27 +13,87 @@ $(function () {
$.router.routes.add(url111, 2); $.router.routes.add(url111, 2);
}, },
teardown: function () { teardown: function () {
$.router.routes.clean(0); $.router.routes.cleanAll();
} }
}); });
test('add test', function () { test('add tests', function () {
console.log($.router.routes.list) equal($.router.routes.list.length, 3, 'should contains 3 levels');
equal($.router.routes.list.length, 3, "should contains 3 levels"); equal($.router.routes.list[0].length, 2, 'should contains 2 routes level 0');
equal($.router.routes.list[0].length, 2, "should contains 2 routes level 0"); equal($.router.routes.list[1].length, 2, 'should contains 2 routes level 1');
equal($.router.routes.list[1].length, 2, "should contains 2 routes level 1"); equal($.router.routes.list[2].length, 1, 'should contains 1 routes level 2')
equal($.router.routes.list[2].length, 1, "should contains 1 routes level 2");
}); });
test('clean test', function () { test('clean test', function () {
$.router.routes.clean(2); $.router.routes.clean(2);
equal($.router.routes.list.length, 2, "should remove all routes level 2"); equal($.router.routes.list.length, 2, 'should remove all routes level 2');
$.router.routes.clean(1); $.router.routes.clean(1);
equal($.router.routes.list.length, 1, "should remove all routes level 1"); equal($.router.routes.list.length, 1, 'should remove all routes level 1');
}); });
test('clean all test', function () { test('clean all test', function () {
$.router.routes.cleanAll(); $.router.routes.cleanAll();
equal($.router.routes.list.length, 0, "should remove all routes"); equal($.router.routes.list.length, 0, 'should remove all routes');
});
module('search tests', {
teardown: function () {
$.router.routes.cleanAll();
}
});
test('search test', function () {
var url1 = {'route': '#/new/path', 'param1': 'foo1', 'param2': 'foo2', 'filter': true},
url2 = {'route': '#/new/path/1', 'param1': 'foo1'},
spy1 = sinon.spy(),
spy2 = sinon.spy();
console.log(url1);
$.router.routes.add(url1.route, 0, spy1);
$.router.routes.add('#/new/path/:id', 1, spy2);
$.router.routes.search(url1);
//delete url1.route;
//ok(spy1.calledOnce);
//ok(spy1.calledWith(url1));
//$.router.routes.search(url2);
//delete url2.route;
//ok(spy2.calledOnce);
//ok(spy2.calledWith(url2));
});
module('router methods tests', {
});
test('serialize tests', function () {
deepEqual($.router.serialize({
'param1': 'foo1',
'param2': 'foo2',
'filter': true
}), 'param1=foo1&param2=foo2&filter=true');
});
test('deserialize tests', function () {
deepEqual({
'param1': 'foo1',
'param2': 'foo2',
'filter': true
}, $.router.deserialize('param1=foo1&param2=foo2&filter=true'));
});
test('parseHash tests', function () {
var response;
urls = {
'#/url1' : {'route': '/url1'},
'#/url2?param1=foo1&param2=foo2': {'route': '/url2', 'param1': 'foo1', 'param2': 'foo2'},
'#/url3?param1=foo1&param2=foo2&search': {'route': '/url3', 'param1': 'foo1', 'param2': 'foo2', 'search': true},
'#/url4?&': {'route': '/url4'},
'#/url5?param1=foo1&filter&row=4&redirect=/instance': {'route': '/url5', 'param1': 'foo1', 'filter': true, 'row': '4', 'redirect': '/instance'}
};
for (var url in urls) {
response = $.router.parseHash(url);
deepEqual(response, urls[url]);
}
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment