Commit b0d3766e authored by Thomas Lechauve's avatar Thomas Lechauve

Merge jquery.urlJS with vifib application

parent cd8770e2
(function ($) { $.extend({
'use strict'; router: {
$.extend({ routes: {
router: { list: [],
routes: { current: null,
list: [],
current: null,
add: function (route, level, callback, context) { add: function (route, level, callback, context) {
var r, keys, i; var r, keys, i;
if (typeof this.list[level] === 'undefined') { if (typeof this.list[level] === 'undefined') {
this.list[level] = []; this.list[level] = [];
} }
r = { r = {
'route': route, 'route': route,
'level': level, 'level': level,
'callback': function (params) { 'callback': function (params) {
if (callback !== undefined) { if (callback !== undefined) {
if (context !== undefined) { if (context === undefined) {
callback(params); callback(params);
} else { } else {
callback.call(context, params); callback.call(context, params);
}
} }
}, }
}, },
i = this.list[level].length;
this.list[level][i] = r;
}, },
i = this.list[level].length;
this.list[level][i] = r;
},
clean: function (level) { clean: function (level) {
this.list = this.list.slice(0, level); this.list = this.list.slice(0, level);
}, },
cleanAll: function () { cleanAll: function () {
this.list = this.list.slice(0, 0); this.list = this.list.slice(0, 0);
}, },
search: function (hash) { search: function (hash) {
var stop = false, var stop = false,
i = 0, j = 0, i = 0, j = 0,
regex, regex,
result, result,
extracted; extracted;
while ((stop === false) && (i < this.list.length)) { while ((stop === false) && (i < this.list.length)) {
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);
regex = new RegExp('^' + extracted.regex + '$'); regex = new RegExp('^' + extracted.regex + '$');
if (regex.test(hash.route)) { if (regex.test(hash.route)) {
result = regex.exec(hash.route); result = regex.exec(hash.route);
stop = true; stop = true;
result.shift(); result.shift();
for (var k = 0; k < result.length; k += 1) { for (var k = 0; k < result.length; k += 1) {
hash[extracted.keys[k]] = result[k]; hash[extracted.keys[k]] = result[k];
}
this.current = this.list[i][j];
this.list[i][j].callback(hash);
} }
j += 1; this.current = this.list[i][j];
this.list[i][j].callback(hash);
} }
i += 1; j += 1;
} }
i += 1;
} }
}, }
},
extractKeys: function (regex) { extractKeys: function (regex) {
var re_key = new RegExp(/:(\w+)/), var re_key = new RegExp(/:(\w+)/),
keys = [], keys = [],
result; result;
while (re_key.test(regex)) { while (re_key.test(regex)) {
result = re_key.exec(regex); result = re_key.exec(regex);
keys.push(result[1]); keys.push(result[1]);
regex = regex.replace(result[0], '([^\/]+)'); regex = regex.replace(result[0], '([^\/]+)');
} }
return {'regex': regex, 'keys': keys} return {'regex': regex, 'keys': keys}
}, },
deserialize: function (params) { deserialize: function (params) {
var result = {}, var result = {},
p, p,
params = params.split('&'); params = params.split('&');
while (params.length) { while (params.length) {
p = params.shift().split('='); p = params.shift().split('=');
if (p[0] !== '') { if (p[0] !== '') {
if (p.length === 2) { if (p.length === 2) {
result[p[0]] = p[1] === 'true' ? true : p[1]; result[p[0]] = p[1] === 'true' ? true : p[1];
} else { } else {
result[p[0]] = true; result[p[0]] = true;
}
} }
} }
return result; }
}, return result;
},
serialize: function (obj) { serialize: function (obj) {
return $.param(obj); return $.param(obj);
}, },
parseHash: function (hashTag) { parseHash: function (hashTag) {
var re = new RegExp(/(?:^#([a-zA-Z0-9\/_-]+))(?:\?([A-Za-z0-9\/&=_-]+))?/g), var re = new RegExp(/(?:^#?([a-zA-Z0-9\/_-]+))(?:\?([A-Za-z0-9\/&=_-]+))?/g),
groups = re.exec(hashTag), groups = re.exec(hashTag),
r, params = {}; r, params = {};
groups.shift(); groups.shift();
// route // route
r = groups[0]; r = groups[0];
// params // params
if (groups[1] !== undefined) { if (groups[1] !== undefined) {
params = this.deserialize(groups[1]); params = this.deserialize(groups[1]);
} }
return params.length === 0 ? {'route': r} : $.extend({'route': r}, params); return params.length === 0 ? {'route': r} : $.extend({'route': r}, params);
}, },
hashHandler: function () { hashHandler: function () {
var hashTag = window.location.href.split('#')[1], var hashTag = window.location.href.split('#')[1],
hashInfo = this.parseHash(hashTag); hashInfo = $.router.parseHash(hashTag);
this.routes.call(hashInfo) $.router.routes.search(hashInfo)
}, },
} }
}); });
$(window).bind('hashchange', $.router.hashchangeHandler); $(window).bind('hashchange', $.router.hashHandler);
$(window).bind('load', $.router.hashchangeHandler); $(window).bind('load', $.router.hashHandler);
}(jQuery));
This diff is collapsed.
...@@ -5,31 +5,7 @@ ...@@ -5,31 +5,7 @@
*/ */
(function ($) { (function ($) {
'use strict'; 'use strict';
var routes = { var getDate = function () {
'/catalog' : 'showCatalog',
'/catalog/all' : 'showCatalogAll',
'/instance' : 'requestInstance',
'/instance/:url' : 'showInstance',
'/instance/:url/bang' : 'showBangInstance',
'/computers' : 'listComputers',
'/instances' : 'listInstances',
'/invoices' : 'listInvoices',
'/dashboard' : 'showDashboard'
},
router = function (e, d) {
var $this = $(this);
$.each(routes, function (pattern, callback) {
pattern = pattern.replace(/:\w+/g, '([^\/]+)');
var regex = new RegExp('^' + pattern + '$'),
result = regex.exec(d);
if (result) {
result.shift();
methods[callback].apply($this, result);
}
});
},
getDate = function () {
var today = new Date(); var today = new Date();
return [today.getFullYear(), today.getMonth(), today.getDay()].join('/') + return [today.getFullYear(), today.getMonth(), today.getDay()].join('/') +
' ' + [today.getHours(), today.getMinutes(), today.getSeconds()].join(':'); ' ' + [today.getHours(), today.getMinutes(), today.getSeconds()].join(':');
...@@ -83,23 +59,33 @@ ...@@ -83,23 +59,33 @@
methods = { methods = {
init: function () { init: function () {
// Initialize slapos in this context var routes = [];
$(this).slapos({'host': 'http://10.8.2.34:12006/erp5/portal_vifib_rest_api_v1'}); routes[0] = [
var $this = $(this); ['/catalog', methods['showCatalog']],
// Bind Loading content ['/catalog/all', methods['showCatalogAll']],
$('#loading').ajaxStart(function () { ['/instance', methods['requestInstance']],
$(this).spin(spinOptions); ['/instance/:url', methods['showInstance']],
}).ajaxStop(function () { ['/instance/:url/bang', methods['showBangInstance']],
$(this).spin(false); ['/computers', methods['listComputers']],
}); ['/instances', methods['listInstances']],
// Bind to urlChange event ['/invoices', methods['listInvoices']],
['/dashboard', methods['showDashboard']]
];
return this.each(function () { return this.each(function () {
$.subscribe('urlChange', function (e, d) { // Initialize slapos in this context
router.call($this, e, d); $(this).slapos({'host': 'http://10.8.2.34:12006/erp5/portal_vifib_rest_api_v1'});
}); // Bind Loading content
$.subscribe('auth', function (e, d) { $('#loading').ajaxStart(function () {
$(this).vifib('authenticate', d); $(this).spin(spinOptions);
}).ajaxStop(function () {
$(this).spin(false);
}); });
for (var level = 0; level < routes.length; level += 1) {
for (var i = 0; i < routes[level].length; i += 1) {
var r = routes[level][i];
$.router.routes.add(r[0], level, r[1], $(this));
}
}
}); });
}, },
......
...@@ -61,5 +61,7 @@ $.redirectHandler = function(e, url){ window.location.hash = $.genHash(url); }; ...@@ -61,5 +61,7 @@ $.redirectHandler = function(e, url){ window.location.hash = $.genHash(url); };
$.redirect = function(url){ $.publish('redirect', [url]); }; $.redirect = function(url){ $.publish('redirect', [url]); };
$.subscribe('redirect', $.redirectHandler) $.subscribe('redirect', $.redirectHandler)
console.log("plop")
$(window).bind('hashchange', $.hashHandler); $(window).bind('hashchange', $.hashHandler);
$(window).bind('load', $.hashHandler); $(window).bind('load', $.hashHandler);
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