-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommon.js
299 lines (250 loc) · 7.72 KB
/
common.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
(function() {
'use strict';
angular.module('base.common', ['base.core'])
.directive('baClose', baClose)
.directive('baOpen', baOpen)
.directive('baToggle', baToggle)
.directive('baEscClose', baEscClose)
.directive('baSwipeClose', baSwipeClose)
.directive('baHardToggle', baHardToggle)
.directive('baCloseAll', baCloseAll)
.directive('baNoSupport', baNoSupport)
;
baClose.$inject = ['BaseAppsApi'];
function baClose(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, element, attrs) {
var targetId = '';
if (attrs.baClose) {
targetId = attrs.baClose;
} else {
var parentElement= false;
var tempElement = element.parent();
// find parent component
while(parentElement === false) {
if(tempElement[0].nodeName == 'BODY') {
parentElement = '';
}
if(typeof tempElement.attr('ba-closable') !== 'undefined' && tempElement.attr('ba-closable') !== false) {
parentElement = tempElement;
}
tempElement = tempElement.parent();
}
targetId = parentElement.attr('id');
}
element.on('click', function(e) {
BaseAppsApi.publish(targetId, 'close');
if (!_hasParentHref(e.target, targetId)) {
// prevent default if target not inside link
e.preventDefault();
}
});
}
}
baOpen.$inject = ['BaseAppsApi'];
function baOpen(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, element, attrs) {
element.on('click', function(e) {
BaseAppsApi.publish(attrs.baOpen, 'open');
if (!_hasParentHref(e.target, attrs.baOpen)) {
// prevent default if target not inside link
e.preventDefault();
}
});
}
}
baToggle.$inject = ['BaseAppsApi'];
function baToggle(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
}
return directive;
function link(scope, element, attrs) {
element.on('click', function(e) {
BaseAppsApi.publish(attrs.baToggle, 'toggle');
if (!_hasParentHref(e.target, attrs.baToggle)) {
// prevent default if target not inside link
e.preventDefault();
}
});
}
}
baEscClose.$inject = ['BaseAppsApi'];
function baEscClose(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, element, attrs) {
element.on('keyup', function(e) {
if (e.keyCode === 27) {
BaseAppsApi.closeActiveElements();
}
e.preventDefault();
});
}
}
baSwipeClose.$inject = ['BaseAppsApi'];
function baSwipeClose(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link($scope, element, attrs) {
var swipeDirection;
var hammerElem, hammerDirection;
// detect what direction the directive is pointing
switch (attrs.baSwipeClose) {
case 'right':
swipeDirection = 'swiperight';
hammerDirection = Hammer.DIRECTION_RIGHT;
break;
case 'left':
swipeDirection = 'swipeleft';
hammerDirection = Hammer.DIRECTION_LEFT;
break;
case 'up':
swipeDirection = 'swipeup';
hammerDirection = Hammer.DIRECTION_UP;
break;
case 'down':
swipeDirection = 'swipedown';
hammerDirection = Hammer.DIRECTION_DOWN;
break;
default:
swipeDirection = 'swipe';
hammerDirection = Hammer.DIRECTION_ALL;
}
if (typeof(Hammer)!=='undefined') {
hammerElem = new Hammer(element[0]);
// set the options for swipe (to make them a bit more forgiving in detection)
hammerElem.get('swipe').set({
direction: hammerDirection,
threshold: 5, // this is how far the swipe has to travel
velocity: 0.5 // and this is how fast the swipe must travel
});
hammerElem.on(swipeDirection, function() {
BaseAppsApi.publish(attrs.id, 'close');
});
}
}
}
baHardToggle.$inject = ['BaseAppsApi'];
function baHardToggle(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, element, attrs) {
element.on('click', function(e) {
BaseAppsApi.closeActiveElements({exclude: attrs.baHardToggle});
BaseAppsApi.publish(attrs.baHardToggle, 'toggle');
if (!_hasParentHref(e.target, attrs.baToggle)) {
// prevent default if target not inside link
e.preventDefault();
}
});
}
}
baCloseAll.$inject = ['BaseAppsApi'];
function baCloseAll(BaseAppsApi) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, element, attrs) {
element.on('click', function(e) {
var tar = e.target, avoid, activeElements, closedElements, i;
// check if clicked target or any of its ancestors is designated to open/close
// another component
avoid = ['ba-toggle', 'ba-hard-toggle', 'ba-open', 'ba-close'].filter(function(e){
var parentElement = tar, hasAttr = false;
while (parentElement && typeof(parentElement.getAttribute) === 'function') {
var attrVal = parentElement.getAttribute(e);
if (typeof(attrVal) !== 'undefined' && attrVal !== null) {
hasAttr = true;
break;
}
parentElement = parentElement.parentNode;
}
return hasAttr;
});
if(avoid.length > 0) {
// do nothing
return;
}
// check if clicked element is inside active closable parent
if (getParentsUntil(tar, 'ba-closable') !== false) {
// do nothing
return;
}
// close all active elements
activeElements = document.querySelectorAll('.is-active[ba-closable]');
closedElements = 0;
if(activeElements.length > 0) {
for(i = 0; i < activeElements.length; i++) {
if (!activeElements[i].hasAttribute('ba-ignore-all-close')) {
BaseAppsApi.publish(activeElements[i].id, 'close');
closedElements++;
}
}
// if one or more elements were closed and the target is not an href,
// prevent the default action
if (closedElements > 0 && !tar.href) {
e.preventDefault();
}
}
});
}
/** special thanks to Chris Ferdinandi for this solution.
* http://gomakethings.com/climbing-up-and-down-the-dom-tree-with-vanilla-javascript/
*/
function getParentsUntil(elem, parent) {
for ( ; elem && elem !== document.body; elem = elem.parentNode ) {
if(elem.hasAttribute(parent)){
if(elem.classList.contains('is-active')){ return elem; }
break;
}
}
return false;
}
}
baNoSupport.$inject = ['$window'];
function baNoSupport($window) {
var directive = {
restrict: 'A',
link: link
};
return directive;
function link(scope, element, attrs) {
if (!$window.Modernizr || $window.Modernizr.flexbox) {
element.remove();
}
}
}
function _hasParentHref(target, rootId) {
var parentElement = target, hasHref = false;
while (parentElement && parentElement.id != rootId) {
if (parentElement.href) {
hasHref = true;
break;
}
parentElement = parentElement.parentNode;
}
return hasHref;
}
})();