woodpecker/cmd/drone-server/static/scripts/drone.js

241 lines
6.8 KiB
JavaScript
Raw Normal View History

2015-04-08 22:43:59 +00:00
'use strict';
(function () {
/**
* Creates the angular application.
*/
angular.module('drone', [
'ngRoute',
'ui.filters',
'ui.router'
]);
/**
* Bootstraps the application and retrieves the
* token from the
*/
function Authorize() {
// First, parse the query string
var params = {}, queryString = location.hash.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
// Loop through and retrieve the token
while (m = regex.exec(queryString)) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
// if the user has just received an auth token we
// should extract from the URL, save to local storage
// and then remove from the URL for good measure.
if (params.access_token) {
localStorage.setItem("access_token", params.access_token);
history.replaceState({}, document.title, location.pathname);
}
}
/**
* Defines the route configuration for the
* main application.
*/
function Config($stateProvider, $httpProvider, $locationProvider) {
// Resolver that will attempt to load the currently
// authenticated user prior to loading the page.
var resolveUser = {
user: function (users) {
return users.getCached();
}
};
$stateProvider
.state('app', {
abstract: true,
views: {
'layout': {
templateUrl: '/static/scripts/views/layout.html',
controller: function ($scope, $routeParams, repos, users) {
users.getCached().then(function (payload) {
$scope.user = payload.data;
});
}
}
}
})
.state('app.index', {
url: '/',
views: {
'toolbar': {
templateUrl: '/static/scripts/views/repos/index/toolbar.html'
},
'content': {
templateUrl: '/static/scripts/views/repos/index/content.html',
controller: 'ReposCtrl',
resolve: resolveUser
}
},
title: 'Dashboard'
})
.state('login', {
url: '/login',
2015-07-13 05:23:50 +00:00
views: {
'layout': {
templateUrl: '/static/scripts/views/login.html',
controller: 'UserLoginCtrl',
resolve: resolveUser
}
},
title: 'Login'
})
.state('app.profile', {
url: '/profile',
views: {
'toolbar': {templateUrl: '/static/scripts/views/profile/toolbar.html'},
'content': {
templateUrl: '/static/scripts/views/profile/content.html',
controller: 'UserCtrl',
resolve: resolveUser
}
},
title: 'Profile'
})
.state('app.users', {
url: '/users',
views: {
'toolbar': {templateUrl: '/static/scripts/views/users/toolbar.html'},
'content': {
templateUrl: '/static/scripts/views/users/content.html',
controller: 'UsersCtrl',
resolve: resolveUser
}
},
title: 'Users'
})
.state('app.builds', {
url: '/:owner/:name',
views: {
'toolbar': {
templateUrl: '/static/scripts/views/builds/index/toolbar.html',
controller: 'UserHeaderCtrl',
2015-07-13 05:23:50 +00:00
resolve: resolveUser
},
'content': {
templateUrl: '/static/scripts/views/builds/index/content.html',
controller: 'BuildsCtrl'
}
2015-07-13 05:23:50 +00:00
},
title: 'Builds'
})
.state('app.repo_edit', {
url: '/:owner/:name/edit',
views: {
'toolbar': {
templateUrl: '/static/scripts/views/repos/toolbar.html',
controller: 'UserHeaderCtrl',
resolve: resolveUser
},
'content': {
templateUrl: '/static/scripts/views/repos/edit.html',
controller: 'RepoEditCtrl',
resolve: resolveUser
}
},
title: 'Edit Repository'
})
.state('app.repo.env', {
url: '/:owner/:name/edit/env',
views: {
'toolbar': {templateUrl: '/static/scripts/views/repos/toolbar.html'},
'content': {templateUrl: '/static/scripts/views/repos/env.html'}
},
controller: 'RepoEditCtrl',
resolve: resolveUser
})
.state('app.repo.del', {
url: '/:owner/:name/delete',
views: {
'toolbar': {templateUrl: '/static/scripts/views/repos/toolbar.html'},
'content': {templateUrl: '/static/scripts/views/repos/del.html'}
},
controller: 'RepoEditCtrl',
resolve: resolveUser
})
.state('app.build', {
url: '/:owner/:name/:number',
views: {
2015-07-13 05:23:50 +00:00
'toolbar': {
templateUrl: '/static/scripts/views/builds/show/toolbar.html',
controller: 'UserHeaderCtrl',
2015-07-13 05:23:50 +00:00
resolve: resolveUser
},
'content': {
templateUrl: '/static/scripts/views/builds/show/content.html',
controller: 'BuildOutCtrl',
2015-07-13 05:23:50 +00:00
resolve: resolveUser
}
},
2015-07-13 05:23:50 +00:00
title: 'Build'
})
2015-07-13 19:24:52 +00:00
.state('app.job', {
url: '/:owner/:name/:number/:step',
views: {
2015-07-13 05:23:50 +00:00
'toolbar': {
templateUrl: '/static/scripts/views/builds/show/toolbar.html',
controller: 'UserHeaderCtrl',
2015-07-13 05:23:50 +00:00
resolve: resolveUser
},
'content': {
templateUrl: '/static/scripts/views/builds/show/content.html',
2015-07-13 05:23:50 +00:00
controller: 'BuildOutCtrl',
resolve: resolveUser
}
},
title: 'Build'
});
// Enables html5 mode
$locationProvider.html5Mode(true);
// Appends the Bearer token to authorize every
// outbound http request.
$httpProvider.defaults.headers.common.Authorization = 'Bearer ' + localStorage.getItem('access_token');
// Intercepts every oubput http response and redirects
// the user to the logic screen if the request was rejected.
$httpProvider.interceptors.push(function ($q, $location) {
return {
'responseError': function (rejection) {
if (rejection.status === 401 && rejection.config.url !== "/api/user") {
$location.path('/login');
}
if (rejection.status === 0) {
// this happens when the app is down or
// the browser loses internet connectivity.
}
return $q.reject(rejection);
}
};
});
}
function RouteChange($rootScope, repos, logs) {
$rootScope.$on('$stateChangeStart', function () {
2015-07-13 05:23:50 +00:00
repos.unsubscribe();
logs.unsubscribe();
});
$rootScope.$on('$stateChangeSuccess', function (event, current) {
if (current.title) {
document.title = current.title + ' · drone';
}
});
}
angular
.module('drone')
.config(Authorize)
.config(Config)
.run(RouteChange);
2015-04-08 22:43:59 +00:00
2015-04-25 00:06:46 +00:00
})();