UNPKG

3.42 kBJavaScriptView Raw
1'use strict';
2
3var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4
5var _ngModule = require('../ngModule');
6
7var _ngModule2 = _interopRequireDefault(_ngModule);
8
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
11function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
13_ngModule2.default.factory('blockHttpInterceptor', function ($q, $injector, avBlockConfig, $templateCache, $log) {
14 var BlockHttpInterceptor = function () {
15 function BlockHttpInterceptor() {
16 _classCallCheck(this, BlockHttpInterceptor);
17
18 this.avBlockManager = null;
19 }
20
21 _createClass(BlockHttpInterceptor, [{
22 key: 'injectBlock',
23 value: function injectBlock() {
24 this.avBlockManager = this.avBlockManager || $injector.get('avBlockManager');
25 }
26 }, {
27 key: 'stopBlock',
28 value: function stopBlock(config) {
29 if (avBlockConfig.autoBlock && config && !config.$_noBlock && config.$_blocks) {
30 this.injectBlock();
31 config.$_blocks.stop();
32 }
33 }
34 }, {
35 key: 'requestError',
36 value: function requestError(rejection) {
37
38 try {
39 this.stopBlock(rejection.config);
40 } catch (ex) {
41 $log.error('httpRequestError', ex);
42 }
43
44 return $q.reject(rejection);
45 }
46 }, {
47 key: 'request',
48 value: function request(config) {
49
50 // Only block when autoBlock is enabled ...
51 // ... and the request doesn't match a cached template.
52
53 if (avBlockConfig.autoBlock && !(config.method === 'GET' && $templateCache.get(config.url))) {
54
55 // Don't block excluded requests
56
57 var result = avBlockConfig.requestFilter(config);
58
59 if (result === false) {
60 // Tag the config so we don't unblock this request
61 config.$_noBlock = true;
62 } else {
63
64 this.injectBlock();
65
66 config.$_blocks = this.avBlockManager.locate(config);
67 config.$_blocks.start(result);
68 }
69 }
70
71 return config;
72 }
73 }, {
74 key: 'requestError',
75 value: function requestError() {
76 this.error.apply(this, arguments);
77 }
78 }, {
79 key: 'response',
80 value: function response(_response) {
81
82 // If the connection to the website goes down the response interceptor gets and error with 'cannot read property config of null'.
83 // https://github.com/McNull/angular-block-ui/issues/53
84
85 if (_response) {
86 this.stopBlock(_response.config);
87 }
88
89 return _response;
90 }
91 }, {
92 key: 'responseError',
93 value: function responseError() {
94 this.error.apply(this, arguments);
95 }
96 }]);
97
98 return BlockHttpInterceptor;
99 }();
100
101 return new BlockHttpInterceptor();
102});
\No newline at end of file