forked from AngularPlus/AngularPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngplus-overlay.js
230 lines (202 loc) · 9.09 KB
/
ngplus-overlay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* ngplus-overlay.js
* Version 0.9.2
* Copyright 2014 John Papa and Dan Wahlin
* All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
*
* Author: John Papa and Dan Wahlin
* Project: https://github.com/AngularPlus
*/
(function () {
'use strict';
var overlayApp = angular.module('ngplus', []);
//Empty factory to hook into $httpProvider.interceptors
//Directive will hookup request, response, and responseError interceptors
overlayApp.factory('ngplus.httpInterceptor', httpInterceptor);
function httpInterceptor () { return {} }
//Hook httpInterceptor factory into the $httpProvider interceptors so that we can monitor XHR calls
overlayApp.config(['$httpProvider', httpProvider]);
function httpProvider ($httpProvider) {
$httpProvider.interceptors.push('ngplus.httpInterceptor');
}
//Directive that uses the httpInterceptor factory above to monitor XHR calls
//When a call is made it displays an overlay and a content area
//No attempt has been made at this point to test on older browsers
overlayApp.directive('ngplusOverlay', ['$q', '$timeout', '$window', 'ngplus.httpInterceptor', '$rootScope', overlay]);
function overlay ($q, $timeout, $window, httpInterceptor, rootScope) {
var directive = {
scope: {
ngplusOverlayDelayIn: "@",
ngplusOverlayDelayOut: "@",
ngplusOverlayAnimation: "@"
},
restrict: 'EA',
transclude: true,
template: getTemplate(),
link: link
};
return directive;
function getTemplate () {
return '<div id="ngplus-overlay-container" ' +
'class="{{ngplusOverlayAnimation}}" data-ng-show="!!show">' +
'<div class="ngplus-overlay-background"></div>' +
'<div id="ngplus-overlay-content" class="ngplus-overlay-content" data-ng-transclude>' +
'</div>' +
'</div>';
}
function link (scope, element, attrs) {
var defaults = {
overlayDelayIn: 500,
overlayDelayOut: 500
};
var delayIn = scope.ngplusOverlayDelayIn ? scope.ngplusOverlayDelayIn : defaults.overlayDelayIn;
var delayOut = scope.ngplusOverlayDelayOut ? scope.ngplusOverlayDelayOut : defaults.overlayDelayOut;
var overlayContainer = null;
var queue = [];
var timerPromise = null;
var timerPromiseHide = null;
init();
var wait = 0;
rootScope.$on('showSpinner', () => {
if (++wait === 1) {
showOverlay()
}
})
rootScope.$on('hideSpinner', () => {
if (--wait === 0) {
hideOverlay()
}
})
function init() {
wireUpHttpInterceptor();
if (window.jQuery) wirejQueryInterceptor();
overlayContainer = document.getElementById('ngplus-overlay-container');
}
//Hook into httpInterceptor factory request/response/responseError functions
function wireUpHttpInterceptor() {
httpInterceptor.request = function (config) {
if (!config.hideOverlay) {
processRequest();
}
return config || $q.when(config);
};
httpInterceptor.response = function (response) {
if (response && response.config && !response.config.hideOverlay) {
processResponse();
}
return response || $q.when(response);
};
httpInterceptor.responseError = function (rejection) {
if (rejection && rejection.config && !rejection.config.hideOverlay) {
processResponse();
}
return $q.reject(rejection);
};
}
//Monitor jQuery Ajax calls in case it's used in an app
function wirejQueryInterceptor() {
$(document).ajaxSend(function(e, xhr, options) {
if (options && !options.hideOverlay) {
processRequest();
}
});
// ajax complete always gets fired, even on errors
$(document).ajaxComplete(function(e, xhr, options) {
if (options && !options.hideOverlay) {
processResponse();
}
});
}
function processRequest() {
queue.push({});
if (queue.length == 1) {
timerPromise = $timeout(function () {
if (queue.length) showOverlay();
}, delayIn); //Delay showing for 500 millis to avoid flicker
}
}
function processResponse() {
queue.pop();
if (queue.length == 0) {
//Since we don't know if another XHR request will be made, pause before
//hiding the overlay. If another XHR request comes in then the overlay
//will stay visible which prevents a flicker
timerPromiseHide = $timeout(function () {
//Make sure queue is still 0 since a new XHR request may have come in
//while timer was running
if (queue.length == 0) {
hideOverlay();
if (timerPromiseHide) $timeout.cancel(timerPromiseHide);
}
}, delayOut);
}
}
function showOverlay() {
var w = 0;
var h = 0;
if (!$window.innerWidth) {
if (!(document.documentElement.clientWidth == 0)) {
w = document.documentElement.clientWidth;
h = document.documentElement.clientHeight;
}
else {
w = document.body.clientWidth;
h = document.body.clientHeight;
}
}
else {
w = $window.innerWidth;
h = $window.innerHeight;
}
var content = document.getElementById('ngplus-overlay-content');
var contentWidth = parseInt(getComputedStyle(content, 'width').replace('px', ''));
var contentHeight = parseInt(getComputedStyle(content, 'height').replace('px', ''));
content.style.top = h / 2 - contentHeight / 2 + 'px';
content.style.left = w / 2 - contentWidth / 2 + 'px';
scope.show = true;
}
function hideOverlay() {
if (timerPromise) $timeout.cancel(timerPromise);
scope.show = false;
}
var getComputedStyle = (function () {
var func = null;
if (document.defaultView && document.defaultView.getComputedStyle) {
func = document.defaultView.getComputedStyle;
} else if (typeof (document.body.currentStyle) !== "undefined") {
func = function (element, anything) {
return element["currentStyle"];
};
} else {
// Polyfill for getComputedStyle from: https://gist.github.com/twolfson/5369885
func = function (el, prop, getComputedStyle) {
getComputedStyle = window.getComputedStyle;
// In one fell swoop
return (
// If we have getComputedStyle
getComputedStyle ?
// Query it
// TODO: From CSS-Query notes, we might need (node, null) for FF
getComputedStyle(el) :
// Otherwise, we are in IE and use currentStyle
el.currentStyle
)[
// Switch to camelCase for CSSOM
// DEV: Grabbed from jQuery
// https://github.com/jquery/jquery/blob/1.9-stable/src/css.js#L191-L194
// https://github.com/jquery/jquery/blob/1.9-stable/src/core.js#L593-L597
prop.replace(/-(\w)/gi, function (word, letter) {
return letter.toUpperCase();
})
];
};
}
return function (element, style) {
return func(element, null)[style];
};
})();
}
}
}());