Clean up the callback spaghetti in Header and CatalogLinks a bit.

This commit is contained in:
ccd0 2016-10-08 16:36:40 -07:00
parent 0e512f96d5
commit 95b12e3240
2 changed files with 51 additions and 49 deletions

View File

@ -84,31 +84,28 @@ Header =
$.on window, 'load popstate', Header.hashScroll $.on window, 'load popstate', Header.hashScroll
$.on d, 'CreateNotification', @createNotification $.on d, 'CreateNotification', @createNotification
$.asap (-> d.body), => $.onExists doc, 'body', =>
return unless Main.isThisPageLegit() return unless Main.isThisPageLegit()
# Wait for #boardNavMobile instead of #boardNavDesktop, $.prepend d.body, @bar
# it might be incomplete otherwise. $.add d.body, Header.hover
$.asap (-> $.id('boardNavMobile') or d.readyState isnt 'loading'), -> @setBarPosition Conf['Bottom Header']
$.onExists doc, '#boardNavDesktop > *', Header.setBoardList
Main.ready ->
if not (footer = $.id 'boardNavDesktopFoot')
return unless (absbot = $.id 'absbot')
footer = $.id('boardNavDesktop').cloneNode true footer = $.id('boardNavDesktop').cloneNode true
footer.id = 'boardNavDesktopFoot' footer.id = 'boardNavDesktopFoot'
$('#navtopright', footer).id = 'navbotright' $('#navtopright', footer).id = 'navbotright'
$('#settingsWindowLink', footer).id = 'settingsWindowLinkBot' $('#settingsWindowLink', footer).id = 'settingsWindowLinkBot'
Header.bottomBoardList = $ '.boardList', footer
if a = $ "a[href*='/#{g.BOARD}/']", footer
a.className = 'current'
Main.ready ->
if (oldFooter = $.id 'boardNavDesktopFoot')
$.replace $('.boardList', oldFooter), Header.bottomBoardList
else if (absbot = $.id 'absbot')
$.before absbot, footer $.before absbot, footer
$.globalEval 'window.cloneTopNav = function() {};' $.globalEval 'window.cloneTopNav = function() {};'
Header.setBoardList() if (a = $ "a[href*='/#{g.BOARD}/']", footer)
$.prepend d.body, @bar a.className = 'current'
$.add d.body, Header.hover Header.bottomBoardList = $ '.boardList', footer
@setBarPosition Conf['Bottom Header'] CatalogLinks.setLinks Header.bottomBoardList
@
Main.ready =>
if g.VIEW is 'catalog' or !Conf['Disable Native Extension'] if g.VIEW is 'catalog' or !Conf['Disable Native Extension']
cs = $.el 'a', href: 'javascript:;' cs = $.el 'a', href: 'javascript:;'
if g.VIEW is 'catalog' if g.VIEW is 'catalog'
@ -170,7 +167,9 @@ Header =
a = node.cloneNode true a = node.cloneNode true
a.className = 'current' if a.pathname.split('/')[1] is g.BOARD.ID a.className = 'current' if a.pathname.split('/')[1] is g.BOARD.ID
nodes.push a nodes.push a
$.add $('.boardList', boardList), nodes fullBoardList = $ '.boardList', boardList
$.add fullBoardList, nodes
CatalogLinks.setLinks fullBoardList
$.add Header.bar, [Header.boardList, Header.shortcuts, Header.noticesRoot, Header.toggle] $.add Header.bar, [Header.boardList, Header.shortcuts, Header.noticesRoot, Header.toggle]
@ -188,9 +187,8 @@ Header =
as = $$ '#full-board-list a[title]', Header.boardList as = $$ '#full-board-list a[title]', Header.boardList
re = /[\w@]+(-(all|title|replace|full|index|catalog|archive|expired|(mode|sort|text):"[^"]+"(,"[^"]+")?))*|[^\w@]+/g re = /[\w@]+(-(all|title|replace|full|index|catalog|archive|expired|(mode|sort|text):"[^"]+"(,"[^"]+")?))*|[^\w@]+/g
nodes = (Header.mapCustomNavigation t, as for t in boardnav.match re) nodes = (Header.mapCustomNavigation t, as for t in boardnav.match re)
$.add list, nodes $.add list, nodes
$.ready CatalogLinks.initBoardList CatalogLinks.setLinks list
mapCustomNavigation: (t, as) -> mapCustomNavigation: (t, as) ->
if /^[^\w@]/.test t if /^[^\w@]/.test t

