199d199f3SIftekharul Islam/**
299d199f3SIftekharul Islam * api Interceptor
399d199f3SIftekharul Islam *
499d199f3SIftekharul Islam * @module app/common/services/apiInterceptor
599d199f3SIftekharul Islam * @exports apiInterceptor
699d199f3SIftekharul Islam * @name apiInterceptor
799d199f3SIftekharul Islam
899d199f3SIftekharul Islam * @version 0.0.1
999d199f3SIftekharul Islam */
1099d199f3SIftekharul Islam
1199d199f3SIftekharul Islamwindow.angular && (function (angular) {
1299d199f3SIftekharul Islam    'use strict';
1399d199f3SIftekharul Islam
1499d199f3SIftekharul Islam    angular
1599d199f3SIftekharul Islam        .module('app.common.services')
1699d199f3SIftekharul Islam        .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){
1799d199f3SIftekharul Islam            return {
1899d199f3SIftekharul Islam                'request': function(config){
1999d199f3SIftekharul Islam                    dataService.loading = true;
20cd789508SIftekharul Islam                    config.timeout = 10000;
2199d199f3SIftekharul Islam                    return config;
2299d199f3SIftekharul Islam                },
2399d199f3SIftekharul Islam                'response': function(response){
2499d199f3SIftekharul Islam                    dataService.loading = false;
2599d199f3SIftekharul Islam
26cd789508SIftekharul Islam                    //not interested in template requests
27cd789508SIftekharul Islam                    if(!/^https?\:/i.test(response.config.url)){
28cd789508SIftekharul Islam                        return response;
29cd789508SIftekharul Islam                    }
30cd789508SIftekharul Islam
31cd789508SIftekharul Islam                    dataService.last_updated = new Date();
32*c1535926SIftekharul Islam                    if(!response){
3399d199f3SIftekharul Islam                        dataService.server_unreachable = true;
34cd789508SIftekharul Islam                    }else{
35cd789508SIftekharul Islam                        dataService.server_unreachable = false;
3699d199f3SIftekharul Islam                    }
3799d199f3SIftekharul Islam
3899d199f3SIftekharul Islam                    if(response && response.status == 'error' &&
3999d199f3SIftekharul Islam                       dataService.path != '/login'){
4099d199f3SIftekharul Islam                        $rootScope.$emit('timedout-user', {});
4199d199f3SIftekharul Islam                    }
4299d199f3SIftekharul Islam
4399d199f3SIftekharul Islam                    return response;
4499d199f3SIftekharul Islam                },
4599d199f3SIftekharul Islam                'responseError': function(rejection){
4699d199f3SIftekharul Islam                    dataService.server_unreachable = true;
4799d199f3SIftekharul Islam                    dataService.loading = false;
48f3f7a5f0SIftekharul Islam                    if(dataService.path != '/login'){
49f3f7a5f0SIftekharul Islam                        $rootScope.$emit('timedout-user', {});
50f3f7a5f0SIftekharul Islam                    }
5199d199f3SIftekharul Islam                    return $q.reject(rejection);
5299d199f3SIftekharul Islam                }
5399d199f3SIftekharul Islam            };
5499d199f3SIftekharul Islam        }]);
5599d199f3SIftekharul Islam
5699d199f3SIftekharul Islam})(window.angular);