Allow searching from URL bar without refreshing the page (add #s=search)

Reduce loops. Because we build threads in a loop, we can build replies
as we're building threads.

Go us.
This commit is contained in:
Zixaphir 2014-03-13 13:25:04 -07:00
parent 4fd7f628f5
commit 60b3d447c5
6 changed files with 137 additions and 114 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -96,7 +96,7 @@ Index =
@searchInput = $ '#index-search', @navLinks @searchInput = $ '#index-search', @navLinks
@searchTest() @searchTest true
@hideLabel = $ '#hidden-label', @navLinks @hideLabel = $ '#hidden-label', @navLinks
@selectMode = $ '#index-mode', @navLinks @selectMode = $ '#index-mode', @navLinks
@ -268,11 +268,15 @@ Index =
{hash} = window.location {hash} = window.location
window.location = './' + hash window.location = './' + hash
searchTest: -> searchTest: (init) ->
return unless hash = window.location.hash return false unless hash = window.location.hash
return unless match = hash.match /s=([\w]+)/ return false unless match = hash.match /s=([\w]+)/
@searchInput.value = match[1] @searchInput.value = match[1]
$.on d, '4chanXInitFinished', @onSearchInput if init
$.on d, '4chanXInitFinished', Index.onSearchInput
else
Index.onSearchInput()
return true
setupNavLinks: -> setupNavLinks: ->
for el in $$ '.navLinks.desktop > a' for el in $$ '.navLinks.desktop > a'
@ -621,12 +625,11 @@ Index =
Index.updateHideLabel() Index.updateHideLabel()
$.event 'IndexRefresh' $.event 'IndexRefresh'
buildReplies: (threads) -> buildReplies: (thread) ->
return unless Conf['Show Replies'] return unless Conf['Show Replies']
posts = [] posts = []
for thread in threads
i = Index.liveThreadIDs.indexOf thread.ID i = Index.liveThreadIDs.indexOf thread.ID
continue unless lastReplies = Index.liveThreadData[i].last_replies return unless lastReplies = Index.liveThreadData[i].last_replies
nodes = [] nodes = []
for data in lastReplies for data in lastReplies
if post = thread.posts[data.no] if post = thread.posts[data.no]
@ -738,11 +741,11 @@ Index =
threads = [] threads = []
i = threadsPerPage * pageNum i = threadsPerPage * pageNum
max = i + threadsPerPage max = i + threadsPerPage
while i < max while i < max and thread = sortedThreads[i++]
threads.push thread = sortedThreads[i++] threads.push thread
nodes.push thread.OP.nodes.root.parentNode, $.el 'hr' nodes.push thread.OP.nodes.root.parentNode, $.el 'hr'
Index.buildReplies thread
Index.buildReplies threads
Index.buildPagelist() Index.buildPagelist()
Index.setPage() Index.setPage()
@ -755,7 +758,8 @@ Index =
i = 0 i = 0
while thread = sortedThreads[i++] while thread = sortedThreads[i++]
nodes.push thread.OP.nodes.root.parentNode, $.el 'hr' nodes.push thread.OP.nodes.root.parentNode, $.el 'hr'
Index.buildReplies sortedThreads Index.buildReplies thread
$.rmAll Index.root unless infinite $.rmAll Index.root unless infinite
$.add Index.root, nodes $.add Index.root, nodes
$.event 'IndexBuild', nodes $.event 'IndexBuild', nodes

View File

@ -11,7 +11,7 @@ Navigate =
@title = -> return @title = -> return
@el = $.el 'div', @el = $.el 'span',
id: 'breadCrumb' id: 'breadCrumb'
Thread.callbacks.push Thread.callbacks.push
@ -172,13 +172,15 @@ Navigate =
navigate: (e) -> navigate: (e) ->
return if @hostname isnt 'boards.4chan.org' or window.location.hostname is 'rs.4chan.org' return if @hostname isnt 'boards.4chan.org' or window.location.hostname is 'rs.4chan.org'
return if e and (e.shiftKey or e.ctrlKey or (e.type is 'click' and e.button isnt 0)) # Not simply a left click if e
if e.shiftKey or e.ctrlKey or (e.type is 'click' and e.button isnt 0) # Not simply a left click
return
if @pathname is Navigate.path if @pathname is Navigate.path
if g.VIEW is 'thread' if g.VIEW is 'thread'
ThreadUpdater.update() ThreadUpdater.update()
else else
unless Index.searchTest()
Index.update() Index.update()
e.preventDefault() e.preventDefault()
return return

View File

@ -478,6 +478,9 @@ div.center:not(.ad-cnt) {
} }
/* Index */ /* Index */
#index-menu {
display: flex;
}
:root.thread #index-menu, :root.thread #index-menu,
:root.index-loading .navLinks, :root.index-loading .navLinks,
:root.index-loading .board, :root.index-loading .board,
@ -485,6 +488,7 @@ div.center:not(.ad-cnt) {
:root.thread .pagelist { :root.thread .pagelist {
display: none; display: none;
} }
:root:not(.catalog-mode) #index-size,
.index:not(.catalog-mode) #returnlink { .index:not(.catalog-mode) #returnlink {
display: none; display: none;
} }

View File

@ -1,11 +1,10 @@
<span class=brackets-wrap id=returnlink><a href=.././>Return</a></span>
<span class=brackets-wrap id=bottomlink><a href="#bottom">Bottom</a></span>
<span id="index-menu"> <span id="index-menu">
<input type="search" id="index-search" class="field" placeholder="Search"> <input type="search" id="index-search" class="field" placeholder="Search">
<a id="index-search-clear" class="fa fa-times-circle" href="javascript:;"></a> <a id="index-search-clear" class="fa fa-times-circle" href="javascript:;"></a>
&nbsp; &nbsp;
<time id="index-last-refresh" title="Last index refresh">...</time> <time id="index-last-refresh" title="Last index refresh">...</time>
<span id="hidden-label" hidden>&nbsp;&mdash; <span id="hidden-count"></span> <span id="hidden-toggle">[<a href="javascript:;">Show</a>]</span></span> <span id="hidden-label" hidden>&nbsp;&mdash; <span id="hidden-count"></span> <span id="hidden-toggle">[<a href="javascript:;">Show</a>]</span></span>
<span style='flex: 1'></span>
<select id="index-mode" name="Index Mode"> <select id="index-mode" name="Index Mode">
<option disabled>Index Mode</option> <option disabled>Index Mode</option>
<option value="paged">Paged</option> <option value="paged">Paged</option>
@ -27,3 +26,5 @@
<option value="large">Large</option> <option value="large">Large</option>
</select> </select>
</span> </span>
<span class=brackets-wrap id=returnlink><a href=.././>Return</a></span>
<span class=brackets-wrap id=bottomlink><a href="#bottom">Bottom</a></span>