These are chat archives for jescalan/roots

5th
Jul 2015
Sergio Forés
@t0t
Jul 05 2015 00:24
I see, href is retrieving only "/", how would you append the full url? indexOf would do the job? http://jsfiddle.net/lucuma/DHABK/
# Active current menu item
pathname = window.location.pathname
href = $('.nav-main__menu a').attr('href')

$('.nav-main__menu a').each ->
  if href == pathname
    $(this).parent().addClass('active')
  return

alert href
Henry Snopek
@hhsnopek
Jul 05 2015 00:32
I wouldn't
Henry Snopek
@hhsnopek
Jul 05 2015 00:37
Use split('/') on the location to match against the href
Sergio Forés
@t0t
Jul 05 2015 01:07
Bfff I understand the idea but not enough skills on JS... I have to learn more JS. If split gives me an array then I only should pick up de second [2] in order to match with href. What's wrong on my poor code? Thanks a lot for helping ;)
# Active current menu item
pathName = window.location.pathname
splitHref = pathName.split('/')[2]

href = $('.nav-main__menu a').attr('href')

$('.nav-main__menu a').each ->
  if href == splitHref
    $(this).parent().addClass('active')
  return

alert href
Henry Snopek
@hhsnopek
Jul 05 2015 01:10
I'll take a closer look tomorrow but you'll wanna window.location.href.split('/') and match the proper part of the array to your href
You could also make it static and make the Nav a mixin that you pass a variable into it
That argument would define the Nav item to be activr
*active
Sergio Forés
@t0t
Jul 05 2015 02:11

Yes! I did it:

# Active current menu item
splitHref = window.location.href.split('/')
splitHref = splitHref[3]

$('.nav-main__menu a').each ->
  href = $(this).attr('href')
  if href == splitHref
    $(this).parent().addClass('active')
  return

The only "problem" is the first href is not like the others; it has to match with the second element of the array instead of third

Sergio Forés
@t0t
Jul 05 2015 02:16
I mean, this formula is not valid with the first item home witch has to match with localhost:4000/ instead of localhost:4000/home
Henry Snopek
@hhsnopek
Jul 05 2015 05:09
just add an if statement to match against /
also use is rather than ==
Sergio Forés
@t0t
Jul 05 2015 22:32
Please @hhsnopek could you help me with that if statement? I'm lost on that point. This is what I 've added :worried:
# Active current menu item
splitHref = window.location.href.split('/')
splitHref = splitHref[3]
home = '/'
$('.nav-main__menu a').each ->
  href = $(this).attr('href')
  if href is splitHref
    $(this).parent().addClass('active')
  return
  if href is home
    $(this).parent().addClass('active')
  return