2016-07-07 18:03:32 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2016-04-13 12:55:05 +02:00
|
|
|
(function() {
|
2016-07-04 11:09:45 +02:00
|
|
|
'use strict';
|
|
|
|
angular
|
|
|
|
.module('harbor.app')
|
|
|
|
.config(function($interpolateProvider){
|
|
|
|
$interpolateProvider.startSymbol('//');
|
|
|
|
$interpolateProvider.endSymbol('//');
|
|
|
|
})
|
2016-07-07 18:03:32 +02:00
|
|
|
.config(function($httpProvider) {
|
2016-07-09 10:38:54 +02:00
|
|
|
//initialize get if not there
|
|
|
|
if (!$httpProvider.defaults.headers.get) {
|
|
|
|
$httpProvider.defaults.headers.get = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Answer edited to include suggestions from comments
|
|
|
|
// because previous version of code introduced browser-related errors
|
|
|
|
|
|
|
|
//disable IE ajax request caching
|
|
|
|
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
|
|
|
|
// extra
|
|
|
|
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
|
|
|
|
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
|
2016-07-04 11:09:45 +02:00
|
|
|
$httpProvider.defaults.headers.common = {'Accept': 'application/json, text/javascript, */*; q=0.01'};
|
|
|
|
$httpProvider.interceptors.push('redirectInterceptor');
|
|
|
|
})
|
2016-07-05 03:06:36 +02:00
|
|
|
.service('redirectInterceptor', RedirectInterceptorService)
|
2016-07-04 11:09:45 +02:00
|
|
|
.factory('getParameterByName', getParameterByName)
|
|
|
|
.filter('dateL', localizeDate)
|
|
|
|
.filter('tr', tr);
|
|
|
|
|
2016-07-07 07:15:00 +02:00
|
|
|
RedirectInterceptorService.$inject = ['$q', '$window', '$location'];
|
2016-07-04 11:09:45 +02:00
|
|
|
|
2016-07-07 07:15:00 +02:00
|
|
|
function RedirectInterceptorService($q, $window, $location) {
|
2016-07-05 03:06:36 +02:00
|
|
|
return {
|
|
|
|
'responseError': function(rejection) {
|
2016-07-06 06:56:44 +02:00
|
|
|
var url = rejection.config.url;
|
|
|
|
console.log('url:' + url);
|
|
|
|
var exclusion = [
|
|
|
|
'/',
|
2016-08-23 05:58:07 +02:00
|
|
|
'/login',
|
2016-07-06 06:56:44 +02:00
|
|
|
'/search',
|
|
|
|
'/reset_password',
|
|
|
|
'/sign_up',
|
|
|
|
'/forgot_password',
|
|
|
|
'/api/targets/ping',
|
|
|
|
'/api/users/current',
|
|
|
|
'/api/repositories',
|
|
|
|
/^\/api\/projects\/[0-9]+\/members\/current$/
|
|
|
|
];
|
2016-07-05 03:06:36 +02:00
|
|
|
var isExcluded = false;
|
2016-07-05 07:03:18 +02:00
|
|
|
for(var i in exclusion) {
|
2016-07-06 06:56:44 +02:00
|
|
|
switch(typeof(exclusion[i])) {
|
|
|
|
case 'string':
|
|
|
|
isExcluded = (exclusion[i] === url);
|
|
|
|
break;
|
|
|
|
case 'object':
|
|
|
|
isExcluded = exclusion[i].test(url);
|
2016-07-05 03:06:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-07-06 06:56:44 +02:00
|
|
|
if(isExcluded) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!isExcluded && rejection.status === 401) {
|
2016-07-07 07:15:00 +02:00
|
|
|
$window.location.href = '/?last_url=' + encodeURIComponent(location.pathname + '#' + $location.url());
|
2016-07-06 06:56:44 +02:00
|
|
|
return;
|
2016-07-05 03:06:36 +02:00
|
|
|
}
|
|
|
|
return $q.reject(rejection);
|
|
|
|
}
|
|
|
|
};
|
2016-07-04 11:09:45 +02:00
|
|
|
}
|
2016-05-19 06:50:32 +02:00
|
|
|
|
|
|
|
function getParameterByName() {
|
|
|
|
return get;
|
|
|
|
function get(name, url) {
|
|
|
|
name = name.replace(/[\[\]]/g, "\\$&");
|
|
|
|
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
|
|
|
|
results = regex.exec(url);
|
2016-05-23 12:29:17 +02:00
|
|
|
if (!results) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!results[2]) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-05-19 06:50:32 +02:00
|
|
|
return decodeURIComponent(results[2].replace(/\+/g, " "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 12:30:01 +02:00
|
|
|
function localizeDate() {
|
|
|
|
return filter;
|
|
|
|
|
|
|
|
function filter(input, pattern) {
|
2016-06-24 12:44:54 +02:00
|
|
|
var d = new Date(input || '');
|
2016-07-11 12:38:51 +02:00
|
|
|
if(d.getTime() <= 0) {return '-';}
|
2016-06-24 12:44:54 +02:00
|
|
|
return moment(d).format(pattern);
|
2016-05-12 12:30:01 +02:00
|
|
|
}
|
2016-05-13 12:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tr.$inject = ['I18nService'];
|
|
|
|
|
|
|
|
function tr(I18nService) {
|
|
|
|
return tr;
|
2016-05-14 13:28:15 +02:00
|
|
|
function tr(label, params) {
|
2016-05-13 12:48:06 +02:00
|
|
|
var currentLanguage = I18nService().getCurrentLanguage();
|
2016-05-14 13:28:15 +02:00
|
|
|
var result = '';
|
2016-05-13 12:48:06 +02:00
|
|
|
if(label && label.length > 0){
|
2016-05-14 13:28:15 +02:00
|
|
|
result = I18nService().getValue(label, currentLanguage);
|
2016-05-13 12:48:06 +02:00
|
|
|
}
|
2016-05-14 13:28:15 +02:00
|
|
|
if(angular.isArray(params)) {
|
|
|
|
angular.forEach(params, function(value, index) {
|
|
|
|
result = result.replace('$' + index, params[index]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
2016-05-13 12:48:06 +02:00
|
|
|
}
|
2016-05-12 12:30:01 +02:00
|
|
|
}
|
2016-04-13 12:55:05 +02:00
|
|
|
|
2016-07-11 12:38:51 +02:00
|
|
|
})();
|