4chan-x/src/Miscellaneous/CatalogLinks.coffee
Zixaphir 3b1340433e Faster Catalog Links code
Ours is based on @ihavenoface's old catalog code, not @MayhemYDG's,
so we don't particularly benefit from advancements in his code, per
se, without rechecking and validating the quality of our own. This
happens to be a case where we were stuck in an inefficient paradigm
and never revisited.

We now appear much closer to Mayhem's code, but are still mostly
absent from being merged into the rest of the Header code. Whether
we decide to merge or not should probably be discussed at some
point.
2013-12-18 16:55:27 -07:00

53 lines
1.9 KiB
CoffeeScript
Executable File

CatalogLinks =
init: ->
return unless Conf['Catalog Links']
CatalogLinks.el = el = $.el 'label',
id: 'toggleCatalog'
href: 'javascript:;'
innerHTML: "<input type=checkbox #{if Conf['Header catalog links'] then 'checked' else ''}> Catalog Links"
input = $ 'input', el
$.on input, 'change', @toggle
$.sync 'Header catalog links', CatalogLinks.set
$.event 'AddMenuEntry',
type: 'header'
el: el
order: 95
$.on d, '4chanXInitFinished', ->
# Set links on load.
CatalogLinks.set Conf['Header catalog links']
toggle: ->
$.event 'CloseMenu'
$.set 'Header catalog links', @checked
CatalogLinks.set @checked
set: (useCatalog) ->
path = if useCatalog then 'catalog' else ''
generateURL = if useCatalog and Conf['External Catalog']
CatalogLinks.external
else
(board) -> a.href = "/#{board}/#{path}"
for a in $$ """#board-list a:not(.catalog), #boardNavDesktopFoot a"""
continue if !['boards.4chan.org', 'catalog.neet.tv', '4index.gropes.us'].contains(a.hostname) or
!(board = a.pathname.split('/')[1]) or
['f', 'status', '4chan'].contains board
# Href is easier than pathname because then we don't have
# conditions where External Catalog has been disabled between switches.
a.href = generateURL board
CatalogLinks.el.title = "Turn catalog links #{if useCatalog then 'off' else 'on'}."
external: (board) ->
switch board
when 'a', 'c', 'g', 'co', 'k', 'm', 'o', 'p', 'v', 'vg', 'w', 'cm', '3', 'adv', 'an', 'cgl', 'ck', 'diy', 'fa', 'fit', 'int', 'jp', 'mlp', 'lit', 'mu', 'n', 'po', 'sci', 'toy', 'trv', 'tv', 'vp', 'x', 'q'
"http://catalog.neet.tv/#{board}"
when 'd', 'e', 'gif', 'h', 'hr', 'hc', 'r9k', 's', 'pol', 'soc', 'u', 'i', 'ic', 'hm', 'r', 'w', 'wg', 'wsg', 't', 'y'
"http://4index.gropes.us/#{board}"
else
"/#{board}/catalog"