From a4ef601748778e8b6c4b3b79869e03d5b1da0e8e Mon Sep 17 00:00:00 2001 From: Mayhem Date: Wed, 29 Jan 2014 14:31:38 +0100 Subject: [PATCH] Refactor the catalog thread views business with a class. --- Gruntfile.coffee | 1 + src/General/Build.coffee | 6 ++--- src/General/CatalogThread.coffee | 14 ++++++++++++ src/General/Index.coffee | 38 ++++++++++++++++++++++++++------ src/General/Thread.coffee | 27 ++++++----------------- 5 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 src/General/CatalogThread.coffee diff --git a/Gruntfile.coffee b/Gruntfile.coffee index c97e8e155..64a932158 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -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' diff --git a/src/General/Build.coffee b/src/General/Build.coffee index cbcb86b06..548cee44e 100644 --- a/src/General/Build.coffee +++ b/src/General/Build.coffee @@ -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 diff --git a/src/General/CatalogThread.coffee b/src/General/CatalogThread.coffee new file mode 100644 index 000000000..ae8f832d0 --- /dev/null +++ b/src/General/CatalogThread.coffee @@ -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 = @ diff --git a/src/General/Index.coffee b/src/General/Index.coffee index 54386601b..1ce167006 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -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 diff --git a/src/General/Thread.coffee b/src/General/Thread.coffee index 3103d08f1..8037f09d2 100644 --- a/src/General/Thread.coffee +++ b/src/General/Thread.coffee @@ -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