scratch/lib/multiMenu.rb

169 lines
4.3 KiB
Ruby
Raw Normal View History

2010-01-22 10:53:59 +00:00
def homepage
@items.find do |i|
i.reps[0].path == %{/Scratch/#{@conf.language}/}
end
end
def sortedChildrenByMenuPriority(item)
2010-02-17 12:27:01 +00:00
item.children.reject{|p| p[:isHidden]}.
sort!{|x,y| x[:menupriority] <=> y[:menupriority]}
2010-01-22 10:53:59 +00:00
end
def generateMenu
home=homepage
2010-02-19 10:26:31 +00:00
liste=[]
liste<<=link_to_unless_current(home[:title],home.reps[0])
2010-04-14 14:26:43 +00:00
sortedChildrenByMenuPriority(home).each do |page|
2010-02-19 10:26:31 +00:00
liste <<= link_to_unless_current(page[:title],page.reps[0])
2010-01-22 10:53:59 +00:00
end
2010-04-27 23:00:16 +00:00
"<ul><li>"+liste.join("</li>\n<li>")+"</li></ul>"
2010-01-22 10:53:59 +00:00
end
2010-02-17 12:27:01 +00:00
def depthOf(item)
2010-02-19 10:26:31 +00:00
res=0
while item.parent != nil
res+=1
item=item.parent
end
return res
2010-02-17 12:27:01 +00:00
end
2010-02-19 10:26:31 +00:00
def getSortedChildren(parent)
2010-03-12 12:11:05 +00:00
if parent[:kind] == "blog"
2010-04-08 12:48:22 +00:00
return parent.children.reject{|p| p[:isHidden]}.sort!{|x,y| x[:created_at] <=> y[:created_at] }
2010-03-12 12:11:05 +00:00
else
return parent.children.reject{|p| p[:isHidden]}.sort!{|x,y| x[:menupriority] <=> y[:menupriority] }
2010-02-19 10:26:31 +00:00
end
end
def generateBlogSubMenu
2010-04-26 10:20:11 +00:00
year=0
res=""
2010-02-19 10:26:31 +00:00
liste=getSortedChildren(@item).reverse!.collect! do |p|
2010-04-26 10:20:11 +00:00
if p[:created_at].strftime("%Y") != year
2010-04-27 08:34:50 +00:00
if year != 0
2010-04-27 23:00:16 +00:00
res<<=%{</ul><script type="text/javascript">$('#archives_#{year}').hide()</script>}
2010-04-27 08:34:50 +00:00
end
2010-04-26 10:20:11 +00:00
year=p[:created_at].strftime("%Y")
2010-04-27 08:34:50 +00:00
res<<=%{<h4 style="cursor: pointer;" onclick="$('#archives_#{year}').slideToggle()">[#{year}]</h4><ul id="archives_#{year}">}
2010-04-26 10:20:11 +00:00
end
res<<='<li>'
res<<=%{<span class="date"><span class="small"><em>#{p[:created_at].strftime("%d %b")}</em></span></span> }+link_to_unless_current(p[:title],p)
res<<='</li>'
2010-02-19 10:26:31 +00:00
end
2010-04-27 23:00:16 +00:00
res<<=%{</ul><script type="text/javascript">$('#archives_#{year}').hide()</script>}
2010-02-19 10:26:31 +00:00
if ! liste.empty?
2010-04-27 23:00:16 +00:00
'<div id="sousliens">'+res+'</div>'
2010-02-19 10:26:31 +00:00
else
return
end
end
def generateSubMenu()
if @item[:noSubMenu]
return
2010-04-14 14:26:43 +00:00
end
2010-02-19 10:26:31 +00:00
depth=depthOf(@item)
if depth == 0
return
end
2010-04-09 14:41:47 +00:00
if @item[:kind].to_s == "blog"
return generateBlogSubMenu
end
2010-04-16 07:37:28 +00:00
if @item.children.length == 0 and not @item.parent[:kind].to_s == "blog"
2010-04-09 14:41:47 +00:00
page=@item.parent
2010-04-14 14:26:43 +00:00
else
2010-04-09 14:41:47 +00:00
page=@item
end
liste=getSortedChildren(page).collect do |p|
link_to_unless_current(p[:title],p)
end
if ! liste.empty? then
2010-04-29 20:46:24 +00:00
liste = [ link_to_unless_current(page[:title],page) ].concat( liste )
2010-04-09 14:41:47 +00:00
'<div id="sousliens"><ul><li>'+liste.join('</li><li>')+'</li></ul></div>'
2010-02-19 10:26:31 +00:00
else
2010-04-09 14:41:47 +00:00
return
2010-02-19 10:26:31 +00:00
end
end
# =======================
2010-04-08 14:23:47 +00:00
def blogimage(val,title="no name")
if depthOf( @item ) == 3
imgpath=@item.parent.path
else
imgpath=@item.path
end
imgpath=imgpath.sub(%r{/Scratch/../},'/Scratch/img/')+val
return %{<img alt="#{title}" src="#{imgpath}"></img>}
2010-02-17 12:27:01 +00:00
end
2010-04-08 14:23:47 +00:00
def leftblogimage(val,title="no name")
if depthOf( @item ) == 3
imgpath=@item.parent.path
else
imgpath=@item.path
end
imgpath=imgpath.sub(%r{/Scratch/../},'/Scratch/img/')+val
return %{<img class="left" alt="#{title}" src="#{imgpath}"></img>}
2010-02-17 12:27:01 +00:00
end
2010-04-08 14:23:47 +00:00
2010-02-17 12:27:01 +00:00
def lnkto(title,item)
language=@item_rep.path.sub(/\/Scratch\//,'').sub(/\/.*$/,'')
link_to(title, "/Scratch/#{language}"+item)
2010-02-17 12:27:01 +00:00
end
2010-02-19 10:26:31 +00:00
2010-04-02 14:13:57 +00:00
def nextFor(page)
2010-04-08 12:48:22 +00:00
depth=depthOf(page)
case depth
when 0..1 then return nil
when 2 then target=getSortedChildren(page)[0]
2010-04-02 14:13:57 +00:00
else
2010-04-08 12:48:22 +00:00
sorted_children=getSortedChildren(page.parent)
index=sorted_children.index(page)
target=sorted_children[ index + 1]
if target.nil?
return nil
end
2010-04-02 14:13:57 +00:00
end
link_to("&rarr;&nbsp;"+tradOf(:next), target)
end
2010-04-08 12:48:22 +00:00
# return the previous page of a post containing many
2010-04-02 14:13:57 +00:00
def previousFor(page)
2010-04-08 12:48:22 +00:00
if depthOf(page) < 3
2010-04-02 14:13:57 +00:00
return nil
end
sorted_children=getSortedChildren(page.parent)
2010-04-08 12:48:22 +00:00
index=sorted_children.index(page)
if index==0
target=page.parent
else
target=sorted_children[ index - 1 ]
2010-04-02 14:13:57 +00:00
end
link_to(tradOf(:previous)+"&nbsp;&larr;", target)
end
def brother_for_at(page,n)
brothers=getSortedChildren(page.parent)
i=brothers.index(page)
if i.nil?
return nil
end
brothers[ brothers.index(page) + n ]
end
def article_brother(n)
2010-04-29 09:11:41 +00:00
if depthOf(@item) > 2
page=@item.parent
else
page=@item
end
brother_for_at(page,n)
end