Semaphore/public/js/app.js

101 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-03-18 23:03:28 +01:00
var app = angular.module('semaphore', ['scs.couch-potato', 'ui.router', 'ui.bootstrap', 'angular-loading-bar']);
2014-08-24 19:36:34 +02:00
2016-03-18 23:03:28 +01:00
couchPotato.configureApp(app);
2016-03-19 00:23:03 +01:00
app.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push(['$q', '$injector', '$log', function ($q, $injector, $log) {
return {
request: function (request) {
var url = request.url;
if (url.indexOf('/tpl/') !== -1) {
request.url = url = url.replace('/tpl/', '/public/html/');
}
2016-04-02 14:40:07 +02:00
if (!(url.indexOf('/public') !== -1 || url.indexOf('://') !== -1 || url.indexOf('uib/template') !== -1)) {
2016-03-19 00:23:03 +01:00
request.url = "/api" + request.url;
request.headers['Cache-Control'] = 'no-cache';
}
return request || $q.when(request);
}
};
}]);
}]);
2016-03-18 23:03:28 +01:00
app.run(['$rootScope', '$window', '$couchPotato', '$injector', '$state', '$http', function ($rootScope, $window, $couchPotato, $injector, $state, $http) {
app.lazy = $couchPotato;
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.pageTitle) {
$rootScope.pageTitle = "Loading " + toState.pageTitle;
} else {
$rootScope.pageTitle = "Loading..";
}
2014-08-24 19:36:34 +02:00
});
2016-03-18 23:03:28 +01:00
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
$rootScope.previousState = {
name: fromState.name,
params: fromParams
}
if (toState.pageTitle) {
$rootScope.pageTitle = toState.pageTitle;
} else {
$rootScope.pageTitle = "Ansible-Semaphore Page";
}
});
$rootScope.refreshUser = function () {
$rootScope.user = null;
$rootScope.loggedIn = false;
2016-04-02 14:40:07 +02:00
$rootScope.ws = null;
2016-03-19 00:23:03 +01:00
$http.get('/user')
2016-03-18 23:03:28 +01:00
.then(function (user) {
$rootScope.user = user;
$rootScope.loggedIn = true;
2016-04-02 14:40:07 +02:00
2016-05-17 17:18:26 +02:00
$rootScope.refreshInfo();
2016-04-02 14:40:07 +02:00
$rootScope.startWS();
2016-03-18 23:03:28 +01:00
}, function () {
$state.go('auth.login');
2016-03-18 23:03:28 +01:00
});
}
2016-04-02 14:40:07 +02:00
$rootScope.startWS = function () {
var ws_proto = 'ws:';
if (document.location.protocol == 'https:') {
ws_proto = 'wss:';
}
$rootScope.ws = new WebSocket(ws_proto + '//' + document.location.host + '/api/ws');
$rootScope.ws.onclose = function () {
console.log('WS closed, retrying');
setTimeout($rootScope.startWS, 2000);
}
$rootScope.ws.onmessage = function (e) {
2016-05-01 13:24:09 +02:00
try {
var d = JSON.parse(e.data);
setTimeout(function () {
$rootScope.$broadcast('task.' + d.type, d);
2016-05-01 13:24:09 +02:00
}, 3000);
} catch (_) {}
2016-04-02 14:40:07 +02:00
}
}
2016-05-17 17:18:26 +02:00
$rootScope.refreshInfo = function (cb) {
if (typeof cb != 'function') cb = function () {}
$http.get('/info').success(function (info) {
$rootScope.semaphore = info;
cb();
}).error(function () {
cb(true);
});
}
2016-03-18 23:03:28 +01:00
$rootScope.refreshUser();
}]);