Ver Fonte

Enable sorting on list views by either date or title/name (alpha)

Markus Ochel há 12 anos atrás
pai
commit
00088ac3f9

+ 3 - 3
admin/controllers/authors.coffee

@@ -56,13 +56,13 @@ class AuthorForm extends Spine.Controller
       @title = 'New Author'
       @item = {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
     @html templates.render('author-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
     else
       @formSite.val(@stack.stack.filterBox.siteId)
@@ -161,7 +161,7 @@ class AuthorList extends Spine.Controller
 
   render: =>
     context = 
-      authors: Author.filter(@filterObj).sort(Author.nameSort)
+      authors: Author.filter(@filterObj).sort(Author.alphaSort)
     @html templates.render('authors.html', {}, context)
 
   filter: (@filterObj) =>

+ 3 - 3
admin/controllers/blocks.coffee

@@ -60,13 +60,13 @@ class BlockForm extends Spine.Controller
 
     @item._attachments ?= {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
     @html templates.render('block-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
       @formEnabled.prop('checked', @item.enabled)
     else
@@ -165,7 +165,7 @@ class BlockList extends Spine.Controller
 
   render: =>
     context = 
-      blocks: Block.filter(@filterObj).sort(Block.titleSort)
+      blocks: Block.filter(@filterObj).sort(Block.alphaSort)
     @html templates.render('blocks.html', {}, context)
 
   filter: (@filterObj) =>

+ 5 - 4
admin/controllers/collections.coffee

@@ -66,14 +66,14 @@ class CollectionForm extends Spine.Controller
 
     @item._attachments ?= {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
-    @item.sponsors = Sponsor.all().sort(Sponsor.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
+    @item.sponsors = Sponsor.all().sort(Sponsor.alphaSort)
     @html templates.render('collection-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
       @formSponsorId.val(@item.sponsor_id)
       @formPinned.prop('checked', @item.pinned)
@@ -178,8 +178,9 @@ class CollectionList extends Spine.Controller
     Spine.bind 'filterbox:change', @filter
 
   render: =>
+    sortFunc = if @filterObj?.sortBy then Collection[@filterObj.sortBy] else Collection.dateSort
     context = 
-      collections: Collection.filter(@filterObj).sort(Collection.nameSort)
+      collections: Collection.filter(@filterObj).sort(sortFunc)
     @html templates.render('collections.html', {}, context)
 
   filter: (@filterObj) =>

+ 1 - 1
admin/controllers/contacts.coffee

@@ -118,7 +118,7 @@ class ContactList extends Spine.Controller
 
   render: =>
     context = 
-      contacts: Contact.filter(@filterObj).sort(Contact.nameSort)
+      contacts: Contact.filter(@filterObj).sort(Contact.alphaSort)
     @html templates.render('contacts.html', {}, context)
 
   filter: (@filterObj) =>

+ 6 - 3
admin/controllers/dashboard.coffee

@@ -22,10 +22,13 @@ class DashboardOne extends Spine.Controller
     Spine.bind 'filterbox:change', @filter
 
   render: =>
+    essaySortFunc = if @filterObj?.sortBy then Essay[@filterObj.sortBy] else Essay.dateSort
+    videoSortFunc = if @filterObj?.sortBy then Video[@filterObj.sortBy] else Video.dateSort
+    sceneSortFunc = if @filterObj?.sortBy then Scene[@filterObj.sortBy] else Scene.dateSort
     context = 
-      essays: Essay.select(@selectFilter)
-      videos: Video.select(@selectFilter)
-      scenes: Scene.select(@selectFilter)
+      essays: Essay.select(@selectFilter).sort(essaySortFunc)
+      videos: Video.select(@selectFilter).sort(videoSortFunc)
+      scenes: Scene.select(@selectFilter).sort(sceneSortFunc)
     @html templates.render('dashboard.html', {}, context)
 
   selectFilter: (item) =>

+ 7 - 6
admin/controllers/essays.coffee

@@ -81,14 +81,14 @@ class EssayForm extends Spine.Controller
     @item.collections ?= []
     @item._attachments ?= {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
-    @item.sponsors = Sponsor.all().sort(Sponsor.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
+    @item.sponsors = Sponsor.all().sort(Sponsor.alphaSort)
     @html templates.render('essay-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
       @formSponsorId.val(@item.sponsor_id)
       @formPublished.prop('checked', @item.published)
@@ -118,7 +118,7 @@ class EssayForm extends Spine.Controller
       $siteSelected.html ""
 
   makeAuthorsList: (site) ->
-    authors = Author.findAllByAttribute('site', site.id).sort(Author.nameSort)
+    authors = Author.findAllByAttribute('site', site.id).sort(Author.alphaSort)
     @formAuthorId.empty()
       .append "<option value=\"\">Select an author...</option>"
     for author in authors
@@ -126,7 +126,7 @@ class EssayForm extends Spine.Controller
     @formAuthorId.val(@item.author_id)
   
   makeCollectionsList: (site) ->
-    collections = Collection.findAllByAttribute('site', site.id).sort(Collection.nameSort)
+    collections = Collection.findAllByAttribute('site', site.id).sort(Collection.alphaSort)
     @collectionSelectUI = new MultiSelectUI
       items: collections
       selectedItems: (c.id for c in @item.collections)
@@ -267,8 +267,9 @@ class EssayList extends Spine.Controller
     Spine.bind 'filterbox:change', @filter
 
   render: =>
+    sortFunc = if @filterObj?.sortBy then Essay[@filterObj.sortBy] else Essay.dateSort
     context = 
-      essays: Essay.filter(@filterObj).sort(Essay.titleSort)
+      essays: Essay.filter(@filterObj).sort(sortFunc)
     @html templates.render('essays.html', {}, context)
 
   filter: (@filterObj) =>

+ 8 - 1
admin/controllers/filter-box.coffee

@@ -15,11 +15,12 @@ class FilterBox extends Spine.Controller
     super
     @siteId = ''
     @query = ''
+    @sortBy = ''
     Site.bind 'change refresh', @render
 
   render: =>
     context = 
-      sites: Site.all().sort(Site.nameSort)
+      sites: Site.all().sort(Site.alphaSort)
     @html templates.render('filter-box.html', {}, context)
     @siteId = ''
     @query = ''
@@ -32,6 +33,10 @@ class FilterBox extends Spine.Controller
     @filterClear = $(@el).find('.clear-filter')
     @selectedSite = $(@el).find('.selected-site')
     @siteSelector = $(@el).find('ul.site-selector')
+    @sortBySelector = $(@el).find('input[name=sort-by]')
+
+    @sortBySelector.on 'change', (e) =>
+      @filter()
     
     @filterInput.on 'keyup', (e) =>
       if e.keyCode is KEYCODE_ESC
@@ -81,9 +86,11 @@ class FilterBox extends Spine.Controller
       @filterClear.addClass('active')
     else
       @filterClear.removeClass('active')
+    @sortBy = @sortBySelector.filter(':checked').val()
     Spine.trigger 'filterbox:change',
       query: @query
       siteId: @siteId
+      sortBy: @sortBy
 
 
 module.exports = FilterBox

+ 3 - 3
admin/controllers/redirects.coffee

@@ -50,13 +50,13 @@ class RedirectForm extends Spine.Controller
       @title = 'New Redirect'
       @item = {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
     @html templates.render('redirect-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
       @formSlug.prop('readonly', true).attr('title', 'Can not change the slug')
     else
@@ -142,7 +142,7 @@ class RedirectList extends Spine.Controller
 
   render: =>
     context = 
-      redirects: Redirect.filter(@filterObj).sort(Redirect.nameSort)
+      redirects: Redirect.filter(@filterObj).sort(Redirect.alphaSort)
     @html templates.render('redirects.html', {}, context)
 
   filter: (@filterObj) =>

+ 7 - 6
admin/controllers/scenes.coffee

@@ -79,14 +79,14 @@ class SceneForm extends Spine.Controller
     @item.collections ?= []
     @item._attachments ?= {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
-    @item.sponsors = Sponsor.all().sort(Sponsor.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
+    @item.sponsors = Sponsor.all().sort(Sponsor.alphaSort)
     @html templates.render('scene-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
       @formSponsorId.val(@item.sponsor_id)
       @formPublished.prop('checked', @item.published)
@@ -116,7 +116,7 @@ class SceneForm extends Spine.Controller
       $siteSelected.html ""
 
   makeAuthorsList: (site) ->
-    authors = Author.findAllByAttribute('site', site.id).sort(Author.nameSort)
+    authors = Author.findAllByAttribute('site', site.id).sort(Author.alphaSort)
     @formAuthorId.empty()
       .append "<option value=\"\">Select an author...</option>"
     for author in authors
@@ -124,7 +124,7 @@ class SceneForm extends Spine.Controller
     @formAuthorId.val(@item.author_id)
   
   makeCollectionsList: (site) ->
-    collections = Collection.findAllByAttribute('site', site.id).sort(Collection.nameSort)
+    collections = Collection.findAllByAttribute('site', site.id).sort(Collection.alphaSort)
     @collectionSelectUI = new MultiSelectUI
       items: collections
       selectedItems: (c.id for c in @item.collections)
@@ -265,8 +265,9 @@ class SceneList extends Spine.Controller
     Spine.bind 'filterbox:change', @filter
 
   render: =>
+    sortFunc = if @filterObj?.sortBy then Scene[@filterObj.sortBy] else Scene.dateSort
     context = 
-      scenes: Scene.filter(@filterObj).sort(Scene.titleSort)
+      scenes: Scene.filter(@filterObj).sort(sortFunc)
     @html templates.render('scenes.html', {}, context)
 
   filter: (@filterObj) =>

+ 2 - 2
admin/controllers/sites.coffee

@@ -56,7 +56,7 @@ class SiteForm extends Spine.Controller
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formTheme.val(@item.theme)
       @formSiteId.prop('readonly', true)
       @formDefaultAdEnabled.prop('checked', @item.default_ad_enabled)
@@ -150,7 +150,7 @@ class SiteList extends Spine.Controller
 
   render: =>
     context = 
-      sites: Site.filter(@filterObj).sort(Site.nameSort)
+      sites: Site.filter(@filterObj).sort(Site.alphaSort)
     @html templates.render('sites.html', {}, context)
 
   filter: (@filterObj) =>

+ 3 - 3
admin/controllers/sponsors.coffee

@@ -60,13 +60,13 @@ class SponsorForm extends Spine.Controller
 
     @item._attachments ?= {}
     
-    @item.contacts = Contact.all().sort(Contact.nameSort)
+    @item.contacts = Contact.all().sort(Contact.alphaSort)
     @html templates.render('sponsor-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formContactId.val(@item.contact_id)
       @formFormat.val(@item.format)
       @formShowLabel.prop('checked', @item.show_label)
@@ -157,7 +157,7 @@ class SponsorList extends Spine.Controller
 
   render: =>
     context = 
-      sponsors: Sponsor.filter(@filterObj).sort(Sponsor.nameSort)
+      sponsors: Sponsor.filter(@filterObj).sort(Sponsor.alphaSort)
     @html templates.render('sponsors.html', {}, context)
 
   filter: (@filterObj) =>

+ 7 - 6
admin/controllers/videos.coffee

@@ -81,14 +81,14 @@ class VideoForm extends Spine.Controller
     @item.collections ?= []
     @item._attachments ?= {}
     
-    @item.sites = Site.all().sort(Site.nameSort)
-    @item.sponsors = Sponsor.all().sort(Sponsor.nameSort)
+    @item.sites = Site.all().sort(Site.alphaSort)
+    @item.sponsors = Sponsor.all().sort(Sponsor.alphaSort)
     @html templates.render('video-form.html', {}, @item)
 
     @itemTitle.html @title
     
     # Set few initial form values
-    if @editing
+    if @editing or @copying
       @formSite.val(@item.site)
       @formSponsorId.val(@item.sponsor_id)
       @formPublished.prop('checked', @item.published)
@@ -118,7 +118,7 @@ class VideoForm extends Spine.Controller
       $siteSelected.html ""
 
   makeAuthorsList: (site) ->
-    authors = Author.findAllByAttribute('site', site.id).sort(Author.nameSort)
+    authors = Author.findAllByAttribute('site', site.id).sort(Author.alphaSort)
     @formAuthorId.empty()
       .append "<option value=\"\">Select an author...</option>"
     for author in authors
@@ -126,7 +126,7 @@ class VideoForm extends Spine.Controller
     @formAuthorId.val(@item.author_id)
   
   makeCollectionsList: (site) ->
-    collections = Collection.findAllByAttribute('site', site.id).sort(Collection.nameSort)
+    collections = Collection.findAllByAttribute('site', site.id).sort(Collection.alphaSort)
     @collectionSelectUI = new MultiSelectUI
       items: collections
       selectedItems: (c.id for c in @item.collections)
@@ -267,8 +267,9 @@ class VideoList extends Spine.Controller
     Spine.bind 'filterbox:change', @filter
 
   render: =>
+    sortFunc = if @filterObj?.sortBy then Video[@filterObj.sortBy] else Video.dateSort
     context = 
-      videos: Video.filter(@filterObj).sort(Video.titleSort)
+      videos: Video.filter(@filterObj).sort(sortFunc)
     @html templates.render('videos.html', {}, context)
 
   filter: (@filterObj) =>

+ 5 - 5
admin/models/base.coffee

@@ -2,11 +2,11 @@ Spine = require('spine/core')
 
 class BaseModel extends Spine.Model
 
-  @nameSort: (a, b) ->
-    if a.name > b.name then 1 else -1
-
-  @titleSort: (a, b) ->
-    if a.title > b.title then 1 else -1
+  @alphaSort: (a, b) ->
+    if a.name?
+      if a.name > b.name then 1 else -1
+    else if a.title?
+      if a.title > b.title then 1 else -1
 
   @queryOn: ['name','title']
 

+ 3 - 0
admin/models/collection.coffee

@@ -11,6 +11,9 @@ class Collection extends BaseModel
 
   @extend @CouchAjax
 
+  @dateSort: (a, b) ->
+    if (a.updated_at or a.name) < (b.updated_at or b.name) then 1 else -1
+
   @queryOn: ['name','slug']
     
   validate: ->

+ 2 - 2
admin/models/essay.coffee

@@ -11,11 +11,11 @@ class Essay extends BaseModel
   
   @extend @CouchAjax
   
-  @titleSort: (a, b) ->
+  @alphaSort: (a, b) ->
     if (a.title or a.published_at) > (b.title or b.published_at) then 1 else -1
 
   @dateSort: (a, b) ->
-    if (a.published_at or a.title) > (b.published_at or b.title) then 1 else -1
+    if (a.published_at or a.title) < (b.published_at or b.title) then 1 else -1
 
   @queryOn: ['title','slug']
     

+ 2 - 2
admin/models/scene.coffee

@@ -11,11 +11,11 @@ class Scene extends BaseModel
   
   @extend @CouchAjax
   
-  @titleSort: (a, b) ->
+  @alphaSort: (a, b) ->
     if (a.title or a.published_at) > (b.title or b.published_at) then 1 else -1
 
   @dateSort: (a, b) ->
-    if (a.published_at or a.title) > (b.published_at or b.title) then 1 else -1
+    if (a.published_at or a.title) < (b.published_at or b.title) then 1 else -1
 
   @queryOn: ['title','slug']
     

+ 2 - 2
admin/models/video.coffee

@@ -11,11 +11,11 @@ class Video extends BaseModel
   
   @extend @CouchAjax
   
-  @titleSort: (a, b) ->
+  @alphaSort: (a, b) ->
     if (a.title or a.published_at) > (b.title or b.published_at) then 1 else -1
 
   @dateSort: (a, b) ->
-    if (a.published_at or a.title) > (b.published_at or b.title) then 1 else -1
+    if (a.published_at or a.title) < (b.published_at or b.title) then 1 else -1
 
   @queryOn: ['title','slug']
     

+ 10 - 0
admin/static/css/theme.styl

@@ -121,6 +121,16 @@ span.label
       &.selected
         opacity: 0.5
 
+  .filter-sort
+    position: relative
+    padding: 10px 40px 10px 10px
+    border: 1px solid $faintGrey
+    border-top: 0
+    line-height: 1em
+    cursor: pointer
+    -webkit-user-select: none
+    background: #f9f9f9
+
 
 .main.navbar
   position: absolute

+ 6 - 0
admin/templates/filter-box.html

@@ -11,3 +11,9 @@
   <li class="site-name theme-{{theme}}" data-id="{{_id}}">{{{name_html}}}</li>
   {{/each}}
 </ul>
+
+<div class="filter-sort">
+  <strong>Sort by</strong>
+  <label><input type="radio" name="sort-by" value="dateSort" checked> Date</label>
+  <label><input type="radio" name="sort-by" value="alphaSort"> Alpha</label>
+</div>