Add search state of index to history, and support #s=search+term.
This commit is contained in:
parent
9257ca2df2
commit
630428e768
@ -9,10 +9,13 @@ Index =
|
|||||||
name: 'Catalog Features'
|
name: 'Catalog Features'
|
||||||
cb: @catalogNode
|
cb: @catalogNode
|
||||||
|
|
||||||
|
@search = history.state?.search or ''
|
||||||
if history.state?.mode
|
if history.state?.mode
|
||||||
Conf['Index Mode'] = history.state?.mode
|
Conf['Index Mode'] = history.state?.mode
|
||||||
|
@currentPage = @getCurrentPage()
|
||||||
@pushState
|
@pushState
|
||||||
command: location.hash[1..]
|
# XXX https://bugzilla.mozilla.org/show_bug.cgi?id=483304
|
||||||
|
command: location.href.match(/#(.*)/)?[1]
|
||||||
replace: true
|
replace: true
|
||||||
|
|
||||||
@button = $.el 'a',
|
@button = $.el 'a',
|
||||||
@ -57,11 +60,11 @@ Index =
|
|||||||
$('.returnlink a', @navLinks).href = CatalogLinks.index()
|
$('.returnlink a', @navLinks).href = CatalogLinks.index()
|
||||||
$('.cataloglink a', @navLinks).href = CatalogLinks.catalog()
|
$('.cataloglink a', @navLinks).href = CatalogLinks.catalog()
|
||||||
@searchInput = $ '#index-search', @navLinks
|
@searchInput = $ '#index-search', @navLinks
|
||||||
|
@setupSearch()
|
||||||
@hideLabel = $ '#hidden-label', @navLinks
|
@hideLabel = $ '#hidden-label', @navLinks
|
||||||
@selectMode = $ '#index-mode', @navLinks
|
@selectMode = $ '#index-mode', @navLinks
|
||||||
@selectSort = $ '#index-sort', @navLinks
|
@selectSort = $ '#index-sort', @navLinks
|
||||||
@selectSize = $ '#index-size', @navLinks
|
@selectSize = $ '#index-size', @navLinks
|
||||||
@currentPage = @getCurrentPage()
|
|
||||||
$.on window, 'popstate', @cb.popstate
|
$.on window, 'popstate', @cb.popstate
|
||||||
|
|
||||||
$.on d, 'scroll', Index.scroll
|
$.on d, 'scroll', Index.scroll
|
||||||
@ -188,10 +191,7 @@ Index =
|
|||||||
unless mode is 'catalog'
|
unless mode is 'catalog'
|
||||||
Conf['Previous Index Mode'] = mode
|
Conf['Previous Index Mode'] = mode
|
||||||
$.set 'Previous Index Mode', mode
|
$.set 'Previous Index Mode', mode
|
||||||
Index.pushState {mode}
|
Index.pageLoad Index.pushState {mode}
|
||||||
Index.applyMode()
|
|
||||||
Index.buildIndex()
|
|
||||||
Index.setPage()
|
|
||||||
sort: ->
|
sort: ->
|
||||||
Index.sort()
|
Index.sort()
|
||||||
Index.buildIndex()
|
Index.buildIndex()
|
||||||
@ -211,23 +211,24 @@ Index =
|
|||||||
Index.sort()
|
Index.sort()
|
||||||
Index.buildIndex()
|
Index.buildIndex()
|
||||||
popstate: (e) ->
|
popstate: (e) ->
|
||||||
unless e?.state
|
if e?.state
|
||||||
|
{search, mode} = e.state
|
||||||
|
page = Index.getCurrentPage()
|
||||||
|
state = {}
|
||||||
|
state.search = Index.search = search if Index.search isnt search
|
||||||
|
state.mode = Conf['Index Mode'] = mode if Conf['Index Mode'] isnt mode
|
||||||
|
state.page = Index.currentPage = page if Index.currentPage isnt page
|
||||||
|
if state.search? or state.mode? or state.page?
|
||||||
|
Index.pageLoad state
|
||||||
|
else
|
||||||
# page load or hash change
|
# page load or hash change
|
||||||
state = Index.pushState
|
state = Index.pushState
|
||||||
command: location.hash[1..]
|
# XXX https://bugzilla.mozilla.org/show_bug.cgi?id=483304
|
||||||
|
command: location.href.match(/#(.*)/)?[1]
|
||||||
replace: true
|
replace: true
|
||||||
|
scroll: true
|
||||||
if state.command
|
if state.command
|
||||||
Index[if Conf['Refreshed Navigation'] then 'update' else 'pageLoad'] state
|
Index[if Conf['Refreshed Navigation'] then 'update' else 'pageLoad'] state
|
||||||
return
|
|
||||||
{mode} = e.state
|
|
||||||
pageNum = Index.getCurrentPage()
|
|
||||||
unless Conf['Index Mode'] is mode and Index.currentPage is pageNum
|
|
||||||
unless Conf['Index Mode'] is mode
|
|
||||||
Conf['Index Mode'] = mode
|
|
||||||
Index.applyMode()
|
|
||||||
Index.currentPage = pageNum
|
|
||||||
Index.buildIndex()
|
|
||||||
Index.setPage()
|
|
||||||
pageNav: (e) ->
|
pageNav: (e) ->
|
||||||
return if e.shiftKey or e.altKey or e.ctrlKey or e.metaKey or e.button isnt 0
|
return if e.shiftKey or e.altKey or e.ctrlKey or e.metaKey or e.button isnt 0
|
||||||
switch e.target.nodeName
|
switch e.target.nodeName
|
||||||
@ -255,41 +256,61 @@ Index =
|
|||||||
else
|
else
|
||||||
+window.location.pathname.split('/')[2] or 1
|
+window.location.pathname.split('/')[2] or 1
|
||||||
userPageNav: (page, noRefresh) ->
|
userPageNav: (page, noRefresh) ->
|
||||||
state = Index.pushState {page}
|
state = Index.pushState {page, scroll: true}
|
||||||
if Conf['Refreshed Navigation'] and !noRefresh
|
if Conf['Refreshed Navigation'] and !noRefresh
|
||||||
Index.update state
|
Index.update state
|
||||||
else
|
else
|
||||||
Index.pageLoad state if state.page
|
Index.pageLoad state if state.page
|
||||||
pushState: (state) ->
|
pushState: (state) ->
|
||||||
{pathname, hash} = location
|
{pathname, hash} = location
|
||||||
{command} = state
|
pageBeforeSearch = history.state?.oldpage
|
||||||
switch command
|
if state.command?
|
||||||
when 'paged', 'infinite', 'all-pages', 'catalog'
|
{command} = state
|
||||||
|
if command in ['paged', 'infinite', 'all-pages', 'catalog']
|
||||||
state.mode = command.replace /-/g, ' '
|
state.mode = command.replace /-/g, ' '
|
||||||
when 'index'
|
else if command is 'index'
|
||||||
state.mode = Conf['Previous Index Mode']
|
state.mode = Conf['Previous Index Mode']
|
||||||
state.page = 1
|
state.page = 1
|
||||||
|
else if /^s=/.test command
|
||||||
|
state.search = decodeURIComponent(command[2..]).replace(/\+/g, ' ').trim()
|
||||||
|
hash = ''
|
||||||
else
|
else
|
||||||
delete state.command
|
delete state.command
|
||||||
{mode} = state
|
if state.search?
|
||||||
if mode
|
{search} = state
|
||||||
|
state.page = if search then 1 else (pageBeforeSearch or 1)
|
||||||
|
if !search
|
||||||
|
pageBeforeSearch = undefined
|
||||||
|
else if !Index.search
|
||||||
|
pageBeforeSearch = Index.currentPage
|
||||||
|
Index.search = search
|
||||||
|
if state.mode?
|
||||||
|
{mode} = state
|
||||||
delete state.mode if mode is Conf['Index Mode']
|
delete state.mode if mode is Conf['Index Mode']
|
||||||
Conf['Index Mode'] = mode
|
Conf['Index Mode'] = mode
|
||||||
state.page = 1 if mode in ['all pages', 'catalog']
|
state.page = 1 if mode in ['all pages', 'catalog']
|
||||||
hash = ''
|
hash = ''
|
||||||
{page} = state
|
if state.page?
|
||||||
if page
|
{page} = state
|
||||||
delete state.page if page is Index.currentPage
|
delete state.page if page is Index.currentPage
|
||||||
Index.currentPage = page
|
Index.currentPage = page
|
||||||
pathname = if page is 1 then "/#{g.BOARD}/" else "/#{g.BOARD}/#{page}"
|
pathname = if page is 1 then "/#{g.BOARD}/" else "/#{g.BOARD}/#{page}"
|
||||||
hash = ''
|
hash = ''
|
||||||
history[if state.replace then 'replaceState' else 'pushState'] {mode: Conf['Index Mode']}, '', pathname + hash
|
history[if state.replace then 'replaceState' else 'pushState']
|
||||||
|
mode: Conf['Index Mode']
|
||||||
|
search: Index.search
|
||||||
|
oldpage: pageBeforeSearch
|
||||||
|
, '', pathname + hash
|
||||||
state
|
state
|
||||||
pageLoad: ({mode}) ->
|
pageLoad: ({sort, search, mode, scroll}) ->
|
||||||
Index.applyMode() if mode
|
if sort or search?
|
||||||
|
Index.sort()
|
||||||
|
Index.buildPagelist()
|
||||||
|
Index.setupSearch() if search?
|
||||||
|
Index.applyMode() if mode?
|
||||||
Index.buildIndex()
|
Index.buildIndex()
|
||||||
Index.setPage()
|
Index.setPage()
|
||||||
Index.scrollToIndex()
|
Index.scrollToIndex() if scroll
|
||||||
applyMode: ->
|
applyMode: ->
|
||||||
for mode in ['paged', 'infinite', 'all pages', 'catalog']
|
for mode in ['paged', 'infinite', 'all pages', 'catalog']
|
||||||
$[if mode is Conf['Index Mode'] then 'addClass' else 'rmClass'] doc, "#{mode.replace /\ /g, '-'}-mode"
|
$[if mode is Conf['Index Mode'] then 'addClass' else 'rmClass'] doc, "#{mode.replace /\ /g, '-'}-mode"
|
||||||
@ -299,7 +320,7 @@ Index =
|
|||||||
$('#hidden-toggle a', Index.navLinks).textContent = 'Show'
|
$('#hidden-toggle a', Index.navLinks).textContent = 'Show'
|
||||||
|
|
||||||
getPagesNum: ->
|
getPagesNum: ->
|
||||||
if Index.isSearching
|
if Index.search
|
||||||
Math.ceil Index.sortedNodes.length / Index.threadsNumPerPage
|
Math.ceil Index.sortedNodes.length / Index.threadsNumPerPage
|
||||||
else
|
else
|
||||||
Index.pagesNum
|
Index.pagesNum
|
||||||
@ -422,13 +443,9 @@ Index =
|
|||||||
$.cleanCache (url) -> /^\/\/a\.4cdn\.org\//.test url
|
$.cleanCache (url) -> /^\/\/a\.4cdn\.org\//.test url
|
||||||
Index.parseThreadList pages
|
Index.parseThreadList pages
|
||||||
Index.buildThreads()
|
Index.buildThreads()
|
||||||
Index.sort()
|
state or= {}
|
||||||
Index.buildPagelist()
|
state.sort = true
|
||||||
if state?
|
Index.pageLoad state
|
||||||
Index.pageLoad state
|
|
||||||
return
|
|
||||||
Index.buildIndex()
|
|
||||||
Index.setPage()
|
|
||||||
|
|
||||||
parseThreadList: (pages) ->
|
parseThreadList: (pages) ->
|
||||||
Index.pagesNum = pages.length
|
Index.pagesNum = pages.length
|
||||||
@ -539,7 +556,7 @@ Index =
|
|||||||
{nodes} = Index
|
{nodes} = Index
|
||||||
for threadID in sortedThreadIDs
|
for threadID in sortedThreadIDs
|
||||||
sortedNodes.push nodes[Index.liveThreadIDs.indexOf(threadID)]
|
sortedNodes.push nodes[Index.liveThreadIDs.indexOf(threadID)]
|
||||||
if Index.isSearching and nodes = Index.querySearch(Index.searchInput.value)
|
if Index.search and nodes = Index.querySearch(Index.search)
|
||||||
Index.sortedNodes = nodes
|
Index.sortedNodes = nodes
|
||||||
# Sticky threads
|
# Sticky threads
|
||||||
Index.sortOnTop (thread) -> thread.isSticky
|
Index.sortOnTop (thread) -> thread.isSticky
|
||||||
@ -590,42 +607,28 @@ Index =
|
|||||||
$.event 'PostsInserted' if doc.contains Index.root
|
$.event 'PostsInserted' if doc.contains Index.root
|
||||||
ThreadHiding.onIndexBuild nodes
|
ThreadHiding.onIndexBuild nodes
|
||||||
|
|
||||||
isSearching: false
|
|
||||||
|
|
||||||
clearSearch: ->
|
clearSearch: ->
|
||||||
Index.searchInput.value = null
|
Index.searchInput.value = ''
|
||||||
Index.onSearchInput()
|
Index.onSearchInput()
|
||||||
Index.searchInput.focus()
|
Index.searchInput.focus()
|
||||||
|
|
||||||
onSearchInput: ->
|
setupSearch: (noUpdate) ->
|
||||||
if Index.isSearching = !!Index.searchInput.value.trim()
|
Index.searchInput.value = Index.search unless noUpdate
|
||||||
unless Index.searchInput.dataset.searching
|
if Index.search
|
||||||
Index.searchInput.dataset.searching = 1
|
Index.searchInput.dataset.searching = 1
|
||||||
Index.pageBeforeSearch = Index.getCurrentPage()
|
|
||||||
pageNum = 1
|
|
||||||
else
|
|
||||||
pageNum = Index.getCurrentPage()
|
|
||||||
else
|
else
|
||||||
return unless Index.searchInput.dataset.searching
|
|
||||||
pageNum = Index.pageBeforeSearch
|
|
||||||
delete Index.pageBeforeSearch
|
|
||||||
# XXX https://github.com/greasemonkey/greasemonkey/issues/1571
|
# XXX https://github.com/greasemonkey/greasemonkey/issues/1571
|
||||||
Index.searchInput.removeAttribute 'data-searching'
|
Index.searchInput.removeAttribute 'data-searching'
|
||||||
Index.sort()
|
|
||||||
# Go to the last available page if we were past the limit.
|
onSearchInput: ->
|
||||||
pageNum = Math.min pageNum, Index.getMaxPageNum() if Conf['Index Mode'] isnt 'all pages'
|
search = Index.searchInput.value.trim()
|
||||||
Index.buildPagelist()
|
return if search is Index.search
|
||||||
if Index.currentPage is pageNum
|
Index.pageLoad Index.pushState
|
||||||
Index.buildIndex()
|
search: search
|
||||||
Index.setPage()
|
replace: !!search is !!Index.search
|
||||||
else
|
|
||||||
Index.pageLoad Index.pushState {page: pageNum}
|
|
||||||
|
|
||||||
querySearch: (query) ->
|
querySearch: (query) ->
|
||||||
return unless keywords = query.toLowerCase().match /\S+/g
|
return unless keywords = query.toLowerCase().match /\S+/g
|
||||||
Index.search keywords
|
|
||||||
|
|
||||||
search: (keywords) ->
|
|
||||||
Index.sortedNodes.filter (threadRoot) ->
|
Index.sortedNodes.filter (threadRoot) ->
|
||||||
Index.searchMatch Get.threadFromRoot(threadRoot), keywords
|
Index.searchMatch Get.threadFromRoot(threadRoot), keywords
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user