This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window-leave.js
211 lines (179 loc) · 5.41 KB
/
window-leave.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
'use strict';
(function (window, angular, undefined) {
angular.module('windowLeave', [])
.directive('windowLeave', ['$window', '$document', '$timeout', function ($window, $document, $timeout) {
return {
restrict: 'A',
transclude: true,
scope: {
"delay": "=?",
"outClass": "=",
"inClass": "=",
"enable": "=?",
"dismissOn": "@",
"dismissDelay": "=?",
"leaveDirection": "@?", // 'top' | 'left' | 'right' | 'bottom' | undefined
"box": "@",
"opened": "=",
"onOpen": "&"
},
controller: ['$scope', '$element', function ($scope, $element, $attrs) {
$scope.disabledByUser = false;
$scope.displayed = false;
$scope.dismissOn = !$scope.dismissOn ? "mouseenter" : $scope.dismissOn;
$scope.config = {
delay: $scope.delay === 0 ? 0 : $scope.delay || 2000,
dismissDelay: $scope.dismissDelay === 0 ? 0 : $scope.dismissDelay || 2000,
outClass: $scope.outClass || "out",
inClass: $scope.inClass || 'in'
};
// disable mouse events, used when the windows is unfocused
function offMouse() {
$document.off('mouseenter', userReturns);
$document.off('mouseleave', userLeaves);
}
// enable mouse events when the windows is focused
function onMouse() {
if($scope.disabledByUser){
return;
}
// Event listeners on $document
$document.on('mouseenter', userReturns);
$document.on('mouseleave', userLeaves);
}
// add watcher on the enable value
$scope.$watch('enable', function (value) {
// if value is defined as false
if (value === false) {
offMouse();
}
// if value is undifined or is true
else if (!value || value === true) {
onMouse();
}
});
// When the scope is destroyed, we have to make sure to teardown
// the event binding so we don't get a leak.
$scope.$on("$destroy", handleDestroy);
// I teardown the directive.
// fires when the scope of the directive is destroyed
function handleDestroy() {
offMouse();
}
function getLeaveSide(event) {
var direction = [];
if (event.type === "blur") {
direction.push("blur");
return;
}
var vWidth = $window.innerWidth,
vHeight = $window.innerHeight;
if (event.clientX > (vWidth - 10)) {
direction.push("right");
}
else if (event.clientX < 10) {
direction.push("left");
}
if (event.clientY > (vHeight - 10)) {
direction.push("bottom");
}
else if (event.clientY < 10) {
direction.push("top");
}
return direction;
}
var leaveTimer;
var dismissTimer;
function userLeaves(event) {
$scope.direction = getLeaveSide(event);
// if leave-direction is defined and if the provided value does not match the cursor leave direction
// do not show the box
if ($scope.leaveDirection && $scope.direction.indexOf($scope.leaveDirection) === -1) {
return;
}
if (dismissTimer) {
$timeout.cancel(dismissTimer);
}
if (!$scope.displayed) {
leaveTimer = $timeout(open, $scope.config.delay);
}
}
function userReturns(event) {
if (leaveTimer && document.hasFocus()) {
$timeout.cancel(leaveTimer);
}
if ($scope.displayed) {
switch ($scope.dismissOn) {
case "click":
// dismisses on click everywhere
// and prevent default actions of elements
$document.on('click', function (e) {
dismiss();
$document.off('click');
});
break;
case "outsideBox":
dismissAfter($scope.dismissDelay);
$scope.config.box.on('mouseleave', dismissAfter);
$scope.config.box.on('mouseenter', cancelDismiss);
break;
case "mouseenter":
dismiss();
break;
case "none":
break;
}
}
}
// public method, accesible inside the directive element
$scope.dismiss = dismiss;
function open() {
$element.addClass($scope.config.outClass)
.removeClass($scope.config.inClass)
.addClass($scope.direction.join(' '));
$scope.displayed = true;
if (typeof $scope.onOpen !== "undefined") {
$scope.onOpen();
}
$scope.opened = true;
$scope.$apply();
}
function dismiss(method) {
switch (method){
case 'disable':
$scope.enable = false;
$scope.disabledByUser = true;
offMouse();
break;
}
$element.addClass($scope.config.inClass)
.removeClass($scope.config.outClass)
.removeClass("top bottom right left blur");
$scope.displayed = false;
$scope.opened = false;
}
// init the dismiss timer
function dismissAfter() {
if (dismissTimer) {
$timeout.cancel(dismissTimer);
}
dismissTimer = $timeout(function () {
dismiss();
}, $scope.config.dismissDelay);
}
// remove dismiss timer
function cancelDismiss() {
if (dismissTimer) {
$timeout.cancel(dismissTimer);
}
}
}],
link: function ($scope, $element, $attrs, $ctrl, transclude) {
transclude($scope.$new(), function (clone) {
$element.append(clone);
$scope.config.box = $scope.box ? angular.element($element[0].querySelector($scope.box)) : null;
});
}
}
}]);
})(window, window.angular);