Skip to content
Browse files

simplify filtering

  • Loading branch information...
1 parent cca8735 commit 44c828565d626f8f375ad34eaf2818ad29ff5b63 @astro committed Mar 29, 2013
Showing with 60 additions and 77 deletions.
  1. +6 −6 Handler/Browse.hs
  2. +13 −9 Handler/Directory.hs
  3. +1 −0 messages/de.msg
  4. +1 −0 messages/en.msg
  5. +39 −62 static/js/filter.js
View
12 Handler/Browse.hs
@@ -9,8 +9,8 @@ import System.Locale
import Network.HTTP.Types (parseQueryText)
import Text.Blaze
import Text.Blaze.Internal (MarkupM)
-import Text.Blaze.Html5 hiding (div, details, map)
-import Text.Blaze.Html5.Attributes hiding (item, min, max, id)
+import Text.Blaze.Html5 hiding (div, details, map, title)
+import Text.Blaze.Html5.Attributes hiding (item, min, max, id, title)
import qualified Data.ByteString.Char8 as BC
import Control.Monad
import Settings.StaticFiles
@@ -416,7 +416,7 @@ renderItem item showOrigin = do
countType t = length $ filter (== t) types
isOnlyType = (== 1) . countType
[whamlet|$newline always
- <article class="item"
+ <article .item .filterable
id="#{itemId item}"
xml:lang="#{fromMaybe "" $ itemLang item}">
<div>
@@ -441,12 +441,12 @@ renderItem item showOrigin = do
<p class="homepage">
<a href="#{itemHomepage item}">#{itemHomepage item}
$forall d <- itemDownloads item
- <ul class="download">
- <li class="torrent">
+ <ul .download>
+ <li .torrent>
<a href="@{TorrentFileR (downloadUser d) (downloadSlug d) (TorrentName $ downloadName d)}"
title="_{MsgDownloadTorrent $ downloadName d}"
rel="enclosure"
- data-type="#{downloadType d}">
+ type="#{downloadType d}">
<span .file-name>
$if isOnlyType (downloadType d)
#{downloadLabel d}
View
22 Handler/Directory.hs
@@ -31,13 +31,14 @@ getDirectoryR = do
|]
where renderLetter (letter, users) =
[hamlet|$newline always
- <article class="directory-page">
- <p class="letter">
+ <article .directory-page .filterable>
+ <p .letter>
<a href="@{DirectoryPageR letter}">
#{show letter}
- <ul class="users">
+ <ul .users>
$forall u <- users
- <li xml:lang="#{Model.activeFeedLangs u}"
+ <li .filterable
+ data-langs="#{Model.activeFeedLangs u}"
data-types="#{Model.activeFeedTypes u}">
<a href="@{UserR $ Model.activeUser u}">
#{T.unpack $ Model.userName $ Model.activeUser u}
@@ -67,21 +68,24 @@ getDirectoryPageR page = do
addFilterScript
[whamlet|$newline always
<h2>_{MsgHeadingDirectory}: #{show page}
+ <p .feedslist>
+ <a href=@{DirectoryR}>_{MsgDirectoryIndexBack}
<section class="directory">
$forall es <- dir
^{renderEntry es}
|]
where renderEntry es =
[hamlet|$newline always
- <article class="meta">
- <img class="logo"
+ <article .meta .filterable>
+ <img .logo
src="@{UserThumbnailR (Model.dirUser $ head es) (Thumbnail 64)}">
- <div class="title">
+ <div .title>
<h3>
<a href="@{UserR $ Model.dirUser $ head es}">#{Model.dirUserTitle $ head es}
- <ul class="feeds">
+ <ul .feeds>
$forall e <- es
- <li xml:lang="#{Model.dirFeedLang e}"
+ <li .filterable
+ xml:lang="#{Model.dirFeedLang e}"
data-types="#{Model.dirFeedTypes e}">
<a href="@{UserFeedR (Model.dirUser e) (Model.dirFeedSlug e)}">
#{Model.dirFeedTitle e}
View
1 messages/de.msg
@@ -13,6 +13,7 @@ For: Für
Users: Nutzer
Podcasters: Podcaster
Directory: Verzeichnis
+DirectoryIndexBack: Zurück zur Verzeichnisübersicht
GettingStarted: Kurzanleitung
NewTorrents: Neue Torrents
TopTorrents: Beliebte Torrents
View
1 messages/en.msg
@@ -14,6 +14,7 @@ Users: Users
Podcasters: Podcasters
GettingStarted: Getting started
Directory: Directory
+DirectoryIndexBack: Back to directory index
NewTorrents: New Torrents
TopTorrents: Popular Torrents
TopDownloadedDay: Top downloaded in 1 day
View
101 static/js/filter.js
@@ -13,20 +13,54 @@ function mapType(type) {
function Filterable(el) {
this.el = el;
+ var children = this.children = [];
+ el.find('.filterable').each(function() {
+ children.push(new Filterable($(this)));
+ });
+
+ this.langs = [];
+ var l = el.attr('xml:lang');
+ l && this.langs.push(l);
+ l = el.data('langs');
+ l && this.langs.push.apply(this.langs, l.split(','));
+
+ var types = [];
+ el.find('.torrent a').each(function() {
+ types.push($(this).attr('type'));
+ });
+ var t = el.data('types');
+ t && types.push.apply(types, t.split(','));
+ this.types = types.map(mapType);
}
Filterable.prototype = {
applyMask: function(mask) {
+ var anyVisible = false;
+ this.children.forEach(function(child) {
+ child.applyMask(mask);
+ anyVisible = anyVisible || child.isVisible();
+ });
+ if (anyVisible) {
+ this.show();
+ /* Avoid any double-hiding */
+ return;
+ }
+
var langMatch = this.langs.some(function(lang) {
return !mask.lang[lang];
});
var typeMatch = this.types.some(function(type) {
return !mask.type[type];
});
- if (typeMatch && langMatch)
+ if (typeMatch && langMatch) {
this.show();
- else
+ } else {
this.hide();
+ /* Avoid any double-hiding */
+ this.children.forEach(function(child) {
+ child.show();
+ });
+ }
},
show: function() {
this.el.removeClass('filteredout');
@@ -41,67 +75,10 @@ Filterable.prototype = {
}
};
-function FilterableItem(el) {
- Filterable.call(this, el);
-
- this.langs = [el.attr('xml:lang')];
- var types = this.types = [];
- el.find('.torrent a').each(function() {
- types.push(mapType($(this).data('type')));
- });
-}
-FilterableItem.prototype = new Filterable();
-
-function FilterableFeed(el) {
- Filterable.call(this, el);
-
- this.el = el;
- this.langs = [el.attr('xml:lang')];
- this.types = ("" + el.data('types')).split(',').map(mapType);
-}
-FilterableFeed.prototype = new Filterable();
-
-function FilterableUser(el) {
- Filterable.call(this, el);
-
- var langs = this.langs = [];
- var types = this.types = [];
- this.feeds = el.find('ul.feeds > li').map(function() {
- var feed = new FilterableFeed($(this));
- langs.push.apply(langs, feed.langs);
- types.push.apply(types, feed.types);
- return feed;
- }).toArray();
-}
-FilterableUser.prototype = new Filterable();
-FilterableUser.prototype.applyMask = function(mask) {
- var anyVisible = false;
- this.feeds.forEach(function(feed) {
- feed.applyMask(mask);
- anyVisible = anyVisible || feed.isVisible();
- });
- if (anyVisible)
- this.show();
- else {
- this.hide();
- /* Avoid double opacity drop */
- this.feeds.forEach(function(feed) {
- feed.show();
- });
- }
-};
-
-var filterItems = [];
-$('.item').each(function() {
- var el = $(this);
- filterItems.push(new FilterableItem($(this)));
-});
var Filter = {
- items: $('.item').map(function() {
- return new FilterableItem($(this));
- }).toArray().concat($('.directory .meta').map(function() {
- return new FilterableUser($(this));
- }).toArray()),
+ items: $('.filterable').map(function() {
+ return new Filterable($(this));
+ }),
isAllowedType: function(type) {
return ["text", "application", "audio", "video", "message", "image"

0 comments on commit 44c8285

Please sign in to comment.
Something went wrong with that request. Please try again.