Sort repos in repos-list component, not in controller

This commit is contained in:
Piotr Sarnacki 2015-10-20 12:28:34 +02:00
parent 47439657a1
commit 8de314e943
5 changed files with 82 additions and 27 deletions

View File

@ -1,6 +0,0 @@
`import Ember from 'ember'`
ReposListComponent = Ember.Component.extend
tagName: 'ul'
`export default ReposListComponent`

View File

@ -0,0 +1,66 @@
import Ember from 'ember';
var sortCallback = function(repo1, repo2) {
// this function could be made simpler, but I think it's clearer this way
// what're we really trying to achieve
var lastBuild1 = repo1.get('defaultBranch.lastBuild');
var lastBuild2 = repo2.get('defaultBranch.lastBuild');
if(!lastBuild1 && !lastBuild2) {
// if both repos lack builds, put newer repo first
return repo1.get('id') > repo2.get('id') ? -1 : 1;
} else if(lastBuild1 && !lastBuild2) {
// if only repo1 has a build, it goes first
return -1;
} else if(lastBuild2 && !lastBuild1) {
// if only repo2 has a build, it goes first
return 1;
}
var finishedAt1 = lastBuild1.get('finishedAt');
var finishedAt2 = lastBuild2.get('finishedAt');
if(finishedAt1) {
finishedAt1 = new Date(finishedAt1);
}
if(finishedAt2) {
finishedAt2 = new Date(finishedAt2);
}
if(finishedAt1 && finishedAt2) {
// if both builds finished, put newer first
return finishedAt1.getTime() > finishedAt2.getTime() ? -1 : 1;
} else if(finishedAt1 && !finishedAt2) {
// if repo1 finished, but repo2 didn't, put repo2 first
return 1;
} else if(finishedAt2 && !finishedAt1) {
// if repo2 finisher, but repo1 didn't, put repo1 first
return -1;
} else {
// none of the builds finished, put newer build first
return lastBuild1.get('id') > lastBuild2.get('id') ? -1 : 1;
}
throw "should not happen";
};
var ReposListComponent = Ember.Component.extend({
tagName: 'ul',
sortedRepos: function() {
var repos = this.get('repos');
if(repos && repos.toArray) {
repos = repos.toArray();
}
if(repos && repos.sort) {
return repos.sort(sortCallback);
} else {
return [];
}
}.property('repos.[]', 'repos')
});
export default ReposListComponent;

View File

@ -30,7 +30,7 @@ Controller = Ember.Controller.extend
if @get('tab') == 'owned' && @get('isLoaded') && @get('repos.length') == 0
@container.lookup('router:main').send('redirectToGettingStarted')
isLoadedBinding: 'repos.isLoaded'
isLoaded: false
repoController: Ember.inject.controller('repo')
currentUserBinding: 'auth.currentUser'
selectedRepo: (->
@ -87,20 +87,25 @@ Controller = Ember.Controller.extend
this["view_#{tab}".camelize()](params)
viewOwned: ->
@set('repos', @get('userRepos'))
@set('isLoaded', false);
if login = @get('currentUser.login')
repos = Repo.accessibleBy(@store, login).then( (reposRecordArray) =>
@set('isLoaded', true)
@set('repos', reposRecordArray)
)
# TODO: handle error
else
@set('repos', [])
viewRunning: ->
userRepos: (->
if login = @get('currentUser.login')
Repo.accessibleBy(@store, login)
else
[]
).property('currentUser.login')
viewSearch: (phrase) ->
@set('search', phrase)
@set('repos', Repo.search(@store, phrase))
@set('isLoaded', false)
Repo.search(@store, phrase).then( (reposRecordArray) =>
@set('isLoaded', true)
@set('repos', reposRecordArray)
)
searchObserver: (->
search = @get('search')

View File

@ -93,14 +93,6 @@ Repo = Model.extend
parseInt(@get('defaultBranch.lastBuild.id'))
).property('defaultBranch.lastBuild.id', 'defaultBranch.lastBuild.state')
sortOrder: (->
# cuz sortAscending seems buggy when set to false
if lastBuildFinishedAt = @get('defaultBranch.lastBuild.finishedAt')
- new Date(lastBuildFinishedAt).getTime()
else
- new Date('9999').getTime() - parseInt(@get('defaultBranch.lastBuild.id'))
).property('defaultBranch.lastBuild.state', 'defaultBranch.lastBuild.finishedAt')
stats: (->
if @get('slug')
@get('_stats') || $.get("https://api.github.com/repos/#{@get('slug')}", (data) =>

View File

@ -1,7 +1,5 @@
{{#each repos as |repo|}}
{{#each sortedRepos as |repo|}}
{{repos-list-item repo=repo selectedRepo=selectedRepo}}
{{else}}
<p class="empty">{{noReposMessage}}</p>
{{/each}}