Merge pull request #284 from travis-ci/ps-router-refactoring

Router refactoring
This commit is contained in:
Piotr Sarnacki 2014-06-03 14:42:38 +02:00
commit fe1ced55ba
17 changed files with 83 additions and 174 deletions

View File

@ -62,6 +62,7 @@ Travis.RepoSettingsController = Em.ObjectController.extend
require 'controllers/accounts' require 'controllers/accounts'
require 'controllers/auth' require 'controllers/auth'
require 'controllers/account'
require 'controllers/build' require 'controllers/build'
require 'controllers/builds' require 'controllers/builds'
require 'controllers/flash' require 'controllers/flash'

View File

@ -0,0 +1,35 @@
Travis.AccountController = Ember.ObjectController.extend
allHooks: []
init: ->
@_super.apply this, arguments
self = this
Travis.on("user:synced", (->
self.reloadHooks()
))
toggle: (hook) ->
hook.toggle()
reloadHooks: ->
if login = @get('login')
@set('allHooks', Travis.Hook.find(all: true, owner_name: login))
hooks: (->
@reloadHooks() unless hooks = @get('allHooks')
@get('allHooks').filter (hook) -> hook.get('admin')
).property('allHooks.length', 'allHooks')
hooksWithoutAdmin: (->
@reloadHooks() unless hooks = @get('allHooks')
@get('allHooks').filter (hook) -> !hook.get('admin')
).property('allHooks.length', 'allHooks')
showPrivateReposHint: (->
Travis.config.show_repos_hint == 'private'
) .property()
showPublicReposHint: (->
Travis.config.show_repos_hint == 'public'
) .property()

View File

@ -1,20 +0,0 @@
Travis.AccountIndexController = Em.Controller.extend
needs: ['profile', 'currentUser']
hooksBinding: 'controllers.profile.hooks'
allHooksBinding: 'controllers.profile.allHooks'
hooksWithoutAdminBinding: 'controllers.profile.hooksWithoutAdmin'
userBinding: 'controllers.currentUser'
sync: ->
@get('user').sync()
toggle: (hook) ->
hook.toggle()
showPrivateReposHint: (->
Travis.config.show_repos_hint == 'private'
) .property()
showPublicReposHint: (->
Travis.config.show_repos_hint == 'public'
) .property()

View File

@ -1,5 +1,2 @@
Travis.AccountsController = Ember.ArrayController.extend Travis.AccountsController = Ember.ArrayController.extend
tab: 'accounts' tab: 'accounts'
findByLogin: (login) ->
@find (account) -> account.get('login') == login

View File

@ -1,52 +1,19 @@
Travis.ProfileController = Travis.Controller.extend Travis.ProfileController = Travis.Controller.extend
name: 'profile' name: 'profile'
needs: ['currentUser', 'accounts'] needs: ['currentUser', 'accounts', 'account']
userBinding: 'controllers.currentUser' userBinding: 'controllers.currentUser'
accountsBinding: 'controllers.accounts' accountBinding: 'controllers.account'
init: ->
@_super.apply this, arguments
self = this
Travis.on("user:synced", (->
self.reloadHooks()
))
account: (->
login = @get('params.login') || @get('user.login')
account = @get('accounts').filter((account) -> account if account.get('login') == login)[0]
account.select() if account
account
).property('accounts.length', 'params.login')
sync: -> sync: ->
@get('user').sync() @get('user').sync()
toggle: (hook) ->
hook.toggle()
activate: (action, params) -> activate: (action, params) ->
@setParams(params || @get('params'))
this["view#{$.camelize(action)}"]() this["view#{$.camelize(action)}"]()
viewHooks: -> viewHooks: ->
@connectTab('hooks') @connectTab('hooks')
@reloadHooks() @get('controllers.account').reloadHooks()
reloadHooks: ->
# TODO: figure out why user is not available sometimes
@set('allHooks', Travis.Hook.find(all: true, owner_name: @get('params.login') || @get('user.login') || Travis.lookup('controller:currentUser').get('login')))
hooks: (->
@reloadHooks() unless hooks = @get('allHooks')
@get('allHooks').filter (hook) -> hook.get('admin')
).property('allHooks.length', 'allHooks')
hooksWithoutAdmin: (->
@reloadHooks() unless hooks = @get('allHooks')
@get('allHooks').filter (hook) -> !hook.get('admin')
).property('allHooks.length', 'allHooks')
viewUser: -> viewUser: ->
@connectTab('user') @connectTab('user')
@ -54,15 +21,3 @@ Travis.ProfileController = Travis.Controller.extend
connectTab: (tab) -> connectTab: (tab) ->
viewClass = Travis["#{$.camelize(tab)}View"] viewClass = Travis["#{$.camelize(tab)}View"]
@set('tab', tab) @set('tab', tab)
setParams: (params) ->
@set('params', {})
@set("params.#{key}", params[key]) for key, value of params
showPrivateReposHint: (->
Travis.config.show_repos_hint == 'private'
) .property()
showPublicReposHint: (->
Travis.config.show_repos_hint == 'public'
) .property()

