Skip to content

Commit

Permalink
Use UI-router instead of ng-router
Browse files Browse the repository at this point in the history
ui-router offers nested routes, which is useful for forcesched dialogs
and direct access to the dialog

From the URL of the builder, you can add /forceched/myforcesched to go
to the forcesched dialog

Some impact on how the plugins should register new routes. Plugins
just have to use ui-router api to declare new routes.
Simplifies the top menu

Signed-off-by: Pierre Tardy <pierre.tardy@intel.com>
  • Loading branch information
Pierre Tardy committed Oct 12, 2013
1 parent 4494c72 commit ddb758b
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 137 deletions.
3 changes: 2 additions & 1 deletion www/Gruntfile.coffee
Expand Up @@ -155,7 +155,8 @@ module.exports = (grunt) ->
'test/dataspec' # test mocks in dev mode
'test/mocks/*' # test mocks in dev mode
'app' # app needs libs
'{routes,views,config,*/**}' # remaining angularjs components
'routes' # default routes first
'{views,config,*/**}' # remaining angularjs components
'run' # run has to be in the end, because it is triggering
# angular's own DI
]
Expand Down
6 changes: 3 additions & 3 deletions www/setup.py
Expand Up @@ -88,7 +88,7 @@ def check_output(cmd):
"grunt-coffeelint": "~0.0.7",
"grunt-requiregen": "~0.1.0",
"grunt-karma": "~0.6.0",
"bower": "~1.1.0"
"bower": "~1.2.7"
},
"engines": {
"node": "0.8.x",
Expand All @@ -106,9 +106,9 @@ def check_output(cmd):
"bootstrap": "~3.0.0",
"font-awesome": "latest",
"angular": ANGULAR_TAG,
"angular-route": ANGULAR_TAG,
"angular-animate": ANGULAR_TAG,
"angular-bootstrap": "latest",
"angular-bootstrap": "~0.6.0",
"angular-ui-router": "~0.2.0",
"restangular": "latest",
"lodash": "latest",
"html5shiv": "~3.6.2",
Expand Down
2 changes: 1 addition & 1 deletion www/src/index.jade
Expand Up @@ -16,4 +16,4 @@ block content
div(ng-controller="alertPanelController")
alert.animate-repeat(ng-repeat="alert in alerts",type="alert.type",close="closeAlert($index)")
| {{alert.msg}}{{alert.occur_msg}}
ng:view
ui:view
2 changes: 1 addition & 1 deletion www/src/scripts/app.coffee
@@ -1 +1 @@
angular.module 'app', ['restangular', 'ui.bootstrap', 'templates-views', 'ngRoute', 'ngAnimate']
angular.module 'app', ['restangular', 'ui.bootstrap', 'templates-views', 'ui.router', 'ngAnimate']
8 changes: 4 additions & 4 deletions www/src/scripts/controllers/buildController.coffee
@@ -1,9 +1,9 @@
angular.module('app').controller 'buildController',
['$log', '$scope', '$location', 'buildbotService', '$routeParams'
($log, $scope, $location, buildbotService, $routeParams) ->
buildbotService.one('builder', $routeParams.builder).get().then (builder) ->
['$log', '$scope', '$location', 'buildbotService', '$stateParams'
($log, $scope, $location, buildbotService, $stateParams) ->
buildbotService.one('builder', $stateParams.builder).get().then (builder) ->
$scope.builder = builder[0]
build = buildbotService.one('builder', $routeParams.builder).one('build', $routeParams.build)
build = buildbotService.one('builder', $stateParams.builder).one('build', $stateParams.build)
build.all('step').bind($scope, 'steps')
build.bind($scope, 'build').then ->
buildbotService.one("buildslave", $scope.build.buildslaveid).get().then (buildslave) ->
Expand Down
6 changes: 3 additions & 3 deletions www/src/scripts/controllers/builderController.coffee
@@ -1,7 +1,7 @@
angular.module('app').controller 'builderController',
['$log', '$scope', '$location', 'buildbotService', '$routeParams'
($log, $scope, $location, buildbotService, $routeParams) ->
builder = buildbotService.one('builder', $routeParams.builder)
['$log', '$scope', '$location', 'buildbotService', '$stateParams'
($log, $scope, $location, buildbotService, $stateParams) ->
builder = buildbotService.one('builder', $stateParams.builder)
builder.bind($scope, 'builder')
builder.all('build').bind($scope, 'builds')
]
4 changes: 2 additions & 2 deletions www/src/scripts/controllers/schedulersController.coffee
@@ -1,6 +1,6 @@
angular.module('app').controller 'schedulersController',
['$log', '$scope', '$location', 'buildbotService', '$routeParams'
($log, $scope, $location, buildbotService, $routeParams) ->
['$log', '$scope', '$location', 'buildbotService', '$stateParams'
($log, $scope, $location, buildbotService, $stateParams) ->
buildbotService.all('scheduler').getList().then (schedulers) ->
$scope.schedulers = schedulers
]
34 changes: 18 additions & 16 deletions www/src/scripts/directives/topmenu.coffee
@@ -1,24 +1,26 @@
angular.module('app').directive 'topmenu',
['$log', 'route_config', ($log, route_config) ->
['$log', ($log) ->
controller = [
'$scope', '$element', '$rootScope', ($scope, $element, $rootScope) ->
$scope.tabs = []
$scope.select = (tab) ->
return if tab.selected is true
angular.forEach $scope.tabs, (tab) ->
tab.selected = false

tab.selected = true
'$scope', '$element', 'topMenuStates', ($scope, $element, topMenuStates) ->

@addTab = (tab, tabId) ->
$scope.select tab if $scope.tabs.length is 0
$scope.tabs.push tab
$scope.tabs = []
select = (state) ->
return if !state
for tab in $scope.tabs
if tab.name == state.data.tabid
tab.data.selected = true
else
tab.data.selected = false
@addTab = (tab) ->
select(tab)
$scope.tabs.push(tab)

for id, tab of route_config
if tab.caption?
@addTab(tab, id)
for tab in topMenuStates
if tab.data.caption?
@addTab(tab)

$rootScope.$watch("selectedTab", $scope.select)
$scope.$on "$stateChangeSuccess", (ev, state) ->
select(state)
]
controller: controller
replace: true
Expand Down
1 change: 0 additions & 1 deletion www/src/scripts/libs/angular-route.js

This file was deleted.

1 change: 1 addition & 0 deletions www/src/scripts/libs/angular-ui-router.js
184 changes: 87 additions & 97 deletions www/src/scripts/routes.coffee
@@ -1,104 +1,94 @@
angular.module('app').constant("route_config",
home:
caption: "Home"
route: "/"
builders:
caption: "Builders"
route: "/builders"
# lastbuilds:
# caption: "Last Builds"
# route: "/lastbuilds"
changes:
caption: "Last Changes"
route: "/changes"
# buildslaves:
# caption: "Build Slaves"
# route: "/buildslaves"
# buildmasters:
# caption: "Build Masters"
# route: "/buildmasters"
schedulers:
caption: "Schedulers"
route: "/schedulers"
# users:
# caption: "Users"
# route: "/users"
# admin:
# caption: "Admin"
# route: "/admin"
about:
caption: "About"
route: "/about"

# order is important in the configuration stage
# so we need to have this provider defined in routes.coffee
# this provider holds the list of states (which unfortunatly not a feature of ui-router)
# so that top menu can be autogenerated
angular.module('app').provider 'topMenuStates', ->
states = []
@state = (state) ->
states.push(state)
@$get = ->
states

builder:
route: "/builders/:builder"
tabid: "builders"
build:
route: "/builders/:builder/build/:build"
tabid: "builders"
step:
route: "/builders/:builder/build/:build/steps/:step"
tabid: "builders"
log:
route: "/builders/:builder/build/:build/steps/:step/logs/:log"
tabid: "builders"
angular.module('app').config [ "$stateProvider", "$urlRouterProvider", "topMenuStatesProvider"
($stateProvider, $urlRouterProvider, topMenuStatesProvider) ->

buildslave:
route: "/buildslaves/:buildslave"
tabid: "buildslaves"
buildmaster:
route: "/buildmasters/:buildmaster"
tabid: "buildmasters"
user:
route: "/users/:user"
tabid: "users"
$urlRouterProvider.otherwise('/');

editconf:
route: "/admin/:conffile"
tabid: "admin"
)
route_config_fn = ($routeProvider, route_config, plugins_routes...) ->
# agregate the base app route_config with plugin route config
# :-/ need to watch for namespace clashes between plugins...
for i in [0..plugins_routes.length - 1]
plugin = plugin_names[i]
plugin_routes = plugins_routes[i]
for id, route_cfg of plugin_routes
route_config[id] = route_cfg
if !route_cfg.templateUrl
route_cfg.templateUrl = "#{plugin}/views/#{id}.html"
# by convention, in this module the key of the mapping
# is the name of the template and of the controller
# If the route has a caption, it is linked in the top menu
# The route is configured in $routeProvider
default_routes =
home:
caption: "Home"
url: "/"
builders:
caption: "Builders"
url: "/builders"
# lastbuilds:
# caption: "Last Builds"
# url: "/lastbuilds"
changes:
caption: "Last Changes"
url: "/changes"
# buildslaves:
# caption: "Build Slaves"
# url: "/buildslaves"
# buildmasters:
# caption: "Build Masters"
# url: "/buildmasters"
schedulers:
caption: "Schedulers"
url: "/schedulers"
# users:
# caption: "Users"
# url: "/users"
# admin:
# caption: "Admin"
# url: "/admin"
about:
caption: "About"
url: "/about"

# by convention, the key of the first mapping
# is the name of the template and of the controller
# If the route has a caption, it is linked in the top menu
# The route is configured in $routeProvider
$.each route_config, (id, cfg) ->
# needs to be a $.each to create a scope for changeTab..
cfg.tabid ?= id
cfg.tabhash = "##{id}"
if !cfg.controller
cfg.controller = "#{id}Controller"
if !cfg.templateUrl
cfg.templateUrl = "views/#{id}.html"
$routeProvider
.when cfg.route,
controller: cfg.controller
reloadOnSearch: true
templateUrl: cfg.templateUrl
resolve:
changeTab: ['$rootScope', ($rootScope) ->
$rootScope.selectedTab = cfg
]
$routeProvider.otherwise redirectTo: '/'

# generate the dependancy injection dynamically, given all plugins
di = ['$routeProvider', 'route_config']
plugin_names = []
if @config?
for plugin, cfg of @config.plugins
di.push(plugin + "_route_config")
plugin_names.push(plugin)
builder:
url: "/builders/:builder"
tabid: "builders"
build:
url: "/builders/:builder/build/:build"
tabid: "builders"
step:
url: "/builders/:builder/build/:build/steps/:step"
tabid: "builders"
log:
url: "/builders/:builder/build/:build/steps/:step/logs/:log"
tabid: "builders"

di.push(route_config_fn)
angular.module('app').config(di)
buildslave:
url: "/buildslaves/:buildslave"
tabid: "buildslaves"
buildmaster:
url: "/buildmasters/:buildmaster"
tabid: "buildmasters"
user:
url: "/users/:user"
tabid: "users"

editconf:
url: "/admin/:conffile"
tabid: "admin"

for id, cfg of default_routes
cfg.tabid ?= id
cfg.tabhash = "##{id}"
state =
controller: "#{id}Controller"
templateUrl: "views/#{id}.html"
name: id
url: cfg.url
data: cfg

$stateProvider.state(state)
topMenuStatesProvider.state(state)
]
8 changes: 2 additions & 6 deletions www/src/scripts/services/alertService.coffee
Expand Up @@ -20,13 +20,9 @@ angular.module('app').config ['$httpProvider', ($httpProvider) ->
return (promise) ->
promise.then (res)->
res
, (res) ->
if res.config.url.indexOf("views") == 0 and res.status == 404
alert.error "view does not exist: " + res.config.url
else
alert.error res.data
, (res, bla) ->
alert.error res.toString()
$q.reject res
console.log res
]

]
4 changes: 2 additions & 2 deletions www/src/views/directives/topmenu.jade
@@ -1,3 +1,3 @@
ul.nav.navbar-nav
li(ng-repeat='tab in tabs', ng-class='{active:tab.selected}')
a(href='{{tab.tabhash}}', ng-click='select(tab)') {{tab.caption}}
li(ng-repeat='tab in tabs', ng-class='{active:tab.data.selected}')
a(href='{{tab.data.tabhash}}') {{tab.data.caption}}

0 comments on commit ddb758b

Please sign in to comment.