In some parts of the code, it is necessary to remove the file extension
from a string. Currently, the last 5 characters are removed from
the text.
Remove the file extension by searching for the last '.' inside the
string. This approach works for other file extension lengths - even
though we will probably use only .html - and is more clear than having
a magic number ("5") inside the code.
Also, add a comment explaining what the substring method does in
that case.
Signed-off-by: Crístian Viana <vianac(a)linux.vnet.ibm.com>
---
ui/js/src/kimchi.main.js | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/ui/js/src/kimchi.main.js b/ui/js/src/kimchi.main.js
index 018f5e7..2999aa2 100644
--- a/ui/js/src/kimchi.main.js
+++ b/ui/js/src/kimchi.main.js
@@ -76,8 +76,9 @@ kimchi.main = function() {
});
var firstTabPath = tabs[0] && tabs[0]['path'];
+ // Remove file extension from 'firstTabPath'
DEFAULT_HASH = firstTabPath &&
- firstTabPath.substring(0, firstTabPath.length - 5);
+ firstTabPath.substring(0, firstTabPath.lastIndexOf('.'))
$('#nav-menu').append(genTabs(tabs));
@@ -183,7 +184,8 @@ kimchi.main = function() {
*/
$('#nav-menu').on('click', 'a.item', function(event) {
var href = $(this).attr('href');
- location.hash = href.substring(0,href.length -5);
+ // Remove file extension from 'href'
+ location.hash = href.substring(0,href.lastIndexOf('.'))
/*
* We use the HTML file name for hash, like: guests for guests.html
* and templates for templates.html.
--
1.9.0