-
Notifications
You must be signed in to change notification settings - Fork 0
/
ayrUtils.js
138 lines (113 loc) · 4.37 KB
/
ayrUtils.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
/**************************************/
/* Helper Functions for AYR Projects
/***************************************************/
var ayrApi = (function () {
var api = {};
api.isHome = function( page ){
if(page === '' || page === null || page === 'index'){
console.log('isHome - page: ', page);
return true;
}
console.log('isHome - not home: ');
};
api.isMobile = function(){
var maxWidth = 768
, iPadDevice = null != navigator.userAgent.match(/iPad/i)
, w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
return w < maxWidth || iPadDevice ? !0 : !1
};
api.toTop = function(e){
targetOffset = document.querySelector('body');
currentPosition = api.getPageScroll();
targetOffset.classList.add('in-transition');
targetOffset.style.WebkitTransform = "translate(0, -" + (targetOffset - currentPosition) + "px)";
targetOffset.style.MozTransform = "translate(0, -" + (targetOffset - currentPosition) + "px)";
targetOffset.style.transform = "translate(0, -" + (targetOffset - currentPosition) + "px)";
window.setTimeout(function () {
targetOffset.classList.remove('in-transition');
targetOffset.style.cssText = "";
window.scrollTo(0, targetOffset);
}, 0);
};
api.getPageScroll = function() {
var yScroll;
if (window.pageYOffset) {
yScroll = window.pageYOffset;
} else if (document.documentElement && document.documentElement.scrollTop) {
yScroll = document.documentElement.scrollTop;
} else if (document.body) {
yScroll = document.body.scrollTop;
}
return yScroll;
};
api.hasClass = function(el, className) {
return el.classList ? el.classList.contains(className) : new RegExp('\\b'+ className+'\\b').test(el.className);
};
api.insertAfter = function(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
};
api.getAjax = function(url, success) {
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if (xhr.readyState>3 && xhr.status==200) success(xhr.responseText);
};
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.send();
return xhr;
};
api.getClosest = function ( elem, selector ) {
// Variables
var firstChar = selector.charAt(0);
var supports = 'classList' in document.documentElement;
var attribute, value;
// If selector is a data attribute, split attribute from value
if ( firstChar === '[' ) {
selector = selector.substr( 1, selector.length - 2 );
attribute = selector.split( '=' );
if ( attribute.length > 1 ) {
value = true;
attribute[1] = attribute[1].replace( /"/g, '' ).replace( /'/g, '' );
}
}
// Get closest match
for ( ; elem && elem !== document && elem.nodeType === 1; elem = elem.parentNode ) {
// If selector is a class
if ( firstChar === '.' ) {
if ( supports ) {
if ( elem.classList.contains( selector.substr(1) ) ) {
return elem;
}
} else {
if ( new RegExp('(^|\\s)' + selector.substr(1) + '(\\s|$)').test( elem.className ) ) {
return elem;
}
}
}
// If selector is an ID
if ( firstChar === '#' ) {
if ( elem.id === selector.substr(1) ) {
return elem;
}
}
// If selector is a data attribute
if ( firstChar === '[' ) {
if ( elem.hasAttribute( attribute[0] ) ) {
if ( value ) {
if ( elem.getAttribute( attribute[0] ) === attribute[1] ) {
return elem;
}
} else {
return elem;
}
}
}
// If selector is a tag
if ( elem.tagName.toLowerCase() === selector ) {
return elem;
}
}
return null;
};
return api;
})();