View File

@ -41,34 +41,38 @@ CatalogLinks =
a.href = "//boards.4chan.org/#{m[1]}/#{m[2] or '#catalog'}" a.href = "//boards.4chan.org/#{m[1]}/#{m[2] or '#catalog'}"
return return
# Set links on load or custom board list change.
# Called by Header when both board lists (header and footer) are ready.
initBoardList: ->
return unless CatalogLinks.el
CatalogLinks.set Conf['Header catalog links']
toggle: -> toggle: ->
$.event 'CloseMenu' $.event 'CloseMenu'
$.set 'Header catalog links', @checked $.set 'Header catalog links', @checked
CatalogLinks.set @checked CatalogLinks.set @checked
set: (useCatalog) -> set: (useCatalog) ->
for a in $$('a:not([data-only])', Header.boardList).concat $$('a', Header.bottomBoardList) Conf['Header catalog links'] = useCatalog
continue if a.hostname not in ['boards.4chan.org', 'catalog.neet.tv'] or CatalogLinks.setLinks Header.boardList
CatalogLinks.setLinks Header.bottomBoardList
CatalogLinks.el.title = "Turn catalog links #{if useCatalog then 'off' else 'on'}."
$('input', CatalogLinks.el).checked = useCatalog
# Also called by Header when board lists are loaded / generated.
setLinks: (list) ->
return unless CatalogLinks.el and list
for a in $$('a:not([data-only])', list)
continue if (
a.hostname not in ['boards.4chan.org', 'catalog.neet.tv'] or
!(board = a.pathname.split('/')[1]) or !(board = a.pathname.split('/')[1]) or
board in ['f', 'status', '4chan'] or board in ['f', 'status', '4chan'] or
a.pathname.split('/')[2] is 'archive' or a.pathname.split('/')[2] is 'archive' or
$.hasClass a, 'external' $.hasClass a, 'external'
)
# Href is easier than pathname because then we don't have # Href is easier than pathname because then we don't have
# conditions where External Catalog has been disabled between switches. # conditions where External Catalog has been disabled between switches.
a.href = if useCatalog then CatalogLinks.catalog(board) else "/#{board}/" a.href = if Conf['Header catalog links'] then CatalogLinks.catalog(board) else "/#{board}/"
if a.dataset.indexOptions and a.hostname is 'boards.4chan.org' and a.pathname.split('/')[2] is '' if a.dataset.indexOptions and a.hostname is 'boards.4chan.org' and a.pathname.split('/')[2] is ''
a.href += (if a.hash then '/' else '#') + a.dataset.indexOptions a.href += (if a.hash then '/' else '#') + a.dataset.indexOptions
return
CatalogLinks.el.title = "Turn catalog links #{if useCatalog then 'off' else 'on'}."
$('input', CatalogLinks.el).checked = useCatalog
catalog: (board=g.BOARD.ID) -> catalog: (board=g.BOARD.ID) ->
if Conf['External Catalog'] and board in ['a', 'c', 'g', 'biz', 'k', 'm', 'o', 'p', 'v', 'vg', 'vr', 'w', 'wg', 'cm', '3', 'adv', 'an', 'asp', 'cgl', 'ck', 'co', 'diy', 'fa', 'fit', 'gd', 'int', 'jp', 'lit', 'mlp', 'mu', 'n', 'out', 'po', 'sci', 'sp', 'tg', 'toy', 'trv', 'tv', 'vp', 'wsg', 'x', 'f', 'pol', 's4s', 'lgbt'] if Conf['External Catalog'] and board in ['a', 'c', 'g', 'biz', 'k', 'm', 'o', 'p', 'v', 'vg', 'vr', 'w', 'wg', 'cm', '3', 'adv', 'an', 'asp', 'cgl', 'ck', 'co', 'diy', 'fa', 'fit', 'gd', 'int', 'jp', 'lit', 'mlp', 'mu', 'n', 'out', 'po', 'sci', 'sp', 'tg', 'toy', 'trv', 'tv', 'vp', 'wsg', 'x', 'f', 'pol', 's4s', 'lgbt']