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,42 +84,39 @@ Header =
$.on window, 'load popstate', Header.hashScroll
$.on d, 'CreateNotification', @createNotification
$.asap (-> d.body), =>
$.onExists doc, 'body', =>
return unless Main.isThisPageLegit()
# Wait for #boardNavMobile instead of #boardNavDesktop,
# it might be incomplete otherwise.
$.asap (-> $.id('boardNavMobile') or d.readyState isnt 'loading'), ->
$.prepend d.body, @bar
$.add d.body, Header.hover
@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 = 'boardNavDesktopFoot'
$('#navtopright', footer).id = 'navbotright'
$('#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
$.globalEval 'window.cloneTopNav = function() {};'
Header.setBoardList()
$.prepend d.body, @bar
$.add d.body, Header.hover
@setBarPosition Conf['Bottom Header']
@
$.before absbot, footer
$.globalEval 'window.cloneTopNav = function() {};'
if (a = $ "a[href*='/#{g.BOARD}/']", footer)
a.className = 'current'
Header.bottomBoardList = $ '.boardList', footer
CatalogLinks.setLinks Header.bottomBoardList
Main.ready =>
if g.VIEW is 'catalog' or !Conf['Disable Native Extension']
cs = $.el 'a', href: 'javascript:;'
if g.VIEW is 'catalog'
cs.title = cs.textContent = 'Catalog Settings'
cs.className = 'fa fa-book'
else
cs.title = cs.textContent = '4chan Settings'
cs.className = 'native-settings'
$.on cs, 'click', () ->
$.id('settingsWindowLink').click()
@addShortcut 'native', cs, 810
if g.VIEW is 'catalog' or !Conf['Disable Native Extension']
cs = $.el 'a', href: 'javascript:;'
if g.VIEW is 'catalog'
cs.title = cs.textContent = 'Catalog Settings'
cs.className = 'fa fa-book'
else
cs.title = cs.textContent = '4chan Settings'
cs.className = 'native-settings'
$.on cs, 'click', () ->
$.id('settingsWindowLink').click()
@addShortcut 'native', cs, 810
@enableDesktopNotifications()
@ -170,7 +167,9 @@ Header =
a = node.cloneNode true
a.className = 'current' if a.pathname.split('/')[1] is g.BOARD.ID
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]
@ -188,9 +187,8 @@ Header =
as = $$ '#full-board-list a[title]', Header.boardList
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)
$.add list, nodes
$.ready CatalogLinks.initBoardList
CatalogLinks.setLinks list
mapCustomNavigation: (t, as) ->
if /^[^\w@]/.test t

View File

@ -41,34 +41,38 @@ CatalogLinks =
a.href = "//boards.4chan.org/#{m[1]}/#{m[2] or '#catalog'}"
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: ->
$.event 'CloseMenu'
$.set 'Header catalog links', @checked
CatalogLinks.set @checked
set: (useCatalog) ->
for a in $$('a:not([data-only])', Header.boardList).concat $$('a', Header.bottomBoardList)
continue if a.hostname not in ['boards.4chan.org', 'catalog.neet.tv'] or
!(board = a.pathname.split('/')[1]) or
board in ['f', 'status', '4chan'] or
a.pathname.split('/')[2] is 'archive' or
$.hasClass a, 'external'
Conf['Header catalog links'] = useCatalog
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 in ['f', 'status', '4chan'] or
a.pathname.split('/')[2] is 'archive' or
$.hasClass a, 'external'
)
# Href is easier than pathname because then we don't have
# 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 ''
a.href += (if a.hash then '/' else '#') + a.dataset.indexOptions
CatalogLinks.el.title = "Turn catalog links #{if useCatalog then 'off' else 'on'}."
$('input', CatalogLinks.el).checked = useCatalog
return
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']