Commit 4b8c8371 authored by Thomas Lechauve's avatar Thomas Lechauve

Fix infinite loop while route is unknown

parent 7f42bc1b
...@@ -23,7 +23,20 @@ $.extend({ ...@@ -23,7 +23,20 @@ $.extend({
}, },
}, },
i = this.list[level].length; i = this.list[level].length;
this.list[level][i] = r; if (this.exist(r) === false) {
this.list[level][i] = r;
}
},
exist: function (r) {
var found = false, i = 0;
while (i < this.list[r.level].length && found === false) {
if (this.list[r.level][i].route === r.route) {
found = true;
}
i += 1;
}
return found;
}, },
clean: function (level) { clean: function (level) {
...@@ -34,14 +47,16 @@ $.extend({ ...@@ -34,14 +47,16 @@ $.extend({
this.list = this.list.slice(0, 0); this.list = this.list.slice(0, 0);
}, },
search: function (hash) { search: function (hash, level) {
var stop = false, var stop = false,
i, j, i, j,
regex, regex,
result, result,
extracted; extracted;
level = level || 0;
i = this.list.length - 1; i = this.list.length - 1;
while ((stop === false) && (i >= 0)) { hash.route = hash.route === "undefined" ? "/" : hash.route;
while ((stop === false) && (i >= level)) {
j = 0; j = 0;
while ((stop === false) && (j < this.list[i].length)) { while ((stop === false) && (j < this.list[i].length)) {
extracted = $.router.extractKeys(this.list[i][j].route); extracted = $.router.extractKeys(this.list[i][j].route);
...@@ -54,12 +69,25 @@ $.extend({ ...@@ -54,12 +69,25 @@ $.extend({
hash[extracted.keys[k]] = result[k]; hash[extracted.keys[k]] = result[k];
} }
this.current = this.list[i][j]; this.current = this.list[i][j];
this.clean(this.list[i][j].level + 1);
console.log(this.list[i][j].route)
this.list[i][j].callback(hash); this.list[i][j].callback(hash);
} }
j += 1; j += 1;
} }
i -= 1; i -= 1;
} }
},
isLastLevel: function () {
return this.current.level === (this.list.length - 1);
}
},
start: function (hash, level) {
var hashInfo = this.parseHash(hash);
if ($.router.routes.current.route !== hashInfo.route) {
this.routes.search(hashInfo, level);
} }
}, },
......
...@@ -36,6 +36,7 @@ $(function () { ...@@ -36,6 +36,7 @@ $(function () {
equal($.router.routes.list.length, 0, 'should remove all routes'); equal($.router.routes.list.length, 0, 'should remove all routes');
}); });
module('search tests', { module('search tests', {
teardown: function () { teardown: function () {
$.router.routes.cleanAll(); $.router.routes.cleanAll();
...@@ -43,24 +44,23 @@ $(function () { ...@@ -43,24 +44,23 @@ $(function () {
}); });
test('search test', function () { test('search test', function () {
var url1 = {'route': '#/new/path', 'param1': 'foo1', 'param2': 'foo2', 'filter': true}, var url1 = {'route': '/new/path', 'param1': 'foo1', 'param2': 'foo2', 'filter': true},
url2 = {'route': '#/new/path/1', 'param1': 'foo1'}, url2 = {'route': '/new/path/1', 'param1': 'foo1'},
url3 = {'route': '#/new/path/1/foo', 'param1': 'foo1'}, url3 = {'route': '/new/path/1/foo', 'param1': 'foo1'},
spy = sinon.spy(); spy = sinon.spy();
$.router.routes.add('#/new/path', 0, spy); $.router.routes.add('/new/path', 0, spy);
$.router.routes.add('#/new/path/:id', 1, spy);
$.router.routes.add('#/new/path/:id/:other', 2, spy);
$.router.routes.search(url1); $.router.routes.search(url1);
delete url1.route; delete url1.route;
ok(spy.calledWith(url1)); ok(spy.calledWith(url1));
$.router.routes.add('/new/path/:id', 1, spy);
$.router.routes.search(url2); $.router.routes.search(url2);
delete url2.route; delete url2.route;
$.extend(url2, {'id': '1'}); $.extend(url2, {'id': '1'});
ok(spy.calledWith(url2)); ok(spy.calledWith(url2));
$.router.routes.add('/new/path/:id/:other', 2, spy);
$.router.routes.search(url3); $.router.routes.search(url3);
delete url3.route; delete url3.route;
$.extend(url3, {'id': '1', 'other': 'foo'}); $.extend(url3, {'id': '1', 'other': 'foo'});
...@@ -68,6 +68,19 @@ $(function () { ...@@ -68,6 +68,19 @@ $(function () {
ok(spy.calledThrice); ok(spy.calledThrice);
}); });
test('avoid loop test', function () {
var spy = sinon.spy(),
i = 0,
callback = function (params) {
console.log(params)
$.router.routes.add('/first', 1, spy)
$.router.start(params.route);
}
$.router.routes.add('/', 0, callback);
$.router.routes.search({'route': '/first'});
ok(spy.calledOnce);
});
module('router methods tests', { module('router methods tests', {
}); });
...@@ -103,4 +116,6 @@ $(function () { ...@@ -103,4 +116,6 @@ $(function () {
deepEqual(response, urls[url]); deepEqual(response, urls[url]);
} }
}); });
}); });
...@@ -282,6 +282,10 @@ ...@@ -282,6 +282,10 @@
</div> </div>
</script> </script>
<script id="topbar" type="text/html">
</script>
<script id="root" type="text/html"> <script id="root" type="text/html">
<div id="loading" style="position: absolute; right: 20px; top: 20px;"></div> <div id="loading" style="position: absolute; right: 20px; top: 20px;"></div>
<div class="navbar"> <div class="navbar">
......
...@@ -54,9 +54,9 @@ ...@@ -54,9 +54,9 @@
} }
$(this).vifib('popup', message); $(this).vifib('popup', message);
}, },
spinOptions = {color: "#FFFFFF", lines: 30, length: 0, width: 5, radius: 7, rotate: 0, trail: 60, speed: 1.6}, spinOptions = {color: "#FFFFFF", lines: 30, length: 0, width: 5, radius: 7, rotate: 0, trail: 60, speed: 1.6},
methods = { methods = {
init: function () { init: function () {
var routes = [[['/', methods['showRoot']]]]; var routes = [[['/', methods['showRoot']]]];
...@@ -94,6 +94,9 @@ ...@@ -94,6 +94,9 @@
nextLevel = route.level + 1; nextLevel = route.level + 1;
$(this).vifib('render', 'root'); $(this).vifib('render', 'root');
$.router.routes.add('/catalog', nextLevel, methods.showCatalog, $("#main")); $.router.routes.add('/catalog', nextLevel, methods.showCatalog, $("#main"));
$.router.routes.add('/dashboard', nextLevel, methods.showDashboard, $("#main"));
// default page
$.router.start(params.route, nextLevel);
}, },
genInstanceUrl: function (uri) { genInstanceUrl: function (uri) {
...@@ -103,7 +106,7 @@ ...@@ -103,7 +106,7 @@
extractInstanceURIFromHref: function () { extractInstanceURIFromHref: function () {
return decodeURIComponent($(this).attr('href').split('/').pop()); return decodeURIComponent($(this).attr('href').split('/').pop());
}, },
extractInstanceURIFromHashtag: function () { extractInstanceURIFromHashtag: function () {
var loc = window.location.href.split('#')[1].split('/'), var loc = window.location.href.split('#')[1].split('/'),
i = $.inArray("instance", loc); i = $.inArray("instance", loc);
...@@ -149,15 +152,14 @@ ...@@ -149,15 +152,14 @@
$(this).vifib('render', 'catalog.all'); $(this).vifib('render', 'catalog.all');
for (i=0; i<14; i++) { for (i=0; i<14; i++) {
item = $(this).vifib('getRender', 'catalog.item'); item = $(this).vifib('getRender', 'catalog.item');
console.log(item)
$("#catalog-all").append(item); $("#catalog-all").append(item);
} }
}); });
}, },
showCatalog: function () { showCatalog: function (params) {
return this.each(function () { return this.each(function () {
var i, item; var i, item, nextLevel;
$(this).vifib('render', 'catalog.preview'); $(this).vifib('render', 'catalog.preview');
for (i=0; i<2; i++) { for (i=0; i<2; i++) {
item = $(this).vifib('getRender', 'catalog.item'); item = $(this).vifib('getRender', 'catalog.item');
...@@ -171,6 +173,9 @@ ...@@ -171,6 +173,9 @@
item = $(this).vifib('getRender', 'catalog.categorie'); item = $(this).vifib('getRender', 'catalog.categorie');
$("#catalog-categories").append(item); $("#catalog-categories").append(item);
} }
nextLevel = $.router.routes.current.level + 1;
$.router.routes.add('/catalog/all', nextLevel, methods.showCatalogAll, $(this));
$.router.start(params.route, nextLevel);
}); });
}, },
...@@ -281,7 +286,7 @@ ...@@ -281,7 +286,7 @@
listComputers: function () { listComputers: function () {
$(this).vifib('render', 'server.list'); $(this).vifib('render', 'server.list');
}, },
refreshRowInstance: function () { refreshRowInstance: function () {
return this.each(function () { return this.each(function () {
var url = $(this).find('a').vifib('extractInstanceURIFromHref'); var url = $(this).find('a').vifib('extractInstanceURIFromHref');
...@@ -348,7 +353,7 @@ ...@@ -348,7 +353,7 @@
statusCode: statusCode statusCode: statusCode
}); });
}, },
listInvoices: function () { listInvoices: function () {
$(this).vifib('render', 'invoice.list'); $(this).vifib('render', 'invoice.list');
}, },
...@@ -424,7 +429,7 @@ ...@@ -424,7 +429,7 @@
$(this).html(ich[template](data, true)); $(this).html(ich[template](data, true));
}); });
}, },
getRender: function (template, data) { getRender: function (template, data) {
return ich[template](data, true); return ich[template](data, true);
}, },
......
...@@ -23,7 +23,20 @@ $.extend({ ...@@ -23,7 +23,20 @@ $.extend({
}, },
}, },
i = this.list[level].length; i = this.list[level].length;
this.list[level][i] = r; if (this.exist(r) === false) {
this.list[level][i] = r;
}
},
exist: function (r) {
var found = false, i = 0;
while (i < this.list[r.level].length && found === false) {
if (this.list[r.level][i].route === r.route) {
found = true;
}
i += 1;
}
return found;
}, },
clean: function (level) { clean: function (level) {
...@@ -34,14 +47,16 @@ $.extend({ ...@@ -34,14 +47,16 @@ $.extend({
this.list = this.list.slice(0, 0); this.list = this.list.slice(0, 0);
}, },
search: function (hash) { search: function (hash, level) {
var stop = false, var stop = false,
i, j, i, j,
regex, regex,
result, result,
extracted; extracted;
level = level || 0;
i = this.list.length - 1; i = this.list.length - 1;
while ((stop === false) && (i >= 0)) { hash.route = hash.route === "undefined" ? "/" : hash.route;
while ((stop === false) && (i >= level)) {
j = 0; j = 0;
while ((stop === false) && (j < this.list[i].length)) { while ((stop === false) && (j < this.list[i].length)) {
extracted = $.router.extractKeys(this.list[i][j].route); extracted = $.router.extractKeys(this.list[i][j].route);
...@@ -54,12 +69,25 @@ $.extend({ ...@@ -54,12 +69,25 @@ $.extend({
hash[extracted.keys[k]] = result[k]; hash[extracted.keys[k]] = result[k];
} }
this.current = this.list[i][j]; this.current = this.list[i][j];
this.clean(this.list[i][j].level + 1);
console.log(this.list[i][j].route)
this.list[i][j].callback(hash); this.list[i][j].callback(hash);
} }
j += 1; j += 1;
} }
i -= 1; i -= 1;
} }
},
isLastLevel: function () {
return this.current.level === (this.list.length - 1);
}
},
start: function (hash, level) {
var hashInfo = this.parseHash(hash);
if ($.router.routes.current.route !== hashInfo.route) {
this.routes.search(hashInfo, level);
} }
}, },
......
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