2010-07-06 00:05:47 +00:00
|
|
|
module Locomotive
|
2010-07-23 20:09:54 +00:00
|
|
|
module Liquid
|
|
|
|
module Tags
|
2011-01-02 22:58:06 +00:00
|
|
|
# Display the children pages of the site, current page or the parent page. If not precised, nav is applied on the current page.
|
2010-07-06 00:05:47 +00:00
|
|
|
# The html output is based on the ul/li tags.
|
|
|
|
#
|
2011-01-24 09:00:02 +00:00
|
|
|
# Passing through depth will control how many nested children are output
|
|
|
|
#
|
2010-07-06 00:05:47 +00:00
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# {% nav site %} => <ul class="nav"><li class="on"><a href="/features">Features</a></li></ul>
|
|
|
|
#
|
2012-02-15 17:26:58 +00:00
|
|
|
# {% nav site, no_wrapper: true, exclude: 'contact|about', id: 'main-nav', class: 'nav', active_class: 'on' }
|
2011-01-02 22:58:06 +00:00
|
|
|
#
|
2010-07-06 00:05:47 +00:00
|
|
|
class Nav < ::Liquid::Tag
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
Syntax = /(#{::Liquid::Expression}+)?/
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-08-20 00:31:01 +00:00
|
|
|
def initialize(tag_name, markup, tokens, context)
|
2010-07-06 00:05:47 +00:00
|
|
|
if markup =~ Syntax
|
2011-01-02 22:58:06 +00:00
|
|
|
@source = ($1 || 'page').gsub(/"|'/, '')
|
2012-02-15 17:26:58 +00:00
|
|
|
@options = { :id => 'nav', :depth => 1, :class => '', :active_class => 'on' }
|
2011-01-03 10:50:09 +00:00
|
|
|
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
|
2010-10-30 22:30:30 +00:00
|
|
|
|
2011-01-03 10:50:09 +00:00
|
|
|
@options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
|
2012-03-06 21:55:45 +00:00
|
|
|
|
|
|
|
@options[:add_attributes] = []
|
|
|
|
if @options[:snippet]
|
|
|
|
template = @options[:snippet].include?('{') ? @options[:snippet] : context[:site].snippets.where(:slug => @options[:snippet] ).try(:first).try(:template)
|
|
|
|
unless template.blank?
|
|
|
|
@options[:liquid_render] = ::Liquid::Template.parse( template )
|
|
|
|
@options[:add_attributes] = ['editable_elements']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
else
|
2010-07-13 00:46:17 +00:00
|
|
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <page|site> <options>")
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
super
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
def render(context)
|
2011-01-03 10:50:09 +00:00
|
|
|
children_output = []
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-01-03 10:50:09 +00:00
|
|
|
entries = fetch_entries(context)
|
2011-01-02 22:58:06 +00:00
|
|
|
|
2011-01-03 10:50:09 +00:00
|
|
|
entries.each_with_index do |p, index|
|
|
|
|
css = []
|
|
|
|
css << 'first' if index == 0
|
|
|
|
css << 'last' if index == entries.size - 1
|
2011-01-02 22:58:06 +00:00
|
|
|
|
2011-03-07 19:31:08 +00:00
|
|
|
children_output << render_entry_link(p, css.join(' '), 1)
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
|
|
|
|
2011-01-02 22:58:06 +00:00
|
|
|
output = children_output.join("\n")
|
2010-10-30 22:30:30 +00:00
|
|
|
|
2011-01-03 10:50:09 +00:00
|
|
|
if @options[:no_wrapper] != 'true'
|
2012-02-16 01:45:14 +00:00
|
|
|
list_class = !@options[:class].blank? ? %( class="#{@options[:class]}") : ''
|
|
|
|
output = %{<ul id="#{@options[:id]}"#{list_class}>\n#{output}</ul>}
|
2011-01-03 10:50:09 +00:00
|
|
|
end
|
2010-10-30 22:30:30 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
output
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
private
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-01-24 23:48:59 +00:00
|
|
|
# Determines root node for the list
|
2011-01-03 10:50:09 +00:00
|
|
|
def fetch_entries(context)
|
2012-01-26 01:33:39 +00:00
|
|
|
@site, @page = context.registers[:site], context.registers[:page]
|
2011-01-03 10:50:09 +00:00
|
|
|
|
|
|
|
children = (case @source
|
2012-03-06 21:55:45 +00:00
|
|
|
when 'site' then @site.pages.root.minimal_attributes( @options[:add_attributes] ).first # start from home page
|
2012-01-26 01:33:39 +00:00
|
|
|
when 'parent' then @page.parent || @page
|
|
|
|
when 'page' then @page
|
2011-01-03 10:50:09 +00:00
|
|
|
else
|
2012-03-06 21:55:45 +00:00
|
|
|
@site.pages.fullpath(@source).minimal_attributes( @options[:add_attributes] ).first
|
|
|
|
end).children_with_minimal_attributes( @options[:add_attributes] ).to_a
|
2011-01-03 10:50:09 +00:00
|
|
|
|
|
|
|
children.delete_if { |p| !include_page?(p) }
|
|
|
|
end
|
|
|
|
|
2011-01-24 23:48:59 +00:00
|
|
|
# Returns a list element, a link to the page and its children
|
2011-03-07 19:31:08 +00:00
|
|
|
def render_entry_link(page, css, depth)
|
2012-02-15 17:26:58 +00:00
|
|
|
selected = @page.fullpath =~ /^#{page.fullpath}/ ? " #{@options[:active_class]}" : ''
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
icon = @options[:icon] ? '<span></span>' : ''
|
2012-03-06 21:55:45 +00:00
|
|
|
|
|
|
|
title = @options[:liquid_render] ? @options[:liquid_render].render( 'page' => page ) : page.title
|
|
|
|
|
|
|
|
label = %{#{icon if @options[:icon] != 'after' }#{title}#{icon if @options[:icon] == 'after' }}
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-03-08 23:27:46 +00:00
|
|
|
output = %{<li id="#{page.slug.dasherize}-link" class="link#{selected} #{css}">}
|
2012-01-26 01:33:39 +00:00
|
|
|
output << %{<a href="/#{@site.localized_page_fullpath(page)}">#{label}</a>}
|
2011-03-07 19:31:08 +00:00
|
|
|
output << render_entry_children(page, depth.succ) if (depth.succ <= @options[:depth].to_i)
|
2011-01-24 09:00:02 +00:00
|
|
|
output << %{</li>}
|
|
|
|
|
|
|
|
output.strip
|
|
|
|
end
|
2011-02-12 14:00:57 +00:00
|
|
|
|
2011-01-24 23:48:59 +00:00
|
|
|
# Recursively creates a nested unordered list for the depth specified
|
2011-03-07 19:31:08 +00:00
|
|
|
def render_entry_children(page, depth)
|
2011-01-24 09:00:02 +00:00
|
|
|
output = %{}
|
2011-02-12 14:00:57 +00:00
|
|
|
|
2012-03-06 21:55:45 +00:00
|
|
|
children = page.children_with_minimal_attributes( @options[:add_attributes] ).reject { |c| !include_page?(c) }
|
2011-01-24 09:00:02 +00:00
|
|
|
if children.present?
|
|
|
|
output = %{<ul id="#{@options[:id]}-#{page.slug.dasherize}">}
|
|
|
|
children.each do |c, page|
|
|
|
|
css = []
|
|
|
|
css << 'first' if children.first == c
|
|
|
|
css << 'last' if children.last == c
|
2011-02-12 14:00:57 +00:00
|
|
|
|
2011-03-07 19:31:08 +00:00
|
|
|
output << render_entry_link(c, css.join(' '), depth)
|
2011-01-24 09:00:02 +00:00
|
|
|
end
|
|
|
|
output << %{</ul>}
|
|
|
|
end
|
2011-02-12 14:00:57 +00:00
|
|
|
|
2011-01-24 09:00:02 +00:00
|
|
|
output
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
2011-02-12 14:00:57 +00:00
|
|
|
|
2011-01-24 23:48:59 +00:00
|
|
|
# Determines whether or not a page should be a part of the menu
|
2010-10-30 22:30:30 +00:00
|
|
|
def include_page?(page)
|
2011-02-01 08:07:56 +00:00
|
|
|
if !page.listed? || page.templatized? || !page.published?
|
2010-10-30 22:30:30 +00:00
|
|
|
false
|
|
|
|
elsif @options[:exclude]
|
|
|
|
(page.fullpath =~ @options[:exclude]).nil?
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
::Liquid::Template.register_tag('nav', Nav)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|