View File

@ -8,9 +8,6 @@ Ember.Router.reopen
@_super(url) @_super(url)
Travis.Route = Ember.Route.extend Travis.Route = Ember.Route.extend
renderFirstSync: ->
@transitionTo 'first_sync'
beforeModel: (transition) -> beforeModel: (transition) ->
@auth.autoSignIn() unless @signedIn() @auth.autoSignIn() unless @signedIn()
@ -40,7 +37,7 @@ Travis.ApplicationRoute = Travis.Route.extend
return true return true
renderFirstSync: -> renderFirstSync: ->
@renderFirstSync() @transitionTo 'first_sync'
afterSignIn: -> afterSignIn: ->
if transition = @auth.get('afterSignInTransition') if transition = @auth.get('afterSignInTransition')
@ -75,29 +72,12 @@ Travis.Router.map ->
@route 'auth', path: '/auth' @route 'auth', path: '/auth'
@resource 'profile', path: '/profile', -> @resource 'profile', path: '/profile', ->
@route 'index', path: '/' @resource 'account', path: '/:login'
@resource 'account', path: '/:login', -> @route 'info', path: '/info'
@route 'index', path: '/'
@route 'profile', path: '/profile'
@route 'notFound', path: "/*path" @route 'notFound', path: "/*path"
Travis.SetupLastBuild = Ember.Mixin.create
setupController: ->
@repoDidLoad()
@controllerFor('repo').addObserver('repo.isLoaded', this, 'repoDidLoad')
repoDidLoad: ->
# TODO: it would be nicer to do it with promises
repo = @controllerFor('repo').get('repo')
if repo && repo.get('isLoaded') && !repo.get('lastBuildId')
Ember.run.next =>
@render('builds/not_found', into: 'repo', outlet: 'pane')
Travis.RequestsRoute = Travis.Route.extend Travis.RequestsRoute = Travis.Route.extend
renderTemplate: ->
@render 'requests', into: 'repo', outlet: 'pane'
setupController: -> setupController: ->
@_super.apply this, arguments @_super.apply this, arguments
@controllerFor('repo').activate('requests') @controllerFor('repo').activate('requests')
@ -106,9 +86,6 @@ Travis.RequestsRoute = Travis.Route.extend
Travis.Request.fetch repository_id: @modelFor('repo').get('id') Travis.Request.fetch repository_id: @modelFor('repo').get('id')
Travis.RequestRoute = Travis.Route.extend Travis.RequestRoute = Travis.Route.extend
renderTemplate: ->
@render 'request', into: 'repo', outlet: 'pane'
setupController: -> setupController: ->
@_super.apply this, arguments @_super.apply this, arguments
@controllerFor('repo').activate('request') @controllerFor('repo').activate('request')
@ -128,7 +105,6 @@ Travis.SimpleLayoutRoute = Travis.Route.extend
@_super.apply(this, arguments) @_super.apply(this, arguments)
renderTemplate: -> renderTemplate: ->
@render 'top', outlet: 'top'
@_super.apply(this, arguments) @_super.apply(this, arguments)
Travis.FirstSyncRoute = Travis.SimpleLayoutRoute.extend Travis.FirstSyncRoute = Travis.SimpleLayoutRoute.extend
@ -143,10 +119,10 @@ Travis.InsufficientOauthPermissionsRoute = Travis.SimpleLayoutRoute.extend
existingUser = document.location.hash.match(/#existing[_-]user/) existingUser = document.location.hash.match(/#existing[_-]user/)
controller.set('existingUser', existingUser) controller.set('existingUser', existingUser)
Travis.IndexCurrentRoute = Travis.Route.extend Travis.SetupLastBuild, Travis.IndexCurrentRoute = Travis.Route.extend
renderTemplate: -> renderTemplate: ->
@render 'repo' @render 'repo'
@render 'build', into: 'repo', outlet: 'pane' @render 'build', into: 'repo'
setupController: -> setupController: ->
@_super.apply this, arguments @_super.apply this, arguments
@ -170,7 +146,7 @@ Travis.IndexCurrentRoute = Travis.Route.extend Travis.SetupLastBuild,
Travis.AbstractBuildsRoute = Travis.Route.extend Travis.AbstractBuildsRoute = Travis.Route.extend
renderTemplate: -> renderTemplate: ->
@render 'builds', into: 'repo', outlet: 'pane' @render 'builds'
setupController: -> setupController: ->
@controllerFor('repo').activate(@get('contentType')) @controllerFor('repo').activate(@get('contentType'))
@ -194,9 +170,6 @@ Travis.PullRequestsRoute = Travis.AbstractBuildsRoute.extend(contentType: 'pull_
Travis.BranchesRoute = Travis.AbstractBuildsRoute.extend(contentType: 'branches') Travis.BranchesRoute = Travis.AbstractBuildsRoute.extend(contentType: 'branches')
Travis.BuildRoute = Travis.Route.extend Travis.BuildRoute = Travis.Route.extend
renderTemplate: ->
@render 'build', into: 'repo', outlet: 'pane'
serialize: (model, params) -> serialize: (model, params) ->
id = if model.get then model.get('id') else model id = if model.get then model.get('id') else model
@ -215,9 +188,6 @@ Travis.BuildRoute = Travis.Route.extend
Travis.Build.fetch(params.build_id) Travis.Build.fetch(params.build_id)
Travis.JobRoute = Travis.Route.extend Travis.JobRoute = Travis.Route.extend
renderTemplate: ->
@render 'job', into: 'repo', outlet: 'pane'
serialize: (model, params) -> serialize: (model, params) ->
id = if model.get then model.get('id') else model id = if model.get then model.get('id') else model
@ -237,13 +207,16 @@ Travis.JobRoute = Travis.Route.extend
model: (params) -> model: (params) ->
Travis.Job.fetch(params.job_id) Travis.Job.fetch(params.job_id)
Travis.RepoIndexRoute = Travis.Route.extend Travis.SetupLastBuild, Travis.RepoIndexRoute = Travis.Route.extend
setupController: (controller, model) -> setupController: (controller, model) ->
@_super.apply this, arguments @_super.apply this, arguments
@controllerFor('repo').activate('current') @controllerFor('repo').activate('current')
renderTemplate: -> renderTemplate: ->
@render 'build', into: 'repo', outlet: 'pane' if @modelFor('repo').get('lastBuildId')
@render 'build'
else
@render 'builds/not_found'
Travis.RepoRoute = Travis.Route.extend Travis.RepoRoute = Travis.Route.extend
renderTemplate: -> renderTemplate: ->
@ -280,8 +253,6 @@ Travis.IndexRoute = Travis.Route.extend
$('body').attr('id', 'home') $('body').attr('id', 'home')
@render 'repos', outlet: 'left' @render 'repos', outlet: 'left'
@render 'top', outlet: 'top'
@render 'flash', outlet: 'flash'
setupController: (controller)-> setupController: (controller)->
@container.lookup('controller:repos').activate() @container.lookup('controller:repos').activate()
@ -291,7 +262,6 @@ Travis.StatsRoute = Travis.Route.extend
renderTemplate: -> renderTemplate: ->
$('body').attr('id', 'stats') $('body').attr('id', 'stats')
@render 'top', outlet: 'top'
@render 'stats' @render 'stats'
setupController: -> setupController: ->
@ -301,7 +271,6 @@ Travis.NotFoundRoute = Travis.Route.extend
renderTemplate: -> renderTemplate: ->
$('body').attr('id', 'not-found') $('body').attr('id', 'not-found')
@render 'top', outlet: 'top'
@render 'not_found' @render 'not_found'
setupController: -> setupController: ->
@ -310,51 +279,36 @@ Travis.NotFoundRoute = Travis.Route.extend
Travis.ProfileRoute = Travis.Route.extend Travis.ProfileRoute = Travis.Route.extend
needsAuth: true needsAuth: true
setupController: -> setupController: (controller, model) ->
@container.lookup('controller:application').connectLayout('profile') @container.lookup('controller:application').connectLayout('profile')
@container.lookup('controller:accounts').set('content', Travis.Account.find(all: true)) @controllerFor('accounts').set('model', model)
model: ->
Travis.Account.fetch(all: true)
renderTemplate: -> renderTemplate: ->
$('body').attr('id', 'profile') $('body').attr('id', 'profile')
@render 'top', outlet: 'top'
@render 'accounts', outlet: 'left' @render 'accounts', outlet: 'left'
@render 'flash', outlet: 'flash'
@_super.apply(this, arguments) @_super.apply(this, arguments)
Travis.ProfileIndexRoute = Travis.Route.extend Travis.ProfileIndexRoute = Travis.Route.extend
setupController: -> redirect: ->
@container.lookup('controller:profile').activate 'hooks' # TODO: setting accounts model in ProfileRoute is wrong, but
# at this stage it's better than what we had before
renderTemplate: -> accounts = @modelFor('profile')
@render 'hooks', outlet: 'pane', into: 'profile', controller: 'profile' login = @controllerFor('currentUser').get('login')
account = accounts.find (account) -> account.get('login') == login
@transitionTo 'account', account
Travis.AccountRoute = Travis.Route.extend Travis.AccountRoute = Travis.Route.extend
setupController: (controller, account) -> setupController: (controller, account) ->
profileController = @container.lookup('controller:profile') @_super.apply this, arguments
profileController.activate 'hooks'
if account @controllerFor('profile').activate 'hooks'
params = { login: account.get('login') }
profileController.setParams(params)
model: (params) -> model: (params) ->
controller = @container.lookup('controller:accounts') @modelFor('profile').find (account) -> account.get('login') == params.login
account = controller.findByLogin(params.login)
if account
account
else
content = Ember.Object.create(login: params.login)
proxy = Ember.ObjectProxy.create(content: content)
observer = ->
if account = controller.findByLogin(params.login)
controller.removeObserver 'content.length', observer
proxy.set('content', account)
controller.addObserver 'content.length', observer
proxy
serialize: (account) -> serialize: (account) ->
if account && account.get if account && account.get
@ -362,25 +316,17 @@ Travis.AccountRoute = Travis.Route.extend
else else
{} {}
Travis.AccountIndexRoute = Travis.Route.extend Travis.ProfileInfoRoute = Travis.Route.extend
setupController: ->
@container.lookup('controller:profile').activate 'hooks'
renderTemplate: ->
@render 'hooks', outlet: 'pane', into: 'profile'
Travis.AccountProfileRoute = Travis.Route.extend
setupController: -> setupController: ->
@container.lookup('controller:profile').activate 'user' @container.lookup('controller:profile').activate 'user'
renderTemplate: -> renderTemplate: ->
@render 'user', outlet: 'pane', into: 'profile' @render 'user'
Travis.AuthRoute = Travis.Route.extend Travis.AuthRoute = Travis.Route.extend
renderTemplate: -> renderTemplate: ->
$('body').attr('id', 'auth') $('body').attr('id', 'auth')
@render 'top', outlet: 'top'
@render 'auth.signin' @render 'auth.signin'
setupController: -> setupController: ->

View File

@ -1,5 +1,5 @@
<div id="top"> <div id="top">
{{outlet top}} {{render "top"}}
</div> </div>
<div id="left"> <div id="left">
@ -7,6 +7,6 @@
</div> </div>
<div id="main"> <div id="main">
{{outlet flash}} {{render "flash"}}
{{outlet main}} {{outlet}}
</div> </div>

View File

@ -1,5 +1,5 @@
<div id="top"> <div id="top">
{{outlet top}} {{render "top"}}
</div> </div>
<div id="left"> <div id="left">
@ -7,7 +7,7 @@
</div> </div>
<div id="main"> <div id="main">
{{outlet flash}} {{render "flash"}}
{{outlet main}} {{outlet main}}
</div> </div>

View File

@ -1,8 +1,8 @@
<div id="top"> <div id="top">
{{outlet top}} {{render "top"}}
</div> </div>
<div id="main"> <div id="main">
{{outlet flash}} {{render "flash"}}
{{outlet main}} {{outlet main}}
</div> </div>

View File

@ -0,0 +1 @@
<div class="loading"><span>Loading</span></div>

View File

@ -9,7 +9,7 @@
<div class="tab"> <div class="tab">
{{#collection Travis.AccountsListView contentBinding="controller"}} {{#collection Travis.AccountsListView contentBinding="controller"}}
{{#link-to "account.index" view.account class="name"}}{{view.name}}{{/link-to}} {{#link-to "account" view.account class="name"}}{{view.name}}{{/link-to}}
<p class="summary"> <p class="summary">
<span class="repos_label">Repositories:</span> <span class="repos_label">Repositories:</span>
<abbr class="repos">{{view.account.reposCount}}</abbr> <abbr class="repos">{{view.account.reposCount}}</abbr>

View File

@ -3,6 +3,6 @@
{{view Travis.ProfileTabsView}} {{view Travis.ProfileTabsView}}
<div class="tab"> <div class="tab">
{{outlet pane}} {{outlet}}
</div> </div>

View File

@ -1,17 +1,13 @@
<ul class="tabs"> <ul class="tabs">
<li id="tab_hooks" {{bind-attr class="view.classHooks"}}> <li id="tab_hooks" {{bind-attr class="view.classHooks"}}>
<h5> <h5>
{{#with view.account}} {{#link-to "account" account}}Repositories{{/link-to}}
{{#if login}}
{{#link-to "account.index" this}}Repositories{{/link-to}}
{{/if}}
{{/with}}
</h5> </h5>
</li> </li>
{{#if view.displayUser}} {{#if view.displayUser}}
<li id="tab_user" {{bind-attr class="view.classUser"}}> <li id="tab_user" {{bind-attr class="view.classUser"}}>
<h5> <h5>
{{#link-to "account.profile" view.account}}Profile{{/link-to}} {{#link-to "profile.info"}}Profile{{/link-to}}
</h5> </h5>
</li> </li>
{{/if}} {{/if}}

View File

@ -17,7 +17,7 @@
{{/with}} {{/with}}
<div class="tab"> <div class="tab">
{{outlet pane}} {{outlet}}
</div> </div>
{{else}} {{else}}
<div class="loading"><span>Loading</span></div> <div class="loading"><span>Loading</span></div>

View File

@ -31,4 +31,4 @@
).property('account.login') ).property('account.login')
click: -> click: ->
@get('controller').transitionToRoute("account", @get('account.login')) @get('controller').transitionToRoute("account", @get('account.login'))

View File

@ -22,8 +22,6 @@ Travis.reopen
'active' if @get('tab') == 'user' 'active' if @get('tab') == 'user'
).property('tab') ).property('tab')
accountBinding: 'controller.account'
displayUser: (-> displayUser: (->
@get('controller.account.login') == @get('controller.user.login') @get('controller.account.login') == @get('controller.user.login')
).property('controller.account.login', 'controller.user.login') ).property('controller.account.login', 'controller.user.login')
@ -42,4 +40,4 @@ Travis.reopen
gravatarUrl: (-> gravatarUrl: (->
"#{location.protocol}//www.gravatar.com/avatar/#{@get('user.gravatarId')}?s=200&d=mm" "#{location.protocol}//www.gravatar.com/avatar/#{@get('user.gravatarId')}?s=200&d=mm"
).property('user.gravatarId') ).property('user.gravatarId')