Skip to content

Commit

Permalink
Add ability to sort by name or date, asc or desc
Browse files Browse the repository at this point in the history
  • Loading branch information
zefer committed Jan 12, 2015
1 parent 7fd6707 commit f801c5a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 18 deletions.
3 changes: 2 additions & 1 deletion frontend/Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ module.exports = (grunt) ->
cwd: 'app/',
dest: '<%= config.dist_dir %>/'
src: [
'index.html',
'index.html'
'partials/*'
'assets/*'
'js/**/*.html'
]
]
vendor:
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/js/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

# last state, because of the wildcard url match
).state("browse.uri",
url: "/{uri:.*}?page"
url: "/{uri:.*}?page&sort&direction"
parent: "main"
controller: "BrowseCtrl as browseCtrl"
templateUrl: "partials/browse.html"
Expand Down
4 changes: 3 additions & 1 deletion frontend/app/js/browse-ctrl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ mod.controller("BrowseCtrl", ($scope, $stateParams, $state, library, playlist) -
$scope.$on "$stateChangeSuccess", (event, toState, toParams, fromState, fromParams) ->
toParams.uri ?= "/"
toParams.page ?= 1
library.ls(toParams.uri).then (items) ->
toParams.sort ?= "date"
toParams.direction ?= "desc"
library.ls(toParams.uri, toParams.sort, toParams.direction).then (items) ->
that.paginate(items, parseInt(toParams.page))
$scope.breadcrumbs = that.breadcrumbs(toParams.uri)

Expand Down
14 changes: 9 additions & 5 deletions frontend/app/js/library-service.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ mod.factory "library", ["$q", "mpd", ($q, mpd) ->
# Cache a single library path, so the controller can paginate without fetching
# the data again.
cache =
uri: null
key: null
items: null

cacheKey = (uri, sort, direction) ->
"#{uri}-#{sort}-#{direction}"

api =
update: (uri) -> mpd.update(uri)

ls: (uri) ->
ls: (uri, sort, direction) ->
deferred = $q.defer()
if uri == cache.uri
key = cacheKey(uri, sort, direction)
if key == cache.key
# Requesting the same path, return the data from the cache.
deferred.resolve(cache.items)
else
mpd.ls(uri).then (items) ->
mpd.ls(uri, sort, direction).then (items) ->
deferred.resolve(items)
cache.uri = uri
cache.key = key
cache.items = items
deferred.promise
]
5 changes: 5 additions & 0 deletions frontend/app/js/m-sort-by/m-sort-by.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod = angular.module('player')

mod.directive 'mSortBy', ->
restrict: 'E'
templateUrl: 'js/m-sort-by/m-sort-by.html',
20 changes: 20 additions & 0 deletions frontend/app/js/m-sort-by/m-sort-by.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="btn-group pull-right" dropdown is-open="status.isopen">
<button type="button" class="btn btn-primary dropdown-toggle" ng-disabled="disabled">
<i class="fa fa-sort-amount-asc"></i>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a ui-sref="{ sort: 'date', direction: 'desc' }">
<i class="fa fa-sort-numeric-desc"></i> Newest first
</a></li>
<li><a ui-sref="{ sort: 'date', direction: 'asc' }">
<i class="fa fa-sort-numeric-asc"></i> Oldest first
</a></li>
<li><a ui-sref="{ sort: 'name', direction: 'asc' }">
<i class="fa fa-sort-alpha-asc"></i> A to Z
</a></li>
<li><a ui-sref="{ sort: 'name', direction: 'desc' }">
<i class="fa fa-sort-alpha-desc"></i> Z to A
</a></li>
</ul>
</div>
6 changes: 4 additions & 2 deletions frontend/app/js/mpd-service.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ mod.factory "mpd", ["$rootScope", "$http", "$interval", "$q", ($rootScope, $http
replace: replace
play: play

ls: (uri) ->
ls: (uri, sort, direction) ->
deferred = $q.defer()
$http.get("/files?uri=#{escape(uri)}").success (data) ->
$http.get(
"/files?uri=#{escape(uri)}&sort=#{sort}&direction=#{direction}"
).success (data) ->
deferred.resolve(data)
deferred.promise

Expand Down
8 changes: 6 additions & 2 deletions frontend/app/partials/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
</li>
</ol>

<nav ng-show="pages.length > 1">
<ul class="pagination">
<nav>

<m-sort-by></m-sort-by>

<ul class="pagination" ng-show="pages.length > 1">
<li><a ui-sref="{ page: page - 1 }">&laquo;</a></li>
<li ng-repeat="p in ::pages" ng-class="{ 'active':p==page }">
<a ui-sref="{ page: p }">{{::p}}</a>
</li>
<li><a ui-sref="{ page: page + 1 }">&raquo;</a></li>
</ul>

</nav>

<div class="list-group">
Expand Down
44 changes: 38 additions & 6 deletions handlers/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ import (
"fmt"
"net/http"
"path"
"sort"

"github.com/golang/glog"
"github.com/zefer/mothership/mpd"
)

type FileListEntry struct {
Path string `json:"path"`
Type string `json:"type"`
Base string `json:"base"`
Path string `json:"path"`
Type string `json:"type"`
Base string `json:"base"`
LastModified string `json:"lastModified"`
}

type ByDate []*FileListEntry
type ByName []*FileListEntry

func (a ByDate) Len() int { return len(a) }
func (a ByDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDate) Less(i, j int) bool { return a[i].LastModified < a[j].LastModified }

func (a ByName) Len() int { return len(a) }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByName) Less(i, j int) bool { return a[i].Path < a[j].Path }

func FileListHandler(c *mpd.Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
Expand All @@ -32,14 +45,33 @@ func FileListHandler(c *mpd.Client) http.Handler {
for _, t := range []string{"file", "directory", "playlist"} {
if p, ok := item[t]; ok {
out[i] = &FileListEntry{
Path: p,
Type: t,
Base: path.Base(p),
Path: p,
Type: t,
Base: path.Base(p),
LastModified: item["last-modified"],
}
break
}
}
}

// Sort the list by the specified field and direction.
s := r.FormValue("sort")
d := r.FormValue("direction")
if s == "date" {
if d == "desc" {
sort.Sort(sort.Reverse(ByDate(out)))
} else {
sort.Sort(ByDate(out))
}
} else {
if d == "desc" {
sort.Sort(sort.Reverse(ByName(out)))
} else {
sort.Sort(ByName(out))
}
}

b, err := json.Marshal(out)
w.Header().Add("Content-Type", "application/json")
fmt.Fprint(w, string(b))
Expand Down

0 comments on commit f801c5a

Please sign in to comment.