Refactor the catalog thread views business with a class.

This commit is contained in:
Mayhem 2014-01-29 14:31:38 +01:00
parent 9f682bea14
commit a4ef601748
5 changed files with 56 additions and 30 deletions

View File

@ -40,6 +40,7 @@ module.exports = (grunt) ->
# <--|
'src/General/Board.coffee'
'src/General/Thread.coffee'
'src/General/CatalogThread.coffee'
'src/General/Post.coffee'
'src/General/Clone.coffee'
'src/General/DataBoard.coffee'

View File

@ -259,10 +259,9 @@ Build =
$.add root, nodes
root
threadCatalog: (thread) ->
catalogThread: (thread) ->
{staticPath, gifIcon} = Build
for data in Index.liveThreadData
break if data.no is thread.ID
data = Index.liveThreadData[Index.liveThreadIDs.indexOf thread.ID]
if data.spoiler and !Conf['Reveal Spoilers']
src = "#{staticPath}spoiler"
@ -297,6 +296,7 @@ Build =
className: 'catalog-thread'
innerHTML: <%= importHTML('General/Thread-catalog-view') %>
root.dataset.fullID = thread.fullID
$.addClass root, 'pinned' if thread.isPinned
$.addClass root, thread.OP.highlights... if thread.OP.highlights

View File

@ -0,0 +1,14 @@
class CatalogThread
@callbacks = []
toString: -> @ID
constructor: (root, @thread) ->
@ID = @thread.ID
@board = @thread.board
@nodes =
root: root
thumb: $ '.thumb', root
postCount: $ '.post-count', root
fileCount: $ '.file-count', root
pageCount: $ '.page-count', root
@thread.catalogView = @

View File

@ -15,7 +15,10 @@ Index =
@db = new DataBoard 'pinnedThreads'
Thread.callbacks.push
name: 'Thread Pinning'
cb: @node
cb: @threadNode
CatalogThread.callbacks.push
name: 'Catalog Features'
cb: @catalogNode
@button = $.el 'a',
className: 'index-refresh-shortcut fa fa-refresh'
@ -117,9 +120,24 @@ Index =
$.asap (-> $('.pagelist') or d.readyState isnt 'loading'), ->
$.replace $('.pagelist'), Index.pagelist
node: ->
threadNode: ->
return unless data = Index.db.get {boardID: @board.ID, threadID: @ID}
@pin() if data.isPinned
catalogNode: ->
$.on @nodes.thumb, 'click', Index.onClick
onClick: (e) ->
return if e.button isnt 0
root = @parentNode.parentNode
thread = g.threads[root.dataset.fullID]
if e.shiftKey
$.rm root
ThreadHiding.hide thread
ThreadHiding.saveHiddenState thread
else if e.altKey
Index.togglePin thread
else
return
e.preventDefault()
togglePin: (thread) ->
if thread.isPinned
thread.unpin()
@ -374,6 +392,16 @@ Index =
Main.handleErrors errors if errors
Main.callbackNodes Post, posts
buildCatalogViews: ->
threads = Index.sortedNodes
.filter((n, i) -> !(i % 2))
.map((threadRoot) -> Get.threadFromRoot threadRoot)
.filter (thread) -> !thread.isHidden
catalogThreads = []
for thread in threads when !thread.catalogView
catalogThreads.push new CatalogThread Build.catalogThread(thread), thread
Main.callbackNodes CatalogThread, catalogThreads
threads.map (thread) -> thread.catalogView.nodes.root
sort: ->
switch Conf['Index Sort']
when 'bump'
@ -414,11 +442,7 @@ Index =
nodesPerPage = Index.threadsNumPerPage * 2
nodes = Index.sortedNodes[nodesPerPage * pageNum ... nodesPerPage * (pageNum + 1)]
when 'catalog'
nodes = Index.sortedNodes
.filter((n, i) -> !(i % 2))
.map((threadRoot) -> Get.threadFromRoot threadRoot)
.filter((thread) -> !thread.isHidden)
.map (thread) -> thread.getCatalogView()
nodes = Index.buildCatalogViews()
else
nodes = Index.sortedNodes
$.rmAll Index.root

View File

@ -10,16 +10,19 @@ class Thread
@postLimit = false
@fileLimit = false
@OP = null
@catalogView = null
g.threads[@fullID] = board.threads[@] = @
setPage: (pageNum) ->
icon = $ '.page-num', @OP.nodes.post
for key in ['title', 'textContent']
icon[key] = icon[key].replace /\d+/, pageNum
$('.page-count', @catalogView).textContent = pageNum if @catalogView
@catalogView.nodes.pageCount.textContent = pageNum if @catalogView
setCount: (type, count, reachedLimit) ->
return unless @catalogView
el = $ ".#{type}-count", @catalogView
el = @catalogView.nodes["#{type}Count"]
el.textContent = count
(if reachedLimit then $.addClass else $.rmClass) el, 'warning'
setStatus: (type, status) ->
@ -50,28 +53,12 @@ class Thread
root = $ '.thread-icons', @catalogView
(if type is 'Sticky' and @isClosed then $.prepend else $.add) root, icon.cloneNode()
getCatalogView: ->
return @catalogView if @catalogView
@catalogView = Build.threadCatalog @
$.on $('.thumb', @catalogView), 'click', @onCatalogViewClick
@catalogView
onCatalogViewClick: (e) =>
return if e.button isnt 0
if e.shiftKey
$.rm @catalogView
ThreadHiding.hide @
ThreadHiding.saveHiddenState @
else if e.altKey
Index.togglePin @
else
return
e.preventDefault()
pin: ->
@isOnTop = @isPinned = true
$.addClass @catalogView, 'pinned' if @catalogView
$.addClass @catalogView.nodes.root, 'pinned' if @catalogView
unpin: ->
@isOnTop = @isPinned = false
$.rmClass @catalogView, 'pinned' if @catalogView
$.rmClass @catalogView.nodes.root, 'pinned' if @catalogView
kill: ->
@isDead = true